Moved more things to the X11 driver.
[wine/multimedia.git] / windows / x11drv / mouse.c
blob6015636c1f88c09cf416539bf1be9fa612d35a2d
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 "debug.h"
14 #include "callback.h"
15 #include "wintypes.h"
16 #include "x11drv.h"
18 /**********************************************************************/
20 Cursor DISPLAY_XCursor = None; /* Current X cursor */
22 BOOL32 DISPLAY_DisableWarpPointer = FALSE; /* hack; see DISPLAY_MoveCursor */
24 /***********************************************************************
25 * X11DRV_MOUSE_DoSetCursor
27 static BOOL32 X11DRV_MOUSE_DoSetCursor( CURSORICONINFO *ptr )
29 Pixmap pixmapBits, pixmapMask, pixmapAll;
30 XColor fg, bg;
31 Cursor cursor = None;
33 if (!ptr) /* Create an empty cursor */
35 static const char data[] = { 0 };
37 bg.red = bg.green = bg.blue = 0x0000;
38 pixmapBits = XCreateBitmapFromData( display, rootWindow, data, 1, 1 );
39 if (pixmapBits)
41 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
42 &bg, &bg, 0, 0 );
43 XFreePixmap( display, pixmapBits );
46 else /* Create the X cursor from the bits */
48 XImage *image;
50 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
52 WARN(cursor, "Cursor has more than 1 bpp!\n" );
53 return FALSE;
56 /* Create a pixmap and transfer all the bits to it */
58 /* NOTE: Following hack works, but only because XFree depth
59 * 1 images really use 1 bit/pixel (and so the same layout
60 * as the Windows cursor data). Perhaps use a more generic
61 * algorithm here.
63 pixmapAll = XCreatePixmap( display, rootWindow,
64 ptr->nWidth, ptr->nHeight * 2, 1 );
65 image = XCreateImage( display, DefaultVisualOfScreen(screen),
66 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
67 ptr->nHeight * 2, 16, ptr->nWidthBytes);
68 if (image)
70 image->byte_order = MSBFirst;
71 image->bitmap_bit_order = MSBFirst;
72 image->bitmap_unit = 16;
73 _XInitImageFuncPtrs(image);
74 if (pixmapAll)
75 XPutImage( display, pixmapAll, BITMAP_monoGC, image,
76 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
77 image->data = NULL;
78 XDestroyImage( image );
81 /* Now create the 2 pixmaps for bits and mask */
83 pixmapBits = XCreatePixmap( display, rootWindow,
84 ptr->nWidth, ptr->nHeight, 1 );
85 pixmapMask = XCreatePixmap( display, rootWindow,
86 ptr->nWidth, ptr->nHeight, 1 );
88 /* Make sure everything went OK so far */
90 if (pixmapBits && pixmapMask && pixmapAll)
92 /* We have to do some magic here, as cursors are not fully
93 * compatible between Windows and X11. Under X11, there
94 * are only 3 possible color cursor: black, white and
95 * masked. So we map the 4th Windows color (invert the
96 * bits on the screen) to black. This require some boolean
97 * arithmetic:
99 * Windows | X11
100 * Xor And Result | Bits Mask Result
101 * 0 0 black | 0 1 background
102 * 0 1 no change | X 0 no change
103 * 1 0 white | 1 1 foreground
104 * 1 1 inverted | 0 1 background
106 * which gives:
107 * Bits = 'Xor' and not 'And'
108 * Mask = 'Xor' or not 'And'
110 * FIXME: apparently some servers do support 'inverted' color.
111 * I don't know if it's correct per the X spec, but maybe
112 * we ought to take advantage of it. -- AJ
114 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
115 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
116 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
117 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
118 XSetFunction( display, BITMAP_monoGC, GXandReverse );
119 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
120 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
121 XSetFunction( display, BITMAP_monoGC, GXorReverse );
122 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
123 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
124 XSetFunction( display, BITMAP_monoGC, GXcopy );
125 fg.red = fg.green = fg.blue = 0xffff;
126 bg.red = bg.green = bg.blue = 0x0000;
127 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
128 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
131 /* Now free everything */
133 if (pixmapAll) XFreePixmap( display, pixmapAll );
134 if (pixmapBits) XFreePixmap( display, pixmapBits );
135 if (pixmapMask) XFreePixmap( display, pixmapMask );
138 if (cursor == None) return FALSE;
139 if (DISPLAY_XCursor != None) XFreeCursor( display, DISPLAY_XCursor );
140 DISPLAY_XCursor = cursor;
142 if (rootWindow != DefaultRootWindow(display) || !WIN_GetDesktop())
144 /* Set the cursor on the desktop window */
145 XDefineCursor( display, rootWindow, cursor );
147 else
149 /* FIXME: this won't work correctly with native USER !*/
151 /* Set the same cursor for all top-level windows */
152 HWND32 hwnd = GetWindow32( GetDesktopWindow32(), GW_CHILD );
153 while(hwnd)
155 Window win = X11DRV_WND_FindXWindow( WIN_FindWndPtr( hwnd ) );
156 if (win && win!=DefaultRootWindow(display))
157 XDefineCursor( display, win, cursor );
158 hwnd = GetWindow32( hwnd, GW_HWNDNEXT );
161 return TRUE;
164 /***********************************************************************
165 * X11DRV_MOUSE_SetCursor
167 void X11DRV_MOUSE_SetCursor( CURSORICONINFO *lpCursor )
169 EnterCriticalSection( &X11DRV_CritSection );
170 CALL_LARGE_STACK( X11DRV_MOUSE_DoSetCursor, lpCursor );
171 LeaveCriticalSection( &X11DRV_CritSection );
174 /***********************************************************************
175 * X11DRV_MOUSE_MoveCursor
177 void X11DRV_MOUSE_MoveCursor(WORD wAbsX, WORD wAbsY)
180 * We do not want the to create MotionNotify events here,
181 * otherwise we will get an endless recursion:
182 * XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
183 * -> XWarpPointer -> XMotionEvent -> ...
185 * Unfortunately, the XWarpPointer call does create a MotionNotify
186 * event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
187 * procedure, it sets a global flag. If this flag is set, we skip the
188 * XWarpPointer call. If we are *not* called from within MOUSE_SendEvent,
189 * we will call XWarpPointer, which will create a MotionNotify event.
190 * Strictly speaking, this is also wrong, but that should normally not
191 * have any negative effects ...
193 * But first of all, we check whether we already are at the position
194 * are supposed to move to; if so, we don't need to do anything.
197 Window root, child;
198 int rootX, rootY, winX, winY;
199 unsigned int xstate;
201 if (DISPLAY_DisableWarpPointer) return;
203 if (!TSXQueryPointer( display, rootWindow, &root, &child,
204 &rootX, &rootY, &winX, &winY, &xstate ))
205 return;
207 if ( winX == wAbsX && winY == wAbsY )
208 return;
210 TRACE( cursor, "(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
212 TSXWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, wAbsX, wAbsY );
215 #endif /* !defined(X_DISPLAY_MISSING) */