Back to index

lightning-sunbird  0.9+nobinonly
nsBeOSMain.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Chris Seawood <cls@seawood.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include <signal.h>
00039 #include <be/app/Application.h>
00040 
00041 #include "nsIAppShell.h"
00042 #include "nsViewerApp.h"
00043 #include "nsBrowserWindow.h"
00044 #include "nsBrowserWindow.h"
00045 #include <stdlib.h>
00046 #include "plevent.h"
00047 
00048 nsNativeViewerApp::nsNativeViewerApp()
00049 {
00050 }
00051 
00052 nsNativeViewerApp::~nsNativeViewerApp()
00053 {
00054 }
00055 
00056 int
00057 nsNativeViewerApp::Run() 
00058 {
00059        OpenWindow();
00060 
00061        if(mAppShell) {
00062               nsIAppShell *theAppShell = mAppShell;
00063               NS_ADDREF(theAppShell);
00064               mAppShell->Run();
00065               NS_RELEASE(theAppShell);
00066        }
00067 
00068        return 0;
00069 }
00070 
00071 //----------------------------------------------------------------------
00072 
00073 nsNativeBrowserWindow::nsNativeBrowserWindow()
00074 {
00075 }
00076 
00077 nsNativeBrowserWindow::~nsNativeBrowserWindow()
00078 {
00079 }
00080 
00081 nsresult
00082 nsNativeBrowserWindow::InitNativeWindow()
00083 {
00084        // override to do something special with platform native windows
00085        return NS_OK;
00086 }
00087 
00088 nsresult
00089 nsNativeBrowserWindow::CreateMenuBar(PRInt32 aWidth)
00090 {
00091        printf("nsNativeBrowserWindow:: CreateMenuBar not implemented\n");
00092 
00093        return NS_OK;
00094 }
00095 
00096 nsresult
00097 nsNativeBrowserWindow::GetMenuBarHeight(PRInt32 * aHeightOut)
00098 {
00099   NS_ASSERTION(nsnull != aHeightOut,"null out param.");
00100 
00101   *aHeightOut = 0;
00102 
00103   return NS_OK;
00104 }
00105 
00106 nsEventStatus
00107 nsNativeBrowserWindow::DispatchMenuItem(PRInt32 aID)
00108 {
00109   // Dispatch windows-only menu code goes here
00110 
00111   // Dispatch xp menu items
00112   return nsBrowserWindow::DispatchMenuItem(aID);
00113 }
00114 
00115 //----------------------------------------------------------------------
00116 
00117 class nsBeOSApp : public BApplication
00118 {
00119 public:
00120   nsBeOSApp(sem_id sem)
00121     : BApplication("application/x-vnd.mozilla.viewer"), init(sem) { }
00122 
00123        void ReadyToRun(void) {
00124               release_sem(init);
00125        }
00126 
00127        static int32 Main(void *args) {
00128               nsBeOSApp *app = new nsBeOSApp((sem_id)args);
00129               if (!app)
00130                      return B_ERROR;
00131               return app->Run();
00132        }
00133 
00134 private:
00135        sem_id init;
00136 };
00137 
00138 //----------------------------------------------------------------------
00139 static nsNativeViewerApp *NVApp = 0;
00140 
00141 static void beos_signal_handler(int signum) {
00142 #ifdef DEBUG
00143        fprintf(stderr, "beos_signal_handler: %d\n", signum);
00144 #endif
00145 
00146        // Exit native appshell loop so that viewer will shutdown normally
00147        if (NVApp)
00148               NVApp->Exit();
00149 }
00150 
00151 static nsresult InitializeBeOSApp(void)
00152 {
00153        nsresult rv = NS_OK;
00154 
00155        sem_id initsem = create_sem(0, "beapp init");
00156        if (initsem < B_OK)
00157               return NS_ERROR_FAILURE;
00158 
00159        thread_id tid = spawn_thread(nsBeOSApp::Main, "BApplication", 
00160                                    B_NORMAL_PRIORITY, (void *)initsem);
00161        if (tid < B_OK || B_OK != resume_thread(tid))
00162               rv = NS_ERROR_FAILURE;
00163 
00164        if (B_OK != acquire_sem(initsem))
00165               rv = NS_ERROR_FAILURE;
00166        if (B_OK != delete_sem(initsem))
00167               rv = NS_ERROR_FAILURE;
00168 
00169        return rv;
00170 }
00171 
00172 
00173 int main(int argc, char **argv)
00174 {
00175        signal(SIGTERM, beos_signal_handler);
00176 
00177        if (NS_OK != InitializeBeOSApp())
00178               return 1;
00179 
00180        // Init XPCOM
00181        nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00182        NS_ASSERTION(NS_SUCCEEDED(rv), "NS_InitXPCOM failed");
00183        if (NS_SUCCEEDED(rv)) {
00184 
00185               // Run viewer app
00186               NVApp = new nsNativeViewerApp();
00187               if (!NVApp) {
00188 #ifdef DEBUG
00189                      fprintf(stderr, "Could not allocate mem for nsNativeViewerApp\n");
00190 #endif
00191                      // Shutdown XPCOM
00192                      rv = NS_ShutdownXPCOM(nsnull);
00193                      return 1;
00194               }
00195               NVApp->Initialize(argc,argv);
00196               NVApp->Run();
00197               delete NVApp;
00198               NVApp = 0;
00199 
00200               // Shutdown XPCOM
00201               rv = NS_ShutdownXPCOM(nsnull);
00202               NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00203        }
00204 
00205        // Delete BApplication
00206        if (be_app->Lock())
00207               be_app->Quit();
00208        delete be_app;
00209 
00210        return 0;
00211 }
00212