OSX, input: implement wakeup in response to Cocoa events
[mplayer.git] / osdep / cocoa_events.m
blobba09c13cad2883f37fdad2b6e4eb1f0a1d4bfc09
1 /*
2  * Cocoa Event Handling
3  *
4  * This file is part of mplayer2.
5  *
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.
10  *
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.
15  *
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/>.
18  */
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.
27  *
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.
31  *
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.
35  */
37 #include "cocoa_events.h"
38 #include "libvo/cocoa_common.h"
39 #include "talloc.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
53 struct priv {
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;
64     NSEvent *event;
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];
75     }
76     p->is_runloop_polling = NO;
79 static void cocoa_wake_runloop()
81     if (p->is_runloop_polling) {
82         NSAutoreleasePool *pool = [NSAutoreleasePool new];
83         NSEvent *event;
85         /* Post an event so we'll wake the run loop that is async polling */
86         event = [NSEvent otherEventWithType: NSApplicationDefined
87                                    location: NSZeroPoint
88                               modifierFlags: 0
89                                   timestamp: 0
90                                windowNumber: 0
91                                     context: nil
92                                     subtype: MP_EVENT_SUBTYPE_WAKE_EVENTLOOP
93                                       data1: 0
94                                       data2: 0];
96         [NSApp postEvent:event atStart:NO];
97         [pool release];
98     }
101 void cocoa_events_init(struct input_ctx *ictx,
102     void (*read_all_fd_events)(struct input_ctx *ictx, int time))
104     NSApplicationLoad();
105     p = talloc_ptrtype(NULL, p);
106     *p = (struct priv){
107         .is_runloop_polling = NO,
108         .read_all_fd_events = read_all_fd_events,
109         .select_queue = dispatch_queue_create("org.mplayer2.select_queue",
110                                               NULL),
111     };
114 void cocoa_events_uninit(void)
116     talloc_free(p);
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();
127         });
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, ^{});
134     } else {
135         p->read_all_fd_events(ictx, time);
136     }