| 1 | #include "control.h" |
| 2 | #include "display.h" |
| 3 | #include "utils.h" |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | void control_expose( |
| 10 | Display *display, Window window, GC gc, XEvent *event, char *input, int *sel) { |
| 11 | if (event->type == ConfigureNotify) |
| 12 | calculate_dimensions(event->xconfigure.width, event->xconfigure.height); |
| 13 | filterdisplay(display, window, gc, input, *sel); |
| 14 | } |
| 15 | |
| 16 | void control_keypress( |
| 17 | Display *display, Window window, GC gc, XEvent *event, char *input, int *sel) { |
| 18 | char buf[32]; |
| 19 | KeySym keysym; |
| 20 | int len = XLookupString(&event->xkey, buf, sizeof(buf) - 1, &keysym, NULL); |
| 21 | |
| 22 | if (keysym == XK_Up) { |
| 23 | if (*sel > 0) { |
| 24 | (*sel)--; |
| 25 | if (*sel < topidx) topidx--; |
| 26 | } |
| 27 | } else if (keysym == XK_Down) { |
| 28 | int filteredcount = filtercount(input); |
| 29 | if (*sel < filteredcount - 1) { |
| 30 | (*sel)++; |
| 31 | if (*sel >= topidx + display_items) topidx++; |
| 32 | } |
| 33 | } else if (keysym == XK_Page_Up) { |
| 34 | if (*sel > 0) { |
| 35 | *sel -= display_items; |
| 36 | if (*sel < topidx) topidx -= display_items; |
| 37 | } |
| 38 | } else if (keysym == XK_Page_Down) { |
| 39 | int filteredcount = filtercount(input); |
| 40 | if (*sel < filteredcount - 1) { |
| 41 | *sel += display_items; |
| 42 | if (*sel >= topidx + display_items) topidx += display_items; |
| 43 | } |
| 44 | } else if (keysym == XK_BackSpace && strlen(input) > 0) { |
| 45 | input[strlen(input) - 1] = '\0'; |
| 46 | *sel = 0; |
| 47 | topidx = 0; |
| 48 | } else if (keysym == XK_Return) { |
| 49 | int visible_index = 0; |
| 50 | int found = 0; |
| 51 | for (int i = 0; i < programcount; i++) { |
| 52 | if (!strcasestr(programs[i].name, input)) continue; |
| 53 | if (visible_index == *sel) { |
| 54 | launch_program(programs[i].exec, programs[i].term); |
| 55 | found = 1; |
| 56 | break; |
| 57 | } |
| 58 | visible_index++; |
| 59 | } |
| 60 | |
| 61 | if (found == 0 && launch_path(input)) { |
| 62 | launch_program(input, "false"); |
| 63 | } |
| 64 | exit(0); |
| 65 | } else if (keysym == XK_Escape) { |
| 66 | exit(0); |
| 67 | } else if (len > 0 && len < MAX_NAME_LEN - 1) { |
| 68 | strncat(input, buf, len); |
| 69 | *sel = 0; |
| 70 | topidx = 0; |
| 71 | } |
| 72 | |
| 73 | filterdisplay(display, window, gc, input, *sel); |
| 74 | } |
| 75 | |
| 76 | void control_buttonpress(XEvent *event, Program *programs, int item_height) { |
| 77 | int x = event->xbutton.x; |
| 78 | int y = event->xbutton.y; |
| 79 | |
| 80 | for (int i = 0; i < programcount; i++) { |
| 81 | if ( |
| 82 | x >= programs[i].x && |
| 83 | x <= programs[i].x + programs[i].width && |
| 84 | y >= programs[i].y - item_height && |
| 85 | y <= programs[i].y |
| 86 | ) { |
| 87 | launch_program(programs[i].exec, programs[i].term); |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |