]>
Commit | Line | Data |
---|---|---|
8000ae7f | 1 | /////////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/gtk/evtloop.cpp |
8000ae7f VZ |
3 | // Purpose: implements wxEventLoop for GTK+ |
4 | // Author: Vadim Zeitlin | |
8000ae7f VZ |
5 | // Created: 10.07.01 |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
821d856a | 8 | // (c) 2013 Rob Bresalier, Vadim Zeitlin |
526954c5 | 9 | // Licence: wxWindows licence |
8000ae7f VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
8000ae7f VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
8000ae7f | 27 | #include "wx/evtloop.h" |
5cd99866 | 28 | #include "wx/evtloopsrc.h" |
670f9935 WS |
29 | |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/app.h" | |
bcf79477 | 32 | #include "wx/log.h" |
670f9935 | 33 | #endif // WX_PRECOMP |
8000ae7f | 34 | |
71e9885b VZ |
35 | #include "wx/private/eventloopsourcesmanager.h" |
36 | #include "wx/apptrait.h" | |
37 | ||
8000ae7f | 38 | #include <gtk/gtk.h> |
6b8ef0b3 | 39 | #include <glib.h> |
8000ae7f | 40 | |
8000ae7f VZ |
41 | // ============================================================================ |
42 | // wxEventLoop implementation | |
43 | // ============================================================================ | |
44 | ||
dde19c21 FM |
45 | extern GtkWidget *wxGetRootWindow(); |
46 | ||
8000ae7f VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // wxEventLoop running and exiting | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
564c7fc4 | 51 | wxGUIEventLoop::wxGUIEventLoop() |
8000ae7f | 52 | { |
564c7fc4 | 53 | m_exitcode = 0; |
8000ae7f VZ |
54 | } |
55 | ||
c738d187 | 56 | int wxGUIEventLoop::DoRun() |
8000ae7f | 57 | { |
d3ad22bd VZ |
58 | guint loopLevel = gtk_main_level(); |
59 | ||
60 | // This is placed inside of a loop to take into account nested | |
61 | // event loops. For example, inside this event loop, we may receive | |
62 | // Exit() for a different event loop (which we are currently inside of) | |
63 | // That Exit() will cause this gtk_main() to exit so we need to re-enter it. | |
64 | while ( !m_shouldExit ) | |
65 | { | |
66 | gtk_main(); | |
67 | } | |
68 | ||
69 | // Force the enclosing event loop to also exit to see if it is done in case | |
70 | // that event loop had Exit() called inside of the just ended loop. If it | |
71 | // is not time yet for that event loop to exit, it will be executed again | |
72 | // due to the while() loop on m_shouldExit(). | |
73 | // | |
74 | // This is unnecessary if we are the top level loop, i.e. loop of level 0. | |
75 | if ( loopLevel ) | |
76 | { | |
77 | gtk_main_quit(); | |
78 | } | |
8000ae7f | 79 | |
16d17da6 VZ |
80 | OnExit(); |
81 | ||
564c7fc4 | 82 | return m_exitcode; |
8000ae7f VZ |
83 | } |
84 | ||
d3ad22bd | 85 | void wxGUIEventLoop::ScheduleExit(int rc) |
8000ae7f | 86 | { |
d3ad22bd | 87 | wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not started") ); |
8000ae7f | 88 | |
564c7fc4 | 89 | m_exitcode = rc; |
8000ae7f | 90 | |
d3ad22bd VZ |
91 | m_shouldExit = true; |
92 | ||
8000ae7f VZ |
93 | gtk_main_quit(); |
94 | } | |
95 | ||
564c7fc4 VZ |
96 | void wxGUIEventLoop::WakeUp() |
97 | { | |
98 | // TODO: idle events handling should really be done by wxEventLoop itself | |
99 | // but for now it's completely in gtk/app.cpp so just call there when | |
100 | // we have wxTheApp and hope that it doesn't matter that we do | |
101 | // nothing when we don't... | |
102 | if ( wxTheApp ) | |
103 | wxTheApp->WakeUpIdle(); | |
104 | } | |
105 | ||
6b8ef0b3 VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // wxEventLoop adding & removing sources | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
5cd99866 VZ |
110 | #if wxUSE_EVENTLOOP_SOURCE |
111 | ||
6b8ef0b3 VZ |
112 | extern "C" |
113 | { | |
357f4c81 | 114 | static gboolean wx_on_channel_event(GIOChannel *channel, |
5cd99866 VZ |
115 | GIOCondition condition, |
116 | gpointer data) | |
6b8ef0b3 | 117 | { |
357f4c81 VZ |
118 | wxUnusedVar(channel); // Unused if !wxUSE_LOG || !wxDEBUG_LEVEL |
119 | ||
5cd99866 VZ |
120 | wxLogTrace(wxTRACE_EVT_SOURCE, |
121 | "wx_on_channel_event, fd=%d, condition=%08x", | |
122 | g_io_channel_unix_get_fd(channel), condition); | |
6b8ef0b3 | 123 | |
5cd99866 VZ |
124 | wxEventLoopSourceHandler * const |
125 | handler = static_cast<wxEventLoopSourceHandler *>(data); | |
6b8ef0b3 | 126 | |
c36ddebf | 127 | if ( (condition & G_IO_IN) || (condition & G_IO_PRI) || (condition & G_IO_HUP) ) |
6b8ef0b3 | 128 | handler->OnReadWaiting(); |
c36ddebf | 129 | |
5cd99866 | 130 | if (condition & G_IO_OUT) |
6b8ef0b3 | 131 | handler->OnWriteWaiting(); |
c36ddebf VZ |
132 | |
133 | if ( (condition & G_IO_ERR) || (condition & G_IO_NVAL) ) | |
6b8ef0b3 | 134 | handler->OnExceptionWaiting(); |
6b8ef0b3 VZ |
135 | |
136 | // we never want to remove source here, so always return true | |
c36ddebf VZ |
137 | // |
138 | // The source may have been removed by the handler, so it may be | |
139 | // a good idea to return FALSE when the source has already been | |
140 | // removed. However, that would involve somehow informing this function | |
141 | // that the source was removed, which is not trivial to implement | |
142 | // and handle all cases. It has been found through testing | |
143 | // that if the source was removed by the handler, that even if we | |
144 | // return TRUE here, the source/callback will not get called again. | |
6b8ef0b3 VZ |
145 | return TRUE; |
146 | } | |
147 | } | |
148 | ||
71e9885b | 149 | class wxGUIEventLoopSourcesManager : public wxEventLoopSourcesManagerBase |
6b8ef0b3 | 150 | { |
71e9885b VZ |
151 | public: |
152 | virtual wxEventLoopSource* | |
153 | AddSourceForFD(int fd, wxEventLoopSourceHandler *handler, int flags) | |
154 | { | |
155 | wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" ); | |
156 | ||
157 | int condition = 0; | |
158 | if ( flags & wxEVENT_SOURCE_INPUT ) | |
159 | condition |= G_IO_IN | G_IO_PRI | G_IO_HUP; | |
160 | if ( flags & wxEVENT_SOURCE_OUTPUT ) | |
161 | condition |= G_IO_OUT; | |
162 | if ( flags & wxEVENT_SOURCE_EXCEPTION ) | |
163 | condition |= G_IO_ERR | G_IO_NVAL; | |
164 | ||
165 | GIOChannel* channel = g_io_channel_unix_new(fd); | |
166 | const unsigned sourceId = g_io_add_watch | |
167 | ( | |
168 | channel, | |
169 | (GIOCondition)condition, | |
170 | &wx_on_channel_event, | |
171 | handler | |
172 | ); | |
173 | // it was ref'd by g_io_add_watch() so we can unref it here | |
174 | g_io_channel_unref(channel); | |
175 | ||
176 | if ( !sourceId ) | |
177 | return NULL; | |
178 | ||
179 | wxLogTrace(wxTRACE_EVT_SOURCE, | |
180 | "Adding event loop source for fd=%d with GTK id=%u", | |
181 | fd, sourceId); | |
182 | ||
183 | ||
184 | return new wxGTKEventLoopSource(sourceId, handler, flags); | |
185 | } | |
186 | }; | |
5cd99866 | 187 | |
71e9885b VZ |
188 | wxEventLoopSourcesManagerBase* wxGUIAppTraits::GetEventLoopSourcesManager() |
189 | { | |
190 | static wxGUIEventLoopSourcesManager s_eventLoopSourcesManager; | |
5cd99866 | 191 | |
71e9885b | 192 | return &s_eventLoopSourcesManager; |
6b8ef0b3 VZ |
193 | } |
194 | ||
5cd99866 | 195 | wxGTKEventLoopSource::~wxGTKEventLoopSource() |
6b8ef0b3 | 196 | { |
6b8ef0b3 | 197 | wxLogTrace(wxTRACE_EVT_SOURCE, |
5cd99866 | 198 | "Removing event loop source with GTK id=%u", m_sourceId); |
6b8ef0b3 | 199 | |
5cd99866 | 200 | g_source_remove(m_sourceId); |
6b8ef0b3 VZ |
201 | } |
202 | ||
5cd99866 VZ |
203 | #endif // wxUSE_EVENTLOOP_SOURCE |
204 | ||
8000ae7f VZ |
205 | // ---------------------------------------------------------------------------- |
206 | // wxEventLoop message processing dispatching | |
207 | // ---------------------------------------------------------------------------- | |
208 | ||
b46b1d59 | 209 | bool wxGUIEventLoop::Pending() const |
8000ae7f | 210 | { |
564c7fc4 VZ |
211 | if ( wxTheApp ) |
212 | { | |
213 | // this avoids false positives from our idle source | |
214 | return wxTheApp->EventsPending(); | |
215 | } | |
216 | ||
217 | return gtk_events_pending() != 0; | |
8000ae7f VZ |
218 | } |
219 | ||
b46b1d59 | 220 | bool wxGUIEventLoop::Dispatch() |
8000ae7f | 221 | { |
9a83f860 | 222 | wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") ); |
8000ae7f | 223 | |
4b8af4ac VZ |
224 | // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called |
225 | return !gtk_main_iteration(); | |
8000ae7f | 226 | } |
564c7fc4 VZ |
227 | |
228 | extern "C" { | |
229 | static gboolean wx_event_loop_timeout(void* data) | |
230 | { | |
231 | bool* expired = static_cast<bool*>(data); | |
232 | *expired = true; | |
233 | ||
234 | // return FALSE to remove this timeout | |
235 | return FALSE; | |
236 | } | |
237 | } | |
238 | ||
239 | int wxGUIEventLoop::DispatchTimeout(unsigned long timeout) | |
240 | { | |
241 | bool expired = false; | |
242 | const unsigned id = g_timeout_add(timeout, wx_event_loop_timeout, &expired); | |
243 | bool quit = gtk_main_iteration() != 0; | |
244 | ||
245 | if ( expired ) | |
246 | return -1; | |
247 | ||
248 | g_source_remove(id); | |
249 | ||
250 | return !quit; | |
251 | } | |
252 | ||
dde19c21 FM |
253 | //----------------------------------------------------------------------------- |
254 | // YieldFor | |
255 | //----------------------------------------------------------------------------- | |
256 | ||
f629f37a PC |
257 | extern "C" { |
258 | static void wxgtk_main_do_event(GdkEvent* event, void* data) | |
387e72ba FM |
259 | { |
260 | // categorize the GDK event according to wxEventCategory. | |
261 | // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType | |
262 | // for more info. | |
03647350 | 263 | |
3bb5029d FM |
264 | // NOTE: GDK_* constants which were not present in the GDK2.0 can be tested for |
265 | // only at compile-time; when running the program (compiled with a recent GDK) | |
03647350 VZ |
266 | // on a system with an older GDK lib we can be sure there won't be problems |
267 | // because event->type will never assume those values corresponding to | |
3bb5029d FM |
268 | // new event types (since new event types are always added in GDK with non |
269 | // conflicting values for ABI compatibility). | |
387e72ba | 270 | |
5e4bbd0f VZ |
271 | // Some events (currently only a single one) may be used for more than one |
272 | // category, so we need 2 variables. The second one will remain "unknown" | |
273 | // in most cases. | |
274 | wxEventCategory cat = wxEVT_CATEGORY_UNKNOWN, | |
275 | cat2 = wxEVT_CATEGORY_UNKNOWN; | |
387e72ba FM |
276 | switch (event->type) |
277 | { | |
278 | case GDK_SELECTION_REQUEST: | |
279 | case GDK_SELECTION_NOTIFY: | |
280 | case GDK_SELECTION_CLEAR: | |
281 | case GDK_OWNER_CHANGE: | |
282 | cat = wxEVT_CATEGORY_CLIPBOARD; | |
283 | break; | |
284 | ||
387e72ba FM |
285 | case GDK_KEY_PRESS: |
286 | case GDK_KEY_RELEASE: | |
287 | case GDK_BUTTON_PRESS: | |
288 | case GDK_2BUTTON_PRESS: | |
289 | case GDK_3BUTTON_PRESS: | |
290 | case GDK_BUTTON_RELEASE: | |
291 | case GDK_SCROLL: // generated from mouse buttons | |
292 | case GDK_CLIENT_EVENT: | |
293 | cat = wxEVT_CATEGORY_USER_INPUT; | |
294 | break; | |
295 | ||
5e4bbd0f VZ |
296 | case GDK_PROPERTY_NOTIFY: |
297 | // This one is special: it can be used for UI purposes but also for | |
298 | // clipboard operations, so allow it in both cases (we probably could | |
299 | // examine the event itself to distinguish between the two cases but | |
300 | // this would be unnecessarily complicated). | |
301 | cat2 = wxEVT_CATEGORY_CLIPBOARD; | |
302 | // Fall through. | |
303 | ||
387e72ba FM |
304 | case GDK_PROXIMITY_IN: |
305 | case GDK_PROXIMITY_OUT: | |
306 | ||
307 | case GDK_MOTION_NOTIFY: | |
308 | case GDK_ENTER_NOTIFY: | |
309 | case GDK_LEAVE_NOTIFY: | |
310 | case GDK_VISIBILITY_NOTIFY: | |
387e72ba FM |
311 | |
312 | case GDK_FOCUS_CHANGE: | |
313 | case GDK_CONFIGURE: | |
314 | case GDK_WINDOW_STATE: | |
315 | case GDK_SETTING: | |
316 | case GDK_DELETE: | |
317 | case GDK_DESTROY: | |
318 | ||
319 | case GDK_EXPOSE: | |
9dc44eff | 320 | #ifndef __WXGTK3__ |
387e72ba | 321 | case GDK_NO_EXPOSE: |
9dc44eff | 322 | #endif |
387e72ba FM |
323 | case GDK_MAP: |
324 | case GDK_UNMAP: | |
325 | ||
326 | case GDK_DRAG_ENTER: | |
327 | case GDK_DRAG_LEAVE: | |
328 | case GDK_DRAG_MOTION: | |
329 | case GDK_DRAG_STATUS: | |
330 | case GDK_DROP_START: | |
331 | case GDK_DROP_FINISHED: | |
3bb5029d | 332 | #if GTK_CHECK_VERSION(2,8,0) |
387e72ba | 333 | case GDK_GRAB_BROKEN: |
3bb5029d FM |
334 | #endif |
335 | #if GTK_CHECK_VERSION(2,14,0) | |
336 | case GDK_DAMAGE: | |
337 | #endif | |
387e72ba FM |
338 | cat = wxEVT_CATEGORY_UI; |
339 | break; | |
340 | ||
341 | default: | |
342 | cat = wxEVT_CATEGORY_UNKNOWN; | |
343 | break; | |
344 | } | |
345 | ||
f629f37a PC |
346 | wxGUIEventLoop* evtloop = static_cast<wxGUIEventLoop*>(data); |
347 | ||
387e72ba | 348 | // is this event allowed now? |
5e4bbd0f VZ |
349 | if (evtloop->IsEventAllowedInsideYield(cat) || |
350 | (cat2 != wxEVT_CATEGORY_UNKNOWN && | |
351 | evtloop->IsEventAllowedInsideYield(cat2))) | |
352 | { | |
353 | // process it now | |
354 | gtk_main_do_event(event); | |
355 | } | |
387e72ba | 356 | else if (event->type != GDK_NOTHING) |
5e4bbd0f VZ |
357 | { |
358 | // process it later (but make a copy; the caller will free the event | |
359 | // pointer) | |
387e72ba | 360 | evtloop->StoreGdkEventForLaterProcessing(gdk_event_copy(event)); |
5e4bbd0f | 361 | } |
387e72ba | 362 | } |
f629f37a | 363 | } |
387e72ba | 364 | |
dde19c21 FM |
365 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) |
366 | { | |
367 | #if wxUSE_THREADS | |
368 | if ( !wxThread::IsMain() ) | |
369 | { | |
370 | // can't call gtk_main_iteration() from other threads like this | |
371 | return true; | |
372 | } | |
373 | #endif // wxUSE_THREADS | |
374 | ||
375 | m_isInsideYield = true; | |
376 | m_eventsToProcessInsideYield = eventsToProcess; | |
377 | ||
378 | #if wxUSE_LOG | |
379 | // disable log flushing from here because a call to wxYield() shouldn't | |
380 | // normally result in message boxes popping up &c | |
381 | wxLog::Suspend(); | |
382 | #endif | |
383 | ||
387e72ba FM |
384 | // temporarily replace the global GDK event handler with our function, which |
385 | // categorizes the events and using m_eventsToProcessInsideYield decides | |
386 | // if an event should be processed immediately or not | |
387 | // NOTE: this approach is better than using gdk_display_get_event() because | |
388 | // gtk_main_iteration() does more than just calling gdk_display_get_event() | |
389 | // and then call gtk_main_do_event()! | |
390 | // In particular in this way we also process input from sources like | |
391 | // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild). | |
f629f37a | 392 | gdk_event_handler_set(wxgtk_main_do_event, this, NULL); |
387e72ba FM |
393 | while (Pending()) // avoid false positives from our idle source |
394 | gtk_main_iteration(); | |
395 | gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL); | |
564c7fc4 | 396 | |
709617ee VZ |
397 | // Process all pending events too, this is consistent with wxMSW behaviour |
398 | // and the behaviour of wxGTK itself in the previous versions. | |
399 | if ( wxTheApp ) | |
400 | wxTheApp->ProcessPendingEvents(); | |
401 | ||
dde19c21 FM |
402 | if (eventsToProcess != wxEVT_CATEGORY_CLIPBOARD) |
403 | { | |
404 | // It's necessary to call ProcessIdle() to update the frames sizes which | |
405 | // might have been changed (it also will update other things set from | |
406 | // OnUpdateUI() which is a nice (and desired) side effect). But we | |
407 | // call ProcessIdle() only once since this is not meant for longish | |
408 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
409 | // return value of Processidle(). | |
709617ee | 410 | ProcessIdle(); |
dde19c21 FM |
411 | } |
412 | //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and | |
413 | // the user app contains an UI update handler which calls wxClipboard::IsSupported, | |
414 | // then we fall into a never-ending loop... | |
415 | ||
416 | // put all unprocessed GDK events back in the queue | |
387e72ba | 417 | GdkDisplay* disp = gtk_widget_get_display(wxGetRootWindow()); |
dde19c21 FM |
418 | for (size_t i=0; i<m_arrGdkEvents.GetCount(); i++) |
419 | { | |
420 | GdkEvent* ev = (GdkEvent*)m_arrGdkEvents[i]; | |
421 | ||
422 | // NOTE: gdk_display_put_event makes a copy of the event passed to it | |
423 | gdk_display_put_event(disp, ev); | |
424 | gdk_event_free(ev); | |
425 | } | |
426 | ||
427 | m_arrGdkEvents.Clear(); | |
428 | ||
429 | #if wxUSE_LOG | |
430 | // let the logs be flashed again | |
431 | wxLog::Resume(); | |
432 | #endif | |
433 | ||
434 | m_isInsideYield = false; | |
435 | ||
436 | return true; | |
437 | } |