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