Added to project. Currently incomplete but will update weekly.
[wine.git] / windows / x11drv / mouse.c
blob1a099172b5cef6f3ef307c0b144c4a1055f33a22
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 */
7 #include "config.h"
9 #include "ts_xlib.h"
11 #include "callback.h"
12 #include "debugtools.h"
13 #include "mouse.h"
14 #include "win.h"
15 #include "windef.h"
16 #include "x11drv.h"
18 DEFAULT_DEBUG_CHANNEL(cursor)
20 /**********************************************************************/
22 Cursor X11DRV_MOUSE_XCursor = None; /* Current X cursor */
24 static LONG X11DRV_MOUSE_WarpPointer = 0; /* hack; see DISPLAY_MoveCursor */
26 /***********************************************************************
27 * X11DRV_MOUSE_DoSetCursor
29 static BOOL X11DRV_MOUSE_DoSetCursor( CURSORICONINFO *ptr )
31 Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
32 XColor fg, bg;
33 Cursor cursor = None;
35 if (!ptr) /* Create an empty cursor */
37 static const char data[] = { 0 };
39 bg.red = bg.green = bg.blue = 0x0000;
40 pixmapBits = XCreateBitmapFromData( display, X11DRV_GetXRootWindow(), data, 1, 1 );
41 if (pixmapBits)
43 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
44 &bg, &bg, 0, 0 );
45 XFreePixmap( display, pixmapBits );
48 else /* Create the X cursor from the bits */
50 XImage *image;
52 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
54 WARN("Cursor has more than 1 bpp!\n" );
55 return FALSE;
58 /* Create a pixmap and transfer all the bits to it */
60 /* NOTE: Following hack works, but only because XFree depth
61 * 1 images really use 1 bit/pixel (and so the same layout
62 * as the Windows cursor data). Perhaps use a more generic
63 * algorithm here.
65 pixmapAll = XCreatePixmap( display, X11DRV_GetXRootWindow(),
66 ptr->nWidth, ptr->nHeight * 2, 1 );
67 image = XCreateImage( display, X11DRV_GetVisual(),
68 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
69 ptr->nHeight * 2, 16, ptr->nWidthBytes);
70 if (image)
72 image->byte_order = MSBFirst;
73 image->bitmap_bit_order = MSBFirst;
74 image->bitmap_unit = 16;
75 _XInitImageFuncPtrs(image);
76 if (pixmapAll)
77 XPutImage( display, pixmapAll, BITMAP_monoGC, image,
78 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
79 image->data = NULL;
80 XDestroyImage( image );
83 /* Now create the 2 pixmaps for bits and mask */
85 pixmapBits = XCreatePixmap( display, X11DRV_GetXRootWindow(),
86 ptr->nWidth, ptr->nHeight, 1 );
87 pixmapMask = XCreatePixmap( display, X11DRV_GetXRootWindow(),
88 ptr->nWidth, ptr->nHeight, 1 );
89 pixmapMaskInv = XCreatePixmap( display, X11DRV_GetXRootWindow(),
90 ptr->nWidth, ptr->nHeight, 1 );
92 /* Make sure everything went OK so far */
94 if (pixmapBits && pixmapMask && pixmapAll)
96 /* We have to do some magic here, as cursors are not fully
97 * compatible between Windows and X11. Under X11, there
98 * are only 3 possible color cursor: black, white and
99 * masked. So we map the 4th Windows color (invert the
100 * bits on the screen) to black and an additional white bit on
101 * an other place (+1,+1). This require some boolean arithmetic:
103 * Windows | X11
104 * And Xor Result | Bits Mask Result
105 * 0 0 black | 0 1 background
106 * 0 1 white | 1 1 foreground
107 * 1 0 no change | X 0 no change
108 * 1 1 inverted | 0 1 background
110 * which gives:
111 * Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
112 * Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
114 * FIXME: apparently some servers do support 'inverted' color.
115 * I don't know if it's correct per the X spec, but maybe
116 * we ought to take advantage of it. -- AJ
118 XSetFunction( display, BITMAP_monoGC, GXcopy );
119 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
120 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
121 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
122 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
123 XCopyArea( display, pixmapAll, pixmapMaskInv, BITMAP_monoGC,
124 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
125 XSetFunction( display, BITMAP_monoGC, GXand );
126 XCopyArea( display, pixmapAll, pixmapMaskInv, BITMAP_monoGC,
127 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
128 XSetFunction( display, BITMAP_monoGC, GXandReverse );
129 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
130 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
131 XSetFunction( display, BITMAP_monoGC, GXorReverse );
132 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
133 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
134 /* Additional white */
135 XSetFunction( display, BITMAP_monoGC, GXor );
136 XCopyArea( display, pixmapMaskInv, pixmapMask, BITMAP_monoGC,
137 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
138 XCopyArea( display, pixmapMaskInv, pixmapBits, BITMAP_monoGC,
139 0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
140 XSetFunction( display, BITMAP_monoGC, GXcopy );
141 fg.red = fg.green = fg.blue = 0xffff;
142 bg.red = bg.green = bg.blue = 0x0000;
143 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
144 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
147 /* Now free everything */
149 if (pixmapAll) XFreePixmap( display, pixmapAll );
150 if (pixmapBits) XFreePixmap( display, pixmapBits );
151 if (pixmapMask) XFreePixmap( display, pixmapMask );
152 if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
155 if (cursor == None) return FALSE;
156 if (X11DRV_MOUSE_XCursor != None) XFreeCursor( display, X11DRV_MOUSE_XCursor );
157 X11DRV_MOUSE_XCursor = cursor;
159 return TRUE;
162 /***********************************************************************
163 * X11DRV_MOUSE_SetCursor
165 void X11DRV_MOUSE_SetCursor( CURSORICONINFO *lpCursor )
167 BOOL success;
169 EnterCriticalSection( &X11DRV_CritSection );
170 success = CALL_LARGE_STACK( X11DRV_MOUSE_DoSetCursor, lpCursor );
171 LeaveCriticalSection( &X11DRV_CritSection );
172 if ( !success ) return;
174 if (X11DRV_GetXRootWindow() != DefaultRootWindow(display))
176 /* If in desktop mode, set the cursor on the desktop window */
178 TSXDefineCursor( display, X11DRV_GetXRootWindow(), X11DRV_MOUSE_XCursor );
180 else
182 /* Else, set the same cursor for all top-level windows */
184 /* FIXME: we should not reference USER internals here, but native USER
185 works only in desktop mode anyway, so this should not matter */
187 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
188 while(hwnd)
190 WND *tmpWnd = WIN_FindWndPtr(hwnd);
191 Window win = X11DRV_WND_FindXWindow(tmpWnd );
192 if (win && win!=DefaultRootWindow(display))
193 TSXDefineCursor( display, win, X11DRV_MOUSE_XCursor );
194 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
195 WIN_ReleaseWndPtr(tmpWnd);
200 /***********************************************************************
201 * X11DRV_MOUSE_MoveCursor
203 void X11DRV_MOUSE_MoveCursor(WORD wAbsX, WORD wAbsY)
206 * We do not want the to create MotionNotify events here,
207 * otherwise we will get an endless recursion:
208 * XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
209 * -> XWarpPointer -> XMotionEvent -> ...
211 * Unfortunately, the XWarpPointer call does create a MotionNotify
212 * event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
213 * procedure, it sets a global flag. If this flag is set, we skip the
214 * XWarpPointer call. If we are *not* called from within MOUSE_SendEvent,
215 * we will call XWarpPointer, which will create a MotionNotify event.
216 * Strictly speaking, this is also wrong, but that should normally not
217 * have any negative effects ...
219 * But first of all, we check whether we already are at the position
220 * are supposed to move to; if so, we don't need to do anything.
223 Window root, child;
224 int rootX, rootY, winX, winY;
225 unsigned int xstate;
227 if (X11DRV_MOUSE_WarpPointer < 0) return;
229 if (!TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
230 &rootX, &rootY, &winX, &winY, &xstate ))
231 return;
233 if ( winX == wAbsX && winY == wAbsY )
234 return;
236 TRACE("(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
238 TSXWarpPointer( display, X11DRV_GetXRootWindow(), X11DRV_GetXRootWindow(),
239 0, 0, 0, 0, wAbsX, wAbsY );
242 /***********************************************************************
243 * X11DRV_MOUSE_EnableWarpPointer
245 LONG X11DRV_MOUSE_EnableWarpPointer(BOOL bEnable)
247 if (bEnable)
248 return InterlockedIncrement( &X11DRV_MOUSE_WarpPointer );
249 else
250 return InterlockedDecrement( &X11DRV_MOUSE_WarpPointer );
253 /***********************************************************************
254 * X11DRV_MOUSE_Init
256 void X11DRV_MOUSE_Init()
258 Window root, child;
259 int root_x, root_y, child_x, child_y;
260 unsigned int KeyState;
262 /* Get the current mouse position and simulate an absolute mouse
263 movement to initialize the mouse global variables */
264 TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
265 &root_x, &root_y, &child_x, &child_y, &KeyState);
267 MOUSE_SendEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
268 root_x, root_y,
269 X11DRV_EVENT_XStateToKeyState(KeyState),
270 GetTickCount(),