1 /* action_item.c -- data for a single recognizer action
2
3 Copyright 2001 Carl Worth
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <errno.h>
20
21 #include "rec.h"
22 #include "rec_mode.h"
23 #include "action_item.h"
24 #include "sprintf_alloc.h"
25
26 #ifdef DMALLOC
27 #include "dmalloc.h"
28 #endif
29
action_item_key_init(action_item_t * item,char * key,int press)30 int action_item_key_init(action_item_t *item, char *key, int press)
31 {
32 item->type = ACTION_KEY;
33 item->data = action_key_data_alloc(key, press);
34
35 return item->data ? 0 : ENOMEM;
36 }
37
action_item_button_init(action_item_t * item,int button)38 int action_item_button_init(action_item_t *item, int button)
39 {
40 item->type = ACTION_BUTTON;
41 item->data = action_button_data_alloc(button);
42
43 return item->data ? 0 : ENOMEM;
44 }
45
action_item_mode_init(action_item_t * item,rec_mode_t * mode,int permanent)46 int action_item_mode_init(action_item_t *item, rec_mode_t *mode, int permanent)
47 {
48 item->type = ACTION_MODE;
49 item->data = action_mode_data_alloc(mode, permanent);
50
51 return item->data ? 0 : ENOMEM;
52 }
53
action_item_exec_init(action_item_t * item,char * exec)54 int action_item_exec_init(action_item_t *item, char *exec)
55 {
56 item->type = ACTION_EXEC;
57 item->data = action_exec_data_alloc(exec);
58
59 return item->data ? 0 : ENOMEM;
60 }
61
action_item_orient_init(action_item_t * item,double orientation)62 int action_item_orient_init(action_item_t *item, double orientation)
63 {
64 item->type = ACTION_ORIENT;
65 item->data = action_orient_data_alloc(orientation);
66
67 return item->data ? 0 : ENOMEM;
68 }
69
action_item_deinit(action_item_t * item)70 void action_item_deinit(action_item_t *item)
71 {
72 switch (item->type) {
73 case ACTION_KEY:
74 action_key_data_deinit(item->data);
75 break;
76 case ACTION_BUTTON:
77 action_button_data_deinit(item->data);
78 break;
79 case ACTION_MODE:
80 action_mode_data_deinit(item->data);
81 break;
82 case ACTION_EXEC:
83 action_exec_data_deinit(item->data);
84 break;
85 case ACTION_ORIENT:
86 action_orient_data_deinit(item->data);
87 break;
88 default:
89 fprintf(stderr, "%s: Unknown action type: %d\n", __FUNCTION__, item->type);
90 break;
91 }
92 free(item->data);
93 item->data = NULL;
94 item->type = 0;
95 }
96
action_key_data_alloc(char * key,int press)97 action_key_data_t *action_key_data_alloc(char *key, int press)
98 {
99 action_key_data_t *data;
100
101 data = malloc(sizeof(action_key_data_t));
102 if (data == NULL) {
103 fprintf(stderr, "%s: Out of memory", __FUNCTION__);
104 return NULL;
105 }
106 action_key_data_init(data, key, press);
107 return data;
108 }
109
action_key_data_init(action_key_data_t * key_data,char * key,int press)110 int action_key_data_init(action_key_data_t *key_data, char *key, int press)
111 {
112 KeySym keysym = XStringToKeysym(key);
113
114 if (keysym == NoSymbol) {
115 fprintf(stderr, "%s: ERROR: No Keysym found for %s\n", __FUNCTION__, key);
116 return EINVAL;
117 }
118
119 return action_keysym_data_init(key_data, keysym, press);
120 }
121
action_keysym_data_init(action_key_data_t * key_data,KeySym keysym,int press)122 int action_keysym_data_init(action_key_data_t *key_data, KeySym keysym, int press)
123 {
124 key_data->keysym = keysym;
125 key_data->keycode = 0;
126 key_data->flags = 0;
127 if (press) {
128 key_data->flags |= ACTION_KEY_IS_PRESS;
129 }
130 if (IsModifierKey(keysym)) {
131 key_data->flags |= ACTION_KEY_IS_MODIFIER;
132 }
133
134 return 0;
135 }
136
action_key_data_deinit(action_key_data_t * key_data)137 void action_key_data_deinit(action_key_data_t *key_data)
138 {
139 key_data->keysym = 0;
140 key_data->keycode = 0;
141 key_data->flags = 0;
142 }
143
action_button_data_alloc(int button)144 action_button_data_t *action_button_data_alloc(int button)
145 {
146 action_button_data_t *data;
147
148 data = malloc(sizeof(action_button_data_t));
149 if (data == NULL) {
150 fprintf(stderr, "%s: Out of memory", __FUNCTION__);
151 return NULL;
152 }
153 action_button_data_init(data, button);
154 return data;
155 }
156
action_button_data_init(action_button_data_t * button_data,int button)157 int action_button_data_init(action_button_data_t *button_data, int button)
158 {
159 button_data->button = button;
160
161 return 0;
162 }
163
action_button_data_deinit(action_button_data_t * button_data)164 void action_button_data_deinit(action_button_data_t *button_data)
165 {
166 button_data->button = 0;
167 }
168
action_mode_data_alloc(struct rec_mode * mode,int permanent)169 action_mode_data_t *action_mode_data_alloc(struct rec_mode *mode, int permanent)
170 {
171 action_mode_data_t *data;
172
173 data = malloc(sizeof(action_mode_data_t));
174 if (data == NULL) {
175 fprintf(stderr, "%s: Out of memory", __FUNCTION__);
176 return NULL;
177 }
178 action_mode_data_init(data, mode, permanent);
179 return data;
180 }
181
action_mode_data_init(action_mode_data_t * mode_data,struct rec_mode * mode,int permanent)182 int action_mode_data_init(action_mode_data_t *mode_data,
183 struct rec_mode *mode, int permanent)
184 {
185 mode_data->mode = mode;
186 mode_data->permanent = permanent;
187
188 return 0;
189 }
190
action_mode_data_deinit(action_mode_data_t * mode_data)191 void action_mode_data_deinit(action_mode_data_t *mode_data)
192 {
193 mode_data->permanent = 0;
194 mode_data->mode = NULL;
195 }
196
action_exec_data_alloc(char * exec)197 action_exec_data_t *action_exec_data_alloc(char *exec)
198 {
199 action_exec_data_t *data;
200
201 data = malloc(sizeof(action_exec_data_t));
202 if (data == NULL) {
203 fprintf(stderr, "%s: Out of memory", __FUNCTION__);
204 return NULL;
205 }
206 action_exec_data_init(data, exec);
207 return data;
208 }
209
action_exec_data_init(action_exec_data_t * exec_data,char * exec)210 int action_exec_data_init(action_exec_data_t *exec_data, char *exec)
211 {
212 exec_data->exec = strdup(exec);
213
214 return 0;
215 }
216
action_exec_data_deinit(action_exec_data_t * exec_data)217 void action_exec_data_deinit(action_exec_data_t *exec_data)
218 {
219 free(exec_data->exec);
220 exec_data->exec = NULL;
221 }
222
action_orient_data_alloc(double orientation)223 action_orient_data_t *action_orient_data_alloc(double orientation)
224 {
225 action_orient_data_t *data;
226
227 data = malloc(sizeof(action_orient_data_t));
228 if (data == NULL) {
229 fprintf(stderr, "%s: Out of memory", __FUNCTION__);
230 return NULL;
231 }
232 action_orient_data_init(data, orientation);
233 return data;
234 }
235
action_orient_data_init(action_orient_data_t * orient_data,double orientation)236 int action_orient_data_init(action_orient_data_t *orient_data, double orientation)
237 {
238 orient_data->orientation = orientation;
239 return 0;
240 }
241
action_orient_data_deinit(action_orient_data_t * orient_data)242 void action_orient_data_deinit(action_orient_data_t *orient_data)
243 {
244 /* nothing to see here, move along... */
245 }
246
action_item_str_alloc(action_item_t * item)247 char *action_item_str_alloc(action_item_t *item)
248 {
249 char *str = NULL;
250
251 switch (item->type) {
252 case ACTION_KEY:
253 {
254 action_key_data_t *key_data = (action_key_data_t *) item->data;
255 sprintf_alloc(&str, "Key %s", XKeysymToString(key_data->keysym));
256 break;
257 }
258 case ACTION_BUTTON:
259 {
260 action_button_data_t *button_data = (action_button_data_t *) item->data;
261 sprintf_alloc(&str, "Button %d", button_data->button);
262 break;
263 }
264 case ACTION_MODE:
265 {
266 action_mode_data_t *mode_data = (action_mode_data_t *) item->data;
267 sprintf_alloc(&str, "%s %s",
268 mode_data->permanent ? "ModeLock" : "ModeShift",
269 mode_data->mode->name);
270 break;
271 }
272 case ACTION_EXEC:
273 {
274 action_exec_data_t *exec_data = (action_exec_data_t *) item->data;
275 sprintf_alloc(&str, "Exec \"%s\"", exec_data->exec);
276 break;
277 }
278 case ACTION_ORIENT:
279 {
280 action_orient_data_t *orient_data = (action_orient_data_t *) item->data;
281 sprintf_alloc(&str, "OrientationCorrection %f",
282 (180 / M_PI) * orient_data->orientation);
283 break;
284 }
285 default:
286 fprintf(stderr, "%s: Unknown action type: %d\n", __FUNCTION__, item->type);
287 break;
288 }
289 return str;
290 }
291