#include #include #include #include #include #include #include #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; 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 void button_press_event_cb(GtkButton *button, gpointer user_data) { struct rcbutton *rcb = user_data; if (GTK_BUTTON(rcb->button) != button) return; if (remote->editing) { printf("Button at %ux%u clicked in edit mode\n", rcb->col, rcb->row); quick_message(GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))), "Edit button pressed"); } else { printf("Button at %ux%u clicked\n", rcb->col, rcb->row); poke_objects(); } } #define REMOTE_BUTTON_WIDTH 64 #define REMOTE_BUTTON_HEIGHT 64 #define REMOTE_BUTTON_PADDING 2 #define REMOTE_BUTTON_FONT_SIZE 15 static void create_button_img(GtkWidget *button, const char *keycode) { struct linux_input_keycode *lik; cairo_surface_t *surface; cairo_t *ct; cairo_text_extents_t text_extents; cairo_font_extents_t font_extents; GtkWidget *img; const char *label = keycode; double x, y; double scale_x, scale_y; double scale = 1.0; GdkRGBA color; 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; surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, REMOTE_BUTTON_WIDTH, REMOTE_BUTTON_HEIGHT); ct = cairo_create(surface); /* FIXME: Make this themeable */ cairo_set_font_size(ct, REMOTE_BUTTON_FONT_SIZE); cairo_text_extents(ct, label, &text_extents); /* font extents so the vertial alignment is the same for all buttons */ cairo_font_extents(ct, &font_extents); scale_x = (REMOTE_BUTTON_WIDTH - REMOTE_BUTTON_PADDING * 2) / text_extents.width; scale_y = (REMOTE_BUTTON_HEIGHT - REMOTE_BUTTON_PADDING * 2) / font_extents.height; scale = scale_x < scale ? scale_x : scale; scale = scale_y < scale ? scale_y : scale; x = REMOTE_BUTTON_WIDTH / 2 - (text_extents.x_bearing + text_extents.width / 2.0) * scale; y = REMOTE_BUTTON_HEIGHT / 2 - (font_extents.descent - font_extents.height / 2.0) * scale; cairo_move_to(ct, x, y); cairo_scale(ct, scale, scale); gtk_style_context_get_color(gtk_widget_get_style_context(button), gtk_widget_get_state_flags(button), &color); gdk_cairo_set_source_rgba(ct, &color); cairo_show_text(ct, label); 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 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->col = x; rcb->row = y; rcb->cols = w; rcb->rows = h; rcb->button = gtk_button_new(); g_signal_connect(rcb->button, "clicked", G_CALLBACK(button_press_event_cb), rcb); remote->buttons = g_list_append(remote->buttons, rcb); gtk_grid_attach(grid, rcb->button, x, y, w, h); create_button_img(rcb->button, name); 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); 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; 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); own_page_num = gtk_notebook_append_page(global->notebook, scrollda, gtk_label_new("Receive")); GtkWidget *fixed; fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(scrollda), fixed); GtkWidget *frame; frame = gtk_frame_new(NULL); gtk_widget_set_name(frame, "RemoteControlClientRemoteFrame"); GtkBorder margins; gtk_style_context_get_margin(gtk_widget_get_style_context(frame), gtk_widget_get_state_flags(frame), &margins); gtk_fixed_put(GTK_FIXED(fixed), frame, margins.top, margins.left); 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), 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); }