]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/mouse/translate.c
add a leftHanded option for mouse bindings, reverses the left/right keywords
[mikachu/openbox.git] / plugins / mouse / translate.c
1 #include "../../kernel/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 = mouse_lefthand ? 3 : 1;
50     else if (!g_ascii_strcasecmp("Middle", l)) *button = 2;
51     else if (!g_ascii_strcasecmp("Right", l)) *button = mouse_lefthand ? 1 : 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 }