/
test_manymouse_sdl.c
300 lines (254 loc) · 7.54 KB
1
/*
2
* A test file for ManyMouse that visualizes input with the SDL library.
3
4
* Simple Directmedia Layer (SDL) can be found at http://libsdl.org/
*
5
* Please see the file LICENSE.txt in the source's root directory.
6
7
8
9
10
11
12
13
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
14
#include "manymouse.h"
15
16
17
#include "SDL.h"
#define MAX_MICE 128
18
#define SCROLLWHEEL_DISPLAY_TICKS 100
19
20
static int available_mice = 0;
21
22
23
24
25
26
27
28
typedef struct
{
int connected;
int x;
int y;
SDL_Color color;
char name[64];
29
Uint32 buttons;
30
31
32
33
Uint32 scrolluptick;
Uint32 scrolldowntick;
Uint32 scrolllefttick;
Uint32 scrollrighttick;
34
35
36
37
38
39
40
} Mouse;
static Mouse mice[MAX_MICE];
static void update_mice(int screen_w, int screen_h)
{
41
42
ManyMouseEvent event;
while (ManyMouse_PollEvent(&event))
43
44
45
46
47
48
49
{
Mouse *mouse;
if (event.device >= (unsigned int) available_mice)
continue;
mouse = &mice[event.device];
50
if (event.type == MANYMOUSE_EVENT_RELMOTION)
51
52
53
54
55
56
57
{
if (event.item == 0)
mouse->x += event.value;
else if (event.item == 1)
mouse->y += event.value;
}
58
else if (event.type == MANYMOUSE_EVENT_ABSMOTION)
59
60
61
62
63
64
65
66
{
float val = (float) (event.value - event.minval);
float maxval = (float) (event.maxval - event.minval);
if (event.item == 0)
mouse->x = (val / maxval) * screen_w;
else if (event.item == 1)
mouse->y = (val / maxval) * screen_h;
}
67
68
else if (event.type == MANYMOUSE_EVENT_BUTTON)
69
70
71
72
73
74
75
76
77
{
if (event.item < 32)
{
if (event.value)
mouse->buttons |= (1 << event.item);
else
mouse->buttons &= ~(1 << event.item);
}
}
78
79
else if (event.type == MANYMOUSE_EVENT_SCROLL)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
if (event.item == 0)
{
if (event.value < 0)
mouse->scrolldowntick = SDL_GetTicks();
else
mouse->scrolluptick = SDL_GetTicks();
}
else if (event.item == 1)
{
if (event.value < 0)
mouse->scrolllefttick = SDL_GetTicks();
else
mouse->scrollrighttick = SDL_GetTicks();
}
}
96
97
else if (event.type == MANYMOUSE_EVENT_DISCONNECT)
98
{
99
mice[event.device].connected = 0;
100
}
101
102
103
104
105
106
}
}
static void draw_mouse(SDL_Surface *screen, int idx)
{
107
int i;
108
Uint32 now = SDL_GetTicks();
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Mouse *mouse = &mice[idx];
SDL_Rect r = { mouse->x, mouse->y, 10, 10 };
Uint32 color = SDL_MapRGB(screen->format, mouse->color.r,
mouse->color.g, mouse->color.b);
if (mouse->x < 0) mouse->x = 0;
if (mouse->x >= screen->w) mouse->x = screen->w-1;
if (mouse->y < 0) mouse->y = 0;
if (mouse->y >= screen->h) mouse->y = screen->h-1;
SDL_FillRect(screen, &r, color); /* draw a square for mouse position. */
/* now draw some buttons... */
color = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
for (i = 0; i < 32; i++)
{
if (mouse->buttons & (1 << i)) /* pressed? */
{
r.w = 20;
r.x = i * r.w;
r.h = 20;
r.y = screen->h - ((idx+1) * r.h);
SDL_FillRect(screen, &r, color);
}
}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* draw scroll wheels... */
#define DRAW_SCROLLWHEEL(var, item) \
if (var > 0) \
{ \
if ((now - var) > SCROLLWHEEL_DISPLAY_TICKS) \
var = 0; \
else \
{ \
r.w = r.h = 20; \
r.y = idx * r.h; \
r.x = item * r.w; \
SDL_FillRect(screen, &r, color); \
} \
}
DRAW_SCROLLWHEEL(mouse->scrolluptick, 0);
DRAW_SCROLLWHEEL(mouse->scrolldowntick, 1);
DRAW_SCROLLWHEEL(mouse->scrolllefttick, 2);
DRAW_SCROLLWHEEL(mouse->scrollrighttick, 3);
#undef DRAW_SCROLLWHEEL
156
157
158
159
160
161
162
}
static void draw_mice(SDL_Surface *screen)
{
int i;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
for (i = 0; i < available_mice; i++)
163
164
165
166
{
if (mice[i].connected)
draw_mouse(screen, i);
}
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
SDL_Flip(screen);
}
static void initial_setup(int screen_w, int screen_h)
{
int i;
memset(mice, '\0', sizeof (mice));
/* pick some random colors for each mouse. */
for (i = 0; i < MAX_MICE; i++)
{
Mouse *mouse = &mice[i];
mouse->x = screen_w / 2;
mouse->y = screen_h / 2;
mouse->color.r = (int) (255.0*rand()/(RAND_MAX+1.0));
mouse->color.g = (int) (255.0*rand()/(RAND_MAX+1.0));
mouse->color.b = (int) (255.0*rand()/(RAND_MAX+1.0));
}
}
static void init_mice(void)
{
190
191
int i;
192
available_mice = ManyMouse_Init();
193
194
195
196
197
198
199
200
if (available_mice < 0)
{
printf("Error initializing ManyMouse!\n");
return;
}
printf("ManyMouse driver: %s\n", ManyMouse_DriverName());
201
202
if (available_mice == 0)
203
{
204
printf("No mice detected!\n");
205
206
207
208
return;
}
for (i = 0; i < available_mice; i++)
209
{
210
211
212
213
214
215
const char *name = ManyMouse_DeviceName(i);
printf("#%d: %s\n", i, name);
}
if (available_mice > MAX_MICE)
{
216
printf("Clamping to first %d mice.\n", available_mice);
217
218
219
220
221
222
223
224
225
available_mice = MAX_MICE;
}
for (i = 0; i < available_mice; i++)
{
const char *name = ManyMouse_DeviceName(i);
strncpy(mice[i].name, name, sizeof (mice[i].name));
mice[i].name[sizeof (mice[i].name) - 1] = '\0';
mice[i].connected = 1;
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
}
}
int main(int argc, char **argv)
{
int must_quit = 0;
int cursor = 0;
SDL_Surface *screen;
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "SDL_Init() failed! %s\n", SDL_GetError());
return 1;
}
screen = SDL_SetVideoMode(640, 480, 0, 0);
if (screen == NULL)
{
fprintf(stderr, "SDL_SetVideoMode() failed! %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_WM_SetCaption("Move your mice, R to rescan, G to (un)grab, S to show/hide, ESC to quit",
251
"manymouse");
252
253
/*SDL_WM_GrabInput(SDL_GRAB_ON);*/
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
SDL_ShowCursor(cursor);
initial_setup(screen->w, screen->h);
init_mice();
while (!must_quit)
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
must_quit = 1;
else if (e.type == SDL_KEYDOWN)
{
if (e.key.keysym.sym == SDLK_ESCAPE)
must_quit = 1;
else if (e.key.keysym.sym == SDLK_g)
{
SDL_GrabMode grab = SDL_WM_GrabInput(SDL_GRAB_QUERY);
grab = (grab == SDL_GRAB_ON) ? SDL_GRAB_OFF : SDL_GRAB_ON;
SDL_WM_GrabInput(grab);
}
else if (e.key.keysym.sym == SDLK_s)
{
cursor = (cursor) ? 0 : 1;
SDL_ShowCursor(cursor);
}
else if (e.key.keysym.sym == SDLK_r)
{
printf("\n\nRESCAN!\n\n");
285
ManyMouse_Quit();
286
287
288
289
290
291
292
293
init_mice();
}
}
}
update_mice(screen->w, screen->h);
draw_mice(screen);
}
294
ManyMouse_Quit();
295
296
297
298
SDL_Quit();
return 0;
}
299
/* end of test_manymouse_sdl.c ... */