From 55f211a5a5217f67abec6dee50b1d7052bf28b59 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 15 Apr 2026 18:45:23 +0200 Subject: Move to glib for getting desktop entries --- Makefile | 4 +-- launcher.c | 119 +++++++++++++++++-------------------------------------------- 2 files changed, 34 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index 8dc8245..d59002d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC ?= clang CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wunused -Wswitch-enum -INCLUDES := $(shell pkg-config --cflags xft libpulse) -LDFLAGS := $(shell pkg-config --libs x11 xft libpulse) -lpthread +INCLUDES := $(shell pkg-config --cflags xft libpulse gio-2.0 gio-unix-2.0) +LDFLAGS := $(shell pkg-config --libs x11 xft libpulse gio-2.0 gio-unix-2.0) -lpthread DESTDIR ?= /usr/local DISPLAY_NUM := 69 diff --git a/launcher.c b/launcher.c index 9c77958..b82577c 100644 --- a/launcher.c +++ b/launcher.c @@ -10,6 +10,8 @@ #include #include +#include + #include "glitch.h" #include "config.h" @@ -17,28 +19,7 @@ extern WindowManager wm; static void launcher_filter(void); -static char *trim_whitespace(char *str) { - char *end; - while (isspace((unsigned char)*str)) str++; - if (*str == 0) return str; - end = str + strlen(str) - 1; - while (end > str && isspace((unsigned char)*end)) end--; - end[1] = '\0'; - return str; -} - static void load_applications(void) { - const char *system_dirs[] = { - "/usr/share/applications", - "/usr/local/share/applications" - }; - - char home_dir[1024]; - char *home = getenv("HOME"); - if (home) { - snprintf(home_dir, sizeof(home_dir), "%s/.local/share/applications", home); - } - if (wm.launcher_items) { for (int i = 0; i < wm.launcher_items_count; i++) { free(wm.launcher_items[i].name); @@ -49,78 +30,42 @@ static void load_applications(void) { wm.launcher_items_count = 0; } - int capacity = 100; - wm.launcher_items = malloc(sizeof(LauncherItem) * capacity); + GList *apps = g_app_info_get_all(); + int total_apps = g_list_length(apps); + wm.launcher_items = malloc(sizeof(LauncherItem) * total_apps); - for (int d = -1; d < (int)LENGTH(system_dirs); d++) { - const char *path_to_open; - if (d == -1) { - if (!home) continue; - path_to_open = home_dir; - } else { - path_to_open = system_dirs[d]; + for (GList *l = apps; l != NULL; l = l->next) { + GAppInfo *app = (GAppInfo *)l->data; + + if (!g_app_info_should_show(app)) { + g_object_unref(app); + continue; } - DIR *dir = opendir(path_to_open); - if (!dir) continue; - - struct dirent *entry; - while ((entry = readdir(dir))) { - if (strstr(entry->d_name, ".desktop")) { - char desktop_path[2048]; - snprintf(desktop_path, sizeof(desktop_path), "%s/%s", path_to_open, entry->d_name); - - FILE *f = fopen(desktop_path, "r"); - if (!f) continue; - - char line[1024]; - char *name = NULL; - char *exec = NULL; - int no_display = 0; - int in_desktop_entry = 0; - - while (fgets(line, sizeof(line), f)) { - char *trimmed = trim_whitespace(line); - if (trimmed[0] == '[' && strstr(trimmed, "[Desktop Entry]")) { - in_desktop_entry = 1; - continue; - } - if (trimmed[0] == '[' && !strstr(trimmed, "[Desktop Entry]")) { - in_desktop_entry = 0; - } - - if (!in_desktop_entry) continue; - - if (strncmp(trimmed, "Name=", 5) == 0 && !name) { - name = strdup(trim_whitespace(trimmed + 5)); - } else if (strncmp(trimmed, "Exec=", 5) == 0 && !exec) { - char *e = strdup(trimmed + 5); - char *percent = strchr(e, '%'); - if (percent) *percent = '\0'; - exec = strdup(trim_whitespace(e)); - free(e); - } else if (strncmp(trimmed, "NoDisplay=true", 14) == 0) { - no_display = 1; - } - } - fclose(f); - - if (name && exec && !no_display) { - if (wm.launcher_items_count >= capacity) { - capacity *= 2; - wm.launcher_items = realloc(wm.launcher_items, sizeof(LauncherItem) * capacity); - } - wm.launcher_items[wm.launcher_items_count].name = name; - wm.launcher_items[wm.launcher_items_count].exec = exec; - wm.launcher_items_count++; - } else { - if (name) free(name); - if (exec) free(exec); - } + const char *name = g_app_info_get_name(app); + const char *exec = g_app_info_get_commandline(app); + + if (name && exec) { + wm.launcher_items[wm.launcher_items_count].name = strdup(name); + + char *e = strdup(exec); + char *percent = strchr(e, '%'); + if (percent) *percent = '\0'; + + // Trim potential trailing space after removing % + int len = strlen(e); + while (len > 0 && isspace((unsigned char)e[len-1])) { + e[--len] = '\0'; } + + wm.launcher_items[wm.launcher_items_count].exec = strdup(e); + free(e); + + wm.launcher_items_count++; } - closedir(dir); + g_object_unref(app); } + g_list_free(apps); } void toggle_launcher(const Arg *arg) { -- cgit v1.2.3