strmbase: Fixed user-after-free (Coverity).
[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         "STATUS_ITEM_CLICKED",
46         "WINDOW_CLOSE_REQUESTED",
47         "WINDOW_DID_MINIMIZE",
48         "WINDOW_DID_UNMINIMIZE",
49         "WINDOW_FRAME_CHANGED",
50         "WINDOW_GOT_FOCUS",
51         "WINDOW_LOST_FOCUS",
52     };
53
54     if (0 <= type && type < NUM_EVENT_TYPES) return event_names[type];
55     return wine_dbg_sprintf("Unknown event %d", type);
56 }
57
58
59 /***********************************************************************
60  *              get_event_mask
61  */
62 static macdrv_event_mask get_event_mask(DWORD mask)
63 {
64     macdrv_event_mask event_mask = 0;
65
66     if ((mask & QS_ALLINPUT) == QS_ALLINPUT) return -1;
67
68     if (mask & QS_KEY)
69     {
70         event_mask |= event_mask_for_type(KEY_PRESS);
71         event_mask |= event_mask_for_type(KEY_RELEASE);
72         event_mask |= event_mask_for_type(KEYBOARD_CHANGED);
73     }
74
75     if (mask & QS_MOUSEBUTTON)
76     {
77         event_mask |= event_mask_for_type(MOUSE_BUTTON);
78         event_mask |= event_mask_for_type(MOUSE_SCROLL);
79     }
80
81     if (mask & QS_MOUSEMOVE)
82     {
83         event_mask |= event_mask_for_type(MOUSE_MOVED);
84         event_mask |= event_mask_for_type(MOUSE_MOVED_ABSOLUTE);
85     }
86
87     if (mask & QS_POSTMESSAGE)
88     {
89         event_mask |= event_mask_for_type(APP_DEACTIVATED);
90         event_mask |= event_mask_for_type(DISPLAYS_CHANGED);
91         event_mask |= event_mask_for_type(STATUS_ITEM_CLICKED);
92         event_mask |= event_mask_for_type(WINDOW_CLOSE_REQUESTED);
93         event_mask |= event_mask_for_type(WINDOW_DID_MINIMIZE);
94         event_mask |= event_mask_for_type(WINDOW_DID_UNMINIMIZE);
95         event_mask |= event_mask_for_type(WINDOW_FRAME_CHANGED);
96         event_mask |= event_mask_for_type(WINDOW_GOT_FOCUS);
97         event_mask |= event_mask_for_type(WINDOW_LOST_FOCUS);
98     }
99
100     if (mask & QS_SENDMESSAGE)
101     {
102         event_mask |= event_mask_for_type(QUERY_EVENT);
103     }
104
105     return event_mask;
106 }
107
108
109 /***********************************************************************
110  *              macdrv_query_event
111  *
112  * Handler for QUERY_EVENT queries.
113  */
114 static void macdrv_query_event(HWND hwnd, macdrv_event *event)
115 {
116     BOOL success = FALSE;
117     macdrv_query *query = event->query_event.query;
118
119     switch (query->type)
120     {
121         case QUERY_DRAG_DROP:
122             TRACE("QUERY_DRAG_DROP\n");
123             success = query_drag_drop(query);
124             break;
125         case QUERY_DRAG_EXITED:
126             TRACE("QUERY_DRAG_EXITED\n");
127             success = query_drag_exited(query);
128             break;
129         case QUERY_DRAG_OPERATION:
130             TRACE("QUERY_DRAG_OPERATION\n");
131             success = query_drag_operation(query);
132             break;
133         case QUERY_PASTEBOARD_DATA:
134             TRACE("QUERY_PASTEBOARD_DATA\n");
135             success = query_pasteboard_data(hwnd, query->pasteboard_data.type);
136             break;
137         default:
138             FIXME("unrecognized query type %d\n", query->type);
139             break;
140     }
141
142     TRACE("success %d\n", success);
143     query->status = success;
144     macdrv_set_query_done(query);
145 }
146
147
148 /***********************************************************************
149  *              macdrv_handle_event
150  */
151 void macdrv_handle_event(macdrv_event *event)
152 {
153     HWND hwnd = macdrv_get_window_hwnd(event->window);
154     const macdrv_event *prev;
155     struct macdrv_thread_data *thread_data = macdrv_thread_data();
156
157     TRACE("%s for hwnd/window %p/%p\n", dbgstr_event(event->type), hwnd,
158           event->window);
159
160     prev = thread_data->current_event;
161     thread_data->current_event = event;
162
163     switch (event->type)
164     {
165     case APP_DEACTIVATED:
166         macdrv_app_deactivated();
167         break;
168     case DISPLAYS_CHANGED:
169         macdrv_displays_changed(event);
170         break;
171     case KEY_PRESS:
172     case KEY_RELEASE:
173         macdrv_key_event(hwnd, event);
174         break;
175     case KEYBOARD_CHANGED:
176         macdrv_keyboard_changed(event);
177         break;
178     case MOUSE_BUTTON:
179         macdrv_mouse_button(hwnd, event);
180         break;
181     case MOUSE_MOVED:
182     case MOUSE_MOVED_ABSOLUTE:
183         macdrv_mouse_moved(hwnd, event);
184         break;
185     case MOUSE_SCROLL:
186         macdrv_mouse_scroll(hwnd, event);
187         break;
188     case QUERY_EVENT:
189         macdrv_query_event(hwnd, event);
190         break;
191     case STATUS_ITEM_CLICKED:
192         macdrv_status_item_clicked(event);
193         break;
194     case WINDOW_CLOSE_REQUESTED:
195         macdrv_window_close_requested(hwnd);
196         break;
197     case WINDOW_DID_MINIMIZE:
198         macdrv_window_did_minimize(hwnd);
199         break;
200     case WINDOW_DID_UNMINIMIZE:
201         macdrv_window_did_unminimize(hwnd);
202         break;
203     case WINDOW_FRAME_CHANGED:
204         macdrv_window_frame_changed(hwnd, event->window_frame_changed.frame);
205         break;
206     case WINDOW_GOT_FOCUS:
207         macdrv_window_got_focus(hwnd, event);
208         break;
209     case WINDOW_LOST_FOCUS:
210         macdrv_window_lost_focus(hwnd, event);
211         break;
212     default:
213         TRACE("    ignoring\n");
214         break;
215     }
216
217     thread_data->current_event = prev;
218 }
219
220
221 /***********************************************************************
222  *              process_events
223  */
224 static int process_events(macdrv_event_queue queue, macdrv_event_mask mask)
225 {
226     macdrv_event event;
227     int count = 0;
228
229     while (macdrv_get_event_from_queue(queue, mask, &event))
230     {
231         count++;
232         macdrv_handle_event(&event);
233         macdrv_cleanup_event(&event);
234     }
235     if (count) TRACE("processed %d events\n", count);
236     return count;
237 }
238
239
240 /***********************************************************************
241  *              MsgWaitForMultipleObjectsEx   (MACDRV.@)
242  */
243 DWORD CDECL macdrv_MsgWaitForMultipleObjectsEx(DWORD count, const HANDLE *handles,
244                                                DWORD timeout, DWORD mask, DWORD flags)
245 {
246     DWORD ret;
247     struct macdrv_thread_data *data = macdrv_thread_data();
248     macdrv_event_mask event_mask = get_event_mask(mask);
249
250     TRACE("count %d, handles %p, timeout %u, mask %x, flags %x\n", count,
251           handles, timeout, mask, flags);
252
253     if (!data)
254     {
255         if (!count && !timeout) return WAIT_TIMEOUT;
256         return WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
257                                         timeout, flags & MWMO_ALERTABLE);
258     }
259
260     if (data->current_event && data->current_event->type != QUERY_EVENT)
261         event_mask = 0;  /* don't process nested events */
262
263     if (process_events(data->queue, event_mask)) ret = count - 1;
264     else if (count || timeout)
265     {
266         ret = WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
267                                        timeout, flags & MWMO_ALERTABLE);
268         if (ret == count - 1) process_events(data->queue, event_mask);
269     }
270     else ret = WAIT_TIMEOUT;
271
272     return ret;
273 }