]> git.sesse.net Git - vlc/blob - src/misc/beos_specific.cpp
beos_specific.cpp: never let anyone but system_End terminate the
[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.35 2003/12/14 16:26:21 titer 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     bool         fReadyToQuit;
60 };
61
62 /*****************************************************************************
63  * Static vars
64  *****************************************************************************/
65
66 #include "../../modules/gui/beos/MsgVals.h"
67 #define REALLY_QUIT 'requ'
68
69 extern "C"
70 {
71
72 /*****************************************************************************
73  * Local prototypes.
74  *****************************************************************************/
75 static void AppThread( vlc_object_t *p_appthread );
76
77 /*****************************************************************************
78  * system_Init: create a BApplication object and fill in program path.
79  *****************************************************************************/
80 void system_Init( vlc_t *p_this, int *pi_argc, char *ppsz_argv[] )
81 {
82     p_this->p_libvlc->p_appthread =
83             (vlc_object_t *)vlc_object_create( p_this, sizeof(vlc_object_t) );
84
85     /* Create the BApplication thread and wait for initialization */
86     vlc_thread_create( p_this->p_libvlc->p_appthread, "app thread", AppThread,
87                        VLC_THREAD_PRIORITY_LOW, VLC_TRUE );
88 }
89
90 /*****************************************************************************
91  * system_Configure: check for system specific configuration options.
92  *****************************************************************************/
93 void system_Configure( vlc_t *, int *pi_argc, char *ppsz_argv[] )
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( REALLY_QUIT );
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 =
119         new VlcApplication("application/x-vnd.videolan-vlc");
120     vlc_object_attach( p_this, p_this->p_vlc );
121     BeApp->p_this = p_this;
122     BeApp->Run();
123     vlc_object_detach( p_this );
124     delete BeApp;
125 }
126
127 } /* extern "C" */
128
129 /*****************************************************************************
130  * VlcApplication: application constructor
131  *****************************************************************************/
132 VlcApplication::VlcApplication( char * psz_mimetype )
133                :BApplication( psz_mimetype ),
134                 fInterfaceWindow( NULL ),
135                 fRefsMessage( NULL ),
136                 fReadyToQuit( false )
137 {
138     /* Nothing to do, we use the default constructor */
139 }
140
141 /*****************************************************************************
142  * ~VlcApplication: application destructor
143  *****************************************************************************/
144 VlcApplication::~VlcApplication( )
145 {
146     /* Nothing to do, we use the default destructor */
147     delete fRefsMessage;
148 }
149
150 /*****************************************************************************
151  * AboutRequested: called by the system on B_ABOUT_REQUESTED
152  *****************************************************************************/
153 void VlcApplication::AboutRequested( )
154 {
155     BAlert *alert;
156     alert = new BAlert( "VLC " PACKAGE_VERSION,
157                         "VLC " PACKAGE_VERSION " for BeOS\n\n"
158                                         "<www.videolan.org>", "OK");
159     alert->Go( NULL );
160 }
161
162 /*****************************************************************************
163  * ReadyToRun: called when the BApplication is initialized
164  *****************************************************************************/
165 void VlcApplication::ReadyToRun( )
166 {
167     BPath path;
168     app_info info;
169
170     /* Get the program path */
171     be_app->GetAppInfo( &info );
172     BEntry entry( &info.ref );
173     entry.GetPath( &path );
174     path.GetParent( &path );
175     p_this->p_libvlc->psz_vlcpath = strdup( path.Path() );
176
177     /* Tell the main thread we are finished initializing the BApplication */
178     vlc_thread_ready( p_this );
179 }
180
181 /*****************************************************************************
182  * RefsReceived: called when files are sent to our application
183  *               (for example when the user drops fils onto our icon)
184  *****************************************************************************/
185 void VlcApplication::RefsReceived(BMessage* message)
186 {
187     if (fInterfaceWindow)
188         fInterfaceWindow->PostMessage(message);
189     else {
190         delete fRefsMessage;
191         fRefsMessage = new BMessage(*message);
192     }
193 }
194
195 /*****************************************************************************
196  * MessageReceived: a BeOS applications main message loop
197  *                  Since VlcApplication and interface are separated
198  *                  in the vlc binary and the interface plugin,
199  *                  we use this method to "stick" them together.
200  *                  The interface will post a message to the global
201  *                  "be_app" pointer when the interface is created
202  *                  containing a pointer to the interface window.
203  *                  In this way, we can keep a B_REFS_RECEIVED message
204  *                  in store for the interface window to handle later.
205  *****************************************************************************/
206 void VlcApplication::MessageReceived(BMessage* message)
207 {
208     switch (message->what) {
209         case INTERFACE_CREATED: {
210             BWindow* interfaceWindow;
211             if (message->FindPointer("window", (void**)&interfaceWindow) == B_OK) {
212                 fInterfaceWindow = interfaceWindow;
213                 if (fRefsMessage) {
214                     fInterfaceWindow->PostMessage(fRefsMessage);
215                     delete fRefsMessage;
216                     fRefsMessage = NULL;
217                 }
218             }
219             break;
220         }
221
222         case REALLY_QUIT:
223             fReadyToQuit = true;
224             PostMessage( B_QUIT_REQUESTED );
225             break;
226         
227         default:
228             BApplication::MessageReceived(message);
229     }
230 }
231
232 bool VlcApplication::QuitRequested()
233 {
234     if( !fReadyToQuit )
235     {
236         p_this->p_vlc->b_die = 1;
237         return false;
238     }
239
240     BApplication::QuitRequested();
241     return true;
242 }