Sweden-Number/windows/x11drv/mouse.c

325 lines
12 KiB
C
Raw Normal View History

1998-12-07 10:13:40 +01:00
/*
1998-12-26 13:00:43 +01:00
* X11 mouse driver
1998-12-07 10:13:40 +01:00
*
1998-12-26 13:00:43 +01:00
* Copyright 1998 Ulrich Weigand
1998-12-07 10:13:40 +01:00
*/
#include "config.h"
1998-12-26 13:00:43 +01:00
#include "ts_xlib.h"
2001-01-15 23:30:50 +01:00
#include "windef.h"
#include "wine/winuser16.h"
#include "debugtools.h"
#include "mouse.h"
1999-02-04 12:11:01 +01:00
#include "win.h"
1998-12-07 10:13:40 +01:00
#include "x11drv.h"
2000-06-08 06:57:22 +02:00
DEFAULT_DEBUG_CHANNEL(cursor);
1998-12-26 13:00:43 +01:00
/**********************************************************************/
static LONG X11DRV_MOUSE_WarpPointer = 0; /* hack; see DISPLAY_MoveCursor */
2000-06-08 06:57:22 +02:00
static LPMOUSE_EVENT_PROC DefMouseEventProc = NULL;
1998-12-26 13:00:43 +01:00
/***********************************************************************
* X11DRV_GetCursor
1998-12-26 13:00:43 +01:00
*/
Cursor X11DRV_GetCursor( Display *display, CURSORICONINFO *ptr )
1998-12-26 13:00:43 +01:00
{
Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
1998-12-26 13:00:43 +01:00
XColor fg, bg;
Cursor cursor = None;
if (!ptr) /* Create an empty cursor */
{
static const char data[] = { 0 };
bg.red = bg.green = bg.blue = 0x0000;
pixmapBits = XCreateBitmapFromData( display, root_window, data, 1, 1 );
1998-12-26 13:00:43 +01:00
if (pixmapBits)
{
cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
&bg, &bg, 0, 0 );
XFreePixmap( display, pixmapBits );
}
}
else /* Create the X cursor from the bits */
{
XImage *image;
GC gc;
1998-12-26 13:00:43 +01:00
if (ptr->bPlanes * ptr->bBitsPerPixel != 1)
{
WARN("Cursor has more than 1 bpp!\n" );
return 0;
1998-12-26 13:00:43 +01:00
}
/* Create a pixmap and transfer all the bits to it */
/* NOTE: Following hack works, but only because XFree depth
* 1 images really use 1 bit/pixel (and so the same layout
* as the Windows cursor data). Perhaps use a more generic
* algorithm here.
*/
if (!(pixmapAll = XCreatePixmap( display, root_window,
ptr->nWidth, ptr->nHeight * 2, 1 ))) return 0;
if (!(image = XCreateImage( display, visual,
1, ZPixmap, 0, (char *)(ptr + 1), ptr->nWidth,
ptr->nHeight * 2, 16, ptr->nWidthBytes))) return 0;
gc = XCreateGC( display, pixmapAll, 0, NULL );
XSetGraphicsExposures( display, gc, False );
image->byte_order = MSBFirst;
image->bitmap_bit_order = MSBFirst;
image->bitmap_unit = 16;
_XInitImageFuncPtrs(image);
XPutImage( display, pixmapAll, gc, image,
0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
image->data = NULL;
XDestroyImage( image );
1998-12-26 13:00:43 +01:00
/* Now create the 2 pixmaps for bits and mask */
pixmapBits = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
pixmapMask = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
pixmapMaskInv = XCreatePixmap( display, root_window, ptr->nWidth, ptr->nHeight, 1 );
1998-12-26 13:00:43 +01:00
/* Make sure everything went OK so far */
if (pixmapBits && pixmapMask && pixmapMaskInv)
1998-12-26 13:00:43 +01:00
{
/* We have to do some magic here, as cursors are not fully
* compatible between Windows and X11. Under X11, there
* are only 3 possible color cursor: black, white and
* masked. So we map the 4th Windows color (invert the
* bits on the screen) to black and an additional white bit on
* an other place (+1,+1). This require some boolean arithmetic:
1998-12-26 13:00:43 +01:00
*
* Windows | X11
* And Xor Result | Bits Mask Result
1998-12-26 13:00:43 +01:00
* 0 0 black | 0 1 background
* 0 1 white | 1 1 foreground
* 1 0 no change | X 0 no change
1998-12-26 13:00:43 +01:00
* 1 1 inverted | 0 1 background
*
* which gives:
* Bits = not 'And' and 'Xor' or 'And2' and 'Xor2'
* Mask = not 'And' or 'Xor' or 'And2' and 'Xor2'
1998-12-26 13:00:43 +01:00
*
* FIXME: apparently some servers do support 'inverted' color.
* I don't know if it's correct per the X spec, but maybe
* we ought to take advantage of it. -- AJ
*/
XSetFunction( display, gc, GXcopy );
XCopyArea( display, pixmapAll, pixmapBits, gc,
1998-12-26 13:00:43 +01:00
0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
XCopyArea( display, pixmapAll, pixmapMask, gc,
1998-12-26 13:00:43 +01:00
0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
XSetFunction( display, gc, GXand );
XCopyArea( display, pixmapAll, pixmapMaskInv, gc,
0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
XSetFunction( display, gc, GXandReverse );
XCopyArea( display, pixmapAll, pixmapBits, gc,
1998-12-26 13:00:43 +01:00
0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
XSetFunction( display, gc, GXorReverse );
XCopyArea( display, pixmapAll, pixmapMask, gc,
1998-12-26 13:00:43 +01:00
0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
/* Additional white */
XSetFunction( display, gc, GXor );
XCopyArea( display, pixmapMaskInv, pixmapMask, gc,
0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
XCopyArea( display, pixmapMaskInv, pixmapBits, gc,
0, 0, ptr->nWidth, ptr->nHeight, 1, 1 );
XSetFunction( display, gc, GXcopy );
1998-12-26 13:00:43 +01:00
fg.red = fg.green = fg.blue = 0xffff;
bg.red = bg.green = bg.blue = 0x0000;
cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
&fg, &bg, ptr->ptHotSpot.x, ptr->ptHotSpot.y );
}
/* Now free everything */
if (pixmapAll) XFreePixmap( display, pixmapAll );
if (pixmapBits) XFreePixmap( display, pixmapBits );
if (pixmapMask) XFreePixmap( display, pixmapMask );
if (pixmapMaskInv) XFreePixmap( display, pixmapMaskInv );
XFreeGC( display, gc );
1998-12-26 13:00:43 +01:00
}
return cursor;
}
1998-12-26 13:00:43 +01:00
/* set the cursor of a window; helper for X11DRV_SetCursor */
static BOOL CALLBACK set_win_cursor( HWND hwnd, LPARAM cursor )
{
Window win = X11DRV_get_whole_window( hwnd );
if (win) TSXDefineCursor( thread_display(), win, (Cursor)cursor );
return TRUE;
}
/***********************************************************************
* SetCursor (X11DRV.@)
*/
void X11DRV_SetCursor( CURSORICONINFO *lpCursor )
{
Cursor cursor;
if (root_window != DefaultRootWindow(gdi_display))
1998-12-26 13:00:43 +01:00
{
/* If in desktop mode, set the cursor on the desktop window */
wine_tsx11_lock();
cursor = X11DRV_GetCursor( gdi_display, lpCursor );
if (cursor)
{
XDefineCursor( gdi_display, root_window, cursor );
XFreeCursor( gdi_display, cursor );
}
wine_tsx11_unlock();
1998-12-26 13:00:43 +01:00
}
else /* set the same cursor for all top-level windows of the current thread */
1998-12-26 13:00:43 +01:00
{
Display *display = thread_display();
wine_tsx11_lock();
cursor = X11DRV_GetCursor( display, lpCursor );
wine_tsx11_unlock();
if (cursor)
1998-12-26 13:00:43 +01:00
{
/* EnumThreadWindows( GetCurrentThreadId(), set_win_cursor, (LPARAM)cursor );*/
EnumWindows( set_win_cursor, (LPARAM)cursor );
TSXFreeCursor( display, cursor );
1998-12-26 13:00:43 +01:00
}
}
}
/***********************************************************************
2001-06-21 02:44:09 +02:00
* SetCursorPos (X11DRV.@)
1998-12-26 13:00:43 +01:00
*/
2001-06-21 02:44:09 +02:00
void X11DRV_SetCursorPos(INT wAbsX, INT wAbsY)
1998-12-26 13:00:43 +01:00
{
/*
* We do not want to create MotionNotify events here,
1998-12-26 13:00:43 +01:00
* otherwise we will get an endless recursion:
* XMotionEvent -> MOUSEEVENTF_MOVE -> mouse_event -> DisplayMoveCursor
* -> XWarpPointer -> XMotionEvent -> ...
*
* Unfortunately, the XWarpPointer call does create a MotionNotify
* event. So, we use a hack: before MOUSE_SendEvent calls the mouse event
* procedure, it sets a global flag. If this flag is set, we skip the
* XWarpPointer call. If we are *not* called from within MOUSE_SendEvent,
* we will call XWarpPointer, which will create a MotionNotify event.
* Strictly speaking, this is also wrong, but that should normally not
* have any negative effects ...
*
* But first of all, we check whether we already are at the position
* are supposed to move to; if so, we don't need to do anything.
*/
2001-06-21 02:44:09 +02:00
Display *display = thread_display();
1998-12-26 13:00:43 +01:00
Window root, child;
int rootX, rootY, winX, winY;
unsigned int xstate;
if (X11DRV_MOUSE_WarpPointer < 0) return;
1998-12-26 13:00:43 +01:00
if (!TSXQueryPointer( display, root_window, &root, &child,
1998-12-26 13:00:43 +01:00
&rootX, &rootY, &winX, &winY, &xstate ))
return;
if ( winX == wAbsX && winY == wAbsY )
return;
TRACE("(%d,%d): moving from (%d,%d)\n", wAbsX, wAbsY, winX, winY );
2001-06-21 02:44:09 +02:00
wine_tsx11_lock();
XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, wAbsX, wAbsY );
XFlush( display ); /* just in case */
wine_tsx11_unlock();
}
/***********************************************************************
* GetCursorPos (X11DRV.@)
*/
void X11DRV_GetCursorPos(LPPOINT pos)
{
Display *display = thread_display();
Window root, child;
int rootX, rootY, winX, winY;
unsigned int xstate;
if (!TSXQueryPointer( display, root_window, &root, &child,
&rootX, &rootY, &winX, &winY, &xstate ))
return;
TRACE("pointer at (%d,%d)\n", winX, winY );
pos->x = winX;
pos->y = winY;
1998-12-26 13:00:43 +01:00
}
/***********************************************************************
* InitMouse (X11DRV.@)
*/
void X11DRV_InitMouse( LPMOUSE_EVENT_PROC proc )
{
2000-06-08 06:57:22 +02:00
static int init_done;
DefMouseEventProc = proc;
if (!init_done)
{
Window root, child;
int root_x, root_y, child_x, child_y;
unsigned int KeyState;
init_done = 1;
/* Get the current mouse position and simulate an absolute mouse
movement to initialize the mouse global variables */
TSXQueryPointer( thread_display(), root_window, &root, &child,
2000-06-08 06:57:22 +02:00
&root_x, &root_y, &child_x, &child_y, &KeyState);
X11DRV_SendEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
2001-06-25 22:01:03 +02:00
root_x, root_y, X11DRV_EVENT_XStateToKeyState(KeyState),
0, GetTickCount(), 0 );
2000-06-08 06:57:22 +02:00
}
}
2000-06-08 06:57:22 +02:00
/***********************************************************************
2000-12-22 02:38:01 +01:00
* X11DRV_SendEvent (internal)
*/
2001-06-25 22:01:03 +02:00
void X11DRV_SendEvent( DWORD mouseStatus, DWORD posX, DWORD posY,
WORD keyState, DWORD data, DWORD time, HWND hWnd )
{
int width = GetSystemMetrics( SM_CXSCREEN );
int height = GetSystemMetrics( SM_CYSCREEN );
2000-06-08 06:57:22 +02:00
int iWndsLocks;
WINE_MOUSEEVENT wme;
if ( !DefMouseEventProc ) return;
TRACE("(%04lX,%ld,%ld)\n", mouseStatus, posX, posY );
if (mouseStatus & MOUSEEVENTF_MOVE) {
if (mouseStatus & MOUSEEVENTF_ABSOLUTE) {
/* Relative mouse movements seems not to be scaled as absolute ones */
posX = (((long)posX << 16) + width-1) / width;
posY = (((long)posY << 16) + height-1) / height;
}
}
wme.magic = WINE_MOUSEEVENT_MAGIC;
wme.time = time;
wme.hWnd = hWnd;
wme.keyState = keyState;
InterlockedDecrement( &X11DRV_MOUSE_WarpPointer );
/* To avoid deadlocks, we have to suspend all locks on windows structures
before the program control is passed to the mouse driver */
iWndsLocks = WIN_SuspendWndsLock();
2001-06-25 22:01:03 +02:00
DefMouseEventProc( mouseStatus, posX, posY, data, (DWORD)&wme );
2000-06-08 06:57:22 +02:00
WIN_RestoreWndsLock(iWndsLocks);
InterlockedIncrement( &X11DRV_MOUSE_WarpPointer );
}