src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1740 db7e15a99cb3
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include <X11/Xlib.h>
slouken@0
    25
#include <X11/Xutil.h>
slouken@0
    26
slouken@0
    27
#include "SDL_mouse.h"
slouken@1361
    28
#include "../../events/SDL_events_c.h"
slouken@1361
    29
#include "../SDL_cursor_c.h"
slouken@0
    30
#include "SDL_x11dga_c.h"
slouken@0
    31
#include "SDL_x11mouse_c.h"
slouken@0
    32
slouken@0
    33
slouken@0
    34
/* The implementation dependent data for the window manager cursor */
slouken@1895
    35
struct WMcursor
slouken@1895
    36
{
slouken@1895
    37
    Cursor x_cursor;
slouken@0
    38
};
slouken@0
    39
slouken@0
    40
slouken@1895
    41
void
slouken@1895
    42
X11_FreeWMCursor(_THIS, WMcursor * cursor)
slouken@0
    43
{
slouken@1895
    44
    if (SDL_Display != NULL) {
slouken@1895
    45
        SDL_Lock_EventThread();
slouken@1895
    46
        XFreeCursor(SDL_Display, cursor->x_cursor);
slouken@1895
    47
        XSync(SDL_Display, False);
slouken@1895
    48
        SDL_Unlock_EventThread();
slouken@1895
    49
    }
slouken@1895
    50
    SDL_free(cursor);
slouken@0
    51
}
slouken@0
    52
slouken@1895
    53
WMcursor *
slouken@1895
    54
X11_CreateWMCursor(_THIS,
slouken@1895
    55
                   Uint8 * data, Uint8 * mask, int w, int h, int hot_x,
slouken@1895
    56
                   int hot_y)
slouken@0
    57
{
slouken@1895
    58
    WMcursor *cursor;
slouken@1895
    59
    XGCValues GCvalues;
slouken@1895
    60
    GC GCcursor;
slouken@1895
    61
    XImage *data_image, *mask_image;
slouken@1895
    62
    Pixmap data_pixmap, mask_pixmap;
slouken@1895
    63
    int clen, i;
slouken@1895
    64
    char *x_data, *x_mask;
slouken@1895
    65
    static XColor black = { 0, 0, 0, 0 };
slouken@1895
    66
    static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
slouken@0
    67
slouken@1895
    68
    /* Allocate the cursor memory */
slouken@1895
    69
    cursor = (WMcursor *) SDL_malloc(sizeof(WMcursor));
slouken@1895
    70
    if (cursor == NULL) {
slouken@1895
    71
        SDL_OutOfMemory();
slouken@1895
    72
        return (NULL);
slouken@1895
    73
    }
slouken@0
    74
slouken@1895
    75
    /* Mix the mask and the data */
slouken@1895
    76
    clen = (w / 8) * h;
slouken@1895
    77
    x_data = (char *) SDL_malloc(clen);
slouken@1895
    78
    if (x_data == NULL) {
slouken@1895
    79
        SDL_free(cursor);
slouken@1895
    80
        SDL_OutOfMemory();
slouken@1895
    81
        return (NULL);
slouken@1895
    82
    }
slouken@1895
    83
    x_mask = (char *) SDL_malloc(clen);
slouken@1895
    84
    if (x_mask == NULL) {
slouken@1895
    85
        SDL_free(cursor);
slouken@1895
    86
        SDL_free(x_data);
slouken@1895
    87
        SDL_OutOfMemory();
slouken@1895
    88
        return (NULL);
slouken@1895
    89
    }
slouken@1895
    90
    for (i = 0; i < clen; ++i) {
slouken@1895
    91
        /* The mask is OR'd with the data to turn inverted color
slouken@1895
    92
           pixels black since inverted color cursors aren't supported
slouken@1895
    93
           under X11.
slouken@1895
    94
         */
slouken@1895
    95
        x_mask[i] = data[i] | mask[i];
slouken@1895
    96
        x_data[i] = data[i];
slouken@1895
    97
    }
slouken@0
    98
slouken@1895
    99
    /* Prevent the event thread from running while we use the X server */
slouken@1895
   100
    SDL_Lock_EventThread();
slouken@0
   101
slouken@1895
   102
    /* Create the data image */
slouken@1895
   103
    data_image = XCreateImage(SDL_Display,
slouken@1895
   104
                              DefaultVisual(SDL_Display, SDL_Screen),
slouken@1895
   105
                              1, XYBitmap, 0, x_data, w, h, 8, w / 8);
slouken@1895
   106
    data_image->byte_order = MSBFirst;
slouken@1895
   107
    data_image->bitmap_bit_order = MSBFirst;
slouken@1895
   108
    data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   109
slouken@1895
   110
    /* Create the data mask */
slouken@1895
   111
    mask_image = XCreateImage(SDL_Display,
slouken@1895
   112
                              DefaultVisual(SDL_Display, SDL_Screen),
slouken@1895
   113
                              1, XYBitmap, 0, x_mask, w, h, 8, w / 8);
slouken@1895
   114
    mask_image->byte_order = MSBFirst;
slouken@1895
   115
    mask_image->bitmap_bit_order = MSBFirst;
slouken@1895
   116
    mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   117
slouken@1895
   118
    /* Create the graphics context */
slouken@1895
   119
    GCvalues.function = GXcopy;
slouken@1895
   120
    GCvalues.foreground = ~0;
slouken@1895
   121
    GCvalues.background = 0;
slouken@1895
   122
    GCvalues.plane_mask = AllPlanes;
slouken@1895
   123
    GCcursor = XCreateGC(SDL_Display, data_pixmap,
slouken@1895
   124
                         (GCFunction | GCForeground | GCBackground |
slouken@1895
   125
                          GCPlaneMask), &GCvalues);
slouken@0
   126
slouken@1895
   127
    /* Blit the images to the pixmaps */
slouken@1895
   128
    XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
slouken@1895
   129
              0, 0, 0, 0, w, h);
slouken@1895
   130
    XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
slouken@1895
   131
              0, 0, 0, 0, w, h);
slouken@1895
   132
    XFreeGC(SDL_Display, GCcursor);
slouken@1895
   133
    /* These free the x_data and x_mask memory pointers */
slouken@1895
   134
    XDestroyImage(data_image);
slouken@1895
   135
    XDestroyImage(mask_image);
slouken@0
   136
slouken@1895
   137
    /* Create the cursor */
slouken@1895
   138
    cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
slouken@1895
   139
                                           mask_pixmap, &black, &white,
slouken@1895
   140
                                           hot_x, hot_y);
slouken@1895
   141
    XFreePixmap(SDL_Display, data_pixmap);
slouken@1895
   142
    XFreePixmap(SDL_Display, mask_pixmap);
slouken@0
   143
slouken@1895
   144
    /* Release the event thread */
slouken@1895
   145
    XSync(SDL_Display, False);
slouken@1895
   146
    SDL_Unlock_EventThread();
slouken@0
   147
slouken@1895
   148
    return (cursor);
slouken@0
   149
}
slouken@0
   150
slouken@1895
   151
int
slouken@1895
   152
X11_ShowWMCursor(_THIS, WMcursor * cursor)
slouken@0
   153
{
slouken@1895
   154
    /* Don't do anything if the display is gone */
slouken@1895
   155
    if (SDL_Display == NULL) {
slouken@1895
   156
        return (0);
slouken@1895
   157
    }
slouken@0
   158
slouken@1895
   159
    /* Set the X11 cursor cursor, or blank if cursor is NULL */
slouken@1895
   160
    if (SDL_Window) {
slouken@1895
   161
        SDL_Lock_EventThread();
slouken@1895
   162
        if (cursor == NULL) {
slouken@1895
   163
            if (SDL_BlankCursor != NULL) {
slouken@1895
   164
                XDefineCursor(SDL_Display, SDL_Window,
slouken@1895
   165
                              SDL_BlankCursor->x_cursor);
slouken@1895
   166
            }
slouken@1895
   167
        } else {
slouken@1895
   168
            XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
slouken@1895
   169
        }
slouken@1895
   170
        XSync(SDL_Display, False);
slouken@1895
   171
        SDL_Unlock_EventThread();
slouken@1895
   172
    }
slouken@1895
   173
    return (1);
slouken@0
   174
}
slouken@0
   175
slouken@1895
   176
void
slouken@1895
   177
X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   178
{
slouken@1895
   179
    if (using_dga & DGA_MOUSE) {
slouken@1895
   180
        SDL_PrivateMouseMotion(0, 0, x, y);
slouken@1895
   181
    } else if (mouse_relative) {
slouken@1895
   182
        /*      RJR: March 28, 2000
slouken@1895
   183
           leave physical cursor at center of screen if
slouken@1895
   184
           mouse hidden and grabbed */
slouken@1895
   185
        SDL_PrivateMouseMotion(0, 0, x, y);
slouken@1895
   186
    } else {
slouken@1895
   187
        SDL_Lock_EventThread();
slouken@1895
   188
        XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
slouken@1895
   189
        XSync(SDL_Display, False);
slouken@1895
   190
        SDL_Unlock_EventThread();
slouken@1895
   191
    }
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
/* Sets the mouse acceleration from a string of the form:
slouken@0
   195
	2/1/0
slouken@0
   196
   The first number is the numerator, followed by the acceleration
slouken@0
   197
   denumenator and threshold.
slouken@0
   198
*/
slouken@1895
   199
static void
slouken@1895
   200
SetMouseAccel(_THIS, const char *accel_param)
slouken@0
   201
{
slouken@1895
   202
    int i;
slouken@1895
   203
    size_t len;
slouken@1895
   204
    int accel_value[3];
slouken@1895
   205
    char *mouse_param, *mouse_param_buf, *pin;
slouken@0
   206
slouken@1895
   207
    len = SDL_strlen(accel_param) + 1;
slouken@1895
   208
    mouse_param_buf = SDL_stack_alloc(char, len);
slouken@1895
   209
    if (!mouse_param_buf) {
slouken@1895
   210
        return;
slouken@1895
   211
    }
slouken@1895
   212
    SDL_strlcpy(mouse_param_buf, accel_param, len);
slouken@1895
   213
    mouse_param = mouse_param_buf;
slouken@0
   214
slouken@1895
   215
    for (i = 0; (i < 3) && mouse_param; ++i) {
slouken@1895
   216
        pin = SDL_strchr(mouse_param, '/');
slouken@1895
   217
        if (pin) {
slouken@1895
   218
            *pin = '\0';
slouken@1895
   219
        }
slouken@1895
   220
        accel_value[i] = atoi(mouse_param);
slouken@1895
   221
        if (pin) {
slouken@1895
   222
            mouse_param = pin + 1;
slouken@1895
   223
        } else {
slouken@1895
   224
            mouse_param = NULL;
slouken@1895
   225
        }
slouken@1895
   226
    }
slouken@1895
   227
    if (mouse_param_buf) {
slouken@1895
   228
        XChangePointerControl(SDL_Display, True, True,
slouken@1895
   229
                              accel_value[0], accel_value[1], accel_value[2]);
slouken@1895
   230
        SDL_free(mouse_param_buf);
slouken@1895
   231
    }
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
/* Check to see if we need to enter or leave mouse relative mode */
slouken@1895
   235
void
slouken@1895
   236
X11_CheckMouseModeNoLock(_THIS)
slouken@0
   237
{
slouken@1895
   238
    const Uint8 full_focus =
slouken@1895
   239
        (SDL_APPACTIVE | SDL_APPINPUTFOCUS | SDL_APPMOUSEFOCUS);
slouken@1895
   240
    char *env_override;
slouken@1895
   241
    int enable_relative = 1;
slouken@79
   242
slouken@1895
   243
    /* Allow the user to override the relative mouse mode.
slouken@1895
   244
       They almost never want to do this, as it seriously affects
slouken@1895
   245
       applications that rely on continuous relative mouse motion.
slouken@1895
   246
     */
slouken@1895
   247
    env_override = SDL_getenv("SDL_MOUSE_RELATIVE");
slouken@1895
   248
    if (env_override) {
slouken@1895
   249
        enable_relative = atoi(env_override);
slouken@1895
   250
    }
slouken@79
   251
slouken@1895
   252
    /* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@1895
   253
    if (enable_relative &&
slouken@1895
   254
        !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@1895
   255
        (SDL_CurrentWindow.input_grab != SDL_GRAB_OFF) &&
slouken@1895
   256
        (SDL_GetAppState() & full_focus) == full_focus) {
slouken@1895
   257
        if (!mouse_relative) {
slouken@1895
   258
            X11_EnableDGAMouse(this);
slouken@1895
   259
            if (!(using_dga & DGA_MOUSE)) {
slouken@1895
   260
                char *xmouse_accel;
slouken@0
   261
slouken@1895
   262
                SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
slouken@1895
   263
                /* Use as raw mouse mickeys as possible */
slouken@1895
   264
                XGetPointerControl(SDL_Display,
slouken@1895
   265
                                   &mouse_accel.numerator,
slouken@1895
   266
                                   &mouse_accel.denominator,
slouken@1895
   267
                                   &mouse_accel.threshold);
slouken@1895
   268
                xmouse_accel = SDL_getenv("SDL_VIDEO_X11_MOUSEACCEL");
slouken@1895
   269
                if (xmouse_accel) {
slouken@1895
   270
                    SetMouseAccel(this, xmouse_accel);
slouken@1895
   271
                }
slouken@1895
   272
            }
slouken@1895
   273
            mouse_relative = 1;
slouken@1895
   274
        }
slouken@1895
   275
    } else {
slouken@1895
   276
        if (mouse_relative) {
slouken@1895
   277
            if (using_dga & DGA_MOUSE) {
slouken@1895
   278
                X11_DisableDGAMouse(this);
slouken@1895
   279
            } else {
slouken@1895
   280
                XChangePointerControl(SDL_Display, True, True,
slouken@1895
   281
                                      mouse_accel.numerator,
slouken@1895
   282
                                      mouse_accel.denominator,
slouken@1895
   283
                                      mouse_accel.threshold);
slouken@1895
   284
            }
slouken@1895
   285
            mouse_relative = 0;
slouken@1895
   286
        }
slouken@1895
   287
    }
slouken@0
   288
}
slouken@1895
   289
void
slouken@1895
   290
X11_CheckMouseMode(_THIS)
slouken@0
   291
{
slouken@1895
   292
    SDL_Lock_EventThread();
slouken@1895
   293
    X11_CheckMouseModeNoLock(this);
slouken@1895
   294
    SDL_Unlock_EventThread();
slouken@0
   295
}
slouken@1895
   296
slouken@1895
   297
/* vi: set ts=4 sw=4 expandtab: */