winemac: Implement WGL_EXT_swap_control.
[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         "WINDOW_CLOSE_REQUESTED",
45         "WINDOW_DID_MINIMIZE",
46         "WINDOW_DID_UNMINIMIZE",
47         "WINDOW_FRAME_CHANGED",
48         "WINDOW_GOT_FOCUS",
49         "WINDOW_LOST_FOCUS",
50     };
51
52     if (0 <= type && type < NUM_EVENT_TYPES) return event_names[type];
53     return wine_dbg_sprintf("Unknown event %d", type);
54 }
55
56
57 /***********************************************************************
58  *              get_event_mask
59  */
60 static macdrv_event_mask get_event_mask(DWORD mask)
61 {
62     macdrv_event_mask event_mask = 0;
63
64     if ((mask & QS_ALLINPUT) == QS_ALLINPUT) return -1;
65
66     if (mask & QS_KEY)
67     {
68         event_mask |= event_mask_for_type(KEY_PRESS);
69         event_mask |= event_mask_for_type(KEY_RELEASE);
70         event_mask |= event_mask_for_type(KEYBOARD_CHANGED);
71     }
72
73     if (mask & QS_MOUSEBUTTON)
74     {
75         event_mask |= event_mask_for_type(MOUSE_BUTTON);
76         event_mask |= event_mask_for_type(MOUSE_SCROLL);
77     }
78
79     if (mask & QS_MOUSEMOVE)
80     {
81         event_mask |= event_mask_for_type(MOUSE_MOVED);
82         event_mask |= event_mask_for_type(MOUSE_MOVED_ABSOLUTE);
83     }
84
85     if (mask & QS_POSTMESSAGE)
86     {
87         event_mask |= event_mask_for_type(APP_DEACTIVATED);
88         event_mask |= event_mask_for_type(DISPLAYS_CHANGED);
89         event_mask |= event_mask_for_type(WINDOW_CLOSE_REQUESTED);
90         event_mask |= event_mask_for_type(WINDOW_DID_MINIMIZE);
91         event_mask |= event_mask_for_type(WINDOW_DID_UNMINIMIZE);
92         event_mask |= event_mask_for_type(WINDOW_FRAME_CHANGED);
93         event_mask |= event_mask_for_type(WINDOW_GOT_FOCUS);
94         event_mask |= event_mask_for_type(WINDOW_LOST_FOCUS);
95     }
96
97     return event_mask;
98 }
99
100
101 /***********************************************************************
102  *              macdrv_handle_event
103  */
104 void macdrv_handle_event(macdrv_event *event)
105 {
106     HWND hwnd = macdrv_get_window_hwnd(event->window);
107     const macdrv_event *prev;
108     struct macdrv_thread_data *thread_data = macdrv_thread_data();
109
110     TRACE("%s for hwnd/window %p/%p\n", dbgstr_event(event->type), hwnd,
111           event->window);
112
113     prev = thread_data->current_event;
114     thread_data->current_event = event;
115
116     switch (event->type)
117     {
118     case APP_DEACTIVATED:
119         macdrv_app_deactivated();
120         break;
121     case DISPLAYS_CHANGED:
122         macdrv_displays_changed(event);
123         break;
124     case KEY_PRESS:
125     case KEY_RELEASE:
126         macdrv_key_event(hwnd, event);
127         break;
128     case KEYBOARD_CHANGED:
129         macdrv_keyboard_changed(event);
130         break;
131     case MOUSE_BUTTON:
132         macdrv_mouse_button(hwnd, event);
133         break;
134     case MOUSE_MOVED:
135     case MOUSE_MOVED_ABSOLUTE:
136         macdrv_mouse_moved(hwnd, event);
137         break;
138     case MOUSE_SCROLL:
139         macdrv_mouse_scroll(hwnd, event);
140         break;
141     case WINDOW_CLOSE_REQUESTED:
142         macdrv_window_close_requested(hwnd);
143         break;
144     case WINDOW_DID_MINIMIZE:
145         macdrv_window_did_minimize(hwnd);
146         break;
147     case WINDOW_DID_UNMINIMIZE:
148         macdrv_window_did_unminimize(hwnd);
149         break;
150     case WINDOW_FRAME_CHANGED:
151         macdrv_window_frame_changed(hwnd, event->window_frame_changed.frame);
152         break;
153     case WINDOW_GOT_FOCUS:
154         macdrv_window_got_focus(hwnd, event);
155         break;
156     case WINDOW_LOST_FOCUS:
157         macdrv_window_lost_focus(hwnd, event);
158         break;
159     default:
160         TRACE("    ignoring\n");
161         break;
162     }
163
164     thread_data->current_event = prev;
165 }
166
167
168 /***********************************************************************
169  *              process_events
170  */
171 static int process_events(macdrv_event_queue queue, macdrv_event_mask mask)
172 {
173     macdrv_event event;
174     int count = 0;
175
176     while (macdrv_get_event_from_queue(queue, mask, &event))
177     {
178         count++;
179         macdrv_handle_event(&event);
180         macdrv_cleanup_event(&event);
181     }
182     if (count) TRACE("processed %d events\n", count);
183     return count;
184 }
185
186
187 /***********************************************************************
188  *              MsgWaitForMultipleObjectsEx   (MACDRV.@)
189  */
190 DWORD CDECL macdrv_MsgWaitForMultipleObjectsEx(DWORD count, const HANDLE *handles,
191                                                DWORD timeout, DWORD mask, DWORD flags)
192 {
193     DWORD ret;
194     struct macdrv_thread_data *data = macdrv_thread_data();
195     macdrv_event_mask event_mask = get_event_mask(mask);
196
197     TRACE("count %d, handles %p, timeout %u, mask %x, flags %x\n", count,
198           handles, timeout, mask, flags);
199
200     if (!data)
201     {
202         if (!count && !timeout) return WAIT_TIMEOUT;
203         return WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
204                                         timeout, flags & MWMO_ALERTABLE);
205     }
206
207     if (data->current_event) event_mask = 0;  /* don't process nested events */
208
209     if (process_events(data->queue, event_mask)) ret = count - 1;
210     else if (count || timeout)
211     {
212         ret = WaitForMultipleObjectsEx(count, handles, flags & MWMO_WAITALL,
213                                        timeout, flags & MWMO_ALERTABLE);
214         if (ret == count - 1) process_events(data->queue, event_mask);
215     }
216     else ret = WAIT_TIMEOUT;
217
218     return ret;
219 }