Update shell xxxAW wrapper prototypes for fixed SHLWAPI functions.
[wine] / dlls / x11drv / mouse.c
1 /*
2  * X11 mouse driver
3  *
4  * Copyright 1998 Ulrich Weigand
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "ts_xlib.h"
24 #ifdef HAVE_LIBXXF86DGA2
25 #include "ts_xf86dga2.h"
26 #endif
27
28 #include "windef.h"
29 #include "wine/winuser16.h"
30
31 #include "x11drv.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(cursor);
35
36 /**********************************************************************/
37
38 #define NB_BUTTONS   5     /* Windows can handle 3 buttons and the wheel too */
39
40 static const UINT button_down_flags[NB_BUTTONS] =
41 {
42     MOUSEEVENTF_LEFTDOWN,
43     MOUSEEVENTF_MIDDLEDOWN,
44     MOUSEEVENTF_RIGHTDOWN,
45     MOUSEEVENTF_WHEEL,
46     MOUSEEVENTF_WHEEL
47 };
48
49 static const UINT button_up_flags[NB_BUTTONS] =
50 {
51     MOUSEEVENTF_LEFTUP,
52     MOUSEEVENTF_MIDDLEUP,
53     MOUSEEVENTF_RIGHTUP,
54     0,
55     0
56 };
57
58 static BYTE *pKeyStateTable;
59
60
61 /***********************************************************************
62  *              get_coords
63  *
64  * get the coordinates of a mouse event
65  */
66 static void get_coords( HWND *hwnd, Window window, int x, int y, POINT *pt )
67 {
68     struct x11drv_win_data *data;
69     WND *win;
70
71     if (!(win = WIN_GetPtr( *hwnd )) || win == WND_OTHER_PROCESS) return;
72     data = win->pDriverData;
73
74     if (window == data->whole_window)
75     {
76         x -= data->client_rect.left;
77         y -= data->client_rect.top;
78     }
79     WIN_ReleasePtr( win );
80
81     pt->x = x;
82     pt->y = y;
83     if (*hwnd != GetDesktopWindow())
84     {
85         ClientToScreen( *hwnd, pt );
86         *hwnd = GetAncestor( *hwnd, GA_ROOT );
87     }
88 }
89
90
91 /***********************************************************************
92  *              update_key_state
93  *
94  * Update the key state with what X provides us 
95  */
96 static void update_key_state( unsigned int state )
97 {
98     pKeyStateTable[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
99     pKeyStateTable[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
100     pKeyStateTable[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
101     pKeyStateTable[VK_SHIFT]   = (state & ShiftMask   ? 0x80 : 0);
102     pKeyStateTable[VK_CONTROL] = (state & ControlMask ? 0x80 : 0);
103 }
104
105
106 /***********************************************************************
107  *              send_mouse_event
108  */
109 static void send_mouse_event( HWND hwnd, DWORD flags, DWORD posX, DWORD posY,
110                               DWORD data, Time time )
111 {
112     INPUT input;
113
114     TRACE("(%04lX,%ld,%ld)\n", flags, posX, posY );
115
116     if (flags & MOUSEEVENTF_ABSOLUTE)
117     {
118         int width  = GetSystemMetrics( SM_CXSCREEN );
119         int height = GetSystemMetrics( SM_CYSCREEN );
120         /* Relative mouse movements seem not to be scaled as absolute ones */
121         posX = (((long)posX << 16) + width-1)  / width;
122         posY = (((long)posY << 16) + height-1) / height;
123     }
124
125     input.type             = WINE_INTERNAL_INPUT_MOUSE;
126     input.u.mi.dx          = posX;
127     input.u.mi.dy          = posY;
128     input.u.mi.mouseData   = data;
129     input.u.mi.dwFlags     = flags;
130     input.u.mi.time        = time - X11DRV_server_startticks;
131     input.u.mi.dwExtraInfo = (ULONG_PTR)hwnd;
132     SendInput( 1, &input, sizeof(input) );
133 }
134
135
136 /***********************************************************************
137  *              X11DRV_GetCursor
138  */
139 Cursor X11DRV_GetCursor( Display *display, CURSORICONINFO *ptr )
140 {
141     Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
142     XColor fg, bg;
143     Cursor cursor = None;
144
145     if (!ptr)  /* Create an empty cursor */
146     {
147         static const char data[] = { 0 };
148
149         bg.red = bg.green = bg.blue = 0x0000;
150         pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
151         if (pixmapBits)
152         {
153             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
154                                           &bg, &bg, 0, 0 );
155             XFreePixmap( display, pixmapBits );
156         }
157     }
158     else  /* Create the X cursor from the bits */
159     {
160         XImage *image;
161         GC gc;
162         
163         TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
164             ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
165             ptr->nWidthBytes);
166         /* Create a pixmap and transfer all the bits to it */
167
168         /* NOTE: Following hack works, but only because XFree depth
169          *       1 images really use 1 bit/pixel (and so the same layout
170          *       as the Windows cursor data). Perhaps use a more generic
171          *       algorithm here.
172          */
173         /* This pixmap will be written with two bitmaps. The first is
174          *  the mask and the second is the image.
175          */
176         if (!(pixmapAll = XCreatePixmap( display, root_window,
177                   ptr->nWidth, ptr->nHeight * 2, 1 ))) 
178             return 0;
179         if (!(image = XCreateImage( display, visual,
180                 1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
181                 ptr->nHeight * 2, 16, ptr->nWidthBytes/ptr->bBitsPerPixel))) 
182         {
183             XFreePixmap( display, pixmapAll );
184             return 0;
185         }
186         gc = XCreateGC( display, pixmapAll, 0, NULL );
187         XSetGraphicsExposures( display, gc, False );
188         image->byte_order = MSBFirst;
189         image->bitmap_bit_order = MSBFirst;
190         image->bitmap_unit = 16;
191         _XInitImageFuncPtrs(image);
192         if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
193         {
194             /* A plain old white on black cursor. */
195             fg.red = fg.green = fg.blue = 0xffff;
196             bg.red = bg.green = bg.blue = 0x0000;
197             XPutImage( display, pixmapAll, gc, image,
198                 0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
199         }
200         else
201         {
202             int     rbits, gbits, bbits, red, green, blue;
203             int     rfg, gfg, bfg, rbg, gbg, bbg;
204             int     rscale, gscale, bscale;
205             int     x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
206             unsigned char *theMask, *theImage, theChar;
207             int     threshold, fgBits, bgBits, bitShifted;
208             BYTE    pXorBits[128];   /* Up to 32x32 icons */
209             
210             switch (ptr->bBitsPerPixel)
211             {
212             case 24:
213                 rbits = 8;
214                 gbits = 8;
215                 bbits = 8;
216                 threshold = 0x40;
217                 break;
218             default:
219                 FIXME("Currently no support for cursors with %d bits per pixel\n",
220                   ptr->bBitsPerPixel);
221                 XFreePixmap( display, pixmapAll );
222                 XFreeGC( display, gc );
223                 image->data = NULL;
224                 XDestroyImage( image );
225                 return 0;
226             }
227             /* The location of the mask. */
228             theMask = (char *)(ptr + 1);
229             /* The mask should still be 1 bit per pixel. The color image
230              * should immediately follow the mask. 
231              */
232             theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
233             rfg = gfg = bfg = rbg = gbg = bbg = 0;
234             bitIndex = 0;
235             byteIndex = 0;
236             xorIndex = 0;
237             fgBits = 0;
238             bitShifted = 0x01;
239             xmax = (ptr->nWidth > 32) ? 32 : ptr->nWidth;
240             if (ptr->nWidth > 32) {
241                 ERR("Got a %dx%d cursor. Cannot handle larger than 32x32.\n",
242                   ptr->nWidth, ptr->nHeight);
243             }
244             ymax = (ptr->nHeight > 32) ? 32 : ptr->nHeight;
245             
246             memset(pXorBits, 0, 128);
247             for (y=0; y<ymax; y++)
248             {
249                 for (x=0; x<xmax; x++)
250                 {
251                     theChar = theImage[byteIndex++];
252                     red = green = blue = 0;
253                     blue = theChar;
254                     theChar = theImage[byteIndex++];
255                     green = theChar;
256                     theChar = theImage[byteIndex++];
257                     red = theChar;
258                     if (red+green+blue > threshold)
259                     {
260                         rfg += red;
261                         gfg += green;
262                         bfg += blue;
263                         fgBits++;
264                         pXorBits[xorIndex] |= bitShifted;
265                     }
266                     else
267                     {
268                         rbg += red;
269                         gbg += green;
270                         bbg += blue;
271                     }
272                     if (x%8 == 7)
273                     {
274                         bitShifted = 0x01;
275                         xorIndex++;
276                     }
277                     else
278                         bitShifted = bitShifted << 1;
279                 }
280             }
281             rscale = 1 << (16 - rbits);
282             gscale = 1 << (16 - gbits);
283             bscale = 1 << (16 - bbits);
284             fg.red   = rfg * rscale / fgBits;
285             fg.green = gfg * gscale / fgBits;
286             fg.blue  = bfg * bscale / fgBits;
287             bgBits = xmax * ymax - fgBits;
288             bg.red   = rbg * rscale / bgBits;
289             bg.green = gbg * gscale / bgBits;
290             bg.blue  = bbg * bscale / bgBits;
291             pixmapBits = XCreateBitmapFromData( display, root_window, pXorBits, xmax, ymax );
292             if (!pixmapBits)
293             {
294                 XFreePixmap( display, pixmapAll );
295                 XFreeGC( display, gc );
296                 image->data = NULL;
297                 XDestroyImage( image );
298                 return 0;
299             }
300             
301             /* Put the mask. */
302             XPutImage( display, pixmapAll, gc, image,
303                    0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
304             XSetFunction( display, gc, GXcopy );
305             /* Put the image */
306             XCopyArea( display, pixmapBits, pixmapAll, gc,
307                        0, 0, xmax, ymax, 0, ptr->nHeight );
308             XFreePixmap( display, pixmapBits );
309         }
310         image->data = NULL;
311         XDestroyImage( image );
312
313         /* Now create the 2 pixmaps for bits and mask */
314
315         pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
316         pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
317         pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
318
319         /* Make sure everything went OK so far */
320
321         if (pixmapBits && pixmapMask && pixmapMaskInv)
322         {
323             /* We have to do some magic here, as cursors are not fully
324              * compatible between Windows and X11. Under X11, there
325              * are only 3 possible color cursor: black, white and
326              * masked. So we map the 4th Windows color (invert the
327              * bits on the screen) to black and an additional white bit on 
328              * an other place (+1,+1). This require some boolean arithmetic:
329              *
330              *         Windows          |          X11
331              * And    Xor      Result   |   Bits     Mask     Result
332              *  0      0     black      |    0        1     background
333              *  0      1     white      |    1        1     foreground
334              *  1      0     no change  |    X        0     no change
335              *  1      1     inverted   |    0        1     background
336              *
337              * which gives:
338              *  Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
339              *  Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
340              *
341              * FIXME: apparently some servers do support 'inverted' color.
342              * I don't know if it's correct per the X spec, but maybe
343              * we ought to take advantage of it.  -- AJ
344              */
345             XSetFunction( display, gc, GXcopy );
346             XCopyArea( display, pixmapAll, pixmapBits, gc,
347                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
348             XCopyArea( display, pixmapAll, pixmapMask, gc,
349                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
350             XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
351                        0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
352             XSetFunction( display, gc, GXand );
353             XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
354                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
355             XSetFunction( display, gc, GXandReverse );
356             XCopyArea( display, pixmapAll, pixmapBits, gc,
357                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
358             XSetFunction( display, gc, GXorReverse );
359             XCopyArea( display, pixmapAll, pixmapMask, gc,
360                        0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
361             /* Additional white */
362             XSetFunction( display, gc, GXor );
363             XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
364                        0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
365             XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
366                        0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
367             XSetFunction( display, gc, GXcopy );
368             cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
369                                 &fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
370         }
371
372         /* Now free everything */
373
374         if (pixmapAll) XFreePixmap( display, pixmapAll );
375         if (pixmapBits) XFreePixmap( display, pixmapBits );
376         if (pixmapMask) XFreePixmap( display, pixmapMask );
377         if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
378         XFreeGC( display, gc );
379     }
380     return cursor;
381 }
382
383 /* set the cursor of a window; helper for X11DRV_SetCursor */
384 static BOOL CALLBACK set_win_cursor( HWND hwnd, LPARAM cursor )
385 {
386     Window win = X11DRV_get_whole_window( hwnd );
387     if (win) TSXDefineCursor( thread_display(), win, (Cursor)cursor );
388     return TRUE;
389 }
390
391 /***********************************************************************
392  *              SetCursor (X11DRV.@)
393  */
394 void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
395 {
396     Cursor cursor;
397
398     if (root_window != DefaultRootWindow(gdi_display))
399     {
400         /* If in desktop mode, set the cursor on the desktop window */
401
402         wine_tsx11_lock();
403         cursor = X11DRV_GetCursor( gdi_display, lpCursor );
404         if (cursor)
405         {
406             XDefineCursor( gdi_display, root_window, cursor );
407             XFreeCursor( gdi_display, cursor );
408         }
409         wine_tsx11_unlock();
410     }
411     else /* set the same cursor for all top-level windows of the current thread */
412     {
413         Display *display = thread_display();
414
415         wine_tsx11_lock();
416         cursor = X11DRV_GetCursor( display, lpCursor );
417         wine_tsx11_unlock();
418         if (cursor)
419         {
420 /*            EnumThreadWindows( GetCurrentThreadId(), set_win_cursor, (LPARAM)cursor );*/
421             EnumWindows( set_win_cursor, (LPARAM)cursor );
422             TSXFreeCursor( display, cursor );
423         }
424     }
425 }
426
427 /***********************************************************************
428  *              SetCursorPos (X11DRV.@)
429  */
430 void X11DRV_SetCursorPos( INT x, INT y )
431 {
432     Display *display = thread_display();
433
434     TRACE( "warping to (%d,%d)\n", x, y );
435
436     wine_tsx11_lock();
437     XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
438     XFlush( display ); /* just in case */
439     wine_tsx11_unlock();
440 }
441
442 /***********************************************************************
443  *              GetCursorPos (X11DRV.@)
444  */
445 void X11DRV_GetCursorPos(LPPOINT pos)
446 {
447   Display *display = thread_display();
448   Window root, child;
449   int rootX, rootY, winX, winY;
450   unsigned int xstate;
451
452   if (!TSXQueryPointer( display, root_window, &root, &child,
453                         &rootX, &rootY, &winX, &winY, &xstate ))
454     return;
455
456   TRACE("pointer at (%d,%d)\n", winX, winY );
457   pos->x = winX;
458   pos->y = winY;
459 }
460
461 /***********************************************************************
462  *              InitMouse (X11DRV.@)
463  */
464 void X11DRV_InitMouse( BYTE *key_state_table )
465 {
466     Window root, child;
467     int root_x, root_y, child_x, child_y;
468     unsigned int KeyState;
469
470     pKeyStateTable = key_state_table;
471     /* Get the current mouse position and simulate an absolute mouse
472        movement to initialize the mouse global variables */
473     TSXQueryPointer( thread_display(), root_window, &root, &child,
474                      &root_x, &root_y, &child_x, &child_y, &KeyState);
475     update_key_state( KeyState );
476     send_mouse_event( 0, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
477                       root_x, root_y, 0, GetTickCount() + X11DRV_server_startticks );
478 }
479
480
481 /***********************************************************************
482  *           X11DRV_ButtonPress
483  */
484 void X11DRV_ButtonPress( HWND hwnd, XButtonEvent *event )
485 {
486     int buttonNum = event->button - 1;
487     WORD wData = 0;
488     POINT pt;
489
490     if (buttonNum >= NB_BUTTONS) return;
491
492     get_coords( &hwnd, event->window, event->x, event->y, &pt );
493
494     switch (buttonNum)
495     {
496     case 3:
497         wData = WHEEL_DELTA;
498         break;
499     case 4:
500         wData = -WHEEL_DELTA;
501         break;
502     }
503     update_key_state( event->state );
504     send_mouse_event( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
505                       pt.x, pt.y, wData, event->time );
506 }
507
508
509 /***********************************************************************
510  *           X11DRV_ButtonRelease
511  */
512 void X11DRV_ButtonRelease( HWND hwnd, XButtonEvent *event )
513 {
514     int buttonNum = event->button - 1;
515     POINT pt;
516
517     if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;
518
519     get_coords( &hwnd, event->window, event->x, event->y, &pt );
520     update_key_state( event->state );
521     send_mouse_event( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
522                       pt.x, pt.y, 0, event->time );
523 }
524
525
526 /***********************************************************************
527  *           X11DRV_MotionNotify
528  */
529 void X11DRV_MotionNotify( HWND hwnd, XMotionEvent *event )
530 {
531     POINT pt;
532
533     get_coords( &hwnd, event->window, event->x, event->y, &pt );
534     update_key_state( event->state );
535     send_mouse_event( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
536                       pt.x, pt.y, 0, event->time );
537 }
538
539
540 #ifdef HAVE_LIBXXF86DGA2
541 /**********************************************************************
542  *              X11DRV_DGAMotionEvent
543  */
544 void X11DRV_DGAMotionEvent( HWND hwnd, XDGAMotionEvent *event )
545 {
546     update_key_state( event->state );
547     send_mouse_event( hwnd, MOUSEEVENTF_MOVE, event->dx, event->dy, 0, event->time );
548 }
549
550 /**********************************************************************
551  *              X11DRV_DGAButtonPressEvent
552  */
553 void X11DRV_DGAButtonPressEvent( HWND hwnd, XDGAButtonEvent *event )
554 {
555     int buttonNum = event->button - 1;
556
557     if (buttonNum >= NB_BUTTONS) return;
558     update_key_state( event->state );
559     send_mouse_event( hwnd, button_down_flags[buttonNum], 0, 0, 0, event->time );
560 }
561
562 /**********************************************************************
563  *              X11DRV_DGAButtonReleaseEvent
564  */
565 void X11DRV_DGAButtonReleaseEvent( HWND hwnd, XDGAButtonEvent *event )
566 {
567     int buttonNum = event->button - 1;
568
569     if (buttonNum >= NB_BUTTONS) return;
570     update_key_state( event->state );
571     send_mouse_event( hwnd, button_up_flags[buttonNum], 0, 0, 0, event->time );
572 }
573 #endif /* HAVE_LIBXXF86DGA2 */