1 /*****
2 * Xnee's Not an Event Emulator
3 *
4 * Xnee enables recording and replaying of X protocol data
5 *
6 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
7 * 2008, 2009, 2010 Henrik Sandklef
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or any later version.
13 *
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Boston,
23 * MA 02110-1301, USA.
24 ****/
25
26 #include "libxnee/xnee.h"
27 #include "libxnee/xnee_error.h"
28 #include "libxnee/xnee_grab.h"
29 #include "libxnee/xnee_alloc.h"
30 #include "libxnee/xnee_range.h"
31 #include "libxnee/xnee_utils.h"
32 #include "libxnee/xnee_session.h"
33 #include "libxnee/xnee_record.h"
34 #include "libxnee/xnee_setget.h"
35 #include "libxnee/xnee_resource.h"
36 #include "libxnee/xnee_fileop.h"
37 #include "libxnee/print_varargs.h"
38
39
40 int xnee_free_dyn_data(xnee_data *xd);
41
42
43 /**************************************************************
44 * *
45 * xnee_new_recordext_setup *
46 * *
47 * *
48 **************************************************************/
49 /*@null@*/
50 xnee_recordext_setup*
xnee_new_recordext_setup()51 xnee_new_recordext_setup()
52 {
53 xnee_recordext_setup* xrs ;
54 xrs = (xnee_recordext_setup*) malloc (sizeof (xnee_recordext_setup));
55 XNEE_CHECK_ALLOCED_MEMORY(xrs);
56
57 memset (xrs, 0, sizeof (xnee_recordext_setup));
58
59 if (xrs->xids != NULL)
60 {
61 XNEE_FREE_IF_NOT_NULL(xrs->xids);
62 }
63 /*@ignore@*/ /* we can ignore splint warning since we know we have freed it */
64 xrs->xids = (XRecordClientSpec*) calloc (2, sizeof(XRecordClientSpec));
65 /*@end@*/
66 XNEE_CHECK_ALLOCED_MEMORY(xrs->xids);
67
68 xrs->rContext = 0;
69
70 if (xrs->rState!=NULL)
71 {
72 XNEE_FREE_IF_NOT_NULL (xrs->rState);
73 }
74 /*@ignore@*/ /* we can ignore splint warning since we know we have freed it */
75 xrs->rState = (XRecordState*) malloc (sizeof(XRecordState));
76 /*@end@*/
77 XNEE_CHECK_ALLOCED_MEMORY(xrs->rState);
78 memset (xrs->rState, 0, sizeof (XRecordState));
79
80 /*
81 * Record ranges are allocated using seperate functions
82 */
83 return xrs;
84 }
85
86
87
88 /**************************************************************
89 * *
90 * xnee_free_recordext_setup *
91 * *
92 * *
93 **************************************************************/
94 /*@null@*/
95 int
xnee_free_recordext_setup(xnee_data * xd)96 xnee_free_recordext_setup(xnee_data* xd)
97 {
98 int i;
99 int max ;
100 int ret ;
101 /*@only@*/ /*@out@*/ /*@null@*/
102 xnee_recordext_setup *record_setup = NULL ;
103
104
105 XNEE_DEBUG ((stderr ," -->xnee_free_recordext_data() \n" ));
106
107 if ( (xd!=NULL) && (xd->record_setup!=NULL))
108 {
109 record_setup = xd->record_setup;
110
111 max = xnee_get_max_range(xd);
112
113 ret = xnee_unsetup_recording(xd);
114 if (ret!=XNEE_OK)
115 {
116 xnee_print_error ("Could not unsetup recording (%d)\n", ret);
117 return ret;
118 }
119
120 if (max>0)
121 {
122 for ( i=0 ; i<max ; i++ )
123 {
124 XNEE_DEBUG ( (stderr, " freeing range at %d since max=%d\n", i, max));
125 /*@ignore@*/
126 if ((record_setup != NULL) &&
127 (record_setup->range_array != NULL ) &&
128 (record_setup->range_array[i] != NULL ))
129 {
130 ret = XNEE_FREE_AND_NULL (record_setup->range_array[i]);
131 if (ret!=XNEE_OK)
132 {
133 xnee_print_error ("Could not free memory at "
134 "record_setup->range_array[%d] (%d)\n",
135 i, ret);
136 return ret;
137 }
138 }
139 /*@end@*/
140 }
141 }
142 XNEE_FREE_IF_NOT_NULL (record_setup->xids);
143 XNEE_FREE_IF_NOT_NULL (record_setup->rState);
144 XNEE_FREE_IF_NOT_NULL (xd->record_setup);
145 XNEE_FREE_AND_NULL (record_setup->range_array);
146 }
147
148
149 XNEE_DEBUG ( (stderr ," <--xnee_free_recordext_setup\n" ));
150 return XNEE_OK;
151 /*@end@*/
152 }
153
154
155 void
xnee_free_replay_setup(xnee_testext_setup * xrs)156 xnee_free_replay_setup( /*@only@*/ /*@null@*/ xnee_testext_setup* xrs)
157 {
158 XNEE_FREE_IF_NOT_NULL (xrs);
159 return ;
160 }
161
162 /*@null@*/
163 xnee_testext_setup*
xnee_new_replay_setup(void)164 xnee_new_replay_setup(void)
165 {
166 xnee_testext_setup* xrs = (xnee_testext_setup*) malloc (sizeof (xnee_testext_setup)) ;
167 XNEE_CHECK_ALLOCED_MEMORY(xrs);
168 memset (xrs, 0, sizeof(xnee_testext_setup));
169 return xrs;
170 }
171
172 static int
xnee_new_dyn_data(xnee_data * xd)173 xnee_new_dyn_data(xnee_data *xd)
174 {
175 if (xd==NULL)
176 {
177 return XNEE_MEMORY_FAULT;
178 }
179
180 xnee_verbose((xd, "---> xnee_new_dyn_data\n"));
181
182 /*
183 * Freeing old memory
184 */
185 xnee_verbose((xd, "--- xnee_new_dyn_data: freeing old memory (if found)\n"));
186 xnee_free_dyn_data(xd);
187
188
189 /*
190 * Getting new memory
191 */
192 xd->replay_setup = xnee_new_replay_setup();
193 XNEE_CHECK_ALLOCED_MEMORY_INT(xd->replay_setup);
194
195 xd->record_setup = xnee_new_recordext_setup();
196 XNEE_CHECK_ALLOCED_MEMORY_INT(xd->record_setup);
197
198 xd->grab_keys = xnee_new_grab_keys(xd);
199 XNEE_CHECK_ALLOCED_MEMORY_INT(xd->grab_keys);
200
201 return XNEE_OK;
202 }
203
204 #ifdef OBSOLETE
205 static int
xnee_reset_retype_info(xnee_data * xd)206 xnee_reset_retype_info(/*@null@*/ xnee_data *xd)
207 {
208 if (xd==NULL)
209 {
210 return XNEE_NO_MAIN_DATA;
211 }
212
213 xnee_set_retype_press_delay(xd, 100);
214 xnee_set_retype_release_delay(xd, 100);
215
216 return XNEE_OK;
217 }
218 #endif /* OBSOLETE */
219
220 static int
xnee_reset_xnee_info(xnee_data * xd)221 xnee_reset_xnee_info(/*@null@*/ xnee_data *xd)
222 {
223 int i ;
224 if (xd==NULL)
225 {
226 return XNEE_NO_MAIN_DATA;
227 }
228
229 for (i=0;i<XNEE_NR_OF_TYPES;i++)
230 {
231 xd->xnee_info.data_ranges[i]=0;
232 }
233
234 xnee_free_ranges(xd);
235
236 return XNEE_OK;
237 }
238
239
240 int
xnee_free_dyn_data(xnee_data * xd)241 xnee_free_dyn_data(xnee_data *xd)
242 {
243 int ret;
244 int i ;
245 xnee_verbose((xd, "---> xnee_free_dyn_data\n"));
246
247
248 if ( xd->grab_keys && xd->grab_keys->action_keys)
249 {
250 for (i=0;i<XNEE_GRAB_LAST;i++)
251 {
252 XNEE_FREE_IF_NOT_NULL(xd->grab_keys->action_keys[i].str);
253 XNEE_FREE_IF_NOT_NULL(xd->grab_keys->action_keys[i].extra_str);
254 }
255 }
256
257 xnee_verbose((xd, " --- xnee_free_dyn_data: program name\n"));
258 XNEE_FREE_IF_NOT_NULL(xd->program_name);
259
260 xnee_verbose((xd, " --- xnee_free_dyn_data: program name\n"));
261 XNEE_FREE_IF_NOT_NULL(xd->program_name);
262
263 xnee_verbose((xd, " --- xnee_free_dyn_data: refreshing ranges\n"));
264 ret = xnee_free_ranges(xd);
265 XNEE_RETURN_IF_ERR(ret);
266
267 xnee_verbose((xd, " --- xnee_free_dyn_data: replay_setup\n"));
268 xnee_free_replay_setup(xd->replay_setup);
269 xd->replay_setup = NULL;
270
271 xnee_verbose((xd, " --- xnee_free_dyn_data: record_ext\n"));
272 ret = xnee_free_recordext_setup (xd);
273 XNEE_RETURN_IF_ERR(ret);
274
275 xnee_verbose((xd, " --- xnee_free_dyn_data: grab_keys\n"));
276 ret = xnee_free_grab_keys(xd->grab_keys);
277 XNEE_RETURN_IF_ERR(ret);
278 xd->grab_keys=NULL;
279
280 xnee_verbose((xd, " --- xnee_free_dyn_data: resource_meta\n"));
281 ret = xnee_free_xnee_resource_meta(&xd->xrm);
282 XNEE_RETURN_IF_ERR(ret);
283
284 xnee_verbose((xd, " --- xnee_free_dyn_data: xnee_info\n"));
285 ret = xnee_reset_xnee_info(xd);
286 XNEE_RETURN_IF_ERR(ret);
287
288 xnee_verbose((xd, " --- xnee_free_dyn_data: x version info\n"));
289 XNEE_FREE_IF_NOT_NULL(xd->x_vendor_name);
290
291 xnee_verbose((xd, "<--- xnee_free_dyn_data\n"));
292 return XNEE_OK;
293 }
294
295
296
297
298
299
300 /**************************************************************
301 * *
302 * xnee_new_xnee_data *
303 * *
304 * *
305 **************************************************************/
306 /*@null@*/
307 xnee_data*
xnee_new_xnee_data()308 xnee_new_xnee_data()
309 {
310 int ret ;
311 xnee_data* xd = (xnee_data*) malloc (sizeof (xnee_data));
312
313 if (xd==NULL)
314 {
315 xnee_print_error ("Could not allocate memory ........\n");
316 return NULL;
317 }
318 memset (xd, 0, sizeof(xnee_data));
319
320 xd->grab_keys=NULL;
321
322 ret = xnee_new_dyn_data(xd);
323 XNEE_RETURN_NULL_IF_ERR(ret);
324
325
326 ret = xnee_init(xd);
327 XNEE_RETURN_NULL_IF_ERR(ret);
328
329 /* xnee_init_strings(xd); */
330
331 /* Set the signal handler the libxnee's built in */
332 (void) signal (SIGINT, signal_handler);
333
334 /* Set the error handler the libxnee's built in */
335 (void) XSetErrorHandler (handle_xerr);
336
337 xnee_set_xnee_data (xd);
338
339 return xd;
340 }
341
342
343 /**************************************************************
344 * *
345 * xnee_free_xnee_data *
346 * *
347 * *
348 **************************************************************/
349 int
xnee_free_xnee_data(xnee_data * xd)350 xnee_free_xnee_data(xnee_data* xd)
351 {
352 int ret ;
353
354 ret = xnee_free_dyn_data(xd);
355 XNEE_RETURN_IF_ERR(ret);
356
357 #ifdef XNEE_USE_SEMAPHORES
358 free (xd->buf_sem);
359 #endif
360 free (xd);
361 return XNEE_OK;
362 }
363
364
365
366 int
xnee_renew_xnee_data(xnee_data * xd)367 xnee_renew_xnee_data(xnee_data *xd)
368 {
369 int ret ;
370
371 ret = xnee_free_dyn_data(xd);
372 XNEE_RETURN_IF_ERR (ret);
373
374 ret = xnee_new_dyn_data(xd);
375 XNEE_RETURN_IF_ERR (ret);
376
377
378 /*
379 * Reset counters...
380 */
381 xd->first_read_time = 0;
382
383 xd->meta_data.sum_max = 0;
384 xd->meta_data.sum_min = 0;
385
386 xd->meta_data.total_diff = 0;
387 xd->meta_data.cached_max = 0;
388 xd->meta_data.cached_min = 0;
389
390 return (XNEE_OK);
391 }
392
393 int
xnee_free(void * mem)394 xnee_free( /*@only@*/ /*@out@*/ /*@null@*/ void *mem)
395 {
396 #ifdef __x86_64
397 if ( (mem==NULL) || ((unsigned long)mem==0x1) )
398 #else
399 if ( (mem==NULL) || ((int)mem==0x1) )
400 #endif
401 {
402 return XNEE_MEMORY_FAULT;
403 }
404 /* fprintf(stderr, "Freeing at %lu\n", mem); */
405 free(mem);
406 return XNEE_OK;
407 }
408
409
410
411