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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <gtk/gtk.h>
#include "generated.h"
#include "rcm-client-main.h"
#include "rcm-client-receive.h"
#include "linux-input-keycodes.h"
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 300
struct remote {
GList *buttons;
GList *header_buttons;
guint width;
guint height;
bool editing;
};
struct rcbutton {
char *name;
guint row;
guint col;
guint rows;
guint cols;
GtkWidget *button;
struct rcbutton *next;
};
static struct remote *remote = NULL;
#if 0
static void
quick_message(GtkWindow *parent, gchar *message)
{
GtkWidget *dialog, *label, *content_area;
GtkDialogFlags flags;
flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
dialog = gtk_dialog_new_with_buttons("Message", parent, flags, "OK",
GTK_RESPONSE_NONE, NULL);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
label = gtk_label_new(message);
g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
gtk_container_add(GTK_CONTAINER(content_area), label);
gtk_widget_show_all(dialog);
}
#endif
/* Hacky McHack - Estimate the size needed for a button with ABCDEF */
static int
calculate_img_size(GtkWidget *button)
{
static bool first = true;
static int size;
cairo_surface_t *surface;
cairo_t *ct;
PangoLayout *layout;
PangoFontDescription *font_description;
if (!first)
return size;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
ct = cairo_create(surface);
layout = pango_cairo_create_layout(ct);
gtk_style_context_get(gtk_widget_get_style_context(button),
gtk_widget_get_state_flags(button),
GTK_STYLE_PROPERTY_FONT, &font_description,
NULL);
pango_layout_set_font_description(layout, font_description);
pango_font_description_free(font_description);
pango_layout_set_text(layout, "ABCDEF", -1);
pango_layout_get_pixel_size(layout, &size, NULL);
g_object_unref(layout);
cairo_destroy(ct);
cairo_surface_destroy(surface);
first = false;
return size;
}
#define REMOTE_BUTTON_PADDING 2.0f
static void
create_button_img(GtkWidget *button, const char *keycode)
{
struct linux_input_keycode *lik;
cairo_surface_t *surface;
cairo_t *ct;
PangoLayout *layout;
PangoFontDescription *font_description;
GtkWidget *img;
const char *label = keycode;
double x, y;
double scale_x, scale_y;
double scale = 1.0;
GdkRGBA *color;
gint pw, ph;
int img_size;
for (lik = &linux_input_keycodes[0]; lik->name; lik++)
if (keycode && !strcmp(lik->name, keycode))
break;
if (!lik->name) {
printf("Unknown keycode! %s\n", keycode);
label = "<?>";
}
if (lik->cs)
goto out;
img_size = calculate_img_size(button);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
img_size, img_size);
ct = cairo_create(surface);
layout = pango_cairo_create_layout(ct);
gtk_style_context_get(gtk_widget_get_style_context(button),
gtk_widget_get_state_flags(button),
GTK_STYLE_PROPERTY_FONT, &font_description,
GTK_STYLE_PROPERTY_COLOR, &color,
NULL);
pango_layout_set_font_description(layout, font_description);
pango_font_description_free(font_description);
gdk_cairo_set_source_rgba(ct, color);
gdk_rgba_free(color);
pango_layout_set_text(layout, label, -1);
pango_layout_get_pixel_size(layout, &pw, &ph);
scale_x = ((float)img_size - REMOTE_BUTTON_PADDING * 2.0f) / (float)pw;
scale_y = ((float)img_size - REMOTE_BUTTON_PADDING * 2.0f) / (float)ph;
scale = scale_x < scale ? scale_x : scale;
scale = scale_y < scale ? scale_y : scale;
x = (float)img_size / 2.0f - ((float)pw / 2.0f) * scale;
y = (float)img_size / 2.0f - ((float)ph / 2.0f) * scale;
cairo_move_to(ct, x, y);
cairo_scale(ct, scale, scale);
pango_cairo_show_layout(ct, layout);
g_object_unref(layout);
cairo_destroy(ct);
lik->cs = surface;
out:
img = gtk_image_new_from_surface(lik->cs);
gtk_button_set_image(GTK_BUTTON(button), img);
}
static struct rcbutton *
new_button_add(GtkGrid *grid, const gchar *protocol, guint64 scancode,
const char *keycode, GdkRGBA *color,
guint x, guint y, guint w, guint h)
{
struct rcbutton *rcb;
gchar *tooltip;
rcb = g_malloc0(sizeof(*rcb));
rcb->name = strdup(keycode);
rcb->col = x;
rcb->row = y;
rcb->cols = w;
rcb->rows = h;
rcb->button = gtk_toggle_button_new();
gtk_widget_set_sensitive(rcb->button, false);
tooltip = g_strdup_printf("Protocol: %s\nScancode: 0x%08" PRIx64 "\nKeycode : %s", protocol, scancode, keycode);
gtk_widget_set_tooltip_text(rcb->button, tooltip);
g_free(tooltip);
remote->buttons = g_list_append(remote->buttons, rcb);
gtk_grid_attach(grid, rcb->button, x, y, w, h);
create_button_img(rcb->button, keycode);
return rcb;
}
static GtkWidget *scrollda = NULL;
void rcng_client_receive_destroy_ui()
{
if (scrollda)
gtk_widget_destroy(scrollda);
scrollda = NULL;
}
static gint own_page_num = -1;
/* FIXME: These two functions should probably move to the core */
static void
create_header_button(const gchar *label, const gchar *icon_name,
GCallback callback, gpointer user_data)
{
GtkWidget *button;
button = gtk_button_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
gtk_button_set_label(GTK_BUTTON(button), label);
gtk_button_set_always_show_image(GTK_BUTTON(button), TRUE);
g_signal_connect(button, "clicked", callback, user_data);
gtk_widget_show_all(button);
gtk_header_bar_pack_end(GTK_HEADER_BAR(global->header), button);
remote->header_buttons = g_list_prepend(remote->header_buttons, button);
}
static void
remove_header_buttons(void)
{
GList *l;
for (l = remote->header_buttons; l; l = l->next)
gtk_widget_destroy(l->data);
g_list_free(remote->header_buttons);
remote->header_buttons = NULL;
}
static void toggle_edit_keymap(GtkButton *button, gpointer user_data);
static void
resize_keymap(GtkButton *button, gpointer user_data)
{
GtkWindow *parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button)));
GtkWidget *dialog;
GtkWidget *content_area;
GtkWidget *grid;
GtkWidget *width_label;
GtkAdjustment *width_adj;
GtkWidget *width_input;
GtkWidget *height_label;
GtkAdjustment *height_adj;
GtkWidget *height_input;
gint r;
gint width;
gint height;
dialog = gtk_dialog_new_with_buttons("Resize Keymap", parent,
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
"OK", GTK_RESPONSE_ACCEPT,
"Cancel", GTK_RESPONSE_REJECT,
NULL);
grid = gtk_grid_new();
gtk_grid_set_column_spacing(GTK_GRID(grid), 12);
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
gtk_container_set_border_width(GTK_CONTAINER(grid), 12);
width_label = gtk_label_new("Width:");
gtk_widget_set_halign(width_label, GTK_ALIGN_END);
width_adj = gtk_adjustment_new(remote->width, 1.0, 100.0, 1.0, 5.0, 0.0);
width_input = gtk_spin_button_new(width_adj, 1.0, 0);
gtk_grid_attach(GTK_GRID(grid), width_label, 1, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), width_input, 2, 1, 1, 1);
height_label = gtk_label_new("Height:");
gtk_widget_set_halign(height_label, GTK_ALIGN_END);
height_adj = gtk_adjustment_new(remote->height, 1.0, 100.0, 1.0, 5.0, 0.0);
height_input = gtk_spin_button_new(height_adj, 1.0, 0);
gtk_grid_attach(GTK_GRID(grid), height_label, 1, 2, 1, 1);
gtk_grid_attach(GTK_GRID(grid), height_input, 2, 2, 1, 1);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_container_set_border_width(GTK_CONTAINER(content_area), 12);
gtk_container_add(GTK_CONTAINER(content_area), grid);
gtk_widget_show_all(dialog);
r = gtk_dialog_run(GTK_DIALOG(dialog));
if (r != GTK_RESPONSE_ACCEPT)
goto out;
width = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(width_input));
height = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(height_input));
printf("Asked to resize keylayout to %ix%i\n", width, height);
out:
gtk_widget_destroy(dialog);
}
static void
set_edit_keymap(bool editing)
{
if (remote->width < 1 || remote->height < 1)
return;
remove_header_buttons();
if (editing) {
create_header_button("Apply", "gtk-apply", G_CALLBACK(toggle_edit_keymap), &remote);
create_header_button("Resize", "document-page-setup", G_CALLBACK(resize_keymap), &remote);
} else
create_header_button("Edit", "list-add", G_CALLBACK(toggle_edit_keymap), &remote);
}
static void
toggle_edit_keymap(GtkButton *button, gpointer user_data)
{
remote->editing = !remote->editing;
set_edit_keymap(remote->editing);
}
static void
on_notebook_page_change(GtkNotebook *notebook, GtkWidget *page, guint new_page_num,
gpointer user_data)
{
if (new_page_num != own_page_num) {
remove_header_buttons();
remote->editing = false;
return;
}
remote->editing = false;
set_edit_keymap(remote->editing);
g_print("Page change: %i -> %u\n", own_page_num, new_page_num);
}
void rcng_client_receive_init_ui(GDBusObject *new_hw)
{
static bool first = true;
if (first) {
g_signal_connect(global->notebook, "switch-page", G_CALLBACK(on_notebook_page_change), NULL);
first = false;
}
rcng_client_receive_destroy_ui();
GDBusInterface *interface;
interface = g_dbus_object_get_interface(new_hw, "org.gnome.RemoteControlManager.Device");
if (!interface)
return;
/* FIXME: if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a(uuuuaa{sv}))"))) */
gchar **keymaps = NULL;
RCDevice *object = RCDEVICE(interface);
rcdevice_call_list_keymaps_sync(object, &keymaps, NULL, NULL);
g_print("I should look at keymap: %s\n", keymaps[0]);
guint16 km_width;
guint16 km_height;
GVariant *km_entries = NULL;
rcdevice_call_get_keymap_sync(object, keymaps[0], &km_width, &km_height,
&km_entries, NULL, NULL);
remote = g_malloc0(sizeof(*remote));
remote->width = km_width;
remote->height = km_height;
remote->editing = false;
g_print("type of keymaps is %s\n", g_variant_get_type_string(km_entries));
GVariantIter iter;
gsize n_items;
GVariant *item;
n_items = g_variant_iter_init(&iter, km_entries);
g_print("Key items: %zu\n", n_items);
guint row = 0;
guint col = 0;
scrollda = gtk_scrolled_window_new(NULL, NULL);
gtk_container_set_border_width(GTK_CONTAINER(scrollda), 12);
own_page_num = gtk_notebook_append_page(global->notebook, scrollda, gtk_label_new("Receive"));
GtkWidget *rgrid;
rgrid = gtk_grid_new();
gtk_widget_set_halign(rgrid, GTK_ALIGN_CENTER);
gtk_grid_set_column_spacing(GTK_GRID(rgrid), 12);
gtk_grid_set_row_spacing(GTK_GRID(rgrid), 12);
gtk_container_add(GTK_CONTAINER(scrollda), rgrid);
GtkWidget *fixed;
fixed = gtk_fixed_new();
gtk_widget_set_name(fixed, "RemoteControlClientFixed");
gtk_grid_attach(GTK_GRID(rgrid), fixed, 1, 1, 1, 1);
GtkWidget *frame;
frame = gtk_frame_new(keymaps[0]);
g_free(keymaps);
gtk_widget_set_name(gtk_frame_get_label_widget(GTK_FRAME(frame)), "RemoteControlClientRemoteLabel");
gtk_widget_set_name(frame, "RemoteControlClientRemoteFrame");
gtk_fixed_put(GTK_FIXED(fixed), frame, 0, 0);
GtkWidget *grid;
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(frame), grid);
gtk_grid_set_column_spacing(GTK_GRID(grid), 12);
gtk_grid_set_row_spacing(GTK_GRID(grid), 12);
gtk_grid_set_column_homogeneous(GTK_GRID(grid), true);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), true);
gtk_container_set_border_width(GTK_CONTAINER(grid), 12);
while (g_variant_iter_loop (&iter, "@a{sv}", &item)) {
gchar *protocol;
guint64 scancode;
gchar *keycode;
g_variant_lookup(item, "protocol", "s", &protocol);
g_variant_lookup(item, "scancode", "t", &scancode);
g_variant_lookup(item, "keycode", "s", &keycode);
new_button_add(GTK_GRID(grid), protocol, scancode, keycode, NULL, col, row, 1, 1);
col++;
if (col >= km_width) {
col = 0;
row++;
}
printf("Got a key: %s:0x%08" PRIx64 ":%s\n", protocol, scancode, keycode);
}
gtk_widget_show_all(scrollda);
}
|