src/video/x11/SDL_x11events.c
author Jim Grandpre <jim.tla@gmail.com>
Fri, 28 May 2010 01:26:52 -0400
changeset 4642 057e8762d2a1
parent 4640 f068a6dfc858
child 4643 8806b78988f7
permissions -rw-r--r--
Added reading of event* for touch events.
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1951
     4
slouken@1951
     5
    This library is free software; you can redistribute it and/or
slouken@1951
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     7
    License as published by the Free Software Foundation; either
slouken@1951
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
     9
slouken@1951
    10
    This library is distributed in the hope that it will be useful,
slouken@1951
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    13
    Lesser General Public License for more details.
slouken@1951
    14
slouken@1951
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    16
    License along with this library; if not, write to the Free Software
slouken@1951
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    18
slouken@1951
    19
    Sam Lantinga
slouken@1951
    20
    slouken@libsdl.org
slouken@1951
    21
*/
slouken@1951
    22
#include "SDL_config.h"
slouken@1951
    23
slouken@2046
    24
#include <sys/types.h>
slouken@2046
    25
#include <sys/time.h>
slouken@2046
    26
#include <unistd.h>
slouken@2046
    27
slouken@1951
    28
#include "SDL_x11video.h"
slouken@1951
    29
#include "../../events/SDL_events_c.h"
slouken@2940
    30
#include "../../events/SDL_mouse_c.h"
jim@4642
    31
#include "../../events/SDL_touch_c.h"
slouken@1951
    32
slouken@3241
    33
#include "SDL_syswm.h"
slouken@3241
    34
jim@4642
    35
#include <stdio.h>
jim@4642
    36
jim@4642
    37
//Touch Input/event* includes
jim@4642
    38
#include <linux/input.h>
jim@4642
    39
#include <fcntl.h>
jim@4642
    40
slouken@1951
    41
static void
slouken@1951
    42
X11_DispatchEvent(_THIS)
slouken@1951
    43
{
slouken@1951
    44
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    45
    SDL_WindowData *data;
slouken@1951
    46
    XEvent xevent;
slouken@1951
    47
    int i;
slouken@1951
    48
slouken@1951
    49
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@1951
    50
    XNextEvent(videodata->display, &xevent);
slouken@1951
    51
bob@2325
    52
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    53
       handler */
bob@2325
    54
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
    55
#if 0
bob@2328
    56
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
    57
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
    58
#endif
bob@2325
    59
        return;
bob@2325
    60
    }
bob@2325
    61
slouken@1951
    62
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
    63
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
    64
        SDL_SysWMmsg wmmsg;
slouken@1951
    65
slouken@1951
    66
        SDL_VERSION(&wmmsg.version);
slouken@1951
    67
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    68
        wmmsg.event.xevent = xevent;
slouken@1951
    69
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    70
    }
slouken@1951
    71
slouken@1951
    72
    data = NULL;
bob@2324
    73
    if (videodata && videodata->windowlist) {
bob@2324
    74
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
    75
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
    76
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
    77
                data = videodata->windowlist[i];
bob@2324
    78
                break;
bob@2324
    79
            }
slouken@1951
    80
        }
slouken@1951
    81
    }
slouken@1951
    82
    if (!data) {
slouken@1951
    83
        return;
slouken@1951
    84
    }
bob@2327
    85
#if 0
bob@2328
    86
    printf("type = %d display = %d window = %d\n",
bob@2328
    87
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
    88
#endif
slouken@1951
    89
    switch (xevent.type) {
slouken@1951
    90
slouken@1951
    91
        /* Gaining mouse coverage? */
slouken@1951
    92
    case EnterNotify:{
slouken@1951
    93
#ifdef DEBUG_XEVENTS
bob@3195
    94
            printf("EnterNotify! (%d,%d,%d)\n", 
bob@3195
    95
	           xevent.xcrossing.x,
bob@3195
    96
 	           xevent.xcrossing.y,
bob@3195
    97
                   xevent.xcrossing.mode);
slouken@1951
    98
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    99
                printf("Mode: NotifyGrab\n");
slouken@1951
   100
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   101
                printf("Mode: NotifyUngrab\n");
slouken@1951
   102
#endif
bob@3195
   103
#if 1
slouken@3322
   104
            /* FIXME: Should we reset data for all mice? */
slouken@3322
   105
            for (i = 0; i < SDL_GetNumMice(); ++i) {
slouken@3322
   106
                SDL_Mouse *mouse = SDL_GetMouse(i);
slouken@3685
   107
                SDL_SetMouseFocus(mouse->id, data->window);
slouken@3322
   108
            }
slouken@2940
   109
#endif
slouken@1951
   110
        }
slouken@1951
   111
        break;
slouken@1951
   112
slouken@1951
   113
        /* Losing mouse coverage? */
slouken@1951
   114
    case LeaveNotify:{
slouken@1951
   115
#ifdef DEBUG_XEVENTS
bob@3195
   116
            printf("LeaveNotify! (%d,%d,%d)\n", 
bob@3195
   117
	           xevent.xcrossing.x,
bob@3195
   118
 	           xevent.xcrossing.y,
bob@3195
   119
                   xevent.xcrossing.mode);
slouken@1951
   120
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   121
                printf("Mode: NotifyGrab\n");
slouken@1951
   122
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   123
                printf("Mode: NotifyUngrab\n");
slouken@1951
   124
#endif
slouken@3322
   125
            if (xevent.xcrossing.detail != NotifyInferior) {
bob@3195
   126
#if 1
slouken@2940
   127
                /* FIXME: Should we reset data for all mice? */
slouken@3322
   128
	        for (i = 0; i < SDL_GetNumMice(); ++i) {
slouken@3322
   129
		    SDL_Mouse *mouse = SDL_GetMouse(i);
slouken@3322
   130
		    SDL_SetMouseFocus(mouse->id, 0);
slouken@3322
   131
	        }
slouken@2940
   132
#endif
slouken@1951
   133
            }
slouken@1951
   134
        }
slouken@1951
   135
        break;
slouken@1951
   136
slouken@1951
   137
        /* Gaining input focus? */
slouken@1951
   138
    case FocusIn:{
slouken@1951
   139
#ifdef DEBUG_XEVENTS
slouken@1951
   140
            printf("FocusIn!\n");
slouken@1951
   141
#endif
slouken@3685
   142
            SDL_SetKeyboardFocus(videodata->keyboard, data->window);
slouken@1951
   143
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   144
            if (data->ic) {
slouken@1951
   145
                XSetICFocus(data->ic);
slouken@1951
   146
            }
slouken@1951
   147
#endif
slouken@1951
   148
        }
slouken@1951
   149
        break;
slouken@1951
   150
slouken@1951
   151
        /* Losing input focus? */
slouken@1951
   152
    case FocusOut:{
slouken@1951
   153
#ifdef DEBUG_XEVENTS
slouken@1951
   154
            printf("FocusOut!\n");
slouken@1951
   155
#endif
slouken@1951
   156
            SDL_SetKeyboardFocus(videodata->keyboard, 0);
slouken@1951
   157
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   158
            if (data->ic) {
slouken@1951
   159
                XUnsetICFocus(data->ic);
slouken@1951
   160
            }
slouken@1951
   161
#endif
slouken@1951
   162
        }
slouken@1951
   163
        break;
slouken@1951
   164
slouken@1951
   165
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   166
    case KeymapNotify:{
slouken@1951
   167
#ifdef DEBUG_XEVENTS
slouken@1951
   168
            printf("KeymapNotify!\n");
slouken@1951
   169
#endif
slouken@1951
   170
            /* FIXME:
slouken@1951
   171
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   172
             */
slouken@1951
   173
        }
slouken@1951
   174
        break;
slouken@1951
   175
slouken@2305
   176
        /* Has the keyboard layout changed? */
slouken@2305
   177
    case MappingNotify:{
slouken@2305
   178
#ifdef DEBUG_XEVENTS
slouken@2305
   179
            printf("MappingNotify!\n");
slouken@2305
   180
#endif
slouken@2306
   181
            X11_UpdateKeymap(_this);
slouken@2305
   182
        }
slouken@2305
   183
        break;
slouken@2305
   184
slouken@1951
   185
        /* Key press? */
slouken@1951
   186
    case KeyPress:{
slouken@1951
   187
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   188
            KeySym keysym = NoSymbol;
slouken@2306
   189
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   190
            Status status = 0;
slouken@1951
   191
slouken@1951
   192
#ifdef DEBUG_XEVENTS
slouken@1951
   193
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   194
#endif
bob@2299
   195
            SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
slouken@2305
   196
                                videodata->key_layout[keycode]);
bob@2325
   197
#if 0
slouken@2305
   198
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   199
                int min_keycode, max_keycode;
slouken@2305
   200
                XDisplayKeycodes(videodata->display, &min_keycode,
slouken@2305
   201
                                 &max_keycode);
slouken@2305
   202
                keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
bob@2299
   203
                fprintf(stderr,
slouken@2305
   204
                        "The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> X11 KeyCode %d (%d), X11 KeySym 0x%X (%s).\n",
slouken@2305
   205
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   206
                        XKeysymToString(keysym));
bob@2299
   207
            }
bob@2295
   208
#endif
bob@2325
   209
            /* */
slouken@2305
   210
            SDL_zero(text);
bob@2325
   211
#ifdef X_HAVE_UTF8_STRING
bob@2325
   212
            if (data->ic) {
bob@2325
   213
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   214
                                  &keysym, &status);
bob@2325
   215
            }
bob@2325
   216
#else
slouken@2306
   217
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   218
#endif
slouken@2305
   219
            if (*text) {
bob@2300
   220
                SDL_SendKeyboardText(videodata->keyboard, text);
bob@2300
   221
            }
slouken@1951
   222
        }
slouken@1951
   223
        break;
slouken@1951
   224
slouken@1951
   225
        /* Key release? */
slouken@1951
   226
    case KeyRelease:{
slouken@1951
   227
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   228
slouken@1951
   229
#ifdef DEBUG_XEVENTS
slouken@1951
   230
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   231
#endif
bob@2295
   232
            SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
slouken@2305
   233
                                videodata->key_layout[keycode]);
slouken@1951
   234
        }
slouken@1951
   235
        break;
slouken@1951
   236
slouken@1951
   237
        /* Have we been iconified? */
slouken@1951
   238
    case UnmapNotify:{
slouken@1951
   239
#ifdef DEBUG_XEVENTS
slouken@1951
   240
            printf("UnmapNotify!\n");
slouken@1951
   241
#endif
slouken@3685
   242
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   243
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   244
        }
slouken@1951
   245
        break;
slouken@1951
   246
slouken@1951
   247
        /* Have we been restored? */
slouken@1951
   248
    case MapNotify:{
slouken@1951
   249
#ifdef DEBUG_XEVENTS
slouken@1951
   250
            printf("MapNotify!\n");
slouken@1951
   251
#endif
slouken@3685
   252
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   253
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   254
        }
slouken@1951
   255
        break;
slouken@1951
   256
slouken@1951
   257
        /* Have we been resized or moved? */
slouken@1951
   258
    case ConfigureNotify:{
slouken@1951
   259
#ifdef DEBUG_XEVENTS
slouken@1951
   260
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   261
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   262
#endif
slouken@3685
   263
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   264
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   265
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   266
                                xevent.xconfigure.width,
slouken@1951
   267
                                xevent.xconfigure.height);
slouken@1951
   268
        }
slouken@1951
   269
        break;
slouken@1951
   270
slouken@1951
   271
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   272
    case ClientMessage:{
slouken@1951
   273
            if ((xevent.xclient.format == 32) &&
slouken@1951
   274
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   275
slouken@3685
   276
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   277
            }
slouken@1951
   278
        }
slouken@1951
   279
        break;
slouken@1951
   280
slouken@1951
   281
        /* Do we need to refresh ourselves? */
slouken@1951
   282
    case Expose:{
slouken@1951
   283
#ifdef DEBUG_XEVENTS
slouken@1951
   284
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   285
#endif
slouken@3685
   286
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   287
        }
slouken@1951
   288
        break;
slouken@1951
   289
slouken@1951
   290
    default:{
slouken@2942
   291
            for (i = 0; i < SDL_GetNumMice(); ++i) {
slouken@2942
   292
                SDL_Mouse *mouse;
slouken@3003
   293
#if SDL_VIDEO_DRIVER_X11_XINPUT
slouken@2942
   294
                X11_MouseData *data;
slouken@3003
   295
#endif
slouken@2940
   296
slouken@2942
   297
                mouse = SDL_GetMouse(i);
slouken@3003
   298
                if (!mouse->driverdata) {
slouken@2992
   299
                    switch (xevent.type) {
slouken@2992
   300
                    case MotionNotify:
slouken@2992
   301
#ifdef DEBUG_MOTION
slouken@2997
   302
                        printf("X11 motion: %d,%d\n", xevent.xmotion.x,
slouken@2997
   303
                               xevent.xmotion.y);
slouken@2992
   304
#endif
slouken@2997
   305
                        SDL_SendMouseMotion(mouse->id, 0, xevent.xmotion.x,
slouken@2997
   306
                                            xevent.xmotion.y, 0);
slouken@2992
   307
                        break;
slouken@2992
   308
slouken@2992
   309
                    case ButtonPress:
slouken@2997
   310
                        SDL_SendMouseButton(mouse->id, SDL_PRESSED,
slouken@2997
   311
                                            xevent.xbutton.button);
slouken@2992
   312
                        break;
slouken@2992
   313
slouken@2992
   314
                    case ButtonRelease:
slouken@2997
   315
                        SDL_SendMouseButton(mouse->id, SDL_RELEASED,
slouken@2997
   316
                                            xevent.xbutton.button);
slouken@2992
   317
                        break;
slouken@2992
   318
                    }
slouken@2942
   319
                    continue;
slouken@2942
   320
                }
slouken@3003
   321
#if SDL_VIDEO_DRIVER_X11_XINPUT
slouken@3003
   322
                data = (X11_MouseData *) mouse->driverdata;
slouken@3003
   323
                if (xevent.type == data->motion) {
slouken@2942
   324
                    XDeviceMotionEvent *move =
slouken@2942
   325
                        (XDeviceMotionEvent *) & xevent;
slouken@2942
   326
#ifdef DEBUG_MOTION
slouken@2942
   327
                    printf("X11 motion: %d,%d\n", move->x, move->y);
slouken@2942
   328
#endif
slouken@2942
   329
                    SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y,
slouken@2942
   330
                                        move->axis_data[2]);
slouken@2942
   331
                    return;
slouken@2942
   332
                }
slouken@3003
   333
                if (xevent.type == data->button_pressed) {
slouken@2942
   334
                    XDeviceButtonPressedEvent *pressed =
slouken@2942
   335
                        (XDeviceButtonPressedEvent *) & xevent;
slouken@2942
   336
                    SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
slouken@2942
   337
                                        pressed->button);
slouken@2942
   338
                    return;
slouken@2942
   339
                }
slouken@3003
   340
                if (xevent.type == data->button_released) {
slouken@2942
   341
                    XDeviceButtonReleasedEvent *released =
slouken@2942
   342
                        (XDeviceButtonReleasedEvent *) & xevent;
slouken@2942
   343
                    SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
slouken@2942
   344
                                        released->button);
slouken@2942
   345
                    return;
slouken@2942
   346
                }
slouken@2942
   347
                if (xevent.type == data->proximity_in) {
slouken@2942
   348
                    XProximityNotifyEvent *proximity =
slouken@2942
   349
                        (XProximityNotifyEvent *) & xevent;
slouken@2942
   350
                    SDL_SendProximity(proximity->deviceid, proximity->x,
slouken@2942
   351
                                      proximity->y, SDL_PROXIMITYIN);
slouken@2942
   352
                    return;
slouken@2942
   353
                }
slouken@2942
   354
                if (xevent.type == data->proximity_out) {
slouken@2942
   355
                    XProximityNotifyEvent *proximity =
slouken@2942
   356
                        (XProximityNotifyEvent *) & xevent;
slouken@2942
   357
                    SDL_SendProximity(proximity->deviceid, proximity->x,
slouken@2942
   358
                                      proximity->y, SDL_PROXIMITYOUT);
slouken@2942
   359
                    return;
slouken@2942
   360
                }
slouken@3003
   361
#endif
slouken@2940
   362
            }
slouken@1951
   363
#ifdef DEBUG_XEVENTS
slouken@2940
   364
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   365
#endif
slouken@1951
   366
        }
slouken@1951
   367
        break;
slouken@1951
   368
    }
slouken@1951
   369
}
slouken@1951
   370
slouken@1951
   371
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   372
int
slouken@1951
   373
X11_Pending(Display * display)
slouken@1951
   374
{
slouken@1951
   375
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   376
    XFlush(display);
slouken@1951
   377
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   378
        return (1);
slouken@1951
   379
    }
slouken@1951
   380
slouken@1951
   381
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   382
    {
slouken@1951
   383
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   384
        int x11_fd;
slouken@1951
   385
        fd_set fdset;
slouken@1951
   386
slouken@1951
   387
        x11_fd = ConnectionNumber(display);
slouken@1951
   388
        FD_ZERO(&fdset);
slouken@1951
   389
        FD_SET(x11_fd, &fdset);
slouken@1951
   390
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   391
            return (XPending(display));
slouken@1951
   392
        }
slouken@1951
   393
    }
slouken@1951
   394
slouken@1951
   395
    /* Oh well, nothing is ready .. */
slouken@1951
   396
    return (0);
slouken@1951
   397
}
slouken@1951
   398
slouken@1951
   399
void
slouken@1951
   400
X11_PumpEvents(_THIS)
slouken@1951
   401
{
slouken@1951
   402
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   403
slouken@3025
   404
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   405
    if (_this->suspend_screensaver) {
slouken@3025
   406
        Uint32 now = SDL_GetTicks();
slouken@3025
   407
        if (!data->screensaver_activity ||
slouken@3040
   408
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   409
            XResetScreenSaver(data->display);
slouken@3025
   410
            data->screensaver_activity = now;
slouken@3025
   411
        }
slouken@3025
   412
    }
slouken@3025
   413
slouken@1951
   414
    /* Keep processing pending events */
slouken@1951
   415
    while (X11_Pending(data->display)) {
slouken@1951
   416
        X11_DispatchEvent(_this);
slouken@1951
   417
    }
jim@4640
   418
jim@4640
   419
jim@4642
   420
    /* Process Touch events - TODO When X gets touch support, use that instead*/
jim@4642
   421
    int i = 0,rd;
jim@4642
   422
    char * name[256];
jim@4642
   423
    struct input_event ev[64];
jim@4642
   424
    int size = sizeof (struct input_event);
jim@4642
   425
    static int initd = 0; //TODO - HACK!
jim@4642
   426
    for(i = 0;i < SDL_GetNumTouch();++i) {
jim@4642
   427
	SDL_Touch* touch = SDL_GetTouchIndex(i);
jim@4642
   428
	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
jim@4642
   429
	EventTouchData* data;
jim@4642
   430
	if(!initd){//data->eventStream <= 0) {
jim@4642
   431
	    touch->driverdata = SDL_malloc(sizeof(EventTouchData));
jim@4642
   432
	    data = (EventTouchData*)(touch->driverdata);
jim@4642
   433
	    printf("Openning device...\n");
jim@4642
   434
	    data->eventStream = open("/dev/input/wacom-touch", 
jim@4642
   435
				     O_RDONLY | O_NONBLOCK);
jim@4642
   436
	    ioctl (data->eventStream, EVIOCGNAME (sizeof (name)), name);
jim@4642
   437
	    printf ("Reading From : %s\n", name);
jim@4642
   438
	    initd = 1;
jim@4642
   439
	}
jim@4642
   440
	else
jim@4642
   441
	 data = (EventTouchData*)(touch->driverdata);
jim@4642
   442
	if(data->eventStream <= 0) 
jim@4642
   443
	    printf("Error: Couldn't open stream\n");
jim@4642
   444
	rd = read(data->eventStream, ev, size * 64);
jim@4642
   445
	//printf("Got %i/%i bytes\n",rd,size);
jim@4642
   446
	if(rd >= size) {
jim@4642
   447
	    for (i = 0; i < rd / sizeof(struct input_event); i++) {
jim@4642
   448
		switch (ev[i].type) {
jim@4642
   449
		case EV_ABS:
jim@4642
   450
		    //printf("Got position x: %i!\n",data->x);
jim@4642
   451
		    if(ev[i].code == ABS_X)
jim@4642
   452
			data->x = ev[i].value;
jim@4642
   453
		    else if (ev[i].code == ABS_Y)
jim@4642
   454
			data->y = ev[i].value;
jim@4642
   455
		    break;
jim@4642
   456
		case EV_MSC:
jim@4642
   457
		    if(ev[i].code == MSC_SERIAL)
jim@4642
   458
			data->finger = ev[i].value;
jim@4642
   459
		    break;
jim@4642
   460
		case EV_SYN:
jim@4642
   461
		    data->finger -= 1; /*Wacom indexes fingers from 1, 
jim@4642
   462
					 I index from 0*/		  
jim@4642
   463
		    if(data->x >= 0 || data->y >= 0)
jim@4642
   464
			SDL_SendTouchMotion(touch->id,data->finger, 
jim@4642
   465
					    SDL_FALSE,data->x,data->y,
jim@4642
   466
					    data->pressure);
jim@4642
   467
		    
jim@4642
   468
		    //printf("Synched: %i tx: %i, ty: %i\n",
jim@4642
   469
		    //	   data->finger,data->x,data->y);
jim@4642
   470
		    data->x = -1;
jim@4642
   471
		    data->y = -1;
jim@4642
   472
		    data->pressure = -1;
jim@4642
   473
		    
jim@4642
   474
		    break;		
jim@4642
   475
		}
jim@4642
   476
	    }
jim@4642
   477
	}
jim@4642
   478
    }
slouken@1951
   479
}
slouken@1951
   480
slouken@3030
   481
/* This is so wrong it hurts */
slouken@3030
   482
#define GNOME_SCREENSAVER_HACK
slouken@3030
   483
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   484
#include <unistd.h>
slouken@3030
   485
static pid_t screensaver_inhibit_pid;
slouken@3040
   486
static void
slouken@3040
   487
gnome_screensaver_disable()
slouken@3030
   488
{
slouken@3030
   489
    screensaver_inhibit_pid = fork();
slouken@3030
   490
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   491
        close(0);
slouken@3031
   492
        close(1);
slouken@3031
   493
        close(2);
slouken@3030
   494
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   495
              "gnome-screensaver-command",
slouken@3030
   496
              "--inhibit",
slouken@3030
   497
              "--reason",
slouken@3040
   498
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   499
        exit(2);
slouken@3030
   500
    }
slouken@3030
   501
}
slouken@3040
   502
static void
slouken@3040
   503
gnome_screensaver_enable()
slouken@3030
   504
{
slouken@3030
   505
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   506
}
slouken@3030
   507
#endif
slouken@3030
   508
slouken@1951
   509
void
slouken@3025
   510
X11_SuspendScreenSaver(_THIS)
slouken@1951
   511
{
slouken@3025
   512
#if SDL_VIDEO_DRIVER_X11_SCRNSAVER
slouken@3025
   513
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   514
    int dummy;
slouken@3025
   515
    int major_version, minor_version;
slouken@1951
   516
slouken@3030
   517
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   518
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   519
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   520
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   521
                                      &major_version, &minor_version) ||
slouken@3030
   522
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   523
            return;
slouken@3030
   524
        }
slouken@3030
   525
slouken@3030
   526
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   527
        XResetScreenSaver(data->display);
slouken@1951
   528
    }
slouken@3030
   529
#endif
slouken@1951
   530
slouken@3030
   531
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   532
    if (_this->suspend_screensaver) {
slouken@3030
   533
        gnome_screensaver_disable();
slouken@3030
   534
    } else {
slouken@3030
   535
        gnome_screensaver_enable();
slouken@3025
   536
    }
slouken@3025
   537
#endif
slouken@1951
   538
}
slouken@1951
   539
slouken@1951
   540
/* vi: set ts=4 sw=4 expandtab: */