2019-08-17 21:19:32 -04:00
|
|
|
/*
|
2022-05-02 04:02:09 -04:00
|
|
|
* Copyright (C) 2019-2022 Scoopta
|
2019-08-17 21:19:32 -04:00
|
|
|
* This file is part of Wofi
|
|
|
|
* Wofi is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Wofi is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wofi.h>
|
2020-03-26 02:05:46 -04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2020-11-05 00:10:57 -05:00
|
|
|
#include <sys/wait.h>
|
2020-03-26 02:05:46 -04:00
|
|
|
|
|
|
|
#include <utils.h>
|
|
|
|
#include <config.h>
|
2020-07-11 20:39:09 -04:00
|
|
|
#include <utils_g.h>
|
2020-03-26 02:05:46 -04:00
|
|
|
#include <property_box.h>
|
2020-06-23 01:45:51 -04:00
|
|
|
#include <widget_builder.h>
|
2020-03-26 02:05:46 -04:00
|
|
|
|
|
|
|
#include <xdg-output-unstable-v1-client-protocol.h>
|
|
|
|
#include <wlr-layer-shell-unstable-v1-client-protocol.h>
|
|
|
|
|
|
|
|
#include <pango/pango.h>
|
|
|
|
#include <gdk/gdkwayland.h>
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2022-05-15 01:03:28 -04:00
|
|
|
#define PROTO_VERSION(v1, v2) (v1 < v2 ? v1 : v2)
|
2022-06-07 09:46:45 -04:00
|
|
|
#define MAX_MULTI_CONTAINS_FILTER_SIZE 256
|
2022-05-15 01:03:28 -04:00
|
|
|
|
2022-05-02 04:02:09 -04:00
|
|
|
static const char* terminals[] = {"kitty", "termite", "alacritty", "foot", "gnome-terminal", "weston-terminal"};
|
2019-09-27 02:24:48 -04:00
|
|
|
|
2019-11-21 04:01:27 -05:00
|
|
|
enum matching_mode {
|
|
|
|
MATCHING_MODE_CONTAINS,
|
2022-06-07 09:46:45 -04:00
|
|
|
MATCHING_MODE_MULTI_CONTAINS,
|
2019-11-21 04:01:27 -05:00
|
|
|
MATCHING_MODE_FUZZY
|
|
|
|
};
|
|
|
|
|
2020-03-11 20:44:08 -04:00
|
|
|
enum location {
|
2020-01-06 03:28:57 -05:00
|
|
|
LOCATION_CENTER,
|
|
|
|
LOCATION_TOP_LEFT,
|
|
|
|
LOCATION_TOP,
|
|
|
|
LOCATION_TOP_RIGHT,
|
|
|
|
LOCATION_RIGHT,
|
|
|
|
LOCATION_BOTTOM_RIGHT,
|
|
|
|
LOCATION_BOTTOM,
|
|
|
|
LOCATION_BOTTOM_LEFT,
|
|
|
|
LOCATION_LEFT
|
|
|
|
};
|
|
|
|
|
2020-02-07 21:04:37 -05:00
|
|
|
enum sort_order {
|
|
|
|
SORT_ORDER_DEFAULT,
|
|
|
|
SORT_ORDER_ALPHABETICAL
|
|
|
|
};
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
static uint64_t width, height;
|
2020-01-06 03:28:57 -05:00
|
|
|
static char* x, *y;
|
2020-05-15 20:32:39 -04:00
|
|
|
static struct zwlr_layer_shell_v1* shell = NULL;
|
2019-08-19 20:34:12 -04:00
|
|
|
static GtkWidget* window, *outer_box, *scroll, *entry, *inner_box, *previous_selection = NULL;
|
2020-02-07 14:51:46 -05:00
|
|
|
static gchar* filter = NULL;
|
2019-11-04 23:56:40 -05:00
|
|
|
static char* mode = NULL;
|
2019-08-28 03:58:45 -04:00
|
|
|
static bool allow_images, allow_markup;
|
2019-08-28 20:36:20 -04:00
|
|
|
static uint64_t image_size;
|
2019-08-28 23:14:38 -04:00
|
|
|
static char* cache_file = NULL;
|
2019-09-13 03:25:25 -04:00
|
|
|
static char* config_dir;
|
2019-11-19 01:16:06 -05:00
|
|
|
static bool mod_shift;
|
2019-11-19 01:13:14 -05:00
|
|
|
static bool mod_ctrl;
|
2019-09-27 02:24:48 -04:00
|
|
|
static char* terminal;
|
2019-10-13 01:24:35 -04:00
|
|
|
static GtkOrientation outer_orientation;
|
2019-11-04 20:46:39 -05:00
|
|
|
static bool exec_search;
|
2019-11-04 23:56:40 -05:00
|
|
|
static struct map* modes;
|
2019-11-21 04:01:27 -05:00
|
|
|
static enum matching_mode matching;
|
2019-11-25 17:56:46 -05:00
|
|
|
static bool insensitive;
|
2019-12-16 21:36:21 -05:00
|
|
|
static bool parse_search;
|
2020-01-07 21:56:57 -05:00
|
|
|
static GtkAlign content_halign;
|
2019-12-02 01:58:30 -05:00
|
|
|
static struct map* config;
|
2020-03-11 20:44:08 -04:00
|
|
|
static enum location location;
|
2020-01-10 17:34:49 -05:00
|
|
|
static bool no_actions;
|
2020-02-06 21:22:50 -05:00
|
|
|
static uint64_t columns;
|
2020-02-07 21:04:37 -05:00
|
|
|
static bool user_moved = false;
|
2020-05-08 17:53:44 -04:00
|
|
|
static uint32_t widget_count = 0;
|
2020-02-07 21:04:37 -05:00
|
|
|
static enum sort_order sort_order;
|
2020-02-25 03:30:35 -05:00
|
|
|
static int64_t max_height = 0;
|
2020-06-16 18:41:28 -04:00
|
|
|
static uint32_t lines, max_lines;
|
2020-02-21 03:43:44 -05:00
|
|
|
static int8_t line_wrap;
|
2020-02-25 20:13:32 -05:00
|
|
|
static int64_t ix, iy;
|
2020-02-29 05:27:59 -05:00
|
|
|
static uint8_t konami_cycle;
|
|
|
|
static bool is_konami = false;
|
2020-05-09 16:16:53 -04:00
|
|
|
static GDBusProxy* dbus = NULL;
|
2020-05-18 23:16:57 -04:00
|
|
|
static GdkRectangle resolution = {0};
|
2020-06-14 04:24:37 -04:00
|
|
|
static bool resize_expander = false;
|
2020-06-16 18:41:28 -04:00
|
|
|
static uint32_t line_count = 0;
|
|
|
|
static bool dynamic_lines;
|
2020-06-23 03:33:57 -04:00
|
|
|
static struct wl_list mode_list;
|
|
|
|
static pthread_t mode_thread;
|
2020-10-02 23:54:36 -04:00
|
|
|
static bool has_joined_mode = false;
|
2020-11-05 00:10:57 -05:00
|
|
|
static char* copy_exec = NULL;
|
2021-10-29 15:34:49 -04:00
|
|
|
static char* pre_display_cmd = NULL;
|
2020-02-21 03:17:59 -05:00
|
|
|
|
2020-04-11 00:50:48 -04:00
|
|
|
static struct map* keys;
|
2020-02-08 21:04:38 -05:00
|
|
|
|
|
|
|
static struct wl_display* wl = NULL;
|
|
|
|
static struct wl_surface* wl_surface;
|
2020-02-25 20:13:32 -05:00
|
|
|
static struct wl_list outputs;
|
|
|
|
static struct zxdg_output_manager_v1* output_manager;
|
2020-02-08 21:04:38 -05:00
|
|
|
static struct zwlr_layer_surface_v1* wlr_surface;
|
2019-08-28 00:36:44 -04:00
|
|
|
|
2020-02-25 20:13:32 -05:00
|
|
|
struct output_node {
|
|
|
|
char* name;
|
|
|
|
struct wl_output* output;
|
|
|
|
int32_t width, height, x, y;
|
|
|
|
struct wl_list link;
|
|
|
|
};
|
|
|
|
|
2020-04-11 00:50:48 -04:00
|
|
|
struct key_entry {
|
|
|
|
char* mod;
|
|
|
|
void (*action)(void);
|
|
|
|
};
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
static void nop() {}
|
|
|
|
|
|
|
|
static void add_interface(void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
|
|
|
|
(void) data;
|
|
|
|
if(strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
|
2022-05-15 01:03:28 -04:00
|
|
|
shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, PROTO_VERSION(version, 4));
|
2020-02-25 20:13:32 -05:00
|
|
|
} else if(strcmp(interface, wl_output_interface.name) == 0) {
|
|
|
|
struct output_node* node = malloc(sizeof(struct output_node));
|
2022-05-15 01:03:28 -04:00
|
|
|
node->output = wl_registry_bind(registry, name, &wl_output_interface, PROTO_VERSION(version, 4));
|
2020-02-25 20:13:32 -05:00
|
|
|
wl_list_insert(&outputs, &node->link);
|
|
|
|
} else if(strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
|
2022-05-15 01:03:28 -04:00
|
|
|
output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, PROTO_VERSION(version, 3));
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 21:04:04 -04:00
|
|
|
static void config_surface(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) {
|
2019-08-17 21:19:32 -04:00
|
|
|
(void) data;
|
2020-10-10 21:04:04 -04:00
|
|
|
(void) width;
|
|
|
|
(void) height;
|
2019-08-17 21:19:32 -04:00
|
|
|
zwlr_layer_surface_v1_ack_configure(surface, serial);
|
2020-10-10 21:04:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_surface(struct zwlr_layer_surface_v1* surface) {
|
2019-08-17 21:19:32 -04:00
|
|
|
zwlr_layer_surface_v1_set_size(surface, width, height);
|
|
|
|
zwlr_layer_surface_v1_set_keyboard_interactivity(surface, true);
|
2020-01-06 03:28:57 -05:00
|
|
|
|
|
|
|
if(location > 8) {
|
|
|
|
location -= 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x != NULL || y != NULL) {
|
|
|
|
if(location == LOCATION_CENTER) {
|
|
|
|
location = LOCATION_TOP_LEFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
zwlr_layer_surface_v1_set_margin(surface, iy, -ix, -iy, ix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(location > 0) {
|
2020-01-06 03:40:56 -05:00
|
|
|
enum zwlr_layer_surface_v1_anchor anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
|
2020-01-06 03:28:57 -05:00
|
|
|
|
|
|
|
switch(location) {
|
|
|
|
case LOCATION_CENTER:
|
|
|
|
break;
|
|
|
|
case LOCATION_TOP_LEFT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
|
|
|
|
break;
|
|
|
|
case LOCATION_TOP:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
|
|
|
|
break;
|
|
|
|
case LOCATION_TOP_RIGHT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
break;
|
|
|
|
case LOCATION_LEFT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
|
|
|
|
break;
|
|
|
|
case LOCATION_RIGHT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
break;
|
|
|
|
case LOCATION_BOTTOM_LEFT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
|
|
|
|
break;
|
|
|
|
case LOCATION_BOTTOM:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
|
|
|
|
break;
|
|
|
|
case LOCATION_BOTTOM_RIGHT:
|
|
|
|
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
zwlr_layer_surface_v1_set_anchor(surface, anchor);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:51:46 -05:00
|
|
|
static gboolean do_search(gpointer data) {
|
2019-08-27 00:19:59 -04:00
|
|
|
(void) data;
|
2020-02-07 14:51:46 -05:00
|
|
|
const gchar* new_filter = gtk_entry_get_text(GTK_ENTRY(entry));
|
|
|
|
if(filter == NULL || strcmp(new_filter, filter) != 0) {
|
|
|
|
if(filter != NULL) {
|
|
|
|
free(filter);
|
|
|
|
}
|
|
|
|
filter = strdup(new_filter);
|
2019-08-28 22:33:08 -04:00
|
|
|
gtk_flow_box_invalidate_filter(GTK_FLOW_BOX(inner_box));
|
2019-11-21 04:01:27 -05:00
|
|
|
gtk_flow_box_invalidate_sort(GTK_FLOW_BOX(inner_box));
|
2020-02-08 01:04:15 -05:00
|
|
|
GtkFlowBoxChild* child = gtk_flow_box_get_child_at_index(GTK_FLOW_BOX(inner_box), 0);
|
|
|
|
if(child != NULL) {
|
|
|
|
gtk_flow_box_select_child(GTK_FLOW_BOX(inner_box), child);
|
|
|
|
}
|
2019-08-27 00:19:59 -04:00
|
|
|
}
|
2020-02-07 14:51:46 -05:00
|
|
|
return G_SOURCE_CONTINUE;
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 02:54:12 -04:00
|
|
|
static void get_img_data(char* original, char* str, struct map* mode_map, bool first, char** mode, char** data) {
|
|
|
|
char* colon = strchr(str, ':');
|
|
|
|
if(colon == NULL) {
|
|
|
|
if(first) {
|
|
|
|
*mode = "text";
|
|
|
|
*data = str;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
*mode = NULL;
|
|
|
|
*data = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*colon = 0;
|
|
|
|
|
|
|
|
if(map_contains(mode_map, str)) {
|
|
|
|
if(original != str) {
|
|
|
|
*(str - 1) = 0;
|
|
|
|
}
|
|
|
|
*mode = str;
|
|
|
|
*data = colon + 1;
|
|
|
|
} else if(first) {
|
|
|
|
*colon = ':';
|
|
|
|
*mode = "text";
|
|
|
|
*data = str;
|
|
|
|
} else {
|
|
|
|
*colon = ':';
|
|
|
|
get_img_data(original, colon + 1, mode_map, first, mode, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 05:00:09 -04:00
|
|
|
//This is hideous, why did I do this to myself
|
2019-12-16 22:15:16 -05:00
|
|
|
static char* parse_images(WofiPropertyBox* box, const char* text, bool create_widgets) {
|
2019-12-16 21:36:21 -05:00
|
|
|
char* ret = strdup("");
|
|
|
|
struct map* mode_map = map_init();
|
|
|
|
map_put(mode_map, "img", "true");
|
2020-05-12 16:58:18 -04:00
|
|
|
map_put(mode_map, "img-noscale", "true");
|
2020-07-11 20:39:09 -04:00
|
|
|
map_put(mode_map, "img-base64", "true");
|
|
|
|
map_put(mode_map, "img-base64-noscale", "true");
|
2019-12-16 21:36:21 -05:00
|
|
|
map_put(mode_map, "text", "true");
|
2019-11-21 01:05:28 -05:00
|
|
|
|
2020-07-28 02:54:12 -04:00
|
|
|
char* original = strdup(text);
|
|
|
|
char* mode1 = NULL;
|
|
|
|
char* mode2 = NULL;
|
2020-07-28 02:55:33 -04:00
|
|
|
char* data1 = NULL;
|
2020-07-28 02:54:12 -04:00
|
|
|
char* data2 = NULL;
|
2019-12-16 20:30:29 -05:00
|
|
|
|
2020-07-28 02:54:12 -04:00
|
|
|
get_img_data(original, original, mode_map, true, &mode2, &data2);
|
2019-12-16 20:30:29 -05:00
|
|
|
|
2019-12-16 21:36:21 -05:00
|
|
|
while(true) {
|
2020-07-28 02:54:12 -04:00
|
|
|
if(mode1 == NULL) {
|
|
|
|
mode1 = mode2;
|
|
|
|
data1 = data2;
|
|
|
|
if(data1 != NULL) {
|
|
|
|
get_img_data(original, data1, mode_map, false, &mode2, &data2);
|
2019-08-28 02:15:18 -04:00
|
|
|
} else {
|
2020-07-28 02:54:12 -04:00
|
|
|
break;
|
2019-12-16 21:36:21 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-28 02:54:12 -04:00
|
|
|
if(strcmp(mode1, "img") == 0 && create_widgets) {
|
|
|
|
GdkPixbuf* buf = gdk_pixbuf_new_from_file(data1, NULL);
|
2020-03-17 17:21:10 -04:00
|
|
|
if(buf == NULL) {
|
2020-07-28 02:54:12 -04:00
|
|
|
fprintf(stderr, "Image %s cannot be loaded\n", data1);
|
2020-03-17 17:21:10 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2020-07-11 20:39:09 -04:00
|
|
|
|
2020-07-18 05:00:09 -04:00
|
|
|
buf = utils_g_resize_pixbuf(buf, image_size * wofi_get_window_scale(), GDK_INTERP_BILINEAR);
|
|
|
|
|
|
|
|
GtkWidget* img = gtk_image_new();
|
|
|
|
cairo_surface_t* surface = gdk_cairo_surface_create_from_pixbuf(buf, wofi_get_window_scale(), gtk_widget_get_window(img));
|
|
|
|
gtk_image_set_from_surface(GTK_IMAGE(img), surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
g_object_unref(buf);
|
2020-07-11 20:39:09 -04:00
|
|
|
|
2019-12-16 21:36:21 -05:00
|
|
|
gtk_widget_set_name(img, "img");
|
|
|
|
gtk_container_add(GTK_CONTAINER(box), img);
|
2020-07-28 02:54:12 -04:00
|
|
|
} else if(strcmp(mode1, "img-noscale") == 0 && create_widgets) {
|
|
|
|
GdkPixbuf* buf = gdk_pixbuf_new_from_file(data1, NULL);
|
2020-05-12 16:58:18 -04:00
|
|
|
if(buf == NULL) {
|
2020-07-28 02:54:12 -04:00
|
|
|
fprintf(stderr, "Image %s cannot be loaded\n", data1);
|
2020-05-12 16:58:18 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2020-07-18 05:00:09 -04:00
|
|
|
|
|
|
|
GtkWidget* img = gtk_image_new();
|
|
|
|
cairo_surface_t* surface = gdk_cairo_surface_create_from_pixbuf(buf, wofi_get_window_scale(), gtk_widget_get_window(img));
|
|
|
|
gtk_image_set_from_surface(GTK_IMAGE(img), surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
g_object_unref(buf);
|
|
|
|
|
2020-05-12 16:58:18 -04:00
|
|
|
gtk_widget_set_name(img, "img");
|
|
|
|
gtk_container_add(GTK_CONTAINER(box), img);
|
2020-07-28 02:54:12 -04:00
|
|
|
} else if(strcmp(mode1, "img-base64") == 0 && create_widgets) {
|
|
|
|
GdkPixbuf* buf = utils_g_pixbuf_from_base64(data1);
|
2020-07-11 20:39:09 -04:00
|
|
|
if(buf == NULL) {
|
|
|
|
fprintf(stderr, "base64 image cannot be loaded\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = utils_g_resize_pixbuf(buf, image_size, GDK_INTERP_BILINEAR);
|
|
|
|
|
2020-07-18 05:00:09 -04:00
|
|
|
GtkWidget* img = gtk_image_new();
|
|
|
|
cairo_surface_t* surface = gdk_cairo_surface_create_from_pixbuf(buf, wofi_get_window_scale(), gtk_widget_get_window(img));
|
|
|
|
gtk_image_set_from_surface(GTK_IMAGE(img), surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
g_object_unref(buf);
|
|
|
|
|
2020-07-11 20:39:09 -04:00
|
|
|
gtk_widget_set_name(img, "img");
|
|
|
|
gtk_container_add(GTK_CONTAINER(box), img);
|
2020-07-28 02:54:12 -04:00
|
|
|
} else if(strcmp(mode1, "img-base64-noscale") == 0 && create_widgets) {
|
|
|
|
GdkPixbuf* buf = utils_g_pixbuf_from_base64(data1);
|
2020-07-11 20:39:09 -04:00
|
|
|
if(buf == NULL) {
|
|
|
|
fprintf(stderr, "base64 image cannot be loaded\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2020-07-18 05:00:09 -04:00
|
|
|
GtkWidget* img = gtk_image_new();
|
|
|
|
cairo_surface_t* surface = gdk_cairo_surface_create_from_pixbuf(buf, wofi_get_window_scale(), gtk_widget_get_window(img));
|
|
|
|
gtk_image_set_from_surface(GTK_IMAGE(img), surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
g_object_unref(buf);
|
|
|
|
|
2020-07-11 20:39:09 -04:00
|
|
|
gtk_widget_set_name(img, "img");
|
|
|
|
gtk_container_add(GTK_CONTAINER(box), img);
|
2020-07-28 02:54:12 -04:00
|
|
|
} else if(strcmp(mode1, "text") == 0) {
|
2019-12-16 21:36:21 -05:00
|
|
|
if(create_widgets) {
|
2020-07-28 02:54:12 -04:00
|
|
|
GtkWidget* label = gtk_label_new(data1);
|
2019-09-01 23:07:15 -04:00
|
|
|
gtk_widget_set_name(label, "text");
|
2019-08-28 03:58:45 -04:00
|
|
|
gtk_label_set_use_markup(GTK_LABEL(label), allow_markup);
|
2019-08-28 02:15:18 -04:00
|
|
|
gtk_label_set_xalign(GTK_LABEL(label), 0);
|
2020-02-21 03:43:44 -05:00
|
|
|
if(line_wrap >= 0) {
|
|
|
|
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
|
|
|
|
gtk_label_set_line_wrap_mode(GTK_LABEL(label), line_wrap);
|
|
|
|
}
|
2019-08-28 02:15:18 -04:00
|
|
|
gtk_container_add(GTK_CONTAINER(box), label);
|
2019-12-16 21:36:21 -05:00
|
|
|
} else {
|
|
|
|
char* tmp = ret;
|
2020-07-28 02:54:12 -04:00
|
|
|
ret = utils_concat(2, ret, data1);
|
2019-12-16 21:36:21 -05:00
|
|
|
free(tmp);
|
2019-12-16 20:30:29 -05:00
|
|
|
}
|
2019-08-28 02:15:18 -04:00
|
|
|
}
|
2020-03-17 17:21:10 -04:00
|
|
|
done:
|
2020-07-28 02:54:12 -04:00
|
|
|
mode1 = NULL;
|
2019-12-16 20:30:29 -05:00
|
|
|
}
|
2019-12-16 21:36:21 -05:00
|
|
|
}
|
2020-07-28 02:54:12 -04:00
|
|
|
free(original);
|
2019-12-16 21:36:21 -05:00
|
|
|
map_free(mode_map);
|
|
|
|
|
|
|
|
if(create_widgets) {
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 22:15:16 -05:00
|
|
|
char* wofi_parse_image_escapes(const char* text) {
|
|
|
|
return parse_images(NULL, text, false);
|
|
|
|
}
|
|
|
|
|
2020-06-30 00:23:36 -04:00
|
|
|
static void setup_label(char* mode, WofiPropertyBox* box) {
|
|
|
|
wofi_property_box_add_property(box, "mode", mode);
|
|
|
|
char index[11];
|
|
|
|
snprintf(index, sizeof(index), "%u", ++widget_count);
|
|
|
|
wofi_property_box_add_property(box, "index", index);
|
|
|
|
|
|
|
|
gtk_widget_set_name(GTK_WIDGET(box), "unselected");
|
|
|
|
|
|
|
|
GtkStyleContext* style = gtk_widget_get_style_context(GTK_WIDGET(box));
|
|
|
|
gtk_style_context_add_class(style, "entry");
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:36:21 -05:00
|
|
|
static GtkWidget* create_label(char* mode, char* text, char* search_text, char* action) {
|
|
|
|
GtkWidget* box = wofi_property_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
|
2020-06-30 00:23:36 -04:00
|
|
|
|
2019-12-16 21:36:21 -05:00
|
|
|
wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "action", action);
|
2020-06-30 00:23:36 -04:00
|
|
|
|
|
|
|
setup_label(mode, WOFI_PROPERTY_BOX(box));
|
2020-02-07 00:47:59 -05:00
|
|
|
|
2021-10-30 04:13:27 -04:00
|
|
|
char* nodetext = text;
|
|
|
|
if (pre_display_cmd != NULL) {
|
|
|
|
text = NULL;
|
|
|
|
FILE *fp_labeltext;
|
|
|
|
char *cmd_labeltext;
|
|
|
|
char line[128]; // you'd think this caps the line's length to 128, but it's just a buffer which due to the nature of fgets() splits on lines
|
|
|
|
size_t size = 0;
|
|
|
|
// first, prepare cmd_labeltext to be each entry's actual comamand to run, aka replacing 'cat %s' to be 'cat filename'
|
|
|
|
if ((asprintf(&cmd_labeltext, pre_display_cmd, nodetext)) == -1) {
|
|
|
|
fprintf(stderr, "error parsing pre_display_cmd to run\n");
|
2021-11-09 10:37:24 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-10-30 04:13:27 -04:00
|
|
|
// then, run the command
|
|
|
|
fp_labeltext = popen(cmd_labeltext, "r");
|
|
|
|
if (fp_labeltext == NULL) {
|
|
|
|
fprintf(stderr, "error executing '%s'\n", cmd_labeltext);
|
|
|
|
exit(EXIT_FAILURE);
|
2021-11-09 10:49:23 -05:00
|
|
|
} else if (fgets(line, sizeof(line), fp_labeltext) != NULL) {
|
2021-10-30 04:13:27 -04:00
|
|
|
// lastly, read the output of said command, and put it into the text variable to be used for the label widgets
|
|
|
|
// consider using 'printf %.10s as your --pre-display-cmd to limit a string to a determined width. 10 here is an example
|
|
|
|
size += strlen(line+1); // we need place for the \0 of strcpy
|
|
|
|
text = (char *) realloc(text, size);
|
|
|
|
strcpy(text, line);
|
2021-11-09 10:49:23 -05:00
|
|
|
while (fgets(line, sizeof(line), fp_labeltext) != NULL) {
|
2021-10-30 04:13:27 -04:00
|
|
|
size += strlen(line);
|
|
|
|
text = (char *) realloc(text, size);
|
|
|
|
strncat(text, line, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pclose(fp_labeltext);
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:36:21 -05:00
|
|
|
if(allow_images) {
|
|
|
|
parse_images(WOFI_PROPERTY_BOX(box), text, true);
|
2019-08-28 02:15:18 -04:00
|
|
|
} else {
|
|
|
|
GtkWidget* label = gtk_label_new(text);
|
2019-09-15 19:29:15 -04:00
|
|
|
gtk_widget_set_name(label, "text");
|
2019-08-28 03:58:45 -04:00
|
|
|
gtk_label_set_use_markup(GTK_LABEL(label), allow_markup);
|
2019-08-28 02:15:18 -04:00
|
|
|
gtk_label_set_xalign(GTK_LABEL(label), 0);
|
2020-02-21 03:43:44 -05:00
|
|
|
if(line_wrap >= 0) {
|
|
|
|
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
|
|
|
|
gtk_label_set_line_wrap_mode(GTK_LABEL(label), line_wrap);
|
|
|
|
}
|
2019-08-28 02:15:18 -04:00
|
|
|
gtk_container_add(GTK_CONTAINER(box), label);
|
|
|
|
}
|
2019-12-16 21:36:21 -05:00
|
|
|
if(parse_search) {
|
2019-12-16 22:38:12 -05:00
|
|
|
search_text = strdup(search_text);
|
|
|
|
if(allow_images) {
|
|
|
|
char* tmp = search_text;
|
|
|
|
search_text = parse_images(WOFI_PROPERTY_BOX(box), search_text, false);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
if(allow_markup) {
|
2020-04-23 08:44:02 -04:00
|
|
|
char* out = NULL;
|
2019-12-16 22:38:12 -05:00
|
|
|
pango_parse_markup(search_text, -1, 0, NULL, &out, NULL, NULL);
|
|
|
|
free(search_text);
|
|
|
|
search_text = out;
|
|
|
|
}
|
2019-12-16 21:36:21 -05:00
|
|
|
}
|
2019-10-28 17:17:06 -04:00
|
|
|
wofi_property_box_add_property(WOFI_PROPERTY_BOX(box), "filter", search_text);
|
2019-12-16 21:36:21 -05:00
|
|
|
if(parse_search) {
|
|
|
|
free(search_text);
|
|
|
|
}
|
2019-08-28 02:15:18 -04:00
|
|
|
return box;
|
2019-08-18 23:49:12 -04:00
|
|
|
}
|
|
|
|
|
2019-11-05 01:41:53 -05:00
|
|
|
static char* get_cache_path(const gchar* mode) {
|
2019-08-28 23:14:38 -04:00
|
|
|
if(cache_file != NULL) {
|
2019-09-12 14:06:57 -04:00
|
|
|
return strdup(cache_file);
|
2019-08-28 23:14:38 -04:00
|
|
|
}
|
2019-08-18 23:49:12 -04:00
|
|
|
char* cache_path = getenv("XDG_CACHE_HOME");
|
|
|
|
if(cache_path == NULL) {
|
|
|
|
cache_path = utils_concat(3, getenv("HOME"), "/.cache/wofi-", mode);
|
|
|
|
} else {
|
|
|
|
cache_path = utils_concat(3, cache_path, "/wofi-", mode);
|
|
|
|
}
|
|
|
|
return cache_path;
|
|
|
|
}
|
|
|
|
|
2019-11-16 17:52:20 -05:00
|
|
|
static void execute_action(const gchar* mode, const gchar* cmd) {
|
2019-11-13 04:17:45 -05:00
|
|
|
struct mode* mode_ptr = map_get(modes, mode);
|
|
|
|
mode_ptr->mode_exec(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void activate_item(GtkFlowBox* flow_box, GtkFlowBoxChild* row, gpointer data) {
|
|
|
|
(void) flow_box;
|
|
|
|
(void) data;
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(row));
|
|
|
|
bool primary_action = GTK_IS_EXPANDER(box);
|
|
|
|
if(primary_action) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
2019-11-16 17:52:20 -05:00
|
|
|
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"));
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 22:14:30 -05:00
|
|
|
static void expand(GtkExpander* expander, gpointer data) {
|
|
|
|
(void) data;
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(expander));
|
2020-06-14 04:24:37 -04:00
|
|
|
resize_expander = !gtk_expander_get_expanded(expander);
|
|
|
|
gtk_widget_set_visible(box, resize_expander);
|
2019-11-19 22:14:30 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:06:14 -05:00
|
|
|
static void update_surface_size(void) {
|
2020-06-16 18:41:28 -04:00
|
|
|
if(lines > 0) {
|
|
|
|
height = max_height * lines;
|
|
|
|
height += 5;
|
|
|
|
}
|
2020-05-15 20:36:45 -04:00
|
|
|
if(shell != NULL) {
|
2020-03-02 15:06:14 -05:00
|
|
|
zwlr_layer_surface_v1_set_size(wlr_surface, width, height);
|
|
|
|
wl_surface_commit(wl_surface);
|
|
|
|
wl_display_roundtrip(wl);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_window_resize(GTK_WINDOW(window), width, height);
|
2021-02-23 02:53:57 -05:00
|
|
|
GtkAllocation alloc;
|
|
|
|
gtk_widget_get_allocated_size(entry, &alloc, NULL);
|
|
|
|
if(outer_orientation == GTK_ORIENTATION_HORIZONTAL) {
|
|
|
|
if(alloc.width > 0) {
|
|
|
|
gtk_widget_set_size_request(scroll, width - alloc.width, height);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(alloc.height > 0) {
|
|
|
|
gtk_widget_set_size_request(scroll, width, height - alloc.height);
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 15:06:14 -05:00
|
|
|
}
|
|
|
|
|
2020-02-08 21:04:38 -05:00
|
|
|
static void widget_allocate(GtkWidget* widget, GdkRectangle* allocation, gpointer data) {
|
|
|
|
(void) data;
|
2021-02-23 02:53:57 -05:00
|
|
|
if(widget == entry) {
|
|
|
|
update_surface_size();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-14 04:24:37 -04:00
|
|
|
if(resize_expander) {
|
|
|
|
return;
|
2020-04-06 18:00:14 -04:00
|
|
|
}
|
2020-06-14 04:24:37 -04:00
|
|
|
|
2020-02-25 03:30:35 -05:00
|
|
|
if(max_height > 0) {
|
|
|
|
if(allocation->height > max_height) {
|
|
|
|
int64_t ratio = allocation->height / max_height;
|
|
|
|
int64_t mod = allocation->height % max_height;
|
|
|
|
if(mod >= max_height / 2) {
|
|
|
|
++ratio;
|
|
|
|
}
|
|
|
|
if(ratio > 1) {
|
2020-03-12 20:09:45 -04:00
|
|
|
gtk_widget_set_size_request(widget, -1, max_height * ratio);
|
2020-02-25 03:30:35 -05:00
|
|
|
} else {
|
|
|
|
max_height = allocation->height;
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-12 20:09:45 -04:00
|
|
|
gtk_widget_set_size_request(widget, -1, max_height);
|
2020-02-25 03:30:35 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
max_height = allocation->height;
|
|
|
|
}
|
2020-03-11 21:27:44 -04:00
|
|
|
if(lines > 0) {
|
|
|
|
update_surface_size();
|
|
|
|
}
|
2020-02-08 21:04:38 -05:00
|
|
|
}
|
|
|
|
|
2019-11-13 04:17:45 -05:00
|
|
|
static gboolean _insert_widget(gpointer data) {
|
2020-01-16 19:35:51 -05:00
|
|
|
struct mode* mode = data;
|
|
|
|
struct widget* node;
|
|
|
|
if(mode->mode_get_widget == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
node = mode->mode_get_widget();
|
|
|
|
}
|
|
|
|
if(node == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-06-23 01:45:51 -04:00
|
|
|
|
2019-11-13 04:17:45 -05:00
|
|
|
GtkWidget* parent;
|
2020-01-10 17:34:49 -05:00
|
|
|
if(node->action_count > 1 && !no_actions) {
|
2019-11-13 04:17:45 -05:00
|
|
|
parent = gtk_expander_new("");
|
2019-11-19 22:14:30 -05:00
|
|
|
g_signal_connect(parent, "activate", G_CALLBACK(expand), NULL);
|
2020-06-23 01:45:51 -04:00
|
|
|
GtkWidget* box;
|
|
|
|
if(node->builder == NULL) {
|
2021-11-09 10:47:12 -05:00
|
|
|
box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
2020-06-23 01:45:51 -04:00
|
|
|
} else {
|
|
|
|
box = GTK_WIDGET(node->builder->box);
|
2020-06-30 00:23:36 -04:00
|
|
|
setup_label(node->builder->mode->name, WOFI_PROPERTY_BOX(box));
|
2020-06-23 01:45:51 -04:00
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
gtk_expander_set_label_widget(GTK_EXPANDER(parent), box);
|
2019-11-19 22:14:30 -05:00
|
|
|
|
2019-11-13 04:17:45 -05:00
|
|
|
GtkWidget* exp_box = gtk_list_box_new();
|
2019-11-13 04:42:27 -05:00
|
|
|
gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(exp_box), FALSE);
|
2019-11-13 04:17:45 -05:00
|
|
|
g_signal_connect(exp_box, "row-activated", G_CALLBACK(activate_item), NULL);
|
|
|
|
gtk_container_add(GTK_CONTAINER(parent), exp_box);
|
|
|
|
for(size_t count = 1; count < node->action_count; ++count) {
|
2020-06-23 01:45:51 -04:00
|
|
|
if(node->builder == NULL) {
|
|
|
|
box = create_label(node->mode, node->text[count], node->search_text, node->actions[count]);
|
|
|
|
} else {
|
|
|
|
box = GTK_WIDGET(node->builder[count].box);
|
2020-06-30 00:23:36 -04:00
|
|
|
setup_label(node->builder->mode->name, WOFI_PROPERTY_BOX(box));
|
2020-06-23 01:45:51 -04:00
|
|
|
}
|
2020-01-18 17:54:52 -05:00
|
|
|
|
|
|
|
GtkWidget* row = gtk_list_box_row_new();
|
|
|
|
gtk_widget_set_name(row, "entry");
|
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(row), box);
|
|
|
|
gtk_container_add(GTK_CONTAINER(exp_box), row);
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-23 01:45:51 -04:00
|
|
|
if(node->builder == NULL) {
|
2021-11-09 10:47:12 -05:00
|
|
|
parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
|
2020-06-23 01:45:51 -04:00
|
|
|
} else {
|
|
|
|
parent = GTK_WIDGET(node->builder->box);
|
2020-06-30 00:23:36 -04:00
|
|
|
setup_label(node->builder->mode->name, WOFI_PROPERTY_BOX(parent));
|
2020-06-23 01:45:51 -04:00
|
|
|
}
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
2020-06-23 01:45:51 -04:00
|
|
|
|
2020-02-08 00:06:30 -05:00
|
|
|
gtk_widget_set_halign(parent, content_halign);
|
2020-01-18 17:54:52 -05:00
|
|
|
GtkWidget* child = gtk_flow_box_child_new();
|
|
|
|
gtk_widget_set_name(child, "entry");
|
2020-03-11 21:27:44 -04:00
|
|
|
g_signal_connect(child, "size-allocate", G_CALLBACK(widget_allocate), NULL);
|
2020-01-18 17:54:52 -05:00
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(child), parent);
|
|
|
|
gtk_widget_show_all(child);
|
2020-06-16 18:41:28 -04:00
|
|
|
gtk_container_add(GTK_CONTAINER(inner_box), child);
|
|
|
|
++line_count;
|
2019-11-13 04:17:45 -05:00
|
|
|
|
2020-02-07 21:04:37 -05:00
|
|
|
if(!user_moved) {
|
2020-02-29 01:15:53 -05:00
|
|
|
GtkFlowBoxChild* child = gtk_flow_box_get_child_at_index(GTK_FLOW_BOX(inner_box), 0);
|
|
|
|
gtk_flow_box_select_child(GTK_FLOW_BOX(inner_box), child);
|
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(child));
|
2020-02-07 00:20:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 22:14:30 -05:00
|
|
|
if(GTK_IS_EXPANDER(parent)) {
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(parent));
|
|
|
|
gtk_widget_set_visible(box, FALSE);
|
|
|
|
}
|
|
|
|
|
2020-06-23 01:45:51 -04:00
|
|
|
if(node->builder != NULL) {
|
2020-07-11 19:39:09 -04:00
|
|
|
wofi_widget_builder_free(node->builder);
|
2020-06-23 01:45:51 -04:00
|
|
|
} else {
|
|
|
|
free(node->mode);
|
|
|
|
for(size_t count = 0; count < node->action_count; ++count) {
|
|
|
|
free(node->text[count]);
|
|
|
|
}
|
|
|
|
free(node->text);
|
|
|
|
free(node->search_text);
|
|
|
|
for(size_t count = 0; count < node->action_count; ++count) {
|
|
|
|
free(node->actions[count]);
|
|
|
|
}
|
|
|
|
free(node->actions);
|
|
|
|
free(node);
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
2020-01-16 19:35:51 -05:00
|
|
|
return TRUE;
|
2019-11-13 04:17:45 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 02:15:13 -05:00
|
|
|
static gboolean insert_all_widgets(gpointer data) {
|
2020-10-02 23:54:36 -04:00
|
|
|
if(!has_joined_mode) {
|
|
|
|
pthread_join(mode_thread, NULL);
|
|
|
|
has_joined_mode = true;
|
|
|
|
}
|
2020-02-01 02:15:13 -05:00
|
|
|
struct wl_list* modes = data;
|
|
|
|
if(modes->prev == modes) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
struct mode* mode = wl_container_of(modes->prev, mode, link);
|
|
|
|
if(!_insert_widget(mode)) {
|
|
|
|
wl_list_remove(&mode->link);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-30 00:49:16 -05:00
|
|
|
static char* escape_lf(const char* cmd) {
|
|
|
|
size_t len = strlen(cmd);
|
|
|
|
char* buffer = calloc(1, (len + 1) * 2);
|
|
|
|
|
|
|
|
size_t buf_count = 0;
|
|
|
|
for(size_t count = 0; count < len; ++count) {
|
|
|
|
char chr = cmd[count];
|
|
|
|
if(chr == '\n') {
|
|
|
|
buffer[buf_count++] = '\\';
|
|
|
|
buffer[buf_count++] = 'n';
|
|
|
|
} else if(chr == '\\') {
|
|
|
|
buffer[buf_count++] = '\\';
|
|
|
|
buffer[buf_count++] = '\\';
|
|
|
|
} else {
|
|
|
|
buffer[buf_count++] = chr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* remove_escapes(const char* cmd) {
|
|
|
|
size_t len = strlen(cmd);
|
|
|
|
char* buffer = calloc(1, len + 1);
|
|
|
|
|
|
|
|
size_t buf_count = 0;
|
|
|
|
for(size_t count = 0; count < len; ++count) {
|
|
|
|
char chr = cmd[count];
|
|
|
|
if(chr == '\\') {
|
|
|
|
chr = cmd[++count];
|
|
|
|
if(chr == 'n') {
|
|
|
|
buffer[buf_count++] = '\n';
|
|
|
|
} else if(chr == '\\') {
|
|
|
|
buffer[buf_count++] = '\\';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer[buf_count++] = chr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wofi_write_cache(struct mode* mode, const char* _cmd) {
|
|
|
|
char* cmd = escape_lf(_cmd);
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
char* cache_path = get_cache_path(mode->name);
|
2020-02-29 01:36:48 -05:00
|
|
|
|
|
|
|
char* tmp_dir = strdup(cache_path);
|
|
|
|
utils_mkdir(dirname(tmp_dir), S_IRWXU | S_IRGRP | S_IXGRP);
|
|
|
|
free(tmp_dir);
|
|
|
|
|
2019-11-16 17:52:20 -05:00
|
|
|
struct wl_list lines;
|
|
|
|
wl_list_init(&lines);
|
|
|
|
bool inc_count = false;
|
2020-01-16 19:49:57 -05:00
|
|
|
if(access(cache_path, R_OK) == 0) {
|
2019-11-16 17:52:20 -05:00
|
|
|
FILE* file = fopen(cache_path, "r");
|
|
|
|
char* line = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
while(getline(&line, &size, file) != -1) {
|
2019-12-17 02:38:25 -05:00
|
|
|
char* space = strchr(line, ' ');
|
2020-01-02 17:36:54 -05:00
|
|
|
char* lf = strchr(line, '\n');
|
|
|
|
if(lf != NULL) {
|
|
|
|
*lf = 0;
|
2019-12-17 02:38:25 -05:00
|
|
|
}
|
|
|
|
if(space != NULL && strcmp(cmd, space + 1) == 0) {
|
|
|
|
struct cache_line* node = malloc(sizeof(struct cache_line));
|
2020-01-30 01:01:29 -05:00
|
|
|
uint64_t count = strtol(line, NULL, 10) + 1;
|
2019-11-16 17:52:20 -05:00
|
|
|
char num[6];
|
|
|
|
snprintf(num, 5, "%" PRIu64, count);
|
|
|
|
node->line = utils_concat(4, num, " ", cmd, "\n");
|
|
|
|
inc_count = true;
|
2019-12-17 02:38:25 -05:00
|
|
|
wl_list_insert(&lines, &node->link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
size = 0;
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
while(getline(&line, &size, file) != -1) {
|
|
|
|
char* space = strchr(line, ' ');
|
|
|
|
char* nl = strchr(line, '\n');
|
|
|
|
if(nl != NULL) {
|
|
|
|
*nl = 0;
|
|
|
|
}
|
|
|
|
if(space == NULL || strcmp(cmd, space + 1) != 0) {
|
|
|
|
struct cache_line* node = malloc(sizeof(struct cache_line));
|
2020-01-30 01:01:29 -05:00
|
|
|
node->line = utils_concat(2, line, "\n");
|
2019-12-17 02:38:25 -05:00
|
|
|
wl_list_insert(&lines, &node->link);
|
2019-11-16 17:52:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(file);
|
2020-01-16 19:49:57 -05:00
|
|
|
}
|
|
|
|
char* tmp_path = strdup(cache_path);
|
|
|
|
char* dir = dirname(tmp_path);
|
2019-11-16 17:52:20 -05:00
|
|
|
|
2020-01-16 19:49:57 -05:00
|
|
|
if(access(dir, W_OK) == 0) {
|
2020-01-16 19:56:08 -05:00
|
|
|
if(!inc_count) {
|
|
|
|
struct cache_line* node = malloc(sizeof(struct cache_line));
|
|
|
|
node->line = utils_concat(3, "1 ", cmd, "\n");
|
|
|
|
wl_list_insert(&lines, &node->link);
|
|
|
|
}
|
2020-01-16 19:49:57 -05:00
|
|
|
FILE* file = fopen(cache_path, "w");
|
2020-01-02 18:24:30 -05:00
|
|
|
struct cache_line* node, *tmp;
|
|
|
|
wl_list_for_each_safe(node, tmp, &lines, link) {
|
|
|
|
fwrite(node->line, 1, strlen(node->line), file);
|
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
2019-11-16 17:52:20 -05:00
|
|
|
|
2020-01-02 18:24:30 -05:00
|
|
|
fclose(file);
|
|
|
|
}
|
2019-11-16 17:52:20 -05:00
|
|
|
free(cache_path);
|
2020-01-16 19:49:57 -05:00
|
|
|
free(tmp_path);
|
2020-01-30 00:49:16 -05:00
|
|
|
free(cmd);
|
2019-11-16 17:52:20 -05:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:49:16 -05:00
|
|
|
void wofi_remove_cache(struct mode* mode, const char* _cmd) {
|
|
|
|
char* cmd = escape_lf(_cmd);
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
char* cache_path = get_cache_path(mode->name);
|
2020-01-02 18:24:30 -05:00
|
|
|
if(access(cache_path, R_OK | W_OK) == 0) {
|
2020-01-02 17:36:54 -05:00
|
|
|
struct wl_list lines;
|
|
|
|
wl_list_init(&lines);
|
|
|
|
|
|
|
|
FILE* file = fopen(cache_path, "r");
|
|
|
|
char* line = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
while(getline(&line, &size, file) != -1) {
|
|
|
|
char* space = strchr(line, ' ');
|
|
|
|
char* lf = strchr(line, '\n');
|
|
|
|
if(lf != NULL) {
|
|
|
|
*lf = 0;
|
|
|
|
}
|
|
|
|
if(space == NULL || strcmp(cmd, space + 1) != 0) {
|
|
|
|
struct cache_line* node = malloc(sizeof(struct cache_line));
|
|
|
|
node->line = utils_concat(2, line, "\n");
|
|
|
|
wl_list_insert(&lines, &node->link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
file = fopen(cache_path, "w");
|
|
|
|
struct cache_line* node, *tmp;
|
|
|
|
wl_list_for_each_safe(node, tmp, &lines, link) {
|
|
|
|
fwrite(node->line, 1, strlen(node->line), file);
|
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
free(cache_path);
|
2020-01-30 00:51:25 -05:00
|
|
|
free(cmd);
|
2020-01-02 17:36:54 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
struct wl_list* wofi_read_cache(struct mode* mode) {
|
|
|
|
char* cache_path = get_cache_path(mode->name);
|
2019-08-18 23:49:12 -04:00
|
|
|
struct wl_list* cache = malloc(sizeof(struct wl_list));
|
|
|
|
wl_list_init(cache);
|
|
|
|
struct wl_list lines;
|
|
|
|
wl_list_init(&lines);
|
|
|
|
if(access(cache_path, R_OK) == 0) {
|
|
|
|
FILE* file = fopen(cache_path, "r");
|
|
|
|
char* line = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
while(getline(&line, &size, file) != -1) {
|
|
|
|
struct cache_line* node = malloc(sizeof(struct cache_line));
|
|
|
|
char* lf = strchr(line, '\n');
|
|
|
|
if(lf != NULL) {
|
|
|
|
*lf = 0;
|
|
|
|
}
|
2020-01-30 00:49:16 -05:00
|
|
|
node->line = remove_escapes(line);
|
2019-08-18 23:49:12 -04:00
|
|
|
wl_list_insert(&lines, &node->link);
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(file);
|
|
|
|
}
|
2020-03-14 00:58:54 -04:00
|
|
|
while(!wl_list_empty(&lines)) {
|
2019-09-13 03:25:25 -04:00
|
|
|
uint64_t smallest = UINT64_MAX;
|
|
|
|
struct cache_line* node, *smallest_node = NULL;
|
2019-08-18 23:49:12 -04:00
|
|
|
wl_list_for_each(node, &lines, link) {
|
|
|
|
uint64_t num = strtol(node->line, NULL, 10);
|
2019-09-13 03:25:25 -04:00
|
|
|
if(num < smallest) {
|
|
|
|
smallest = num;
|
|
|
|
smallest_node = node;
|
2019-08-18 23:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
char* tmp = strdup(strchr(smallest_node->line, ' ') + 1);
|
|
|
|
free(smallest_node->line);
|
|
|
|
smallest_node->line = tmp;
|
|
|
|
wl_list_remove(&smallest_node->link);
|
|
|
|
wl_list_insert(cache, &smallest_node->link);
|
2019-08-18 23:49:12 -04:00
|
|
|
}
|
|
|
|
free(cache_path);
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:44:00 -05:00
|
|
|
struct widget* wofi_create_widget(struct mode* mode, char* text[], char* search_text, char* actions[], size_t action_count) {
|
2020-06-23 01:45:51 -04:00
|
|
|
struct widget* widget = calloc(1, sizeof(struct widget));
|
2020-01-20 23:09:40 -05:00
|
|
|
widget->mode = strdup(mode->name);
|
2019-11-13 04:17:45 -05:00
|
|
|
widget->text = malloc(action_count * sizeof(char*));
|
|
|
|
for(size_t count = 0; count < action_count; ++count) {
|
|
|
|
widget->text[count] = strdup(text[count]);
|
|
|
|
}
|
2019-10-28 17:17:06 -04:00
|
|
|
widget->search_text = strdup(search_text);
|
2019-11-13 04:17:45 -05:00
|
|
|
widget->action_count = action_count;
|
|
|
|
widget->actions = malloc(action_count * sizeof(char*));
|
|
|
|
for(size_t count = 0; count < action_count; ++count) {
|
|
|
|
widget->actions[count] = strdup(actions[count]);
|
|
|
|
}
|
2020-01-16 19:35:51 -05:00
|
|
|
return widget;
|
2019-08-28 23:43:47 -04:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:25:04 -05:00
|
|
|
void wofi_insert_widgets(struct mode* mode) {
|
|
|
|
gdk_threads_add_idle(_insert_widget, mode);
|
|
|
|
}
|
|
|
|
|
2020-03-13 16:00:59 -04:00
|
|
|
char* wofi_get_dso_path(struct mode* mode) {
|
|
|
|
return mode->dso;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
bool wofi_allow_images(void) {
|
2019-09-13 03:25:25 -04:00
|
|
|
return allow_images;
|
2019-08-17 23:21:52 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
bool wofi_allow_markup(void) {
|
2019-12-16 22:38:12 -05:00
|
|
|
return allow_markup;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
uint64_t wofi_get_image_size(void) {
|
2019-09-13 03:25:25 -04:00
|
|
|
return image_size;
|
2019-09-12 00:00:26 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 02:22:05 -04:00
|
|
|
uint64_t wofi_get_window_scale(void) {
|
|
|
|
return gdk_window_get_scale_factor(gtk_widget_get_window(window));
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
bool wofi_mod_shift(void) {
|
2019-11-19 01:16:06 -05:00
|
|
|
return mod_shift;
|
2019-09-27 02:24:48 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
bool wofi_mod_control(void) {
|
2019-11-19 01:13:14 -05:00
|
|
|
return mod_ctrl;
|
|
|
|
}
|
|
|
|
|
2019-09-27 02:24:48 -04:00
|
|
|
void wofi_term_run(const char* cmd) {
|
|
|
|
if(terminal != NULL) {
|
2020-05-05 16:38:01 -04:00
|
|
|
execlp(terminal, terminal, "-e", cmd, NULL);
|
2019-09-27 02:24:48 -04:00
|
|
|
}
|
|
|
|
size_t term_count = sizeof(terminals) / sizeof(char*);
|
|
|
|
for(size_t count = 0; count < term_count; ++count) {
|
2020-05-05 16:38:01 -04:00
|
|
|
execlp(terminals[count], terminals[count], "-e", cmd, NULL);
|
2019-09-27 02:24:48 -04:00
|
|
|
}
|
|
|
|
fprintf(stderr, "No terminal emulator found please set term in config or use --term\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:41:49 -04:00
|
|
|
static void flag_box(GtkBox* box, GtkStateFlags flags) {
|
|
|
|
GList* selected_children = gtk_container_get_children(GTK_CONTAINER(box));
|
2020-07-11 20:39:09 -04:00
|
|
|
for(GList* list = selected_children; list != NULL; list = list->next) {
|
|
|
|
GtkWidget* child = list->data;
|
2020-04-25 01:41:49 -04:00
|
|
|
gtk_widget_set_state_flags(child, flags, TRUE);
|
|
|
|
}
|
|
|
|
g_list_free(selected_children);
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:33:08 -04:00
|
|
|
static void select_item(GtkFlowBox* flow_box, gpointer data) {
|
2019-08-17 21:34:56 -04:00
|
|
|
(void) data;
|
|
|
|
if(previous_selection != NULL) {
|
|
|
|
gtk_widget_set_name(previous_selection, "unselected");
|
2020-04-25 01:41:49 -04:00
|
|
|
flag_box(GTK_BOX(previous_selection), GTK_STATE_FLAG_NORMAL);
|
2019-08-17 21:34:56 -04:00
|
|
|
}
|
2019-08-28 22:33:08 -04:00
|
|
|
GList* selected_children = gtk_flow_box_get_selected_children(flow_box);
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(selected_children->data));
|
|
|
|
g_list_free(selected_children);
|
2020-05-08 17:18:39 -04:00
|
|
|
if(GTK_IS_EXPANDER(box)) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
2020-04-25 01:41:49 -04:00
|
|
|
flag_box(GTK_BOX(box), GTK_STATE_FLAG_SELECTED);
|
2019-08-28 02:15:18 -04:00
|
|
|
gtk_widget_set_name(box, "selected");
|
|
|
|
previous_selection = box;
|
2019-08-17 21:34:56 -04:00
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
static void activate_search(GtkEntry* entry, gpointer data) {
|
2019-08-19 20:34:12 -04:00
|
|
|
(void) data;
|
2020-02-07 00:20:08 -05:00
|
|
|
GtkFlowBoxChild* child = gtk_flow_box_get_child_at_index(GTK_FLOW_BOX(inner_box), 0);
|
2020-02-17 03:15:57 -05:00
|
|
|
gboolean is_visible = gtk_widget_get_visible(GTK_WIDGET(child));
|
2020-02-16 23:34:55 -05:00
|
|
|
if(mode != NULL && (exec_search || child == NULL || !is_visible)) {
|
2019-11-16 17:52:20 -05:00
|
|
|
execute_action(mode, gtk_entry_get_text(entry));
|
2020-01-23 23:26:33 -05:00
|
|
|
} else if(child != NULL) {
|
2019-10-13 15:42:05 -04:00
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
|
2019-11-13 04:17:45 -05:00
|
|
|
bool primary_action = GTK_IS_EXPANDER(box);
|
|
|
|
if(primary_action) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
2019-11-16 17:52:20 -05:00
|
|
|
execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"));
|
2019-08-19 20:34:12 -04:00
|
|
|
}
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
static gboolean do_strcomp(gchar* filter, const gchar* text) {
|
2019-08-17 21:19:32 -04:00
|
|
|
if(filter == NULL || strcmp(filter, "") == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-08-29 00:15:29 -04:00
|
|
|
if(text == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-11-25 17:56:46 -05:00
|
|
|
if(insensitive) {
|
|
|
|
return strcasestr(text, filter) != NULL;
|
|
|
|
} else {
|
|
|
|
return strstr(text, filter) != NULL;
|
|
|
|
}
|
2019-11-21 04:01:27 -05:00
|
|
|
}
|
|
|
|
|
2022-06-15 13:07:40 -04:00
|
|
|
static char* strcasechr(const char *s, char c) {
|
|
|
|
const char accept[3] = {c, toupper(c), 0};
|
|
|
|
return strpbrk(s, accept);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean do_fuzzy_strcomp(gchar* filter, const gchar* text) {
|
|
|
|
if (filter == NULL || strcmp(filter, "") == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (text == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
// we just check that all the characters (ignoring case) are in the
|
|
|
|
// search text possibly case insensitively in the correct order
|
|
|
|
while (*filter) {
|
|
|
|
char nch = *filter++;
|
|
|
|
|
|
|
|
if (!(text = strcasechr(text, nch))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
static gboolean do_multi_strcomp(gchar* filter, const gchar* text) {
|
|
|
|
if(filter == NULL || strcmp(filter, "") == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if(text == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gchar new_filter[MAX_MULTI_CONTAINS_FILTER_SIZE];
|
|
|
|
strncpy(new_filter, filter, sizeof(new_filter));
|
|
|
|
new_filter[sizeof(new_filter) - 1] = '\0';
|
|
|
|
gchar* token;
|
|
|
|
gchar* rest = new_filter;
|
|
|
|
while((token = strtok_r(rest, " ", &rest))) {
|
|
|
|
if(do_strcomp(token, text) == FALSE) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean filter_proxy(GtkFlowBoxChild* row) {
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(row));
|
|
|
|
if(GTK_IS_EXPANDER(box)) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
|
|
|
const gchar* text =
|
|
|
|
wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "filter");
|
|
|
|
return do_strcomp(filter, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean filter_multi_proxy(GtkFlowBoxChild* row) {
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(row));
|
|
|
|
if(GTK_IS_EXPANDER(box)) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
|
|
|
const gchar* text =
|
|
|
|
wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "filter");
|
|
|
|
return do_multi_strcomp(filter, text);
|
|
|
|
}
|
2020-06-16 18:41:28 -04:00
|
|
|
|
2022-06-15 13:07:40 -04:00
|
|
|
static gboolean filter_fuzzy_proxy(GtkFlowBoxChild *row) {
|
|
|
|
GtkWidget *box = gtk_bin_get_child(GTK_BIN(row));
|
|
|
|
if (GTK_IS_EXPANDER(box)) {
|
|
|
|
box = gtk_expander_get_label_widget(GTK_EXPANDER(box));
|
|
|
|
}
|
|
|
|
const gchar *text =
|
|
|
|
wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "filter");
|
|
|
|
return do_fuzzy_strcomp(filter, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_resize_surface_after_filter(GtkFlowBoxChild *row, gboolean filter_return) {
|
2022-06-07 09:46:45 -04:00
|
|
|
|
|
|
|
if (gtk_widget_get_visible(GTK_WIDGET(row)) == !filter_return &&
|
|
|
|
dynamic_lines) {
|
|
|
|
if (filter_return) {
|
2020-06-16 18:41:28 -04:00
|
|
|
++line_count;
|
|
|
|
} else {
|
|
|
|
--line_count;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
if (line_count < max_lines) {
|
2020-06-16 18:41:28 -04:00
|
|
|
lines = line_count;
|
|
|
|
update_surface_size();
|
|
|
|
} else {
|
|
|
|
lines = max_lines;
|
|
|
|
update_surface_size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
gtk_widget_set_visible(GTK_WIDGET(row), filter_return);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean do_filter(GtkFlowBoxChild* row, gpointer data) {
|
|
|
|
(void) data;
|
|
|
|
gboolean ret = filter_proxy(row);
|
|
|
|
|
|
|
|
do_resize_surface_after_filter(row, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-15 13:07:40 -04:00
|
|
|
static gboolean do_fuzzy_filter(GtkFlowBoxChild* row, gpointer data) {
|
|
|
|
(void)data;
|
|
|
|
gboolean ret = filter_fuzzy_proxy(row);
|
|
|
|
|
|
|
|
do_resize_surface_after_filter(row, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
static gboolean do_multi_filter(GtkFlowBoxChild* row, gpointer data) {
|
|
|
|
(void)data;
|
|
|
|
gboolean ret = filter_multi_proxy(row);
|
|
|
|
|
|
|
|
do_resize_surface_after_filter(row, ret);
|
2020-06-16 18:41:28 -04:00
|
|
|
|
2020-02-17 03:15:57 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:54:34 -05:00
|
|
|
static gint fuzzy_sort(const gchar* text1, const gchar* text2) {
|
2020-01-26 14:45:56 -05:00
|
|
|
char* _filter = strdup(filter);
|
|
|
|
size_t len = strlen(_filter);
|
|
|
|
|
|
|
|
char* t1 = strdup(text1);
|
|
|
|
size_t t1l = strlen(t1);
|
|
|
|
|
|
|
|
char* t2 = strdup(text2);
|
|
|
|
size_t t2l = strlen(t2);
|
|
|
|
|
|
|
|
if(insensitive) {
|
|
|
|
for(size_t count = 0; count < len; ++count) {
|
|
|
|
char chr = _filter[count];
|
|
|
|
if(isalpha(chr)) {
|
|
|
|
_filter[count] = tolower(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(size_t count = 0; count < t1l; ++count) {
|
|
|
|
char chr = t1[count];
|
|
|
|
if(isalpha(chr)) {
|
|
|
|
t1[count] = tolower(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(size_t count = 0; count < t2l; ++count) {
|
|
|
|
char chr = t2[count];
|
|
|
|
if(isalpha(chr)) {
|
|
|
|
t2[count] = tolower(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t dist1 = utils_distance(t1, _filter);
|
|
|
|
size_t dist2 = utils_distance(t2, _filter);
|
|
|
|
free(_filter);
|
|
|
|
free(t1);
|
|
|
|
free(t2);
|
2020-01-26 15:03:08 -05:00
|
|
|
return dist1 - dist2;
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
2022-06-07 09:46:45 -04:00
|
|
|
// we sort based on how early in the string all the matches are.
|
|
|
|
// if there are matches for each.
|
|
|
|
static gint multi_contains_sort(const gchar* text1, const gchar* text2) {
|
|
|
|
// sum of string positions of each match
|
|
|
|
int t1_count = 0;
|
|
|
|
int t2_count = 0;
|
|
|
|
// does this string match with mult-contains
|
|
|
|
bool t1_match = true;
|
|
|
|
bool t2_match = true;
|
|
|
|
|
|
|
|
gchar new_filter[MAX_MULTI_CONTAINS_FILTER_SIZE];
|
|
|
|
strncpy(new_filter, filter, sizeof(new_filter));
|
|
|
|
new_filter[sizeof(new_filter) - 1] = '\0';
|
|
|
|
|
|
|
|
gchar* token;
|
|
|
|
gchar* rest = new_filter;
|
|
|
|
while((token = strtok_r(rest, " ", &rest))) {
|
|
|
|
char* str1, *str2;
|
|
|
|
if(insensitive) {
|
|
|
|
str1 = strcasestr(text1, token);
|
|
|
|
str2 = strcasestr(text2, token);
|
|
|
|
} else {
|
|
|
|
str1 = strstr(text1, token);
|
|
|
|
str2 = strstr(text2, token);
|
|
|
|
}
|
|
|
|
t1_match = t1_match && str1 != NULL;
|
|
|
|
t2_match = t2_match && str2 != NULL;
|
|
|
|
if(str1 != NULL) {
|
|
|
|
int pos1 = str1 - text1;
|
|
|
|
t1_count += pos1;
|
|
|
|
}
|
|
|
|
if(str2 != NULL) {
|
|
|
|
int pos2 = str2 - text2;
|
|
|
|
t2_count += pos2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(t1_match && t2_match) {
|
|
|
|
// both match
|
|
|
|
// return the one with the smallest count.
|
|
|
|
return t1_count - t2_count;
|
|
|
|
} else if(t1_match) {
|
|
|
|
return -1;
|
|
|
|
} else if(t2_match) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 16:54:34 -05:00
|
|
|
static gint contains_sort(const gchar* text1, const gchar* text2) {
|
2020-02-07 00:20:08 -05:00
|
|
|
char* str1, *str2;
|
2020-02-02 16:54:34 -05:00
|
|
|
|
|
|
|
if(insensitive) {
|
2020-02-07 00:20:08 -05:00
|
|
|
str1 = strcasestr(text1, filter);
|
|
|
|
str2 = strcasestr(text2, filter);
|
2020-02-02 16:54:34 -05:00
|
|
|
} else {
|
2020-02-07 00:20:08 -05:00
|
|
|
str1 = strstr(text1, filter);
|
|
|
|
str2 = strstr(text2, filter);
|
2020-02-02 16:54:34 -05:00
|
|
|
}
|
2020-02-07 00:20:08 -05:00
|
|
|
bool tx1 = str1 == text1;
|
|
|
|
bool tx2 = str2 == text2;
|
|
|
|
bool txc1 = str1 != NULL;
|
|
|
|
bool txc2 = str2 != NULL;
|
|
|
|
|
2020-02-02 16:54:34 -05:00
|
|
|
if(tx1 && tx2) {
|
|
|
|
return 0;
|
|
|
|
} else if(tx1) {
|
|
|
|
return -1;
|
|
|
|
} else if(tx2) {
|
|
|
|
return 1;
|
2020-02-07 00:20:08 -05:00
|
|
|
} else if(txc1 && txc2) {
|
|
|
|
return 0;
|
|
|
|
} else if(txc1) {
|
|
|
|
return -1;
|
|
|
|
} else if(txc2) {
|
|
|
|
return 1;
|
2020-02-02 16:54:34 -05:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint do_sort(GtkFlowBoxChild* child1, GtkFlowBoxChild* child2, gpointer data) {
|
|
|
|
(void) data;
|
2020-02-07 00:20:08 -05:00
|
|
|
gtk_flow_box_get_child_at_index(GTK_FLOW_BOX(inner_box), 0);
|
|
|
|
|
2020-02-02 16:54:34 -05:00
|
|
|
GtkWidget* box1 = gtk_bin_get_child(GTK_BIN(child1));
|
|
|
|
GtkWidget* box2 = gtk_bin_get_child(GTK_BIN(child2));
|
|
|
|
if(GTK_IS_EXPANDER(box1)) {
|
|
|
|
box1 = gtk_expander_get_label_widget(GTK_EXPANDER(box1));
|
|
|
|
}
|
|
|
|
if(GTK_IS_EXPANDER(box2)) {
|
|
|
|
box2 = gtk_expander_get_label_widget(GTK_EXPANDER(box2));
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar* text1 = wofi_property_box_get_property(WOFI_PROPERTY_BOX(box1), "filter");
|
|
|
|
const gchar* text2 = wofi_property_box_get_property(WOFI_PROPERTY_BOX(box2), "filter");
|
2020-02-07 00:47:59 -05:00
|
|
|
uint64_t index1 = strtol(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box1), "index"), NULL, 10);
|
|
|
|
uint64_t index2 = strtol(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box2), "index"), NULL, 10);
|
2020-04-11 01:12:43 -04:00
|
|
|
|
2020-02-07 21:10:06 -05:00
|
|
|
if(text1 == NULL || text2 == NULL) {
|
|
|
|
return index1 - index2;
|
|
|
|
}
|
2020-04-11 01:12:43 -04:00
|
|
|
|
|
|
|
uint64_t fallback = 0;
|
|
|
|
switch(sort_order) {
|
|
|
|
case SORT_ORDER_DEFAULT:
|
|
|
|
fallback = index1 - index2;
|
|
|
|
break;
|
|
|
|
case SORT_ORDER_ALPHABETICAL:
|
|
|
|
if(insensitive) {
|
|
|
|
fallback = strcasecmp(text1, text2);
|
|
|
|
} else {
|
|
|
|
fallback = strcmp(text1, text2);
|
2020-02-07 21:04:37 -05:00
|
|
|
}
|
2020-04-11 01:12:43 -04:00
|
|
|
break;
|
2020-02-02 16:54:34 -05:00
|
|
|
}
|
|
|
|
|
2020-04-11 01:12:43 -04:00
|
|
|
if(filter == NULL || strcmp(filter, "") == 0) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint primary = 0;
|
2020-02-02 16:54:34 -05:00
|
|
|
switch(matching) {
|
2022-06-07 09:46:45 -04:00
|
|
|
case MATCHING_MODE_MULTI_CONTAINS:
|
|
|
|
primary = multi_contains_sort(text1, text2);
|
|
|
|
if(primary == 0) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return primary;
|
2020-02-02 16:54:34 -05:00
|
|
|
case MATCHING_MODE_CONTAINS:
|
2020-04-11 01:12:43 -04:00
|
|
|
primary = contains_sort(text1, text2);
|
|
|
|
if(primary == 0) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return primary;
|
2020-02-02 16:54:34 -05:00
|
|
|
case MATCHING_MODE_FUZZY:
|
2020-04-11 01:12:43 -04:00
|
|
|
primary = fuzzy_sort(text1, text2);
|
|
|
|
if(primary == 0) {
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
return primary;
|
2020-02-02 16:54:34 -05:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:02 -05:00
|
|
|
static void select_first(void) {
|
2020-05-18 22:28:07 -04:00
|
|
|
GtkFlowBoxChild* child = gtk_flow_box_get_child_at_index(GTK_FLOW_BOX(inner_box), 1);
|
2020-02-16 00:53:02 -05:00
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(child));
|
|
|
|
gtk_flow_box_select_child(GTK_FLOW_BOX(inner_box), GTK_FLOW_BOX_CHILD(child));
|
|
|
|
}
|
|
|
|
|
2020-02-16 17:43:14 -05:00
|
|
|
static GdkModifierType get_mask_from_keyval(guint keyval) {
|
|
|
|
switch(keyval) {
|
|
|
|
case GDK_KEY_Shift_L:
|
|
|
|
case GDK_KEY_Shift_R:
|
|
|
|
return GDK_SHIFT_MASK;
|
|
|
|
case GDK_KEY_Control_L:
|
|
|
|
case GDK_KEY_Control_R:
|
|
|
|
return GDK_CONTROL_MASK;
|
2020-04-06 18:35:22 -04:00
|
|
|
case GDK_KEY_Alt_L:
|
|
|
|
case GDK_KEY_Alt_R:
|
|
|
|
return GDK_MOD1_MASK;
|
2020-02-16 17:43:14 -05:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkModifierType get_mask_from_name(char* name) {
|
|
|
|
return get_mask_from_keyval(gdk_keyval_from_name(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_up(void) {
|
|
|
|
user_moved = true;
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_down(void) {
|
|
|
|
user_moved = true;
|
|
|
|
if(outer_orientation == GTK_ORIENTATION_VERTICAL) {
|
|
|
|
if(gtk_widget_has_focus(entry) || gtk_widget_has_focus(scroll)) {
|
|
|
|
select_first();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_left(void) {
|
|
|
|
user_moved = true;
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_right(void) {
|
|
|
|
user_moved = true;
|
|
|
|
if(outer_orientation == GTK_ORIENTATION_HORIZONTAL) {
|
|
|
|
if(gtk_widget_has_focus(entry) || gtk_widget_has_focus(scroll)) {
|
|
|
|
select_first();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_RIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_forward(void) {
|
|
|
|
user_moved = true;
|
|
|
|
if(gtk_widget_has_focus(entry) || gtk_widget_has_focus(scroll)) {
|
|
|
|
select_first();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_TAB_FORWARD);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_backward(void) {
|
|
|
|
user_moved = true;
|
|
|
|
gtk_widget_child_focus(window, GTK_DIR_TAB_BACKWARD);
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:27:44 -04:00
|
|
|
static void move_pgup(void) {
|
|
|
|
uint64_t lines = height / max_height;
|
|
|
|
for(size_t count = 0; count < lines; ++count) {
|
|
|
|
move_up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_pgdn(void) {
|
|
|
|
uint64_t lines = height / max_height;
|
|
|
|
for(size_t count = 0; count < lines; ++count) {
|
|
|
|
move_down();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 17:43:14 -05:00
|
|
|
static void do_exit(void) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:41:12 -04:00
|
|
|
static void do_expand(void) {
|
|
|
|
GList* children = gtk_flow_box_get_selected_children(GTK_FLOW_BOX(inner_box));
|
|
|
|
if(children->data != NULL && gtk_widget_has_focus(children->data)) {
|
|
|
|
GtkWidget* expander = gtk_bin_get_child(children->data);
|
|
|
|
if(GTK_IS_EXPANDER(expander)) {
|
|
|
|
g_signal_emit_by_name(expander, "activate");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_list_free(children);
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:17:12 -04:00
|
|
|
static void do_hide_search(void) {
|
|
|
|
gtk_widget_set_visible(entry, !gtk_widget_get_visible(entry));
|
|
|
|
update_surface_size();
|
|
|
|
}
|
|
|
|
|
2020-11-05 00:10:57 -05:00
|
|
|
static void do_copy(void) {
|
|
|
|
GList* children = gtk_flow_box_get_selected_children(GTK_FLOW_BOX(inner_box));
|
|
|
|
if(children->data != NULL && gtk_widget_has_focus(children->data)) {
|
|
|
|
GtkWidget* widget = gtk_bin_get_child(children->data);
|
|
|
|
if(GTK_IS_EXPANDER(widget)) {
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(widget));
|
|
|
|
GtkListBoxRow* row = gtk_list_box_get_selected_row(GTK_LIST_BOX(box));
|
|
|
|
if(row == NULL) {
|
|
|
|
widget = gtk_expander_get_label_widget(GTK_EXPANDER(widget));
|
|
|
|
} else {
|
|
|
|
widget = gtk_bin_get_child(GTK_BIN(row));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(WOFI_IS_PROPERTY_BOX(widget)) {
|
|
|
|
const gchar* action = wofi_property_box_get_property(WOFI_PROPERTY_BOX(widget), "action");
|
|
|
|
if(action == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fds[2];
|
2021-10-29 15:34:49 -04:00
|
|
|
if (pipe(fds) == -1) {
|
|
|
|
perror("pipe broken");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-11-05 00:10:57 -05:00
|
|
|
if(fork() == 0) {
|
|
|
|
close(fds[1]);
|
|
|
|
dup2(fds[0], STDIN_FILENO);
|
|
|
|
execlp(copy_exec, copy_exec, NULL);
|
|
|
|
fprintf(stderr, "%s could not be executed: %s\n", copy_exec, strerror(errno));
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
|
2021-11-11 01:00:50 -05:00
|
|
|
if(write(fds[1], action, strlen(action)) <= 0) {
|
2021-11-09 10:37:24 -05:00
|
|
|
fprintf(stderr, "fd pipe failed to write\n");
|
2021-10-29 15:34:49 -04:00
|
|
|
}
|
2020-11-05 00:10:57 -05:00
|
|
|
|
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
while(waitpid(-1, NULL, WNOHANG) > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 01:01:22 -05:00
|
|
|
static bool do_key_action(GdkEvent* event, char* mod, void (*action)(void)) {
|
2020-02-16 17:43:14 -05:00
|
|
|
if(mod != NULL) {
|
|
|
|
GdkModifierType mask = get_mask_from_name(mod);
|
|
|
|
if((event->key.state & mask) == mask) {
|
|
|
|
event->key.state &= ~mask;
|
|
|
|
action();
|
2020-03-08 01:01:22 -05:00
|
|
|
return true;
|
2020-02-16 17:43:14 -05:00
|
|
|
}
|
2020-03-08 01:01:22 -05:00
|
|
|
return false;
|
2020-02-16 17:43:14 -05:00
|
|
|
} else {
|
|
|
|
action();
|
2020-03-08 01:01:22 -05:00
|
|
|
return true;
|
2020-02-16 17:43:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool has_mod(guint state) {
|
2020-04-06 18:35:22 -04:00
|
|
|
return (state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK || (state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK || (state & GDK_MOD1_MASK) == GDK_MOD1_MASK;
|
2020-02-16 17:43:14 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 05:27:59 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 21:23:45 -04:00
|
|
|
static gboolean key_press(GtkWidget* widget, GdkEvent* event, gpointer data) {
|
2019-08-17 21:19:32 -04:00
|
|
|
(void) widget;
|
|
|
|
(void) data;
|
2020-02-16 00:53:02 -05:00
|
|
|
gchar* name = gdk_keyval_name(event->key.keyval);
|
2020-02-16 17:43:14 -05:00
|
|
|
bool printable = strlen(name) == 1 && isprint(name[0]) && !has_mod(event->key.state);
|
2020-02-16 00:53:02 -05:00
|
|
|
|
2020-02-29 05:27:59 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:02 -05:00
|
|
|
if(gtk_widget_has_focus(entry) && printable) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:50:48 -04:00
|
|
|
|
2020-03-08 01:01:22 -05:00
|
|
|
bool key_success = true;
|
2020-04-11 00:50:48 -04:00
|
|
|
struct key_entry* key_ent = map_get(keys, gdk_keyval_name(event->key.keyval));
|
|
|
|
|
|
|
|
if(key_ent != NULL && key_ent->action != NULL) {
|
|
|
|
key_success = do_key_action(event, key_ent->mod, key_ent->action);
|
|
|
|
} else if(key_ent != NULL) {
|
2019-11-19 01:16:06 -05:00
|
|
|
mod_shift = (event->key.state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK;
|
2019-11-19 01:13:14 -05:00
|
|
|
mod_ctrl = (event->key.state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK;
|
2019-11-19 01:16:06 -05:00
|
|
|
if(mod_shift) {
|
2019-09-27 02:24:48 -04:00
|
|
|
event->key.state &= ~GDK_SHIFT_MASK;
|
|
|
|
}
|
2019-11-19 01:13:14 -05:00
|
|
|
if(mod_ctrl) {
|
|
|
|
event->key.state &= ~GDK_CONTROL_MASK;
|
|
|
|
}
|
2019-09-19 03:45:57 -04:00
|
|
|
if(gtk_widget_has_focus(scroll)) {
|
|
|
|
gtk_entry_grab_focus_without_selecting(GTK_ENTRY(entry));
|
|
|
|
}
|
2020-02-16 00:53:02 -05:00
|
|
|
GList* children = gtk_flow_box_get_selected_children(GTK_FLOW_BOX(inner_box));
|
|
|
|
if(gtk_widget_has_focus(entry)) {
|
|
|
|
g_signal_emit_by_name(entry, "activate", entry, NULL);
|
2020-08-27 22:18:42 -04:00
|
|
|
} else if(gtk_widget_has_focus(inner_box) || children->data != NULL) {
|
|
|
|
gpointer obj = children->data;
|
|
|
|
|
|
|
|
if(obj != NULL) {
|
|
|
|
GtkWidget* exp = gtk_bin_get_child(GTK_BIN(obj));
|
|
|
|
if(GTK_IS_EXPANDER(exp)) {
|
|
|
|
GtkWidget* box = gtk_bin_get_child(GTK_BIN(exp));
|
|
|
|
GtkListBoxRow* row = gtk_list_box_get_selected_row(GTK_LIST_BOX(box));
|
|
|
|
if(row != NULL) {
|
|
|
|
obj = row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_signal_emit_by_name(obj, "activate", obj, NULL);
|
2020-02-16 00:53:02 -05:00
|
|
|
}
|
2019-08-28 22:33:08 -04:00
|
|
|
}
|
2020-02-16 00:53:02 -05:00
|
|
|
g_list_free(children);
|
2020-04-06 18:35:22 -04:00
|
|
|
} else if(event->key.keyval == GDK_KEY_Shift_L || event->key.keyval == GDK_KEY_Shift_R) {
|
|
|
|
} else if(event->key.keyval == GDK_KEY_Control_L || event->key.keyval == GDK_KEY_Control_R) {
|
|
|
|
} else if(event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) {
|
2020-02-16 00:53:02 -05:00
|
|
|
} else {
|
2020-03-08 01:01:22 -05:00
|
|
|
key_success = false;
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
2020-03-08 01:01:22 -05:00
|
|
|
|
|
|
|
if(key_success) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if(!gtk_widget_has_focus(entry)) {
|
|
|
|
gtk_entry_grab_focus_without_selecting(GTK_ENTRY(entry));
|
|
|
|
}
|
|
|
|
return FALSE;
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|
|
|
|
|
2020-02-01 01:04:07 -05:00
|
|
|
static gboolean focus(GtkWidget* widget, GdkEvent* event, gpointer data) {
|
|
|
|
(void) data;
|
|
|
|
if(event->focus_change.in) {
|
|
|
|
gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_FOCUSED, TRUE);
|
|
|
|
} else {
|
|
|
|
gtk_widget_set_state_flags(widget, GTK_STATE_FLAG_NORMAL, TRUE);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:16:53 -04:00
|
|
|
static gboolean focus_entry(GtkWidget* widget, GdkEvent* event, gpointer data) {
|
|
|
|
(void) data;
|
|
|
|
if(widget == entry && dbus != NULL) {
|
|
|
|
GError* err = NULL;
|
2021-02-19 23:59:39 -05:00
|
|
|
GVariant* ret = g_dbus_proxy_call_sync(dbus, "SetVisible", g_variant_new("(b)", event->focus_change.in), G_DBUS_CALL_FLAGS_NONE, 2000, NULL, &err);
|
2020-05-09 16:20:19 -04:00
|
|
|
if(err != NULL) {
|
|
|
|
if(err->code != G_DBUS_ERROR_SERVICE_UNKNOWN) {
|
|
|
|
fprintf(stderr, "Error while changing OSK state %s\n", err->message);
|
|
|
|
}
|
2020-05-09 16:16:53 -04:00
|
|
|
g_error_free(err);
|
2021-02-20 00:00:40 -05:00
|
|
|
} else {
|
|
|
|
g_variant_unref(ret);
|
2020-05-09 16:16:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
static void* get_plugin_proc(const char* prefix, const char* suffix) {
|
2019-11-04 16:25:44 -05:00
|
|
|
char* proc_name = utils_concat(3, "wofi_", prefix, suffix);
|
2019-09-13 03:25:25 -04:00
|
|
|
void* proc = dlsym(RTLD_DEFAULT, proc_name);
|
|
|
|
free(proc_name);
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2020-03-13 21:56:39 -04:00
|
|
|
static void* load_mode(char* _mode, char* name, struct mode* mode_ptr, struct map* props) {
|
2020-01-23 23:26:33 -05:00
|
|
|
char* dso = strstr(_mode, ".so");
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-03-13 21:56:39 -04:00
|
|
|
mode_ptr->name = strdup(name);
|
2020-01-20 23:09:40 -05:00
|
|
|
|
2020-01-23 23:26:33 -05:00
|
|
|
void (*init)(struct mode* _mode, struct map* props);
|
2020-03-13 20:08:35 -04:00
|
|
|
void (*load)(struct mode* _mode);
|
2020-01-23 23:26:33 -05:00
|
|
|
const char** (*get_arg_names)(void);
|
|
|
|
size_t (*get_arg_count)(void);
|
|
|
|
bool (*no_entry)(void);
|
2019-09-13 03:25:25 -04:00
|
|
|
if(dso == NULL) {
|
2020-03-13 16:00:59 -04:00
|
|
|
mode_ptr->dso = NULL;
|
2020-01-23 23:26:33 -05:00
|
|
|
init = get_plugin_proc(_mode, "_init");
|
2020-03-13 20:08:35 -04:00
|
|
|
load = get_plugin_proc(_mode, "_load");
|
2020-01-23 23:26:33 -05:00
|
|
|
get_arg_names = get_plugin_proc(_mode, "_get_arg_names");
|
|
|
|
get_arg_count = get_plugin_proc(_mode, "_get_arg_count");
|
|
|
|
mode_ptr->mode_exec = get_plugin_proc(_mode, "_exec");
|
|
|
|
mode_ptr->mode_get_widget = get_plugin_proc(_mode, "_get_widget");
|
|
|
|
no_entry = get_plugin_proc(_mode, "_no_entry");
|
2019-09-13 03:25:25 -04:00
|
|
|
} else {
|
|
|
|
char* plugins_dir = utils_concat(2, config_dir, "/plugins/");
|
2020-01-23 23:26:33 -05:00
|
|
|
char* full_name = utils_concat(2, plugins_dir, _mode);
|
2020-03-13 16:00:59 -04:00
|
|
|
mode_ptr->dso = strdup(full_name);
|
2019-11-04 16:29:53 -05:00
|
|
|
void* plugin = dlopen(full_name, RTLD_LAZY | RTLD_LOCAL);
|
2019-09-13 03:25:25 -04:00
|
|
|
free(full_name);
|
|
|
|
free(plugins_dir);
|
|
|
|
init = dlsym(plugin, "init");
|
2020-03-13 20:08:35 -04:00
|
|
|
load = dlsym(plugin, "load");
|
2019-12-02 01:58:30 -05:00
|
|
|
get_arg_names = dlsym(plugin, "get_arg_names");
|
|
|
|
get_arg_count = dlsym(plugin, "get_arg_count");
|
2019-11-04 23:56:40 -05:00
|
|
|
mode_ptr->mode_exec = dlsym(plugin, "exec");
|
2020-01-16 19:35:51 -05:00
|
|
|
mode_ptr->mode_get_widget = dlsym(plugin, "get_widget");
|
2020-01-23 23:26:33 -05:00
|
|
|
no_entry = dlsym(plugin, "no_entry");
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:08:35 -04:00
|
|
|
if(load != NULL) {
|
|
|
|
load(mode_ptr);
|
|
|
|
}
|
|
|
|
|
2019-12-02 01:58:30 -05:00
|
|
|
const char** arg_names = NULL;
|
|
|
|
size_t arg_count = 0;
|
|
|
|
if(get_arg_names != NULL && get_arg_count != NULL) {
|
|
|
|
arg_names = get_arg_names();
|
|
|
|
arg_count = get_arg_count();
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:26:33 -05:00
|
|
|
if(mode == NULL && no_entry != NULL && no_entry()) {
|
2020-03-13 15:27:26 -04:00
|
|
|
mode = mode_ptr->name;
|
2020-01-23 23:26:33 -05:00
|
|
|
}
|
|
|
|
|
2019-12-02 01:58:30 -05:00
|
|
|
for(size_t count = 0; count < arg_count; ++count) {
|
|
|
|
const char* arg = arg_names[count];
|
2020-03-13 21:56:39 -04:00
|
|
|
char* full_name = utils_concat(3, name, "-", arg);
|
2019-12-02 01:58:30 -05:00
|
|
|
map_put(props, arg, config_get(config, full_name, NULL));
|
|
|
|
free(full_name);
|
|
|
|
}
|
2020-02-01 00:55:28 -05:00
|
|
|
return init;
|
|
|
|
}
|
2019-12-02 01:58:30 -05:00
|
|
|
|
2020-02-01 02:15:13 -05:00
|
|
|
static struct mode* add_mode(char* _mode) {
|
2020-02-01 00:55:28 -05:00
|
|
|
struct mode* mode_ptr = calloc(1, sizeof(struct mode));
|
|
|
|
struct map* props = map_init();
|
2020-03-13 21:56:39 -04:00
|
|
|
void (*init)(struct mode* _mode, struct map* props) = load_mode(_mode, _mode, mode_ptr, props);
|
2020-02-01 00:55:28 -05:00
|
|
|
|
|
|
|
if(init == NULL) {
|
|
|
|
free(mode_ptr->name);
|
2020-03-13 16:00:59 -04:00
|
|
|
free(mode_ptr->dso);
|
2020-02-01 00:55:28 -05:00
|
|
|
free(mode_ptr);
|
|
|
|
map_free(props);
|
|
|
|
|
|
|
|
mode_ptr = calloc(1, sizeof(struct mode));
|
|
|
|
props = map_init();
|
|
|
|
|
2020-03-13 15:30:00 -04:00
|
|
|
char* name = utils_concat(3, "lib", _mode, ".so");
|
2020-03-13 21:56:39 -04:00
|
|
|
init = load_mode(name, _mode, mode_ptr, props);
|
2020-03-13 15:30:00 -04:00
|
|
|
free(name);
|
2020-02-01 00:55:28 -05:00
|
|
|
|
|
|
|
if(init == NULL) {
|
2020-03-13 15:30:00 -04:00
|
|
|
free(mode_ptr->name);
|
2020-03-13 16:00:59 -04:00
|
|
|
free(mode_ptr->dso);
|
2020-03-13 15:30:00 -04:00
|
|
|
free(mode_ptr);
|
|
|
|
map_free(props);
|
|
|
|
|
|
|
|
mode_ptr = calloc(1, sizeof(struct mode));
|
|
|
|
props = map_init();
|
|
|
|
|
2020-03-13 21:56:39 -04:00
|
|
|
init = load_mode("external", _mode, mode_ptr, props);
|
2020-03-13 15:30:00 -04:00
|
|
|
|
|
|
|
map_put(props, "exec", _mode);
|
|
|
|
|
|
|
|
if(init == NULL) {
|
|
|
|
fprintf(stderr, "I would love to show %s but Idk what it is\n", _mode);
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-02-01 00:55:28 -05:00
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2020-02-01 00:55:28 -05:00
|
|
|
map_put_void(modes, _mode, mode_ptr);
|
|
|
|
init(mode_ptr, props);
|
2019-12-02 01:58:30 -05:00
|
|
|
|
|
|
|
map_free(props);
|
2020-02-01 02:15:13 -05:00
|
|
|
return mode_ptr;
|
2019-11-04 23:56:40 -05:00
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
static void* start_mode_thread(void* data) {
|
|
|
|
char* mode = data;
|
|
|
|
if(strchr(mode, ',') != NULL) {
|
|
|
|
char* save_ptr;
|
|
|
|
char* str = strtok_r(mode, ",", &save_ptr);
|
|
|
|
do {
|
|
|
|
struct mode* mode_ptr = add_mode(str);
|
|
|
|
wl_list_insert(&mode_list, &mode_ptr->link);
|
|
|
|
} while((str = strtok_r(NULL, ",", &save_ptr)) != NULL);
|
|
|
|
} else {
|
|
|
|
struct mode* mode_ptr = add_mode(mode);
|
|
|
|
wl_list_insert(&mode_list, &mode_ptr->link);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:50:48 -04:00
|
|
|
static void parse_mods(char* key, void (*action)(void)) {
|
|
|
|
char* tmp = strdup(key);
|
|
|
|
char* save_ptr;
|
|
|
|
char* str = strtok_r(tmp, ",", &save_ptr);
|
|
|
|
do {
|
|
|
|
if(str == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char* hyphen = strchr(str, '-');
|
|
|
|
char* mod;
|
|
|
|
if(hyphen != NULL) {
|
|
|
|
*hyphen = 0;
|
|
|
|
guint key1 = gdk_keyval_from_name(str);
|
|
|
|
guint key2 = gdk_keyval_from_name(hyphen + 1);
|
|
|
|
if(get_mask_from_keyval(key1) != 0) {
|
|
|
|
mod = str;
|
|
|
|
str = hyphen + 1;
|
|
|
|
} else if(get_mask_from_keyval(key2) != 0) {
|
|
|
|
mod = hyphen + 1;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Neither %s nor %s is a modifier, this is not supported\n", str, hyphen + 1);
|
|
|
|
mod = NULL;
|
|
|
|
}
|
2020-02-16 17:43:14 -05:00
|
|
|
} else {
|
2020-04-11 00:50:48 -04:00
|
|
|
mod = NULL;
|
2020-02-16 17:43:14 -05:00
|
|
|
}
|
2020-04-11 00:50:48 -04:00
|
|
|
struct key_entry* entry = malloc(sizeof(struct key_entry));
|
2020-04-11 00:55:03 -04:00
|
|
|
if(mod == NULL) {
|
|
|
|
entry->mod = NULL;
|
|
|
|
} else {
|
|
|
|
entry->mod = strdup(mod);
|
|
|
|
}
|
2020-04-11 00:50:48 -04:00
|
|
|
entry->action = action;
|
|
|
|
map_put_void(keys, str, entry);
|
|
|
|
} while((str = strtok_r(NULL, ",", &save_ptr)) != NULL);
|
|
|
|
free(tmp);
|
2020-02-16 17:43:14 -05:00
|
|
|
}
|
|
|
|
|
2020-02-25 20:13:32 -05:00
|
|
|
static void get_output_name(void* data, struct zxdg_output_v1* output, const char* name) {
|
|
|
|
(void) output;
|
|
|
|
struct output_node* node = data;
|
|
|
|
node->name = strdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_output_res(void* data, struct zxdg_output_v1* output, int32_t width, int32_t height) {
|
|
|
|
(void) output;
|
|
|
|
struct output_node* node = data;
|
|
|
|
node->width = width;
|
|
|
|
node->height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_output_pos(void* data, struct zxdg_output_v1* output, int32_t x, int32_t y) {
|
|
|
|
(void) output;
|
|
|
|
struct output_node* node = data;
|
|
|
|
node->x = x;
|
|
|
|
node->y = y;
|
|
|
|
}
|
|
|
|
|
2020-03-03 03:04:53 -05:00
|
|
|
static gboolean do_percent_size(gpointer data) {
|
|
|
|
char** geo_str = data;
|
|
|
|
bool width_percent = strchr(geo_str[0], '%') != NULL;
|
2020-03-03 14:13:11 -05:00
|
|
|
bool height_percent = strchr(geo_str[1], '%') != NULL && lines == 0;
|
2020-05-18 23:16:57 -04:00
|
|
|
|
2020-03-03 03:04:53 -05:00
|
|
|
GdkMonitor* monitor = gdk_display_get_monitor_at_window(gdk_display_get_default(), gtk_widget_get_window(window));
|
2020-05-18 23:16:57 -04:00
|
|
|
|
2020-03-03 03:04:53 -05:00
|
|
|
GdkRectangle rect;
|
|
|
|
gdk_monitor_get_geometry(monitor, &rect);
|
2020-05-18 23:16:57 -04:00
|
|
|
|
|
|
|
if(rect.width == resolution.width && rect.height == resolution.height) {
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolution = rect;
|
|
|
|
|
2020-03-03 03:04:53 -05:00
|
|
|
if(width_percent) {
|
2020-05-18 23:16:57 -04:00
|
|
|
uint64_t w_percent = strtol(geo_str[0], NULL, 10);
|
|
|
|
width = (w_percent / 100.f) * rect.width;
|
2020-03-03 03:04:53 -05:00
|
|
|
}
|
|
|
|
if(height_percent) {
|
2020-05-18 23:16:57 -04:00
|
|
|
uint64_t h_percent = strtol(geo_str[1], NULL, 10);
|
|
|
|
height = (h_percent / 100.f) * rect.height;
|
2020-03-03 03:04:53 -05:00
|
|
|
}
|
|
|
|
update_surface_size();
|
2020-05-18 23:16:57 -04:00
|
|
|
return G_SOURCE_CONTINUE;
|
2020-03-03 03:04:53 -05:00
|
|
|
}
|
|
|
|
|
2019-12-02 01:58:30 -05:00
|
|
|
void wofi_init(struct map* _config) {
|
|
|
|
config = _config;
|
2020-03-03 03:10:12 -05:00
|
|
|
char* width_str = config_get(config, "width", "50%");
|
|
|
|
char* height_str = config_get(config, "height", "40%");
|
2020-03-02 15:06:14 -05:00
|
|
|
width = strtol(width_str, NULL, 10);
|
|
|
|
height = strtol(height_str, NULL, 10);
|
2020-01-06 03:28:57 -05:00
|
|
|
x = map_get(config, "x");
|
|
|
|
y = map_get(config, "y");
|
2019-08-17 22:22:09 -04:00
|
|
|
bool normal_window = strcmp(config_get(config, "normal_window", "false"), "true") == 0;
|
2020-01-23 23:26:33 -05:00
|
|
|
char* mode = map_get(config, "mode");
|
2020-01-07 21:56:57 -05:00
|
|
|
GtkOrientation orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "vertical", "horizontal");
|
2019-10-13 01:24:35 -04:00
|
|
|
outer_orientation = config_get_mnemonic(config, "orientation", "vertical", 2, "horizontal", "vertical");
|
2020-01-07 21:56:57 -05:00
|
|
|
GtkAlign halign = config_get_mnemonic(config, "halign", "fill", 4, "fill", "start", "end", "center");
|
2019-12-23 04:27:49 -05:00
|
|
|
content_halign = config_get_mnemonic(config, "content_halign", "fill", 4, "fill", "start", "end", "center");
|
2019-10-13 01:24:35 -04:00
|
|
|
char* default_valign = "start";
|
|
|
|
if(outer_orientation == GTK_ORIENTATION_HORIZONTAL) {
|
|
|
|
default_valign = "center";
|
|
|
|
}
|
2020-01-07 21:56:57 -05:00
|
|
|
GtkAlign valign = config_get_mnemonic(config, "valign", default_valign, 4, "fill", "start", "end", "center");
|
2019-08-17 21:19:32 -04:00
|
|
|
char* prompt = config_get(config, "prompt", mode);
|
2020-02-07 15:23:57 -05:00
|
|
|
uint64_t filter_rate = strtol(config_get(config, "filter_rate", "100"), NULL, 10);
|
2019-08-28 02:15:18 -04:00
|
|
|
allow_images = strcmp(config_get(config, "allow_images", "false"), "true") == 0;
|
2019-08-28 03:58:45 -04:00
|
|
|
allow_markup = strcmp(config_get(config, "allow_markup", "false"), "true") == 0;
|
2019-08-28 20:36:20 -04:00
|
|
|
image_size = strtol(config_get(config, "image_size", "32"), NULL, 10);
|
2019-08-28 23:14:38 -04:00
|
|
|
cache_file = map_get(config, "cache_file");
|
2019-09-13 03:25:25 -04:00
|
|
|
config_dir = map_get(config, "config_dir");
|
2019-09-27 02:24:48 -04:00
|
|
|
terminal = map_get(config, "term");
|
2019-10-23 00:19:57 -04:00
|
|
|
char* password_char = map_get(config, "password_char");
|
2019-11-04 20:46:39 -05:00
|
|
|
exec_search = strcmp(config_get(config, "exec_search", "false"), "true") == 0;
|
2019-11-18 16:42:40 -05:00
|
|
|
bool hide_scroll = strcmp(config_get(config, "hide_scroll", "false"), "true") == 0;
|
2022-06-07 09:46:45 -04:00
|
|
|
matching = config_get_mnemonic(config, "matching", "contains", 3, "contains", "multi-contains", "fuzzy");
|
2019-11-25 17:56:46 -05:00
|
|
|
insensitive = strcmp(config_get(config, "insensitive", "false"), "true") == 0;
|
2019-12-16 21:36:21 -05:00
|
|
|
parse_search = strcmp(config_get(config, "parse_search", "false"), "true") == 0;
|
2020-01-07 21:30:34 -05:00
|
|
|
location = config_get_mnemonic(config, "location", "center", 18,
|
|
|
|
"center", "top_left", "top", "top_right", "right", "bottom_right", "bottom", "bottom_left", "left",
|
|
|
|
"0", "1", "2", "3", "4", "5", "6", "7", "8");
|
2020-01-10 17:34:49 -05:00
|
|
|
no_actions = strcmp(config_get(config, "no_actions", "false"), "true") == 0;
|
2020-02-08 21:04:38 -05:00
|
|
|
lines = strtol(config_get(config, "lines", "0"), NULL, 10);
|
2020-06-16 18:41:28 -04:00
|
|
|
max_lines = lines;
|
2020-02-06 21:22:50 -05:00
|
|
|
columns = strtol(config_get(config, "columns", "1"), NULL, 10);
|
2020-02-07 21:04:37 -05:00
|
|
|
sort_order = config_get_mnemonic(config, "sort_order", "default", 2, "default", "alphabetical");
|
2020-02-21 03:43:44 -05:00
|
|
|
line_wrap = config_get_mnemonic(config, "line_wrap", "off", 4, "off", "word", "char", "word_char") - 1;
|
2020-02-25 20:13:32 -05:00
|
|
|
bool global_coords = strcmp(config_get(config, "global_coords", "false"), "true") == 0;
|
2020-02-29 04:04:00 -05:00
|
|
|
bool hide_search = strcmp(config_get(config, "hide_search", "false"), "true") == 0;
|
2020-06-14 06:03:00 -04:00
|
|
|
char* search = map_get(config, "search");
|
2020-06-16 18:41:28 -04:00
|
|
|
dynamic_lines = strcmp(config_get(config, "dynamic_lines", "false"), "true") == 0;
|
2020-07-18 16:32:05 -04:00
|
|
|
char* monitor = map_get(config, "monitor");
|
2020-07-28 00:49:21 -04:00
|
|
|
char* layer = config_get(config, "layer", "top");
|
2020-11-05 00:10:57 -05:00
|
|
|
copy_exec = config_get(config, "copy_exec", "wl-copy");
|
2022-06-15 13:07:40 -04:00
|
|
|
|
2021-10-29 15:34:49 -04:00
|
|
|
pre_display_cmd = map_get(config, "pre_display_cmd");
|
2020-02-16 17:43:14 -05:00
|
|
|
|
2020-04-11 00:50:48 -04:00
|
|
|
keys = map_init_void();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* key_up = config_get(config, "key_up", "Up");
|
|
|
|
char* key_down = config_get(config, "key_down", "Down");
|
|
|
|
char* key_left = config_get(config, "key_left", "Left");
|
|
|
|
char* key_right = config_get(config, "key_right", "Right");
|
|
|
|
char* key_forward = config_get(config, "key_forward", "Tab");
|
|
|
|
char* key_backward = config_get(config, "key_backward", "ISO_Left_Tab");
|
|
|
|
char* key_submit = config_get(config, "key_submit", "Return");
|
|
|
|
char* key_exit = config_get(config, "key_exit", "Escape");
|
|
|
|
char* key_pgup = config_get(config, "key_pgup", "Page_Up");
|
|
|
|
char* key_pgdn = config_get(config, "key_pgdn", "Page_Down");
|
|
|
|
char* key_expand = config_get(config, "key_expand", "");
|
|
|
|
char* key_hide_search = config_get(config, "key_hide_search", "");
|
2020-11-05 00:10:57 -05:00
|
|
|
char* key_copy = config_get(config, "key_copy", "Control_L-c");
|
2020-04-11 00:50:48 -04:00
|
|
|
|
|
|
|
parse_mods(key_up, move_up);
|
|
|
|
parse_mods(key_down, move_down);
|
|
|
|
parse_mods(key_left, move_left);
|
|
|
|
parse_mods(key_right, move_right);
|
|
|
|
parse_mods(key_forward, move_forward);
|
|
|
|
parse_mods(key_backward, move_backward);
|
|
|
|
parse_mods(key_submit, NULL); //submit is a special case, when a NULL action is encountered submit is used instead
|
|
|
|
parse_mods(key_exit, do_exit);
|
|
|
|
parse_mods(key_pgup, move_pgup);
|
|
|
|
parse_mods(key_pgdn, move_pgdn);
|
|
|
|
parse_mods(key_expand, do_expand);
|
|
|
|
parse_mods(key_hide_search, do_hide_search);
|
2020-11-05 00:10:57 -05:00
|
|
|
parse_mods(key_copy, do_copy);
|
2020-02-16 17:43:14 -05:00
|
|
|
|
2019-11-04 23:56:40 -05:00
|
|
|
modes = map_init_void();
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2020-02-03 01:29:44 -05:00
|
|
|
if(lines > 0) {
|
2020-02-08 21:04:38 -05:00
|
|
|
height = 1;
|
2020-02-03 01:29:44 -05:00
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_widget_realize(window);
|
|
|
|
gtk_widget_set_name(window, "window");
|
2019-08-17 22:22:09 -04:00
|
|
|
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
|
|
|
|
gtk_window_resize(GTK_WINDOW(window), width, height);
|
|
|
|
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
|
|
|
|
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
|
2020-03-02 15:06:14 -05:00
|
|
|
|
2019-08-17 22:22:09 -04:00
|
|
|
if(!normal_window) {
|
2020-02-25 20:13:32 -05:00
|
|
|
wl_list_init(&outputs);
|
2020-08-11 22:07:49 -04:00
|
|
|
wl = gdk_wayland_display_get_wl_display(gdk_display_get_default());
|
2020-05-15 20:32:39 -04:00
|
|
|
|
|
|
|
if(wl == NULL) {
|
|
|
|
fprintf(stderr, "Failed to connect to wayland compositor\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:22:09 -04:00
|
|
|
struct wl_registry* registry = wl_display_get_registry(wl);
|
|
|
|
struct wl_registry_listener listener = {
|
|
|
|
.global = add_interface,
|
|
|
|
.global_remove = nop
|
|
|
|
};
|
|
|
|
wl_registry_add_listener(registry, &listener, NULL);
|
|
|
|
wl_display_roundtrip(wl);
|
2020-02-25 20:13:32 -05:00
|
|
|
|
2020-05-15 20:32:39 -04:00
|
|
|
if(shell == NULL) {
|
|
|
|
fprintf(stderr, "Compositor does not support wlr_layer_shell protocol, switching to normal window mode\n");
|
2020-05-15 20:36:45 -04:00
|
|
|
normal_window = true;
|
2020-05-15 20:32:39 -04:00
|
|
|
goto normal_win;
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:13:32 -05:00
|
|
|
struct output_node* node;
|
|
|
|
wl_list_for_each(node, &outputs, link) {
|
|
|
|
struct zxdg_output_v1* xdg_output = zxdg_output_manager_v1_get_xdg_output(output_manager, node->output);
|
|
|
|
struct zxdg_output_v1_listener* xdg_listener = malloc(sizeof(struct zxdg_output_v1_listener));
|
|
|
|
xdg_listener->description = nop;
|
|
|
|
xdg_listener->done = nop;
|
|
|
|
xdg_listener->logical_position = get_output_pos;
|
|
|
|
xdg_listener->logical_size = get_output_res;
|
|
|
|
xdg_listener->name = get_output_name;
|
|
|
|
zxdg_output_v1_add_listener(xdg_output, xdg_listener, node);
|
|
|
|
}
|
|
|
|
wl_display_roundtrip(wl);
|
|
|
|
|
|
|
|
ix = x == NULL ? 0 : strtol(x, NULL, 10);
|
|
|
|
iy = y == NULL ? 0 : strtol(y, NULL, 10);
|
|
|
|
|
|
|
|
struct wl_output* output = NULL;
|
|
|
|
if(global_coords) {
|
|
|
|
wl_list_for_each(node, &outputs, link) {
|
|
|
|
if(ix >= node->x && ix <= node->width + node->x
|
|
|
|
&& iy >= node->y && iy <= node->height + node->y) {
|
|
|
|
output = node->output;
|
|
|
|
ix -= node->x;
|
|
|
|
iy -= node->y;
|
2020-07-18 16:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(monitor != NULL) {
|
|
|
|
wl_list_for_each(node, &outputs, link) {
|
|
|
|
if(strcmp(monitor, node->name) == 0) {
|
|
|
|
output = node->output;
|
2020-02-25 20:13:32 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:32:39 -04:00
|
|
|
GdkWindow* gdk_win = gtk_widget_get_window(window);
|
|
|
|
|
2019-08-17 22:22:09 -04:00
|
|
|
gdk_wayland_window_set_use_custom_surface(gdk_win);
|
2020-02-08 21:04:38 -05:00
|
|
|
wl_surface = gdk_wayland_window_get_wl_surface(gdk_win);
|
2020-02-25 20:13:32 -05:00
|
|
|
|
2020-07-28 02:57:18 -04:00
|
|
|
enum zwlr_layer_shell_v1_layer wlr_layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
|
2020-07-28 00:49:21 -04:00
|
|
|
|
|
|
|
if(strcmp(layer, "background") == 0) {
|
|
|
|
wlr_layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
|
|
|
|
} else if(strcmp(layer, "bottom") == 0) {
|
|
|
|
wlr_layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
|
|
|
|
} else if(strcmp(layer, "top") == 0) {
|
|
|
|
wlr_layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
|
|
|
|
} else if(strcmp(layer, "overlay") == 0) {
|
|
|
|
wlr_layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_surface = zwlr_layer_shell_v1_get_layer_surface(shell, wl_surface, output, wlr_layer, "wofi");
|
2020-10-10 21:04:04 -04:00
|
|
|
setup_surface(wlr_surface);
|
2019-08-17 22:22:09 -04:00
|
|
|
struct zwlr_layer_surface_v1_listener* surface_listener = malloc(sizeof(struct zwlr_layer_surface_v1_listener));
|
|
|
|
surface_listener->configure = config_surface;
|
|
|
|
surface_listener->closed = nop;
|
2020-02-08 21:04:38 -05:00
|
|
|
zwlr_layer_surface_v1_add_listener(wlr_surface, surface_listener, NULL);
|
2019-08-17 22:22:09 -04:00
|
|
|
wl_surface_commit(wl_surface);
|
|
|
|
wl_display_roundtrip(wl);
|
|
|
|
}
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2020-05-15 20:32:39 -04:00
|
|
|
normal_win:
|
|
|
|
|
2019-10-13 01:24:35 -04:00
|
|
|
outer_box = gtk_box_new(outer_orientation, 0);
|
2019-08-19 20:34:12 -04:00
|
|
|
gtk_widget_set_name(outer_box, "outer-box");
|
|
|
|
gtk_container_add(GTK_CONTAINER(window), outer_box);
|
|
|
|
entry = gtk_search_entry_new();
|
2021-02-23 02:53:57 -05:00
|
|
|
g_signal_connect(entry, "size-allocate", G_CALLBACK(widget_allocate), NULL);
|
2020-02-03 01:29:44 -05:00
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
gtk_widget_set_name(entry, "input");
|
|
|
|
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), prompt);
|
2020-04-06 18:17:12 -04:00
|
|
|
gtk_container_add(GTK_CONTAINER(outer_box), entry);
|
|
|
|
gtk_widget_set_child_visible(entry, !hide_search);
|
|
|
|
|
2020-06-14 06:03:00 -04:00
|
|
|
if(search != NULL) {
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(entry), search);
|
|
|
|
}
|
|
|
|
|
2019-10-23 00:19:57 -04:00
|
|
|
if(password_char != NULL) {
|
|
|
|
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
|
|
|
|
gtk_entry_set_invisible_char(GTK_ENTRY(entry), password_char[0]);
|
|
|
|
}
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2019-08-19 20:34:12 -04:00
|
|
|
scroll = gtk_scrolled_window_new(NULL, NULL);
|
2019-08-17 21:19:32 -04:00
|
|
|
gtk_widget_set_name(scroll, "scroll");
|
2019-08-19 20:34:12 -04:00
|
|
|
gtk_container_add(GTK_CONTAINER(outer_box), scroll);
|
2019-11-18 16:42:40 -05:00
|
|
|
if(hide_scroll) {
|
|
|
|
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_EXTERNAL, GTK_POLICY_EXTERNAL);
|
|
|
|
}
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2019-08-28 22:33:08 -04:00
|
|
|
inner_box = gtk_flow_box_new();
|
2020-04-25 12:09:17 -04:00
|
|
|
gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(inner_box), GTK_SELECTION_BROWSE);
|
2020-02-06 21:22:50 -05:00
|
|
|
gtk_flow_box_set_max_children_per_line(GTK_FLOW_BOX(inner_box), columns);
|
2019-08-28 22:33:08 -04:00
|
|
|
gtk_orientable_set_orientation(GTK_ORIENTABLE(inner_box), orientation);
|
|
|
|
gtk_widget_set_halign(inner_box, halign);
|
|
|
|
gtk_widget_set_valign(inner_box, valign);
|
|
|
|
|
2019-08-17 21:19:32 -04:00
|
|
|
gtk_widget_set_name(inner_box, "inner-box");
|
2019-08-28 22:33:08 -04:00
|
|
|
gtk_flow_box_set_activate_on_single_click(GTK_FLOW_BOX(inner_box), FALSE);
|
2019-10-13 02:56:27 -04:00
|
|
|
|
|
|
|
GtkWidget* wrapper_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
gtk_box_set_homogeneous(GTK_BOX(wrapper_box), TRUE);
|
|
|
|
gtk_container_add(GTK_CONTAINER(wrapper_box), inner_box);
|
|
|
|
gtk_container_add(GTK_CONTAINER(scroll), wrapper_box);
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2019-11-21 04:01:27 -05:00
|
|
|
switch(matching) {
|
2022-06-07 09:46:45 -04:00
|
|
|
case MATCHING_MODE_MULTI_CONTAINS:
|
|
|
|
gtk_flow_box_set_filter_func(GTK_FLOW_BOX(inner_box), do_multi_filter, NULL, NULL);
|
|
|
|
gtk_flow_box_set_sort_func(GTK_FLOW_BOX(inner_box), do_sort, NULL, NULL);
|
|
|
|
break;
|
2019-11-21 04:01:27 -05:00
|
|
|
case MATCHING_MODE_CONTAINS:
|
|
|
|
gtk_flow_box_set_filter_func(GTK_FLOW_BOX(inner_box), do_filter, NULL, NULL);
|
2020-02-02 16:54:34 -05:00
|
|
|
gtk_flow_box_set_sort_func(GTK_FLOW_BOX(inner_box), do_sort, NULL, NULL);
|
2019-11-21 04:01:27 -05:00
|
|
|
break;
|
|
|
|
case MATCHING_MODE_FUZZY:
|
2022-06-15 13:07:40 -04:00
|
|
|
gtk_flow_box_set_filter_func(GTK_FLOW_BOX(inner_box), do_fuzzy_filter, NULL,
|
|
|
|
NULL);
|
|
|
|
gtk_flow_box_set_sort_func(GTK_FLOW_BOX(inner_box), do_sort, NULL,
|
|
|
|
NULL);
|
2019-11-21 04:01:27 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2019-08-28 22:33:08 -04:00
|
|
|
g_signal_connect(inner_box, "child-activated", G_CALLBACK(activate_item), NULL);
|
|
|
|
g_signal_connect(inner_box, "selected-children-changed", G_CALLBACK(select_item), NULL);
|
2019-08-19 20:34:12 -04:00
|
|
|
g_signal_connect(entry, "activate", G_CALLBACK(activate_search), NULL);
|
|
|
|
g_signal_connect(window, "key-press-event", G_CALLBACK(key_press), NULL);
|
2020-02-01 01:04:07 -05:00
|
|
|
g_signal_connect(window, "focus-in-event", G_CALLBACK(focus), NULL);
|
|
|
|
g_signal_connect(window, "focus-out-event", G_CALLBACK(focus), NULL);
|
2020-05-09 16:16:53 -04:00
|
|
|
g_signal_connect(entry, "focus-in-event", G_CALLBACK(focus_entry), NULL);
|
|
|
|
g_signal_connect(entry, "focus-out-event", G_CALLBACK(focus_entry), NULL);
|
2020-03-09 17:27:07 -04:00
|
|
|
g_signal_connect(window, "destroy", G_CALLBACK(do_exit), NULL);
|
2019-08-17 21:19:32 -04:00
|
|
|
|
2020-05-09 16:16:53 -04:00
|
|
|
dbus = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, "sm.puri.OSK0", "/sm/puri/OSK0", "sm.puri.OSK0", NULL, NULL);
|
|
|
|
|
2020-02-29 05:27:59 -05:00
|
|
|
gdk_threads_add_timeout(filter_rate, do_search, NULL);
|
2020-02-07 14:51:46 -05:00
|
|
|
|
2020-05-12 18:21:13 -04:00
|
|
|
|
2020-03-03 03:04:53 -05:00
|
|
|
bool width_percent = strchr(width_str, '%') != NULL;
|
2020-03-03 14:13:11 -05:00
|
|
|
bool height_percent = strchr(height_str, '%') != NULL && lines == 0;
|
2020-03-03 03:04:53 -05:00
|
|
|
if(width_percent || height_percent) {
|
2020-05-18 23:16:57 -04:00
|
|
|
static char* geo_str[2];
|
2020-03-03 03:04:53 -05:00
|
|
|
geo_str[0] = width_str;
|
|
|
|
geo_str[1] = height_str;
|
2020-05-18 23:16:57 -04:00
|
|
|
gdk_threads_add_timeout(50, do_percent_size, geo_str);
|
|
|
|
do_percent_size(geo_str);
|
2020-03-03 03:04:53 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
wl_list_init(&mode_list);
|
2020-03-07 22:55:30 -05:00
|
|
|
|
2020-06-23 03:33:57 -04:00
|
|
|
pthread_create(&mode_thread, NULL, start_mode_thread, mode);
|
|
|
|
|
|
|
|
gdk_threads_add_idle(insert_all_widgets, &mode_list);
|
2020-03-07 22:55:30 -05:00
|
|
|
|
2019-08-18 01:00:23 -04:00
|
|
|
gtk_window_set_title(GTK_WINDOW(window), prompt);
|
2020-05-12 18:21:13 -04:00
|
|
|
gtk_widget_show_all(window);
|
2019-08-17 21:19:32 -04:00
|
|
|
}
|