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