From 72533577b61f431306278d088c7b6d8620198272 Mon Sep 17 00:00:00 2001 From: Kasper Seweryn Date: Wed, 8 Jan 2020 20:25:35 +0100 Subject: [PATCH] Add pywal support Add css to config Fixes #10 --- CMakeLists.txt | 2 +- README.md | 2 +- assets/main.css | 29 ++++++++++++++++++++++++----- docs/project.md | 5 +++-- src/colors.c | 27 +++++++++++++++++++++++++++ src/colors.h | 12 ++++++++++++ src/config.c | 12 +++++++----- src/config.h | 1 - src/main.c | 10 +++++++--- src/utils.c | 13 +++++++++++++ src/utils.h | 5 +++++ 11 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 src/colors.c create mode 100644 src/colors.h diff --git a/CMakeLists.txt b/CMakeLists.txt index eca8a53..cc84eb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,5 +23,5 @@ link_directories(${GTK3_LIBRARY_DIRS}) add_definitions(${GTK3_CFLAGS_OTHER}) #add_definitions(${WAYLAND_DEFINITIONS}) -add_executable(waffy src/main.c src/utils.h src/utils.c src/find_desktop.c src/find_desktop.h src/desktop_entry.c src/desktop_entry.h src/filter.c src/filter.h src/config.c src/config.h) +add_executable(waffy src/main.c src/utils.h src/utils.c src/find_desktop.c src/find_desktop.h src/desktop_entry.c src/desktop_entry.h src/filter.c src/filter.h src/config.c src/config.h src/colors.c src/colors.h) target_link_libraries(waffy ${GTK3_LAYER_SHELL_LIBRARIES} ${WAYLAND_LIBRARIES} ${GTK3_LIBRARIES}) diff --git a/README.md b/README.md index 645b672..0d9d3e7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Wayland compatible, touch friendly application launcher - Fuzzy find apps ## Requirements -- [gtk-layer-shell](https://github.com/wmww/gtk-layer-shell/) - A library to write GTK applications that use Layer Shell +- [gtk-layer-shell](https://github.com/wmww/gtk-layer-shell) - A library to write GTK applications that use Layer Shell ## Other information - [Studies project info](/docs/project.md) diff --git a/assets/main.css b/assets/main.css index d5a345d..e21668c 100644 --- a/assets/main.css +++ b/assets/main.css @@ -1,6 +1,3 @@ -window { - background: rgba(0, 0, 0, .7); -} label { color: #fff; @@ -28,11 +25,33 @@ textview text { } #apps > button { - background: rgba(255, 255, 255, .1); min-width: 270px; transition: none; + border: none; +} + +/* default colors */ +window { + background: alpha(#000, .7); +} + +#apps > button { + background: alpha(#fff, .1); } #apps > button.active { - background: rgba(255, 255, 255, .3); + background: alpha(#fff, .3); +} + +/* pywal colors */ +window.pywal { + background: alpha(@background, .7); +} + +window.pywal #apps > button { + background: @background; +} + +window.pywal #apps > button.active { + background: @color5; } diff --git a/docs/project.md b/docs/project.md index 06c398f..7f9569c 100644 --- a/docs/project.md +++ b/docs/project.md @@ -11,9 +11,10 @@ Year | 2019 / 2020 Everything is inside the `*.h` files - [Structures example](/src/desktop_entry.h#L14) -- [File operation example](/src/config.c#L10) +- [File operation example](/src/config.c#L18) - [List example](/src/desktop_entry.h#L28) -- [Functions example](/src/utils.c#L32) +- [Function example](/src/utils.c#L36) +- [Recursive function example](/src/config.c#L59) ### Open source Well it turned out that I've found a bug in sway which is my main wayland client diff --git a/src/colors.c b/src/colors.c new file mode 100644 index 0000000..acc4283 --- /dev/null +++ b/src/colors.c @@ -0,0 +1,27 @@ +// +// Created by waff on 1/8/20. +// + +#include "colors.h" +#include "config.h" + +char* get_css () { + open_config(); + + char* css_file = str_concat(get_user_home(), "/.config/waffy/main.css"); + + FILE* fp = fopen(css_file, "r"); + if (fp == NULL) { + fp = fopen(css_file, "w"); + fprintf(fp, "%s", read_file("../assets/main.css")); + } + + fclose(fp); + + if (config_wal == 1) { + char* wal_file = str_concat(get_user_home(), "/.cache/wal/colors-waybar.css"); + return str_concat(read_file(wal_file), read_file(css_file)); + } + + return read_file(css_file); +} \ No newline at end of file diff --git a/src/colors.h b/src/colors.h new file mode 100644 index 0000000..0ca5dd9 --- /dev/null +++ b/src/colors.h @@ -0,0 +1,12 @@ +// +// Created by waff on 1/8/20. +// + +#ifndef WAFFY_COLORS_H +#define WAFFY_COLORS_H + +#include "utils.h" + +char* get_css (); + +#endif //WAFFY_COLORS_H diff --git a/src/config.c b/src/config.c index e00bf01..95f42a9 100644 --- a/src/config.c +++ b/src/config.c @@ -4,6 +4,8 @@ #include "config.h" +char* config_dir = NULL; + // Default config long config_columns = 4; long config_wal = 0; @@ -14,6 +16,10 @@ void get_str(char *value, char** res); void get_bool(char* value, long* res); void open_config() { + if (config_dir != NULL) return; + config_dir = str_concat(get_user_home(), "/.config/waffy"); + mkdir(config_dir, 0755); + char* file = str_concat(get_user_home(), "/.config/waffy/config"); FILE* fp = fopen(file, "r"); @@ -84,11 +90,7 @@ void get_long(char *value, long *res) { } void write_config() { - char* dir = str_concat(get_user_home(), "/.config/waffy"); - char* file = str_concat(dir, "/config"); - - mkdir(dir, 0755); - + char* file = str_concat(config_dir, "/config"); FILE* fp = fopen(file, "w"); fprintf(fp, "columns %ld\n", config_columns); diff --git a/src/config.h b/src/config.h index a463b02..5245cb0 100644 --- a/src/config.h +++ b/src/config.h @@ -8,7 +8,6 @@ #define MAX_CONFIG_LINE_LENGTH 256 #include -#include #include "utils.h" long config_columns; diff --git a/src/main.c b/src/main.c index 662faa8..82afb8f 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include "find_desktop.h" #include "filter.h" #include "config.h" +#include "colors.h" #define ICON_SIZE 48 #define MAX_CHARS 16 @@ -358,11 +359,14 @@ int main (int argc, char* argv[]) { update_apps(all_desktop_entries); - // Set window style - // TODO: Respect pywal GtkCssProvider* css_provider = gtk_css_provider_new(); - if (gtk_css_provider_load_from_path(css_provider, "../assets/main.css", NULL)) { + if (config_wal == 1) { + add_class(window, "pywal"); + } + + char* css = get_css(); + if (gtk_css_provider_load_from_data(css_provider, css, strlen(css), NULL)) { gtk_style_context_add_provider_for_screen(gtk_widget_get_screen(window), GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_USER); diff --git a/src/utils.c b/src/utils.c index c8d2000..76cf151 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,6 +4,19 @@ #include "utils.h" +char *read_file(const char *path) { + FILE* fp = fopen(path, "r"); + if (fp == NULL) return ""; + + size_t len; + char* res = NULL; + ssize_t bytes_read = getdelim(&res, &len, '\0', fp); + fclose(fp); + + if (bytes_read == -1) return ""; + return res; +} + int str_starts_with (const char *hay, const char *needle) { return strncmp(needle, hay, strlen(needle)) == 0; } diff --git a/src/utils.h b/src/utils.h index 0a51cbe..3f8f53e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -11,6 +11,11 @@ #include #include #include +#include +#include + +// Read whole file into a string +char* read_file (const char* path); // Removes whitespaces from start and end of string char* str_trim (const char* str);