-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyhandler.h
126 lines (113 loc) · 3.43 KB
/
keyhandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef WITHX11
// Event printing functions
static int print_key_event(struct libinput_event *event)
{
struct libinput_event_keyboard *keyboard = libinput_event_get_keyboard_event(event);
guint32 key_code = libinput_event_keyboard_get_key(keyboard);
enum libinput_key_state state_code = libinput_event_keyboard_get_key_state(keyboard);
const guint32 KEY_CAPSLOCK_CODE = KEY_CAPSLOCK;
const guint32 KEY_NUMLOCK_CODE = KEY_NUMLOCK;
if (showcap && key_code == KEY_CAPSLOCK_CODE && state_code == LIBINPUT_KEY_STATE_RELEASED)
{
capstate ^= 1;
const gchar *icon_name = capstate ? "keyboard-caps-enabled" : "keyboard-caps-disabled";
const gchar *status_message = capstate ? "Caps Lock: Enabled" : "Caps Lock: Disabled";
app_indicator_set_icon_full(capindicator, icon_name, status_message);
app_indicator_set_title(capindicator, status_message);
}
else if (shownum && key_code == KEY_NUMLOCK_CODE && state_code == LIBINPUT_KEY_STATE_RELEASED)
{
numstate ^=1;
const gchar *icon_name = numstate ? "keyboard-num-enabled" : "keyboard-num-disabled";
const gchar *status_message = numstate ? "Num Lock: Enabled" : "Num Lock: Disabled";
app_indicator_set_icon_full(numindicator, icon_name, status_message);
app_indicator_set_title(numindicator, status_message);
}
return 0;
}
static int handle_events(struct libinput *libinput)
{
struct libinput_event *event;
if (libinput_dispatch(libinput) < 0)
return -1;
while ((event = libinput_get_event(libinput)) != NULL)
{
switch (libinput_event_get_type(event))
{
case LIBINPUT_EVENT_KEYBOARD_KEY:
print_key_event(event);
break;
default:
break;
}
libinput_event_destroy(event);
}
return 0;
}
static int run_mainloop(struct libinput *libinput)
{
struct pollfd fd;
fd.fd = libinput_get_fd(libinput);
fd.events = POLLIN;
while (poll(&fd, 1, -1) > -1)
handle_events(libinput);
return 0;
}
#endif
void *toggle_cap()
{
#ifdef WITHX11
XkbGetIndicatorState(d, XkbUseCoreKbd, &capstate);
int capsLock = (capstate & 0x01) != 0;
XkbLockModifiers(d, XkbUseCoreKbd, LockMask, capsLock ? 0 : LockMask);
#else
g_warning("not, implemented");
#endif
return NULL;
}
void *check_cap()
{
#ifdef WITHX11
XkbStateRec xkbState;
while (1)
{
XkbGetState(d, XkbUseCoreKbd, &xkbState);
capstate = (xkbState.locked_mods & LockMask) ? 1 : 0;
const gchar *icon_name = capstate ? "keyboard-caps-enabled" : "keyboard-caps-disabled";
const gchar *status_message = capstate ? "Caps Lock: Enabled" : "Caps Lock: Disabled";
app_indicator_set_icon_full(capindicator, icon_name, status_message);
app_indicator_set_title(capindicator, status_message);
sleep(updrate);
}
#else
run_mainloop(libinput);
#endif
return NULL;
}
void *toggle_num()
{
#ifdef WITHX11
XkbGetIndicatorState(d, XkbUseCoreKbd, &numstate);
int numLock = (numstate & 0x02) != 0;
XkbLockModifiers(d, XkbUseCoreKbd, Mod2Mask, numLock ? 0 : Mod2Mask);
#else
g_warning("not, implemented");
#endif
return NULL;
}
void *check_num(void *arg)
{
#ifdef WITHX11
while (1)
{
XkbGetState(d, XkbUseCoreKbd, &xkbnumstate);
numstate = (xkbnumstate.locked_mods & Mod2Mask) ? 1 : 0;
const gchar *icon_name = numstate ? "keyboard-num-enabled" : "keyboard-num-disabled";
const gchar *status_message = numstate ? "Num Lock: Enabled" : "Num Lock: Disabled";
app_indicator_set_icon_full(numindicator, icon_name, status_message);
app_indicator_set_title(numindicator, status_message);
sleep(updrate);
}
#endif
return NULL;
}