Remmina - The GTK+ Remote Desktop Client  v1.4.34
Remmina is a remote desktop client written in GTK+, aiming to be useful for system administrators and travellers, who need to work with lots of remote computers in front of either large monitors or tiny netbooks. Remmina supports multiple network protocols in an integrated and consistent user interface. Currently RDP, VNC, NX, XDMCP and SSH are supported.
remmina_key_chooser.c
Go to the documentation of this file.
1 /*
2  * Remmina - The GTK+ Remote Desktop Client
3  * Copyright (C) 2009-2010 Vic Lee
4  * Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
5  * Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * In addition, as a special exception, the copyright holders give
23  * permission to link the code of portions of this program with the
24  * OpenSSL library under certain conditions as described in each
25  * individual source file, and distribute linked combinations
26  * including the two.
27  * You must obey the GNU General Public License in all respects
28  * for all of the code used other than OpenSSL. * If you modify
29  * file(s) with this exception, you may extend this exception to your
30  * version of the file(s), but you are not obligated to do so. * If you
31  * do not wish to do so, delete this exception statement from your
32  * version. * If you delete this exception statement from all source
33  * files in the program, then also delete it here.
34  *
35  */
36 
37 #include <glib/gi18n.h>
38 #include "remmina_key_chooser.h"
39 #include "remmina_public.h"
41 
42 /* Handle key-presses on the GtkEventBox */
43 static gboolean remmina_key_chooser_dialog_on_key_press(GtkWidget *widget, GdkEventKey *event, RemminaKeyChooserArguments *arguments)
44 {
45  TRACE_CALL(__func__);
46  if (!arguments->use_modifiers || !event->is_modifier) {
47  arguments->state = event->state;
48  arguments->keyval = gdk_keyval_to_lower(event->keyval);
49  gtk_dialog_response(GTK_DIALOG(gtk_widget_get_toplevel(widget)),
50  event->keyval == GDK_KEY_Escape ? GTK_RESPONSE_CANCEL : GTK_RESPONSE_OK);
51  }
52  return TRUE;
53 }
54 
55 /* User option to use key modifiers when selecting keyboard shortcuts */
56 void remmina_key_chooser_dialog_set_option_modifier(GtkWidget *widget, gboolean state, RemminaKeyChooserArguments *arguments)
57 {
58  TRACE_CALL(__func__);
59  gtk_switch_set_state(GTK_SWITCH(widget), state);
60  arguments->use_modifiers = state;
61 }
62 
63 /* Show a key chooser dialog and return the keyval for the selected key */
64 RemminaKeyChooserArguments* remmina_key_chooser_new(GtkWindow *parent_window, gboolean use_modifiers)
65 {
66  TRACE_CALL(__func__);
67  GtkBuilder *builder = remmina_public_gtk_builder_new_from_resource ("/org/remmina/Remmina/src/../data/ui/remmina_key_chooser.glade");
68  GtkDialog *dialog;
69  RemminaKeyChooserArguments *arguments;
70  arguments = g_new0(RemminaKeyChooserArguments, 1);
71  arguments->state = 0;
72  arguments->use_modifiers = use_modifiers;
73 
74  /* Setup the dialog */
75  dialog = GTK_DIALOG(gtk_builder_get_object(builder, "KeyChooserDialog"));
76  gtk_window_set_transient_for(GTK_WINDOW(dialog), parent_window);
77 
78  /* Connect the key modifier switch signal */
79  g_signal_connect(gtk_builder_get_object(builder, "switch_option_key_modifier"), "state-set",
80  G_CALLBACK(remmina_key_chooser_dialog_set_option_modifier), arguments);
81 
82  /* Connect the GtkEventBox signal */
83  g_signal_connect(gtk_builder_get_object(builder, "eventbox_key_chooser"), "key-press-event",
84  G_CALLBACK(remmina_key_chooser_dialog_on_key_press), arguments);
85 
86  /* Show the dialog and destroy it after the use */
87  arguments->response = gtk_dialog_run(dialog);
88  gtk_widget_destroy(GTK_WIDGET(dialog));
89  /* The delete button set the keyval 0 */
90  if (arguments->response == GTK_RESPONSE_REJECT)
91  arguments->keyval = 0;
92  return arguments;
93 }
94 
95 /* Get the uppercase character value of a keyval */
96 gchar* remmina_key_chooser_get_value(guint keyval, guint state)
97 {
98  TRACE_CALL(__func__);
99 
100  if (!keyval)
101  return g_strdup(KEY_CHOOSER_NONE);
102 
103  return g_strdup_printf("%s%s%s%s%s%s%s",
104  (state & GDK_SHIFT_MASK) ? KEY_MODIFIER_SHIFT : "",
105  (state & GDK_CONTROL_MASK) ? KEY_MODIFIER_CTRL : "",
106  (state & GDK_MOD1_MASK) ? KEY_MODIFIER_ALT : "",
107  (state & GDK_SUPER_MASK) ? KEY_MODIFIER_SUPER : "",
108  (state & GDK_HYPER_MASK) ? KEY_MODIFIER_HYPER : "",
109  (state & GDK_META_MASK) ? KEY_MODIFIER_META : "",
110  gdk_keyval_name(gdk_keyval_to_upper(keyval)));
111 }
112 
113 /* Get the keyval of a (lowercase) character value */
114 guint remmina_key_chooser_get_keyval(const gchar *value)
115 {
116  TRACE_CALL(__func__);
117  gchar *patterns[] =
118  {
119  KEY_MODIFIER_SHIFT,
120  KEY_MODIFIER_CTRL,
121  KEY_MODIFIER_ALT,
122  KEY_MODIFIER_SUPER,
123  KEY_MODIFIER_HYPER,
124  KEY_MODIFIER_META,
125  NULL
126  };
127  gint i;
128  gchar *tmpvalue;
129  gchar *newvalue;
130  guint keyval;
131 
132  if (g_strcmp0(value, KEY_CHOOSER_NONE) == 0)
133  return 0;
134 
135  /* Remove any modifier text before to get the keyval */
136  newvalue = g_strdup(value);
137  for (i = 0; i < g_strv_length(patterns); i++) {
138  tmpvalue = remmina_public_str_replace(newvalue, patterns[i], "");
139  g_free(newvalue);
140  newvalue = g_strdup(tmpvalue);
141  g_free(tmpvalue);
142  }
143  keyval = gdk_keyval_to_lower(gdk_keyval_from_name(newvalue));
144  g_free(newvalue);
145  return keyval;
146 }
static gboolean remmina_key_chooser_dialog_on_key_press(GtkWidget *widget, GdkEventKey *event, RemminaKeyChooserArguments *arguments)
gchar * remmina_key_chooser_get_value(guint keyval, guint state)
RemminaKeyChooserArguments * remmina_key_chooser_new(GtkWindow *parent_window, gboolean use_modifiers)
guint remmina_key_chooser_get_keyval(const gchar *value)
void remmina_key_chooser_dialog_set_option_modifier(GtkWidget *widget, gboolean state, RemminaKeyChooserArguments *arguments)
GtkBuilder * remmina_public_gtk_builder_new_from_resource(gchar *resource)
gchar * remmina_public_str_replace(const gchar *string, const gchar *search, const gchar *replacement)