summaryrefslogtreecommitdiff
path: root/Source/DiVine/examples/spooky.c
blob: 62a417ff0d325f0c17d2d68b12ab010947a45d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <math.h>

#include <directfb.h>
#include <directfb_keynames.h>

#include <divine.h>


static const DirectFBKeySymbolNames(m_symbols);


static IDiVine *m_divine;
static int     m_x;
static int     m_y;

static int     m_sin16_16[360 * 16];


static inline int
sin_16_16( int deg16_16 )
{
     return m_sin16_16[ (deg16_16 >> 12) % (360 * 16) ];
}

static inline int
cos_16_16( int deg16_16 )
{
     return m_sin16_16[ ((deg16_16 + 90 * 65536) >> 12) % (360 * 16) ];
}


typedef void (*HandlerProcess)( FILE *stream );

typedef struct {
     HandlerProcess process;
} Handler;


static void
process_button( FILE *stream )
{
     DFBInputEvent event;

     event.flags = DIEF_NONE;

     if (fgetc(stream) == '+')
          event.type = DIET_BUTTONPRESS;
     else
          event.type = DIET_BUTTONRELEASE;

     event.button = fgetc(stream) - '0';

     m_divine->SendEvent( m_divine, &event );
}

static Handler m_button = {
     .process = process_button
};


static void
process_motion( FILE *stream )
{
     DFBInputEvent event;

     event.flags = DIEF_AXISABS;
     event.type  = DIET_AXISMOTION;
     event.axis  = fgetc(stream) - 'x';

     if (fscanf( stream, "%d", &event.axisabs ) != 1)
          return;

     switch (event.axis) {
          case DIAI_X:
               m_x = event.axisabs;
               break;

          case DIAI_Y:
               m_y = event.axisabs;
               break;

          default:
               break;
     }

     m_divine->SendEvent( m_divine, &event );
}

static Handler m_motion = {
     .process = process_motion
};


static void
process_motion_special( FILE *stream )
{
     int           i, ms, ex, ey, _x = m_x, _y = m_y;
     int           count = 1;
     int           step  = 36;
     DFBInputEvent event;

     if (fscanf( stream, "%d", &ms ) != 1)
          return;

     while (!feof(stream)) {
          switch (fgetc(stream)) {
               case 'c':
                    if (fscanf( stream, "%d", &count ) != 1)
                         return;
                    break;

               case 's':
                    if (fscanf( stream, "%d", &step ) != 1)
                         return;
                    break;

               case 'o':
                    if (fscanf( stream, "%d", &ex ) != 1)
                         return;

                    for (i=0; i<=360*count; i+=step) {
                         event.flags = DIEF_AXISABS | DIEF_FOLLOW;
                         event.type  = DIET_AXISMOTION;
                         event.axis  = DIAI_X;

                         event.axisabs = _x = m_x + ((ex * cos_16_16( (i % 360) << 16 )) >> 16) - ex;

                         m_divine->SendEvent( m_divine, &event );


                         event.flags = DIEF_AXISABS;
                         event.type  = DIET_AXISMOTION;
                         event.axis  = DIAI_Y;

                         event.axisabs = _y = m_y + ((ex * sin_16_16( (i % 360) << 16 )) >> 16);

                         m_divine->SendEvent( m_divine, &event );


                         usleep( ms * 1000 );
                    }

                    m_x = _x;
                    m_y = _y;
                    break;

               case 'l':
                    if (fscanf( stream, "%d,%d", &ex, &ey ) != 2)
                         return;

                    for (i=0; i<count; i++) {
                         event.flags = DIEF_AXISABS | DIEF_FOLLOW;
                         event.type  = DIET_AXISMOTION;
                         event.axis  = DIAI_X;

                         event.axisabs = _x = m_x + ex * i;

                         m_divine->SendEvent( m_divine, &event );


                         event.flags = DIEF_AXISABS;
                         event.type  = DIET_AXISMOTION;
                         event.axis  = DIAI_Y;

                         event.axisabs = _y = m_y + ey * i;

                         m_divine->SendEvent( m_divine, &event );


                         usleep( ms * 1000 );
                    }

                    m_x = _x;
                    m_y = _y;
                    break;

               default:
                    return;
          }
     }
}

static Handler m_motion_special = {
     .process = process_motion_special
};


static void
process_pause( FILE *stream )
{
     int ms = 0;

     fscanf( stream, "%d", &ms );

     usleep( ms * 1000 );
}

static Handler m_pause = {
     .process = process_pause
};


static void
process_text( FILE *stream )
{
     int  ms      = 0;
     bool leading = true;

     fscanf( stream, "%d", &ms );

     while (!feof( stream )) {
          int c = fgetc( stream );

          if (c == ' ' && leading)
               continue;

          if (c == '\n')
               break;

          m_divine->SendSymbol( m_divine, c );

          leading = false;

          usleep( ms * 1000 );
     }
}

static Handler m_text = {
     .process = process_text
};


static void
process_text_special( FILE *stream )
{
     int  i, ms = 0;
     char buf[31];

     fscanf( stream, "%d", &ms );

     while (!feof( stream )) {
          switch (fgetc( stream )) {
               case '\n':
                    return;

               case 'b':
                    m_divine->SendSymbol( m_divine, DIKS_BACKSPACE );
                    break;

               case 'd':
                    m_divine->SendSymbol( m_divine, DIKS_DELETE );
                    break;

               case 'r':
                    m_divine->SendSymbol( m_divine, DIKS_RETURN );
                    break;

               case 'C':
                    switch (fgetc( stream )) {
                         case 'l':
                              m_divine->SendSymbol( m_divine, DIKS_CURSOR_LEFT );
                              break;

                         case 'r':
                              m_divine->SendSymbol( m_divine, DIKS_CURSOR_RIGHT );
                              break;

                         case 'u':
                              m_divine->SendSymbol( m_divine, DIKS_CURSOR_UP );
                              break;

                         case 'd':
                              m_divine->SendSymbol( m_divine, DIKS_CURSOR_DOWN );
                              break;
                    }
                    break;

               case 'S':
                    fscanf( stream, "%30s", buf );

                    for (i=0; m_symbols[i].symbol != DIKS_NULL; i++) {
                         if (!strcasecmp( buf, m_symbols[i].name )) {
                              m_divine->SendSymbol( m_divine, m_symbols[i].symbol );
                              break;
                         }
                    }
                    break;
          }

          usleep( ms * 1000 );
     }
}

static Handler m_text_special = {
     .process = process_text_special
};


static Handler *m_handlers[] = {
     ['b'] = &m_button,
     ['m'] = &m_motion,
     ['M'] = &m_motion_special,
     ['p'] = &m_pause,
     ['t'] = &m_text,
     ['T'] = &m_text_special
};


int
main( int argc, char *argv[] )
{
     DFBResult  ret;
     int   c;
     FILE *stream;

     DiVineInit( &argc, &argv );

     if (argc > 1) {
          if (!strcmp( argv[1], "--help" ) || argc > 2) {
               fprintf( stderr, "Spooky - DiVine event generator\n"
                                "\n"
                                "     Usage: spooky [filename]\n"
                                "\n"
                                "Reads from stdin unless filename is given."
                                "\n"
                                "Commands in stream:\n"
                                " Code  Description                Arguments & Options        Examples\n"
                                "  b    Press/release button       +/-  <button>              b+0 b-0\n"
                                "  m    Motion event (absolute)    <axis> <value>             mx23 my42\n"
                                "  p    Pause spooky               <milliseconds>             p20 p1000\n"
                                "  M    Special motion generator   <ms-between-events> {o|l}  M20o50\n"
                                "                                  c <count>                  M20c1000o50\n"
                                "               (linear)           l <x,y>                    M20c1000l10,20\n"
                                "               (circular)         o <radius>                 M20c1000o100\n"
                                "                                  s <step>                   M20c1000s30o50\n"
                                "  t    Entering text              <ms> <Text to enter>       t0 directfb.org\n"
                                "  T    Special characters/keys    <ms> {b|d|r|Cx|S<symbol>}  T3 CuClbbr Sprint Sf11\n"
                                "                                        a e e Cl (left)\n"
                                "                                        c l t Cr (right)\n"
                                "                                        k     Cu (up)\n"
                                "                                              Cd (down)\n"
                                "\n" );

               return -1;
          }

          stream = fopen( argv[1], "r" );
          if (!stream) {
               fprintf( stderr, "Spooky: Could not open '%s' for reading! (%s)\n", argv[1], strerror(errno) );
               return -2;
          }
     }
     else
          stream = stdin;

     /* open the connection to the input driver */
     ret = DiVineCreate( &m_divine );
     if (ret) {
          D_DERROR( ret, "DiVineCreate() failed!\n" );
          return ret;
     }

     /* Init 1/16th step sin table */
     for (c=0; c<360 * 16; c++)
          m_sin16_16[c] = (int)(sin( (double) c / 16.0 * M_PI / 180.0 ) * 65536.0);

     /* Main loop */
     while (!feof(stream) && (c = fgetc(stream)) != '$') {
          if (c < sizeof(m_handlers)/sizeof(m_handlers[0]) && m_handlers[c])
               m_handlers[c]->process( stream );
     }

     /* close the connection */
     m_divine->Release( m_divine );

     if (stream != stdin)
          fclose( stream );

     return 0;
}