]> git.sesse.net Git - vlc/blob - modules/gui/minimal_macosx/intf.m
55251e422e5af4746ccceedbea804a16659c36fb
[vlc] / modules / gui / minimal_macosx / intf.m
1 /*****************************************************************************
2  * intf.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <hartman at videolan.org>
10  *          Felix Kühne <fkuehne at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <sys/param.h>                                    /* for MAXPATHLEN */
32 #include <string.h>
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38
39 #include <vlc_keys.h>
40
41 #include <vlc_input.h>
42 #import <vlc_interface.h>
43
44 #include <vlc_vout_window.h>
45
46 #import <intf.h>
47 #import "VLCMinimalVoutWindow.h"
48
49 /*****************************************************************************
50  * Local prototypes.
51  *****************************************************************************/
52 static void Run ( intf_thread_t *p_intf );
53
54 /*****************************************************************************
55  * OpenIntf: initialize interface
56  *****************************************************************************/
57 int OpenIntf ( vlc_object_t *p_this )
58 {
59     intf_thread_t *p_intf = (intf_thread_t*) p_this;
60         
61     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
62     if( p_intf->p_sys == NULL )
63     {
64         return VLC_ENOMEM;
65     }
66
67     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
68
69     Run(p_intf);
70
71     return VLC_SUCCESS;
72 }
73
74 /*****************************************************************************
75  * CloseIntf: destroy interface
76  *****************************************************************************/
77 void CloseIntf ( vlc_object_t *p_this )
78 {
79     intf_thread_t *p_intf = (intf_thread_t*) p_this;
80
81     free( p_intf->p_sys );
82 }
83
84 /* Dock Connection */
85 typedef struct CPSProcessSerNum
86 {
87         UInt32                lo;
88         UInt32                hi;
89 } CPSProcessSerNum;
90
91 extern OSErr    CPSGetCurrentProcess( CPSProcessSerNum *psn);
92 extern OSErr    CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
93 extern OSErr    CPSSetFrontProcess( CPSProcessSerNum *psn);
94
95 /*****************************************************************************
96  * KillerThread: Thread that kill the application
97  *****************************************************************************/
98 static void * KillerThread( void *user_data )
99 {
100     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
101
102     intf_thread_t *p_intf = user_data;
103
104     vlc_mutex_init( &p_intf->p_sys->lock );
105     vlc_cond_init( &p_intf->p_sys->wait );
106
107     vlc_mutex_lock ( &p_intf->p_sys->lock );
108     while( vlc_object_alive( p_intf ) )
109         vlc_cond_wait( &p_intf->p_sys->wait, &p_intf->p_sys->lock );
110     vlc_mutex_unlock( &p_intf->p_sys->lock );
111
112     vlc_mutex_destroy( &p_intf->p_sys->lock );
113     vlc_cond_destroy( &p_intf->p_sys->wait );
114
115     /* We are dead, terminate */
116     [NSApp terminate: nil];
117     [o_pool release];
118     return NULL;
119 }
120
121 /*****************************************************************************
122  * Run: main loop
123  *****************************************************************************/
124 static void Run( intf_thread_t *p_intf )
125 {
126     sigset_t set;
127
128     /* Make sure the "force quit" menu item does quit instantly.
129      * VLC overrides SIGTERM which is sent by the "force quit"
130      * menu item to make sure deamon mode quits gracefully, so
131      * we un-override SIGTERM here. */
132     sigemptyset( &set );
133     sigaddset( &set, SIGTERM );
134     pthread_sigmask( SIG_UNBLOCK, &set, NULL );
135
136     /* Setup a thread that will monitor the module killing */
137     pthread_t killer_thread;
138     pthread_create( &killer_thread, NULL, KillerThread, p_intf );
139
140     CPSProcessSerNum PSN;
141     NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
142     [NSApplication sharedApplication];
143     if (!CPSGetCurrentProcess(&PSN))
144         if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
145             if (!CPSSetFrontProcess(&PSN))
146                 [NSApplication sharedApplication];
147     [NSApp run];
148
149     pthread_join( killer_thread, NULL );
150
151     [pool release];
152 }
153
154 /*****************************************************************************
155  * Vout window management
156  *****************************************************************************/
157 static int WindowControl(vout_window_t *, int i_query, va_list);
158
159 int WindowOpen(vout_window_t *p_wnd, const vout_window_cfg_t *cfg)
160 {
161     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
162
163     NSRect proposedVideoViewPosition = NSMakeRect(cfg->x, cfg->y, cfg->width, cfg->height);
164
165     VLCMinimalVoutWindow *o_window = [[VLCMinimalVoutWindow alloc] initWithContentRect:proposedVideoViewPosition];
166     [o_window makeKeyAndOrderFront:nil];
167
168     if (!o_window) {
169         msg_Err(p_wnd, "window creation failed");
170         [o_pool release];
171         return VLC_EGENERIC;
172     }
173
174     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);
175     p_wnd->handle.nsobject = [o_window contentView];
176
177     // TODO: find a cleaner way for "start in fullscreen"
178     if (var_GetBool(pl_Get(p_wnd), "fullscreen"))
179         [o_window performSelectorOnMainThread:@selector(enterFullscreen) withObject:nil waitUntilDone:NO];
180
181     p_wnd->control = WindowControl;
182
183     [o_pool release];
184     return VLC_SUCCESS;
185 }
186
187 static int WindowControl(vout_window_t *p_wnd, int i_query, va_list args)
188 {
189     NSWindow * o_window = [(id)p_wnd->handle.nsobject window];
190     if (!o_window) {
191         msg_Err(p_wnd, "failed to recover cocoa window");
192         return VLC_EGENERIC;
193     }
194
195     switch(i_query) {
196         case VOUT_WINDOW_SET_STATE:
197         {
198             unsigned i_state = va_arg(args, unsigned);
199
200             [o_window setLevel: i_state];
201
202             return VLC_SUCCESS;
203         }
204         case VOUT_WINDOW_SET_SIZE:
205         {
206             NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
207
208             NSRect theFrame = [o_window frame];
209             unsigned int i_width  = va_arg(args, unsigned int);
210             unsigned int i_height = va_arg(args, unsigned int);
211             theFrame.size.width = i_width;
212             theFrame.size.height = i_height;
213             [o_window setFrame: theFrame display: YES animate: YES];
214
215             [o_pool release];
216             return VLC_SUCCESS;
217         }
218         case VOUT_WINDOW_SET_FULLSCREEN:
219         {
220             NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
221             int i_full = va_arg(args, int);
222
223             if (i_full)
224                 [o_window performSelectorOnMainThread:@selector(enterFullscreen) withObject:nil waitUntilDone:NO];
225             else
226                 [o_window performSelectorOnMainThread:@selector(leaveFullscreen) withObject:nil waitUntilDone:NO];
227
228             [o_pool release];
229             return VLC_SUCCESS;
230         }
231         default:
232             msg_Warn(p_wnd, "unsupported control query");
233             return VLC_EGENERIC;
234     }
235 }
236
237 void WindowClose(vout_window_t *p_wnd)
238 {
239     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
240
241     NSWindow * o_window = [(id)p_wnd->handle.nsobject window];
242     if (o_window)
243         [o_window release];
244
245     [o_pool release];
246 }
247