/*
 * X11 mouse driver
 *
 * Copyright 1998 Ulrich Weigand
 */

#include "config.h"

#include "ts_xlib.h"
#ifdef HAVE_LIBXXF86DGA2
#include "ts_xf86dga2.h"
#endif

#include "windef.h"
#include "wine/winuser16.h"

#include "x11drv.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(cursor);

extern Window X11DRV_DD_PrimaryFrame;
extern void X11DRV_KeyStateNotify( HWND hwnd, Time evtime, unsigned int state );

int X11DRV_NoCursor;

/**********************************************************************/

#define NB_BUTTONS   7     /* Windows can handle 3 buttons and the wheel too */

static const UINT button_down_flags[NB_BUTTONS] =
{
    MOUSEEVENTF_LEFTDOWN,
    MOUSEEVENTF_MIDDLEDOWN,
    MOUSEEVENTF_RIGHTDOWN,
    MOUSEEVENTF_WHEEL,
    MOUSEEVENTF_WHEEL,
    MOUSEEVENTF_XDOWN,
    MOUSEEVENTF_XDOWN
};

static const UINT button_up_flags[NB_BUTTONS] =
{
    MOUSEEVENTF_LEFTUP,
    MOUSEEVENTF_MIDDLEUP,
    MOUSEEVENTF_RIGHTUP,
    0,
    0,
    MOUSEEVENTF_XUP,
    MOUSEEVENTF_XUP
};

static const UINT button_data[NB_BUTTONS] =
{
    0,
    0,
    0,
    WHEEL_DELTA,
    -WHEEL_DELTA,
    XBUTTON1,
    XBUTTON2
};

static BYTE *pKeyStateTable;


/***********************************************************************
 *		get_coords
 *
 * get the coordinates of a mouse event
 */
static void get_coords( HWND *hwnd, Window window, int x, int y, POINT *pt )
{
    struct x11drv_win_data *data;
    WND *win;

    if (window == root_window && !*hwnd)
    {
        /* occurs in DirectDraw full-screen modes */
        pt->x = x;
        pt->y = y;
        *hwnd = GetDesktopWindow();
        return;
    }

    if (!(win = WIN_GetPtr( *hwnd )) || win == WND_OTHER_PROCESS) return;
    data = win->pDriverData;

    if (window == data->whole_window)
    {
        x -= data->client_rect.left;
        y -= data->client_rect.top;
    }
    WIN_ReleasePtr( win );

    pt->x = x;
    pt->y = y;
    if (*hwnd != GetDesktopWindow())
    {
        ClientToScreen( *hwnd, pt );
        *hwnd = GetAncestor( *hwnd, GA_ROOT );
    }
}


/***********************************************************************
 *		update_key_state
 *
 * Update the key state with what X provides us
 */
static void update_key_state( HWND hwnd, Time evtime, unsigned int state )
{
    pKeyStateTable[VK_LBUTTON] = (state & Button1Mask ? 0x80 : 0);
    pKeyStateTable[VK_MBUTTON] = (state & Button2Mask ? 0x80 : 0);
    pKeyStateTable[VK_RBUTTON] = (state & Button3Mask ? 0x80 : 0);
    pKeyStateTable[VK_SHIFT]   = (state & ShiftMask   ? 0x80 : 0);
    pKeyStateTable[VK_CONTROL] = (state & ControlMask ? 0x80 : 0);
    X11DRV_KeyStateNotify( hwnd, evtime, state );
}


/***********************************************************************
 *		send_mouse_event
 */
static void send_mouse_event( HWND hwnd, DWORD flags, DWORD posX, DWORD posY,
                              DWORD data, Time time )
{
    INPUT input;

    TRACE("(%04lX,%ld,%ld)\n", flags, posX, posY );

    if (flags & MOUSEEVENTF_ABSOLUTE)
    {
        int width  = GetSystemMetrics( SM_CXSCREEN );
        int height = GetSystemMetrics( SM_CYSCREEN );
        /* Relative mouse movements seem not to be scaled as absolute ones */
        posX = (((long)posX << 16) + width-1)  / width;
        posY = (((long)posY << 16) + height-1) / height;
    }

    input.type             = WINE_INTERNAL_INPUT_MOUSE;
    input.u.mi.dx          = posX;
    input.u.mi.dy          = posY;
    input.u.mi.mouseData   = data;
    input.u.mi.dwFlags     = flags;
    input.u.mi.time        = (time == CurrentTime) ? GetTickCount() : X11DRV_event_time(time);
    input.u.mi.dwExtraInfo = (ULONG_PTR)hwnd;
    SendInput( 1, &input, sizeof(input) );
}


/***********************************************************************
 *		X11DRV_GetCursor
 */
Cursor X11DRV_GetCursor( Display *display, CURSORICONINFO *ptr )
{
    Pixmap pixmapBits, pixmapMask, pixmapMaskInv, pixmapAll;
    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 );
        if (pixmapBits)
        {
            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapBits,
                                          &bg, &bg, 0, 0 );
            XFreePixmap( display, pixmapBits );
        }
    }
    else if (((ptr->bBitsPerPixel * ptr->bPlanes) > 1) &&
             X11DRV_XRender_Cursor &&
             (cursor = X11DRV_XRender_CreateCursor(display, ptr)))
    {
        /* nothing more to do */
    }
    else  /* Create the X cursor from the bits */
    {
        XImage *image;
        int hotx, hoty;
        GC gc;

        TRACE("Bitmap %dx%d planes=%d bpp=%d bytesperline=%d\n",
            ptr->nWidth, ptr->nHeight, ptr->bPlanes, ptr->bBitsPerPixel,
            ptr->nWidthBytes);
        /* 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.
         */
        /* This pixmap will be written with two bitmaps. The first is
         *  the mask and the second is the image.
         */
        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/ptr->bBitsPerPixel)))
        {
            XFreePixmap( display, pixmapAll );
            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);
        if (ptr->bPlanes * ptr->bBitsPerPixel == 1)
        {
            /* A plain old white on black cursor. */
            fg.red = fg.green = fg.blue = 0xffff;
            bg.red = bg.green = bg.blue = 0x0000;
            XPutImage( display, pixmapAll, gc, image,
                0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
        }
        else
        {
            int     rbits, gbits, bbits, red, green, blue;
            int     rfg, gfg, bfg, rbg, gbg, bbg;
            int     rscale, gscale, bscale;
            int     x, y, xmax, ymax, bitIndex, byteIndex, xorIndex;
            unsigned char *theMask, *theImage;
            RGBQUAD *thePal;
            DWORD   scolor;
            int     threshold, fgBits, bgBits, bitShifted, bitRShifted;
            BYTE    *pXorBits;

            switch (ptr->bBitsPerPixel)
            {
            case 8:
            case 15:
            case 16:
            case 24:
            case 32:
                rbits = 8;
                gbits = 8;
                bbits = 8;
                threshold = 0x40;
                break;
            default:
                FIXME("Currently no support for cursors with %d bits per pixel\n",
                  ptr->bBitsPerPixel);
                XFreePixmap( display, pixmapAll );
                XFreeGC( display, gc );
                image->data = NULL;
                XDestroyImage( image );
                return 0;
            }
            /* The location of the mask. */
            theMask = (char *)(ptr + 1);
            /* The mask should still be 1 bit per pixel. The color image
             * should immediately follow the mask.
             */
            theImage = &theMask[ptr->nWidth/8 * ptr->nHeight];
            thePal = (RGBQUAD*)&theImage[ptr->nWidthBytes * ptr->nHeight];
            rfg = gfg = bfg = rbg = gbg = bbg = 0;
            bitIndex = 0;
            byteIndex = 0;
            xorIndex = 0;
            fgBits = 0;
            bgBits = 0;
            bitShifted = 0x01;
            bitRShifted = 0x80;
            if (ptr->nWidth > 32 || ptr->nHeight > 32) {
                unsigned int width_max, height_max;
                XQueryBestCursor( display, root_window, ptr->nWidth, ptr->nHeight, &width_max, &height_max);
                TRACE("Got a %dx%d cursor. XQueryBestCursor says to use %dx%d.\n",
                      ptr->nWidth, ptr->nHeight, width_max, height_max);
                xmax = (ptr->nWidth > width_max) ? width_max : ptr->nWidth;
                ymax = (ptr->nHeight > height_max) ? height_max : ptr->nHeight;
            } else {
                xmax = ptr->nWidth;
                ymax = ptr->nHeight;
	    }

            pXorBits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, xmax*ymax/8);
            for (y=0; y<ymax; y++)
            {
                for (x=0; x<xmax; x++)
                {
                    red = green = blue = 0;
                    switch (ptr->bBitsPerPixel) {
                    case 8:
                        scolor = theImage[byteIndex++];
                        blue = thePal[scolor].rgbBlue;
                        green = thePal[scolor].rgbGreen;
                        red = thePal[scolor].rgbRed;
                        break;
                    case 15:
                        scolor = theImage[byteIndex++];
                        scolor |= theImage[byteIndex++] << 8;
                        blue = ((scolor&0x001f)<<3) | ((scolor&0x001c)>>2);
                        green = ((scolor&0x03e0)>>2) | ((scolor&0x0380)>>7);
                        red  = ((scolor&0x7c00)>>7) | ((scolor&0x7000)>>12);
                        break;
                    case 16:
                        scolor = theImage[byteIndex++];
                        scolor |= theImage[byteIndex++] << 8;
                        blue = ((scolor&0x001f)<<3) | ((scolor&0x001c)>>2);
                        green = ((scolor&0x07e0)>>1) | ((scolor&0x0380)>>7);
                        red  = ((scolor&0xf800)>>8) | ((scolor&0x7000)>>13);
                        break;
                    case 24:
                        blue = theImage[byteIndex++];
                        green = theImage[byteIndex++];
                        red = theImage[byteIndex++];
                        break;
                    case 32:
                        blue = theImage[byteIndex++];
                        green = theImage[byteIndex++];
                        red = theImage[byteIndex++];
                        byteIndex++;
                        break;
                    }
                    if (!(theMask[xorIndex] & bitRShifted)) {
                        if (red+green+blue > threshold)
                        {
                            rfg += red;
                            gfg += green;
                            bfg += blue;
                            fgBits++;
                            pXorBits[xorIndex] |= bitShifted;
                        }
                        else
                        {
                            rbg += red;
                            gbg += green;
                            bbg += blue;
                            bgBits++;
                        }
                    }
                    if (x%8 == 7)
                    {
                        bitShifted = 0x01;
                        bitRShifted = 0x80;
                        xorIndex++;
                    }
                    else
                    {
                        bitShifted = bitShifted << 1;
                        bitRShifted = bitRShifted >> 1;
                    }
                }
            }
            rscale = 1 << (16 - rbits);
            gscale = 1 << (16 - gbits);
            bscale = 1 << (16 - bbits);
            if( fgBits ) {
              fg.red   = rfg * rscale / fgBits;
              fg.green = gfg * gscale / fgBits;
              fg.blue  = bfg * bscale / fgBits;
            } else {
              fg.red = fg.green = fg.blue = 0;
            }
            if( bgBits ) {
              bg.red   = rbg * rscale / bgBits;
              bg.green = gbg * gscale / bgBits;
              bg.blue  = bbg * bscale / bgBits;
            } else {
              bg.red = bg.green = bg.blue = 0;
            }
            pixmapBits = XCreateBitmapFromData( display, root_window, pXorBits, xmax, ymax );
            HeapFree( GetProcessHeap(), 0, pXorBits );
            if (!pixmapBits)
            {
                XFreePixmap( display, pixmapAll );
                XFreeGC( display, gc );
                image->data = NULL;
                XDestroyImage( image );
                return 0;
            }

            /* Put the mask. */
            XPutImage( display, pixmapAll, gc, image,
                   0, 0, 0, 0, ptr->nWidth, ptr->nHeight );
            XSetFunction( display, gc, GXcopy );
            /* Put the image */
            XCopyArea( display, pixmapBits, pixmapAll, gc,
                       0, 0, xmax, ymax, 0, ptr->nHeight );
            XFreePixmap( display, pixmapBits );
        }
        image->data = NULL;
        XDestroyImage( image );

        /* 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 );

        /* Make sure everything went OK so far */

        if (pixmapBits && pixmapMask && pixmapMaskInv)
        {
            /* 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:
             *
             *         Windows          |          X11
             * And    Xor      Result   |   Bits     Mask     Result
             *  0      0     black      |    0        1     background
             *  0      1     white      |    1        1     foreground
             *  1      0     no change  |    X        0     no change
             *  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'
             *
             * 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,
                       0, 0, ptr->nWidth, ptr->nHeight, 0, 0 );
            XCopyArea( display, pixmapAll, pixmapMask, gc,
                       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,
                       0, ptr->nHeight, ptr->nWidth, ptr->nHeight, 0, 0 );
            XSetFunction( display, gc, GXorReverse );
            XCopyArea( display, pixmapAll, pixmapMask, gc,
                       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 );

            hotx = ptr->ptHotSpot.x;
            if (hotx < 0) hotx = 0;
            if (hotx > ptr->nWidth) hotx = ptr->nWidth;
            hoty = ptr->ptHotSpot.y;
            if (hoty < 0) hoty = 0;
            if (hoty > ptr->nHeight) hoty = ptr->nHeight;

            cursor = XCreatePixmapCursor( display, pixmapBits, pixmapMask,
                                &fg, &bg, hotx, hoty );
        }

        /* 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 );
    }
    return cursor;
}

/* 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;

    X11DRV_NoCursor = (lpCursor == NULL);

    if (root_window != DefaultRootWindow(gdi_display))
    {
        /* If in desktop mode, set the cursor on the desktop window */

        wine_tsx11_lock();
        cursor = X11DRV_GetCursor( gdi_display, lpCursor );
        if (cursor)
        {
            if (X11DRV_DD_PrimaryFrame) XDefineCursor( gdi_display, X11DRV_DD_PrimaryFrame, cursor );
            else XDefineCursor( gdi_display, root_window, cursor );
            XFreeCursor( gdi_display, cursor );
        }
        wine_tsx11_unlock();
    }
    else /* set the same cursor for all top-level windows of the current thread */
    {
        Display *display = thread_display();

        wine_tsx11_lock();
        cursor = X11DRV_GetCursor( display, lpCursor );
        wine_tsx11_unlock();
        if (cursor)
        {
/*            EnumThreadWindows( GetCurrentThreadId(), set_win_cursor, (LPARAM)cursor );*/
            EnumWindows( set_win_cursor, (LPARAM)cursor );
            if (X11DRV_DD_PrimaryFrame) TSXDefineCursor( gdi_display, X11DRV_DD_PrimaryFrame, cursor );
            TSXFreeCursor( display, cursor );
            TSXSync( display, False );
        }
    }
}

/***********************************************************************
 *		SetCursorPos (X11DRV.@)
 */
void X11DRV_SetCursorPos( INT x, INT y )
{
    Display *display = thread_display();

    TRACE( "warping to (%d,%d)\n", x, y );

    wine_tsx11_lock();
    XWarpPointer( display, root_window, root_window, 0, 0, 0, 0, x, y );
    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;
}

/***********************************************************************
 *		InitMouse (X11DRV.@)
 */
void X11DRV_InitMouse( BYTE *key_state_table )
{
    Window root, child;
    int root_x, root_y, child_x, child_y;
    unsigned int KeyState;

    pKeyStateTable = key_state_table;
    /* Get the current mouse position and simulate an absolute mouse
       movement to initialize the mouse global variables */
    TSXQueryPointer( thread_display(), root_window, &root, &child,
                     &root_x, &root_y, &child_x, &child_y, &KeyState);
    update_key_state( 0, CurrentTime, KeyState );
    send_mouse_event( 0, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
                      root_x, root_y, 0, CurrentTime );
}


/***********************************************************************
 *           X11DRV_ButtonPress
 */
void X11DRV_ButtonPress( HWND hwnd, XButtonEvent *event )
{
    int buttonNum = event->button - 1;
    POINT pt;

    if (buttonNum >= NB_BUTTONS) return;

    get_coords( &hwnd, event->window, event->x, event->y, &pt );
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, button_down_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
                      pt.x, pt.y, button_data[buttonNum], event->time );
}


/***********************************************************************
 *           X11DRV_ButtonRelease
 */
void X11DRV_ButtonRelease( HWND hwnd, XButtonEvent *event )
{
    int buttonNum = event->button - 1;
    POINT pt;

    if (buttonNum >= NB_BUTTONS || !button_up_flags[buttonNum]) return;

    get_coords( &hwnd, event->window, event->x, event->y, &pt );
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, button_up_flags[buttonNum] | MOUSEEVENTF_ABSOLUTE,
                      pt.x, pt.y, button_data[buttonNum], event->time );
}


/***********************************************************************
 *           X11DRV_MotionNotify
 */
void X11DRV_MotionNotify( HWND hwnd, XMotionEvent *event )
{
    POINT pt;

    get_coords( &hwnd, event->window, event->x, event->y, &pt );
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
                      pt.x, pt.y, 0, event->time );
}


#ifdef HAVE_LIBXXF86DGA2
/**********************************************************************
 *              X11DRV_DGAMotionEvent
 */
void X11DRV_DGAMotionEvent( HWND hwnd, XDGAMotionEvent *event )
{
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, MOUSEEVENTF_MOVE, event->dx, event->dy, 0, event->time );
}

/**********************************************************************
 *              X11DRV_DGAButtonPressEvent
 */
void X11DRV_DGAButtonPressEvent( HWND hwnd, XDGAButtonEvent *event )
{
    int buttonNum = event->button - 1;

    if (buttonNum >= NB_BUTTONS) return;
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, button_down_flags[buttonNum],
                      0, 0, button_data[buttonNum], event->time );
}

/**********************************************************************
 *              X11DRV_DGAButtonReleaseEvent
 */
void X11DRV_DGAButtonReleaseEvent( HWND hwnd, XDGAButtonEvent *event )
{
    int buttonNum = event->button - 1;

    if (buttonNum >= NB_BUTTONS) return;
    update_key_state( hwnd, event->time, event->state );
    send_mouse_event( hwnd, button_up_flags[buttonNum],
                      0, 0, button_data[buttonNum], event->time );
}
#endif /* HAVE_LIBXXF86DGA2 */
