From a55c0421b2c38ac3a7e2f7022309377bddc54e8d Mon Sep 17 00:00:00 2001 From: Scoopta Date: Sat, 29 Feb 2020 02:27:59 -0800 Subject: [PATCH] gdk_threads_add_timeout() is now used instead of g_timeout_add() --- inc/wofi.h | 2 ++ src/main.c | 40 ++++++++++++++++++++++++++------------- src/wofi.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/inc/wofi.h b/inc/wofi.h index 1981551..978d50e 100644 --- a/inc/wofi.h +++ b/inc/wofi.h @@ -42,4 +42,6 @@ #include void wofi_init(struct map* config); + +void wofi_load_css(bool nyan); #endif diff --git a/src/main.c b/src/main.c index 4701b14..d803feb 100644 --- a/src/main.c +++ b/src/main.c @@ -27,12 +27,16 @@ #include #include +static const char* nyan_colors[] = {"#FF0000", "#FFA500", "#FFFF00", "#00FF00", "#0000FF", "#FF00FF"}; +static size_t nyan_color_l = sizeof(nyan_colors) / sizeof(char*); + static char* CONFIG_LOCATION; static char* COLORS_LOCATION; static struct map* config; static char* config_path; static char* stylesheet; static char* color_path; +static uint8_t nyan_shift = 0; struct option_node { char* option; @@ -86,7 +90,7 @@ static void print_usage(char** argv) { exit(0); } -static void load_css(void) { +void wofi_load_css(bool nyan) { if(access(stylesheet, R_OK) == 0) { FILE* file = fopen(stylesheet, "r"); fseek(file, 0, SEEK_END); @@ -103,20 +107,30 @@ static void load_css(void) { struct wl_list link; }; wl_list_init(&lines); - if(access(color_path, R_OK) == 0) { - file = fopen(color_path, "r"); - char* line = NULL; - size_t line_size = 0; - ssize_t line_l = 0; - while((line_l = getline(&line, &line_size, file)) != -1) { + if(nyan) { + for(ssize_t count = nyan_shift; count < 32 + nyan_shift; ++count) { + size_t i = count % nyan_color_l; struct node* entry = malloc(sizeof(struct node)); - line[line_l - 1] = 0; - entry->line = malloc(line_l + 1); - strcpy(entry->line, line); + entry->line = strdup(nyan_colors[i]); wl_list_insert(&lines, &entry->link); } - fclose(file); - free(line); + nyan_shift = (nyan_shift + 1) % nyan_color_l; + } else { + if(access(color_path, R_OK) == 0) { + file = fopen(color_path, "r"); + char* line = NULL; + size_t line_size = 0; + ssize_t line_l = 0; + while((line_l = getline(&line, &line_size, file)) != -1) { + struct node* entry = malloc(sizeof(struct node)); + line[line_l - 1] = 0; + entry->line = malloc(line_l + 1); + strcpy(entry->line, line); + wl_list_insert(&lines, &entry->link); + } + fclose(file); + free(line); + } } ssize_t count = wl_list_length(&lines) - 1; @@ -706,7 +720,7 @@ int main(int argc, char** argv) { gtk_init(&argc, &argv); - load_css(); + wofi_load_css(false); wofi_init(config); gtk_main(); diff --git a/src/wofi.c b/src/wofi.c index 8a325ad..e4d6088 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -72,6 +72,8 @@ static int64_t max_height = 0; static uint64_t lines; static int8_t line_wrap; static int64_t ix, iy; +static uint8_t konami_cycle; +static bool is_konami = false; static char* key_up, *key_down, *key_left, *key_right, *key_forward, *key_backward, *key_submit, *key_exit; static char* mod_up, *mod_down, *mod_left, *mod_right, *mod_forward, *mod_backward, *mod_exit; @@ -1025,12 +1027,63 @@ static bool has_mod(guint state) { return (state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK || (state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK; } +static gboolean do_nyan(gpointer data) { + (void) data; + wofi_load_css(true); + return G_SOURCE_CONTINUE; +} + +static guint get_konami_key(uint8_t cycle) { + switch(cycle) { + case 0: + return GDK_KEY_Up; + case 1: + return GDK_KEY_Up; + case 2: + return GDK_KEY_Down; + case 3: + return GDK_KEY_Down; + case 4: + return GDK_KEY_Left; + case 5: + return GDK_KEY_Right; + case 6: + return GDK_KEY_Left; + case 7: + return GDK_KEY_Right; + case 8: + return GDK_KEY_b; + case 9: + return GDK_KEY_a; + case 10: + return GDK_KEY_Return; + default: + return GDK_KEY_VoidSymbol; + } +} + static gboolean key_press(GtkWidget* widget, GdkEvent* event, gpointer data) { (void) widget; (void) data; gchar* name = gdk_keyval_name(event->key.keyval); bool printable = strlen(name) == 1 && isprint(name[0]) && !has_mod(event->key.state); + guint konami_key = get_konami_key(konami_cycle); + if(event->key.keyval == konami_key) { + if(konami_cycle == 10) { + konami_cycle = 0; + if(!is_konami) { + is_konami = true; + gdk_threads_add_timeout(500, do_nyan, NULL); + } + return TRUE; + } else { + ++konami_cycle; + } + } else { + konami_cycle = 0; + } + if(gtk_widget_has_focus(entry) && printable) { return FALSE; } @@ -1418,7 +1471,7 @@ void wofi_init(struct map* _config) { g_signal_connect(window, "focus-in-event", G_CALLBACK(focus), NULL); g_signal_connect(window, "focus-out-event", G_CALLBACK(focus), NULL); - g_timeout_add(filter_rate, do_search, NULL); + gdk_threads_add_timeout(filter_rate, do_search, NULL); pthread_t thread; pthread_create(&thread, NULL, start_thread, mode);