intf.m 6.43 KB
Newer Older
1
/*****************************************************************************
2
 * intf.m: macOS minimal interface module
3
 *****************************************************************************
4
 * Copyright (C) 2002-2017 VLC authors and VideoLAN
5 6
 * $Id$
 *
7
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8
 *          Felix Paul Kühne <fkuehne at videolan dot org>
9
 *
10 11 12
 * This program 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
13
 * (at your option) any later version.
14
 *
15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28
#import <stdlib.h>
29
#import <string.h>
30 31
#import <unistd.h>

32
#ifdef HAVE_CONFIG_H
33
# import "config.h"
34 35
#endif

36 37 38 39
#import <vlc_common.h>
#import <vlc_playlist.h>
#import <vlc_interface.h>
#import <vlc_vout_window.h>
40

41
#import "VLCMinimalVoutWindow.h"
42 43 44 45

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
46
static void Run (intf_thread_t *p_intf);
47 48 49 50

/*****************************************************************************
 * OpenIntf: initialize interface
 *****************************************************************************/
51
int OpenIntf (vlc_object_t *p_this)
52 53
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;
54
    msg_Dbg(p_intf, "Using minimal macosx interface");
55

56
    p_intf->p_sys = NULL;
57

58
    Run(p_intf);
59

60
    return VLC_SUCCESS;
61 62 63 64 65
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
66
void CloseIntf (vlc_object_t *p_this)
67 68 69
{
    intf_thread_t *p_intf = (intf_thread_t*) p_this;

70
    free(p_intf->p_sys);
71 72
}

73 74 75 76 77 78 79
/* Dock Connection */
typedef struct CPSProcessSerNum
{
        UInt32                lo;
        UInt32                hi;
} CPSProcessSerNum;

80 81 82
extern OSErr    CPSGetCurrentProcess(CPSProcessSerNum *psn);
extern OSErr    CPSEnableForegroundOperation(CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
extern OSErr    CPSSetFrontProcess(CPSProcessSerNum *psn);
83

84

85 86 87
/*****************************************************************************
 * Run: main loop
 *****************************************************************************/
88
static void Run(intf_thread_t *p_intf)
89
{
90
    CPSProcessSerNum PSN;
91 92 93 94 95 96 97
    @autoreleasepool {
        [NSApplication sharedApplication];
        if (!CPSGetCurrentProcess(&PSN))
            if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
                if (!CPSSetFrontProcess(&PSN))
                    [NSApplication sharedApplication];
    }
98 99
}

100 101 102 103 104 105 106
/*****************************************************************************
 * Vout window management
 *****************************************************************************/
static int WindowControl(vout_window_t *, int i_query, va_list);

int WindowOpen(vout_window_t *p_wnd, const vout_window_cfg_t *cfg)
{
107 108 109 110
    if (cfg->type != VOUT_WINDOW_TYPE_INVALID
     && cfg->type != VOUT_WINDOW_TYPE_NSOBJECT)
        return VLC_EGENERIC;

111 112
    @autoreleasepool {
        NSRect proposedVideoViewPosition = NSMakeRect(cfg->x, cfg->y, cfg->width, cfg->height);
113

114 115
        VLCMinimalVoutWindow *o_window = [[VLCMinimalVoutWindow alloc] initWithContentRect:proposedVideoViewPosition];
        [o_window makeKeyAndOrderFront:nil];
116

117 118 119 120
        if (!o_window) {
            msg_Err(p_wnd, "window creation failed");
            return VLC_EGENERIC;
        }
121

122 123
        msg_Dbg(p_wnd, "returning video window with proposed position x=%i, y=%i, width=%i, height=%i", cfg->x, cfg->y, cfg->width, cfg->height);
        p_wnd->handle.nsobject = (void *)CFBridgingRetain([o_window contentView]);
124

125 126 127
        p_wnd->type = VOUT_WINDOW_TYPE_NSOBJECT;
        p_wnd->control = WindowControl;
    }
128

129
    vout_window_SetFullScreen(p_wnd, cfg->is_fullscreen);
130 131 132 133 134
    return VLC_SUCCESS;
}

static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
{
135
    NSWindow* o_window = [(__bridge id)p_wnd->handle.nsobject window];
136 137 138 139 140
    if (!o_window) {
        msg_Err(p_wnd, "failed to recover cocoa window");
        return VLC_EGENERIC;
    }

141
    switch (i_query) {
142 143 144
        case VOUT_WINDOW_SET_STATE:
        {
            unsigned i_state = va_arg(args, unsigned);
145

146
            [o_window setLevel:i_state];
147

148 149 150 151
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_SIZE:
        {
152 153
            unsigned int i_width  = va_arg(args, unsigned int);
            unsigned int i_height = va_arg(args, unsigned int);
154
            @autoreleasepool {
155 156 157 158
                dispatch_sync(dispatch_get_main_queue(), ^{
                    NSRect theFrame = [o_window frame];
                    theFrame.size.width = i_width;
                    theFrame.size.height = i_height;
159
                    [o_window setFrame:theFrame display:YES animate:YES];
160
                });
161
            }
162 163 164 165
            return VLC_SUCCESS;
        }
        case VOUT_WINDOW_SET_FULLSCREEN:
        {
166
            int i_full = va_arg(args, int);
167
            @autoreleasepool {
168 169 170 171 172 173
                dispatch_sync(dispatch_get_main_queue(), ^{
                    if (i_full)
                        [(VLCMinimalVoutWindow*)o_window enterFullscreen];
                    else
                        [(VLCMinimalVoutWindow*)o_window leaveFullscreen];
                });
174
            }
175 176 177 178 179 180 181 182 183 184
            return VLC_SUCCESS;
        }
        default:
            msg_Warn(p_wnd, "unsupported control query");
            return VLC_EGENERIC;
    }
}

void WindowClose(vout_window_t *p_wnd)
{
185 186 187 188 189
    @autoreleasepool {
        NSWindow * o_window = [(__bridge id)p_wnd->handle.nsobject window];
        if (o_window)
            o_window = nil;
    }
190
}