src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1657 5b0805ceb50f
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    35
struct WMcursor
slouken@1662
    36
{
slouken@1662
    37
    Cursor x_cursor;
slouken@0
    38
};
slouken@0
    39
slouken@0
    40
slouken@1662
    41
void
slouken@1662
    42
X11_FreeWMCursor (_THIS, WMcursor * cursor)
slouken@0
    43
{
slouken@1662
    44
    if (SDL_Display != NULL) {
slouken@1662
    45
        SDL_Lock_EventThread ();
slouken@1662
    46
        XFreeCursor (SDL_Display, cursor->x_cursor);
slouken@1662
    47
        XSync (SDL_Display, False);
slouken@1662
    48
        SDL_Unlock_EventThread ();
slouken@1662
    49
    }
slouken@1662
    50
    SDL_free (cursor);
slouken@0
    51
}
slouken@0
    52
slouken@1662
    53
WMcursor *
slouken@1662
    54
X11_CreateWMCursor (_THIS,
slouken@1662
    55
                    Uint8 * data, Uint8 * mask, int w, int h, int hot_x,
slouken@1662
    56
                    int hot_y)
slouken@0
    57
{
slouken@1662
    58
    WMcursor *cursor;
slouken@1662
    59
    XGCValues GCvalues;
slouken@1662
    60
    GC GCcursor;
slouken@1662
    61
    XImage *data_image, *mask_image;
slouken@1662
    62
    Pixmap data_pixmap, mask_pixmap;
slouken@1662
    63
    int clen, i;
slouken@1662
    64
    char *x_data, *x_mask;
slouken@1662
    65
    static XColor black = { 0, 0, 0, 0 };
slouken@1662
    66
    static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
slouken@0
    67
slouken@1662
    68
    /* Allocate the cursor memory */
slouken@1662
    69
    cursor = (WMcursor *) SDL_malloc (sizeof (WMcursor));
slouken@1662
    70
    if (cursor == NULL) {
slouken@1662
    71
        SDL_OutOfMemory ();
slouken@1662
    72
        return (NULL);
slouken@1662
    73
    }
slouken@0
    74
slouken@1662
    75
    /* Mix the mask and the data */
slouken@1662
    76
    clen = (w / 8) * h;
slouken@1662
    77
    x_data = (char *) SDL_malloc (clen);
slouken@1662
    78
    if (x_data == NULL) {
slouken@1662
    79
        SDL_free (cursor);
slouken@1662
    80
        SDL_OutOfMemory ();
slouken@1662
    81
        return (NULL);
slouken@1662
    82
    }
slouken@1662
    83
    x_mask = (char *) SDL_malloc (clen);
slouken@1662
    84
    if (x_mask == NULL) {
slouken@1662
    85
        SDL_free (cursor);
slouken@1662
    86
        SDL_free (x_data);
slouken@1662
    87
        SDL_OutOfMemory ();
slouken@1662
    88
        return (NULL);
slouken@1662
    89
    }
slouken@1662
    90
    for (i = 0; i < clen; ++i) {
slouken@1662
    91
        /* The mask is OR'd with the data to turn inverted color
slouken@1662
    92
           pixels black since inverted color cursors aren't supported
slouken@1662
    93
           under X11.
slouken@1662
    94
         */
slouken@1662
    95
        x_mask[i] = data[i] | mask[i];
slouken@1662
    96
        x_data[i] = data[i];
slouken@1662
    97
    }
slouken@0
    98
slouken@1662
    99
    /* Prevent the event thread from running while we use the X server */
slouken@1662
   100
    SDL_Lock_EventThread ();
slouken@0
   101
slouken@1662
   102
    /* Create the data image */
slouken@1662
   103
    data_image = XCreateImage (SDL_Display,
slouken@1662
   104
                               DefaultVisual (SDL_Display, SDL_Screen),
slouken@1662
   105
                               1, XYBitmap, 0, x_data, w, h, 8, w / 8);
slouken@1662
   106
    data_image->byte_order = MSBFirst;
slouken@1662
   107
    data_image->bitmap_bit_order = MSBFirst;
slouken@1662
   108
    data_pixmap = XCreatePixmap (SDL_Display, SDL_Root, w, h, 1);
slouken@0
   109
slouken@1662
   110
    /* Create the data mask */
slouken@1662
   111
    mask_image = XCreateImage (SDL_Display,
slouken@1662
   112
                               DefaultVisual (SDL_Display, SDL_Screen),
slouken@1662
   113
                               1, XYBitmap, 0, x_mask, w, h, 8, w / 8);
slouken@1662
   114
    mask_image->byte_order = MSBFirst;
slouken@1662
   115
    mask_image->bitmap_bit_order = MSBFirst;
slouken@1662
   116
    mask_pixmap = XCreatePixmap (SDL_Display, SDL_Root, w, h, 1);
slouken@0
   117
slouken@1662
   118
    /* Create the graphics context */
slouken@1662
   119
    GCvalues.function = GXcopy;
slouken@1662
   120
    GCvalues.foreground = ~0;
slouken@1662
   121
    GCvalues.background = 0;
slouken@1662
   122
    GCvalues.plane_mask = AllPlanes;
slouken@1662
   123
    GCcursor = XCreateGC (SDL_Display, data_pixmap,
slouken@1662
   124
                          (GCFunction | GCForeground | GCBackground |
slouken@1662
   125
                           GCPlaneMask), &GCvalues);
slouken@0
   126
slouken@1662
   127
    /* Blit the images to the pixmaps */
slouken@1662
   128
    XPutImage (SDL_Display, data_pixmap, GCcursor, data_image,
slouken@1662
   129
               0, 0, 0, 0, w, h);
slouken@1662
   130
    XPutImage (SDL_Display, mask_pixmap, GCcursor, mask_image,
slouken@1662
   131
               0, 0, 0, 0, w, h);
slouken@1662
   132
    XFreeGC (SDL_Display, GCcursor);
slouken@1662
   133
    /* These free the x_data and x_mask memory pointers */
slouken@1662
   134
    XDestroyImage (data_image);
slouken@1662
   135
    XDestroyImage (mask_image);
slouken@0
   136
slouken@1662
   137
    /* Create the cursor */
slouken@1662
   138
    cursor->x_cursor = XCreatePixmapCursor (SDL_Display, data_pixmap,
slouken@1662
   139
                                            mask_pixmap, &black, &white,
slouken@1662
   140
                                            hot_x, hot_y);
slouken@1662
   141
    XFreePixmap (SDL_Display, data_pixmap);
slouken@1662
   142
    XFreePixmap (SDL_Display, mask_pixmap);
slouken@0
   143
slouken@1662
   144
    /* Release the event thread */
slouken@1662
   145
    XSync (SDL_Display, False);
slouken@1662
   146
    SDL_Unlock_EventThread ();
slouken@0
   147
slouken@1662
   148
    return (cursor);
slouken@0
   149
}
slouken@0
   150
slouken@1662
   151
int
slouken@1662
   152
X11_ShowWMCursor (_THIS, WMcursor * cursor)
slouken@0
   153
{
slouken@1662
   154
    /* Don't do anything if the display is gone */
slouken@1662
   155
    if (SDL_Display == NULL) {
slouken@1662
   156
        return (0);
slouken@1662
   157
    }
slouken@0
   158
slouken@1662
   159
    /* Set the X11 cursor cursor, or blank if cursor is NULL */
slouken@1662
   160
    if (SDL_Window) {
slouken@1662
   161
        SDL_Lock_EventThread ();
slouken@1662
   162
        if (cursor == NULL) {
slouken@1662
   163
            if (SDL_BlankCursor != NULL) {
slouken@1662
   164
                XDefineCursor (SDL_Display, SDL_Window,
slouken@1662
   165
                               SDL_BlankCursor->x_cursor);
slouken@1662
   166
            }
slouken@1662
   167
        } else {
slouken@1662
   168
            XDefineCursor (SDL_Display, SDL_Window, cursor->x_cursor);
slouken@1662
   169
        }
slouken@1662
   170
        XSync (SDL_Display, False);
slouken@1662
   171
        SDL_Unlock_EventThread ();
slouken@1662
   172
    }
slouken@1662
   173
    return (1);
slouken@0
   174
}
slouken@0
   175
slouken@1662
   176
void
slouken@1662
   177
X11_WarpWMCursor (_THIS, Uint16 x, Uint16 y)
slouken@0
   178
{
slouken@1662
   179
    if (using_dga & DGA_MOUSE) {
slouken@1662
   180
        SDL_PrivateMouseMotion (0, 0, x, y);
slouken@1662
   181
    } else if (mouse_relative) {
slouken@1662
   182
        /*      RJR: March 28, 2000
slouken@1662
   183
           leave physical cursor at center of screen if
slouken@1662
   184
           mouse hidden and grabbed */
slouken@1662
   185
        SDL_PrivateMouseMotion (0, 0, x, y);
slouken@1662
   186
    } else {
slouken@1662
   187
        SDL_Lock_EventThread ();
slouken@1662
   188
        XWarpPointer (SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
slouken@1662
   189
        XSync (SDL_Display, False);
slouken@1662
   190
        SDL_Unlock_EventThread ();
slouken@1662
   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@1662
   199
static void
slouken@1662
   200
SetMouseAccel (_THIS, const char *accel_param)
slouken@0
   201
{
slouken@1662
   202
    int i;
slouken@1662
   203
    size_t len;
slouken@1662
   204
    int accel_value[3];
slouken@1662
   205
    char *mouse_param, *mouse_param_buf, *pin;
slouken@0
   206
slouken@1662
   207
    len = SDL_strlen (accel_param) + 1;
slouken@1662
   208
    mouse_param_buf = SDL_stack_alloc (char, len);
slouken@1662
   209
    if (!mouse_param_buf) {
slouken@1662
   210
        return;
slouken@1662
   211
    }
slouken@1662
   212
    SDL_strlcpy (mouse_param_buf, accel_param, len);
slouken@1662
   213
    mouse_param = mouse_param_buf;
slouken@0
   214
slouken@1662
   215
    for (i = 0; (i < 3) && mouse_param; ++i) {
slouken@1662
   216
        pin = SDL_strchr (mouse_param, '/');
slouken@1662
   217
        if (pin) {
slouken@1662
   218
            *pin = '\0';
slouken@1662
   219
        }
slouken@1662
   220
        accel_value[i] = atoi (mouse_param);
slouken@1662
   221
        if (pin) {
slouken@1662
   222
            mouse_param = pin + 1;
slouken@1662
   223
        } else {
slouken@1662
   224
            mouse_param = NULL;
slouken@1662
   225
        }
slouken@1662
   226
    }
slouken@1662
   227
    if (mouse_param_buf) {
slouken@1662
   228
        XChangePointerControl (SDL_Display, True, True,
slouken@1662
   229
                               accel_value[0], accel_value[1],
slouken@1662
   230
                               accel_value[2]);
slouken@1662
   231
        SDL_free (mouse_param_buf);
slouken@1662
   232
    }
slouken@0
   233
}
slouken@0
   234
slouken@0
   235
/* Check to see if we need to enter or leave mouse relative mode */
slouken@1662
   236
void
slouken@1662
   237
X11_CheckMouseModeNoLock (_THIS)
slouken@0
   238
{
slouken@1662
   239
    const Uint8 full_focus =
slouken@1662
   240
        (SDL_APPACTIVE | SDL_APPINPUTFOCUS | SDL_APPMOUSEFOCUS);
slouken@1662
   241
    char *env_override;
slouken@1662
   242
    int enable_relative = 1;
slouken@79
   243
slouken@1662
   244
    /* Allow the user to override the relative mouse mode.
slouken@1662
   245
       They almost never want to do this, as it seriously affects
slouken@1662
   246
       applications that rely on continuous relative mouse motion.
slouken@1662
   247
     */
slouken@1662
   248
    env_override = SDL_getenv ("SDL_MOUSE_RELATIVE");
slouken@1662
   249
    if (env_override) {
slouken@1662
   250
        enable_relative = atoi (env_override);
slouken@1662
   251
    }
slouken@79
   252
slouken@1662
   253
    /* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@1662
   254
    if (enable_relative &&
slouken@1662
   255
        !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@1662
   256
        (SDL_CurrentWindow.input_grab != SDL_GRAB_OFF) &&
slouken@1662
   257
        (SDL_GetAppState () & full_focus) == full_focus) {
slouken@1662
   258
        if (!mouse_relative) {
slouken@1662
   259
            X11_EnableDGAMouse (this);
slouken@1662
   260
            if (!(using_dga & DGA_MOUSE)) {
slouken@1662
   261
                char *xmouse_accel;
slouken@0
   262
slouken@1662
   263
                SDL_GetMouseState (&mouse_last.x, &mouse_last.y);
slouken@1662
   264
                /* Use as raw mouse mickeys as possible */
slouken@1662
   265
                XGetPointerControl (SDL_Display,
slouken@1662
   266
                                    &mouse_accel.numerator,
slouken@1662
   267
                                    &mouse_accel.denominator,
slouken@1662
   268
                                    &mouse_accel.threshold);
slouken@1662
   269
                xmouse_accel = SDL_getenv ("SDL_VIDEO_X11_MOUSEACCEL");
slouken@1662
   270
                if (xmouse_accel) {
slouken@1662
   271
                    SetMouseAccel (this, xmouse_accel);
slouken@1662
   272
                }
slouken@1662
   273
            }
slouken@1662
   274
            mouse_relative = 1;
slouken@1662
   275
        }
slouken@1662
   276
    } else {
slouken@1662
   277
        if (mouse_relative) {
slouken@1662
   278
            if (using_dga & DGA_MOUSE) {
slouken@1662
   279
                X11_DisableDGAMouse (this);
slouken@1662
   280
            } else {
slouken@1662
   281
                XChangePointerControl (SDL_Display, True, True,
slouken@1662
   282
                                       mouse_accel.numerator,
slouken@1662
   283
                                       mouse_accel.denominator,
slouken@1662
   284
                                       mouse_accel.threshold);
slouken@1662
   285
            }
slouken@1662
   286
            mouse_relative = 0;
slouken@1662
   287
        }
slouken@1662
   288
    }
slouken@0
   289
}
slouken@1662
   290
void
slouken@1662
   291
X11_CheckMouseMode (_THIS)
slouken@0
   292
{
slouken@1662
   293
    SDL_Lock_EventThread ();
slouken@1662
   294
    X11_CheckMouseModeNoLock (this);
slouken@1662
   295
    SDL_Unlock_EventThread ();
slouken@0
   296
}
slouken@1662
   297
slouken@1662
   298
/* vi: set ts=4 sw=4 expandtab: */