~ubuntu-branches/ubuntu/quantal/joystick/quantal

« back to all changes in this revision

Viewing changes to .pc/remap-calibration.patch/utils/jscal.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Kitt
  • Date: 2010-05-16 16:11:59 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100516161159-tf754mo83ojh1yqj
Tags: 20051019-11
evtest: flush standard output, thanks Florian Fainelli! Closes:
#581740.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * jscal.c  Version 1.2
 
3
 *
 
4
 * Copyright (c) 1997-199 Vojtech Pavlik
 
5
 *
 
6
 * Sponsored by SuSE
 
7
 */
 
8
 
 
9
/*
 
10
 * This is a joystick calibration program for Linux joystick driver.
 
11
 */
 
12
 
 
13
/*
 
14
 * This program is free software; you can redistribute it and/or modify
 
15
 * it under the terms of the GNU General Public License as published by
 
16
 * the Free Software Foundation; either version 2 of the License, or
 
17
 * (at your option) any later version.
 
18
 *
 
19
 * This program is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU General Public License for more details.
 
23
 *
 
24
 * You should have received a copy of the GNU General Public License
 
25
 * along with this program; if not, write to the Free Software
 
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
27
 *
 
28
 * Should you need to contact me, the author, you can do so either by
 
29
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 
30
 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 
31
 */
 
32
 
 
33
#include <sys/ioctl.h>
 
34
#include <sys/time.h>
 
35
#include <fcntl.h>
 
36
#include <unistd.h>
 
37
#include <stdio.h>
 
38
#include <math.h>
 
39
#include <getopt.h>
 
40
#include <string.h>
 
41
#include <fcntl.h>
 
42
#include <stdlib.h>
 
43
 
 
44
#include <asm/param.h>
 
45
#include <linux/joystick.h>
 
46
 
 
47
#define PIT_HZ 1193180L
 
48
 
 
49
#define NUM_POS 3
 
50
#define MAX_AXES 16
 
51
#define MAX_CORR 1
 
52
 
 
53
const char *pos_name[] = {"minimum", "center", "maximum"};
 
54
const char *corr_name[] = {"none (raw)", "broken line"};
 
55
const char corr_coef_num[] = {0,4};
 
56
 
 
57
struct correction_data {
 
58
        int cmin[NUM_POS];
 
59
        int cmax[NUM_POS];
 
60
};
 
61
 
 
62
int fd;
 
63
struct js_corr corr[MAX_AXES];
 
64
__u8 axmap[ABS_MAX + 1];
 
65
__u16 buttonmap[(KEY_MAX - BTN_MISC + 1)];
 
66
char axes, buttons, fuzz;
 
67
int version;
 
68
struct correction_data corda[MAX_AXES];
 
69
 
 
70
struct js_info {
 
71
        int buttons;
 
72
        int axis[MAX_AXES];
 
73
        } js;
 
74
 
 
75
void print_position(int i, int a)
 
76
{
 
77
        printf("Axis %d: %8d\r", i, a);
 
78
        fflush(stdout);
 
79
 
 
80
}
 
81
 
 
82
int get_time(void)
 
83
{
 
84
        struct timeval tv;
 
85
 
 
86
        gettimeofday(&tv, NULL);
 
87
 
 
88
        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
89
}
 
90
 
 
91
void wait_for_event(int d, struct js_info *s)
 
92
{
 
93
        struct js_event ev;
 
94
        struct timeval tv;
 
95
        char buf;
 
96
        fd_set set;
 
97
 
 
98
        tv.tv_sec = 0;
 
99
        tv.tv_usec = 100000;
 
100
 
 
101
        FD_ZERO(&set);
 
102
        FD_SET(0, &set);
 
103
        FD_SET(d, &set);
 
104
 
 
105
        if (select(d+1, &set, NULL, NULL, &tv)) {
 
106
 
 
107
                if (FD_ISSET(d, &set)) {
 
108
                        if (read(d, &ev, sizeof(struct js_event)) == sizeof(struct js_event))
 
109
                        switch (ev.type & ~JS_EVENT_INIT) {
 
110
                                case JS_EVENT_AXIS:
 
111
                                        s->axis[ev.number] = ev.value; break;
 
112
                                case JS_EVENT_BUTTON:
 
113
                                        s->buttons = (s->buttons & ~(1 << ev.number)) | (ev.value << ev.number);
 
114
                        }
 
115
                }
 
116
 
 
117
                if (FD_ISSET(0, &set)) {
 
118
                        read(0, &buf, 1);
 
119
                        s->buttons |= (1 << 31);
 
120
                }
 
121
 
 
122
        } else {
 
123
                s->buttons &= ~(1 << 31);
 
124
        }
 
125
}
 
126
 
 
127
void putcs(char *s)
 
128
{
 
129
        int i;
 
130
        putchar('\r');
 
131
        for (i = 0; i < 78; i++) putchar(' ');
 
132
        putchar('\r');
 
133
        fputs(s, stdout);
 
134
        fflush(stdout);
 
135
}
 
136
 
 
137
int solve_broken(int *results, struct correction_data inputs)
 
138
{
 
139
        double a, b, c, d;
 
140
 
 
141
        a = inputs.cmin[1];
 
142
        b = inputs.cmax[1];
 
143
        c = 32767.0 / (inputs.cmin[1] - inputs.cmax[0]);
 
144
        d = 32767.0 / (inputs.cmin[2] - inputs.cmax[1]);
 
145
 
 
146
        results[0] = rint(a);
 
147
        results[1] = rint(b);
 
148
        results[2] = rint(c*16384.0);
 
149
        results[3] = rint(d*16384.0);
 
150
 
 
151
        return 1;
 
152
}
 
153
 
 
154
void help(void)
 
155
{
 
156
        putchar('\n');
 
157
        puts("Usage: jscal <device>");
 
158
        putchar('\n');
 
159
        puts("  -c             --calibrate         Calibrate the joystick");
 
160
        puts("  -h             --help              Display this help");
 
161
        puts("  -s <x,y,z...>  --set-correction    Sets correction to specified values");
 
162
        puts("  -t             --test-center       Tests if joystick is corectly calibrated");
 
163
        puts("                                       returns 0 on success, see the jscal");
 
164
        puts("                                       manpage for error values");
 
165
        puts("  -V             --version           Prints the version numbers");
 
166
        puts("  -p             --print-correction  Prints the current settings as a jscal");
 
167
        puts("                                       command line");
 
168
        puts("  -q             --print-mappings    Print the current axis and button");
 
169
  puts("                                       mappings as a jscal command line");
 
170
        puts("  -u <n_of_axes,axmap1,axmap2,...,");
 
171
  puts("      n_of_buttons,btnmap1,btnmap2,");
 
172
  puts("      ...>       --set-mappings      Sets axis and button mappings to the");
 
173
  puts("                                        specified values");
 
174
        putchar('\n');
 
175
}
 
176
 
 
177
void print_info()
 
178
{
 
179
        int i,j;
 
180
 
 
181
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
182
                perror("jscal: error getting axes");
 
183
                exit(1);
 
184
        }
 
185
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
186
                perror("jscal: error getting buttons");
 
187
                exit(1);
 
188
        }
 
189
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
190
                perror("jscal: error getting correction");
 
191
                exit(1);
 
192
        }
 
193
 
 
194
        printf("Joystick has %d axes and %d buttons.\n", axes, buttons);
 
195
        for (i = 0; i < axes; i++) {
 
196
                printf("Correction for axis %d is %s, precision is %d.\n",
 
197
                        i, corr_name[(int)corr[i].type], corr[i].prec);
 
198
                if (corr_coef_num[(int)corr[i].type]) {
 
199
                        printf("Coeficients are:");
 
200
                        for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
 
201
                                printf(" %d", corr[i].coef[j]);
 
202
                                if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
 
203
                        }
 
204
                putchar('\n');
 
205
                }
 
206
        }
 
207
        putchar('\n');
 
208
}
 
209
 
 
210
void calibrate()
 
211
{
 
212
        int i, j, t, b;
 
213
        int axis, pos;
 
214
 
 
215
        for (i=0; i<MAX_AXES; i++) {
 
216
                corr[i].type = JS_CORR_NONE;
 
217
                corr[i].prec = 0;
 
218
        }
 
219
 
 
220
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
221
                perror("jscal: error setting correction");
 
222
                exit(1);
 
223
        }
 
224
 
 
225
        {
 
226
 
 
227
                int i;
 
228
                int amax[MAX_AXES], amin[MAX_AXES];
 
229
 
 
230
                puts("Calibrating precision: wait and don't touch the joystick.");
 
231
 
 
232
                wait_for_event(fd, &js);
 
233
                t = get_time();
 
234
                while (get_time() < t+50) wait_for_event(fd, &js);
 
235
 
 
236
                wait_for_event(fd, &js);
 
237
                t = get_time();
 
238
                for(i=0; i < axes; i++)
 
239
                        amin[i] = amax[i] = js.axis[i];
 
240
 
 
241
                do {
 
242
                        wait_for_event(fd, &js);
 
243
                        for(i=0; i < axes; i++) {
 
244
                                if (amin[i] > js.axis[i]) {
 
245
                                        amin[i] = js.axis[i];
 
246
                                        t = get_time();
 
247
                                }
 
248
                                if (amax[i] < js.axis[i]) {
 
249
                                        amax[i] = js.axis[i];
 
250
                                        t = get_time();
 
251
                                }
 
252
                                printf("Axis %d:%5d,%5d ", i, amin[i], amax[i]);
 
253
                        }
 
254
                        printf("\r");
 
255
                        fflush(stdout);
 
256
                } while (get_time() < t+4000);
 
257
 
 
258
                printf("Done. Precision is:                                             \n");
 
259
 
 
260
                for (i=0; i < axes; i++) {
 
261
                        corr[i].prec = amax[i] - amin[i];
 
262
                        printf("Axis: %d: %5d\n", i, corr[i].prec);
 
263
                }
 
264
 
 
265
                puts("");
 
266
 
 
267
        }
 
268
 
 
269
 
 
270
        b = js.buttons;
 
271
 
 
272
        for (axis = 0; axis < axes; axis++)
 
273
                for (pos = 0; pos < NUM_POS; pos++) {
 
274
                        while(b ^ js.buttons) wait_for_event(fd, &js);
 
275
                        printf("Move axis %d to %s position and push any button.\n", axis, pos_name[pos]);
 
276
 
 
277
                        while (!(b ^ js.buttons)) {
 
278
                                print_position(axis, js.axis[axis]);
 
279
                                wait_for_event(fd, &js);
 
280
                        }
 
281
 
 
282
                        putcs("Hold ... ");
 
283
 
 
284
                        corda[axis].cmin[pos] = js.axis[axis];
 
285
                        corda[axis].cmax[pos] = js.axis[axis];
 
286
 
 
287
                        t = get_time();
 
288
 
 
289
                        while (get_time() < t + 2000 && (b ^ js.buttons)) {
 
290
                                if (js.axis[axis] < corda[axis].cmin[pos]) {
 
291
                                        corda[axis].cmin[pos] = js.axis[axis];
 
292
                                        t = get_time();
 
293
                                }
 
294
                                if (js.axis[axis] > corda[axis].cmax[pos]) {
 
295
                                        corda[axis].cmax[pos] = js.axis[axis];
 
296
                                        t = get_time();
 
297
                                }
 
298
                                wait_for_event(fd, &js);
 
299
                        }
 
300
                        puts("OK.");
 
301
                }
 
302
 
 
303
        puts("");
 
304
 
 
305
        for (j = 0; j < axes; j++) {
 
306
                solve_broken(corr[j].coef, corda[j]);
 
307
                corr[j].type = JS_CORR_BROKEN;
 
308
        }
 
309
 
 
310
        puts("Setting correction to:");
 
311
        for (i = 0; i < axes; i++) {
 
312
                printf("Correction for axis %d: %s, precision: %d.\n",
 
313
                        i, corr_name[(int)corr[i].type], corr[i].prec);
 
314
                if (corr_coef_num[(int)corr[i].type]) {
 
315
                        printf("Coeficients:");
 
316
                        for(j = 0; j < corr_coef_num[(int)corr[i].type]; j++) {
 
317
                                printf(" %d", corr[i].coef[j]);
 
318
                                if (j < corr_coef_num[(int)corr[i].type] - 1) putchar(',');
 
319
                        }
 
320
                putchar('\n');
 
321
                }
 
322
        }
 
323
 
 
324
        putchar('\n');
 
325
 
 
326
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
327
                perror("jscal: error setting correction");
 
328
                exit(1);
 
329
        }
 
330
}
 
331
 
 
332
void print_version()
 
333
{
 
334
        printf("JsCal was compiled for driver version: %d.%d.%d\n", JS_VERSION >> 16,
 
335
                (JS_VERSION >> 8) & 0xff, JS_VERSION & 0xff);
 
336
        printf("Current running driver version: %d.%d.%d\n", version >> 16,
 
337
                (version >> 8) & 0xff, version & 0xff);
 
338
}
 
339
 
 
340
void print_mappings(char *devicename)
 
341
{
 
342
        int i;
 
343
 
 
344
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
345
                perror("jscal: error getting axes");
 
346
                exit(1);
 
347
        }
 
348
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
349
                perror("jscal: error getting buttons");
 
350
                exit(1);
 
351
        }
 
352
        if (ioctl(fd, JSIOCGAXMAP, &axmap)) {
 
353
                perror("jscal: error getting axis map");
 
354
                exit(1);
 
355
        }
 
356
        if (ioctl(fd, JSIOCGBTNMAP, &buttonmap)) {
 
357
                perror("jscal: error getting button map");
 
358
                exit(1);
 
359
        }
 
360
 
 
361
        printf("jscal -u %d", axes);
 
362
        for (i = 0; i < axes; i++)
 
363
  {
 
364
                printf( ",%d", axmap[i]);
 
365
        }
 
366
 
 
367
  printf(",%d", buttons);
 
368
        for (i = 0; i < buttons; i++)
 
369
  {
 
370
                printf( ",%d", buttonmap[i]);
 
371
        }
 
372
 
 
373
        printf(" %s\n",devicename);
 
374
}
 
375
 
 
376
void print_settings(char *devicename)
 
377
{
 
378
        int i,j;
 
379
 
 
380
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
381
                perror("jscal: error getting axes");
 
382
                exit(1);
 
383
        }
 
384
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
385
                perror("jscal: error getting buttons");
 
386
                exit(1);
 
387
        }
 
388
        if (ioctl(fd, JSIOCGCORR, &corr)) {
 
389
                perror("jscal: error getting correction");
 
390
                exit(1);
 
391
        }
 
392
 
 
393
        printf("jscal -s %d", axes);
 
394
        for (i = 0; i < axes; i++) {
 
395
                printf( ",%d,%d", corr[i].type, corr[i].prec);
 
396
                for (j = 0; j < corr_coef_num[(int)corr[i].type]; j++)
 
397
                        printf(",%d", corr[i].coef[j]);
 
398
        }
 
399
        printf(" %s\n",devicename);
 
400
}
 
401
 
 
402
// n axes                      n buttons
 
403
// 10,0,1,2,5,6,16,17,40,41,42:13,288,289,290,291,292,293,294,295,296,297,298,299,300
 
404
void set_mappings(char *p)
 
405
{
 
406
        int i;
 
407
        int axes_on_cl = 0;
 
408
        int btns_on_cl = 0;
 
409
  int axis_mapping = 0;
 
410
  int btn_mapping = 0;
 
411
 
 
412
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
413
                perror("jscal: error getting axes");
 
414
                exit(1);
 
415
        }
 
416
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
417
                perror("jscal: error getting buttons");
 
418
                exit(1);
 
419
        }
 
420
 
 
421
        if (axes > MAX_AXES) axes = MAX_AXES;
 
422
 
 
423
        if (!p) {
 
424
                fprintf(stderr, "jscal: missing argument for --set-mappings\n");
 
425
                exit(1);
 
426
        }
 
427
 
 
428
  //axes
 
429
        sscanf(p, "%d", &axes_on_cl);
 
430
        p = strstr(p, ",");
 
431
 
 
432
        if (axes_on_cl != axes) {
 
433
                fprintf(stderr, "jscal: joystick has %d axes and not %d as specified on command line\n", 
 
434
                        axes, axes_on_cl);
 
435
                exit(1);
 
436
        }
 
437
 
 
438
 
 
439
        for (i = 0; i < axes; i++)
 
440
  {
 
441
                if (!p) {
 
442
                        fprintf(stderr, "jscal: missing mapping for axis %d\n", i);
 
443
                        exit(1);
 
444
                }
 
445
                sscanf(++p, "%d", &axis_mapping);
 
446
                p = strstr(p, ",");
 
447
 
 
448
 
 
449
                if (axis_mapping > ABS_MAX + 1) {
 
450
                        fprintf(stderr, "jscal: invalid axis mapping for axis %d (max is %d)\n", i, ABS_MAX + 1);
 
451
                        exit(1);
 
452
                }
 
453
                axmap[i] = axis_mapping;
 
454
        }
 
455
 
 
456
  //buttons
 
457
        sscanf(++p, "%d", &btns_on_cl);
 
458
        p = strstr(p, ",");
 
459
 
 
460
        if (btns_on_cl != buttons) {
 
461
                fprintf(stderr, "jscal: joystick has %d buttons and not %d as specified on command line\n", 
 
462
                        buttons, btns_on_cl);
 
463
                exit(1);
 
464
        }
 
465
 
 
466
 
 
467
        for (i = 0; i < buttons; i++)
 
468
  {
 
469
                if (!p) {
 
470
                        fprintf(stderr, "jscal: missing mapping for button %d\n", i);
 
471
                        exit(1);
 
472
                }
 
473
                sscanf(++p, "%d", &btn_mapping);
 
474
                p = strstr(p, ",");
 
475
 
 
476
 
 
477
                if (btn_mapping > KEY_MAX) {
 
478
                        fprintf(stderr, "jscal: invalid button mapping for button %d (max is %d)\n", i, KEY_MAX);
 
479
                        exit(1);
 
480
                }
 
481
                if (btn_mapping < BTN_MISC) {
 
482
                        fprintf(stderr, "jscal: invalid button mapping for button %d (min is %d)\n", i, BTN_MISC);
 
483
                        exit(1);
 
484
                }
 
485
                buttonmap[i] = btn_mapping;
 
486
        }
 
487
 
 
488
        if (p) {
 
489
                fprintf(stderr, "jscal: too many values\n");
 
490
                exit(1);
 
491
        }
 
492
        
 
493
        if (ioctl(fd, JSIOCSAXMAP, &axmap)) {
 
494
                perror("jscal: error setting axis map");
 
495
                exit(1);
 
496
        }
 
497
        if (ioctl(fd, JSIOCSBTNMAP, &buttonmap)) {
 
498
                perror("jscal: error setting button map");
 
499
                exit(1);
 
500
        }
 
501
}
 
502
 
 
503
void set_correction(char *p)
 
504
{
 
505
        int i,j;
 
506
        int t = 0;
 
507
 
 
508
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
509
                perror("jscal: error getting axes");
 
510
                exit(1);
 
511
        }
 
512
 
 
513
        if (axes > MAX_AXES) axes = MAX_AXES;
 
514
 
 
515
        if (!p) {
 
516
                fprintf(stderr, "jscal: missing number of axes\n");
 
517
                exit(1);
 
518
        }
 
519
        sscanf(p, "%d", &t);
 
520
        p = strstr(p, ",");
 
521
 
 
522
        if (t != axes) {
 
523
                fprintf(stderr, "jscal: joystick has different number of axes (%d) than specified in command line (%d)\n", 
 
524
                        axes, t);
 
525
                exit(1);
 
526
        }
 
527
 
 
528
 
 
529
        for (i = 0; i < axes; i++) {
 
530
 
 
531
                if (!p) {
 
532
                        fprintf(stderr, "jscal: missing correction type for axis %d\n", i);
 
533
                        exit(1);
 
534
                }
 
535
                sscanf(++p, "%d", &t);
 
536
                p = strstr(p, ",");
 
537
 
 
538
 
 
539
                if (t > MAX_CORR) {
 
540
                        fprintf(stderr, "jscal: unknown correction type for axis %d\n", i);
 
541
                        exit(1);
 
542
                }
 
543
                corr[i].type = t;
 
544
 
 
545
                if (!p) {
 
546
                        fprintf(stderr, "jscal: missing precision for axis %d\n", i);
 
547
                        exit(1);
 
548
                }
 
549
                sscanf(++p, "%d", &t);
 
550
                p = strstr(p, ",");
 
551
 
 
552
                corr[i].prec = t;
 
553
 
 
554
                for(j = 0; j < corr_coef_num[corr[i].type]; j++) {
 
555
                        if (!p) {
 
556
                                fprintf(stderr, "jscal: missing coefficient %d for axis %d\n", j, i);
 
557
                                exit(1);
 
558
                        }
 
559
                        sscanf(++p, "%d", (int*) &corr[i].coef[j]);
 
560
                        p = strstr(p, ",");
 
561
                }
 
562
        }
 
563
 
 
564
        if (p) {
 
565
                fprintf(stderr, "jscal: too many values\n");
 
566
                exit(1);
 
567
        }
 
568
        
 
569
        if (ioctl(fd, JSIOCSCORR, &corr)) {
 
570
                perror("jscal: error setting correction");
 
571
                exit(1);
 
572
        }
 
573
}
 
574
 
 
575
void test_center()
 
576
{
 
577
        int i;
 
578
        struct js_event ev;
 
579
 
 
580
        if (ioctl(fd, JSIOCGAXES, &axes)) {
 
581
                perror("jscal: error getting axes");
 
582
                exit(1);
 
583
        }
 
584
 
 
585
        if (ioctl(fd, JSIOCGBUTTONS, &buttons)) {
 
586
                perror("jscal: error getting buttons");
 
587
                exit(1);
 
588
        }
 
589
 
 
590
        if (fcntl(fd, F_SETFL, O_NONBLOCK)) {
 
591
                perror("jscal: cannot set nonblocking mode");
 
592
                exit(1);
 
593
        }
 
594
 
 
595
        while (read(fd, &ev, sizeof(struct js_event)) == sizeof(struct js_event)) {
 
596
                switch (ev.type & ~JS_EVENT_INIT) {
 
597
                        case JS_EVENT_AXIS:
 
598
                                js.axis[ev.number] = ev.value; break;
 
599
                        case JS_EVENT_BUTTON:
 
600
                                js.buttons = (js.buttons & ~(1 << ev.number)) | (ev.value << ev.number);
 
601
                }
 
602
        }
 
603
 
 
604
        for (i = 0; i < axes; i++) if (js.axis[i]) {
 
605
                fprintf(stderr, "jscal: axes not calibrated\n");
 
606
                exit(2);
 
607
        }
 
608
        if (js.buttons) {
 
609
                fprintf(stderr, "jscal: buttons pressed\n");
 
610
                exit(3);
 
611
        }
 
612
}
 
613
 
 
614
int action = 0;
 
615
 
 
616
int main(int argc, char **argv)
 
617
{
 
618
        int option_index = 0;
 
619
        char *parameter = NULL;
 
620
        int t;
 
621
 
 
622
  // /usr/include/getopt.h
 
623
        static struct option long_options[] =
 
624
        {
 
625
                {"calibrate", no_argument, NULL, 'c'},
 
626
                {"help", no_argument, NULL, 'h'},
 
627
                {"set-correction", required_argument, NULL, 's'},
 
628
                {"set-mappings", required_argument, NULL, 'u'},
 
629
                {"test-center", no_argument, NULL, 't'},
 
630
                {"version", no_argument, NULL, 'V'},
 
631
                {"print-correction", no_argument, NULL, 'p'},
 
632
                {"print-mappings", no_argument, NULL, 'q'},
 
633
    {NULL, no_argument, NULL, 0 }
 
634
        };
 
635
 
 
636
        if (argc == 1) {
 
637
                help();
 
638
                exit(1);
 
639
        }
 
640
 
 
641
        do {
 
642
                t = getopt_long(argc, argv, "chpqu:s:vVt", long_options, &option_index);
 
643
                switch (t) {
 
644
                        case 'p':
 
645
                        case 'q':
 
646
                        case 's':
 
647
                        case 'u':
 
648
                        case 'c':
 
649
                        case 't':
 
650
                        case 'V':
 
651
                                if (action) {
 
652
                                        fprintf(stderr, "jscal: more than one action specified\n");
 
653
                                        exit(1);
 
654
                                } else {
 
655
                                        action = t;
 
656
                                        if ((parameter=optarg)) strcpy(parameter,optarg);
 
657
                                }
 
658
                                break;
 
659
                        case 'h':
 
660
                                help();
 
661
                                exit(0);
 
662
                        case 0:
 
663
                        case EOF:
 
664
                                break;
 
665
                        case ':':
 
666
                                fprintf(stderr, "jscal: missing parameter\n");
 
667
                                exit(1);
 
668
                        case '?':
 
669
                                fprintf(stderr, "jscal: unknown option\n");
 
670
                                exit(1);
 
671
                        default:
 
672
                                fprintf(stderr, "jscal: option parsing error\n");
 
673
                }
 
674
        } while (t != EOF);
 
675
 
 
676
        if (argc != optind + 1) {
 
677
                fprintf(stderr, "jscal: missing devicename\n");
 
678
                exit(1);
 
679
        }
 
680
 
 
681
        if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
 
682
                perror("jscal: can't open joystick device");
 
683
                exit(1);
 
684
        }
 
685
 
 
686
        if (ioctl(fd, JSIOCGVERSION, &version)) {
 
687
                perror("jscal: error getting version");
 
688
                exit(1);
 
689
        }
 
690
        if (version != JS_VERSION) {
 
691
                fprintf(stderr, "jscal: wrong version\n");
 
692
                print_version();
 
693
                exit(1);
 
694
        }
 
695
 
 
696
        switch (action) {
 
697
                case 0:
 
698
                        print_info();
 
699
                        break;
 
700
                case 'c':
 
701
                        print_info();
 
702
                        calibrate();
 
703
                        break;
 
704
                case 'p':
 
705
                        print_settings(argv[argc -1]);
 
706
                        break;
 
707
                case 'q':
 
708
                        print_mappings(argv[argc -1]);
 
709
                        break;
 
710
                case 's':
 
711
                        set_correction(parameter);
 
712
                        break;
 
713
                case 'u':
 
714
                        set_mappings(parameter);
 
715
                        break;
 
716
                case 't':
 
717
                        test_center();
 
718
                        break;
 
719
                case 'V':
 
720
                        print_version();
 
721
                        break;
 
722
                default:
 
723
                        fprintf(stderr, "jscal: this cannot happen\n");
 
724
                        exit(1);
 
725
        }
 
726
 
 
727
        close(fd);
 
728
        return 0;
 
729
}