]> git.sesse.net Git - vlc/blob - src/misc/beos_specific.cpp
e8d80a021213e80e8c3af77de4db261d8cb94167
[vlc] / src / misc / beos_specific.cpp
1 /*****************************************************************************
2  * beos_init.cpp: Initialization for BeOS specific features 
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: beos_specific.cpp,v 1.32 2003/09/29 17:36:35 gbazin Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
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 #include <Application.h>
24 #include <Roster.h>
25 #include <Path.h>
26 #include <Alert.h>
27 #include <Message.h>
28 #include <Window.h>
29
30 #include <stdio.h>
31 #include <string.h> /* strdup() */
32 #include <malloc.h>   /* free() */
33
34 extern "C"
35 {
36 #include <vlc/vlc.h>
37 }
38
39 /*****************************************************************************
40  * The VlcApplication class
41  *****************************************************************************/
42 class VlcApplication : public BApplication
43 {
44 public:
45     vlc_object_t *p_this;
46
47     VlcApplication(char* );
48     ~VlcApplication();
49
50     virtual void ReadyToRun();
51     virtual void AboutRequested();
52     virtual void RefsReceived(BMessage* message);
53     virtual void MessageReceived(BMessage* message);
54     virtual bool QuitRequested();
55     
56 private:
57     BWindow*     fInterfaceWindow;
58     BMessage*    fRefsMessage;
59 };
60
61 /*****************************************************************************
62  * Static vars
63  *****************************************************************************/
64
65 //const uint32 INTERFACE_CREATED = 'ifcr';  /* message sent from interface */
66 #include "../../modules/gui/beos/MsgVals.h"
67
68 extern "C"
69 {
70
71 /*****************************************************************************
72  * Local prototypes.
73  *****************************************************************************/
74 static void AppThread( vlc_object_t *p_appthread );
75
76 /*****************************************************************************
77  * system_Init: create a BApplication object and fill in program path.
78  *****************************************************************************/
79 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
80 {
81     p_this->p_libvlc->p_appthread =
82             (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
83
84     /* Create the BApplication thread and wait for initialization */
85     vlc_thread_create( p_this->p_libvlc->p_appthread, "app thread", AppThread,
86                        VLC_THREAD_PRIORITY_LOW, VLC_TRUE );
87 }
88
89 /*****************************************************************************
90  * system_Configure: check for system specific configuration options.
91  *****************************************************************************/
92 void system_Configure( vlc_t *, int *pi_argc, char *ppsz_argv[] )
93 {
94
95 }
96
97 /*****************************************************************************
98  * system_End: destroy the BApplication object.
99  *****************************************************************************/
100 void system_End( vlc_t *p_this )
101 {
102     /* Tell the BApplication to die */
103     be_app->PostMessage( B_QUIT_REQUESTED );
104
105     vlc_thread_join( p_this->p_libvlc->p_appthread );
106     vlc_object_destroy( p_this->p_libvlc->p_appthread );
107
108     free( p_this->p_libvlc->psz_vlcpath );
109 }
110
111 /* following functions are local */
112
113 /*****************************************************************************
114  * AppThread: the BApplication thread.
115  *****************************************************************************/
116 static void AppThread( vlc_object_t * p_this )
117 {
118     VlcApplication *BeApp = new VlcApplication("application/x-vnd.videolan-vlc");
119     vlc_object_attach( p_this, p_this->p_vlc );
120     BeApp->p_this = p_this;
121     BeApp->Run();
122     vlc_object_detach( p_this );
123     delete BeApp;
124 }
125
126 } /* extern "C" */
127
128 /*****************************************************************************
129  * VlcApplication: application constructor
130  *****************************************************************************/
131 VlcApplication::VlcApplication( char * psz_mimetype )
132                :BApplication( psz_mimetype ),
133                 fInterfaceWindow( NULL ),
134                 fRefsMessage( NULL )
135 {
136     /* Nothing to do, we use the default constructor */
137 }
138
139 /*****************************************************************************
140  * ~VlcApplication: application destructor
141  *****************************************************************************/
142 VlcApplication::~VlcApplication( )
143 {
144     /* Nothing to do, we use the default destructor */
145     delete fRefsMessage;
146 }
147
148 /*****************************************************************************
149  * AboutRequested: called by the system on B_ABOUT_REQUESTED
150  *****************************************************************************/
151 void VlcApplication::AboutRequested( )
152 {
153     BAlert *alert;
154     alert = new BAlert( "VLC " PACKAGE_VERSION,
155                         "VLC " PACKAGE_VERSION " for BeOS\n\n"
156                                         "<www.videolan.org>", "OK");
157     alert->Go( NULL );
158 }
159
160 /*****************************************************************************
161  * ReadyToRun: called when the BApplication is initialized
162  *****************************************************************************/
163 void VlcApplication::ReadyToRun( )
164 {
165     BPath path;
166     app_info info; 
167
168     /* Get the program path */
169     be_app->GetAppInfo( &info ); 
170     BEntry entry( &info.ref ); 
171     entry.GetPath( &path ); 
172     path.GetParent( &path );
173     p_this->p_libvlc->psz_vlcpath = strdup( path.Path() );
174
175     /* Tell the main thread we are finished initializing the BApplication */
176     vlc_thread_ready( p_this );
177 }
178
179 /*****************************************************************************
180  * RefsReceived: called when files are sent to our application
181  *               (for example when the user drops fils onto our icon)
182  *****************************************************************************/
183 void VlcApplication::RefsReceived(BMessage* message)
184 {
185         if (fInterfaceWindow)
186                 fInterfaceWindow->PostMessage(message);
187         else {
188                 delete fRefsMessage;
189                 fRefsMessage = new BMessage(*message);
190         }
191 }
192
193 /*****************************************************************************
194  * MessageReceived: a BeOS applications main message loop
195  *                  Since VlcApplication and interface are separated
196  *                  in the vlc binary and the interface plugin,
197  *                  we use this method to "stick" them together.
198  *                  The interface will post a message to the global
199  *                  "be_app" pointer when the interface is created
200  *                  containing a pointer to the interface window.
201  *                  In this way, we can keep a B_REFS_RECEIVED message
202  *                  in store for the interface window to handle later.
203  *****************************************************************************/
204 void VlcApplication::MessageReceived(BMessage* message)
205 {
206         switch (message->what) {
207                 case INTERFACE_CREATED: {
208                         BWindow* interfaceWindow;
209                         if (message->FindPointer("window", (void**)&interfaceWindow) == B_OK) {
210                                 fInterfaceWindow = interfaceWindow;
211                                 if (fRefsMessage) {
212                                         fInterfaceWindow->PostMessage(fRefsMessage);
213                                         delete fRefsMessage;
214                                         fRefsMessage = NULL;
215                                 }
216                         }
217                         break;
218                 }
219                 
220                 default:
221                         BApplication::MessageReceived(message);
222         }
223 }
224
225 bool VlcApplication::QuitRequested()
226 {
227     if( CurrentMessage() && CurrentMessage()->FindBool( "shortcut" ) ) 
228     {
229         /* The user hit Alt+Q, don't let the be_app exit without cleaning.
230            Let the interface do the job */
231         if( fInterfaceWindow )
232             fInterfaceWindow->PostMessage( B_QUIT_REQUESTED );
233         return false;
234     }
235
236     BApplication::QuitRequested();
237     return true;
238 }