-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.c
257 lines (229 loc) · 9.85 KB
/
source.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
#include <linux/input-event-codes.h>
#include "gtklock-module.h"
#include "keyboard.h"
#include "layout.mobintl.h"
#include <gtk/gtk.h>
#include <math.h>
#include <stdbool.h>
#define MODULE_DATA(x) (x->module_data[self_id])
#define VIRTUAL_KEYBOARD(x) ((struct keyboard *)MODULE_DATA(x))
extern char *xdg_get_config_path(const char *basename);
extern void config_load(const char *path, const char *group,
GOptionEntry entries[]);
// Module information
const gchar module_name[] = "virtual_keyboard";
const guint module_major_version = 3;
const guint module_minor_version = 0;
static int self_id; // Module's unique ID
struct keyboard {
GtkWidget *revealer;
GtkWidget *keyboard_grid;
GtkWidget *layout_button;
GtkWidget *mode_button;
gboolean compose;
gboolean shift;
struct layout *current_layout;
};
static void setup_virtual_keyboard(struct Window *ctx);
static void create_keys(struct Window *ctx, struct layout *l);
static void gtk_grid_remove_all(GtkGrid *grid) {
GList *children = gtk_container_get_children(GTK_CONTAINER(grid));
while (children != NULL) {
gtk_container_remove(GTK_CONTAINER(grid), children->data);
children = g_list_next(children);
}
}
static void key_pressed(GtkWidget *button, gpointer user_data) {
struct Window *win = (struct Window *)user_data;
struct key *key_data =
(struct key *)g_object_get_data(G_OBJECT(button), "key_data");
if (key_data->type == Code || key_data->type == Copy) {
if (VIRTUAL_KEYBOARD(win)->compose) {
VIRTUAL_KEYBOARD(win)->compose = FALSE;
if (key_data->layout) {
create_keys(user_data, key_data->layout);
} else {
create_keys(user_data, &layouts[Landscape]);
}
} else {
if (key_data->code == KEY_BACKSPACE) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "backspace");
} else if (key_data->code == KEY_ENTER) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "activate");
} else if (key_data->code == KEY_TAB) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "insert-at-cursor",
"\t");
} else if (key_data->code == KEY_ESC) {
} else if (key_data->code == KEY_UP) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_DISPLAY_LINES, -1, FALSE);
} else if (key_data->code == KEY_DOWN) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_DISPLAY_LINES, 1, FALSE);
} else if (key_data->code == KEY_LEFT) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_LOGICAL_POSITIONS, -1, FALSE);
} else if (key_data->code == KEY_RIGHT) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_LOGICAL_POSITIONS, 1, FALSE);
} else if (key_data->code == KEY_HOME) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1, FALSE);
} else if (key_data->code == KEY_END) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1, FALSE);
} else if (key_data->code == KEY_PAGEUP) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_PAGES, -1, FALSE);
} else if (key_data->code == KEY_PAGEDOWN) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "move-cursor",
GTK_MOVEMENT_PAGES, 1, FALSE);
} else if (VIRTUAL_KEYBOARD(win)->shift) {
VIRTUAL_KEYBOARD(win)->shift = false;
if (key_data->code == KEY_SPACE) {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)),
"insert-at-cursor", "\t");
} else {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)),
"insert-at-cursor", key_data->shift_label);
}
create_keys(win, VIRTUAL_KEYBOARD(win)->current_layout);
} else {
g_signal_emit_by_name((GTK_ENTRY(win->input_field)), "insert-at-cursor",
key_data->label);
}
}
} else if (key_data->type == BackLayer) {
create_keys(user_data, &layouts[Landscape]);
} else if (key_data->type == NextLayer) {
create_keys(user_data, &layouts[LandscapeSpecial]);
} else if (key_data->type == Layout) {
create_keys(user_data, key_data->layout);
} else if (key_data->type == Compose) {
VIRTUAL_KEYBOARD(win)->compose =
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
} else if (key_data->type == Mod && key_data->code == Shift) {
VIRTUAL_KEYBOARD(win)->shift =
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
create_keys(win, VIRTUAL_KEYBOARD(win)->current_layout);
}
}
static void create_keys(struct Window *ctx, struct layout *l) {
VIRTUAL_KEYBOARD(ctx)->current_layout = l;
gtk_grid_remove_all(GTK_GRID(VIRTUAL_KEYBOARD(ctx)->keyboard_grid));
int row = 0, col = 0;
struct key *k = l->keys;
while (k->type != Last) {
if (k->type == EndRow) {
row++;
col = 0;
} else if (k->type == Pad) {
GtkWidget *pad = gtk_label_new(k->label);
gtk_grid_attach(GTK_GRID(VIRTUAL_KEYBOARD(ctx)->keyboard_grid), pad, col,
row, round(k->width * 4), 1);
col += (int)(k->width * 4);
} else {
GtkWidget *button;
if (k->type == Compose || k->type == Mod) {
button = gtk_toggle_button_new_with_label(k->label);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), false);
if (k->code == Shift) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
VIRTUAL_KEYBOARD(ctx)->shift);
} else if (k->type == Mod) {
gtk_widget_set_sensitive(button, false);
}
} else {
const gchar *lbl = k->label;
if (VIRTUAL_KEYBOARD(ctx)->shift) {
lbl = k->shift_label;
}
button = gtk_button_new_with_label(lbl);
}
g_object_set_data(G_OBJECT(button), "key_data", k);
g_signal_connect(button, "clicked", G_CALLBACK(key_pressed), ctx);
gtk_widget_set_can_focus(button, false);
gtk_grid_attach(GTK_GRID(VIRTUAL_KEYBOARD(ctx)->keyboard_grid), button,
col, row, (int)(k->width * 4), 1);
col += round(k->width * 4);
}
k++;
}
gtk_widget_show_all(VIRTUAL_KEYBOARD(ctx)->keyboard_grid);
}
static void setup_virtual_keyboard(struct Window *ctx) {
if (MODULE_DATA(ctx) != NULL) {
gtk_widget_destroy(VIRTUAL_KEYBOARD(ctx)->revealer);
g_free(MODULE_DATA(ctx));
MODULE_DATA(ctx) = NULL;
}
MODULE_DATA(ctx) = g_malloc(sizeof(struct keyboard));
VIRTUAL_KEYBOARD(ctx)->revealer = gtk_revealer_new();
VIRTUAL_KEYBOARD(ctx)->shift = false;
VIRTUAL_KEYBOARD(ctx)->compose = false;
g_object_set(VIRTUAL_KEYBOARD(ctx)->revealer, "margin", 5, NULL);
gtk_widget_set_halign(VIRTUAL_KEYBOARD(ctx)->revealer, GTK_ALIGN_CENTER);
gtk_widget_set_valign(VIRTUAL_KEYBOARD(ctx)->revealer, GTK_ALIGN_END);
gtk_revealer_set_transition_type(
GTK_REVEALER(VIRTUAL_KEYBOARD(ctx)->revealer),
GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP);
gtk_revealer_set_reveal_child(GTK_REVEALER(VIRTUAL_KEYBOARD(ctx)->revealer),
TRUE);
gtk_overlay_add_overlay(GTK_OVERLAY(ctx->overlay),
VIRTUAL_KEYBOARD(ctx)->revealer);
VIRTUAL_KEYBOARD(ctx)->keyboard_grid = gtk_grid_new();
gtk_widget_set_name(VIRTUAL_KEYBOARD(ctx)->keyboard_grid, "kbgrid");
gtk_grid_set_row_spacing(GTK_GRID(VIRTUAL_KEYBOARD(ctx)->keyboard_grid), 5);
gtk_grid_set_column_spacing(GTK_GRID(VIRTUAL_KEYBOARD(ctx)->keyboard_grid),
5);
create_keys(ctx, &layouts[Landscape]);
gtk_container_add(GTK_CONTAINER(VIRTUAL_KEYBOARD(ctx)->revealer),
VIRTUAL_KEYBOARD(ctx)->keyboard_grid);
gtk_widget_show_all(VIRTUAL_KEYBOARD(ctx)->revealer);
}
// Handle the activation of the module
void on_activation(struct GtkLock *gtklock, int id) {
self_id = id;
GtkCssProvider *provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(provider,
"#kbgrid button {"
" font-size: 200%;"
"}"
"#kbgrid button label {"
" padding: 0.25em;"
"}",
-1, NULL);
GdkDisplay *display = gdk_display_get_default();
GdkScreen *screen = gdk_display_get_default_screen(display);
gtk_style_context_add_provider_for_screen(
screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
}
// Handle focus change to setup the keyboard for the newly focused window
void on_focus_change(struct GtkLock *gtklock, struct Window *win,
struct Window *old) {
setup_virtual_keyboard(win);
}
// Handle window destruction and cleanup the associated keyboard
void on_window_destroy(struct GtkLock *gtklock, struct Window *ctx) {
if (MODULE_DATA(ctx) != NULL) {
gtk_widget_destroy(VIRTUAL_KEYBOARD(ctx)->revealer);
g_free(MODULE_DATA(ctx));
MODULE_DATA(ctx) = NULL;
}
}
// Handle idle hide event to hide the keyboard
void on_idle_hide(struct GtkLock *gtklock) {
if (gtklock->focused_window) {
gtk_revealer_set_reveal_child(
GTK_REVEALER(VIRTUAL_KEYBOARD(gtklock->focused_window)->revealer),
FALSE);
}
}
// Handle idle show event to show the keyboard
void on_idle_show(struct GtkLock *gtklock) {
if (gtklock->focused_window) {
gtk_revealer_set_reveal_child(
GTK_REVEALER(VIRTUAL_KEYBOARD(gtklock->focused_window)->revealer),
TRUE);
}
}