Bug 575855 - Fix for transitions from fullscreen briefly show an aero basic window...
[mozilla-central.git] / toolkit / xre / nsNativeAppSupportBeOS.cpp
blob717c61cc06a5d1cfe2be12c156bd0cc2ec2b9d73
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Mozilla Browser.
16 * The Initial Developer of the Original Code is
17 * Fredrik Holmqvist <thesuckiestemail@yahoo.se>.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sergei Dolgov <sergei_d@fi.tartu.ee>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 //This define requires DebugConsole (see BeBits.com) to be installed
39 //#define DC_PROGRAMNAME "firefox-bin"
40 # ifdef DC_PROGRAMNAME
41 #include <DebugConsole.h>
42 #endif
44 #include "nsIServiceManager.h"
45 #include "nsNativeAppSupportBase.h"
46 #include "nsICommandLineRunner.h"
47 #include "nsCOMPtr.h"
48 #include "nsIProxyObjectManager.h"
49 //#include "nsIBrowserDOMWindow.h"
50 #include "nsPIDOMWindow.h"
51 #include "nsIDOMChromeWindow.h"
52 #include "nsIWindowMediator.h"
53 #include "nsXPIDLString.h"
54 #include "nsIBaseWindow.h"
55 #include "nsIWidget.h"
56 #include "nsIDocShell.h"
58 #include <Application.h>
59 #include <AppFileInfo.h>
60 #include <Resources.h>
61 #include <Path.h>
62 #include <Window.h>
63 #include <unistd.h>
65 // Two static helpers for future - if we decide to use OpenBrowserWindow, like we do in SeaMonkey
66 static nsresult
67 GetMostRecentWindow(const PRUnichar* aType, nsIDOMWindowInternal** aWindow)
69 nsresult rv;
70 nsCOMPtr<nsIWindowMediator> med(do_GetService( NS_WINDOWMEDIATOR_CONTRACTID, &rv));
71 if (NS_FAILED(rv))
72 return rv;
74 if (med)
76 nsCOMPtr<nsIWindowMediator> medProxy;
77 rv = NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD, NS_GET_IID(nsIWindowMediator),
78 med, NS_PROXY_SYNC | NS_PROXY_ALWAYS,
79 getter_AddRefs(medProxy));
80 if (NS_FAILED(rv))
81 return rv;
82 return medProxy->GetMostRecentWindow( aType, aWindow );
84 return NS_ERROR_FAILURE;
87 static nsresult
88 ActivateWindow(nsIDOMWindowInternal* aWindow)
90 nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
91 NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
92 nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(window->GetDocShell()));
93 NS_ENSURE_TRUE(baseWindow, NS_ERROR_FAILURE);
94 nsCOMPtr<nsIWidget> mainWidget;
95 baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
96 NS_ENSURE_TRUE(mainWidget, NS_ERROR_FAILURE);
97 BWindow *bwindow = (BWindow *)(mainWidget->GetNativeData(NS_NATIVE_WINDOW));
98 if (bwindow)
99 bwindow->Activate(true);
100 return NS_OK;
102 //End static helpers
104 class nsNativeAppSupportBeOS : public nsNativeAppSupportBase
106 public:
107 NS_DECL_ISUPPORTS
108 NS_DECL_NSINATIVEAPPSUPPORT
109 static void HandleCommandLine( int32 argc, char **argv, PRUint32 aState);
110 }; // nsNativeAppSupportBeOS
113 class nsBeOSApp : public BApplication
115 public:
116 nsBeOSApp(sem_id sem) : BApplication( GetAppSig() ), init(sem), mMessage(NULL)
119 ~nsBeOSApp()
121 delete mMessage;
124 void ReadyToRun()
126 release_sem(init);
129 static int32 Main( void *args )
131 nsBeOSApp *app = new nsBeOSApp((sem_id)args);
132 if (app == NULL)
133 return B_ERROR;
134 return app->Run();
137 void ArgvReceived(int32 argc, char **argv)
139 if (IsLaunching())
141 #ifdef DC_PROGRAMNAME
142 TRACE("ArgvReceived Launching\n");
143 #endif
144 return;
146 PRInt32 aState = /*IsLaunching() ?
147 nsICommandLine::STATE_INITIAL_LAUNCH :*/
148 nsICommandLine::STATE_REMOTE_AUTO;
149 nsNativeAppSupportBeOS::HandleCommandLine(argc, argv, aState);
152 void RefsReceived(BMessage* msg)
154 #ifdef DC_PROGRAMNAME
155 TRACE("RefsReceived\n");
156 #endif
157 if (IsLaunching())
159 mMessage = new BMessage(*msg);
160 return;
162 BPath path;
163 entry_ref er;
164 for (uint32 i = 0; msg->FindRef("refs", i, &er) == B_OK; i++)
166 int Argc = 2;
167 char **Argv = new char*[ 3 ];
168 BEntry entry(&er, true);
169 BEntry fentry(GetAppFile(), false);
170 entry.GetPath(&path);
172 Argv[0] = strdup( GetAppFile() ? GetAppFile() : "" );
173 Argv[1] = strdup( path.Path() ? path.Path() : "" );
174 // Safety measure
175 Argv[2] = 0;
176 // Is started, call ArgReceived, delete mArgv, else store for future usage
177 // after ::Enable() was called
178 ArgvReceived(2, Argv);
179 Argc = 0;
180 delete [] Argv;
181 Argv = NULL;
185 void MessageReceived(BMessage* msg)
187 // BMessage from nsNativeAppBeOS::Enable() received.
188 // Services are ready, so we can supply stored refs
189 if (msg->what == 'enbl' && mMessage)
191 #ifdef DC_PROGRAMNAME
192 TRACE("enbl received");
193 #endif
194 be_app_messenger.SendMessage(mMessage);
196 // Processing here file drop events from BWindow
197 // - until we implement native DnD in widget.
198 else if (msg->what == B_SIMPLE_DATA)
200 RefsReceived(msg);
202 else
203 BApplication::MessageReceived(msg);
205 private:
206 char *GetAppSig()
208 image_info info;
209 int32 cookie = 0;
210 BFile file;
211 BAppFileInfo appFileInfo;
212 static char sig[B_MIME_TYPE_LENGTH];
214 sig[0] = 0;
215 if (get_next_image_info(0, &cookie, &info) == B_OK &&
216 file.SetTo(info.name, B_READ_ONLY) == B_OK &&
217 appFileInfo.SetTo(&file) == B_OK &&
218 appFileInfo.GetSignature(sig) == B_OK)
219 return sig;
221 return "application/x-vnd.Mozilla";
224 char *GetAppFile()
226 image_info info;
227 int32 cookie = 0;
228 if (get_next_image_info(0, &cookie, &info) == B_OK && strlen(info.name) > 0)
229 return info.name;
231 return "";
234 sem_id init;
235 BMessage *mMessage;
236 }; //class nsBeOSApp
238 // Create and return an instance of class nsNativeAppSupportBeOS.
239 nsresult
240 NS_CreateNativeAppSupport(nsINativeAppSupport **aResult)
242 if (!aResult)
243 return NS_ERROR_NULL_POINTER;
245 nsNativeAppSupportBeOS *pNative = new nsNativeAppSupportBeOS;
246 if (!pNative)
247 return NS_ERROR_OUT_OF_MEMORY;
249 *aResult = pNative;
250 NS_ADDREF(*aResult);
251 return NS_OK;
254 NS_IMPL_ISUPPORTS1(nsNativeAppSupportBeOS, nsINativeAppSupport)
257 void
258 nsNativeAppSupportBeOS::HandleCommandLine(int32 argc, char **argv, PRUint32 aState)
260 nsresult rv;
261 // Here we get stuck when starting from file-click or "OpenWith".
262 // No cmdLine or any other service can be created
263 // To workaround the problem, we store arguments if IsLaunching()
264 // and using this after ::Enable() was called.
265 nsCOMPtr<nsICommandLineRunner> cmdLine(do_CreateInstance("@mozilla.org/toolkit/command-line;1"));
266 if (!cmdLine)
268 #ifdef DC_PROGRAMNAME
269 TRACE("Couldn't create command line!");
270 #endif
271 return;
274 // nsICommandLineRunner::Init() should be called from main mozilla thread
275 // but we are at be_app thread. Using proxy to switch thread
276 nsCOMPtr<nsICommandLineRunner> cmdLineProxy;
277 rv = NS_GetProxyForObject( NS_PROXY_TO_MAIN_THREAD, NS_GET_IID(nsICommandLineRunner),
278 cmdLine, NS_PROXY_ASYNC | NS_PROXY_ALWAYS, getter_AddRefs(cmdLineProxy));
279 if (rv != NS_OK)
281 #ifdef DC_PROGRAMNAME
282 TRACE("Couldn't get command line Proxy!");
283 #endif
284 return;
287 // nsICommandLineRunner::Init(,,workingdir,) requires some folder to be provided
288 // but that's unclear if we need it, so using 0 instead atm
289 rv = cmdLine->Init(argc, argv, 0 , aState);
290 if (rv != NS_OK)
292 #ifdef DC_PROGRAMNAME
293 TRACE("Couldn't init command line!");
294 #endif
295 return;
298 nsCOMPtr<nsIDOMWindowInternal> navWin;
299 GetMostRecentWindow( NS_LITERAL_STRING( "navigator:browser" ).get(),
300 getter_AddRefs(navWin ));
301 if (navWin)
303 # ifdef DC_PROGRAMNAME
304 TRACE("GotNavWin!");
305 # endif
306 cmdLine->SetWindowContext(navWin);
309 // TODO: try to use OpenURI here if there is navWin, maybe using special function
310 // OpenBrowserWindow which calls OpenURI like we do for SeaMonkey,
311 // else let CommandLineRunner to do its work.
312 // Problem with current implementation is unsufficient tabbed browsing support
313 cmdLineProxy->Run();
316 NS_IMETHODIMP
317 nsNativeAppSupportBeOS::Start(PRBool *aResult)
319 NS_ENSURE_ARG(aResult);
320 NS_ENSURE_TRUE(be_app == NULL, NS_ERROR_NOT_INITIALIZED);
321 sem_id initsem = create_sem(0, "Mozilla BApplication init");
322 if (initsem < B_OK)
323 return NS_ERROR_FAILURE;
324 thread_id tid = spawn_thread(nsBeOSApp::Main, "Mozilla XUL BApplication", B_NORMAL_PRIORITY, (void *)initsem);
325 #ifdef DC_PROGRAMNAME
326 TRACE("BeApp created");
327 #endif
328 *aResult = PR_TRUE;
329 if (tid < B_OK || B_OK != resume_thread(tid))
330 *aResult = PR_FALSE;
332 if (B_OK != acquire_sem(initsem))
333 *aResult = PR_FALSE;
335 if (B_OK != delete_sem(initsem))
336 *aResult = PR_FALSE;
337 return *aResult == PR_TRUE ? NS_OK : NS_ERROR_FAILURE;
340 NS_IMETHODIMP
341 nsNativeAppSupportBeOS::Stop(PRBool *aResult)
343 NS_ENSURE_ARG(aResult);
344 NS_ENSURE_TRUE(be_app, NS_ERROR_NOT_INITIALIZED);
346 *aResult = PR_TRUE;
347 return NS_OK;
350 NS_IMETHODIMP
351 nsNativeAppSupportBeOS::Quit()
353 if (be_app->Lock())
355 be_app->Quit();
356 return NS_OK;
358 return NS_ERROR_FAILURE;
361 NS_IMETHODIMP
362 nsNativeAppSupportBeOS::ReOpen()
364 return NS_ERROR_NOT_IMPLEMENTED;
367 NS_IMETHODIMP
368 nsNativeAppSupportBeOS::Enable()
370 // Informing be_app that UI and services are ready to use.
371 if (be_app)
373 be_app_messenger.SendMessage('enbl');
375 return NS_OK;
378 NS_IMETHODIMP
379 nsNativeAppSupportBeOS::OnLastWindowClosing()
381 return NS_OK;