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

« back to all changes in this revision

Viewing changes to .pc/fix-warnings.patch/utils/ffmvforce.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
 * Tests the force feedback driver
 
3
 * Opens a window. When the user clicks in the window, a force effect
 
4
 * is generated according to the position of the mouse.
 
5
 * This program needs the SDL library (http://www.libsdl.org)
 
6
 * Copyright 2001 Johann Deneux <deneux@ifrance.com>
 
7
 */
 
8
 
 
9
/*
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
23
 *
 
24
 * You can contact the author by email at this address:
 
25
 * Johann Deneux <deneux@ifrance.com>
 
26
 */
 
27
 
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <sys/stat.h>
 
32
#include <sys/ioctl.h>
 
33
#include <fcntl.h>
 
34
#include <unistd.h>
 
35
#include <math.h>
 
36
#include <linux/input.h>
 
37
#include <SDL.h>
 
38
 
 
39
#define BIT(x) (1<<(x))
 
40
#define STR_LEN 64
 
41
#define WIN_W   400
 
42
#define WIN_H   400
 
43
#define max(a,b)        ((a)>(b)?(a):(b))
 
44
 
 
45
/* File descriptor of the force feedback /dev entry */
 
46
static int ff_fd;
 
47
static struct ff_effect effect;
 
48
 
 
49
static void welcome()
 
50
{
 
51
        const char* txt[] = {
 
52
"ffmvforce: test orientation of forces",
 
53
"Click in the window to generate a force whose direction will be the position",
 
54
"of your mouse relatively to the center of the window",
 
55
"USE WITH CARE !!! HOLD STRONGLY YOUR WHEEL OR JOYSTICK TO PREVENT DAMAGES",
 
56
"To run this program, run it with at least one argument.",
 
57
"",
 
58
NULL };
 
59
 
 
60
        const char** p = txt;
 
61
 
 
62
        while (*p) {
 
63
                printf("%s\n", *p);
 
64
                p++;
 
65
        }
 
66
}
 
67
 
 
68
static void generate_force(int x, int y)
 
69
{
 
70
        static int first = 1;
 
71
        double nx, ny;
 
72
        double angle;
 
73
 
 
74
        nx = 2*(x-WIN_W/2.0)/WIN_W;
 
75
        ny = 2*(y-WIN_H/2.0)/WIN_H;
 
76
        angle = atan2(nx, -ny);
 
77
printf("mouse: %d %d n: %4.2f %4.2f angle: %4.2f\n", x, y, nx, ny, angle);
 
78
        effect.type = FF_CONSTANT;
 
79
        effect.u.constant.level = 0x7fff * max(fabs(nx), fabs(ny));
 
80
        effect.direction = 0x8000 * (angle + M_PI)/M_PI;
 
81
printf("level: %04x direction: %04x\n", (unsigned int)effect.u.constant.level, (unsigned int)effect.direction);
 
82
        effect.u.constant.envelope.attack_length = 0;
 
83
        effect.u.constant.envelope.attack_level = 0;
 
84
        effect.u.constant.envelope.fade_length = 0;
 
85
        effect.u.constant.envelope.fade_level = 0;
 
86
        effect.trigger.button = 0;
 
87
        effect.trigger.interval = 0;
 
88
        effect.replay.length = 0xffff;
 
89
        effect.replay.delay = 0;
 
90
 
 
91
        if (first) {
 
92
                effect.id = -1;
 
93
        }
 
94
 
 
95
        if (ioctl(ff_fd, EVIOCSFF, &effect) < 0) {
 
96
/* If updates are sent to frequently, they can be refused */
 
97
        }
 
98
 
 
99
        /* If first time, start to play the effect */
 
100
        if (first) {
 
101
                struct input_event play;
 
102
                play.type = EV_FF;
 
103
                play.code = effect.id;
 
104
                play.value = 1;
 
105
 
 
106
                if (write(ff_fd, (const void*) &play, sizeof(play)) == -1) {
 
107
                        perror("Play effect");
 
108
                        exit(1);
 
109
                }
 
110
        }
 
111
 
 
112
        first = 0;
 
113
}
 
114
 
 
115
int main(int argc, char** argv)
 
116
{
 
117
        SDL_Surface* screen;
 
118
        char dev_name[STR_LEN];
 
119
        int i;
 
120
        Uint32 ticks, period = 200;
 
121
 
 
122
        welcome();
 
123
        if (argc <= 1) return 0;
 
124
 
 
125
        /* Parse parameters */
 
126
        strcpy(dev_name, "/dev/input/event0");
 
127
        for (i=1; i<argc; ++i) {
 
128
                if (strcmp(argv[i], "--help") == 0) {
 
129
                        printf("Usage: %s /dev/input/eventXX [-u update frequency in HZ]\n", argv[0]);
 
130
                        printf("Generates constant force effects depending on the position of the mouse\n");
 
131
                        exit(1);
 
132
                }
 
133
                else if (strcmp(argv[i], "-u") == 0) {
 
134
                        if (++i >= argc) {
 
135
                                fprintf(stderr, "Missing update frequency\n");
 
136
                                exit(1);
 
137
                        }
 
138
                        period = 1000.0/atof(argv[i]);
 
139
                }
 
140
                else {
 
141
                        strncpy(dev_name, argv[i], STR_LEN);
 
142
                }
 
143
        }
 
144
 
 
145
        /* Initialize SDL */
 
146
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
 
147
                fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
 
148
                exit(1);
 
149
        }
 
150
        on_exit(SDL_Quit, NULL);
 
151
        screen = SDL_SetVideoMode(WIN_W, WIN_H, 0, SDL_SWSURFACE);
 
152
        if (screen == NULL) {
 
153
                fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError());
 
154
                exit(1);
 
155
        }
 
156
                
 
157
        /* Open force feedback device */
 
158
        ff_fd = open(dev_name, O_RDWR);
 
159
        if (ff_fd == -1) {
 
160
                perror("Open device file");
 
161
                exit(1);
 
162
        }
 
163
 
 
164
        ticks = SDL_GetTicks();
 
165
        /* Main loop */
 
166
        for (;;) {
 
167
                SDL_Event event;
 
168
                SDL_WaitEvent(&event);
 
169
 
 
170
                switch (event.type) {
 
171
                case SDL_QUIT:
 
172
                        exit(0);
 
173
                        break;
 
174
 
 
175
                case SDL_MOUSEMOTION:
 
176
                        if (event.motion.state && SDL_GetTicks()-ticks > period) {
 
177
                                ticks = SDL_GetTicks();
 
178
                                generate_force(event.motion.x, event.motion.y);
 
179
                        }
 
180
                        
 
181
                        break;
 
182
                }
 
183
        }
 
184
 
 
185
        return 0;
 
186
}