event.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * MACDRV event driver
 *
 * Copyright 1993 Alexandre Julliard
 *           1999 Noel Borthwick
 * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include "macdrv.h"
#include "winuser.h"

WINE_DEFAULT_DEBUG_CHANNEL(event);


/* return the name of an Mac event */
static const char *dbgstr_event(int type)
{
34
    static const char * const event_names[] = {
35
        "APP_ACTIVATED",
36
        "APP_DEACTIVATED",
37
        "APP_QUIT_REQUESTED",
38
        "DISPLAYS_CHANGED",
39
        "HOTKEY_PRESS",
40
        "IM_SET_TEXT",
41 42
        "KEY_PRESS",
        "KEY_RELEASE",
43
        "KEYBOARD_CHANGED",
44
        "LOST_PASTEBOARD_OWNERSHIP",
45
        "MOUSE_BUTTON",
46 47
        "MOUSE_MOVED",
        "MOUSE_MOVED_ABSOLUTE",
48
        "MOUSE_SCROLL",
49
        "QUERY_EVENT",
50
        "QUERY_EVENT_NO_PREEMPT_WAIT",
51
        "REASSERT_WINDOW_POSITION",
52
        "RELEASE_CAPTURE",
53
        "SENT_TEXT_INPUT",
54 55
        "STATUS_ITEM_MOUSE_BUTTON",
        "STATUS_ITEM_MOUSE_MOVE",
56
        "WINDOW_BROUGHT_FORWARD",
57
        "WINDOW_CLOSE_REQUESTED",
58
        "WINDOW_DID_UNMINIMIZE",
59 60
        "WINDOW_DRAG_BEGIN",
        "WINDOW_DRAG_END",
61
        "WINDOW_FRAME_CHANGED",
62
        "WINDOW_GOT_FOCUS",
63
        "WINDOW_LOST_FOCUS",
64
        "WINDOW_MAXIMIZE_REQUESTED",
65
        "WINDOW_MINIMIZE_REQUESTED",
66
        "WINDOW_RESIZE_ENDED",
67
        "WINDOW_RESTORE_REQUESTED",
68 69 70
    };

    if (0 <= type && type < NUM_EVENT_TYPES) return event_names[type];
71 72 73 74 75 76 77 78 79
    return wine_dbg_sprintf("Unknown event %d", type);
}


/***********************************************************************
 *              get_event_mask
 */
static macdrv_event_mask get_event_mask(DWORD mask)
{
80 81
    macdrv_event_mask event_mask = 0;

82
    if ((mask & QS_ALLINPUT) == QS_ALLINPUT) return -1;
83

84 85 86
    if (mask & QS_HOTKEY)
        event_mask |= event_mask_for_type(HOTKEY_PRESS);

87
    if (mask & QS_KEY)
88 89 90
    {
        event_mask |= event_mask_for_type(KEY_PRESS);
        event_mask |= event_mask_for_type(KEY_RELEASE);
91
        event_mask |= event_mask_for_type(KEYBOARD_CHANGED);
92
    }
93

94
    if (mask & QS_MOUSEBUTTON)
95
    {
96
        event_mask |= event_mask_for_type(MOUSE_BUTTON);
97 98
        event_mask |= event_mask_for_type(MOUSE_SCROLL);
    }
99

100 101 102 103 104 105
    if (mask & QS_MOUSEMOVE)
    {
        event_mask |= event_mask_for_type(MOUSE_MOVED);
        event_mask |= event_mask_for_type(MOUSE_MOVED_ABSOLUTE);
    }

106
    if (mask & QS_POSTMESSAGE)
107
    {
108
        event_mask |= event_mask_for_type(APP_ACTIVATED);
109
        event_mask |= event_mask_for_type(APP_DEACTIVATED);
110
        event_mask |= event_mask_for_type(APP_QUIT_REQUESTED);
111
        event_mask |= event_mask_for_type(DISPLAYS_CHANGED);
112
        event_mask |= event_mask_for_type(IM_SET_TEXT);
113
        event_mask |= event_mask_for_type(LOST_PASTEBOARD_OWNERSHIP);
114 115
        event_mask |= event_mask_for_type(STATUS_ITEM_MOUSE_BUTTON);
        event_mask |= event_mask_for_type(STATUS_ITEM_MOUSE_MOVE);
116
        event_mask |= event_mask_for_type(WINDOW_DID_UNMINIMIZE);
117
        event_mask |= event_mask_for_type(WINDOW_FRAME_CHANGED);
118
        event_mask |= event_mask_for_type(WINDOW_GOT_FOCUS);
119
        event_mask |= event_mask_for_type(WINDOW_LOST_FOCUS);
120
    }
121

122 123 124
    if (mask & QS_SENDMESSAGE)
    {
        event_mask |= event_mask_for_type(QUERY_EVENT);
125
        event_mask |= event_mask_for_type(QUERY_EVENT_NO_PREEMPT_WAIT);
126
        event_mask |= event_mask_for_type(REASSERT_WINDOW_POSITION);
127
        event_mask |= event_mask_for_type(RELEASE_CAPTURE);
128
        event_mask |= event_mask_for_type(SENT_TEXT_INPUT);
129
        event_mask |= event_mask_for_type(WINDOW_BROUGHT_FORWARD);
130
        event_mask |= event_mask_for_type(WINDOW_CLOSE_REQUESTED);
131 132
        event_mask |= event_mask_for_type(WINDOW_DRAG_BEGIN);
        event_mask |= event_mask_for_type(WINDOW_DRAG_END);
133
        event_mask |= event_mask_for_type(WINDOW_MAXIMIZE_REQUESTED);
134
        event_mask |= event_mask_for_type(WINDOW_MINIMIZE_REQUESTED);
135
        event_mask |= event_mask_for_type(WINDOW_RESIZE_ENDED);
136
        event_mask |= event_mask_for_type(WINDOW_RESTORE_REQUESTED);
137 138
    }

139
    return event_mask;
140 141 142
}


143 144 145
/***********************************************************************
 *              macdrv_query_event
 *
146
 * Handler for QUERY_EVENT and QUERY_EVENT_NO_PREEMPT_WAIT queries.
147
 */
148
static void macdrv_query_event(HWND hwnd, const macdrv_event *event)
149 150 151 152 153 154
{
    BOOL success = FALSE;
    macdrv_query *query = event->query_event.query;

    switch (query->type)
    {
155 156 157 158 159 160 161 162 163 164 165 166
        case QUERY_DRAG_DROP:
            TRACE("QUERY_DRAG_DROP\n");
            success = query_drag_drop(query);
            break;
        case QUERY_DRAG_EXITED:
            TRACE("QUERY_DRAG_EXITED\n");
            success = query_drag_exited(query);
            break;
        case QUERY_DRAG_OPERATION:
            TRACE("QUERY_DRAG_OPERATION\n");
            success = query_drag_operation(query);
            break;
167 168 169 170
        case QUERY_IME_CHAR_RECT:
            TRACE("QUERY_IME_CHAR_RECT\n");
            success = query_ime_char_rect(query);
            break;
171 172 173 174
        case QUERY_PASTEBOARD_DATA:
            TRACE("QUERY_PASTEBOARD_DATA\n");
            success = query_pasteboard_data(hwnd, query->pasteboard_data.type);
            break;
175 176 177 178
        case QUERY_RESIZE_SIZE:
            TRACE("QUERY_RESIZE_SIZE\n");
            success = query_resize_size(hwnd, query);
            break;
179 180 181 182
        case QUERY_RESIZE_START:
            TRACE("QUERY_RESIZE_START\n");
            success = query_resize_start(hwnd);
            break;
183 184 185 186
        case QUERY_MIN_MAX_INFO:
            TRACE("QUERY_MIN_MAX_INFO\n");
            success = query_min_max_info(hwnd);
            break;
187 188 189 190 191 192 193 194 195 196 197
        default:
            FIXME("unrecognized query type %d\n", query->type);
            break;
    }

    TRACE("success %d\n", success);
    query->status = success;
    macdrv_set_query_done(query);
}


198 199 200
/***********************************************************************
 *              macdrv_handle_event
 */
201
void macdrv_handle_event(const macdrv_event *event)
202 203 204 205 206 207 208 209 210 211 212 213 214
{
    HWND hwnd = macdrv_get_window_hwnd(event->window);
    const macdrv_event *prev;
    struct macdrv_thread_data *thread_data = macdrv_thread_data();

    TRACE("%s for hwnd/window %p/%p\n", dbgstr_event(event->type), hwnd,
          event->window);

    prev = thread_data->current_event;
    thread_data->current_event = event;

    switch (event->type)
    {
215 216 217
    case APP_ACTIVATED:
        macdrv_app_activated();
        break;
218 219 220
    case APP_DEACTIVATED:
        macdrv_app_deactivated();
        break;
221 222 223
    case APP_QUIT_REQUESTED:
        macdrv_app_quit_requested(event);
        break;
224 225 226
    case DISPLAYS_CHANGED:
        macdrv_displays_changed(event);
        break;
227 228 229
    case HOTKEY_PRESS:
        macdrv_hotkey_press(event);
        break;
230 231 232
    case IM_SET_TEXT:
        macdrv_im_set_text(event);
        break;
233 234 235 236
    case KEY_PRESS:
    case KEY_RELEASE:
        macdrv_key_event(hwnd, event);
        break;
237 238 239
    case KEYBOARD_CHANGED:
        macdrv_keyboard_changed(event);
        break;
240 241 242
    case LOST_PASTEBOARD_OWNERSHIP:
        macdrv_lost_pasteboard_ownership(hwnd);
        break;
243 244 245
    case MOUSE_BUTTON:
        macdrv_mouse_button(hwnd, event);
        break;
246 247 248 249
    case MOUSE_MOVED:
    case MOUSE_MOVED_ABSOLUTE:
        macdrv_mouse_moved(hwnd, event);
        break;
250 251 252
    case MOUSE_SCROLL:
        macdrv_mouse_scroll(hwnd, event);
        break;
253
    case QUERY_EVENT:
254
    case QUERY_EVENT_NO_PREEMPT_WAIT:
255 256
        macdrv_query_event(hwnd, event);
        break;
257 258 259
    case REASSERT_WINDOW_POSITION:
        macdrv_reassert_window_position(hwnd);
        break;
260 261 262
    case RELEASE_CAPTURE:
        macdrv_release_capture(hwnd, event);
        break;
263 264 265
    case SENT_TEXT_INPUT:
        macdrv_sent_text_input(event);
        break;
266 267 268 269 270
    case STATUS_ITEM_MOUSE_BUTTON:
        macdrv_status_item_mouse_button(event);
        break;
    case STATUS_ITEM_MOUSE_MOVE:
        macdrv_status_item_mouse_move(event);
271
        break;
272 273 274
    case WINDOW_BROUGHT_FORWARD:
        macdrv_window_brought_forward(hwnd);
        break;
275 276 277
    case WINDOW_CLOSE_REQUESTED:
        macdrv_window_close_requested(hwnd);
        break;
278 279 280
    case WINDOW_DID_UNMINIMIZE:
        macdrv_window_did_unminimize(hwnd);
        break;
281
    case WINDOW_DRAG_BEGIN:
282
        macdrv_window_drag_begin(hwnd, event);
283 284 285 286
        break;
    case WINDOW_DRAG_END:
        macdrv_window_drag_end(hwnd);
        break;
287
    case WINDOW_FRAME_CHANGED:
288
        macdrv_window_frame_changed(hwnd, event);
289
        break;
290 291 292
    case WINDOW_GOT_FOCUS:
        macdrv_window_got_focus(hwnd, event);
        break;
293 294 295
    case WINDOW_LOST_FOCUS:
        macdrv_window_lost_focus(hwnd, event);
        break;
296 297 298
    case WINDOW_MAXIMIZE_REQUESTED:
        macdrv_window_maximize_requested(hwnd);
        break;
299 300 301
    case WINDOW_MINIMIZE_REQUESTED:
        macdrv_window_minimize_requested(hwnd);
        break;
302 303 304
    case WINDOW_RESIZE_ENDED:
        macdrv_window_resize_ended(hwnd);
        break;
305
    case WINDOW_RESTORE_REQUESTED:
306
        macdrv_window_restore_requested(hwnd, event);
307
        break;
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    default:
        TRACE("    ignoring\n");
        break;
    }

    thread_data->current_event = prev;
}


/***********************************************************************
 *              process_events
 */
static int process_events(macdrv_event_queue queue, macdrv_event_mask mask)
{
322
    macdrv_event *event;
323 324
    int count = 0;

325
    while (macdrv_copy_event_from_queue(queue, mask, &event))
326 327
    {
        count++;
328 329
        macdrv_handle_event(event);
        macdrv_release_event(event);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    }
    if (count) TRACE("processed %d events\n", count);
    return count;
}


/***********************************************************************
 *              MsgWaitForMultipleObjectsEx   (MACDRV.@)
 */
DWORD CDECL macdrv_MsgWaitForMultipleObjectsEx(DWORD count, const HANDLE *handles,
                                               DWORD timeout, DWORD mask, DWORD flags)
{
    DWORD ret;
    struct macdrv_thread_data *data = macdrv_thread_data();
    macdrv_event_mask event_mask = get_event_mask(mask);

    TRACE("count %d, handles %p, timeout %u, mask %x, flags %x\n", count,
          handles, timeout, mask, flags);

    if (!data)
    {
        if (!count && !timeout) return WAIT_TIMEOUT;
        return WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
                                        timeout, flags & MWMO_ALERTABLE);
    }

356
    if (data->current_event && data->current_event->type != QUERY_EVENT &&
357
        data->current_event->type != QUERY_EVENT_NO_PREEMPT_WAIT &&
358 359
        data->current_event->type != APP_QUIT_REQUESTED &&
        data->current_event->type != WINDOW_DRAG_BEGIN)
360
        event_mask = 0;  /* don't process nested events */
361 362 363 364 365 366 367 368 369 370 371 372

    if (process_events(data->queue, event_mask)) ret = count - 1;
    else if (count || timeout)
    {
        ret = WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
                                       timeout, flags & MWMO_ALERTABLE);
        if (ret == count - 1) process_events(data->queue, event_mask);
    }
    else ret = WAIT_TIMEOUT;

    return ret;
}