1 /*****************************************************************************
2 * intf_macosx.c: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Colin Delacroix <colin@zoy.org>
7 * Eugenio Jarosiewicz <ej0@cise.ufl.edu>
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.
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.
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 *****************************************************************************/
24 #define MODULE_NAME macosx
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
41 #include "interface.h"
43 /* FIXME: get rid of this and do menus & command keys*/
44 #include "keystrokes.h"
48 #include "stream_control.h"
49 #include "input_ext-intf.h"
51 #include "intf_playlist.h"
52 #include "audio_output.h"
56 #include <Carbon/Carbon.h>
58 //how often to have callback to main loop. Target of 30fps then 30hz + maybe some more...
59 //it doesn't really scale if we move to 2x the hz... something else is slowing us down...
61 #define kMainLoopFrequency (kEventDurationSecond / 45) //45 for good measure
74 kAppleQuit = 7, //is this always the same?
78 kFileCloseDivisor = 3,
81 kControlsPlayORPause = 1,
85 kControlsChapterDiv = 5,
86 kControlsChapterNext = 6,
87 kControlsChapterPrevious = 7,
90 kControlsVolumeDiv = 10,
91 kControlsVolumeUp = 11,
92 kControlsVolumeDown = 12,
93 kControlsVolumeMute = 13,
94 kControlsEjectDiv = 14,
103 kAppleQuit = 7, //is this always the same?
110 kControlsPlayORPause = 1,
115 kControlsChapterNext,
116 kControlsChapterPrevious,
129 //virtual key codes ; raw subtract 0x40 from these values
130 //http://devworld.apple.com/techpubs/mac/Text/Text-577.html#HEADING577-0
136 //http://devworld.apple.com/techpubs/mac/Text/Text-571.html#MARKER-9-18
137 kPeriod = 47, //(decimal)
138 kSpace = 49, //(decimal)
139 kEscape = 53 //(decimal)
144 /*****************************************************************************
145 * intf_sys_t: description and status of the interface
146 *****************************************************************************/
147 typedef struct intf_sys_s
149 EventLoopTimerRef manageTimer;
153 /*****************************************************************************
155 *****************************************************************************/
156 static int intf_Probe ( probedata_t *p_data );
157 static int intf_Open ( intf_thread_t *p_intf );
158 static void intf_Close ( intf_thread_t *p_intf );
159 static void intf_Run ( intf_thread_t *p_intf );
163 void CarbonManageCallback ( EventLoopTimerRef inTimer, void *inUserData );
166 void EventLoop( intf_thread_t *p_intf );
167 void DoEvent( intf_thread_t *p_intf , EventRecord *event);
168 void DoMenuCommand( intf_thread_t *p_intf , long menuResult);
169 void DrawWindow(WindowRef window);
172 pascal OSErr QuitEventHandler(const AppleEvent *theEvent, AppleEvent *theReply, SInt32 refCon);
173 static pascal OSStatus MyKeyHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* userData );
174 static pascal OSStatus MyWindowEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData);
178 /*****************************************************************************
179 * Functions exported as capabilities. They are declared as static so that
180 * we don't pollute the namespace too much.
181 *****************************************************************************/
182 void _M( intf_getfunctions )( function_list_t * p_function_list )
184 p_function_list->pf_probe = intf_Probe;
185 p_function_list->functions.intf.pf_open = intf_Open;
186 p_function_list->functions.intf.pf_close = intf_Close;
187 p_function_list->functions.intf.pf_run = intf_Run;
190 /*****************************************************************************
191 * intf_Probe: probe the interface and return a score
192 *****************************************************************************
193 * This function checks the interface can be run and returns a score to the
194 * plugin manager so that it can select the best plugin.
195 *****************************************************************************/
196 static int intf_Probe( probedata_t *p_data )
198 if( TestMethod( INTF_METHOD_VAR, "macosx" ) )
203 /* Under MacOS X, this plugin always works */
207 /*****************************************************************************
208 * intf_Open: initialize interface
209 *****************************************************************************/
210 static int intf_Open( intf_thread_t *p_intf )
215 /* Allocate instance and initialize some members */
216 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
217 if( p_intf->p_sys == NULL )
224 SetQDGlobalsRandomSeed( TickCount() );
227 p_intf->p_intf_get_key = intf_GetKey;
229 intf_AssignKey( p_intf , 'Q', INTF_KEY_QUIT, 0);
230 intf_AssignKey( p_intf , 'q', INTF_KEY_QUIT, 0);
231 intf_AssignKey( p_intf , 27, INTF_KEY_QUIT, 0);
232 intf_AssignKey( p_intf , 3, INTF_KEY_QUIT, 0);
233 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
234 intf_AssignKey( p_intf , '1', INTF_KEY_SET_CHANNEL, 1);
235 intf_AssignKey( p_intf , '2', INTF_KEY_SET_CHANNEL, 2);
236 intf_AssignKey( p_intf , '3', INTF_KEY_SET_CHANNEL, 3);
237 intf_AssignKey( p_intf , '4', INTF_KEY_SET_CHANNEL, 4);
238 intf_AssignKey( p_intf , '5', INTF_KEY_SET_CHANNEL, 5);
239 intf_AssignKey( p_intf , '6', INTF_KEY_SET_CHANNEL, 6);
240 intf_AssignKey( p_intf , '7', INTF_KEY_SET_CHANNEL, 7);
241 intf_AssignKey( p_intf , '8', INTF_KEY_SET_CHANNEL, 8);
242 intf_AssignKey( p_intf , '9', INTF_KEY_SET_CHANNEL, 9);
243 intf_AssignKey( p_intf , '0', INTF_KEY_SET_CHANNEL, 0);
244 intf_AssignKey( p_intf , '+', INTF_KEY_INC_VOLUME, 0);
245 intf_AssignKey( p_intf , '-', INTF_KEY_DEC_VOLUME, 0);
246 intf_AssignKey( p_intf , 'm', INTF_KEY_TOGGLE_VOLUME, 0);
247 intf_AssignKey( p_intf , 'M', INTF_KEY_TOGGLE_VOLUME, 0);
248 intf_AssignKey( p_intf , 'g', INTF_KEY_DEC_GAMMA, 0);
249 intf_AssignKey( p_intf , 'G', INTF_KEY_INC_GAMMA, 0);
250 intf_AssignKey( p_intf , 'c', INTF_KEY_TOGGLE_GRAYSCALE, 0);
251 intf_AssignKey( p_intf , ' ', INTF_KEY_TOGGLE_INTERFACE, 0);
252 intf_AssignKey( p_intf , 'i', INTF_KEY_TOGGLE_INFO, 0);
253 intf_AssignKey( p_intf , 's', INTF_KEY_TOGGLE_SCALING, 0);
254 intf_AssignKey( p_intf , 'd', INTF_KEY_DUMP_STREAM, 0);
257 //EJ - neat menu but don't know if we want it.
258 // Install the Windows menu. Free of charge!
259 // CreateStandardWindowMenu( 0, &windMenu );
260 // InsertMenu( windMenu, 0 );
265 menu = NewMenu( kMenuApple, "\p\024" );
266 AppendMenu( menu, "\pAbout VLCÉ/A" );
267 InsertMenu( menu, 0 );
269 menu = NewMenu( kMenuFile, "\pFile" );
270 AppendMenu( menu, "\pNew Viewer Window/N" );
271 AppendMenu( menu, "\pOpenÉ/O" );
272 AppendMenu( menu, "\p(-" );
273 AppendMenu( menu, "\pClose/W" );
274 InsertMenu( menu, 0 );
276 //BIG HONKING MENU - in order Mac OS 9 dvd player
277 //can't get key codes right for menus... argh that's why they use resources!
279 menu = NewMenu( kMenuControls, "\pControls" );
281 AppendMenu( menu, "\pPlay/," );
282 // SetMenuItemCommandKey(menu, 0, false, kSpace);
283 // SetMenuItemModifiers( menu, 0, kMenuNoCommandModifier);
285 AppendMenu( menu, "\pStop/." );
287 AppendMenu( menu, "\pFast Forward/f" );
288 // SetMenuItemCommandKey(menu, 2, false, kRightArrow);
290 AppendMenu( menu, "\pRewind/r" );
291 // SetMenuItemCommandKey(menu, 3, false, kLeftArrow);
293 AppendMenu( menu, "\p(-" ); //4
295 AppendMenu( menu, "\pNext Chapter/c" );
296 // SetMenuItemCommandKey(menu, 5, false, kRightArrow);
297 // SetMenuItemModifiers( menu, 5, kMenuNoCommandModifier);
299 AppendMenu( menu, "\pPrevious Chapter/p" );
300 // SetMenuItemCommandKey(menu, 6, false, kLeftArrow);
301 // SetMenuItemModifiers( menu, 6, kMenuNoCommandModifier);
303 AppendMenu( menu, "\p(-" ); //7
305 AppendMenu( menu, "\pDVD Menu/v" );
306 // SetMenuItemCommandKey(menu, 8, false, kEscape);
307 // SetMenuItemModifiers( menu, 8, kMenuNoCommandModifier);
309 AppendMenu( menu, "\p(-" ); //9
311 AppendMenu( menu, "\pVolume Up/u" );
312 // SetMenuItemCommandKey(menu, 10, false, kUpArrow);
314 AppendMenu( menu, "\pVolume Down/d" );
315 // SetMenuItemCommandKey(menu, 11, false, kDownArrow);
317 AppendMenu( menu, "\pMute/M" ); //12
319 AppendMenu( menu, "\p(-" ); //13
321 AppendMenu( menu, "\pEject/E" ); //14
323 InsertMenu( menu, 0 );
331 /*****************************************************************************
332 * intf_Close: destroy interface
333 *****************************************************************************/
334 static void intf_Close( intf_thread_t *p_intf )
336 /* Destroy structure */
337 free( p_intf->p_sys );
340 /*****************************************************************************
341 * intf_Run: main loop
342 *****************************************************************************/
343 static void intf_Run( intf_thread_t *p_intf )
347 EventLoopTimerUPP manageUPP;
349 // EventTypeSpec windowEventType = { kEventClassWindow, kEventWindowClose };
350 // EventHandlerUPP windowHandlerUPP;
352 //kinda going out of bounds here... need to bring window creation to this file.
356 EventTypeSpec keyboardEventType = { kEventClassKeyboard, kEventRawKeyDown };
357 EventHandlerUPP keyboardHandlerUPP;
360 manageUPP = NewEventLoopTimerUPP ( CarbonManageCallback );
361 err = InstallEventLoopTimer ( GetCurrentEventLoop(), 0, kMainLoopFrequency, manageUPP, (void *) p_intf, &p_intf->p_sys->manageTimer );
362 assert(err == noErr);
363 DisposeEventLoopTimerUPP(manageUPP);
365 /* windowHandlerUPP = NewEventHandlerUPP ( MyWindowEventHandler );
366 err = InstallWindowEventHandler ( p_main->p_vout->p_sys->p_window , windowHandlerUPP, GetEventTypeCount(windowEventType), &windowEventType, (void *) p_intf, NULL );
367 assert(err == noErr);
368 DisposeEventHandlerUPP(windowHandlerUPP);
376 //Our big event loop !-)
377 RunApplicationEventLoop();
379 err = RemoveEventLoopTimer(p_intf->p_sys->manageTimer);
380 assert(err == noErr);
385 void CarbonManageCallback ( EventLoopTimerRef inTimer, void *inUserData )
387 intf_thread_t * p_intf = (intf_thread_t *) inUserData;
389 /* Manage core vlc functions through the callback */
390 p_intf->pf_manage( p_intf );
394 QuitApplicationEventLoop();
400 void EventLoop( intf_thread_t *p_intf )
407 gotEvent = WaitNextEvent(everyEvent,&event,32767,nil);
409 DoEvent( p_intf, &event);
410 } while (! p_intf->b_die );
415 void DoEvent( intf_thread_t *p_intf , EventRecord *event)
421 WindowRef whichWindow;
426 part = FindWindow(event->where, &whichWindow);
429 case inMenuBar: /* process a moused menu command */
430 DoMenuCommand( p_intf, MenuSelect(event->where));
437 if (whichWindow != FrontWindow())
438 SelectWindow(whichWindow);
441 case inDrag: /* pass screenBits.bounds */
442 GetRegionBounds(GetGrayRgn(), &tempRect);
443 DragWindow(whichWindow, event->where, &tempRect);
450 p_intf->b_die = true;
452 //DisposeWindow(whichWindow);
458 hit = TrackBox(whichWindow, event->where, part);
461 SetPort(GetWindowPort(whichWindow)); // window must be current port
462 EraseRect(GetWindowPortBounds(whichWindow, &tempRect)); // inval/erase because of ZoomWindow bug
463 ZoomWindow(whichWindow, part, true);
464 InvalWindowRect(whichWindow, GetWindowPortBounds(whichWindow, &tempRect));
472 key = event->message & charCodeMask;
473 if (event->modifiers & cmdKey)
474 if (event->what == keyDown)
475 DoMenuCommand( p_intf, MenuKey(key));
477 case activateEvt: /* if you needed to do something special */
481 DrawWindow((WindowRef) event->message);
484 case kHighLevelEvent:
485 AEProcessAppleEvent( event );
493 void DoMenuCommand( intf_thread_t *p_intf , long menuResult)
495 short menuID; /* the resource ID of the selected menu */
496 short menuItem; /* the item number of the selected menu */
498 static int vol_val; // remember the current volume
499 static int playback_status; // remember playback state
501 menuID = HiWord(menuResult); /* use macros to get item & menu number */
502 menuItem = LoWord(menuResult);
516 p_intf->b_die = true;
538 char device_method_and_name[B_FILE_NAME_LENGTH + 4];
539 if(p_message->FindString("device", device) != B_ERROR)
541 sprintf(device_method_and_name, "dvd:%s", *device);
542 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END, device_method_and_name );
551 HideWindow( FrontWindow() );
565 case kControlsPlayORPause:
566 // pause the playback
567 if (p_intf->p_input != NULL )
569 // mute the volume if currently playing
570 if (playback_status == PLAYING)
572 if (p_main->p_aout != NULL)
574 p_main->p_aout->vol = 0;
576 playback_status = PAUSED;
579 // restore the volume
581 if (p_main->p_aout != NULL)
583 p_main->p_aout->vol = vol_val;
585 playback_status = PLAYING;
588 input_SetStatus(p_intf->p_input, INPUT_STATUS_PAUSE);
593 // this currently stops playback not nicely
594 if (p_intf->p_input != NULL )
596 // silence the sound, otherwise very horrible
597 if (p_main->p_aout != NULL)
599 p_main->p_aout->vol = 0;
602 input_SetStatus(p_intf->p_input, INPUT_STATUS_END);
606 case kControlsForward:
607 // cycle the fast playback modes
608 if (p_intf->p_input != NULL )
610 if (p_main->p_aout != NULL)
612 p_main->p_aout->vol = 0;
615 input_SetStatus(p_intf->p_input, INPUT_STATUS_FASTER);
619 case kControlsRewind:
620 // cycle the slow playback modes
621 if (p_intf->p_input != NULL )
623 if (p_main->p_aout != NULL)
625 p_main->p_aout->vol = 0;
628 input_SetStatus(p_intf->p_input, INPUT_STATUS_SLOWER);
632 case kControlsChapterNext:
637 case kControlsChapterPrevious:
642 case kControlsDVDMenu:
647 case kControlsVolumeUp:
649 if (p_main->p_aout != NULL)
651 p_main->p_aout->vol++;
655 case kControlsVolumeDown:
657 if (p_main->p_aout != NULL)
659 p_main->p_aout->vol--;
663 case kControlsVolumeMute:
665 if (p_main->p_aout != NULL)
667 if (p_main->p_aout->vol == 0)
669 //p_vol->SetEnabled(true);
670 p_main->p_aout->vol = vol_val;
674 //p_vol->SetEnabled(false);
675 vol_val = p_main->p_aout->vol;
676 p_main->p_aout->vol = 0;
694 HiliteMenu(0); /* unhighlight what MenuSelect (or MenuKey) hilited */
697 void DrawWindow(WindowRef window)
703 SetPort(GetWindowPort(window));
705 EraseRect(GetWindowPortBounds(window, &tempRect));
706 DrawControls(window);
707 DrawGrowIcon(window);
715 static pascal OSStatus MyEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData)
720 HICommand commandStruct;
723 OSStatus result = eventNotHandledErr; // report failure by default
725 GetEventParameter(event, kEventParamDirectObject, typeWindowRef, NULL, sizeof(window), NULL, &window);
727 whatHappened = GetEventKind(event);
729 switch (whatHappened)
731 case kEventWindowActivated:
734 case kEventWindowDeactivated:
737 case kEventWindowDrawContent:
742 case kEventWindowBoundsChanged:
743 InvalWindowRect(window, GetWindowPortBounds(window, &bounds));
748 case kEventWindowClickContentRgn:
749 /*DoContentClick(window);
755 case kEventCommandProcess:
756 GetEventParameter (event, kEventParamDirectObject,
757 typeHICommand, NULL, sizeof(HICommand),
758 NULL, &commandStruct);
759 theMenuRef = commandStruct.menu.menuRef;
761 if (theMenuRef == GetMenuHandle(kMenuApple))
763 // Because the event didn't occur *in* the window, the
764 // window reference isn't valid until we set it here
765 window = FrontWindow();
767 theMenuItem = commandStruct.menu.menuItemIndex;
768 switch ( theMenuItem )
771 SetLight(window, true);
774 SetLight(window, false);
784 case kEventMouseMoved:
786 CursorRgn = NewRgn();
787 GetEventParameter (event, kEventParamMouseLocation, typeQDPoint,
788 NULL, sizeof(Point), NULL, &wheresMyMouse);
789 AdjustCursor(wheresMyMouse, CursorRgn);
790 DisposeRgn(CursorRgn);
796 // If nobody handled the event, it gets propagated to the
797 // application-level handler.