Release 950403
[wine/multimedia.git] / windows / cursor.c
blobfb13b6d376126be63d8180ded20e66b71cc186f3
1 /*
2 * WINE
3 static char Copyright[] = "Copyright Martin Ayotte, 1993";
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xlib.h>
15 #include "windows.h"
16 #include "win.h"
17 #include "gdi.h"
18 #include "library.h"
19 #include "neexe.h"
20 #include "wine.h"
21 #include "cursor.h"
22 #include "stddebug.h"
23 /* #define DEBUG_CURSOR */
24 /* #define DEBUG_RESOURCE */
25 #include "debug.h"
26 #include "arch.h"
28 static int ShowCursCount = 0;
29 static HCURSOR hActiveCursor;
30 static HCURSOR hEmptyCursor = 0;
31 RECT ClipCursorRect;
33 static struct { SEGPTR name; HCURSOR cursor; } system_cursor[] =
35 { IDC_ARROW, 0 },
36 { IDC_IBEAM, 0 },
37 { IDC_WAIT, 0 },
38 { IDC_CROSS, 0 },
39 { IDC_UPARROW, 0 },
40 { IDC_SIZE, 0 },
41 { IDC_ICON, 0 },
42 { IDC_SIZENWSE, 0 },
43 { IDC_SIZENESW, 0 },
44 { IDC_SIZEWE, 0 },
45 { IDC_SIZENS, 0 }
48 #define NB_SYS_CURSORS (sizeof(system_cursor)/sizeof(system_cursor[0]))
51 /**********************************************************************
52 * LoadCursor [USER.173]
54 HCURSOR LoadCursor(HANDLE instance, SEGPTR cursor_name)
56 HCURSOR hCursor;
57 HANDLE rsc_mem;
58 WORD *lp;
59 LONG *lpl,size;
60 CURSORDESCRIP *lpcurdesc;
61 CURSORALLOC *lpcur;
62 HDC hdc;
63 int i, image_size;
64 unsigned char *cp1,*cp2;
65 dprintf_resource(stddeb,"LoadCursor: instance = %04x, name = %08lx\n",
66 instance, cursor_name);
67 if (!instance)
69 for (i = 0; i < NB_SYS_CURSORS; i++)
70 if (system_cursor[i].name == cursor_name)
72 if (system_cursor[i].cursor) return system_cursor[i].cursor;
73 else break;
75 if (i == NB_SYS_CURSORS) return 0;
77 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
78 if (hCursor == (HCURSOR)NULL) return 0;
79 if (!instance) system_cursor[i].cursor = hCursor;
81 dprintf_cursor(stddeb,"LoadCursor Alloc hCursor=%X\n", hCursor);
82 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
83 memset(lpcur, 0, sizeof(CURSORALLOC));
84 if (instance == (HANDLE)NULL) {
85 switch((LONG)cursor_name) {
86 case IDC_ARROW:
87 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
88 GlobalUnlock(hCursor);
89 return hCursor;
90 case IDC_CROSS:
91 lpcur->xcursor = XCreateFontCursor(display, XC_crosshair);
92 GlobalUnlock(hCursor);
93 return hCursor;
94 case IDC_IBEAM:
95 lpcur->xcursor = XCreateFontCursor(display, XC_xterm);
96 GlobalUnlock(hCursor);
97 return hCursor;
98 case IDC_WAIT:
99 lpcur->xcursor = XCreateFontCursor(display, XC_watch);
100 GlobalUnlock(hCursor);
101 return hCursor;
102 case IDC_SIZENS:
103 lpcur->xcursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
104 GlobalUnlock(hCursor);
105 return hCursor;
106 case IDC_SIZEWE:
107 lpcur->xcursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
108 GlobalUnlock(hCursor);
109 return hCursor;
110 case IDC_SIZENWSE:
111 case IDC_SIZENESW:
112 lpcur->xcursor = XCreateFontCursor(display, XC_fleur);
113 GlobalUnlock(hCursor);
114 return hCursor;
115 default:
116 break;
120 #if 0
121 /* this code replaces all bitmap cursors with the default cursor */
122 lpcur->xcursor = XCreateFontCursor(display, XC_top_left_arrow);
123 GlobalUnlock(hCursor);
124 return hCursor;
125 #endif
127 if (!(hdc = GetDC(0))) return 0;
128 rsc_mem = RSC_LoadResource(instance, cursor_name, NE_RSCTYPE_GROUP_CURSOR,
129 &image_size);
130 if (rsc_mem == (HANDLE)NULL) {
131 fprintf(stderr,"LoadCursor / Cursor %08lx not Found !\n", cursor_name);
132 ReleaseDC(0, hdc);
133 return 0;
135 lp = (WORD *)GlobalLock(rsc_mem);
136 if (lp == NULL) {
137 GlobalFree(rsc_mem);
138 ReleaseDC(0, hdc);
139 return 0;
141 lpcurdesc = (CURSORDESCRIP *)(lp + 3);
142 #if 0
143 dprintf_cursor(stddeb,"LoadCursor / image_size=%d\n", image_size);
144 dprintf_cursor(stddeb,"LoadCursor / curReserved=%X\n", *lp);
145 dprintf_cursor(stddeb,"LoadCursor / curResourceType=%X\n", *(lp + 1));
146 dprintf_cursor(stddeb,"LoadCursor / curResourceCount=%X\n", *(lp + 2));
147 dprintf_cursor(stddeb,"LoadCursor / cursor Width=%d\n",
148 (int)lpcurdesc->Width);
149 dprintf_cursor(stddeb,"LoadCursor / cursor Height=%d\n",
150 (int)lpcurdesc->Height);
151 dprintf_cursor(stddeb,"LoadCursor / cursor curXHotspot=%d\n",
152 (int)lpcurdesc->curXHotspot);
153 dprintf_cursor(stddeb,"LoadCursor / cursor curYHotspot=%d\n",
154 (int)lpcurdesc->curYHotspot);
155 dprintf_cursor(stddeb,"LoadCursor / cursor curDIBSize=%lX\n",
156 (DWORD)lpcurdesc->curDIBSize);
157 dprintf_cursor(stddeb,"LoadCursor / cursor curDIBOffset=%lX\n",
158 (DWORD)lpcurdesc->curDIBOffset);
159 #endif
160 lpcur->descriptor = *lpcurdesc;
161 GlobalUnlock(rsc_mem);
162 GlobalFree(rsc_mem);
163 rsc_mem = RSC_LoadResource(instance,
164 MAKEINTRESOURCE(lpcurdesc->curDIBOffset),
165 NE_RSCTYPE_CURSOR, &image_size);
166 if (rsc_mem == (HANDLE)NULL) {
167 fprintf(stderr,
168 "LoadCursor / Cursor %08lx Bitmap not Found !\n", cursor_name);
169 ReleaseDC(0, hdc);
170 return 0;
172 lpl = (LONG *)GlobalLock(rsc_mem);
173 if (lp == NULL) {
174 GlobalFree(rsc_mem);
175 ReleaseDC(0, hdc);
176 return 0;
178 lpl++;
179 size = CONV_LONG (*lpl);
180 if (size == sizeof(BITMAPCOREHEADER)){
181 CONV_BITMAPCOREHEADER (lpl);
182 ((BITMAPINFOHEADER *)lpl)->biHeight /= 2;
183 lpcur->hBitmap = ConvertCoreBitmap( hdc, (BITMAPCOREHEADER *) lpl );
184 } else if (size == sizeof(BITMAPINFOHEADER)){
185 CONV_BITMAPINFO (lpl);
186 ((BITMAPINFOHEADER *)lpl)->biHeight /= 2;
187 lpcur->hBitmap = ConvertInfoBitmap( hdc, (BITMAPINFO *) lpl );
188 } else {
189 fprintf(stderr,"No bitmap for cursor?\n");
190 lpcur->hBitmap = 0;
192 lpl = (char *)lpl + size + 8;
193 /* This is rather strange! The data is stored *BACKWARDS* and */
194 /* mirrored! But why?? FIXME: the image must be flipped at the Y */
195 /* axis, either here or in CreateCusor(); */
196 size = lpcur->descriptor.Height/2 * ((lpcur->descriptor.Width+7)/8);
197 #if 0
198 dprintf_cursor(stddeb,"Before:\n");
199 for(i=0;i<2*size;i++) {
200 dprintf_cursor(stddeb,"%02x ",((unsigned char *)lpl)[i]);
201 if ((i & 7) == 7) dprintf_cursor(stddeb,"\n");
203 #endif
204 cp1 = (char *)lpl;
205 cp2 = cp1+2*size;
206 for(i = 0; i < size; i++) {
207 char tmp=*--cp2;
208 *cp2 = *cp1;
209 *cp1++ = tmp;
211 #if 0
212 dprintf_cursor(stddeb,"After:\n");
213 for(i=0;i<2*size;i++) {
214 dprintf_cursor(stddeb,"%02x ",((unsigned char *)lpl)[i]);
215 if ((i & 7) == 7) dprintf_cursor(stddeb,"\n");
217 #endif
218 hCursor = CreateCursor(instance, lpcur->descriptor.curXHotspot,
219 lpcur->descriptor.curYHotspot, lpcur->descriptor.Width,
220 lpcur->descriptor.Height/2,
221 (LPSTR)lpl, ((LPSTR)lpl)+size);
223 GlobalUnlock(rsc_mem);
224 GlobalFree(rsc_mem);
225 GlobalUnlock(hCursor);
226 ReleaseDC(0,hdc);
227 return hCursor;
232 /**********************************************************************
233 * CreateCursor [USER.406]
235 HCURSOR CreateCursor(HANDLE instance, short nXhotspot, short nYhotspot,
236 short nWidth, short nHeight, LPSTR lpANDbitPlane, LPSTR lpXORbitPlane)
238 HCURSOR hCursor;
239 CURSORALLOC *lpcur;
240 HDC hdc;
241 int bpllen = (nWidth + 7)/8 * nHeight;
242 char *tmpbpl = malloc(bpllen);
243 int i;
245 XColor bkcolor,fgcolor;
246 Colormap cmap = XDefaultColormap(display,XDefaultScreen(display));
248 dprintf_resource(stddeb,"CreateCursor: inst=%04x nXhotspot=%d nYhotspot=%d nWidth=%d nHeight=%d\n",
249 instance, nXhotspot, nYhotspot, nWidth, nHeight);
250 dprintf_resource(stddeb,"CreateCursor: inst=%04x lpANDbitPlane=%p lpXORbitPlane=%p\n",
251 instance, lpANDbitPlane, lpXORbitPlane);
253 if (!(hdc = GetDC(GetDesktopWindow()))) return 0;
254 hCursor = GlobalAlloc(GMEM_MOVEABLE, sizeof(CURSORALLOC) + 1024L);
255 if (hCursor == (HCURSOR)NULL) {
256 ReleaseDC(GetDesktopWindow(), hdc);
257 return 0;
259 dprintf_cursor(stddeb,"CreateCursor Alloc hCursor=%X\n", hCursor);
260 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
261 memset(lpcur, 0, sizeof(CURSORALLOC));
262 lpcur->descriptor.curXHotspot = nXhotspot;
263 lpcur->descriptor.curYHotspot = nYhotspot;
264 for(i=0; i<bpllen; i++) tmpbpl[i] = ~lpANDbitPlane[i];
265 lpcur->pixmask = XCreatePixmapFromBitmapData(
266 display, DefaultRootWindow(display),
267 tmpbpl, nWidth, nHeight, 1, 0, 1);
268 for(i=0; i<bpllen; i++) tmpbpl[i] ^= lpXORbitPlane[i];
269 lpcur->pixshape = XCreatePixmapFromBitmapData(
270 display, DefaultRootWindow(display),
271 tmpbpl, nWidth, nHeight, 1, 0, 1);
272 XParseColor(display,cmap,"#000000",&fgcolor);
273 XParseColor(display,cmap,"#ffffff",&bkcolor);
274 lpcur->xcursor = XCreatePixmapCursor(display,
275 lpcur->pixshape, lpcur->pixmask,
276 &fgcolor, &bkcolor, lpcur->descriptor.curXHotspot,
277 lpcur->descriptor.curYHotspot);
278 free(tmpbpl);
279 XFreePixmap(display, lpcur->pixshape);
280 XFreePixmap(display, lpcur->pixmask);
281 ReleaseDC(GetDesktopWindow(), hdc);
282 GlobalUnlock(hCursor);
283 return hCursor;
288 /**********************************************************************
289 * DestroyCursor [USER.458]
291 BOOL DestroyCursor(HCURSOR hCursor)
293 CURSORALLOC *lpcur;
294 if (hCursor == (HCURSOR)NULL) return FALSE;
295 lpcur = (CURSORALLOC *)GlobalLock(hCursor);
296 if (lpcur->hBitmap != (HBITMAP)NULL) DeleteObject(lpcur->hBitmap);
297 GlobalUnlock(hCursor);
298 GlobalFree(hCursor);
299 return TRUE;
303 /**********************************************************************
304 * CURSOR_SetCursor
306 * Internal helper function for SetCursor() and ShowCursor().
308 static void CURSOR_SetCursor( HCURSOR hCursor )
310 CURSORALLOC *lpcur;
312 if (!(lpcur = (CURSORALLOC *)GlobalLock(hCursor))) return;
313 if (rootWindow != DefaultRootWindow(display))
315 XDefineCursor( display, rootWindow, lpcur->xcursor );
317 else
319 HWND hwnd = GetWindow( GetDesktopWindow(), GW_CHILD );
320 while(hwnd)
322 Window win = WIN_GetXWindow( hwnd );
323 if (win) XDefineCursor( display, win, lpcur->xcursor );
324 hwnd = GetWindow( hwnd, GW_HWNDNEXT );
327 GlobalUnlock( hCursor );
330 /**********************************************************************
331 * SetCursor [USER.69]
333 HCURSOR SetCursor(HCURSOR hCursor)
335 HCURSOR hOldCursor;
337 dprintf_cursor(stddeb,"SetCursor / hCursor=%04X !\n", hCursor);
338 hOldCursor = hActiveCursor;
339 hActiveCursor = hCursor;
340 if ((hCursor != hOldCursor) || (ShowCursCount < 0))
342 CURSOR_SetCursor( hCursor );
344 ShowCursCount = 0;
345 return hOldCursor;
349 /**********************************************************************
350 * GetCursor [USER.247]
352 HCURSOR GetCursor(void)
354 return hActiveCursor;
358 /**********************************************************************
359 * SetCursorPos [USER.70]
361 void SetCursorPos(short x, short y)
363 dprintf_cursor(stddeb,"SetCursorPos // x=%d y=%d\n", x, y);
364 XWarpPointer( display, None, rootWindow, 0, 0, 0, 0, x, y );
368 /**********************************************************************
369 * GetCursorPos [USER.17]
371 void GetCursorPos(LPPOINT lpRetPoint)
373 Window root, child;
374 int rootX, rootY;
375 int childX, childY;
376 unsigned int mousebut;
378 if (!lpRetPoint) return;
379 if (!XQueryPointer( display, rootWindow, &root, &child,
380 &rootX, &rootY, &childX, &childY, &mousebut ))
381 lpRetPoint->x = lpRetPoint->y = 0;
382 else
384 lpRetPoint->x = rootX + desktopX;
385 lpRetPoint->y = rootY + desktopY;
387 dprintf_cursor(stddeb,
388 "GetCursorPos // x=%d y=%d\n", lpRetPoint->x, lpRetPoint->y);
392 /**********************************************************************
393 * ShowCursor [USER.71]
395 int ShowCursor(BOOL bShow)
397 dprintf_cursor(stddeb, "ShowCursor(%d), count=%d\n", bShow, ShowCursCount);
399 if (bShow)
401 if (++ShowCursCount == 0) /* Time to show it */
402 CURSOR_SetCursor( hActiveCursor );
404 else /* Hide it */
406 if (--ShowCursCount == -1) /* Time to hide it */
408 if (!hEmptyCursor)
409 hEmptyCursor = CreateCursor( 0, 1, 1, 1, 1,
410 "\xFF\xFF", "\xFF\xFF" );
411 CURSOR_SetCursor( hEmptyCursor );
414 return 0;
418 /**********************************************************************
419 * ClipCursor [USER.16]
421 void ClipCursor(LPRECT lpNewClipRect)
423 if (!lpNewClipRect) SetRectEmpty( &ClipCursorRect );
424 else CopyRect( &ClipCursorRect, lpNewClipRect );
428 /**********************************************************************
429 * GetClipCursor [USER.309]
431 void GetClipCursor(LPRECT lpRetClipRect)
433 if (lpRetClipRect != NULL)
434 CopyRect(lpRetClipRect, &ClipCursorRect);