Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbejy@wpi.edu>
[wine/multimedia.git] / windows / x11drv / mouse.c
blob3fcfa0e9f3c522176f3b9d8ac2d72bddd3499604
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 "mouse.h"
16 #include "win.h"
17 #include "windef.h"
18 #include "x11drv.h"
20 DEFAULT_DEBUG_CHANNEL(cursor)
22 /**********************************************************************/
24 Cursor X11DRV_MOUSE_XCursor = None; /* Current X cursor */
26 static BOOL X11DRV_MOUSE_WarpPointer = TRUE; /* hack; see DISPLAY_MoveCursor */
28 /***********************************************************************
29 * X11DRV_MOUSE_DoSetCursor
31 static BOOL X11DRV_MOUSE_DoSetCursor( CURSORICONINFO *ptr )
33 Pixmap pixmapBits, pixmapMask, pixmapAll;
34 XColor fg, bg;
35 Cursor cursor = None;
37 if (!ptr) /* Create an empty cursor */
39 static const char data[] = { 0 };
41 bg.red = bg.green = bg.blue = 0x0000;
42 pixmapBits = XCreateBitmapFromData( display, X11DRV_GetXRootWindow(), data, 1, 1 );
43 if (pixmapBits)
45 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
46 &bg, &bg, 0, 0 );
47 XFreePixmap( display, pixmapBits );
50 else /* Create the X cursor from the bits */
52 XImage *image;
54 if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
56 WARN(cursor, "Cursor has more than 1 bpp!\n" );
57 return FALSE;
60 /* Create a pixmap and transfer all the bits to it */
62 /* NOTE: Following hack works, but only because XFree depth
63 * 1 images really use 1 bit/pixel (and so the same layout
64 * as the Windows cursor data). Perhaps use a more generic
65 * algorithm here.
67 pixmapAll = XCreatePixmap( display, X11DRV_GetXRootWindow(),
68 ptr->nWidth, ptr->nHeight * 2, 1 );
69 image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
70 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
71 ptr->nHeight * 2, 16, ptr->nWidthBytes);
72 if (image)
74 image->byte_order = MSBFirst;
75 image->bitmap_bit_order = MSBFirst;
76 image->bitmap_unit = 16;
77 _XInitImageFuncPtrs(image);
78 if (pixmapAll)
79 XPutImage( display, pixmapAll, BITMAP_monoGC, image,
80 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
81 image->data = NULL;
82 XDestroyImage( image );
85 /* Now create the 2 pixmaps for bits and mask */
87 pixmapBits = XCreatePixmap( display, X11DRV_GetXRootWindow(),
88 ptr->nWidth, ptr->nHeight, 1 );
89 pixmapMask = 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. This require some boolean
101 * arithmetic:
103 * Windows | X11
104 * Xor And Result | Bits Mask Result
105 * 0 0 black | 0 1 background
106 * 0 1 no change | X 0 no change
107 * 1 0 white | 1 1 foreground
108 * 1 1 inverted | 0 1 background
110 * which gives:
111 * Bits = 'Xor' and not 'And'
112 * Mask = 'Xor' or not 'And'
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 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
119 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
120 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
121 0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
122 XSetFunction( display, BITMAP_monoGC, GXandReverse );
123 XCopyArea( display, pixmapAll, pixmapBits, BITMAP_monoGC,
124 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
125 XSetFunction( display, BITMAP_monoGC, GXorReverse );
126 XCopyArea( display, pixmapAll, pixmapMask, BITMAP_monoGC,
127 0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
128 XSetFunction( display, BITMAP_monoGC, GXcopy );
129 fg.red = fg.green = fg.blue = 0xffff;
130 bg.red = bg.green = bg.blue = 0x0000;
131 cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
132 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
135 /* Now free everything */
137 if (pixmapAll) XFreePixmap( display, pixmapAll );
138 if (pixmapBits) XFreePixmap( display, pixmapBits );
139 if (pixmapMask) XFreePixmap( display, pixmapMask );
142 if (cursor == None) return FALSE;
143 if (X11DRV_MOUSE_XCursor != None) XFreeCursor( display, X11DRV_MOUSE_XCursor );
144 X11DRV_MOUSE_XCursor = cursor;
146 if (X11DRV_GetXRootWindow() != DefaultRootWindow(display) || !WIN_GetDesktop())
148 /* Set the cursor on the desktop window */
149 XDefineCursor( display, X11DRV_GetXRootWindow(), cursor );
151 else
153 /* FIXME: this won't work correctly with native USER !*/
155 /* Set the same cursor for all top-level windows */
156 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
157 while(hwnd)
159 WND *tmpWnd = WIN_FindWndPtr(hwnd);
160 Window win = X11DRV_WND_FindXWindow(tmpWnd );
161 if (win && win!=DefaultRootWindow(display))
162 XDefineCursor( display, win, cursor );
163 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
164 WIN_ReleaseWndPtr(tmpWnd);
167 WIN_ReleaseDesktop();
168 return TRUE;
171 /***********************************************************************
172 * X11DRV_MOUSE_SetCursor
174 void X11DRV_MOUSE_SetCursor( CURSORICONINFO *lpCursor )
176 EnterCriticalSection( &X11DRV_CritSection );
177 CALL_LARGE_STACK( X11DRV_MOUSE_DoSetCursor, lpCursor );
178 LeaveCriticalSection( &X11DRV_CritSection );
181 /***********************************************************************
182 * X11DRV_MOUSE_MoveCursor
184 void X11DRV_MOUSE_MoveCursor(WORD wAbsX, WORD wAbsY)
187 * We do not want the to create MotionNotify events here,
188 * otherwise we will get an endless recursion:
189 * XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
190 * -> XWarpPointer -> XMotionEvent -> ...
192 * Unfortunately, the XWarpPointer call does create a MotionNotify
193 * event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
194 * procedure, it sets a global flag. If this flag is set, we skip the
195 * XWarpPointer call. If we are *not* called from within MOUSE_SendEvent,
196 * we will call XWarpPointer, which will create a MotionNotify event.
197 * Strictly speaking, this is also wrong, but that should normally not
198 * have any negative effects ...
200 * But first of all, we check whether we already are at the position
201 * are supposed to move to; if so, we don't need to do anything.
204 Window root, child;
205 int rootX, rootY, winX, winY;
206 unsigned int xstate;
208 if (!X11DRV_MOUSE_WarpPointer) return;
210 if (!TSXQueryPointer( display, X11DRV_GetXRootWindow(), &root, &child,
211 &rootX, &rootY, &winX, &winY, &xstate ))
212 return;
214 if ( winX == wAbsX && winY == wAbsY )
215 return;
217 TRACE( cursor, "(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
219 TSXWarpPointer( display, X11DRV_GetXRootWindow(), X11DRV_GetXRootWindow(),
220 0, 0, 0, 0, wAbsX, wAbsY );
223 /***********************************************************************
224 * X11DRV_MOUSE_EnableWarpPointer
226 BOOL X11DRV_MOUSE_EnableWarpPointer(BOOL bEnable)
228 BOOL bOldEnable = X11DRV_MOUSE_WarpPointer;
230 X11DRV_MOUSE_WarpPointer = bEnable;
232 return bOldEnable;
235 #endif /* !defined(X_DISPLAY_MISSING) */