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