summaryrefslogtreecommitdiffstats
path: root/osdep/mac/events.m
blob: 127a6743398dc2f2323e5c48e4b222677a60a383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Cocoa Application Event Handling
 *
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#import <Cocoa/Cocoa.h>

#include "mpv_talloc.h"
#include "input/event.h"
#include "input/input.h"
#include "player/client.h"

#import "osdep/mac/events_objc.h"
#import "osdep/mac/application_objc.h"

#include "config.h"

#if HAVE_SWIFT
#include "osdep/mac/swift.h"
#endif

@interface EventsResponder ()
{
    struct mpv_handle *_ctx;
    BOOL _is_application;
}

- (BOOL)setMpvHandle:(struct mpv_handle *)ctx;
- (void)initCocoaCb;
- (void)readEvents;
- (void)startMediaKeys;
- (void)stopMediaKeys;
@end

void cocoa_init_media_keys(void)
{
    [[EventsResponder sharedInstance] startMediaKeys];
}

void cocoa_uninit_media_keys(void)
{
    [[EventsResponder sharedInstance] stopMediaKeys];
}

void cocoa_set_input_context(struct input_ctx *input_context)
{
    [[EventsResponder sharedInstance].inputHelper signalWithInput:input_context];
}

static void wakeup(void *context)
{
    [[EventsResponder sharedInstance] readEvents];
}

void cocoa_set_mpv_handle(struct mpv_handle *ctx)
{
    if ([[EventsResponder sharedInstance] setMpvHandle:ctx]) {
        mpv_observe_property(ctx, 0, "duration", MPV_FORMAT_DOUBLE);
        mpv_observe_property(ctx, 0, "time-pos", MPV_FORMAT_DOUBLE);
        mpv_observe_property(ctx, 0, "speed", MPV_FORMAT_DOUBLE);
        mpv_observe_property(ctx, 0, "pause", MPV_FORMAT_FLAG);
        mpv_observe_property(ctx, 0, "media-title", MPV_FORMAT_STRING);
        mpv_observe_property(ctx, 0, "chapter-metadata/title", MPV_FORMAT_STRING);
        mpv_observe_property(ctx, 0, "metadata/by-key/album", MPV_FORMAT_STRING);
        mpv_observe_property(ctx, 0, "metadata/by-key/artist", MPV_FORMAT_STRING);
        mpv_set_wakeup_callback(ctx, wakeup, NULL);
    }
}

void cocoa_init_cocoa_cb(void)
{
    [[EventsResponder sharedInstance] initCocoaCb];
}

@implementation EventsResponder

@synthesize remoteCommandCenter = _remoteCommandCenter;
@synthesize inputHelper = _inputHelper;

+ (EventsResponder *)sharedInstance
{
    static EventsResponder *responder = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        responder = [EventsResponder new];
        responder.inputHelper = [[InputHelper alloc] init: nil :nil];
    });
    return responder;
}

- (void)setIsApplication:(BOOL)isApplication
{
    _is_application = isApplication;
}

- (BOOL)setMpvHandle:(struct mpv_handle *)ctx
{
    if (_is_application) {
        _ctx = ctx;
        return YES;
    }

    mpv_destroy(ctx);
    return NO;
}

- (void)initCocoaCb
{
    if (_is_application) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [NSApp initCocoaCb:_ctx];
        });
    }
}

- (void)readEvents
{
    dispatch_async(dispatch_get_main_queue(), ^{
        while (_ctx) {
            mpv_event *event = mpv_wait_event(_ctx, 0);
            if (event->event_id == MPV_EVENT_NONE)
                break;
            [self processEvent:event];
        }
    });
}

-(void)processEvent:(struct mpv_event *)event
{
    if(_is_application) {
        [NSApp processEvent:event];
    }

    if (_remoteCommandCenter) {
        [_remoteCommandCenter processEvent:event];
    }

    switch (event->event_id) {
    case MPV_EVENT_SHUTDOWN: {
#if HAVE_MACOS_COCOA_CB
        if ([(Application *)NSApp cocoaCB].isShuttingDown) {
            _ctx = nil;
            return;
        }
#endif
        mpv_destroy(_ctx);
        _ctx = nil;
        break;
    }
    default:
        break;
    }
}

- (void)startMediaKeys
{
#if HAVE_MACOS_MEDIA_PLAYER
    if (_remoteCommandCenter == nil) {
        _remoteCommandCenter = [[RemoteCommandCenter alloc] init];
    }
#endif

    [_remoteCommandCenter start];
}

- (void)stopMediaKeys
{
    [_remoteCommandCenter stop];
}

@end