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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
#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"
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 300
struct remote {
GList *buttons;
GList *header_buttons;
guint width;
guint height;
struct rcbutton *hover;
bool editing;
};
struct rcbutton {
char *name;
bool clicked;
guint x;
guint y;
guint row;
guint col;
guint rows;
guint cols;
guint width;
guint height;
guint padding;
guint corner_radius;
struct rcbutton *next;
GdkRGBA *color;
void (*on_hover)(struct rcbutton *rcb, GtkWidget *widget, bool hover);
};
static struct remote *remote = NULL;
static void
curve_rectangle(cairo_t * cr, gdouble x0, gdouble y0,
gdouble width, gdouble height, gdouble radius)
{
gdouble x1, y1;
if (!width || !height)
return;
x1 = x0 + width;
y1 = y0 + height;
radius = MIN (radius, MIN (width / 2, height / 2));
cairo_move_to (cr, x0, y0 + radius);
cairo_arc (cr, x0 + radius, y0 + radius, radius, M_PI,
3 * M_PI / 2);
cairo_line_to (cr, x1 - radius, y0);
cairo_arc (cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2,
2 * M_PI);
cairo_line_to (cr, x1, y1 - radius);
cairo_arc (cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
cairo_line_to (cr, x0 + radius, y1);
cairo_arc (cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
cairo_close_path (cr);
}
GdkRGBA *remote_background_color;
GdkRGBA *remote_edit_background_color;
GdkRGBA *cd;
GdkRGBA *cre;
GdkRGBA *cb;
GdkRGBA cgreen;
GdkRGBA cred;
static void
draw_curve_rectangle(cairo_t * cr, struct rcbutton *rcb)
{
double tx, ty;
cairo_text_extents_t extents;
const char *label = rcb->name ? rcb->name : "?";
curve_rectangle(cr, rcb->x, rcb->y,
rcb->width, rcb->height, rcb->corner_radius);
if (rcb->color)
gdk_cairo_set_source_rgba(cr, rcb->color);
else
gdk_cairo_set_source_rgba(cr, rcb->clicked ? cre : cd);
cairo_fill_preserve(cr);
gdk_cairo_set_source_rgba(cr, cb);
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
/*
if (filled)
cairo_fill(cr);
else {
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
}
*/
cairo_save(cr);
cairo_rectangle(cr,
rcb->x + rcb->padding,
rcb->y + rcb->padding,
rcb->width - rcb->padding * 2,
rcb->height - rcb->padding * 2);
cairo_clip(cr);
cairo_set_font_size(cr, 13);
cairo_text_extents (cr, label, &extents);
tx = rcb->x + (rcb->width / 2) - (extents.width / 2 + extents.x_bearing);
ty = rcb->y + (rcb->height / 2) - (extents.height / 2 + extents.y_bearing);
cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
cairo_move_to(cr, tx, ty);
cairo_show_text(cr, label);
cairo_restore(cr);
cairo_new_sub_path(cr);
}
#define SPACING 5
#define PADDING 5
#define BW 56
#define BH 56
static gboolean draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
{
struct rcbutton *rcb;
guint w = 0, h = 0;
/* Set color for background */
gdk_cairo_set_source_rgba(cr, remote->editing ?
remote_edit_background_color :
remote_background_color);
/* fill in the background color*/
cairo_paint(cr);
GList *l;
for (l = remote->buttons; l; l = l->next) {
rcb = l->data;
draw_curve_rectangle(cr, rcb);
if (rcb->x + rcb->width > w)
w = rcb->x + rcb->width;
if (rcb->y + rcb->height > h)
h = rcb->y + rcb->height;
}
w += (SPACING + BW) / 2;
h += (SPACING + BH) / 2;
/*->name ? rcb->name : "?", false,
rcb->clicked ? cre : cd,
rcb->x, rcb->y, rcb->width, rcb->height,
rcb->padding, rcb->corner_radius);
*/
gtk_widget_set_size_request(widget, w, h);
gtk_widget_set_halign(widget, GTK_ALIGN_CENTER);
gtk_widget_set_valign(widget, GTK_ALIGN_CENTER);
gtk_widget_set_margin_top(widget, BH / 2);
gtk_widget_set_margin_bottom(widget, BH / 2);
gtk_widget_set_margin_start(widget, BW / 2);
gtk_widget_set_margin_end(widget, BW / 2);
return FALSE;
}
#if 0
static struct rcbutton *new_button(const char *name, guint row, guint col)
{
struct rcbutton *rcb;
rcb = g_malloc(sizeof(*rcb));
rcb->name = strdup(name);
rcb->clicked = false;
rcb->x = (SPACING + BW) * (col) + (SPACING + BW) / 2;
rcb->y = (SPACING + BH) * (row) + (SPACING + BH) / 2;
rcb->width = BW;
rcb->height = BH;
rcb->padding = PADDING;
rcb->corner_radius = 3;
rcb->color = NULL;
return rcb;
}
#endif
static void
button_calculate_position(struct rcbutton *rcb)
{
rcb->x = (SPACING + BW) * (rcb->col) + (SPACING + BW) / 2;
rcb->y = (SPACING + BH) * (rcb->row) + (SPACING + BH) / 2;
rcb->width = BW + (rcb->cols - 1) * (BW + SPACING);
rcb->height = BH + (rcb->rows - 1) * (BH + SPACING);
}
static struct rcbutton *
new_button_add(const char *name, GdkRGBA *color,
guint x, guint y, guint w, guint h)
{
struct rcbutton *rcb;
rcb = g_malloc0(sizeof(*rcb));
rcb->name = strdup(name);
rcb->clicked = false;
rcb->col = x;
rcb->row = y;
rcb->cols = w;
rcb->rows = h;
button_calculate_position(rcb);
rcb->padding = PADDING;
rcb->corner_radius = 3;
rcb->color = color;
remote->buttons = g_list_append(remote->buttons, rcb);
remote->width = MAX(remote->width, w);
remote->height = MAX(remote->height, h);
return rcb;
}
#if 0
static struct rcbutton *new_button_addr(const char *name, guint row, guint col)
{
struct rcbutton *rcb;
rcb = g_malloc(sizeof(*rcb));
rcb->name = strdup(name);
rcb->clicked = false;
rcb->x = (SPACING + BW) / 2;
rcb->y = (SPACING + BH) / 2;
rcb->width = (SPACING + BW) * (col) - SPACING;
rcb->height = BH;
rcb->padding = PADDING;
rcb->corner_radius = 3;
rcb->color = &cred;
return rcb;
}
#endif
static void
button_redraw(struct rcbutton *rcb, GtkWidget *widget)
{
printf("Asked to redraw: X,Y,W,H - %u,%u,%u,%u\n",
rcb->x - 1, rcb->y - 1, rcb->width + 2, rcb->height + 2);
gtk_widget_queue_draw_area(widget, rcb->x - 1, rcb->y - 1,
rcb->width + 2, rcb->height + 2);
}
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);
}
static gboolean
button_press_event_cb(GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
struct rcbutton *rcb;
GList *l;
if (event->button != GDK_BUTTON_PRIMARY)
return true;
for (l = remote->buttons; l; l = l->next) {
rcb = l->data;
if (event->x < rcb->x || event->x > (rcb->x + rcb->width))
continue;
if (event->y < rcb->y || event->y > (rcb->y + rcb->height))
continue;
if (remote->editing) {
printf("Button at %ux%u clicked in edit mode\n", rcb->x, rcb->y);
quick_message(GTK_WINDOW(gtk_widget_get_toplevel(widget)), "Edit button pressed");
} else {
rcb->clicked = true;
button_redraw(rcb, widget);
printf("Button at %ux%u clicked\n", rcb->x, rcb->y);
poke_objects();
}
}
return true;
}
static gboolean
button_release_event_cb(GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
struct rcbutton *rcb;
GList *l;
if (event->button != GDK_BUTTON_PRIMARY)
return true;
for (l = remote->buttons; l; l = l->next) {
rcb = l->data;
if (!rcb->clicked)
continue;
rcb->clicked = false;
button_redraw(rcb, widget);
}
return true;
}
static gboolean
motion_notify_event_cb(GtkWidget *widget,
GdkEventMotion *event,
gpointer data)
{
struct rcbutton *rcb;
static GdkCursor *cursor = NULL;
GdkWindow* win = gtk_widget_get_parent_window(widget);
GList *l;
if (!cursor)
cursor = gdk_cursor_new_for_display(gdk_window_get_display(win), GDK_HAND1);
for (l = remote->buttons; l; l = l->next) {
rcb = l->data;
if (event->x < rcb->x || event->x > (rcb->x + rcb->width))
continue;
if (event->y < rcb->y || event->y > (rcb->y + rcb->height))
continue;
break;
}
if (!l)
rcb = NULL;
if (remote->hover == rcb)
return true;
gdk_window_set_cursor(win, rcb ? cursor : NULL);
if (remote->hover && remote->hover->on_hover) {
remote->hover->on_hover(remote->hover, widget, false);
remote->hover = NULL;
}
if (rcb && rcb->on_hover) {
rcb->on_hover(rcb, widget, true);
remote->hover = rcb;
}
return true;
}
static GtkWidget *scrollda = NULL;
void rcng_client_receive_destroy_ui()
{
if (scrollda)
gtk_widget_destroy(scrollda);
scrollda = NULL;
}
static gint own_page_num = -1;
static GtkWidget *da;
/* 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);
gtk_widget_queue_draw(da);
}
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)
{
GtkStateFlags state;
GtkStyleContext *style_context;
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);
g_free(keymaps);
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;
style_context = gtk_widget_get_style_context(GTK_WIDGET(global->window));
state = gtk_widget_get_state_flags(GTK_WIDGET(global->window));
gtk_style_context_get(style_context, state, "background-color",
&remote_background_color, NULL);
remote_edit_background_color = gdk_rgba_copy(remote_background_color);
remote_edit_background_color->green *= 1.5;
cd = gdk_rgba_copy(remote_background_color);
cd->red *= 0.7;
cd->green *= 0.7;
cd->blue *= 0.7;
cre = gdk_rgba_copy(remote_background_color);
cre->red *= 2.0;
cb = gdk_rgba_copy(remote_background_color);
cb->red = 0.0;
cb->green = 0.0;
cb->blue = 0.0;
cb->alpha = 1.0;
gdk_rgba_parse(&cgreen, "#a9b68f");
gdk_rgba_parse(&cred, "#cf8989");
cred.alpha = 0.8;
n_items = g_variant_iter_init(&iter, km_entries);
g_print("Key items: %zu\n", n_items);
guint row = 0;
guint col = 0;
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(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);
}
da = gtk_drawing_area_new();
// gtk_widget_set_size_request (da, WINDOW_WIDTH, WINDOW_HEIGHT);
g_signal_connect (da, "draw", G_CALLBACK(draw_cb), NULL);
g_signal_connect(da, "button-press-event",
G_CALLBACK (button_press_event_cb), NULL);
g_signal_connect(da, "button-release-event",
G_CALLBACK (button_release_event_cb), NULL);
g_signal_connect(da, "motion-notify-event",
G_CALLBACK (motion_notify_event_cb), NULL);
gtk_widget_set_events (da, gtk_widget_get_events (da)
| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
scrollda = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrollda), da);
gtk_widget_show_all(scrollda);
own_page_num = gtk_notebook_append_page(global->notebook, scrollda, gtk_label_new("Receive"));
}
|