event.c 42.9 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)(Display*, XEvent*,XPointer), 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 (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOACTIVATE) return FALSE;
467
    if (hwnd == GetDesktopWindow()) return FALSE;
468 469 470 471
    return !(style & WS_DISABLED);
}


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

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

484 485
    GetGUIThreadInfo(0, &threadinfo);
    focus = threadinfo.hwndFocus;
486
    if (focus) focus = GetAncestor( focus, GA_ROOT );
487 488 489 490
    win = X11DRV_get_whole_window(focus);

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


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

    if (!protocol) return;
507

508
    if (protocol == x11drv_atom(WM_DELETE_WINDOW))
509 510 511 512 513
    {
        /* 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.
         */
514 515 516
        if (IsWindowEnabled(hwnd))
        {
            HMENU hSysMenu;
517
            POINT pt;
518 519 520 521 522 523 524 525 526

            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;
            }
527 528 529 530
            if (GetActiveWindow() != hwnd)
            {
                LRESULT ma = SendMessageW( hwnd, WM_MOUSEACTIVATE,
                                           (WPARAM)GetAncestor( hwnd, GA_ROOT ),
531
                                           MAKELPARAM( HTCLOSE, WM_NCLBUTTONDOWN ) );
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
                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;
                }
            }
548 549 550 551
            /* Simulate clicking the caption Close button */
            GetCursorPos( &pt );
            PostMessageW( hwnd, WM_NCLBUTTONDOWN, HTCLOSE, MAKELPARAM( pt.x, pt.y ) );
            PostMessageW( hwnd, WM_LBUTTONUP, HTCLOSE, MAKELPARAM( pt.x, pt.y ) );
552
        }
553
    }
554
    else if (protocol == x11drv_atom(WM_TAKE_FOCUS))
555
    {
556 557 558
        Time event_time = (Time)event->data.l[1];
        HWND last_focus = x11drv_thread_data()->last_focus;

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

563
        if (can_activate_window(hwnd))
564
        {
565 566 567
            /* simulate a mouse click on the caption to find out
             * whether the window wants to be activated */
            LRESULT ma = SendMessageW( hwnd, WM_MOUSEACTIVATE,
568
                                       (WPARAM)GetAncestor( hwnd, GA_ROOT ),
569
                                       MAKELONG(HTCAPTION,WM_LBUTTONDOWN) );
570 571
            if (ma != MA_NOACTIVATEANDEAT && ma != MA_NOACTIVATE)
            {
572
                set_focus( event->display, hwnd, event_time );
573 574
                return;
            }
575
        }
576 577 578 579 580 581 582 583
        else if (hwnd == GetDesktopWindow())
        {
            hwnd = GetForegroundWindow();
            if (!hwnd) hwnd = last_focus;
            if (!hwnd) hwnd = GetDesktopWindow();
            set_focus( event->display, hwnd, event_time );
            return;
        }
584 585 586 587 588
        /* 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;
589
        if (hwnd && can_activate_window(hwnd)) set_focus( event->display, hwnd, event_time );
590 591 592
    }
    else if (protocol == x11drv_atom(_NET_WM_PING))
    {
593 594 595 596
      XClientMessageEvent xev;
      xev = *event;
      
      TRACE("NET_WM Ping\n");
597
      wine_tsx11_lock();
598 599
      xev.window = DefaultRootWindow(xev.display);
      XSendEvent(xev.display, xev.window, False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*)&xev);
600
      wine_tsx11_unlock();
601 602
      /* this line is semi-stolen from gtk2 */
      TRACE("NET_WM Pong\n");
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    }
}


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

/**********************************************************************
620
 *              X11DRV_FocusIn
621
 */
622
static void X11DRV_FocusIn( HWND hwnd, XEvent *xev )
623
{
624
    XFocusChangeEvent *event = &xev->xfocus;
625 626
    XIC xic;

627 628
    if (!hwnd) return;

629
    TRACE( "win %p xwin %lx detail=%s\n", hwnd, event->window, focus_details[event->detail] );
630 631 632

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

633 634 635 636 637 638
    if ((xic = X11DRV_get_ic( hwnd )))
    {
        wine_tsx11_lock();
        XSetICFocus( xic );
        wine_tsx11_unlock();
    }
639
    if (use_take_focus) return;  /* ignore FocusIn if we are using take focus */
640

641
    if (!can_activate_window(hwnd))
642 643
    {
        HWND hwnd = GetFocus();
644
        if (hwnd) hwnd = GetAncestor( hwnd, GA_ROOT );
645 646
        if (!hwnd) hwnd = GetActiveWindow();
        if (!hwnd) hwnd = x11drv_thread_data()->last_focus;
647
        if (hwnd && can_activate_window(hwnd)) set_focus( event->display, hwnd, CurrentTime );
648
    }
649
    else SetForegroundWindow( hwnd );
650 651
}

652

653
/**********************************************************************
654
 *              X11DRV_FocusOut
655 656
 *
 * Note: only top-level windows get FocusOut events.
657
 */
658
static void X11DRV_FocusOut( HWND hwnd, XEvent *xev )
659
{
660
    XFocusChangeEvent *event = &xev->xfocus;
661 662 663
    HWND hwnd_tmp;
    Window focus_win;
    int revert;
664
    XIC xic;
665

666 667
    if (!hwnd) return;

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

    if (event->detail == NotifyPointer) return;
671 672
    if (ximInComposeMode) return;

673
    x11drv_thread_data()->last_focus = hwnd;
674 675 676 677 678 679
    if ((xic = X11DRV_get_ic( hwnd )))
    {
        wine_tsx11_lock();
        XUnsetICFocus( xic );
        wine_tsx11_unlock();
    }
680
    if (hwnd != GetForegroundWindow()) return;
681
    if (root_window != DefaultRootWindow(event->display)) return;
682
    SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
683 684 685 686

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

687
    wine_tsx11_lock();
688
    XGetInputFocus( event->display, &focus_win, &revert );
689 690
    if (focus_win)
    {
691
        if (XFindContext( event->display, focus_win, winContext, (char **)&hwnd_tmp ) != 0)
692 693 694 695 696
            focus_win = 0;
    }
    wine_tsx11_unlock();

    if (!focus_win)
697 698 699 700 701 702 703
    {
        /* 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())
        {
704 705
            TRACE( "lost focus, setting fg to desktop\n" );
            SetForegroundWindow( GetDesktopWindow() );
706 707
        }
    }
708 709 710
}


711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
/***********************************************************************
 *           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 )
        {
744
            req->window      = wine_server_user_handle( hwnd );
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
            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 );
}


762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
/**********************************************************************
 *		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 */
    }
}


780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
/***********************************************************************
 *     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);
}


810 811 812 813 814 815 816 817 818 819 820 821 822
/***********************************************************************
 *		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;
823
    if (!data->mapped || data->iconic || !data->managed) return;
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

    /* 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 );
840 841
    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,
842 843 844
           event->x, event->y, event->width, event->height );
    X11DRV_X_to_window_rect( data, &rect );

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    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;
        }
    }

864 865
    /* Compare what has changed */

866 867 868 869 870 871 872
    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;
873 874
    else
        TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
875
               hwnd, data->window_rect.left, data->window_rect.top, x, y );
876

877 878 879
    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))
880 881 882 883 884 885
    {
        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",
886 887
               hwnd, data->window_rect.right - data->window_rect.left,
               data->window_rect.bottom - data->window_rect.top, cx, cy );
888 889 890 891 892

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


893 894 895
/***********************************************************************
 *           get_window_wm_state
 */
896
static int get_window_wm_state( Display *display, struct x11drv_win_data *data )
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
{
    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 ))
    {
912
        if (type == x11drv_atom(WM_STATE) && get_property_size( format, count ) >= sizeof(*state))
913 914 915 916 917 918 919 920
            ret = state->state;
        XFree( state );
    }
    wine_tsx11_unlock();
    return ret;
}


921
/***********************************************************************
922 923 924
 *           handle_wm_state_notify
 *
 * Handle a PropertyNotify for WM_STATE.
925
 */
926 927
static void handle_wm_state_notify( struct x11drv_win_data *data, XPropertyEvent *event,
                                    BOOL update_window )
928
{
929
    switch(event->state)
930
    {
931
    case PropertyDelete:
932
        TRACE( "%p/%lx: WM_STATE deleted from %d\n", data->hwnd, data->whole_window, data->wm_state );
933
        data->wm_state = WithdrawnState;
934
        break;
935
    case PropertyNewValue:
936
        {
937
            int old_state = data->wm_state;
938 939 940
            int new_state = get_window_wm_state( event->display, data );
            if (new_state != -1 && new_state != data->wm_state)
            {
941 942
                TRACE( "%p/%lx: new WM_STATE %d from %d\n",
                       data->hwnd, data->whole_window, new_state, old_state );
943
                data->wm_state = new_state;
944 945 946
                /* ignore the initial state transition out of withdrawn state */
                /* metacity does Withdrawn->NormalState->IconicState when mapping an iconic window */
                if (!old_state) return;
947 948 949
            }
        }
        break;
950
    }
951 952 953 954 955 956

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

    if (data->iconic && data->wm_state == NormalState)  /* restore window */
    {
        data->iconic = FALSE;
957 958 959 960 961 962 963 964 965 966
        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 );
        }
967 968 969 970 971
    }
    else if (!data->iconic && data->wm_state == IconicState)
    {
        TRACE( "minimizing win %p/%lx\n", data->hwnd, data->whole_window );
        data->iconic = TRUE;
972
        SendMessageW( data->hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
973
    }
974
}
975

976

977
/***********************************************************************
978
 *           X11DRV_PropertyNotify
979
 */
980
static void X11DRV_PropertyNotify( HWND hwnd, XEvent *xev )
981 982 983 984 985 986 987
{
    XPropertyEvent *event = &xev->xproperty;
    struct x11drv_win_data *data;

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

988
    if (event->atom == x11drv_atom(WM_STATE)) handle_wm_state_notify( data, event, TRUE );
989 990 991
}


992 993 994
/* 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 )
{
995 996 997
    if (event->xany.window != (Window)arg) return 0;
    return (event->type == DestroyNotify ||
            (event->type == PropertyNotify && event->xproperty.atom == x11drv_atom(WM_STATE)));
998 999 1000 1001 1002
}

/***********************************************************************
 *           wait_for_withdrawn_state
 */
1003
void wait_for_withdrawn_state( Display *display, struct x11drv_win_data *data, BOOL set )
1004 1005 1006
{
    DWORD end = GetTickCount() + 2000;

1007
    if (!data->managed) return;
1008

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

1012 1013
    while (data->whole_window && ((data->wm_state == WithdrawnState) == !set))
    {
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        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();
1026
                handle_wm_state_notify( data, &event.xproperty, FALSE );
1027 1028 1029 1030 1031 1032
                wine_tsx11_lock();
            }
        }
        wine_tsx11_unlock();

        if (!count)
1033
        {
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
            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;
            }
1044 1045
        }
    }
1046
    TRACE( "window %p/%lx state now %d\n", data->hwnd, data->whole_window, data->wm_state );
1047 1048 1049
}


1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
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 ))
    {
1062 1063
        POINT pt = *lpPt;
        ScreenToClient( hQueryWnd, &pt );
1064 1065
        GetClientRect( hQueryWnd, &tempRect );

1066
        if (PtInRect( &tempRect, pt))
1067
        {
1068 1069
            HWND ret = ChildWindowFromPointEx( hQueryWnd, pt, CWP_SKIPINVISIBLE|CWP_SKIPDISABLED );
            if (ret && ret != hQueryWnd)
1070
            {
1071 1072
                ret = find_drop_window( ret, lpPt );
                if (ret) return ret;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
            }
        }
    }

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

    return hQueryWnd;
}

1084 1085 1086
/**********************************************************************
 *           EVENT_DropFromOffix
 *
Austin English's avatar
Austin English committed
1087
 * don't know if it still works (last Changelog is from 96/11/04)
1088
 */
1089
static void EVENT_DropFromOffiX( HWND hWnd, XClientMessageEvent *event )
1090
{
1091
    struct x11drv_win_data *data;
1092 1093 1094
    unsigned long	data_length;
    unsigned long	aux_long;
    unsigned char*	p_data = NULL;
1095 1096
    Atom atom_aux;
    int			x, y, dummy;
1097
    BOOL	        bAccept;
1098
    Window		win, w_aux_root, w_aux_child;
1099

1100
    win = X11DRV_get_whole_window(hWnd);
1101
    wine_tsx11_lock();
1102
    XQueryPointer( event->display, win, &w_aux_root, &w_aux_child,
1103
                   &x, &y, &dummy, &dummy, (unsigned int*)&aux_long);
1104 1105
    x += virtual_screen_rect.left;
    y += virtual_screen_rect.top;
1106
    wine_tsx11_unlock();
1107

1108
    if (!(data = X11DRV_get_win_data( hWnd ))) return;
1109

1110 1111
    /* find out drop point and drop window */
    if( x < 0 || y < 0 ||
1112 1113
        x > (data->whole_rect.right - data->whole_rect.left) ||
        y > (data->whole_rect.bottom - data->whole_rect.top) )
1114
    {   
1115
	bAccept = GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES;
1116 1117 1118 1119
	x = 0;
	y = 0; 
    }
    else
1120
    {
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    	POINT	pt = { x, y };
        HWND    hwndDrop = find_drop_window( hWnd, &pt );
	if (hwndDrop)
	{
	    x = pt.x;
	    y = pt.y;
	    bAccept = TRUE;
	}
	else
	{
	    bAccept = FALSE;
	}
1133
    }
1134 1135 1136

    if (!bAccept) return;

1137 1138 1139
    wine_tsx11_lock();
    XGetWindowProperty( event->display, DefaultRootWindow(event->display),
                        x11drv_atom(DndSelection), 0, 65535, FALSE,
1140
                        AnyPropertyType, &atom_aux, &dummy,
1141 1142
                        &data_length, &aux_long, &p_data);
    wine_tsx11_unlock();
1143 1144

    if( !aux_long && p_data)  /* don't bother if > 64K */
1145
    {
1146
        char *p = (char *)p_data;
1147
        char *p_drop;
1148

1149 1150 1151
        aux_long = 0;
        while( *p )  /* calculate buffer size */
        {
1152 1153
            INT len = GetShortPathNameA( p, NULL, 0 );
            if (len) aux_long += len + 1;
1154 1155 1156 1157 1158 1159 1160 1161 1162
            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 );
1163
            lpDrop = GlobalLock( hDrop );
1164 1165 1166 1167 1168 1169

            if( lpDrop )
            {
                lpDrop->pFiles = sizeof(DROPFILES);
                lpDrop->pt.x = x;
                lpDrop->pt.y = y;
1170
                lpDrop->fNC = FALSE;
1171 1172
                lpDrop->fWide = FALSE;
                p_drop = (char *)(lpDrop + 1);
1173
                p = (char *)p_data;
1174 1175
                while(*p)
                {
1176
                    if (GetShortPathNameA( p, p_drop, aux_long - (p_drop - (char *)lpDrop) ))
1177 1178 1179 1180 1181 1182 1183 1184
                        p_drop += strlen( p_drop ) + 1;
                    p += strlen(p) + 1;
                }
                *p_drop = '\0';
                PostMessageA( hWnd, WM_DROPFILES, (WPARAM)hDrop, 0L );
            }
        }
    }
1185 1186 1187
    wine_tsx11_lock();
    if( p_data ) XFree(p_data);
    wine_tsx11_unlock();
1188 1189 1190 1191 1192
}

/**********************************************************************
 *           EVENT_DropURLs
 *
1193
 * drop items are separated by \n
1194 1195 1196 1197
 * each item is prefixed by its mime type
 *
 * event->data.l[3], event->data.l[4] contains drop x,y position
 */
1198
static void EVENT_DropURLs( HWND hWnd, XClientMessageEvent *event )
1199
{
1200
  struct x11drv_win_data *win_data;
1201 1202 1203 1204 1205
  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;
1206 1207 1208
  int		x, y;
  DROPFILES *lpDrop;
  HDROP hDrop;
1209 1210 1211 1212
  union {
    Atom	atom_aux;
    int         i;
    Window      w_aux;
Mike McCormack's avatar
Mike McCormack committed
1213
    unsigned int u;
1214 1215
  }		u; /* unused */

1216
  if (!(GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)) return;
1217

1218 1219 1220 1221 1222 1223
  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();
1224
  if (aux_long)
1225 1226
    WARN("property too large, truncated!\n");
  TRACE("urls=%s\n", p_data);
1227 1228 1229

  if( !aux_long && p_data) {	/* don't bother if > 64K */
    /* calculate length */
Mike McCormack's avatar
Mike McCormack committed
1230
    p = (char*) p_data;
1231 1232 1233 1234
    next = strchr(p, '\n');
    while (p) {
      if (next) *next=0;
      if (strncmp(p,"file:",5) == 0 ) {
1235
	INT len = GetShortPathNameA( p+5, NULL, 0 );
1236 1237
	if (len) drop_len += len + 1;
      }
1238 1239
      if (next) {
	*next = '\n';
1240 1241 1242 1243 1244 1245
	p = next + 1;
	next = strchr(p, '\n');
      } else {
	p = NULL;
      }
    }
1246

1247
    if( drop_len && drop_len < 65535 ) {
1248 1249
      wine_tsx11_lock();
      XQueryPointer( event->display, root_window, &u.w_aux, &u.w_aux,
Mike McCormack's avatar
Mike McCormack committed
1250
                     &x, &y, &u.i, &u.i, &u.u);
1251 1252
      x += virtual_screen_rect.left;
      y += virtual_screen_rect.top;
1253
      wine_tsx11_unlock();
1254

1255
      drop_len += sizeof(DROPFILES) + 1;
1256
      hDrop = GlobalAlloc( GMEM_SHARE, drop_len );
1257
      lpDrop = GlobalLock( hDrop );
1258

1259 1260
      if( lpDrop && (win_data = X11DRV_get_win_data( hWnd )))
      {
1261
	  lpDrop->pFiles = sizeof(DROPFILES);
1262 1263
	  lpDrop->pt.x = x;
	  lpDrop->pt.y = y;
1264
	  lpDrop->fNC =
1265 1266 1267 1268
	    ( 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) );
1269 1270
	  lpDrop->fWide = FALSE;
	  p_drop = (char*)(lpDrop + 1);
1271
      }
1272

1273 1274
      /* create message content */
      if (p_drop) {
Mike McCormack's avatar
Mike McCormack committed
1275
	p = (char*) p_data;
1276 1277 1278 1279
	next = strchr(p, '\n');
	while (p) {
	  if (next) *next=0;
	  if (strncmp(p,"file:",5) == 0 ) {
1280
	    INT len = GetShortPathNameA( p+5, p_drop, 65535 );
1281
	    if (len) {
1282
	      TRACE("drop file %s as %s\n", p+5, p_drop);
1283 1284
	      p_drop += len+1;
	    } else {
1285
	      WARN("can't convert file %s to dos name\n", p+5);
1286 1287
	    }
	  } else {
1288
	    WARN("unknown mime type %s\n", p);
1289
	  }
1290 1291
	  if (next) {
	    *next = '\n';
1292 1293 1294 1295 1296 1297 1298 1299
	    p = next + 1;
	    next = strchr(p, '\n');
	  } else {
	    p = NULL;
	  }
	  *p_drop = '\0';
	}

1300
        GlobalUnlock(hDrop);
1301
        PostMessageA( hWnd, WM_DROPFILES, (WPARAM)hDrop, 0L );
1302 1303
      }
    }
1304 1305 1306
    wine_tsx11_lock();
    if( p_data ) XFree(p_data);
    wine_tsx11_unlock();
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 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
/**********************************************************************
 *              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 }
};


1350
/**********************************************************************
1351
 *           X11DRV_ClientMessage
1352
 */
1353
static void X11DRV_ClientMessage( HWND hwnd, XEvent *xev )
1354
{
1355
    XClientMessageEvent *event = &xev->xclient;
1356
    unsigned int i;
1357

1358
    if (!hwnd) return;
1359

1360
    if (event->format != 32)
1361
    {
1362 1363
        WARN( "Don't know how to handle format %d\n", event->format );
        return;
1364
    }
1365 1366

    for (i = 0; i < sizeof(client_messages)/sizeof(client_messages[0]); i++)
1367
    {
1368 1369 1370 1371 1372
        if (event->message_type == X11DRV_Atoms[client_messages[i].atom - FIRST_XATOM])
        {
            client_messages[i].handler( hwnd, event );
            return;
        }
1373
    }
1374
    TRACE( "no handler found for %ld\n", event->message_type );
1375
}
1376 1377


1378 1379 1380
/***********************************************************************
 *		X11DRV_SendInput  (X11DRV.@)
 */
1381
UINT CDECL X11DRV_SendInput( UINT count, LPINPUT inputs, int size )
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
{
    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;
}