Skip to content

Latest commit

 

History

History
300 lines (254 loc) · 7.54 KB

test_manymouse_sdl.c

File metadata and controls

300 lines (254 loc) · 7.54 KB
 
Jun 27, 2005
Jun 27, 2005
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/
*
Jul 28, 2008
Jul 28, 2008
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>
Jun 27, 2005
Jun 27, 2005
14
#include "manymouse.h"
15
16
17
#include "SDL.h"
#define MAX_MICE 128
Jul 9, 2005
Jul 9, 2005
18
#define SCROLLWHEEL_DISPLAY_TICKS 100
Jul 9, 2005
Jul 9, 2005
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];
Jul 9, 2005
Jul 9, 2005
29
Uint32 buttons;
Jul 9, 2005
Jul 9, 2005
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)
{
Jun 27, 2005
Jun 27, 2005
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];
Jun 27, 2005
Jun 27, 2005
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;
}
Jun 27, 2005
Jun 27, 2005
58
else if (event.type == MANYMOUSE_EVENT_ABSMOTION)
Jul 9, 2005
Jul 9, 2005
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;
}
Jun 27, 2005
Jun 27, 2005
68
else if (event.type == MANYMOUSE_EVENT_BUTTON)
Jul 9, 2005
Jul 9, 2005
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);
}
}
Jun 27, 2005
Jun 27, 2005
79
else if (event.type == MANYMOUSE_EVENT_SCROLL)
Jul 9, 2005
Jul 9, 2005
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();
}
}
Jun 27, 2005
Jun 27, 2005
97
else if (event.type == MANYMOUSE_EVENT_DISCONNECT)
Jul 9, 2005
Jul 9, 2005
98
{
99
mice[event.device].connected = 0;
Jul 9, 2005
Jul 9, 2005
100
}
101
102
103
104
105
106
}
}
static void draw_mouse(SDL_Surface *screen, int idx)
{
Jul 9, 2005
Jul 9, 2005
107
int i;
Jul 9, 2005
Jul 9, 2005
108
Uint32 now = SDL_GetTicks();
Jul 9, 2005
Jul 9, 2005
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);
}
}
Jul 9, 2005
Jul 9, 2005
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++)
Jul 9, 2005
Jul 9, 2005
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)
{
Aug 9, 2012
Aug 9, 2012
190
191
int i;
Jun 27, 2005
Jun 27, 2005
192
available_mice = ManyMouse_Init();
Aug 9, 2012
Aug 9, 2012
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)
Aug 9, 2012
Aug 9, 2012
203
{
204
printf("No mice detected!\n");
Aug 9, 2012
Aug 9, 2012
205
206
207
208
return;
}
for (i = 0; i < available_mice; i++)
Aug 9, 2012
Aug 9, 2012
210
211
212
213
214
215
const char *name = ManyMouse_DeviceName(i);
printf("#%d: %s\n", i, name);
}
if (available_mice > MAX_MICE)
{
Aug 9, 2012
Aug 9, 2012
216
printf("Clamping to first %d mice.\n", available_mice);
Aug 9, 2012
Aug 9, 2012
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",
Jun 27, 2005
Jun 27, 2005
251
"manymouse");
Jun 29, 2005
Jun 29, 2005
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");
Jun 27, 2005
Jun 27, 2005
285
ManyMouse_Quit();
286
287
288
289
290
291
292
293
init_mice();
}
}
}
update_mice(screen->w, screen->h);
draw_mice(screen);
}
Jun 27, 2005
Jun 27, 2005
294
ManyMouse_Quit();
295
296
297
298
SDL_Quit();
return 0;
}
Jun 27, 2005
Jun 27, 2005
299
/* end of test_manymouse_sdl.c ... */