]> icculus.org git repositories - dana/obconf.git/blob - src/keybindings.c
stupid glade
[dana/obconf.git] / src / keybindings.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    keybindings.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "main.h"
20 #include "tree.h"
21
22 static gboolean mapping = FALSE;
23 static gchar   *saved_text = NULL;
24
25 static gboolean validate_key(const gchar *s);
26
27 void keybindings_setup_tab()
28 {
29     GtkWidget *w;
30     gchar *s;
31
32     mapping = TRUE;
33
34     w = get_widget("chain_quit_key");
35     s = tree_get_string("keyboard/chainQuitKey", "C-g");
36     gtk_entry_set_text(GTK_ENTRY(w), s);
37     g_free(s);
38
39     mapping = FALSE;
40 }
41
42 void on_chain_quit_key_focus_in(GtkEntry *w, gpointer data)
43 {
44     g_assert(saved_text == NULL);
45     saved_text = g_strdup(gtk_entry_get_text(w));
46 }
47
48 void on_chain_quit_key_focus_out(GtkEntry *w, gpointer data)
49 {
50     const gchar *s;
51
52     if (mapping) return;
53
54     s = gtk_entry_get_text(w);
55     if (!validate_key(s)) {
56         g_print("bad key binding: %s\n", s);
57         gtk_entry_set_text(w, saved_text);
58     } else
59         tree_set_string("keyboard/chainQuitKey", s);
60
61     g_free(saved_text);
62     saved_text = NULL;
63 }
64
65 static gboolean validate_key(const gchar *s)
66 {
67     const gchar *next;
68     const gchar *valid[] = { "Mod1", "Mod2", "Mod3", "Mod4", "Mod5",
69                              "Control", "C", "Alt", "A", "Meta", "M",
70                              "Super", "W", "Shift", "S", "Hyper", "H",
71                              NULL };
72
73     while ((next = strchr(s, '-'))) {
74         /* it's a modifier, validate it */
75         const gchar **it;
76         gboolean found = FALSE;
77
78         for (it = valid; *it && !found; ++it)
79             if (!g_ascii_strncasecmp(*it, s, strlen(*it)))
80                 found = TRUE;
81
82         if (!found) {
83             g_print("Invalid modifier\n");
84             return FALSE;
85         }
86
87         s = next + 1; /* skip past the '-' */
88     }
89     /* we're at the real key part */
90     if (!gdk_keyval_from_name(s)) {
91         g_print("Invalid key: %s\n", s);
92         return FALSE;
93     }
94
95     return TRUE;
96 }