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
7  *                      2004, 2005, 2006, 2007 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 
27 #include "libxnee/xnee.h"
28 #include "libxnee/xnee_setget.h"
29 #include "libxnee/xnee_resource.h"
30 #include "libxnee/xnee_range.h"
31 #include "libxnee/xnee_session.h"
32 #include "libxnee/xnee_alloc.h"
33 #include "libxnee/xnee_xinput.h"
34 
35 #include "parse.h"
36 #include "cnee_strings.h"
37 
38 
39 #define CNEE_DEMO_DELAYED_START 3
40 extern xnee_option_t *cnee_options ;
41 
42 static char *
cnee_get_default_filename()43 cnee_get_default_filename()
44 {
45   char *tmp_dir = NULL;
46   char *user_name = NULL;
47 
48   const char *default_tmp_dir   = "/tmp/";
49   const char *default_user_name = "user";
50   const char *file_prefix = "cnee_";
51   const char *file_suffix = ".xns";
52 
53   char *ret_str;
54   int   size = 0 ;
55 
56   /* set the dir */
57   tmp_dir = getenv("TMPIR");
58   if (tmp_dir==NULL)
59     {
60       tmp_dir= (char *) default_tmp_dir;
61     }
62 
63   /* set the user name */
64   user_name = getlogin();
65   if (user_name==NULL)
66     {
67       user_name = (char *) default_user_name;
68     }
69 
70   size = strlen (tmp_dir) +
71     strlen (user_name)    +
72     strlen (file_prefix)  +
73     strlen (file_suffix) ;
74 
75   ret_str = (char*) malloc (size*sizeof(char));
76   if (ret_str==NULL)
77     {
78       return NULL;
79     }
80 
81   strncpy (ret_str, tmp_dir, size);
82   strncat (ret_str, file_prefix, size - strlen(ret_str));
83   strncat (ret_str, user_name, size - strlen(ret_str));
84   strncat (ret_str, file_suffix, size - strlen(ret_str));
85   return ret_str;
86 }
87 
88 
89 
90 int
cnee_demonstration(xnee_data * xd)91 cnee_demonstration (xnee_data *xd)
92 {
93   int ret;
94   char *file;
95 
96   /* Setup for recording */
97   ret = xnee_set_recorder(xd);
98   XNEE_RETURN_IF_ERR(ret);
99 
100   xnee_init_xinput(xd);
101 
102   ret = xnee_parse_range (xd, XNEE_DEVICE_EVENT,
103 			  "ButtonPress-MotionNotify");
104   XNEE_RETURN_IF_ERR(ret);
105 #ifdef  XNEE_XINPUT_SUPPORT
106       xnee_xinput_request_mouse(xd);
107 #endif /*  XNEE_XINPUT_SUPPORT      */
108 
109   ret = xnee_parse_range (xd, XNEE_DEVICE_EVENT,
110 			  "KeyPress-KeyRelease");
111   XNEE_RETURN_IF_ERR(ret);
112 #ifdef  XNEE_XINPUT_SUPPORT
113       xnee_xinput_request_mouse(xd);
114 #endif /*  XNEE_XINPUT_SUPPORT      */
115 
116   file = cnee_get_default_filename();
117   if (file==NULL)
118     {
119       return XNEE_FILE_NOT_FOUND;
120     }
121 
122   ret = xnee_set_out_name(xd, file);
123   XNEE_RETURN_IF_ERR(ret);
124 
125   ret = xnee_set_interval(xd, CNEE_DEMO_DELAYED_START);
126   XNEE_RETURN_IF_ERR(ret);
127 
128   ret = xnee_set_events_max(xd, 200);
129   XNEE_RETURN_IF_ERR(ret);
130 
131 
132   /* Set the cli parameters */
133   xnee_set_application_parameters (xd, NULL);
134   XNEE_RETURN_IF_ERR(ret);
135 
136   ret = xnee_prepare(xd);
137   if (ret==XNEE_OK)
138     {
139       fprintf (stderr,
140 	       "Xnee will now start recording your "
141 	       "mouse and keyboard in %d seconds\n",
142 	       CNEE_DEMO_DELAYED_START);
143 
144       /* start up the action set during parsing the commnd line */
145       ret = xnee_start(xd);
146     }
147 
148   if ( ret != XNEE_OK)
149     {
150       xnee_print_error (PACKAGE " failed to start\n");
151       xnee_print_error ("   Error number:    %d\n", ret);
152       xnee_print_error ("   Error string:    '%s'\n",
153 			xnee_get_err_description(ret));
154       xnee_print_error ("   Solution string: '%s'\n",
155 			xnee_get_err_solution(ret));
156       exit(ret);
157     }
158   else
159     {
160       fprintf (stderr, "Reording went well....\n");
161     }
162 
163   ret = xnee_renew_xnee_data(xd);
164   XNEE_RETURN_IF_ERR(ret);
165 
166   ret = xnee_set_data_name (xd, file);
167   XNEE_RETURN_IF_ERR(ret);
168 
169   ret = xnee_set_replayer (xd);
170   XNEE_RETURN_IF_ERR(ret);
171 
172   ret = xnee_prepare(xd);
173   if (ret==XNEE_OK)
174     {
175       fprintf (stderr,
176 	       "Xnee will now replay your recorded "
177 	       "mouse and keyboard events in %d seconds\n",
178 	       CNEE_DEMO_DELAYED_START);
179 
180       /* start up the action set during parsing the commnd line */
181       ret = xnee_start(xd);
182     }
183   if ( ret != XNEE_OK)
184     {
185       xnee_print_error (PACKAGE " failed to start\n");
186       xnee_print_error ("   Error number:    %d\n", ret);
187       xnee_print_error ("   Error string:    '%s'\n",
188 			xnee_get_err_description(ret));
189       xnee_print_error ("   Solution string: '%s'\n",
190 			xnee_get_err_solution(ret));
191       exit(ret);
192     }
193   else
194     {
195       fprintf (stderr, "Replay went well....\n");
196     }
197 
198   fprintf (stderr, "By the way, you can find the recorded file here:\n\t%s\n",
199 	   file);
200   fprintf (stderr, "To replay that file again, type the following command\n");
201 
202   fprintf (stderr,
203 	   "\t"
204 	   XNEE_CLI " --%s --%s %s\n",
205 	   xnee_key2string(xd, xnee_options, XNEE_REPLAY_OPTION_KEY),
206 	   xnee_key2string(xd, cnee_options, CNEE_FILE_OPTION_KEY),
207 	   file);
208 
209   xnee_free (file);
210 
211 
212   /* hey, we are fin(n)ished .... close down */
213   xnee_close_down(xd);
214 
215   /* Since we are here, we can exit gracefully */
216   exit(XNEE_OK);
217 }
218 
219 
220 
221 int
cnee_record_replay(xnee_data * xd)222 cnee_record_replay (xnee_data *xd)
223 {
224   int ret;
225   char *file;
226 
227   /* Setup for recording */
228   ret = xnee_set_recorder(xd);
229   XNEE_RETURN_IF_ERR(ret);
230 
231   xnee_init_xinput(xd);
232 
233   ret = xnee_parse_range (xd, XNEE_DEVICE_EVENT,
234 			  "ButtonPress-MotionNotify");
235   XNEE_RETURN_IF_ERR(ret);
236 #ifdef  XNEE_XINPUT_SUPPORT
237       xnee_xinput_request_mouse(xd);
238 #endif /*  XNEE_XINPUT_SUPPORT      */
239 
240   ret = xnee_parse_range (xd, XNEE_DEVICE_EVENT,
241 			  "KeyPress-KeyRelease");
242   XNEE_RETURN_IF_ERR(ret);
243 #ifdef  XNEE_XINPUT_SUPPORT
244       xnee_xinput_request_mouse(xd);
245 #endif /*  XNEE_XINPUT_SUPPORT      */
246 
247   file = cnee_get_default_filename();
248   if (file==NULL)
249     {
250       return XNEE_FILE_NOT_FOUND;
251     }
252 
253   ret = xnee_set_out_name(xd, file);
254   XNEE_RETURN_IF_ERR(ret);
255 
256   ret = xnee_set_interval(xd, CNEE_DEMO_DELAYED_START);
257   XNEE_RETURN_IF_ERR(ret);
258 
259   ret = xnee_set_key (xd, XNEE_GRAB_STOP, "q");
260   XNEE_RETURN_IF_ERR(ret);
261 
262   /* Set the cli parameters */
263   xnee_set_application_parameters (xd, NULL);
264   XNEE_RETURN_IF_ERR(ret);
265 
266   ret = xnee_prepare(xd);
267   if (ret==XNEE_OK)
268     {
269       fprintf (stderr,
270 	       "Xnee will now start recording your "
271 	       "mouse and keyboard in %d seconds\n",
272 	       CNEE_DEMO_DELAYED_START);
273 
274       /* start up the action set during parsing the commnd line */
275       ret = xnee_start(xd);
276     }
277 
278   if ( ret != XNEE_OK)
279     {
280       xnee_print_error (PACKAGE " failed to start\n");
281       xnee_print_error ("   Error number:    %d\n", ret);
282       xnee_print_error ("   Error string:    '%s'\n",
283 			xnee_get_err_description(ret));
284       xnee_print_error ("   Solution string: '%s'\n",
285 			xnee_get_err_solution(ret));
286       exit(ret);
287     }
288   else
289     {
290       fprintf (stderr, "Reording went well....\n");
291     }
292 
293   ret = xnee_renew_xnee_data(xd);
294   XNEE_RETURN_IF_ERR(ret);
295 
296   ret = xnee_set_data_name (xd, file);
297   XNEE_RETURN_IF_ERR(ret);
298 
299   ret = xnee_set_replayer (xd);
300   XNEE_RETURN_IF_ERR(ret);
301 
302   ret = xnee_prepare(xd);
303   if (ret==XNEE_OK)
304     {
305       fprintf (stderr,
306 	       "Xnee will now replay your recorded "
307 	       "mouse and keyboard events in %d seconds\n",
308 	       CNEE_DEMO_DELAYED_START);
309 
310       /* start up the action set during parsing the commnd line */
311       ret = xnee_start(xd);
312     }
313   if ( ret != XNEE_OK)
314     {
315       xnee_print_error (PACKAGE " failed to start\n");
316       xnee_print_error ("   Error number:    %d\n", ret);
317       xnee_print_error ("   Error string:    '%s'\n",
318 			xnee_get_err_description(ret));
319       xnee_print_error ("   Solution string: '%s'\n",
320 			xnee_get_err_solution(ret));
321       exit(ret);
322     }
323   else
324     {
325       fprintf (stderr, "Replay went well....\n");
326     }
327 
328   fprintf (stderr, "By the way, you can find the recorded file here:\n\t%s\n",
329 	   file);
330   fprintf (stderr, "To replay that file again, type the following command\n");
331 
332   fprintf (stderr,
333 	   "\t"
334 	   XNEE_CLI " --%s --%s %s\n",
335 	   xnee_key2string(xd, xnee_options, XNEE_REPLAY_OPTION_KEY),
336 	   xnee_key2string(xd, cnee_options, CNEE_FILE_OPTION_KEY),
337 	   file);
338 
339   xnee_free (file);
340 
341 
342   /* hey, we are fin(n)ished .... close down */
343   xnee_close_down(xd);
344 
345   /* Since we are here, we can exit gracefully */
346   exit(XNEE_OK);
347 }
348