]> icculus.org git repositories - dana/openbox.git/blob - openbox/translate.c
the focus_client = NULL got moved by mistake.
[dana/openbox.git] / openbox / translate.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    translate.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
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    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox.h"
21 #include "mouse.h"
22 #include "gettext.h"
23 #include <glib.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 static guint translate_modifier(gchar *str)
28 {
29     if (!g_ascii_strcasecmp("Mod1", str) ||
30         !g_ascii_strcasecmp("A", str)) return Mod1Mask;
31     else if (!g_ascii_strcasecmp("Mod2", str)) return Mod2Mask;
32     else if (!g_ascii_strcasecmp("Mod3", str) ||
33              !g_ascii_strcasecmp("M", str)) return Mod3Mask;
34     else if (!g_ascii_strcasecmp("Mod4", str) ||
35              !g_ascii_strcasecmp("W", str)) return Mod4Mask;
36     else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
37     else if (!g_ascii_strcasecmp("Control", str) ||
38              !g_ascii_strcasecmp("C", str)) return ControlMask;
39     else if (!g_ascii_strcasecmp("Shift", str) ||
40              !g_ascii_strcasecmp("S", str)) return ShiftMask;
41     g_message(_("Invalid modifier key '%s' in key/pointer binding"), str);
42     return 0;
43 }
44
45 gboolean translate_button(const gchar *str, guint *state, guint *button)
46 {
47     gchar **parsed;
48     gchar *l;
49     gint i;
50     gboolean ret = FALSE;
51
52     parsed = g_strsplit(str, "-", -1);
53     
54     /* first, find the button (last token) */
55     l = NULL;
56     for (i = 0; parsed[i] != NULL; ++i)
57         l = parsed[i];
58     if (l == NULL)
59         goto translation_fail;
60
61     /* figure out the mod mask */
62     *state = 0;
63     for (i = 0; parsed[i] != l; ++i) {
64         guint m = translate_modifier(parsed[i]);
65         if (!m) goto translation_fail;
66         *state |= m;
67     }
68
69     /* figure out the button */
70     if (!g_ascii_strcasecmp("Left", l)) *button = 1;
71     else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
72     else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
73     else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
74     else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
75     else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
76     if (!*button) {
77         g_message(_("Invalid button '%s' in pointer binding"), l);
78         goto translation_fail;
79     }
80
81     ret = TRUE;
82
83 translation_fail:
84     g_strfreev(parsed);
85     return ret;
86 }
87
88 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
89 {
90     gchar **parsed;
91     gchar *l;
92     gint i;
93     gboolean ret = FALSE;
94     KeySym sym;
95
96     parsed = g_strsplit(str, "-", -1);
97     
98     /* first, find the key (last token) */
99     l = NULL;
100     for (i = 0; parsed[i] != NULL; ++i)
101         l = parsed[i];
102     if (l == NULL)
103         goto translation_fail;
104
105     /* figure out the mod mask */
106     *state = 0;
107     for (i = 0; parsed[i] != l; ++i) {
108         guint m = translate_modifier(parsed[i]);
109         if (!m) goto translation_fail;
110         *state |= m;
111     }
112
113     if (!g_ascii_strncasecmp("0x", l, 2)) {
114         gchar *end;
115
116         /* take it directly */
117         *keycode = strtol(l, &end, 16);
118         if (*l == '\0' || *end != '\0') {
119             g_message(_("Invalid key code '%s' in key binding"), l);
120             goto translation_fail;
121         }
122     } else {
123         /* figure out the keycode */
124         sym = XStringToKeysym(l);
125         if (sym == NoSymbol) {
126             g_message(_("Invalid key name '%s' in key binding"), l);
127             goto translation_fail;
128         }
129         *keycode = XKeysymToKeycode(ob_display, sym);
130     }
131     if (!*keycode) {
132         g_message(_("Requested key '%s' does not exist on the display"), l); 
133         goto translation_fail;
134     }
135
136     ret = TRUE;
137
138 translation_fail:
139     g_strfreev(parsed);
140     return ret;
141 }