Skip to content

Commit 39871ad

Browse files
committed
Implement built-in key navigation menu handling
Add function to handle built-in menu key navigation. Signed-off-by: gujie <[email protected]>
1 parent 62e661d commit 39871ad

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/input.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "settings.h"
55
#include "queues.h"
66
#include <stddef.h>
7+
#include <xkbcommon/xkbcommon.h>
78
#if defined(__linux__) || defined(__FreeBSD__)
89
#include <linux/input-event-codes.h>
910
#else
@@ -55,6 +56,35 @@ struct notification *get_notification_at(const int y) {
5556
return NULL;
5657
}
5758

59+
bool handle_builtin_menu_click(int x, int y) {
60+
if (!settings.built_in_menu) {
61+
return false;
62+
}
63+
64+
struct notification *n = get_notification_at(y);
65+
if (n) {
66+
if (menu_get_count(n) > 0) {
67+
struct menu *m = menu_get_at(n, x, y);
68+
if (m) {
69+
// find the index of the menu item
70+
for (guint i = 0; i < n->menus->len; i++) {
71+
struct menu *button = &g_array_index(n->menus, struct menu, i);
72+
if (button == m) {
73+
// update the selection
74+
update_menu_selection(n, i);
75+
break;
76+
}
77+
}
78+
79+
m->n = n;
80+
menu_activate(m); // activate the selected menu item
81+
return true;
82+
}
83+
}
84+
}
85+
return false;
86+
}
87+
5888
void input_handle_click(unsigned int button, bool button_down, int mouse_x, int mouse_y){
5989
LOG_I("Pointer handle button %i: %i", button, button_down);
6090

@@ -116,4 +146,59 @@ void input_handle_click(unsigned int button, bool button_down, int mouse_x, int
116146

117147
wake_up();
118148
}
149+
150+
static bool handle_builtin_menu_key(unsigned int keysym, bool pressed) {
151+
if (!settings.built_in_menu || !settings.built_in_menu_key_navigation) {
152+
return false;
153+
}
154+
155+
if (keysym == XKB_KEY_Right ||
156+
keysym == XKB_KEY_Left ||
157+
keysym == XKB_KEY_Up ||
158+
keysym == XKB_KEY_Down
159+
) {
160+
struct notification *n = queues_get_displayed_head();
161+
if (n) {
162+
if (menu_get_count(n) > 0) {
163+
struct menu *m = menu_get_selected(n);
164+
m->n = n;
165+
if (m) {
166+
if (pressed) {
167+
menu_move_selection(n, keysym);
168+
}
169+
return true;
170+
}
171+
}
172+
}
173+
}
174+
175+
if (keysym == XKB_KEY_Return ||
176+
keysym == XKB_KEY_KP_Enter
177+
) {
178+
struct notification *n = queues_get_displayed_head();
179+
if (n) {
180+
if (menu_get_count(n) > 0) {
181+
struct menu *m = menu_get_selected(n);
182+
m->n = n;
183+
if (m) {
184+
if (pressed) {
185+
// activate the selected menu item
186+
menu_activate(m);
187+
}
188+
return true;
189+
}
190+
}
191+
}
192+
}
193+
return false;
194+
}
195+
196+
void input_handle_key(unsigned int keysym, bool pressed) {
197+
LOG_I("Key %s: %u", pressed ? "pressed" : "released", keysym);
198+
199+
if (settings.built_in_menu && settings.built_in_menu_key_navigation) {
200+
if (handle_builtin_menu_key(keysym, pressed))
201+
return;
202+
}
203+
}
119204
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

src/input.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@
1414
*/
1515
void input_handle_click(unsigned int button, bool button_down, int mouse_x, int mouse_y);
1616

17+
/**
18+
* Handle incoming key events
19+
* @param keysym The key symbol
20+
* @param True if the key is pressed, false if it is released
21+
*/
22+
void input_handle_key(unsigned int keysym, bool pressed);
23+
1724
#endif
1825
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

0 commit comments

Comments
 (0)