aboutsummaryrefslogtreecommitdiff
path: root/rawhid/rawmouse.c
blob: 07199901cdfd86ee500412eca13ad701af40e369 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright 2003 Hans-Christoph Steiner <hans@eds.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * TODO
 * -obj argument selects device# (event.button.which/event.motion.which)
 *
 * $Id $
 */
static char *version = "$Revision $";

#include <SDL/SDL.h>
#include <m_pd.h>
#include "m_imp.h"

#if PD_MINOR_VERSION >= 37 
#include "s_stuff.h"
#endif

/* #define DEBUG(x) */
#define DEBUG(x) x 

#define RAWMOUSE_AXES     2
#define RAWMOUSE_BUTTONS  9

#define RAWMOUSE_XAXIS    0
#define RAWMOUSE_YAXIS    1

/*------------------------------------------------------------------------------
 *  SDL FUNCTIONS
 */

/* Is the cursor visible? */
static int visible = 1;

void HotKey_ToggleFullScreen(void)
{
	SDL_Surface *screen;

	screen = SDL_GetVideoSurface();
	if ( SDL_WM_ToggleFullScreen(screen) ) {
		printf("Toggled fullscreen mode - now %s\n",
		    (screen->flags&SDL_FULLSCREEN) ? "fullscreen" : "windowed");
	} else {
		printf("Unable to toggle fullscreen mode\n");
	}
}

void HotKey_ToggleGrab(void)
{
	SDL_GrabMode mode;

	printf("Ctrl-G: toggling input grab!\n");
	mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
	if ( mode == SDL_GRAB_ON ) {
		printf("Grab was on\n");
	} else {
		printf("Grab was off\n");
	}
	mode = SDL_WM_GrabInput(mode ? SDL_GRAB_OFF : SDL_GRAB_ON);
	if ( mode == SDL_GRAB_ON ) {
		printf("Grab is now on\n");
	} else {
		printf("Grab is now off\n");
	}
}

void HotKey_Iconify(void)
{
	printf("Ctrl-Z: iconifying window!\n");
	SDL_WM_IconifyWindow();
}

void HotKey_Quit(void)
{
	SDL_Event event;

	printf("Posting internal quit request\n");
	event.type = SDL_USEREVENT;
	SDL_PushEvent(&event);
}

int FilterEvents(const SDL_Event *event)
{
	static int reallyquit = 0;
	
	switch (event->type) {
		
		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			/* We want to toggle visibility on buttonpress */
			if ( event->button.state == SDL_PRESSED ) {
				visible = !visible;
				SDL_ShowCursor(visible);
			}
			printf("Mouse button %d has been %s\n",
				event->button.button,
				(event->button.state == SDL_PRESSED) ?
						"pressed" : "released");
			return(0);

		/* Show relative mouse motion */
		case SDL_MOUSEMOTION:
#if 1
			printf("Mouse relative motion: {%d,%d}\n",
				event->motion.xrel, event->motion.yrel);
#endif
			return(0);

		case SDL_KEYDOWN:
			if ( event->key.keysym.sym == SDLK_ESCAPE ) {
				HotKey_Quit();
			}
			if ( (event->key.keysym.sym == SDLK_g) &&
			     (event->key.keysym.mod & KMOD_CTRL) ) {
				HotKey_ToggleGrab();
			}
			if ( (event->key.keysym.sym == SDLK_z) &&
			     (event->key.keysym.mod & KMOD_CTRL) ) {
				HotKey_Iconify();
			}
			if ( (event->key.keysym.sym == SDLK_RETURN) &&
			     (event->key.keysym.mod & KMOD_ALT) ) {
				HotKey_ToggleFullScreen();
			}
			return(0);

		/* Pass the video resize event through .. */
		case SDL_VIDEORESIZE:
			return(1);

		/* This is important!  Queue it if we want to quit. */
		case SDL_QUIT:
			if ( ! reallyquit ) {
				reallyquit = 1;
				printf("Quit requested\n");
				return(0);
			}
			printf("Quit demanded\n");
			return(1);

		/* This will never happen because events queued directly
		   to the event queue are not filtered.
		 */
		case SDL_USEREVENT:
			return(1);

		/* Drop all other events */
		default:
			return(0);
	}
}



/*------------------------------------------------------------------------------
 *  CLASS DEF
 */
static t_class *rawmouse_class;

typedef struct _rawmouse  {
		t_object            x_obj;
		SDL_Cursor          *x_mouse;
		int                 read_ok;
		int                 started;
		int                 relative;
		t_outlet            *x_axis_out[RAWMOUSE_AXES];
		t_outlet            *x_button_num_out;
		t_outlet            *x_button_val_out;
		t_clock             *x_clock;
		double              x_delaytime;
		int                 x_buttons;
		int                 x_axes;
} t_rawmouse;

/*------------------------------------------------------------------------------
  */

static int rawmouse_close(t_rawmouse *x)  {
    DEBUG(post("rawmouse_CLOSE"));

}

static int rawmouse_open(t_rawmouse *x)  {
  rawmouse_close(x);

  DEBUG(post("rawmouse_OPEN"));

  post ("   device has %i axes and %i buttons.\n",x->x_axes,x->x_buttons);
  post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
  post ("This object is under development!  The interface could change at anytime!");
  post ("As I write cross-platform versions, the interface might have to change.");
  post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
    
  return 1;
}

void rawmouse_start(t_rawmouse* x)
{
  DEBUG(post("rawmouse_START"));

  post("started: %f",x->started);
  if ( ! x->started ) {
/*   SDL_ShowCursor(0); */
	  SDL_WM_GrabInput(SDL_GRAB_ON);
	  
	  clock_delay(x->x_clock, 0);
	  
	  x->started = 1;
  }
}


void rawmouse_stop(t_rawmouse* x)  {
  DEBUG(post("rawmouse_STOP");)

  if ( x->started ) {
/*   SDL_ShowCursor(1); */
	  SDL_WM_GrabInput(SDL_GRAB_OFF);

	  clock_unset(x->x_clock);
	  x->started = 0;
  }
}

static int rawmouse_read(t_rawmouse *x,int fd)  {
  SDL_Event     event; 

  DEBUG(post("rawmouse_READ"));   

  while (SDL_PollEvent(&event)) {
	  post("event type: %s", event.type);
	  switch (event.type) {
		  case SDL_KEYDOWN:
			  post("SDL_KEYDOWN");
			  if ( event.key.keysym.sym == SDLK_ESCAPE )
				  rawmouse_stop(x);
			  break;		  
/* 		  case SDL_MOUSEMOTION: */
/* 			  if (x->relative) { */
/* 				  outlet_float (x->x_axis_out[RAWMOUSE_XAXIS], event.motion.xrel);	 */
/* 				  outlet_float (x->x_axis_out[RAWMOUSE_YAXIS], event.motion.yrel);	 */
/* 			  } else { */
/* 				  outlet_float (x->x_axis_out[RAWMOUSE_XAXIS], event.motion.xrel);	 */
/* 				  outlet_float (x->x_axis_out[RAWMOUSE_YAXIS], event.motion.yrel);	 */
/* 			  } */
/* 			  break; */
/* 		  case SDL_MOUSEBUTTONDOWN: */
/* 			  outlet_float (x->x_button_val_out, 1); */
/* 			  outlet_float (x->x_button_num_out, event.button.button); */
/* 			  break; */
/* 		  case SDL_MOUSEBUTTONUP: */
/* 			  outlet_float (x->x_button_val_out, 0); */
/* 			  outlet_float (x->x_button_num_out, event.button.button); */
/* 			  break; */
		  default:
			  DEBUG(post("Unhandled event."));
	  }
  } 
  
  if (x->started) 
	  clock_delay(x->x_clock, x->x_delaytime);

  return NULL;    
}

/* Actions */

static void rawmouse_bang(t_rawmouse* x)  {
    DEBUG(post("rawmouse_bang"));   
}

static void rawmouse_float(t_rawmouse* x)  {
    DEBUG(post("rawmouse_float"));   
}

void rawmouse_delay(t_rawmouse* x, t_float f)  {
	DEBUG(post("rawmouse_DELAY %f",f);)
	  
   x->x_delaytime = f;
}
void rawmouse_absolute(t_rawmouse* x)  {
	DEBUG(post("rawmouse_ABSOLUTE"));
	  
   x->relative = 0;
}
void rawmouse_relative(t_rawmouse* x)  {
	DEBUG(post("rawmouse_RELATIVE"));
	  
   x->relative = 1;
}


/* SETUP FUNCTIONS */

static void rawmouse_free(t_rawmouse* x) {
  DEBUG(post("rawmouse_free"));
    
  rawmouse_stop(x);
  
  SDL_Quit();

  clock_free(x->x_clock);
}

static void *rawmouse_new(t_float argument) {
  int i;
  t_rawmouse *x = (t_rawmouse *)pd_new(rawmouse_class);

  DEBUG(post("rawmouse_NEW"));
  post("rawHID(e) rawmouse  %s, <hans@eds.org>", version);

  /* init vars */
  x->read_ok = 1;
  x->started = 0;
  x->relative = 1;

  x->x_delaytime = 5;

  x->x_clock = clock_new(x, (t_method)rawmouse_read);

  /* Note: Video is required to start Event Loop !! */
  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 
	  post("Could not initialize SDL: %s\n", SDL_GetError());
	  // exit(-1);
	  return (0);	/* changed by olafmatt */
  }    
  atexit(SDL_Quit);
  
  SDL_WM_SetCaption(title, "rawmouse");
  
  /* create outlets for each axis */
  for (i = 0; i < RAWMOUSE_AXES; i++) 
    x->x_axis_out[i] = outlet_new(&x->x_obj, &s_float);
  
  /* create outlets for buttons */
  x->x_button_num_out = outlet_new(&x->x_obj, &s_float);
  x->x_button_val_out = outlet_new(&x->x_obj, &s_float);
  
  /* Open the device and save settings */
  if ( ! rawmouse_open(x) ) return x;
  
  return (x);
}


void rawmouse_setup(void) {
  DEBUG(post("rawmouse_setup");)
  rawmouse_class = class_new(gensym("rawmouse"), 
			     (t_newmethod)rawmouse_new, 
			     (t_method)rawmouse_free,
			     sizeof(t_rawmouse),0,A_DEFFLOAT,0);

  /* add inlet datatype methods */
  class_addfloat(rawmouse_class,(t_method) rawmouse_float);
  class_addbang(rawmouse_class,(t_method) rawmouse_bang);

  /* add inlet message methods */
  class_addmethod(rawmouse_class,(t_method) rawmouse_open,gensym("open"),0);
  class_addmethod(rawmouse_class,(t_method) rawmouse_close,gensym("close"),0);

  class_addmethod(rawmouse_class,(t_method) rawmouse_start,gensym("start"),0);
  class_addmethod(rawmouse_class,(t_method) rawmouse_stop,gensym("stop"),0);

  class_addmethod(rawmouse_class,(t_method) rawmouse_read,gensym("read"),0);

  class_addmethod(rawmouse_class,(t_method) rawmouse_delay,gensym("delay"),A_FLOAT,0);

  class_addmethod(rawmouse_class,(t_method) rawmouse_absolute,gensym("absolute"),0);
  class_addmethod(rawmouse_class,(t_method) rawmouse_relative,gensym("relative"),0);
}