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