| 1 | #include "program.h" |
| 2 | #include "parser.h" |
| 3 | |
| 4 | #include <X11/Xlib.h> |
| 5 | #include <X11/Xft/Xft.h> |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | Program programs[MAX_ITEMS]; |
| 13 | int programcount = 0; |
| 14 | int window_width = 300; |
| 15 | int window_height = 240; |
| 16 | int item_height = 20; |
| 17 | int display_items = 10; |
| 18 | |
| 19 | void fetch_programs() { |
| 20 | char *xdg_data_home = getenv("XDG_DATA_HOME"); |
| 21 | if (!xdg_data_home) { |
| 22 | xdg_data_home = getenv("HOME"); |
| 23 | if (xdg_data_home) { |
| 24 | char path[MAX_NAME_LEN]; |
| 25 | snprintf(path, sizeof(path), "%s/.local/share/applications", xdg_data_home); |
| 26 | scan_desktop_files(path); |
| 27 | } |
| 28 | } else { |
| 29 | char path[MAX_NAME_LEN]; |
| 30 | snprintf(path, sizeof(path), "%s/applications", xdg_data_home); |
| 31 | scan_desktop_files(path); |
| 32 | } |
| 33 | |
| 34 | scan_desktop_files("/usr/share/applications"); |
| 35 | scan_desktop_files("/usr/local/share/applications"); |
| 36 | #if defined(__NetBSD__) |
| 37 | scan_desktop_files("/usr/pkg/share/applications"); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | void launch_program(const char *exec, const char *term) { |
| 42 | if (fork() == 0) { |
| 43 | setsid(); |
| 44 | if (strncmp(term, "true", 4) == 0) { |
| 45 | char *terminal = getenv("TERMINAL"); |
| 46 | if (!terminal) terminal = "xterm"; |
| 47 | execlp(terminal, terminal, "-e", exec, NULL); |
| 48 | } else { |
| 49 | execl("/bin/sh", "sh", "-c", exec, NULL); |
| 50 | } |
| 51 | exit(0); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int filtercount(const char *input) { |
| 56 | int count = 0; |
| 57 | |
| 58 | for (int i = 0; i < programcount; i++) { |
| 59 | if (strcasestr(programs[i].name, input) || strcasestr(programs[i].keys, input)) |
| 60 | count++; |
| 61 | } |
| 62 | |
| 63 | return count; |
| 64 | } |
| 65 | |
| 66 | void calculate_dimensions(int win_width, int win_height) { |
| 67 | window_width = win_width; |
| 68 | window_height = win_height; |
| 69 | item_height = win_height / (display_items + 2); |
| 70 | } |