event.c 42.1 KB
Newer Older
1
/*
2
 * X11 event driver
3 4
 *
 * Copyright 1993 Alexandre Julliard
5
 *	     1999 Noel Borthwick
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 */

#include "config.h"

24 25 26 27 28 29
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
30 31
#include <X11/Xatom.h>
#include <X11/keysym.h>
32
#include <X11/Xlib.h>
33 34
#include <X11/Xresource.h>
#include <X11/Xutil.h>
35

36
#include <assert.h>
37
#include <stdarg.h>
38
#include <string.h>
39 40 41

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
42 43 44 45
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "wingdi.h"
46

47
#include "x11drv.h"
48 49 50 51

/* avoid conflict with field names in included win32 headers */
#undef Status
#include "shlobj.h"  /* DROPFILES */
52
#include "shellapi.h"
53

54
#include "wine/server.h"
55
#include "wine/debug.h"
56

57
WINE_DEFAULT_DEBUG_CHANNEL(event);
58

59 60
extern BOOL ximInComposeMode;

61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define DndNotDnd       -1    /* OffiX drag&drop */
#define DndUnknown      0
#define DndRawData      1
#define DndFile         2
#define DndFiles        3
#define DndText         4
#define DndDir          5
#define DndLink         6
#define DndExe          7

#define DndEND          8

#define DndURL          128   /* KDE drag&drop */

75
  /* Event handlers */
76 77 78
static void X11DRV_FocusIn( HWND hwnd, XEvent *event );
static void X11DRV_FocusOut( HWND hwnd, XEvent *event );
static void X11DRV_Expose( HWND hwnd, XEvent *event );
79
static void X11DRV_MapNotify( HWND hwnd, XEvent *event );
80
static void X11DRV_ConfigureNotify( HWND hwnd, XEvent *event );
81 82
static void X11DRV_PropertyNotify( HWND hwnd, XEvent *event );
static void X11DRV_ClientMessage( HWND hwnd, XEvent *event );
83 84

struct event_handler
85
{
86 87
    int                  type;    /* event type */
    x11drv_event_handler handler; /* corresponding handler function */
88 89
};

90 91 92 93 94 95 96 97 98 99 100 101
#define MAX_EVENT_HANDLERS 64

static struct event_handler handlers[MAX_EVENT_HANDLERS] =
{
    /* list must be sorted by event type */
    { KeyPress,         X11DRV_KeyEvent },
    { KeyRelease,       X11DRV_KeyEvent },
    { ButtonPress,      X11DRV_ButtonPress },
    { ButtonRelease,    X11DRV_ButtonRelease },
    { MotionNotify,     X11DRV_MotionNotify },
    { EnterNotify,      X11DRV_EnterNotify },
    /* LeaveNotify */
102 103
    { FocusIn,          X11DRV_FocusIn },
    { FocusOut,         X11DRV_FocusOut },
104 105 106 107 108 109
    { KeymapNotify,     X11DRV_KeymapNotify },
    { Expose,           X11DRV_Expose },
    /* GraphicsExpose */
    /* NoExpose */
    /* VisibilityNotify */
    /* CreateNotify */
110
    { DestroyNotify,    X11DRV_DestroyNotify },
111
    /* UnmapNotify */
112 113 114 115 116 117 118 119 120
    { MapNotify,        X11DRV_MapNotify },
    /* MapRequest */
    /* ReparentNotify */
    { ConfigureNotify,  X11DRV_ConfigureNotify },
    /* ConfigureRequest */
    /* GravityNotify */
    /* ResizeRequest */
    /* CirculateNotify */
    /* CirculateRequest */
121
    { PropertyNotify,   X11DRV_PropertyNotify },
122 123 124 125
    { SelectionClear,   X11DRV_SelectionClear },
    { SelectionRequest, X11DRV_SelectionRequest },
    /* SelectionNotify */
    /* ColormapNotify */
126
    { ClientMessage,    X11DRV_ClientMessage },
127 128
    { MappingNotify,    X11DRV_MappingNotify },
};
129

130
static int nb_event_handlers = 18;  /* change this if you add handlers above */
131 132


133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* return the name of an X event */
static const char *dbgstr_event( int type )
{
    static const char * const event_names[] =
    {
        "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease",
        "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut",
        "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify",
        "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",
        "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
        "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify",
        "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify",
        "ClientMessage", "MappingNotify"
    };

    if (type >= KeyPress && type <= MappingNotify) return event_names[type - KeyPress];
    return wine_dbg_sprintf( "Extension event %d", type );
}

152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/***********************************************************************
 *           find_handler
 *
 * Find the handler for a given event type. Caller must hold the x11 lock.
 */
static inline x11drv_event_handler find_handler( int type )
{
    int min = 0, max = nb_event_handlers - 1;

    while (min <= max)
    {
        int pos = (min + max) / 2;
        if (handlers[pos].type == type) return handlers[pos].handler;
        if (handlers[pos].type > type) max = pos - 1;
        else min = pos + 1;
    }
    return NULL;
}


/***********************************************************************
 *           X11DRV_register_event_handler
 *
 * Register a handler for a given event type.
 * If already registered, overwrite the previous handler.
 */
void X11DRV_register_event_handler( int type, x11drv_event_handler handler )
{
    int min, max;

    wine_tsx11_lock();
    min = 0;
    max = nb_event_handlers - 1;
    while (min <= max)
    {
        int pos = (min + max) / 2;
        if (handlers[pos].type == type)
        {
            handlers[pos].handler = handler;
            goto done;
        }
        if (handlers[pos].type > type) max = pos - 1;
        else min = pos + 1;
    }
    /* insert it between max and min */
    memmove( &handlers[min+1], &handlers[min], (nb_event_handlers - min) * sizeof(handlers[0]) );
    handlers[min].type = type;
    handlers[min].handler = handler;
    nb_event_handlers++;
    assert( nb_event_handlers <= MAX_EVENT_HANDLERS );
done:
    wine_tsx11_unlock();
    TRACE("registered handler %p for event %d count %d\n", handler, type, nb_event_handlers );
}
207 208


209 210 211 212 213
/***********************************************************************
 *           filter_event
 */
static Bool filter_event( Display *display, XEvent *event, char *arg )
{
214
    ULONG_PTR mask = (ULONG_PTR)arg;
215 216 217 218 219 220 221 222

    if ((mask & QS_ALLINPUT) == QS_ALLINPUT) return 1;

    switch(event->type)
    {
    case KeyPress:
    case KeyRelease:
    case KeymapNotify:
223
    case MappingNotify:
224 225 226 227 228 229 230 231 232 233
        return (mask & QS_KEY) != 0;
    case ButtonPress:
    case ButtonRelease:
        return (mask & QS_MOUSEBUTTON) != 0;
    case MotionNotify:
    case EnterNotify:
    case LeaveNotify:
        return (mask & QS_MOUSEMOVE) != 0;
    case Expose:
        return (mask & QS_PAINT) != 0;
234 235 236 237 238 239
    case FocusIn:
    case FocusOut:
    case MapNotify:
    case UnmapNotify:
    case ConfigureNotify:
    case PropertyNotify:
240 241 242 243 244 245 246 247
    case ClientMessage:
        return (mask & QS_POSTMESSAGE) != 0;
    default:
        return (mask & QS_SENDMESSAGE) != 0;
    }
}


248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
enum event_merge_action
{
    MERGE_DISCARD,  /* discard the old event */
    MERGE_HANDLE,   /* handle the old event */
    MERGE_KEEP      /* keep the old event for future merging */
};

/***********************************************************************
 *           merge_events
 *
 * Try to merge 2 consecutive events.
 */
static enum event_merge_action merge_events( XEvent *prev, XEvent *next )
{
    switch (prev->type)
    {
    case ConfigureNotify:
        switch (next->type)
        {
        case ConfigureNotify:
268 269 270 271 272 273
            if (prev->xany.window == next->xany.window)
            {
                TRACE( "discarding duplicate ConfigureNotify for window %lx\n", prev->xany.window );
                return MERGE_DISCARD;
            }
            break;
274 275 276 277 278 279
        case Expose:
        case PropertyNotify:
            return MERGE_KEEP;
        }
        break;
    case MotionNotify:
280
        if (prev->xany.window == next->xany.window && next->type == MotionNotify)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        {
            TRACE( "discarding duplicate MotionNotify for window %lx\n", prev->xany.window );
            return MERGE_DISCARD;
        }
        break;
    }
    return MERGE_HANDLE;
}


/***********************************************************************
 *           call_event_handler
 */
static inline void call_event_handler( Display *display, XEvent *event )
{
    HWND hwnd;
    x11drv_event_handler handler;
298 299
    XEvent *prev;
    struct x11drv_thread_data *thread_data;
300 301 302 303 304 305 306 307 308 309 310 311 312 313

    if (!(handler = find_handler( event->type )))
    {
        TRACE( "%s for win %lx, ignoring\n", dbgstr_event( event->type ), event->xany.window );
        return;  /* no handler, ignore it */
    }

    if (XFindContext( display, event->xany.window, winContext, (char **)&hwnd ) != 0)
        hwnd = 0;  /* not for a registered window */
    if (!hwnd && event->xany.window == root_window) hwnd = GetDesktopWindow();

    TRACE( "%s for hwnd/window %p/%lx\n",
           dbgstr_event( event->type ), hwnd, event->xany.window );
    wine_tsx11_unlock();
314 315 316
    thread_data = x11drv_thread_data();
    prev = thread_data->current_event;
    thread_data->current_event = event;
317
    handler( hwnd, event );
318
    thread_data->current_event = prev;
319 320 321 322
    wine_tsx11_lock();
}


323
/***********************************************************************
324
 *           process_events
325
 */
326
static int process_events( Display *display, Bool (*filter)(), ULONG_PTR arg )
327
{
328
    XEvent event, prev_event;
329
    int count = 0;
330
    enum event_merge_action action = MERGE_DISCARD;
331

332
    prev_event.type = 0;
333
    wine_tsx11_lock();
334
    while (XCheckIfEvent( display, &event, filter, (char *)arg ))
335
    {
336
        count++;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        if (XFilterEvent( &event, None ))
        {
            /*
             * SCIM on linux filters key events strangely. It does not filter the
             * KeyPress events for these keys however it does filter the
             * KeyRelease events. This causes wine to become very confused as
             * to the keyboard state.
             *
             * We need to let those KeyRelease events be processed so that the
             * keyboard state is correct.
             */
            if (event.type == KeyRelease)
            {
                KeySym keysym = 0;
                XKeyEvent *keyevent = &event.xkey;

                XLookupString(keyevent, NULL, 0, &keysym, NULL);
                if (!(keysym == XK_Shift_L ||
                    keysym == XK_Shift_R ||
                    keysym == XK_Control_L ||
                    keysym == XK_Control_R ||
                    keysym == XK_Alt_R ||
                    keysym == XK_Alt_L ||
                    keysym == XK_Meta_R ||
                    keysym == XK_Meta_L))
                        continue; /* not a key we care about, ignore it */
            }
            else
                continue;  /* filtered, ignore it */
        }
367 368
        if (prev_event.type) action = merge_events( &prev_event, &event );
        switch( action )
369
        {
370 371 372 373 374 375 376 377 378 379
        case MERGE_DISCARD:  /* discard prev, keep new */
            prev_event = event;
            break;
        case MERGE_HANDLE:  /* handle prev, keep new */
            call_event_handler( display, &prev_event );
            prev_event = event;
            break;
        case MERGE_KEEP:  /* handle new, keep prev for future merging */
            call_event_handler( display, &event );
            break;
380
        }
381
    }
382
    XFlush( gdi_display );
383
    if (prev_event.type) call_event_handler( display, &prev_event );
384
    wine_tsx11_unlock();
385
    if (count) TRACE( "processed %d events\n", count );
386
    return count;
387 388
}

389

390
/***********************************************************************
391
 *           MsgWaitForMultipleObjectsEx   (X11DRV.@)
392
 */
393 394
DWORD CDECL X11DRV_MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
                                                DWORD timeout, DWORD mask, DWORD flags )
395
{
396
    DWORD ret;
397
    struct x11drv_thread_data *data = TlsGetValue( thread_data_tls_index );
398

399
    if (!data)
400 401
    {
        if (!count && !timeout) return WAIT_TIMEOUT;
402 403
        return WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
                                         timeout, flags & MWMO_ALERTABLE );
404
    }
405

406
    if (data->current_event) mask = 0;  /* don't process nested events */
407

408
    if (process_events( data->display, filter_event, mask )) ret = count - 1;
409
    else if (count || timeout)
410
    {
411
        ret = WaitForMultipleObjectsEx( count, handles, flags & MWMO_WAITALL,
412
                                        timeout, flags & MWMO_ALERTABLE );
413
        if (ret == count - 1) process_events( data->display, filter_event, mask );
414
    }
415 416
    else ret = WAIT_TIMEOUT;

417
    return ret;
418 419
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/***********************************************************************
 *           EVENT_x11_time_to_win32_time
 *
 * Make our timer and the X timer line up as best we can
 *  Pass 0 to retrieve the current adjustment value (times -1)
 */
DWORD EVENT_x11_time_to_win32_time(Time time)
{
  static DWORD adjust = 0;
  DWORD now = GetTickCount();
  DWORD ret;

  if (! adjust && time != 0)
  {
    ret = now;
    adjust = time - now;
  }
  else
  {
      /* If we got an event in the 'future', then our clock is clearly wrong. 
         If we got it more than 10000 ms in the future, then it's most likely
         that the clock has wrapped.  */

      ret = time - adjust;
      if (ret > now && ((ret - now) < 10000) && time != 0)
      {
        adjust += ret - now;
        ret    -= ret - now;
      }
  }

  return ret;

}
454

455 456 457 458 459
/*******************************************************************
 *         can_activate_window
 *
 * Check if we can activate the specified window.
 */
460
static inline BOOL can_activate_window( HWND hwnd )
461 462 463 464
{
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
    if (!(style & WS_VISIBLE)) return FALSE;
    if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
465
    if (style & WS_MINIMIZE) return FALSE;
466
    if (hwnd == GetDesktopWindow()) return FALSE;
467 468 469 470
    return !(style & WS_DISABLED);
}


471 472 473
/**********************************************************************
 *              set_focus
 */
474
static void set_focus( Display *display, HWND hwnd, Time time )
475
{
476
    HWND focus;
477
    Window win;
478

479
    TRACE( "setting foreground window to %p\n", hwnd );
480
    SetForegroundWindow( hwnd );
481

482
    focus = GetFocus();
483
    if (focus) focus = GetAncestor( focus, GA_ROOT );
484 485 486 487
    win = X11DRV_get_whole_window(focus);

    if (win)
    {
488
        TRACE( "setting focus to %p (%lx) time=%ld\n", focus, win, time );
489
        wine_tsx11_lock();
490
        XSetInputFocus( display, win, RevertToParent, time );
491
        wine_tsx11_unlock();
492
    }
493 494 495 496
}


/**********************************************************************
497
 *              handle_wm_protocols
498
 */
499
static void handle_wm_protocols( HWND hwnd, XClientMessageEvent *event )
500
{
501 502 503
    Atom protocol = (Atom)event->data.l[0];

    if (!protocol) return;
504

505
    if (protocol == x11drv_atom(WM_DELETE_WINDOW))
506 507 508 509 510
    {
        /* Ignore the delete window request if the window has been disabled
         * and we are in managed mode. This is to disallow applications from
         * being closed by the window manager while in a modal state.
         */
511 512 513 514 515 516 517 518 519 520 521 522
        if (IsWindowEnabled(hwnd))
        {
            HMENU hSysMenu;

            if (GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE) return;
            hSysMenu = GetSystemMenu(hwnd, FALSE);
            if (hSysMenu)
            {
                UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
                if (state == 0xFFFFFFFF || (state & (MF_DISABLED | MF_GRAYED)))
                    return;
            }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            if (GetActiveWindow() != hwnd)
            {
                LRESULT ma = SendMessageW( hwnd, WM_MOUSEACTIVATE,
                                           (WPARAM)GetAncestor( hwnd, GA_ROOT ),
                                           MAKELONG(HTCLOSE,WM_LBUTTONDOWN) );
                switch(ma)
                {
                    case MA_NOACTIVATEANDEAT:
                    case MA_ACTIVATEANDEAT:
                        return;
                    case MA_NOACTIVATE:
                        break;
                    case MA_ACTIVATE:
                    case 0:
                        SetActiveWindow(hwnd);
                        break;
                    default:
                        WARN( "unknown WM_MOUSEACTIVATE code %d\n", (int) ma );
                        break;
                }
            }
544 545
            PostMessageW( hwnd, WM_X11DRV_DELETE_WINDOW, 0, 0 );
        }
546
    }
547
    else if (protocol == x11drv_atom(WM_TAKE_FOCUS))
548
    {
549 550 551
        Time event_time = (Time)event->data.l[1];
        HWND last_focus = x11drv_thread_data()->last_focus;

552
        TRACE( "got take focus msg for %p, enabled=%d, visible=%d (style %08x), focus=%p, active=%p, fg=%p, last=%p\n",
553 554
               hwnd, IsWindowEnabled(hwnd), IsWindowVisible(hwnd), GetWindowLongW(hwnd, GWL_STYLE),
               GetFocus(), GetActiveWindow(), GetForegroundWindow(), last_focus );
555

556
        if (can_activate_window(hwnd))
557
        {
558 559 560
            /* simulate a mouse click on the caption to find out
             * whether the window wants to be activated */
            LRESULT ma = SendMessageW( hwnd, WM_MOUSEACTIVATE,
561
                                       (WPARAM)GetAncestor( hwnd, GA_ROOT ),
562
                                       MAKELONG(HTCAPTION,WM_LBUTTONDOWN) );
563 564
            if (ma != MA_NOACTIVATEANDEAT && ma != MA_NOACTIVATE)
            {
565
                set_focus( event->display, hwnd, event_time );
566 567
                return;
            }
568
        }
569 570 571 572 573
        /* try to find some other window to give the focus to */
        hwnd = GetFocus();
        if (hwnd) hwnd = GetAncestor( hwnd, GA_ROOT );
        if (!hwnd) hwnd = GetActiveWindow();
        if (!hwnd) hwnd = last_focus;
574
        if (hwnd && can_activate_window(hwnd)) set_focus( event->display, hwnd, event_time );
575 576 577
    }
    else if (protocol == x11drv_atom(_NET_WM_PING))
    {
578 579 580 581
      XClientMessageEvent xev;
      xev = *event;
      
      TRACE("NET_WM Ping\n");
582
      wine_tsx11_lock();
583 584
      xev.window = DefaultRootWindow(xev.display);
      XSendEvent(xev.display, xev.window, False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*)&xev);
585
      wine_tsx11_unlock();
586 587
      /* this line is semi-stolen from gtk2 */
      TRACE("NET_WM Pong\n");
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    }
}


static const char * const focus_details[] =
{
    "NotifyAncestor",
    "NotifyVirtual",
    "NotifyInferior",
    "NotifyNonlinear",
    "NotifyNonlinearVirtual",
    "NotifyPointer",
    "NotifyPointerRoot",
    "NotifyDetailNone"
};

/**********************************************************************
605
 *              X11DRV_FocusIn
606
 */
607
static void X11DRV_FocusIn( HWND hwnd, XEvent *xev )
608
{
609
    XFocusChangeEvent *event = &xev->xfocus;
610 611
    XIC xic;

612 613
    if (!hwnd) return;

614
    TRACE( "win %p xwin %lx detail=%s\n", hwnd, event->window, focus_details[event->detail] );
615 616 617

    if (event->detail == NotifyPointer) return;

618 619 620 621 622 623
    if ((xic = X11DRV_get_ic( hwnd )))
    {
        wine_tsx11_lock();
        XSetICFocus( xic );
        wine_tsx11_unlock();
    }
624
    if (use_take_focus) return;  /* ignore FocusIn if we are using take focus */
625

626
    if (!can_activate_window(hwnd))
627 628
    {
        HWND hwnd = GetFocus();
629
        if (hwnd) hwnd = GetAncestor( hwnd, GA_ROOT );
630 631
        if (!hwnd) hwnd = GetActiveWindow();
        if (!hwnd) hwnd = x11drv_thread_data()->last_focus;
632
        if (hwnd && can_activate_window(hwnd)) set_focus( event->display, hwnd, CurrentTime );
633
    }
634
    else SetForegroundWindow( hwnd );
635 636
}

637

638
/**********************************************************************
639
 *              X11DRV_FocusOut
640 641
 *
 * Note: only top-level windows get FocusOut events.
642
 */
643
static void X11DRV_FocusOut( HWND hwnd, XEvent *xev )
644
{
645
    XFocusChangeEvent *event = &xev->xfocus;
646 647 648
    HWND hwnd_tmp;
    Window focus_win;
    int revert;
649
    XIC xic;
650

651 652
    if (!hwnd) return;

653
    TRACE( "win %p xwin %lx detail=%s\n", hwnd, event->window, focus_details[event->detail] );
654 655

    if (event->detail == NotifyPointer) return;
656 657
    if (ximInComposeMode) return;

658
    x11drv_thread_data()->last_focus = hwnd;
659 660 661 662 663 664
    if ((xic = X11DRV_get_ic( hwnd )))
    {
        wine_tsx11_lock();
        XUnsetICFocus( xic );
        wine_tsx11_unlock();
    }
665
    if (hwnd != GetForegroundWindow()) return;
666
    SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
667 668 669 670

    /* don't reset the foreground window, if the window which is
       getting the focus is a Wine window */

671
    wine_tsx11_lock();
672
    XGetInputFocus( event->display, &focus_win, &revert );
673 674
    if (focus_win)
    {
675
        if (XFindContext( event->display, focus_win, winContext, (char **)&hwnd_tmp ) != 0)
676 677 678 679 680
            focus_win = 0;
    }
    wine_tsx11_unlock();

    if (!focus_win)
681 682 683 684 685 686 687
    {
        /* Abey : 6-Oct-99. Check again if the focus out window is the
           Foreground window, because in most cases the messages sent
           above must have already changed the foreground window, in which
           case we don't have to change the foreground window to 0 */
        if (hwnd == GetForegroundWindow())
        {
688 689
            TRACE( "lost focus, setting fg to desktop\n" );
            SetForegroundWindow( GetDesktopWindow() );
690 691
        }
    }
692 693 694
}


695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
/***********************************************************************
 *           X11DRV_Expose
 */
static void X11DRV_Expose( HWND hwnd, XEvent *xev )
{
    XExposeEvent *event = &xev->xexpose;
    RECT rect;
    struct x11drv_win_data *data;
    int flags = RDW_INVALIDATE | RDW_ERASE;

    TRACE( "win %p (%lx) %d,%d %dx%d\n",
           hwnd, event->window, event->x, event->y, event->width, event->height );

    if (!(data = X11DRV_get_win_data( hwnd ))) return;

    if (event->window == data->whole_window)
    {
        rect.left = data->whole_rect.left + event->x;
        rect.top  = data->whole_rect.top + event->y;
        flags |= RDW_FRAME;
    }
    else
    {
        rect.left = data->client_rect.left + event->x;
        rect.top  = data->client_rect.top + event->y;
    }
    rect.right  = rect.left + event->width;
    rect.bottom = rect.top + event->height;

    if (event->window != root_window)
    {
        SERVER_START_REQ( update_window_zorder )
        {
728
            req->window      = wine_server_user_handle( hwnd );
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
            req->rect.left   = rect.left;
            req->rect.top    = rect.top;
            req->rect.right  = rect.right;
            req->rect.bottom = rect.bottom;
            wine_server_call( req );
        }
        SERVER_END_REQ;

        /* make position relative to client area instead of parent */
        OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
        flags |= RDW_ALLCHILDREN;
    }

    RedrawWindow( hwnd, &rect, 0, flags );
}


746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
/**********************************************************************
 *		X11DRV_MapNotify
 */
static void X11DRV_MapNotify( HWND hwnd, XEvent *event )
{
    struct x11drv_win_data *data;

    if (!(data = X11DRV_get_win_data( hwnd ))) return;
    if (!data->mapped) return;

    if (!data->managed)
    {
        HWND hwndFocus = GetFocus();
        if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
    }
}


764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/***********************************************************************
 *     is_net_wm_state_maximized
 */
static BOOL is_net_wm_state_maximized( Display *display, struct x11drv_win_data *data )
{
    Atom type, *state;
    int format, ret = 0;
    unsigned long i, count, remaining;

    wine_tsx11_lock();
    if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), 0,
                             65536/sizeof(CARD32), False, XA_ATOM, &type, &format, &count,
                             &remaining, (unsigned char **)&state ))
    {
        if (type == XA_ATOM && format == 32)
        {
            for (i = 0; i < count; i++)
            {
                if (state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_VERT) ||
                    state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
                    ret++;
            }
        }
        XFree( state );
    }
    wine_tsx11_unlock();
    return (ret == 2);
}


794 795 796 797 798 799 800 801 802 803 804 805 806
/***********************************************************************
 *		X11DRV_ConfigureNotify
 */
void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
{
    XConfigureEvent *event = &xev->xconfigure;
    struct x11drv_win_data *data;
    RECT rect;
    UINT flags;
    int cx, cy, x = event->x, y = event->y;

    if (!hwnd) return;
    if (!(data = X11DRV_get_win_data( hwnd ))) return;
807
    if (!data->mapped || data->iconic || !data->managed) return;
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

    /* Get geometry */

    if (!event->send_event)  /* normal event, need to map coordinates to the root */
    {
        Window child;
        wine_tsx11_lock();
        XTranslateCoordinates( event->display, data->whole_window, root_window,
                               0, 0, &x, &y, &child );
        wine_tsx11_unlock();
    }
    rect.left   = x;
    rect.top    = y;
    rect.right  = x + event->width;
    rect.bottom = y + event->height;
    OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
824 825
    TRACE( "win %p/%lx new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
           hwnd, data->whole_window, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
826 827 828
           event->x, event->y, event->width, event->height );
    X11DRV_X_to_window_rect( data, &rect );

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    if (is_net_wm_state_maximized( event->display, data ))
    {
        if (!IsZoomed( data->hwnd ))
        {
            TRACE( "win %p/%lx is maximized\n", data->hwnd, data->whole_window );
            SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0 );
            return;
        }
    }
    else
    {
        if (IsZoomed( data->hwnd ))
        {
            TRACE( "window %p/%lx is no longer maximized\n", data->hwnd, data->whole_window );
            SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
            return;
        }
    }

848 849
    /* Compare what has changed */

850 851 852 853 854 855 856
    x     = rect.left;
    y     = rect.top;
    cx    = rect.right - rect.left;
    cy    = rect.bottom - rect.top;
    flags = SWP_NOACTIVATE | SWP_NOZORDER;

    if (data->window_rect.left == x && data->window_rect.top == y) flags |= SWP_NOMOVE;
857 858
    else
        TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
859
               hwnd, data->window_rect.left, data->window_rect.top, x, y );
860

861 862 863
    if ((data->window_rect.right - data->window_rect.left == cx &&
         data->window_rect.bottom - data->window_rect.top == cy) ||
        (IsRectEmpty( &data->window_rect ) && event->width == 1 && event->height == 1))
864 865 866 867 868 869
    {
        if (flags & SWP_NOMOVE) return;  /* if nothing changed, don't do anything */
        flags |= SWP_NOSIZE;
    }
    else
        TRACE( "%p resizing from (%dx%d) to (%dx%d)\n",
870 871
               hwnd, data->window_rect.right - data->window_rect.left,
               data->window_rect.bottom - data->window_rect.top, cx, cy );
872 873 874 875 876

    SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
}


877 878 879
/***********************************************************************
 *           get_window_wm_state
 */
880
static int get_window_wm_state( Display *display, struct x11drv_win_data *data )
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
{
    struct
    {
        CARD32 state;
        XID     icon;
    } *state;
    Atom type;
    int format, ret = -1;
    unsigned long count, remaining;

    wine_tsx11_lock();
    if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(WM_STATE), 0,
                             sizeof(*state)/sizeof(CARD32), False, x11drv_atom(WM_STATE),
                             &type, &format, &count, &remaining, (unsigned char **)&state ))
    {
896
        if (type == x11drv_atom(WM_STATE) && get_property_size( format, count ) >= sizeof(*state))
897 898 899 900 901 902 903 904
            ret = state->state;
        XFree( state );
    }
    wine_tsx11_unlock();
    return ret;
}


905
/***********************************************************************
906 907 908
 *           handle_wm_state_notify
 *
 * Handle a PropertyNotify for WM_STATE.
909
 */
910 911
static void handle_wm_state_notify( struct x11drv_win_data *data, XPropertyEvent *event,
                                    BOOL update_window )
912
{
913
    switch(event->state)
914
    {
915
    case PropertyDelete:
916
        TRACE( "%p/%lx: WM_STATE deleted from %d\n", data->hwnd, data->whole_window, data->wm_state );
917
        data->wm_state = WithdrawnState;
918
        break;
919
    case PropertyNewValue:
920
        {
921
            int old_state = data->wm_state;
922 923 924
            int new_state = get_window_wm_state( event->display, data );
            if (new_state != -1 && new_state != data->wm_state)
            {
925 926
                TRACE( "%p/%lx: new WM_STATE %d from %d\n",
                       data->hwnd, data->whole_window, new_state, old_state );
927
                data->wm_state = new_state;
928 929 930
                /* ignore the initial state transition out of withdrawn state */
                /* metacity does Withdrawn->NormalState->IconicState when mapping an iconic window */
                if (!old_state) return;
931 932 933
            }
        }
        break;
934
    }
935 936 937 938 939 940

    if (!update_window || !data->managed || !data->mapped) return;

    if (data->iconic && data->wm_state == NormalState)  /* restore window */
    {
        data->iconic = FALSE;
941 942 943 944 945 946 947 948 949 950
        if (is_net_wm_state_maximized( event->display, data ))
        {
            TRACE( "restoring to max %p/%lx\n", data->hwnd, data->whole_window );
            SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0 );
        }
        else
        {
            TRACE( "restoring win %p/%lx\n", data->hwnd, data->whole_window );
            SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
        }
951 952 953 954 955
    }
    else if (!data->iconic && data->wm_state == IconicState)
    {
        TRACE( "minimizing win %p/%lx\n", data->hwnd, data->whole_window );
        data->iconic = TRUE;
956
        SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
957
    }
958
}
959

960

961
/***********************************************************************
962
 *           X11DRV_PropertyNotify
963
 */
964
static void X11DRV_PropertyNotify( HWND hwnd, XEvent *xev )
965 966 967 968 969 970 971
{
    XPropertyEvent *event = &xev->xproperty;
    struct x11drv_win_data *data;

    if (!hwnd) return;
    if (!(data = X11DRV_get_win_data( hwnd ))) return;

972
    if (event->atom == x11drv_atom(WM_STATE)) handle_wm_state_notify( data, event, TRUE );
973 974 975
}


976 977 978
/* event filter to wait for a WM_STATE change notification on a window */
static Bool is_wm_state_notify( Display *display, XEvent *event, XPointer arg )
{
979 980 981
    if (event->xany.window != (Window)arg) return 0;
    return (event->type == DestroyNotify ||
            (event->type == PropertyNotify && event->xproperty.atom == x11drv_atom(WM_STATE)));
982 983 984 985 986
}

/***********************************************************************
 *           wait_for_withdrawn_state
 */
987
void wait_for_withdrawn_state( Display *display, struct x11drv_win_data *data, BOOL set )
988 989 990
{
    DWORD end = GetTickCount() + 2000;

991
    if (!data->managed) return;
992

993 994
    TRACE( "waiting for window %p/%lx to become %swithdrawn\n",
           data->hwnd, data->whole_window, set ? "" : "not " );
995

996 997
    while (data->whole_window && ((data->wm_state == WithdrawnState) == !set))
    {
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        XEvent event;
        int count = 0;

        wine_tsx11_lock();
        while (XCheckIfEvent( display, &event, is_wm_state_notify, (char *)data->whole_window ))
        {
            count++;
            if (XFilterEvent( &event, None )) continue;  /* filtered, ignore it */
            if (event.type == DestroyNotify) call_event_handler( display, &event );
            else
            {
                wine_tsx11_unlock();
1010
                handle_wm_state_notify( data, &event.xproperty, FALSE );
1011 1012 1013 1014 1015 1016
                wine_tsx11_lock();
            }
        }
        wine_tsx11_unlock();

        if (!count)
1017
        {
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
            struct pollfd pfd;
            int timeout = end - GetTickCount();

            pfd.fd = ConnectionNumber(display);
            pfd.events = POLLIN;
            if (timeout <= 0 || poll( &pfd, 1, timeout ) != 1)
            {
                FIXME( "window %p/%lx wait timed out\n", data->hwnd, data->whole_window );
                break;
            }
1028 1029
        }
    }
1030
    TRACE( "window %p/%lx state now %d\n", data->hwnd, data->whole_window, data->wm_state );
1031 1032 1033
}


1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
static HWND find_drop_window( HWND hQueryWnd, LPPOINT lpPt )
{
    RECT tempRect;

    if (!IsWindowEnabled(hQueryWnd)) return 0;
    
    GetWindowRect(hQueryWnd, &tempRect);

    if(!PtInRect(&tempRect, *lpPt)) return 0;

    if (!IsIconic( hQueryWnd ))
    {
1046 1047
        POINT pt = *lpPt;
        ScreenToClient( hQueryWnd, &pt );
1048 1049
        GetClientRect( hQueryWnd, &tempRect );

1050
        if (PtInRect( &tempRect, pt))
1051
        {
1052 1053
            HWND ret = ChildWindowFromPointEx( hQueryWnd, pt, CWP_SKIPINVISIBLE|CWP_SKIPDISABLED );
            if (ret && ret != hQueryWnd)
1054
            {
1055 1056
                ret = find_drop_window( ret, lpPt );
                if (ret) return ret;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
            }
        }
    }

    if(!(GetWindowLongA( hQueryWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)) return 0;
    
    ScreenToClient(hQueryWnd, lpPt);

    return hQueryWnd;
}

1068 1069 1070
/**********************************************************************
 *           EVENT_DropFromOffix
 *
Austin English's avatar
Austin English committed
1071
 * don't know if it still works (last Changelog is from 96/11/04)
1072
 */
1073
static void EVENT_DropFromOffiX( HWND hWnd, XClientMessageEvent *event )
1074
{
1075
    struct x11drv_win_data *data;
1076 1077 1078
    unsigned long	data_length;
    unsigned long	aux_long;
    unsigned char*	p_data = NULL;
1079 1080
    Atom atom_aux;
    int			x, y, dummy;
1081
    BOOL	        bAccept;
1082
    Window		win, w_aux_root, w_aux_child;
1083

1084
    win = X11DRV_get_whole_window(hWnd);
1085
    wine_tsx11_lock();
1086
    XQueryPointer( event->display, win, &w_aux_root, &w_aux_child,
1087
                   &x, &y, &dummy, &dummy, (unsigned int*)&aux_long);
1088 1089
    x += virtual_screen_rect.left;
    y += virtual_screen_rect.top;
1090
    wine_tsx11_unlock();
1091

1092
    if (!(data = X11DRV_get_win_data( hWnd ))) return;
1093

1094 1095
    /* find out drop point and drop window */
    if( x < 0 || y < 0 ||
1096 1097
        x > (data->whole_rect.right - data->whole_rect.left) ||
        y > (data->whole_rect.bottom - data->whole_rect.top) )
1098
    {   
1099
	bAccept = GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES;
1100 1101 1102 1103
	x = 0;
	y = 0; 
    }
    else
1104
    {
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    	POINT	pt = { x, y };
        HWND    hwndDrop = find_drop_window( hWnd, &pt );
	if (hwndDrop)
	{
	    x = pt.x;
	    y = pt.y;
	    bAccept = TRUE;
	}
	else
	{
	    bAccept = FALSE;
	}
1117
    }
1118 1119 1120

    if (!bAccept) return;

1121 1122 1123
    wine_tsx11_lock();
    XGetWindowProperty( event->display, DefaultRootWindow(event->display),
                        x11drv_atom(DndSelection), 0, 65535, FALSE,
1124
                        AnyPropertyType, &atom_aux, &dummy,
1125 1126
                        &data_length, &aux_long, &p_data);
    wine_tsx11_unlock();
1127 1128

    if( !aux_long && p_data)  /* don't bother if > 64K */
1129
    {
1130
        char *p = (char *)p_data;
1131
        char *p_drop;
1132

1133 1134 1135
        aux_long = 0;
        while( *p )  /* calculate buffer size */
        {
1136 1137
            INT len = GetShortPathNameA( p, NULL, 0 );
            if (len) aux_long += len + 1;
1138 1139 1140 1141 1142 1143 1144 1145 1146
            p += strlen(p) + 1;
        }
        if( aux_long && aux_long < 65535 )
        {
            HDROP                 hDrop;
            DROPFILES *lpDrop;

            aux_long += sizeof(DROPFILES) + 1;
            hDrop = GlobalAlloc( GMEM_SHARE, aux_long );
1147
            lpDrop = GlobalLock( hDrop );
1148 1149 1150 1151 1152 1153

            if( lpDrop )
            {
                lpDrop->pFiles = sizeof(DROPFILES);
                lpDrop->pt.x = x;
                lpDrop->pt.y = y;
1154
                lpDrop->fNC = FALSE;
1155 1156
                lpDrop->fWide = FALSE;
                p_drop = (char *)(lpDrop + 1);
1157
                p = (char *)p_data;
1158 1159
                while(*p)
                {
1160
                    if (GetShortPathNameA( p, p_drop, aux_long - (p_drop - (char *)lpDrop) ))
1161 1162 1163 1164 1165 1166 1167 1168
                        p_drop += strlen( p_drop ) + 1;
                    p += strlen(p) + 1;
                }
                *p_drop = '\0';
                PostMessageA( hWnd, WM_DROPFILES, (WPARAM)hDrop, 0L );
            }
        }
    }
1169 1170 1171
    wine_tsx11_lock();
    if( p_data ) XFree(p_data);
    wine_tsx11_unlock();
1172 1173 1174 1175 1176
}

/**********************************************************************
 *           EVENT_DropURLs
 *
1177
 * drop items are separated by \n
1178 1179 1180 1181
 * each item is prefixed by its mime type
 *
 * event->data.l[3], event->data.l[4] contains drop x,y position
 */
1182
static void EVENT_DropURLs( HWND hWnd, XClientMessageEvent *event )
1183
{
1184
  struct x11drv_win_data *win_data;
1185 1186 1187 1188 1189
  unsigned long	data_length;
  unsigned long	aux_long, drop_len = 0;
  unsigned char	*p_data = NULL; /* property data */
  char		*p_drop = NULL;
  char          *p, *next;
1190 1191 1192
  int		x, y;
  DROPFILES *lpDrop;
  HDROP hDrop;
1193 1194 1195 1196
  union {
    Atom	atom_aux;
    int         i;
    Window      w_aux;
Mike McCormack's avatar
Mike McCormack committed
1197
    unsigned int u;
1198 1199
  }		u; /* unused */

1200
  if (!(GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)) return;
1201

1202 1203 1204 1205 1206 1207
  wine_tsx11_lock();
  XGetWindowProperty( event->display, DefaultRootWindow(event->display),
                      x11drv_atom(DndSelection), 0, 65535, FALSE,
                      AnyPropertyType, &u.atom_aux, &u.i,
                      &data_length, &aux_long, &p_data);
  wine_tsx11_unlock();
1208
  if (aux_long)
1209 1210
    WARN("property too large, truncated!\n");
  TRACE("urls=%s\n", p_data);
1211 1212 1213

  if( !aux_long && p_data) {	/* don't bother if > 64K */
    /* calculate length */
Mike McCormack's avatar
Mike McCormack committed
1214
    p = (char*) p_data;
1215 1216 1217 1218
    next = strchr(p, '\n');
    while (p) {
      if (next) *next=0;
      if (strncmp(p,"file:",5) == 0 ) {
1219
	INT len = GetShortPathNameA( p+5, NULL, 0 );
1220 1221
	if (len) drop_len += len + 1;
      }
1222 1223
      if (next) {
	*next = '\n';
1224 1225 1226 1227 1228 1229
	p = next + 1;
	next = strchr(p, '\n');
      } else {
	p = NULL;
      }
    }
1230

1231
    if( drop_len && drop_len < 65535 ) {
1232 1233
      wine_tsx11_lock();
      XQueryPointer( event->display, root_window, &u.w_aux, &u.w_aux,
Mike McCormack's avatar
Mike McCormack committed
1234
                     &x, &y, &u.i, &u.i, &u.u);
1235 1236
      x += virtual_screen_rect.left;
      y += virtual_screen_rect.top;
1237
      wine_tsx11_unlock();
1238

1239
      drop_len += sizeof(DROPFILES) + 1;
1240
      hDrop = GlobalAlloc( GMEM_SHARE, drop_len );
1241
      lpDrop = GlobalLock( hDrop );
1242

1243 1244
      if( lpDrop && (win_data = X11DRV_get_win_data( hWnd )))
      {
1245
	  lpDrop->pFiles = sizeof(DROPFILES);
1246 1247
	  lpDrop->pt.x = x;
	  lpDrop->pt.y = y;
1248
	  lpDrop->fNC =
1249 1250 1251 1252
	    ( x < (win_data->client_rect.left - win_data->whole_rect.left)  ||
	      y < (win_data->client_rect.top - win_data->whole_rect.top)    ||
	      x > (win_data->client_rect.right - win_data->whole_rect.left) ||
	      y > (win_data->client_rect.bottom - win_data->whole_rect.top) );
1253 1254
	  lpDrop->fWide = FALSE;
	  p_drop = (char*)(lpDrop + 1);
1255
      }
1256

1257 1258
      /* create message content */
      if (p_drop) {
Mike McCormack's avatar
Mike McCormack committed
1259
	p = (char*) p_data;
1260 1261 1262 1263
	next = strchr(p, '\n');
	while (p) {
	  if (next) *next=0;
	  if (strncmp(p,"file:",5) == 0 ) {
1264
	    INT len = GetShortPathNameA( p+5, p_drop, 65535 );
1265
	    if (len) {
1266
	      TRACE("drop file %s as %s\n", p+5, p_drop);
1267 1268
	      p_drop += len+1;
	    } else {
1269
	      WARN("can't convert file %s to dos name\n", p+5);
1270 1271
	    }
	  } else {
1272
	    WARN("unknown mime type %s\n", p);
1273
	  }
1274 1275
	  if (next) {
	    *next = '\n';
1276 1277 1278 1279 1280 1281 1282 1283
	    p = next + 1;
	    next = strchr(p, '\n');
	  } else {
	    p = NULL;
	  }
	  *p_drop = '\0';
	}

1284
        GlobalUnlock(hDrop);
1285
        PostMessageA( hWnd, WM_DROPFILES, (WPARAM)hDrop, 0L );
1286 1287
      }
    }
1288 1289 1290
    wine_tsx11_lock();
    if( p_data ) XFree(p_data);
    wine_tsx11_unlock();
1291 1292 1293
  }
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
/**********************************************************************
 *              handle_dnd_protocol
 */
static void handle_dnd_protocol( HWND hwnd, XClientMessageEvent *event )
{
    Window root, child;
    int root_x, root_y, child_x, child_y;
    unsigned int u;

    /* query window (drag&drop event contains only drag window) */
    wine_tsx11_lock();
    XQueryPointer( event->display, root_window, &root, &child,
                   &root_x, &root_y, &child_x, &child_y, &u);
    if (XFindContext( event->display, child, winContext, (char **)&hwnd ) != 0) hwnd = 0;
    wine_tsx11_unlock();
    if (!hwnd) return;
    if (event->data.l[0] == DndFile || event->data.l[0] == DndFiles)
        EVENT_DropFromOffiX(hwnd, event);
    else if (event->data.l[0] == DndURL)
        EVENT_DropURLs(hwnd, event);
}


struct client_message_handler
{
    int    atom;                                  /* protocol atom */
    void (*handler)(HWND, XClientMessageEvent *); /* corresponding handler function */
};

static const struct client_message_handler client_messages[] =
{
    { XATOM_WM_PROTOCOLS, handle_wm_protocols },
    { XATOM_DndProtocol,  handle_dnd_protocol },
    { XATOM_XdndEnter,    X11DRV_XDND_EnterEvent },
    { XATOM_XdndPosition, X11DRV_XDND_PositionEvent },
    { XATOM_XdndDrop,     X11DRV_XDND_DropEvent },
    { XATOM_XdndLeave,    X11DRV_XDND_LeaveEvent }
};


1334
/**********************************************************************
1335
 *           X11DRV_ClientMessage
1336
 */
1337
static void X11DRV_ClientMessage( HWND hwnd, XEvent *xev )
1338
{
1339
    XClientMessageEvent *event = &xev->xclient;
1340
    unsigned int i;
1341

1342
    if (!hwnd) return;
1343

1344
    if (event->format != 32)
1345
    {
1346 1347
        WARN( "Don't know how to handle format %d\n", event->format );
        return;
1348
    }
1349 1350

    for (i = 0; i < sizeof(client_messages)/sizeof(client_messages[0]); i++)
1351
    {
1352 1353 1354 1355 1356
        if (event->message_type == X11DRV_Atoms[client_messages[i].atom - FIRST_XATOM])
        {
            client_messages[i].handler( hwnd, event );
            return;
        }
1357
    }
1358
    TRACE( "no handler found for %ld\n", event->message_type );
1359
}
1360 1361


1362 1363 1364
/***********************************************************************
 *		X11DRV_SendInput  (X11DRV.@)
 */
1365
UINT CDECL X11DRV_SendInput( UINT count, LPINPUT inputs, int size )
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
{
    UINT i;

    for (i = 0; i < count; i++, inputs++)
    {
        switch(inputs->type)
        {
        case INPUT_MOUSE:
            X11DRV_send_mouse_input( 0, inputs->u.mi.dwFlags, inputs->u.mi.dx, inputs->u.mi.dy,
                                     inputs->u.mi.mouseData, inputs->u.mi.time,
                                     inputs->u.mi.dwExtraInfo, LLMHF_INJECTED );
            break;
        case INPUT_KEYBOARD:
            X11DRV_send_keyboard_input( inputs->u.ki.wVk, inputs->u.ki.wScan, inputs->u.ki.dwFlags,
                                        inputs->u.ki.time, inputs->u.ki.dwExtraInfo, LLKHF_INJECTED );
            break;
        case INPUT_HARDWARE:
            FIXME( "INPUT_HARDWARE not supported\n" );
            break;
        }
    }
    return count;
}