Since SetParent now hides/shows, explicitly invalidating DCs in the
[wine/testsucceed.git] / windows / x11drv / mouse.c
blobfa4f8868ef92e97286d12283218bc844d808bef3
1 /*
2 * X11 mouse driver
4 * Copyright 1998 Ulrich Weigand
5 */
7 #include "config.h"
9 #ifndef X_DISPLAY_MISSING
11 #include "ts_xlib.h"
13 #include "callback.h"
14 #include "debug.h"
15 #include "display.h"
16 #include "win.h"
17 #include "wintypes.h"
18 #include "x11drv.h"
20 /**********************************************************************/
22 Cursor X11DRV_MOUSE_XCursor = None; /* Current X cursor */
24 BOOL32 X11DRV_MOUSE_DisableWarpPointer = FALSE; /* hack; see DISPLAY_MoveCursor */
26 /***********************************************************************
27 * X11DRV_MOUSE_DoSetCursor
29 static BOOL32 X11DRV_MOUSE_DoSetCursor( CURSORICONINFO *ptr )
31 Pixmap pixmapBits, pixmapMask, 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, "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, DefaultVisualOfScreen(X11DRV_GetXScreen()),
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 );
90 /* Make sure everything went OK so far */
92 if (pixmapBits && pixmapMask && pixmapAll)
94 /* We have to do some magic here, as cursors are not fully
95 * compatible between Windows and X11. Under X11, there
96 * are only 3 possible color cursor: black, white and
97 * masked. So we map the 4th Windows color (invert the
98 * bits on the screen) to black. This require some boolean
99 * arithmetic:
101 * Windows | X11
102 * Xor And Result | Bits Mask Result
103 * 0 0 black | 0 1 background
104 * 0 1 no change | X 0 no change
105 * 1 0 white | 1 1 foreground
106 * 1 1 inverted | 0 1 background
108 * which gives:
109 * Bits = 'Xor' and not 'And'
110 * Mask = 'Xor' or not 'And'
112 * FIXME: apparently some servers do support 'inverted' color.
113 * I don't know if it's correct per the X spec, but maybe
114 * we ought to take advantage of it. -- AJ
116 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
117 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
118 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
119 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
120 XSetFunction( display, BITMAP_monoGC, GXandReverse );
121 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
122 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
123 XSetFunction( display, BITMAP_monoGC, GXorReverse );
124 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
125 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
126 XSetFunction( display, BITMAP_monoGC, GXcopy );
127 fg.red = fg.green = fg.blue = 0xffff;
128 bg.red = bg.green = bg.blue = 0x0000;
129 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
130 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
133 /* Now free everything */
135 if (pixmapAll) XFreePixmap( display, pixmapAll );
136 if (pixmapBits) XFreePixmap( display, pixmapBits );
137 if (pixmapMask) XFreePixmap( display, pixmapMask );
140 if (cursor == None) return FALSE;
141 if (X11DRV_MOUSE_XCursor != None) XFreeCursor( display, X11DRV_MOUSE_XCursor );
142 X11DRV_MOUSE_XCursor = cursor;
144 if (X11DRV_GetXRootWindow() != DefaultRootWindow(display) || !WIN_GetDesktop())
146 /* Set the cursor on the desktop window */
147 XDefineCursor( display, X11DRV_GetXRootWindow(), cursor );
149 else
151 /* FIXME: this won't work correctly with native USER !*/
153 /* Set the same cursor for all top-level windows */
154 HWND32 hwnd = GetWindow32( GetDesktopWindow32(), GW_CHILD );
155 while(hwnd)
157 Window win = X11DRV_WND_FindXWindow( WIN_FindWndPtr( hwnd ) );
158 if (win && win!=DefaultRootWindow(display))
159 XDefineCursor( display, win, cursor );
160 hwnd = GetWindow32( hwnd, GW_HWNDNEXT );
163 return TRUE;
166 /***********************************************************************
167 * X11DRV_MOUSE_SetCursor
169 void X11DRV_MOUSE_SetCursor( CURSORICONINFO *lpCursor )
171 EnterCriticalSection( &X11DRV_CritSection );
172 CALL_LARGE_STACK( X11DRV_MOUSE_DoSetCursor, lpCursor );
173 LeaveCriticalSection( &X11DRV_CritSection );
176 /***********************************************************************
177 * X11DRV_MOUSE_MoveCursor
179 void X11DRV_MOUSE_MoveCursor(WORD wAbsX, WORD wAbsY)
182 * We do not want the to create MotionNotify events here,
183 * otherwise we will get an endless recursion:
184 * XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
185 * -> XWarpPointer -> XMotionEvent -> ...
187 * Unfortunately, the XWarpPointer call does create a MotionNotify
188 * event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
189 * procedure, it sets a global flag. If this flag is set, we skip the
190 * XWarpPointer call. If we are *not* called from within MOUSE_SendEvent,
191 * we will call XWarpPointer, which will create a MotionNotify event.
192 * Strictly speaking, this is also wrong, but that should normally not
193 * have any negative effects ...
195 * But first of all, we check whether we already are at the position
196 * are supposed to move to; if so, we don't need to do anything.
199 Window root, child;
200 int rootX, rootY, winX, winY;
201 unsigned int xstate;
203 if (X11DRV_MOUSE_DisableWarpPointer) return;
205 if (!TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
206 &rootX, &rootY, &winX, &winY, &xstate ))
207 return;
209 if ( winX == wAbsX && winY == wAbsY )
210 return;
212 TRACE( cursor, "(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
214 TSXWarpPointer( display, X11DRV_GetXRootWindow(), X11DRV_GetXRootWindow(),
215 0, 0, 0, 0, wAbsX, wAbsY );
218 #endif /* !defined(X_DISPLAY_MISSING) */