]> git.sesse.net Git - vlc/blob - plugins/macosx/vout_vlc_wrapper.m
* ALL: the first libvlc commit.
[vlc] / plugins / macosx / vout_vlc_wrapper.m
1 /*****************************************************************************
2  * vout_vlc_wrapper.c: MacOS X plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: vout_vlc_wrapper.m,v 1.3 2002/06/01 12:32:00 sam Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net> 
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <stdlib.h>                                                /* free() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33 #include <vlc/vout.h>
34
35 #include "macosx.h"
36 #include "vout_vlc_wrapper.h"
37
38 /*****************************************************************************
39  * Vout_VLCWrapper implementation 
40  *****************************************************************************/
41 @implementation Vout_VLCWrapper
42
43 static Vout_VLCWrapper *o_vout = nil;
44
45 + (Vout_VLCWrapper *)instance
46 {
47     if( o_vout == nil )
48     {
49         o_vout = [[[Vout_VLCWrapper alloc] init] autorelease];
50
51         [[Vout_VLCWrapper sendPort] setDelegate: o_vout];
52         [[NSRunLoop currentRunLoop]
53             addPort: [Vout_VLCWrapper sendPort]
54             forMode: NSDefaultRunLoopMode];
55     }
56
57     return( o_vout );
58 }
59
60 - (void)dealloc
61 {
62     o_vout = nil;
63     [super dealloc];
64 }
65
66 + (NSPort *)sendPort
67 {
68     return( p_main->p_intf->p_sys->o_port );
69 }
70
71 - (void)mouseEvent:(unsigned int)ui_status forVout:(void *)_p_vout
72 {
73     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
74
75     if( ui_status & MOUSE_MOVED ) 
76         p_vout->p_sys->b_mouse_moved = 1;
77     if( ui_status & MOUSE_NOT_MOVED ) 
78         p_vout->p_sys->b_mouse_moved = 0;
79     if( ui_status & MOUSE_LAST_MOVED ) 
80         p_vout->p_sys->i_time_mouse_last_moved = mdate();
81     if( ui_status & MOUSE_NOT_LAST_MOVED )
82         p_vout->p_sys->i_time_mouse_last_moved = 0;
83     if( ui_status & MOUSE_DOWN )
84     {
85         if( p_vout->p_sys->b_mouse_pointer_visible )
86         {
87             CGDisplayHideCursor( kCGDirectMainDisplay );
88         }
89         else
90         {
91             CGDisplayShowCursor( kCGDirectMainDisplay );
92         }
93         p_vout->p_sys->b_mouse_pointer_visible = !p_vout->p_sys->b_mouse_pointer_visible;
94     }
95 }
96
97 - (BOOL)keyDown:(NSEvent *)o_event forVout:(void *)_p_vout
98 {
99     unichar key = 0;
100
101     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
102
103     if( [[o_event characters] length] )
104     {
105         key = [[o_event characters] characterAtIndex: 0];
106     }
107
108     switch( key )
109     {
110         case 'f': case 'F':
111             p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
112             break;
113
114         case 'q': case 'Q':
115             p_vout->p_vlc->b_die = 1;
116             break;
117
118         default:
119             return( NO );
120             break;
121     }
122
123     return( YES );
124 }
125
126 - (void)voutDidResize:(void *)_p_vout
127 {
128     vout_thread_t * p_vout = (vout_thread_t *)_p_vout;
129
130     p_vout->i_changes |= VOUT_SIZE_CHANGE;
131 }
132
133 @end
134
135 @implementation Vout_VLCWrapper (Internal)
136
137 - (void)handlePortMessage:(NSPortMessage *)o_msg
138 {
139     NSData *o_req;
140     struct vout_req_s *p_req;
141
142     o_req = [[o_msg components] lastObject];
143     p_req = *((struct vout_req_s **)[o_req bytes]);
144
145     [p_req->o_lock lock];
146
147     if( p_req->i_type == VOUT_REQ_CREATE_WINDOW )
148     {
149         VLCView *o_view;
150
151         p_req->p_vout->p_sys->o_window = [VLCWindow alloc];
152         [p_req->p_vout->p_sys->o_window 
153             setWrapper: self forVout: (void *)p_req->p_vout];
154         [p_req->p_vout->p_sys->o_window setReleasedWhenClosed: YES];
155
156         if( p_req->p_vout->b_fullscreen )
157         {
158             [p_req->p_vout->p_sys->o_window 
159                 initWithContentRect: [[NSScreen mainScreen] frame] 
160                 styleMask: NSBorderlessWindowMask 
161                 backing: NSBackingStoreBuffered
162                 defer: NO screen: [NSScreen mainScreen]];
163
164             [p_req->p_vout->p_sys->o_window 
165                 setLevel: CGShieldingWindowLevel()];
166         }
167         else
168         {
169             unsigned int i_stylemask = NSTitledWindowMask |
170                                        NSMiniaturizableWindowMask |
171                                        NSResizableWindowMask;
172
173             [p_req->p_vout->p_sys->o_window 
174                 initWithContentRect: p_req->p_vout->p_sys->s_rect 
175                 styleMask: i_stylemask
176                 backing: NSBackingStoreBuffered
177                 defer: NO screen: [NSScreen mainScreen]];
178
179             if( !p_req->p_vout->p_sys->b_pos_saved )
180             {
181                 [p_req->p_vout->p_sys->o_window center];
182             }
183         }
184
185         o_view = [[VLCView alloc] 
186             initWithWrapper: self forVout: (void *)p_req->p_vout];
187         [p_req->p_vout->p_sys->o_window setContentView: o_view];
188         [o_view autorelease];
189
190         [o_view lockFocus];
191         p_req->p_vout->p_sys->p_qdport = [o_view qdPort];
192         [o_view unlockFocus];
193
194         [p_req->p_vout->p_sys->o_window setTitle: [NSString 
195             stringWithCString: VOUT_TITLE]];
196         [p_req->p_vout->p_sys->o_window setAcceptsMouseMovedEvents: YES];
197         [p_req->p_vout->p_sys->o_window makeKeyAndOrderFront: nil];
198
199         p_req->i_result = 1;
200     }
201     else if( p_req->i_type == VOUT_REQ_DESTROY_WINDOW )
202     {
203         if( !p_req->p_vout->b_fullscreen )
204         {
205             NSRect s_rect;
206
207             s_rect = [[p_req->p_vout->p_sys->o_window contentView] frame];
208             p_req->p_vout->p_sys->s_rect.size = s_rect.size;
209
210             s_rect = [p_req->p_vout->p_sys->o_window frame];
211             p_req->p_vout->p_sys->s_rect.origin = s_rect.origin;
212
213             p_req->p_vout->p_sys->b_pos_saved = 1;
214         }
215
216         p_req->p_vout->p_sys->p_qdport = nil;
217         [p_req->p_vout->p_sys->o_window close];
218         p_req->p_vout->p_sys->o_window = nil;
219
220         p_req->i_result = 1;
221     }
222
223     [p_req->o_lock unlockWithCondition: 1];
224 }
225
226 @end