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