summaryrefslogtreecommitdiff
path: root/src/video/gtk/SDL_gtkevents.c
blob: 988234d86d8059084ab377012383c1dc26045592 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2006 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

#include "SDL.h"
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"

#include "SDL_gtkvideo.h"
#include "SDL_gtkevents_c.h"

#define DEBUG_GTKPLUS_EVENTS 0
#if DEBUG_GTKPLUS_EVENTS
#define DEBUG_GTKPLUS_SIGNAL() printf("GTK+: %s\n", __FUNCTION__)
#else
#define DEBUG_GTKPLUS_SIGNAL()
#endif

static gboolean
signal_enter_notify(GtkWidget *w, GdkEventCrossing *evt, gpointer data)
{
    SET_THIS_POINTER(data);
    DEBUG_GTKPLUS_SIGNAL();
    if ( (evt->mode != GDK_CROSSING_GRAB) &&
         (evt->mode != GDK_CROSSING_UNGRAB) ) {
        if ( this->input_grab == SDL_GRAB_OFF ) {
            SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
        }
        SDL_PrivateMouseMotion(0, 0, evt->x, evt->y);
	}

    return FALSE;  /* don't eat event, in case app connected a handler. */
}


static gboolean
signal_leave_notify(GtkWidget *w, GdkEventCrossing *evt, gpointer data)
{
    SET_THIS_POINTER(data);
    DEBUG_GTKPLUS_SIGNAL();
    if ( (evt->mode != GDK_CROSSING_GRAB) &&
         (evt->mode != GDK_CROSSING_UNGRAB) &&
         (evt->detail != GDK_NOTIFY_INFERIOR) ) {
        if ( this->input_grab == SDL_GRAB_OFF ) {
            SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
        } else {
            SDL_PrivateMouseMotion(0, 0, evt->x, evt->y);
        }
    }
    return FALSE;  /* don't eat event, in case app connected a handler. */
}


static gboolean
signal_focus_in(GtkWidget *w, GdkEventFocus *evt, gpointer data)
{
    DEBUG_GTKPLUS_SIGNAL();
    SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
    return FALSE;  /* don't eat event, in case app connected a handler. */
}


static gboolean
signal_focus_out(GtkWidget *w, GdkEventFocus *evt, gpointer data)
{
    DEBUG_GTKPLUS_SIGNAL();
    SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
    return FALSE;  /* don't eat event, in case app connected a handler. */
}


#if 0
	    /* Generated upon EnterWindow and FocusIn */
	    case KeymapNotify: {
#ifdef DEBUG_XEVENTS
printf("KeymapNotify!\n");
#endif
		X11_SetKeyboardState(SDL_Display,  xevent.xkeymap.key_vector);
	    }
	    break;
#endif


static gboolean
signal_motion_notify(GtkWidget *w, GdkEventMotion *evt, gpointer data)
{
    SET_THIS_POINTER(data);
    DEBUG_GTKPLUS_SIGNAL();
    if ( SDL_VideoSurface ) {
        if ( 0 /* !!! FIXME mouse_relative */ ) {
            SDLGTK_WarpedMotion(this, evt);
        } else {
            SDL_PrivateMouseMotion(0, 0, evt->x, evt->y);
        }
    }

    return FALSE;  /* don't eat event, in case app connected a handler. */
}


static gboolean
signal_button_press(GtkWidget *w, GdkEventButton *evt, gpointer data)
{
    DEBUG_GTKPLUS_SIGNAL();
    SDL_PrivateMouseButton(SDL_PRESSED, evt->button, 0, 0);
    return FALSE;  /* don't eat event, in case app connected a handler. */
}

static gboolean
signal_button_release(GtkWidget *w, GdkEventButton *evt, gpointer data)
{
    DEBUG_GTKPLUS_SIGNAL();
    SDL_PrivateMouseButton(SDL_RELEASED, evt->button, 0, 0);
    return FALSE;  /* don't eat event, in case app connected a handler. */
}

static gboolean
signal_expose(GtkWidget *w, GdkEventExpose *evt, gpointer data)
{
    SET_THIS_POINTER(data);
    const GdkRectangle *area = &evt->area;
    SDL_Rect r = { area->x, area->y, area->width, area->height };
    DEBUG_GTKPLUS_SIGNAL();
    this->UpdateRects(this, 1, &r);
    return FALSE;  /* don't eat event, in case app connected a handler. */
}


static gboolean
signal_delete(GtkWidget *w, GdkEvent *evt, gpointer data)
{
    DEBUG_GTKPLUS_SIGNAL();
    SDL_PrivateQuit();
    return TRUE;  /* eat event: default handler destroys window! */
}


void GTKPLUS_ConnectSignals(_THIS)
{
    gtk_widget_set_events( this->hidden->gtkdrawingarea,
                           GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK |
                           GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
                           GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
                           GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );

    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkwindow),
                       "delete-event",
                       GTK_SIGNAL_FUNC(signal_delete), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "enter-notify-event",
                       GTK_SIGNAL_FUNC(signal_enter_notify), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "leave-notify-event",
                       GTK_SIGNAL_FUNC(signal_leave_notify), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkwindow),
                       "focus-in-event",
                       GTK_SIGNAL_FUNC(signal_focus_in), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkwindow),
                       "focus-out-event",
                       GTK_SIGNAL_FUNC(signal_focus_out), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "motion-notify-event",
                       GTK_SIGNAL_FUNC(signal_motion_notify), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "button-press-event",
                       GTK_SIGNAL_FUNC(signal_button_press), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "button-release-event",
                       GTK_SIGNAL_FUNC(signal_button_release), this);
    gtk_signal_connect(GTK_OBJECT(this->hidden->gtkdrawingarea),
                       "expose-event", GTK_SIGNAL_FUNC(signal_expose), this);
    /* !!! FIXME: more to connect here. */
}



/* !!! FIXME: more to connect here. */
#if 0
	    /* Key press? */
	    case KeyPress: {
		static SDL_keysym saved_keysym;
		SDL_keysym keysym;
		KeyCode keycode = xevent.xkey.keycode;

#ifdef DEBUG_XEVENTS
printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
		/* Get the translated SDL virtual keysym */
		if ( keycode ) {
			keysym.scancode = keycode;
			keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
			keysym.mod = KMOD_NONE;
			keysym.unicode = 0;
		} else {
			keysym = saved_keysym;
		}

		/* If we're not doing translation, we're done! */
		if ( !SDL_TranslateUNICODE ) {
			posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
			break;
		}

		if ( XFilterEvent(&xevent, None) ) {
			if ( xevent.xkey.keycode ) {
				posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
			} else {
				/* Save event to be associated with IM text
				   In 1.3 we'll have a text event instead.. */
				saved_keysym = keysym;
			}
			break;
		}

		/* Look up the translated value for the key event */
#ifdef X_HAVE_UTF8_STRING
		if ( SDL_IC != NULL ) {
			static Status state;
			/* A UTF-8 character can be at most 6 bytes */
			char keybuf[6];
			if ( Xutf8LookupString(SDL_IC, &xevent.xkey,
			                        keybuf, sizeof(keybuf),
			                        NULL, &state) ) {
				keysym.unicode = Utf8ToUcs4((Uint8*)keybuf);
			}
		}
		else
#endif
		{
			static XComposeStatus state;
			char keybuf[32];

			if ( XLookupString(&xevent.xkey,
			                    keybuf, sizeof(keybuf),
			                    NULL, &state) ) {
				/*
				* FIXME: XLookupString() may yield more than one
				* character, so we need a mechanism to allow for
				* this (perhaps null keypress events with a
				* unicode value)
				*/
				keysym.unicode = (Uint8)keybuf[0];
			}
		}
		posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
	    }
	    break;

	    /* Key release? */
	    case KeyRelease: {
		SDL_keysym keysym;
		KeyCode keycode = xevent.xkey.keycode;

#ifdef DEBUG_XEVENTS
printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
#endif
		/* Check to see if this is a repeated key */
		if ( X11_KeyRepeat(SDL_Display, &xevent) ) {
			break;
		}

		/* Get the translated SDL virtual keysym */
		keysym.scancode = keycode;
		keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
		keysym.mod = KMOD_NONE;
		keysym.unicode = 0;

		posted = SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
	    }
	    break;

	    /* Have we been iconified? */
	    case UnmapNotify: {
#ifdef DEBUG_XEVENTS
printf("UnmapNotify!\n");
#endif
		/* If we're active, make ourselves inactive */
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
			/* Swap out the gamma before we go inactive */
			X11_SwapVidModeGamma(this);

			/* Send an internal deactivate event */
			posted = SDL_PrivateAppActive(0,
					SDL_APPACTIVE|SDL_APPINPUTFOCUS);
		}
	    }
	    break;

	    /* Have we been restored? */
	    case MapNotify: {
#ifdef DEBUG_XEVENTS
printf("MapNotify!\n");
#endif
		/* If we're not active, make ourselves active */
		if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
			/* Send an internal activate event */
			posted = SDL_PrivateAppActive(1, SDL_APPACTIVE);

			/* Now that we're active, swap the gamma back */
			X11_SwapVidModeGamma(this);
		}

		if ( SDL_VideoSurface &&
		     (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
			X11_EnterFullScreen(this);
		} else {
			X11_GrabInputNoLock(this, this->input_grab);
		}
		X11_CheckMouseModeNoLock(this);

		if ( SDL_VideoSurface ) {
			X11_RefreshDisplay(this);
		}
	    }
	    break;

	    /* Have we been resized or moved? */
	    case ConfigureNotify: {
#ifdef DEBUG_XEVENTS
printf("ConfigureNotify! (resize: %dx%d)\n", xevent.xconfigure.width, xevent.xconfigure.height);
#endif
		if ( SDL_VideoSurface ) {
		    if ((xevent.xconfigure.width != SDL_VideoSurface->w) ||
		        (xevent.xconfigure.height != SDL_VideoSurface->h)) {
			/* FIXME: Find a better fix for the bug with KDE 1.2 */
			if ( ! ((xevent.xconfigure.width == 32) &&
			        (xevent.xconfigure.height == 32)) ) {
				SDL_PrivateResize(xevent.xconfigure.width,
				                  xevent.xconfigure.height);
			}
		    } else {
			/* OpenGL windows need to know about the change */
			if ( SDL_VideoSurface->flags & SDL_OPENGL ) {
				SDL_PrivateExpose();
			}
		    }
		}
	    }
	    break;

	    /* Have we been requested to quit (or another client message?) */
	    case ClientMessage: {
		if ( (xevent.xclient.format == 32) &&
		     (xevent.xclient.data.l[0] == WM_DELETE_WINDOW) )
		{
			posted = SDL_PrivateQuit();
		} else
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
			SDL_SysWMmsg wmmsg;

			SDL_VERSION(&wmmsg.version);
			wmmsg.subsystem = SDL_SYSWM_X11;
			wmmsg.event.xevent = xevent;
			posted = SDL_PrivateSysWMEvent(&wmmsg);
		}
	    }
	    break;



	    default: {
#ifdef DEBUG_XEVENTS
printf("Unhandled event %d\n", xevent.type);
#endif
		/* Only post the event if we're watching for it */
		if ( SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE ) {
			SDL_SysWMmsg wmmsg;

			SDL_VERSION(&wmmsg.version);
			wmmsg.subsystem = SDL_SYSWM_X11;
			wmmsg.event.xevent = xevent;
			posted = SDL_PrivateSysWMEvent(&wmmsg);
		}
	    }
	    break;
	}
#endif



void GTKPLUS_PumpEvents(_THIS)
{
    while (gtk_events_pending())
	    gtk_main_iteration();
}

void GTKPLUS_InitOSKeymap(_THIS)
{
    /* !!! FIXME */
	/* do nothing. */
}

/* end of SDL_gtkevents.c ... */