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