]> icculus.org git repositories - mikachu/openbox.git/blob - plugins/mouse/mouserc_parse.l
add a shaded button
[mikachu/openbox.git] / plugins / mouse / mouserc_parse.l
1 %{
2 #include "mouse.h"
3 #include <glib.h>
4 #ifdef HAVE_STDIO_H
5 #  include <stdio.h>
6 #endif
7
8 static int lineno;
9 static char *path;
10 static gboolean error;
11
12 static char *context;
13 static char *event;
14 static char *button;
15 static char *action;
16
17 static void endofline();
18 static int mparsewrap();
19 static void gotfield();
20 static void addbinding();
21 %}
22
23 field [A-Za-z0-9][-A-Za-z0-9]*
24 white [ \t]*
25
26 %%
27
28 ^{white}#.*\n lineno++;
29 {field} gotfield();
30 \n endofline();
31 [ \t]
32 . error = TRUE;
33
34 %%
35
36 static void gotfield()
37 {
38     if (context == NULL)
39         context = g_strdup(mparsetext);
40     else if (event == NULL)
41         event = g_strdup(mparsetext);
42     else if (button == NULL)
43         button = g_strdup(mparsetext);
44     else if (action == NULL)
45         action = g_strdup(mparsetext);
46     else
47         error = TRUE;
48 }
49
50 static void endofline()
51 {
52     if (!error && context && event && button && action)
53         addbinding();
54     else if (error || context || event || button || action)
55         g_warning("Parser error in '%s' on line %d", path, lineno);
56
57     error = FALSE;
58     g_free(context); g_free(event); g_free(button); g_free(action);
59     context = event = button = action = NULL;
60
61     ++lineno;
62 }
63
64 static void addbinding()
65 {
66     Action *a = NULL;
67     MouseAction mact;
68
69     if (!g_ascii_strcasecmp(event, "press"))
70         mact = MouseAction_Press;
71     else if (!g_ascii_strcasecmp(event, "release"))
72         mact = MouseAction_Release;
73     else if (!g_ascii_strcasecmp(event, "click"))
74         mact = MouseAction_Click;
75     else if (!g_ascii_strcasecmp(event, "doubleclick"))
76         mact = MouseAction_DClick;
77     else if (!g_ascii_strcasecmp(event, "drag"))
78         mact = MouseAction_Motion;
79     else {
80         g_warning("Invalid event '%s' in '%s' on line %d", event, path,
81                   lineno);
82         return;
83     }
84
85     a = action_from_string(action);
86  
87     if (mact == MouseAction_Motion) {
88         if (a && !(a->func == action_move || a->func == action_resize)) {
89             action_free(a);
90             a = NULL;
91         }
92         /* the below types cannot be used with !motion events, or at all with
93            mouse bindings */
94     } else if (a && (a->func == action_move || a->func == action_resize ||
95                      a->func == action_execute || a->func == action_desktop ||
96                      a->func == action_move_relative_horz ||
97                      a->func == action_move_relative_vert ||
98                      a->func == action_resize_relative_horz ||
99                      a->func == action_resize_relative_vert)) {
100         action_free(a);
101         a = NULL;
102     }
103     if (a == NULL) {
104         g_warning("Invalid action '%s' in '%s' on line %d", action, path,
105                   lineno);
106         return;
107     }
108
109     if (!mbind(button, context, mact, a)) {
110         action_free(a);
111         g_warning("Unable to add binding '%s %s %s %s'",
112                   context, event, button, action);
113     }
114 }
115
116
117 static int mparsewrap()
118 {
119     g_free(context); g_free(event); g_free(button); g_free(action);
120     return 1;
121 }
122
123 void mouserc_parse()
124 {
125     path = g_build_filename(g_get_home_dir(), ".openbox", "mouserc", NULL);
126     if ((mparsein = fopen(path, "r")) == NULL) {
127         g_free(path);
128         path = g_build_filename(RCDIR, "mouserc", NULL);
129         if ((mparsein = fopen(path, "r")) == NULL) {
130             g_warning("No mouserc file found!");
131             g_free(path);
132             return;
133         }
134     }
135
136     lineno = 1;
137     error = FALSE;
138     context = event = button = action = NULL;
139
140     mparselex();
141
142     g_free(path);
143 }