4 * This file is part of mplayer2.
6 * mplayer2 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * mplayer2 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with mplayer2. If not, see <http://www.gnu.org/licenses/>.
21 * Implementation details:
22 * This file deals with custom event polling on MacOSX. When mplayer2 is paused
23 * it will asynchronously poll for events using select. This works correctly on
24 * Linux with X11 since the events are notified through the file descriptors
25 * where mplayer2 is listening on. On the other hand, the OSX window server
26 * notifies the processes for events using mach ports.
28 * The code below uses functionality from Cocoa that abstracts the async polling
29 * of events from the window server. When a Cocoa event comes in, the polling is
30 * interrupted and the event is dealt with in the next vo_check_events.
32 * To keep the select fd polling code working, that functionality is executed
33 * from another thread. Whoever finishes polling before the given time, be it
34 * Cocoa or the original select code, notifies the other for an immediate wake.
37 #include "cocoa_events.h"
38 #include "libvo/cocoa_common.h"
41 #import <Cocoa/Cocoa.h>
42 #include <dispatch/dispatch.h>
44 // Bogus event subtype to wake the Cocoa code from polling state
45 #define MP_EVENT_SUBTYPE_WAKE_EVENTLOOP 100
47 // This is the threshold in milliseconds below which the Cocoa polling is not
48 // executed. There is some overhead caused by the synchronization between
49 // threads. Even if in practice it isn't noticeable, we try to avoid the useless
50 // waste of resources.
51 #define MP_ASYNC_THRESHOLD 50
54 dispatch_queue_t select_queue;
55 bool is_runloop_polling;
56 void (*read_all_fd_events)(struct input_ctx *ictx, int time);
59 static struct priv *p;
61 static void cocoa_wait_events(int mssleeptime)
63 NSTimeInterval sleeptime = mssleeptime / 1000.0;
65 p->is_runloop_polling = YES;
66 event = [NSApp nextEventMatchingMask:NSAnyEventMask
67 untilDate:[NSDate dateWithTimeIntervalSinceNow:sleeptime]
68 inMode:NSEventTrackingRunLoopMode dequeue:NO];
70 // dequeue the next event if it is a fake to wake the cocoa polling
71 if (event && [event type] == NSApplicationDefined &&
72 [event subtype] == MP_EVENT_SUBTYPE_WAKE_EVENTLOOP) {
73 [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil
74 inMode:NSEventTrackingRunLoopMode dequeue:YES];
76 p->is_runloop_polling = NO;
79 static void cocoa_wake_runloop()
81 if (p->is_runloop_polling) {
82 NSAutoreleasePool *pool = [NSAutoreleasePool new];
85 /* Post an event so we'll wake the run loop that is async polling */
86 event = [NSEvent otherEventWithType: NSApplicationDefined
92 subtype: MP_EVENT_SUBTYPE_WAKE_EVENTLOOP
96 [NSApp postEvent:event atStart:NO];
101 void cocoa_events_init(struct input_ctx *ictx,
102 void (*read_all_fd_events)(struct input_ctx *ictx, int time))
105 p = talloc_ptrtype(NULL, p);
107 .is_runloop_polling = NO,
108 .read_all_fd_events = read_all_fd_events,
109 .select_queue = dispatch_queue_create("org.mplayer2.select_queue",
114 void cocoa_events_uninit(void)
119 void cocoa_events_read_all_events(struct input_ctx *ictx, int time)
121 // don't bother delegating the select to the async queue if the blocking
122 // time is really low or if we are not running a GUI
123 if (time > MP_ASYNC_THRESHOLD && vo_cocoa_gui_running()) {
124 dispatch_async(p->select_queue, ^{
125 p->read_all_fd_events(ictx, time);
126 cocoa_wake_runloop();
129 cocoa_wait_events(time);
130 mp_input_wakeup(ictx);
132 // wait for the async queue to get empty.
133 dispatch_sync(p->select_queue, ^{});
135 p->read_all_fd_events(ictx, time);