]> Dogcows Code - chaz/openbox/blob - plugins/mouse/mouserc_parse.l
use the action_from_string helper
[chaz/openbox] / 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_move_relative ||
96 a->func == action_resize_relative)) {
97 action_free(a);
98 a = NULL;
99 }
100 if (a == NULL) {
101 g_warning("Invalid action '%s' in '%s' on line %d", action, path,
102 lineno);
103 return;
104 }
105
106 if (!mbind(button, context, mact, a)) {
107 action_free(a);
108 g_warning("Unable to add binding '%s %s %s %s'",
109 context, event, button, action);
110 }
111 }
112
113
114 static int mparsewrap()
115 {
116 g_free(context); g_free(event); g_free(button); g_free(action);
117 return 1;
118 }
119
120 void mouserc_parse()
121 {
122 path = g_build_filename(g_get_home_dir(), ".openbox", "mouserc", NULL);
123 if ((mparsein = fopen(path, "r")) == NULL) {
124 g_free(path);
125 path = g_build_filename(RCDIR, "mouserc", NULL);
126 if ((mparsein = fopen(path, "r")) == NULL) {
127 g_warning("No mouserc file found!");
128 g_free(path);
129 return;
130 }
131 }
132
133 lineno = 1;
134 error = FALSE;
135 context = event = button = action = NULL;
136
137 mparselex();
138
139 g_free(path);
140 }
This page took 0.047064 seconds and 5 git commands to generate.