src/video/x11/SDL_x11xinput2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11602 8dd72342f1f1
child 12029 dfebed374d73
permissions -rw-r--r--
Updated copyright for 2018
dimitris@6316
     1
/*
dimitris@6316
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
dimitris@6316
     4
dimitris@6316
     5
  This software is provided 'as-is', without any express or implied
dimitris@6316
     6
  warranty.  In no event will the authors be held liable for any damages
dimitris@6316
     7
  arising from the use of this software.
dimitris@6316
     8
dimitris@6316
     9
  Permission is granted to anyone to use this software for any purpose,
dimitris@6316
    10
  including commercial applications, and to alter it and redistribute it
dimitris@6316
    11
  freely, subject to the following restrictions:
dimitris@6316
    12
dimitris@6316
    13
  1. The origin of this software must not be misrepresented; you must not
dimitris@6316
    14
     claim that you wrote the original software. If you use this software
dimitris@6316
    15
     in a product, an acknowledgment in the product documentation would be
dimitris@6316
    16
     appreciated but is not required.
dimitris@6316
    17
  2. Altered source versions must be plainly marked as such, and must not be
dimitris@6316
    18
     misrepresented as being the original software.
dimitris@6316
    19
  3. This notice may not be removed or altered from any source distribution.
dimitris@6316
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
dimitris@6316
    22
dimitris@6316
    23
#if SDL_VIDEO_DRIVER_X11
dimitris@6316
    24
dimitris@6316
    25
#include "SDL_x11video.h"
dimitris@6316
    26
#include "SDL_x11xinput2.h"
dimitris@6316
    27
#include "../../events/SDL_mouse_c.h"
dimitris@6316
    28
#include "../../events/SDL_touch_c.h"
dimitris@6316
    29
dimitris@6316
    30
#define MAX_AXIS 16
dimitris@6316
    31
icculus@6375
    32
#if SDL_VIDEO_DRIVER_X11_XINPUT2
dimitris@6316
    33
static int xinput2_initialized = 0;
icculus@6375
    34
icculus@6375
    35
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
dimitris@6316
    36
static int xinput2_multitouch_supported = 0;
icculus@6375
    37
#endif
icculus@6375
    38
icculus@7827
    39
/* Opcode returned X11_XQueryExtension
dimitris@6316
    40
 * It will be used in event processing
dimitris@6316
    41
 * to know that the event came from
slouken@7191
    42
 * this extension */
dimitris@6316
    43
static int xinput2_opcode;
dimitris@6316
    44
dimitris@6316
    45
static void parse_valuators(const double *input_values,unsigned char *mask,int mask_len,
dimitris@6316
    46
                            double *output_values,int output_values_len) {
dimitris@6316
    47
    int i = 0,z = 0;
dimitris@6316
    48
    int top = mask_len * 8;
dimitris@6316
    49
    if (top > MAX_AXIS)
dimitris@6316
    50
        top = MAX_AXIS;
dimitris@6316
    51
dimitris@6316
    52
    SDL_memset(output_values,0,output_values_len * sizeof(double));
dimitris@6316
    53
    for (; i < top && z < output_values_len; i++) {
dimitris@6316
    54
        if (XIMaskIsSet(mask, i)) {
dimitris@6316
    55
            const int value = (int) *input_values;
dimitris@6316
    56
            output_values[z] = value;
dimitris@6316
    57
            input_values++;
dimitris@6316
    58
        }
dimitris@6316
    59
        z++;
dimitris@6316
    60
    }
dimitris@6316
    61
}
icculus@8019
    62
icculus@8283
    63
static int
slouken@8286
    64
query_xinput2_version(Display *display, int major, int minor)
icculus@8283
    65
{
icculus@8283
    66
    /* We don't care if this fails, so long as it sets major/minor on it's way out the door. */
icculus@8283
    67
    X11_XIQueryVersion(display, &major, &minor);
icculus@8283
    68
    return ((major * 1000) + minor);
icculus@8283
    69
}
icculus@8283
    70
icculus@8019
    71
static SDL_bool
icculus@8283
    72
xinput2_version_atleast(const int version, const int wantmajor, const int wantminor)
icculus@8019
    73
{
icculus@8283
    74
    return ( version >= ((wantmajor * 1000) + wantminor) );
icculus@8019
    75
}
slouken@11598
    76
slouken@11602
    77
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
slouken@11598
    78
static void
slouken@11598
    79
xinput2_normalize_touch_coordinates(SDL_VideoData *videodata, Window window,
slouken@11598
    80
    double in_x, double in_y, float *out_x, float *out_y)
slouken@11598
    81
{
slouken@11602
    82
    int i;
slouken@11602
    83
    for (i = 0; i < videodata->numwindows; i++) {
slouken@11602
    84
        SDL_WindowData *d = videodata->windowlist[i];
slouken@11602
    85
        if (d->xwindow == window) {
slouken@11602
    86
            if (d->window->w == 1) {
slouken@11602
    87
                *out_x = 0.5f;
slouken@11602
    88
            } else {
slouken@11602
    89
                *out_x = in_x / (d->window->w - 1);
slouken@11602
    90
            }
slouken@11602
    91
            if (d->window->h == 1) {
slouken@11602
    92
                *out_y = 0.5f;
slouken@11602
    93
            } else {
slouken@11602
    94
                *out_y = in_y / (d->window->h - 1);
slouken@11602
    95
            }
slouken@11602
    96
            return;
slouken@11602
    97
        }
slouken@11602
    98
    }
slouken@11602
    99
    // couldn't find the window...
slouken@11602
   100
    *out_x = in_x;
slouken@11602
   101
    *out_y = in_y;
slouken@11598
   102
}
slouken@11602
   103
#endif /* SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH */
slouken@11602
   104
dimitris@6316
   105
#endif /* SDL_VIDEO_DRIVER_X11_XINPUT2 */
dimitris@6316
   106
slouken@7191
   107
void
slouken@6877
   108
X11_InitXinput2(_THIS)
slouken@6877
   109
{
dimitris@6316
   110
#if SDL_VIDEO_DRIVER_X11_XINPUT2
dimitris@6316
   111
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
dimitris@6316
   112
icculus@8283
   113
    int version = 0;
dimitris@6316
   114
    XIEventMask eventmask;
dimitris@6316
   115
    unsigned char mask[3] = { 0,0,0 };
dimitris@6316
   116
    int event, err;
icculus@8283
   117
dimitris@6316
   118
    /*
dimitris@6316
   119
    * Initialize XInput 2
dimitris@6316
   120
    * According to http://who-t.blogspot.com/2009/05/xi2-recipes-part-1.html its better
slouken@7191
   121
    * to inform Xserver what version of Xinput we support.The server will store the version we support.
slouken@7191
   122
    * "As XI2 progresses it becomes important that you use this call as the server may treat the client
dimitris@6316
   123
    * differently depending on the supported version".
dimitris@6316
   124
    *
icculus@7827
   125
    * FIXME:event and err are not needed but if not passed X11_XQueryExtension returns SegmentationFault
dimitris@6316
   126
    */
slouken@6877
   127
    if (!SDL_X11_HAVE_XINPUT2 ||
icculus@7827
   128
        !X11_XQueryExtension(data->display, "XInputExtension", &xinput2_opcode, &event, &err)) {
icculus@8019
   129
        return; /* X server does not have XInput at all */
dimitris@6316
   130
    }
dimitris@6316
   131
icculus@8283
   132
    /* We need at least 2.2 for Multitouch, 2.0 otherwise. */
slouken@8286
   133
    version = query_xinput2_version(data->display, 2, 2);
icculus@8283
   134
    if (!xinput2_version_atleast(version, 2, 0)) {
icculus@8283
   135
        return; /* X server does not support the version we want at all. */
dimitris@6316
   136
    }
dimitris@6316
   137
dimitris@6316
   138
    xinput2_initialized = 1;
icculus@8019
   139
icculus@8283
   140
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH  /* Multitouch needs XInput 2.2 */
icculus@8283
   141
    xinput2_multitouch_supported = xinput2_version_atleast(version, 2, 2);
dimitris@6316
   142
#endif
dimitris@6316
   143
gabomdq@7678
   144
    /* Enable  Raw motion events for this display */
dimitris@6316
   145
    eventmask.deviceid = XIAllMasterDevices;
dimitris@6316
   146
    eventmask.mask_len = sizeof(mask);
dimitris@6316
   147
    eventmask.mask = mask;
dimitris@6316
   148
dimitris@6316
   149
    XISetMask(mask, XI_RawMotion);
icculus@10018
   150
    XISetMask(mask, XI_RawButtonPress);
icculus@10018
   151
    XISetMask(mask, XI_RawButtonRelease);
slouken@7191
   152
icculus@7827
   153
    if (X11_XISelectEvents(data->display,DefaultRootWindow(data->display),&eventmask,1) != Success) {
slouken@7191
   154
        return;
dimitris@6316
   155
    }
dimitris@6316
   156
#endif
dimitris@6316
   157
}
dimitris@6316
   158
slouken@7191
   159
int
slouken@6877
   160
X11_HandleXinput2Event(SDL_VideoData *videodata,XGenericEventCookie *cookie)
slouken@6877
   161
{
dimitris@6316
   162
#if SDL_VIDEO_DRIVER_X11_XINPUT2
dimitris@6316
   163
    if(cookie->extension != xinput2_opcode) {
dimitris@6316
   164
        return 0;
dimitris@6316
   165
    }
dimitris@6316
   166
    switch(cookie->evtype) {
dimitris@6316
   167
        case XI_RawMotion: {
dimitris@6316
   168
            const XIRawEvent *rawev = (const XIRawEvent*)cookie->data;
dimitris@6316
   169
            SDL_Mouse *mouse = SDL_GetMouse();
icculus@9721
   170
            double relative_coords[2];
icculus@9721
   171
            static Time prev_time = 0;
icculus@9721
   172
            static double prev_rel_coords[2];
dimitris@6316
   173
icculus@10018
   174
            videodata->global_mouse_changed = SDL_TRUE;
icculus@10018
   175
slouken@8071
   176
            if (!mouse->relative_mode || mouse->relative_mode_warp) {
dimitris@6316
   177
                return 0;
dimitris@6316
   178
            }
dimitris@6316
   179
dimitris@6316
   180
            parse_valuators(rawev->raw_values,rawev->valuators.mask,
icculus@9721
   181
                            rawev->valuators.mask_len,relative_coords,2);
icculus@9721
   182
icculus@9721
   183
            if ((rawev->time == prev_time) && (relative_coords[0] == prev_rel_coords[0]) && (relative_coords[1] == prev_rel_coords[1])) {
icculus@9721
   184
                return 0;  /* duplicate event, drop it. */
icculus@9721
   185
            }
icculus@9721
   186
icculus@9721
   187
            SDL_SendMouseMotion(mouse->focus,mouse->mouseID,1,(int)relative_coords[0],(int)relative_coords[1]);
icculus@9721
   188
            prev_rel_coords[0] = relative_coords[0];
icculus@9721
   189
            prev_rel_coords[1] = relative_coords[1];
icculus@9721
   190
            prev_time = rawev->time;
dimitris@6316
   191
            return 1;
dimitris@6316
   192
            }
dimitris@6316
   193
            break;
icculus@10018
   194
icculus@10018
   195
        case XI_RawButtonPress:
icculus@10018
   196
        case XI_RawButtonRelease:
icculus@10018
   197
            videodata->global_mouse_changed = SDL_TRUE;
icculus@10018
   198
            break;
icculus@10018
   199
dimitris@6316
   200
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
dimitris@6316
   201
        case XI_TouchBegin: {
dimitris@6316
   202
            const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
slouken@11598
   203
            float x, y;
slouken@11598
   204
            xinput2_normalize_touch_coordinates(videodata, xev->event,
slouken@11598
   205
                                  xev->event_x, xev->event_y, &x, &y);
slouken@11602
   206
            SDL_SendTouch(xev->sourceid,xev->detail, SDL_TRUE, x, y, 1.0);
dimitris@6316
   207
            return 1;
dimitris@6316
   208
            }
dimitris@6316
   209
            break;
dimitris@6316
   210
        case XI_TouchEnd: {
dimitris@6316
   211
            const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
slouken@11598
   212
            float x, y;
slouken@11598
   213
            xinput2_normalize_touch_coordinates(videodata, xev->event,
slouken@11598
   214
                                  xev->event_x, xev->event_y, &x, &y);
slouken@11602
   215
            SDL_SendTouch(xev->sourceid,xev->detail, SDL_FALSE, x, y, 1.0);
dimitris@6316
   216
            return 1;
dimitris@6316
   217
            }
dimitris@6316
   218
            break;
dimitris@6316
   219
        case XI_TouchUpdate: {
dimitris@6316
   220
            const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
slouken@11598
   221
            float x, y;
slouken@11598
   222
            xinput2_normalize_touch_coordinates(videodata, xev->event,
slouken@11598
   223
                                  xev->event_x, xev->event_y, &x, &y);
slouken@11602
   224
            SDL_SendTouchMotion(xev->sourceid,xev->detail, x, y, 1.0);
dimitris@6316
   225
            return 1;
dimitris@6316
   226
            }
dimitris@6316
   227
            break;
dimitris@6316
   228
#endif
dimitris@6316
   229
    }
dimitris@6316
   230
#endif
dimitris@6316
   231
    return 0;
dimitris@6316
   232
}
dimitris@6316
   233
slouken@7191
   234
void
slouken@6877
   235
X11_InitXinput2Multitouch(_THIS)
slouken@6877
   236
{
dimitris@6316
   237
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
dimitris@6316
   238
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
dimitris@6316
   239
    XIDeviceInfo *info;
dimitris@6316
   240
    int ndevices,i,j;
icculus@9696
   241
    info = X11_XIQueryDevice(data->display, XIAllDevices, &ndevices);
dimitris@6316
   242
dimitris@6316
   243
    for (i = 0; i < ndevices; i++) {
dimitris@6316
   244
        XIDeviceInfo *dev = &info[i];
dimitris@6316
   245
        for (j = 0; j < dev->num_classes; j++) {
dimitris@6316
   246
            SDL_TouchID touchId;
dimitris@6316
   247
            XIAnyClassInfo *class = dev->classes[j];
dimitris@6316
   248
            XITouchClassInfo *t = (XITouchClassInfo*)class;
dimitris@6316
   249
gabomdq@7678
   250
            /* Only touch devices */
dimitris@6316
   251
            if (class->type != XITouchClass)
dimitris@6316
   252
                continue;
dimitris@6316
   253
dimitris@6316
   254
            touchId = t->sourceid;
slouken@9679
   255
            SDL_AddTouch(touchId, dev->name);
dimitris@6316
   256
        }
dimitris@6316
   257
    }
icculus@7836
   258
    X11_XIFreeDeviceInfo(info);
dimitris@6316
   259
#endif
dimitris@6316
   260
}
dimitris@6316
   261
slouken@7191
   262
void
slouken@6877
   263
X11_Xinput2SelectTouch(_THIS, SDL_Window *window)
slouken@6877
   264
{
dimitris@6316
   265
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
gabomdq@7679
   266
    SDL_VideoData *data = NULL;
gabomdq@7679
   267
    XIEventMask eventmask;
gabomdq@7679
   268
    unsigned char mask[3] = { 0,0,0 };
gabomdq@7679
   269
    SDL_WindowData *window_data = NULL;
gabomdq@7679
   270
    
icculus@6433
   271
    if (!X11_Xinput2IsMultitouchSupported()) {
icculus@6433
   272
        return;
icculus@6433
   273
    }
icculus@6433
   274
gabomdq@7679
   275
    data = (SDL_VideoData *) _this->driverdata;
gabomdq@7679
   276
    window_data = (SDL_WindowData*)window->driverdata;
dimitris@6316
   277
dimitris@6316
   278
    eventmask.deviceid = XIAllMasterDevices;
dimitris@6316
   279
    eventmask.mask_len = sizeof(mask);
dimitris@6316
   280
    eventmask.mask = mask;
dimitris@6316
   281
dimitris@6316
   282
    XISetMask(mask, XI_TouchBegin);
dimitris@6316
   283
    XISetMask(mask, XI_TouchUpdate);
dimitris@6316
   284
    XISetMask(mask, XI_TouchEnd);
slouken@7191
   285
icculus@7827
   286
    X11_XISelectEvents(data->display,window_data->xwindow,&eventmask,1);
dimitris@6316
   287
#endif
dimitris@6316
   288
}
dimitris@6316
   289
dimitris@6316
   290
slouken@7191
   291
int
slouken@6877
   292
X11_Xinput2IsInitialized()
slouken@6877
   293
{
icculus@6375
   294
#if SDL_VIDEO_DRIVER_X11_XINPUT2
dimitris@6316
   295
    return xinput2_initialized;
icculus@6375
   296
#else
icculus@6375
   297
    return 0;
icculus@6375
   298
#endif
dimitris@6316
   299
}
dimitris@6316
   300
dimitris@6316
   301
int
slouken@6877
   302
X11_Xinput2IsMultitouchSupported()
slouken@6877
   303
{
icculus@6375
   304
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
dimitris@6316
   305
    return xinput2_initialized && xinput2_multitouch_supported;
icculus@6375
   306
#else
icculus@6375
   307
    return 0;
icculus@6375
   308
#endif
dimitris@6316
   309
}
dimitris@6316
   310
dimitris@6316
   311
#endif /* SDL_VIDEO_DRIVER_X11 */
dimitris@6316
   312
dimitris@6316
   313
/* vi: set ts=4 sw=4 expandtab: */