-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
392 lines (324 loc) · 9.56 KB
/
main.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "i3keys.h"
// X11
Display *display;
Window window;
int screen;
// Dimensions
int height;
int width;
int total_keyboard_width;
int total_keyboard_height;
// Colors
GC gc;
Colormap colormap;
XColor color_background;
XColor color_focused;
XColor color_focused_text;
XColor color_unfocused;
XColor color_unfocused_text;
// Font
XFontStruct *font;
int font_height;
// Switch
int switch_height = 30;
// State
i3k_key *last_key;
i3k_key *modifier_key;
int modifier_mask;
int open = 1;
// Create an XColor from a hex code such as #1B1D1E
static XColor color_from_hex(const char *code) {
XColor color;
XParseColor(display, colormap, code, &color);
XAllocColor(display, colormap, &color);
return color;
}
// Calculate the pixel key width and heights, x and y from the abstract
// key layout definition. Further centers the keyboard in the middle of the screen
static void calculate_layout_dimensions() {
int x = 0;
int y = 0;
int last_row = -1;
int max_rows = 0;
int keyboard_offset_from_left = 0;
i3k_key *key = keys;
i3k_key *keyEnd = keys + sizeof(keys)/sizeof(keys[0]);
while(key < keyEnd) {
// Calculate starting x
if(last_row != key->row) {
x = 0;
max_rows++;
}
last_row = key->row;
x += key_margin;
// Calculate starting y
y = switch_height + key_margin + key->row * (key_margin + key_size);
// Set values to key layout
key->x = x;
key->y = y;
key->width = key->size * key_size + (key->size-1) * key_margin;
key->height = key_size;
// Set next X starting point
x += key->width;
// Track maximum total keyboard width for positioning it in the middle
if(x > total_keyboard_width) {
total_keyboard_width = x;
}
key++;
}
// Total keyboard height based on rows
total_keyboard_height = max_rows * (key_margin + key_size) + key_margin + switch_height;
// Calculate offset from left to center keyboard
keyboard_offset_from_left = (width - (total_keyboard_width + key_margin)) / 2;
// Apply offset to center the keyboard
key = keys;
while(key < keyEnd) {
key->x = key->x + keyboard_offset_from_left;
key++;
}
}
// Renders each key based on its position and modifier status
static void render_keys() {
// Draw keys
i3k_key *key = keys;
i3k_key *keyEnd = keys + sizeof(keys)/sizeof(keys[0]);
while(key < keyEnd) {
// Draw key background
XColor key_color = key->is_pressed ? color_focused : color_unfocused;
XSetForeground(display, gc, key_color.pixel);
XFillRectangle(display, window, gc, key->x, key->y, key->width, key->height);
// Draw text
XColor font_color = key->is_pressed ? color_focused_text : color_unfocused_text;
XSetForeground(display, gc, font_color.pixel);
char *text = modifier_mask == ShiftMask ? key->shift_text : key->text;
int text_width = XTextWidth(font, text, strlen(text));
int x = key->x + ((key->width - text_width) / 2);
int y = key->y + ((key->height + font_height) / 2);
XDrawString(display, window, gc, x, y, text, strlen(text));
key++;
}
}
// Renders the switch to open/close the keyboard
static void render_switch() {
XSetForeground(display, gc, color_focused.pixel);
XFillRectangle(display, window, gc, 0, 0, width, switch_height);
}
// Try to find key from button event
static i3k_key* find_key_from_button(XButtonEvent *event) {
i3k_key *key = keys;
i3k_key *keyEnd = keys + sizeof(keys)/sizeof(keys[0]);
while(key < keyEnd) {
if(
event->x >= key->x &&
event->x <= (key->x + key->width) &&
event->y >= key->y &&
event->y <= (key->y + key->height)
){
return key;
}
key++;
}
return NULL;
}
// Expose is called to draw and redraw
static void expose(XEvent *event) {
calculate_layout_dimensions();
render_switch();
render_keys();
}
// Send a key event where the type is KeyPress or KeyRelease, keycodes are defined in X11/keysym, e.g. XK_g
// and the keymask defines the modifier states, e.g. ShiftMask or LockMask or ControlMask
static void send_key(int type, int keycode, int keymask) {
// Select target window from current focus
Window focus;
int revert;
XGetInputFocus(display, &focus, &revert);
// Create key event
XKeyEvent event;
event.display = display;
event.window = focus;
event.root = window;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.keycode = XKeysymToKeycode(display, keycode);
event.state = keymask;
event.type = type;
// Send event
long event_mask = type == KeyPress ? KeyPressMask : KeyReleaseMask;
XSendEvent(event.display, event.window, True, event_mask, (XEvent *)&event);
}
// Button press is called on touch or click down
static void button_press(XButtonEvent *event) {
// Switch
if(event->y <= switch_height) {
if(open == 1) {
open = 0;
XMoveResizeWindow(display, window, 0, 0, width, switch_height);
} else {
open = 1;
XMoveResizeWindow(display, window, 0, 0, width, total_keyboard_height);
}
return;
}
// Key
i3k_key *key = find_key_from_button(event);
if(key == NULL) {
return;
}
// Handle modifier key press
if(key->modifier_mask){
// Deselect potential last modifier key
if(modifier_key != NULL && modifier_key != key){
modifier_key->is_pressed = False;
modifier_mask = None;
modifier_key = NULL;
}
// Select
if(modifier_mask == None) {
key->is_pressed = True;
modifier_mask = key->modifier_mask;
modifier_key = key;
} else {
// Unselect pressed modifier
key->is_pressed = False;
modifier_mask = None;
modifier_key = NULL;
}
// Handle normal key press
} else {
// Highlight key
key->is_pressed = True;
last_key = key;
// Send press to XServer
send_key(KeyPress, key->keycode, modifier_mask);
// Reset modifier
if(modifier_key != NULL){
modifier_key->is_pressed = False;
modifier_mask = None;
}
}
// Rerender
render_keys();
}
// Button release is called on touch up or release
static void button_release(XButtonEvent *event) {
i3k_key *key = last_key;
if(last_key == NULL) {
return;
}
// Unhighlight key
key->is_pressed = False;
// Send release to XServer
send_key(KeyRelease, key->keycode, modifier_mask);
last_key = NULL;
// Rerender
render_keys();
}
// Setup Colors
static void setup_colors(){
// Colormap
colormap = DefaultColormap(display, 0);
// Colors
color_background = color_from_hex("#1B1D1E");
color_unfocused = color_from_hex("#303030");
color_unfocused_text = color_from_hex("#ffffff");
color_focused = color_from_hex("#00B7FF");
color_focused_text = color_from_hex("#ffffff");
// Setup Graphics Context
gc = DefaultGC(display, screen);
}
// Setup Fonts
static void setup_fonts(){
// Setup Font
font = XLoadQueryFont(display, key_font);
if (font == NULL) {
fprintf(stderr, "Cannot load font %s\n", key_font);
exit(1);
}
XSetFont(display, gc, font->fid);
font_height = font->ascent + font->descent;
}
// Setup Window
static void setup_window(){
// Width is the full window width
width = DisplayWidth(display, screen);
// Calculate keyboard dimensions and layout for total_keyboard_height
calculate_layout_dimensions();
// Create window
window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, width, total_keyboard_height, 0, None, color_background.pixel);
}
// Setup Window Properties
static void setup_window_properties(){
// Set window name
Atom type = XInternAtom(display, "_NET_WM_NAME", False);
Atom value = XInternAtom(display, "i3keys", False);
XChangeProperty(display, window, type, XA_ATOM, 32, PropModeReplace, (unsigned char *)&value, 1);
// Set window type to DOCK
type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
value = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DOCK", False);
XChangeProperty(display, window, type, XA_ATOM, 32, PropModeReplace, (unsigned char *)&value, 1);
// Position at bottom of window
unsigned long strut[12];
memset(&strut, 0, sizeof(strut));
strut[3] = total_keyboard_height; // bottom
type = XInternAtom(display, "_NET_WM_STRUT_PARTIAL", False);
XChangeProperty(display, window, type, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)strut, 12);
// Show the window
XMapWindow(display, window);
XFlush(display);
}
// Start Event Loop
static void start_event_loop(){
// Select kind of events we want to receive
XSelectInput(display, window, ExposureMask | ButtonPressMask | ButtonReleaseMask);
// Event loop
XEvent event;
while(1) {
XNextEvent(display, &event);
// Draw or redraw window
if(event.type == Expose) {
expose(&event);
continue;
}
// Handle mouse button press
if(event.type == ButtonPress) {
button_press((XButtonEvent*)&event);
continue;
}
// Handle mouse button release
if(event.type == ButtonRelease) {
button_release((XButtonEvent*)&event);
continue;
}
}
}
// Main
int main() {
// Open connection to X server
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
screen = DefaultScreen(display);
// Setup
setup_colors();
setup_fonts();
setup_window();
setup_window_properties();
// Loop
start_event_loop();
// Close connection to X server
XCloseDisplay(display);
return 0;
}