mshtml: Use lazy allocation for connection points.
[wine] / dlls / winemac.drv / event.c
1 /*
2  * MACDRV event driver
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include "macdrv.h"
26 #include "winuser.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(event);
29
30
31 /* return the name of an Mac event */
32 static const char *dbgstr_event(int type)
33 {
34     static const char * const event_names[] = {
35         "APP_DEACTIVATED",
36         "DISPLAYS_CHANGED",
37         "KEY_PRESS",
38         "KEY_RELEASE",
39         "KEYBOARD_CHANGED",
40         "MOUSE_BUTTON",
41         "MOUSE_MOVED",
42         "MOUSE_MOVED_ABSOLUTE",
43         "MOUSE_SCROLL",
44         "QUERY_EVENT",
45         "WINDOW_CLOSE_REQUESTED",
46         "WINDOW_DID_MINIMIZE",
47         "WINDOW_DID_UNMINIMIZE",
48         "WINDOW_FRAME_CHANGED",
49         "WINDOW_GOT_FOCUS",
50         "WINDOW_LOST_FOCUS",
51     };
52
53     if (0 <= type && type < NUM_EVENT_TYPES) return event_names[type];
54     return wine_dbg_sprintf("Unknown event %d", type);
55 }
56
57
58 /***********************************************************************
59  *              get_event_mask
60  */
61 static macdrv_event_mask get_event_mask(DWORD mask)
62 {
63     macdrv_event_mask event_mask = 0;
64
65     if ((mask & QS_ALLINPUT) == QS_ALLINPUT) return -1;
66
67     if (mask & QS_KEY)
68     {
69         event_mask |= event_mask_for_type(KEY_PRESS);
70         event_mask |= event_mask_for_type(KEY_RELEASE);
71         event_mask |= event_mask_for_type(KEYBOARD_CHANGED);
72     }
73
74     if (mask & QS_MOUSEBUTTON)
75     {
76         event_mask |= event_mask_for_type(MOUSE_BUTTON);
77         event_mask |= event_mask_for_type(MOUSE_SCROLL);
78     }
79
80     if (mask & QS_MOUSEMOVE)
81     {
82         event_mask |= event_mask_for_type(MOUSE_MOVED);
83         event_mask |= event_mask_for_type(MOUSE_MOVED_ABSOLUTE);
84     }
85
86     if (mask & QS_POSTMESSAGE)
87     {
88         event_mask |= event_mask_for_type(APP_DEACTIVATED);
89         event_mask |= event_mask_for_type(DISPLAYS_CHANGED);
90         event_mask |= event_mask_for_type(WINDOW_CLOSE_REQUESTED);
91         event_mask |= event_mask_for_type(WINDOW_DID_MINIMIZE);
92         event_mask |= event_mask_for_type(WINDOW_DID_UNMINIMIZE);
93         event_mask |= event_mask_for_type(WINDOW_FRAME_CHANGED);
94         event_mask |= event_mask_for_type(WINDOW_GOT_FOCUS);
95         event_mask |= event_mask_for_type(WINDOW_LOST_FOCUS);
96     }
97
98     if (mask & QS_SENDMESSAGE)
99     {
100         event_mask |= event_mask_for_type(QUERY_EVENT);
101     }
102
103     return event_mask;
104 }
105
106
107 /***********************************************************************
108  *              macdrv_query_event
109  *
110  * Handler for QUERY_EVENT queries.
111  */
112 static void macdrv_query_event(HWND hwnd, macdrv_event *event)
113 {
114     BOOL success = FALSE;
115     macdrv_query *query = event->query_event.query;
116
117     switch (query->type)
118     {
119         case QUERY_DRAG_DROP:
120             TRACE("QUERY_DRAG_DROP\n");
121             success = query_drag_drop(query);
122             break;
123         case QUERY_DRAG_EXITED:
124             TRACE("QUERY_DRAG_EXITED\n");
125             success = query_drag_exited(query);
126             break;
127         case QUERY_DRAG_OPERATION:
128             TRACE("QUERY_DRAG_OPERATION\n");
129             success = query_drag_operation(query);
130             break;
131         case QUERY_PASTEBOARD_DATA:
132             TRACE("QUERY_PASTEBOARD_DATA\n");
133             success = query_pasteboard_data(hwnd, query->pasteboard_data.type);
134             break;
135         default:
136             FIXME("unrecognized query type %d\n", query->type);
137             break;
138     }
139
140     TRACE("success %d\n", success);
141     query->status = success;
142     macdrv_set_query_done(query);
143 }
144
145
146 /***********************************************************************
147  *              macdrv_handle_event
148  */
149 void macdrv_handle_event(macdrv_event *event)
150 {
151     HWND hwnd = macdrv_get_window_hwnd(event->window);
152     const macdrv_event *prev;
153     struct macdrv_thread_data *thread_data = macdrv_thread_data();
154
155     TRACE("%s for hwnd/window %p/%p\n", dbgstr_event(event->type), hwnd,
156           event->window);
157
158     prev = thread_data->current_event;
159     thread_data->current_event = event;
160
161     switch (event->type)
162     {
163     case APP_DEACTIVATED:
164         macdrv_app_deactivated();
165         break;
166     case DISPLAYS_CHANGED:
167         macdrv_displays_changed(event);
168         break;
169     case KEY_PRESS:
170     case KEY_RELEASE:
171         macdrv_key_event(hwnd, event);
172         break;
173     case KEYBOARD_CHANGED:
174         macdrv_keyboard_changed(event);
175         break;
176     case MOUSE_BUTTON:
177         macdrv_mouse_button(hwnd, event);
178         break;
179     case MOUSE_MOVED:
180     case MOUSE_MOVED_ABSOLUTE:
181         macdrv_mouse_moved(hwnd, event);
182         break;
183     case MOUSE_SCROLL:
184         macdrv_mouse_scroll(hwnd, event);
185         break;
186     case QUERY_EVENT:
187         macdrv_query_event(hwnd, event);
188         break;
189     case WINDOW_CLOSE_REQUESTED:
190         macdrv_window_close_requested(hwnd);
191         break;
192     case WINDOW_DID_MINIMIZE:
193         macdrv_window_did_minimize(hwnd);
194         break;
195     case WINDOW_DID_UNMINIMIZE:
196         macdrv_window_did_unminimize(hwnd);
197         break;
198     case WINDOW_FRAME_CHANGED:
199         macdrv_window_frame_changed(hwnd, event->window_frame_changed.frame);
200         break;
201     case WINDOW_GOT_FOCUS:
202         macdrv_window_got_focus(hwnd, event);
203         break;
204     case WINDOW_LOST_FOCUS:
205         macdrv_window_lost_focus(hwnd, event);
206         break;
207     default:
208         TRACE("    ignoring\n");
209         break;
210     }
211
212     thread_data->current_event = prev;
213 }
214
215
216 /***********************************************************************
217  *              process_events
218  */
219 static int process_events(macdrv_event_queue queue, macdrv_event_mask mask)
220 {
221     macdrv_event event;
222     int count = 0;
223
224     while (macdrv_get_event_from_queue(queue, mask, &event))
225     {
226         count++;
227         macdrv_handle_event(&event);
228         macdrv_cleanup_event(&event);
229     }
230     if (count) TRACE("processed %d events\n", count);
231     return count;
232 }
233
234
235 /***********************************************************************
236  *              MsgWaitForMultipleObjectsEx   (MACDRV.@)
237  */
238 DWORD CDECL macdrv_MsgWaitForMultipleObjectsEx(DWORD count, const HANDLE *handles,
239                                                DWORD timeout, DWORD mask, DWORD flags)
240 {
241     DWORD ret;
242     struct macdrv_thread_data *data = macdrv_thread_data();
243     macdrv_event_mask event_mask = get_event_mask(mask);
244
245     TRACE("count %d, handles %p, timeout %u, mask %x, flags %x\n", count,
246           handles, timeout, mask, flags);
247
248     if (!data)
249     {
250         if (!count && !timeout) return WAIT_TIMEOUT;
251         return WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
252                                         timeout, flags & MWMO_ALERTABLE);
253     }
254
255     if (data->current_event && data->current_event->type != QUERY_EVENT)
256         event_mask = 0;  /* don't process nested events */
257
258     if (process_events(data->queue, event_mask)) ret = count - 1;
259     else if (count || timeout)
260     {
261         ret = WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
262                                        timeout, flags & MWMO_ALERTABLE);
263         if (ret == count - 1) process_events(data->queue, event_mask);
264     }
265     else ret = WAIT_TIMEOUT;
266
267     return ret;
268 }