Make the relative desktop switching actions interactive
[openbox.git] / openbox / translate.c
blob50d500082ce098757b795972700f6f4abebf4279
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 translate.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "openbox.h"
21 #include "mouse.h"
22 #include "gettext.h"
23 #include "obt/keyboard.h"
25 #include <glib.h>
26 #include <string.h>
27 #include <stdlib.h>
29 static guint translate_modifier(gchar *str)
31 guint mask = 0;
33 if (!g_ascii_strcasecmp("Mod1", str)) mask = Mod1Mask;
34 else if (!g_ascii_strcasecmp("Mod2", str)) mask = Mod2Mask;
35 else if (!g_ascii_strcasecmp("Mod3", str)) mask = Mod3Mask;
36 else if (!g_ascii_strcasecmp("Mod4", str)) mask = Mod4Mask;
37 else if (!g_ascii_strcasecmp("Mod5", str)) mask = Mod5Mask;
39 else if (!g_ascii_strcasecmp("Control", str) ||
40 !g_ascii_strcasecmp("C", str))
41 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_CONTROL);
42 else if (!g_ascii_strcasecmp("Alt", str) ||
43 !g_ascii_strcasecmp("A", str))
44 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_ALT);
45 else if (!g_ascii_strcasecmp("Meta", str) ||
46 !g_ascii_strcasecmp("M", str))
47 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_META);
48 /* W = windows key, is linked to the Super_L/R buttons */
49 else if (!g_ascii_strcasecmp("Super", str) ||
50 !g_ascii_strcasecmp("W", str))
51 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SUPER);
52 else if (!g_ascii_strcasecmp("Shift", str) ||
53 !g_ascii_strcasecmp("S", str))
54 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SHIFT);
55 else if (!g_ascii_strcasecmp("Hyper", str) ||
56 !g_ascii_strcasecmp("H", str))
57 mask = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_HYPER);
58 else
59 g_message(_("Invalid modifier key \"%s\" in key/mouse binding"), str);
61 return mask;
64 gboolean translate_button(const gchar *str, guint *state, guint *button)
66 gchar **parsed;
67 gchar *l;
68 gint i;
69 gboolean ret = FALSE;
71 parsed = g_strsplit(str, "-", -1);
73 /* first, find the button (last token) */
74 l = NULL;
75 for (i = 0; parsed[i] != NULL; ++i)
76 l = parsed[i];
77 if (l == NULL)
78 goto translation_fail;
80 /* figure out the mod mask */
81 *state = 0;
82 for (i = 0; parsed[i] != l; ++i) {
83 guint m = translate_modifier(parsed[i]);
84 if (!m) goto translation_fail;
85 *state |= m;
88 /* figure out the button */
89 if (!g_ascii_strcasecmp("Left", l)) *button = 1;
90 else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
91 else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
92 else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
93 else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
94 else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
95 if (!*button)
96 goto translation_fail;
98 ret = TRUE;
100 translation_fail:
101 g_strfreev(parsed);
102 return ret;
105 gboolean translate_key(const gchar *str, guint *state, guint *keycode)
107 gchar **parsed;
108 gchar *l;
109 gint i;
110 gboolean ret = FALSE;
111 KeySym sym;
113 parsed = g_strsplit(str, "-", -1);
115 *state = *keycode = 0;
117 /* first, find the key (last token) */
118 l = NULL;
119 for (i = 0; parsed[i] != NULL; ++i)
120 l = parsed[i];
121 if (l == NULL)
122 goto translation_fail;
124 /* figure out the mod mask */
125 *state = 0;
126 for (i = 0; parsed[i] != l; ++i) {
127 guint m = translate_modifier(parsed[i]);
128 if (!m) goto translation_fail;
129 *state |= m;
132 if (!g_ascii_strncasecmp("0x", l, 2)) {
133 gchar *end;
135 /* take it directly */
136 *keycode = strtol(l, &end, 16);
137 if (*l == '\0' || *end != '\0') {
138 g_message(_("Invalid key code \"%s\" in key binding"), l);
139 goto translation_fail;
141 } else {
142 /* figure out the keycode */
143 sym = XStringToKeysym(l);
144 if (sym == NoSymbol) {
145 g_message(_("Invalid key name \"%s\" in key binding"), l);
146 goto translation_fail;
148 *keycode = XKeysymToKeycode(obt_display, sym);
150 if (!*keycode) {
151 g_message(_("Requested key \"%s\" does not exist on the display"), l);
152 goto translation_fail;
155 ret = TRUE;
157 translation_fail:
158 g_strfreev(parsed);
159 return ret;