]> Dogcows Code - chaz/openbox/blob - openbox/translate.c
use the values passed to the function
[chaz/openbox] / openbox / translate.c
1 #include "openbox.h"
2 #include "mouse.h"
3 #include <glib.h>
4 #include <string.h>
5 #include <stdlib.h>
6
7 static guint translate_modifier(char *str)
8 {
9 if (!g_ascii_strcasecmp("Mod1", str) ||
10 !g_ascii_strcasecmp("A", str)) return Mod1Mask;
11 else if (!g_ascii_strcasecmp("Mod2", str)) return Mod2Mask;
12 else if (!g_ascii_strcasecmp("Mod3", str)) return Mod3Mask;
13 else if (!g_ascii_strcasecmp("Mod4", str) ||
14 !g_ascii_strcasecmp("W", str)) return Mod4Mask;
15 else if (!g_ascii_strcasecmp("Mod5", str)) return Mod5Mask;
16 else if (!g_ascii_strcasecmp("Control", str) ||
17 !g_ascii_strcasecmp("C", str)) return ControlMask;
18 else if (!g_ascii_strcasecmp("Shift", str) ||
19 !g_ascii_strcasecmp("S", str)) return ShiftMask;
20 g_warning("Invalid modifier '%s' in binding.", str);
21 return 0;
22 }
23
24 gboolean translate_button(char *str, guint *state, guint *button)
25 {
26 char **parsed;
27 char *l;
28 int i;
29 gboolean ret = FALSE;
30
31 parsed = g_strsplit(str, "-", -1);
32
33 /* first, find the button (last token) */
34 l = NULL;
35 for (i = 0; parsed[i] != NULL; ++i)
36 l = parsed[i];
37 if (l == NULL)
38 goto translation_fail;
39
40 /* figure out the mod mask */
41 *state = 0;
42 for (i = 0; parsed[i] != l; ++i) {
43 guint m = translate_modifier(parsed[i]);
44 if (!m) goto translation_fail;
45 *state |= m;
46 }
47
48 /* figure out the button */
49 if (!g_ascii_strcasecmp("Left", l)) *button = 1;
50 else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
51 else if (!g_ascii_strcasecmp("Right", l)) *button = 3;
52 else if (!g_ascii_strcasecmp("Up", l)) *button = 4;
53 else if (!g_ascii_strcasecmp("Down", l)) *button = 5;
54 else if (!g_ascii_strncasecmp("Button", l, 6)) *button = atoi(l+6);
55 if (!*button) {
56 g_warning("Invalid button '%s' in pointer binding.", l);
57 goto translation_fail;
58 }
59
60 ret = TRUE;
61
62 translation_fail:
63 g_strfreev(parsed);
64 return ret;
65 }
66
67 gboolean translate_key(char *str, guint *state, guint *keycode)
68 {
69 char **parsed;
70 char *l;
71 int i;
72 gboolean ret = FALSE;
73 KeySym sym;
74
75 parsed = g_strsplit(str, "-", -1);
76
77 /* first, find the key (last token) */
78 l = NULL;
79 for (i = 0; parsed[i] != NULL; ++i)
80 l = parsed[i];
81 if (l == NULL)
82 goto translation_fail;
83
84 /* figure out the mod mask */
85 *state = 0;
86 for (i = 0; parsed[i] != l; ++i) {
87 guint m = translate_modifier(parsed[i]);
88 if (!m) goto translation_fail;
89 *state |= m;
90 }
91
92 /* figure out the keycode */
93 sym = XStringToKeysym(l);
94 if (sym == NoSymbol) {
95 g_warning("Invalid key name '%s' in key binding.", l);
96 goto translation_fail;
97 }
98 *keycode = XKeysymToKeycode(ob_display, sym);
99 if (!*keycode) {
100 g_warning("Key '%s' does not exist on the display.", l);
101 goto translation_fail;
102 }
103
104 ret = TRUE;
105
106 translation_fail:
107 g_strfreev(parsed);
108 return ret;
109 }
This page took 0.042963 seconds and 4 git commands to generate.