mouse.c 11.3 KB
Newer Older
1
/*
2
 * X11 mouse driver
3
 *
4
 * Copyright 1998 Ulrich Weigand
5 6 7 8
 */

#include "config.h"

9 10
#include "ts_xlib.h"

11 12 13
#include "windef.h"
#include "wine/winuser16.h"

14
#include "debugtools.h"
15
#include "mouse.h"
Patrik Stridvall's avatar
Patrik Stridvall committed
16
#include "win.h"
17 18
#include "x11drv.h"

19
DEFAULT_DEBUG_CHANNEL(cursor);
20

21 22
/**********************************************************************/

Patrik Stridvall's avatar
Patrik Stridvall committed
23
Cursor X11DRV_MOUSE_XCursor = None;    /* Current X cursor */
24

25
static LONG X11DRV_MOUSE_WarpPointer = 0;  /* hack; see DISPLAY_MoveCursor */
26
static LPMOUSE_EVENT_PROC DefMouseEventProc = NULL;
27 28 29 30

/***********************************************************************
 *		X11DRV_MOUSE_DoSetCursor
 */
31
static BOOL X11DRV_MOUSE_DoSetCursor( CURSORICONINFO *ptr )
32
{
33
    Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
34 35 36 37 38 39 40 41
    XColor fg, bg;
    Cursor cursor = None;

    if (!ptr)  /* Create an empty cursor */
    {
        static const char data[] = { 0 };

        bg.red = bg.green = bg.blue = 0x0000;
Patrik Stridvall's avatar
Patrik Stridvall committed
42
        pixmapBits = XCreateBitmapFromData( display, X11DRV_GetXRootWindow(), data, 1, 1 );
43 44 45 46 47 48 49 50 51 52 53 54 55
        if (pixmapBits)
        {
            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
                                          &bg, &bg, 0, 0 );
            XFreePixmap( display, pixmapBits );
        }
    }
    else  /* Create the X cursor from the bits */
    {
        XImage *image;

        if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
        {
56
            WARN("Cursor has more than 1 bpp!\n" );
57 58 59 60 61 62 63 64 65 66
            return FALSE;
        }

        /* Create a pixmap and transfer all the bits to it */

        /* NOTE: Following hack works, but only because XFree depth
         *       1 images really use 1 bit/pixel (and so the same layout
         *       as the Windows cursor data). Perhaps use a more generic
         *       algorithm here.
         */
Patrik Stridvall's avatar
Patrik Stridvall committed
67
        pixmapAll = XCreatePixmap( display, X11DRV_GetXRootWindow(),
68
                                   ptr->nWidth, ptr->nHeight * 2, 1 );
69
        image = XCreateImage( display, X11DRV_GetVisual(),
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                              1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
                              ptr->nHeight * 2, 16, ptr->nWidthBytes);
        if (image)
        {
            image->byte_order = MSBFirst;
            image->bitmap_bit_order = MSBFirst;
            image->bitmap_unit = 16;
            _XInitImageFuncPtrs(image);
            if (pixmapAll)
                XPutImage( display, pixmapAll, BITMAP_monoGC, image,
                           0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
            image->data = NULL;
            XDestroyImage( image );
        }

        /* Now create the 2 pixmaps for bits and mask */

Patrik Stridvall's avatar
Patrik Stridvall committed
87
        pixmapBits = XCreatePixmap( display, X11DRV_GetXRootWindow(),
88
                                    ptr->nWidth, ptr->nHeight, 1 );
Patrik Stridvall's avatar
Patrik Stridvall committed
89
        pixmapMask = XCreatePixmap( display, X11DRV_GetXRootWindow(),
90
                                    ptr->nWidth, ptr->nHeight, 1 );
91 92
        pixmapMaskInv = XCreatePixmap( display, X11DRV_GetXRootWindow(),
                                    ptr->nWidth, ptr->nHeight, 1 );
93 94 95 96 97 98 99 100 101

        /* Make sure everything went OK so far */

        if (pixmapBits && pixmapMask && pixmapAll)
        {
            /* We have to do some magic here, as cursors are not fully
             * compatible between Windows and X11. Under X11, there
             * are only 3 possible color cursor: black, white and
             * masked. So we map the 4th Windows color (invert the
102 103
             * bits on the screen) to black and an additional white bit on 
             * an other place (+1,+1). This require some boolean arithmetic:
104 105
             *
             *         Windows          |          X11
106
             * And    Xor      Result   |   Bits     Mask     Result
107
             *  0      0     black      |    0        1     background
108 109
             *  0      1     white      |    1        1     foreground
             *  1      0     no change  |    X        0     no change
110 111 112
             *  1      1     inverted   |    0        1     background
             *
             * which gives:
113 114
             *  Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
             *  Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
115 116 117 118 119
             *
             * FIXME: apparently some servers do support 'inverted' color.
             * I don't know if it's correct per the X spec, but maybe
             * we ought to take advantage of it.  -- AJ
             */
120
            XSetFunction( display, BITMAP_monoGC, GXcopy );
121 122 123 124
            XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
                       0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
            XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
                       0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
125 126 127 128 129
            XCopyArea( display, pixmapAll, pixmapMaskInv, BITMAP_monoGC,
                       0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
            XSetFunction( display, BITMAP_monoGC, GXand );
            XCopyArea( display, pixmapAll, pixmapMaskInv, BITMAP_monoGC,
                       0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
130 131 132 133 134 135
            XSetFunction( display, BITMAP_monoGC, GXandReverse );
            XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
                       0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
            XSetFunction( display, BITMAP_monoGC, GXorReverse );
            XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
                       0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
136 137 138 139 140 141
            /* Additional white */
            XSetFunction( display, BITMAP_monoGC, GXor );
            XCopyArea( display, pixmapMaskInv, pixmapMask, BITMAP_monoGC,
                       0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
            XCopyArea( display, pixmapMaskInv, pixmapBits, BITMAP_monoGC,
                       0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
142 143 144 145 146 147 148 149 150 151 152 153
            XSetFunction( display, BITMAP_monoGC, GXcopy );
            fg.red = fg.green = fg.blue = 0xffff;
            bg.red = bg.green = bg.blue = 0x0000;
            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
                                &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
        }

        /* Now free everything */

        if (pixmapAll) XFreePixmap( display, pixmapAll );
        if (pixmapBits) XFreePixmap( display, pixmapBits );
        if (pixmapMask) XFreePixmap( display, pixmapMask );
154
        if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
155 156 157
    }

    if (cursor == None) return FALSE;
Patrik Stridvall's avatar
Patrik Stridvall committed
158 159
    if (X11DRV_MOUSE_XCursor != None) XFreeCursor( display, X11DRV_MOUSE_XCursor );
    X11DRV_MOUSE_XCursor = cursor;
160

161 162 163 164
    return TRUE;
}

/***********************************************************************
165
 *		SetCursor (X11DRV.@)
166
 */
167
void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
168 169 170
{
    BOOL success;

171 172 173
    wine_tsx11_lock();
    success = X11DRV_MOUSE_DoSetCursor( lpCursor );
    wine_tsx11_unlock();
174 175 176
    if ( !success ) return;

    if (X11DRV_GetXRootWindow() != DefaultRootWindow(display))
177
    {
178 179
        /* If in desktop mode, set the cursor on the desktop window */

180
        TSXDefineCursor( display, X11DRV_GetXRootWindow(), X11DRV_MOUSE_XCursor );
181 182 183
    }
    else
    {
184 185 186 187
        /* Else, set the same cursor for all top-level windows */

        /* FIXME: we should not reference USER internals here, but native USER 
                  works only in desktop mode anyway, so this should not matter */
188

189
        HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
190 191
        while(hwnd)
        {
192 193
            WND *tmpWnd = WIN_FindWndPtr(hwnd);
            Window win = X11DRV_WND_FindXWindow(tmpWnd );
194
            if (win && win!=DefaultRootWindow(display))
195
                TSXDefineCursor( display, win, X11DRV_MOUSE_XCursor );
196
            hwnd = GetWindow( hwnd, GW_HWNDNEXT );
197
            WIN_ReleaseWndPtr(tmpWnd);
198 199 200 201 202
        }
    }
}

/***********************************************************************
203
 *		MoveCursor (X11DRV.@)
204
 */
205
void X11DRV_MoveCursor(WORD wAbsX, WORD wAbsY)
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
{
  /* 
   * We do not want the to create MotionNotify events here, 
   * otherwise we will get an endless recursion:
   * XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
   * -> XWarpPointer -> XMotionEvent -> ...
   *
   * Unfortunately, the XWarpPointer call does create a MotionNotify
   * event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
   * procedure, it sets a global flag. If this flag is set, we skip the
   * XWarpPointer call.  If we are *not* called from within MOUSE_SendEvent,
   * we will call XWarpPointer, which will create a MotionNotify event.
   * Strictly speaking, this is also wrong, but that should normally not
   * have any negative effects ...
   *
   * But first of all, we check whether we already are at the position
   * are supposed to move to; if so, we don't need to do anything.
   */
  
  Window root, child;
  int rootX, rootY, winX, winY;
  unsigned int xstate;
  
229
  if (X11DRV_MOUSE_WarpPointer < 0) return;
230

Patrik Stridvall's avatar
Patrik Stridvall committed
231
  if (!TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
232 233 234 235 236 237
			&rootX, &rootY, &winX, &winY, &xstate ))
    return;
  
  if ( winX == wAbsX && winY == wAbsY )
    return;
  
238
  TRACE("(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
239
  
Patrik Stridvall's avatar
Patrik Stridvall committed
240 241
  TSXWarpPointer( display, X11DRV_GetXRootWindow(), X11DRV_GetXRootWindow(), 
		  0, 0, 0, 0, wAbsX, wAbsY );
242 243
}

244
/***********************************************************************
245
 *		InitMouse (X11DRV.@)
246
 */
247
void X11DRV_InitMouse( LPMOUSE_EVENT_PROC proc )
248
{
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    static int init_done;

    DefMouseEventProc = proc;

    if (!init_done)
    {
        Window root, child;
        int root_x, root_y, child_x, child_y;
        unsigned int KeyState;
  
        init_done = 1;
        /* Get the current mouse position and simulate an absolute mouse
           movement to initialize the mouse global variables */
        TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
                         &root_x, &root_y, &child_x, &child_y, &KeyState);
264
        X11DRV_SendEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
265 266 267
                               root_x, root_y, X11DRV_EVENT_XStateToKeyState(KeyState),
                               GetTickCount(), 0 );
    }
268 269
}

270

271
/***********************************************************************
272
 *		X11DRV_SendEvent (internal)
273
 */
274
void X11DRV_SendEvent( DWORD mouseStatus, DWORD posX, DWORD posY, 
275
                             DWORD keyState, DWORD time, HWND hWnd )
276
{
277 278
    int width  = GetSystemMetrics( SM_CXSCREEN );
    int height = GetSystemMetrics( SM_CYSCREEN );
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
    int iWndsLocks;
    WINE_MOUSEEVENT wme;

    if ( !DefMouseEventProc ) return;

    TRACE("(%04lX,%ld,%ld)\n", mouseStatus, posX, posY );

    if (mouseStatus & MOUSEEVENTF_MOVE) {
      if (mouseStatus & MOUSEEVENTF_ABSOLUTE) {
	/* Relative mouse movements seems not to be scaled as absolute ones */
	posX = (((long)posX << 16) + width-1)  / width;
	posY = (((long)posY << 16) + height-1) / height;
      }
    }

    wme.magic    = WINE_MOUSEEVENT_MAGIC;
    wme.time     = time;
    wme.hWnd     = hWnd;
    wme.keyState = keyState;
    
    InterlockedDecrement( &X11DRV_MOUSE_WarpPointer );
    /* To avoid deadlocks, we have to suspend all locks on windows structures
       before the program control is passed to the mouse driver */
    iWndsLocks = WIN_SuspendWndsLock();
    DefMouseEventProc( mouseStatus, posX, posY, 0, (DWORD)&wme );
    WIN_RestoreWndsLock(iWndsLocks);
    InterlockedIncrement( &X11DRV_MOUSE_WarpPointer );
306
}