summaryrefslogtreecommitdiff
path: root/Source/DirectFB/tools/dfbinput.c
blob: fa8112dc1f12eb6e2286db78eb1178af6cb121bc (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
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This file is subject to the terms and conditions of the MIT License:

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <direct/messages.h>
#include <direct/util.h>

#include <core/input.h>

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


/**************************************************************************************************/

static const DirectFBKeySymbolNames( symbol_names );

static const char *
symbol_name( DFBInputDeviceKeySymbol symbol )
{
     int i;
     static char buf[64];

     for (i=0; i<D_ARRAY_SIZE(symbol_names); i++) {
          if (symbol_names[i].symbol == symbol)
               return symbol_names[i].name;
     }

     snprintf( buf, sizeof(buf), "<0x%08x>", symbol );

     return buf;
}

/**************************************************************************************************/

static IDirectFB                 *dfb;
static IDirectFBInputDevice      *device;
static DFBInputDeviceDescription  desc;

/**************************************************************************************************/

static DFBInputDeviceID id     = DIDID_KEYBOARD;
static unsigned int     reload = false;
static unsigned int     dump   = false;
static unsigned int     sensitivity = 0;

/**************************************************************************************************/

static bool parse_command_line( int argc, char *argv[] );

/**************************************************************************************************/

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

     /* Initialize DirectFB including command line parsing. */
     ret = DirectFBInit( &argc, &argv );
     if (ret) {
          D_DERROR( ret, "Tools/Input: DirectFBInit() failed!\n" );
          goto error;
     }

     /* Parse the command line. */
     if (!parse_command_line( argc, argv ))
          goto error;

     /* Create the super interface. */
     ret = DirectFBCreate( &dfb );
     if (ret) {
          D_DERROR( ret, "Tools/Input: DirectFBCreate() failed!\n" );
          goto error;
     }

     dfb->RescanInputDevices( dfb );

     /* Get the input device. */
     ret = dfb->GetInputDevice( dfb, id, &device );
     if (ret) {
          if (ret == DFB_IDNOTFOUND)
               fprintf (stderr, "\nUnknown device id, check 'dfbinfo' for valid values.\n\n");
          else
               D_DERROR( ret, "Tools/Input: IDirectFB::GetInputDevice() failed!\n" );

          goto error;
     }

     if (sensitivity)
          device->SetSensitivity( device, sensitivity );

     /* Get a description of the device. */
     ret = device->GetDescription( device, &desc );
     if (ret) {
          D_DERROR( ret, "Tools/Input: IDirectFBInputDevice::GetDescription() failed!\n" );
          goto error;
     }

     /* Reload the keymap. FIXME: Make public API? */
     if (reload) {
          ret = dfb_input_device_reload_keymap( dfb_input_device_at( id ) );
          if (ret) {
               D_DERROR( ret, "Tools/Input: Reloading the keymap failed!\n" );
               goto error;
          }
     }

     /* Dump the keymap. */
     if (dump) {
          int i;

          printf( "\n" );

          for (i=desc.min_keycode; i<=desc.max_keycode; i++) {
               DFBInputDeviceKeymapEntry entry;

               ret = device->GetKeymapEntry( device, i, &entry );
               if (ret) {
                    D_DERROR( ret, "Tools/Input: IDirectFBInputDevice::GetKeymapEntry( %d ) failed!\n", i );
                    goto error;
               }

               printf( "%3d:  %-16s  %-16s  %-16s  %-16s\n", i,
                       symbol_name(entry.symbols[DIKSI_BASE]),
                       symbol_name(entry.symbols[DIKSI_BASE_SHIFT]),
                       symbol_name(entry.symbols[DIKSI_ALT]),
                       symbol_name(entry.symbols[DIKSI_ALT_SHIFT]) );
          }

          printf( "\n" );
     }

error:
     /* Release the device. */
     if (device)
          device->Release( device );

     /* Release the super interface. */
     if (dfb)
          dfb->Release( dfb );

     return ret;
}

/**************************************************************************************************/

typedef struct __AnyOption AnyOption;


typedef bool (*ParseFunc)( const AnyOption *option,
                           const char      *arg );

struct __AnyOption {
     const char   *short_name;
     const char   *long_name;

     const char   *arg_name;
     const char   *arg_desc;

     void         *value;

     unsigned int *flags;
     unsigned int  flag;

     ParseFunc     parse;
     const void   *data;
};

typedef struct {
     int           value;
     const char   *name;
} ValueName;

/**************************************************************************************************/

static bool
parse_int( const AnyOption *option, const char *arg )
{
     int   ret;
     char *end;

     ret = strtoul( arg, &end, option->data ? (unsigned long) option->data : 10 );

     if (*end || ret < 0) {
          fprintf( stderr, "\nInvalid argument to '%s' or '%s' specified!\n\n",
                   option->short_name, option->long_name );

          return false;
     }

     *((int*)option->value) = ret;

     return true;
}

/**************************************************************************************************/

static const AnyOption options[] = {
     { "-d",  "--device",       "<id>",       "ID of device to use",
       &id,      NULL, 0, parse_int, NULL },

     { "-r",  "--reload",       "",           "Reload the keymap",
       NULL,     &reload, true, NULL, NULL },

     { "-s",  "--sensitivity",  "<value>",    "Set sensitivity of axes (16.16 fixed)",
       &sensitivity,     NULL, 0, parse_int, NULL },

     { "-k",  "--keymap",       "",           "Show the keymap",
       NULL,     &dump,   true, NULL, NULL },
};

/**************************************************************************************************/

static void
print_usage (const char *prg_name)
{
     int i;

     fprintf (stderr, "\nDirectFB Input Device Configuration (version %s)\n\n", DIRECTFB_VERSION);
     fprintf (stderr, "Usage: %s [options]\n\n", prg_name);
     fprintf (stderr, "Options:\n");
     fprintf (stderr, "   -h   --help                             Show this help message\n");
     fprintf (stderr, "   -v   --version                          Print version information\n");

     for (i=0; i<D_ARRAY_SIZE(options); i++) {
          const AnyOption *option = &options[i];

          fprintf( stderr, "   %-3s  %-16s   %-12s    %s\n",
                   option->short_name, option->long_name, option->arg_name, option->arg_desc );
     }

     fprintf (stderr, "\n");
}

/**************************************************************************************************/

static bool
parse_option( const AnyOption *option, const char *arg )
{
     if (option->parse && !option->parse( option, arg ))
          return false;

     if (option->flags)
          *option->flags |= option->flag;

     return true;
}

static bool
parse_command_line( int argc, char *argv[] )
{
     int i, n;

     for (n = 1; n < argc; n++) {
          bool        ok  = false;
          const char *arg = argv[n];

          if (strcmp (arg, "-h") == 0 || strcmp (arg, "--help") == 0) {
               print_usage (argv[0]);
               return false;
          }

          if (strcmp (arg, "-v") == 0 || strcmp (arg, "--version") == 0) {
               fprintf (stderr, "dfbinput version %s\n", DIRECTFB_VERSION);
               return false;
          }

          for (i=0; i<D_ARRAY_SIZE(options); i++) {
               const AnyOption *opt = &options[i];

               if (!strcmp (arg, opt->short_name) || !strcmp (arg, opt->long_name)) {
                    if (opt->parse && ++n == argc) {
                         print_usage (argv[0]);
                         return false;
                    }

                    if (!parse_option( opt, argv[n] ))
                         return false;

                    ok = true;

                    break;
               }
          }

          if (!ok) {
               print_usage (argv[0]);
               return false;
          }
     }

     return true;
}