1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:expandtab:shiftwidth=2:tabstop=8:
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Christopher Blizzard.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Christopher Blizzard <blizzard@mozilla.org>
26 : * Benjamin Smedberg <benjamin@smedbergs.us>
27 : * Miika Jarvinen <mjarvin@gmail.com>
28 : *
29 : * Alternatively, the contents of this file may be used under the terms of
30 : * either the GNU General Public License Version 2 or later (the "GPL"), or
31 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 : * in which case the provisions of the GPL or the LGPL are applicable instead
33 : * of those above. If you wish to allow use of your version of this file only
34 : * under the terms of either the GPL or the LGPL, and not to allow others to
35 : * use your version of this file under the terms of the MPL, indicate your
36 : * decision by deleting the provisions above and replace them with the notice
37 : * and other provisions required by the GPL or the LGPL. If you do not delete
38 : * the provisions above, a recipient may use your version of this file under
39 : * the terms of any one of the MPL, the GPL or the LGPL.
40 : *
41 : * ***** END LICENSE BLOCK ***** */
42 :
43 : #include "nsGTKRemoteService.h"
44 :
45 : #include <gtk/gtk.h>
46 : #include <gdk/gdk.h>
47 : #include <gdk/gdkx.h>
48 :
49 : #include "nsIBaseWindow.h"
50 : #include "nsIDocShell.h"
51 : #include "nsPIDOMWindow.h"
52 : #include "mozilla/ModuleUtils.h"
53 : #include "nsIServiceManager.h"
54 : #include "nsIWeakReference.h"
55 : #include "nsIWidget.h"
56 : #include "nsIAppShellService.h"
57 : #include "nsAppShellCID.h"
58 :
59 : #include "nsCOMPtr.h"
60 :
61 : #include "nsGTKToolkit.h"
62 :
63 0 : NS_IMPL_ISUPPORTS2(nsGTKRemoteService,
64 : nsIRemoteService,
65 : nsIObserver)
66 :
67 : NS_IMETHODIMP
68 0 : nsGTKRemoteService::Startup(const char* aAppName, const char* aProfileName)
69 : {
70 0 : NS_ASSERTION(aAppName, "Don't pass a null appname!");
71 0 : sRemoteImplementation = this;
72 :
73 0 : if (mServerWindow) return NS_ERROR_ALREADY_INITIALIZED;
74 :
75 0 : XRemoteBaseStartup(aAppName, aProfileName);
76 :
77 0 : mServerWindow = gtk_invisible_new();
78 0 : gtk_widget_realize(mServerWindow);
79 0 : HandleCommandsFor(mServerWindow, nsnull);
80 :
81 0 : if (!mWindows.IsInitialized())
82 0 : mWindows.Init();
83 :
84 0 : mWindows.EnumerateRead(StartupHandler, this);
85 :
86 0 : return NS_OK;
87 : }
88 :
89 : PLDHashOperator
90 0 : nsGTKRemoteService::StartupHandler(const void* aKey,
91 : nsIWeakReference* aData,
92 : void* aClosure)
93 : {
94 0 : GtkWidget* widget = (GtkWidget*) aKey;
95 0 : nsGTKRemoteService* aThis = (nsGTKRemoteService*) aClosure;
96 :
97 0 : aThis->HandleCommandsFor(widget, aData);
98 0 : return PL_DHASH_NEXT;
99 : }
100 :
101 0 : static nsIWidget* GetMainWidget(nsIDOMWindow* aWindow)
102 : {
103 : // get the native window for this instance
104 0 : nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
105 0 : NS_ENSURE_TRUE(window, nsnull);
106 :
107 : nsCOMPtr<nsIBaseWindow> baseWindow
108 0 : (do_QueryInterface(window->GetDocShell()));
109 0 : NS_ENSURE_TRUE(baseWindow, nsnull);
110 :
111 0 : nsCOMPtr<nsIWidget> mainWidget;
112 0 : baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
113 0 : return mainWidget;
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : nsGTKRemoteService::RegisterWindow(nsIDOMWindow* aWindow)
118 : {
119 0 : nsIWidget* mainWidget = GetMainWidget(aWindow);
120 0 : NS_ENSURE_TRUE(mainWidget, NS_ERROR_FAILURE);
121 :
122 : // walk up the widget tree and find the toplevel window in the
123 : // hierarchy
124 :
125 0 : nsIWidget* tempWidget = mainWidget->GetParent();
126 :
127 0 : while (tempWidget) {
128 0 : tempWidget = tempWidget->GetParent();
129 0 : if (tempWidget)
130 0 : mainWidget = tempWidget;
131 : }
132 :
133 : GtkWidget* widget =
134 0 : (GtkWidget*) mainWidget->GetNativeData(NS_NATIVE_SHELLWIDGET);
135 0 : NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
136 :
137 0 : nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(aWindow);
138 0 : NS_ENSURE_TRUE(weak, NS_ERROR_FAILURE);
139 :
140 0 : if (!mWindows.IsInitialized())
141 0 : mWindows.Init();
142 :
143 0 : mWindows.Put(widget, weak);
144 :
145 : // If Startup() has already been called, immediately register this window.
146 0 : if (mServerWindow) {
147 0 : HandleCommandsFor(widget, weak);
148 : }
149 :
150 0 : return NS_OK;
151 : }
152 :
153 : NS_IMETHODIMP
154 0 : nsGTKRemoteService::Shutdown()
155 : {
156 0 : if (!mServerWindow)
157 0 : return NS_ERROR_NOT_INITIALIZED;
158 :
159 0 : gtk_widget_destroy(mServerWindow);
160 0 : mServerWindow = nsnull;
161 0 : return NS_OK;
162 : }
163 :
164 : // Set desktop startup ID to the passed ID, if there is one, so that any created
165 : // windows get created with the right window manager metadata, and any windows
166 : // that get new tabs and are activated also get the right WM metadata.
167 : // The timestamp will be used if there is no desktop startup ID, or if we're
168 : // raising an existing window rather than showing a new window for the first time.
169 : void
170 0 : nsGTKRemoteService::SetDesktopStartupIDOrTimestamp(const nsACString& aDesktopStartupID,
171 : PRUint32 aTimestamp) {
172 0 : nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit();
173 0 : if (!toolkit)
174 0 : return;
175 :
176 0 : if (!aDesktopStartupID.IsEmpty()) {
177 0 : toolkit->SetDesktopStartupID(aDesktopStartupID);
178 : }
179 :
180 0 : toolkit->SetFocusTimestamp(aTimestamp);
181 : }
182 :
183 :
184 : void
185 0 : nsGTKRemoteService::HandleCommandsFor(GtkWidget* widget,
186 : nsIWeakReference* aWindow)
187 : {
188 0 : g_signal_connect(G_OBJECT(widget), "property_notify_event",
189 0 : G_CALLBACK(HandlePropertyChange), aWindow);
190 :
191 0 : gtk_widget_add_events(widget, GDK_PROPERTY_CHANGE_MASK);
192 :
193 0 : Window window = GDK_WINDOW_XWINDOW(widget->window);
194 0 : nsXRemoteService::HandleCommandsFor(window);
195 :
196 0 : }
197 :
198 : gboolean
199 0 : nsGTKRemoteService::HandlePropertyChange(GtkWidget *aWidget,
200 : GdkEventProperty *pevent,
201 : nsIWeakReference *aThis)
202 : {
203 0 : if (pevent->state == GDK_PROPERTY_NEW_VALUE) {
204 0 : Atom changedAtom = gdk_x11_atom_to_xatom(pevent->atom);
205 :
206 : return HandleNewProperty(GDK_WINDOW_XWINDOW(pevent->window),
207 : GDK_DISPLAY(),
208 0 : pevent->time, changedAtom, aThis);
209 : }
210 0 : return FALSE;
211 : }
212 :
213 :
214 : // {C0773E90-5799-4eff-AD03-3EBCD85624AC}
215 : #define NS_REMOTESERVICE_CID \
216 : { 0xc0773e90, 0x5799, 0x4eff, { 0xad, 0x3, 0x3e, 0xbc, 0xd8, 0x56, 0x24, 0xac } }
217 :
218 0 : NS_GENERIC_FACTORY_CONSTRUCTOR(nsGTKRemoteService)
219 : NS_DEFINE_NAMED_CID(NS_REMOTESERVICE_CID);
220 :
221 : static const mozilla::Module::CIDEntry kRemoteCIDs[] = {
222 : { &kNS_REMOTESERVICE_CID, false, NULL, nsGTKRemoteServiceConstructor },
223 : { NULL }
224 : };
225 :
226 : static const mozilla::Module::ContractIDEntry kRemoteContracts[] = {
227 : { "@mozilla.org/toolkit/remote-service;1", &kNS_REMOTESERVICE_CID },
228 : { NULL }
229 : };
230 :
231 : static const mozilla::Module kRemoteModule = {
232 : mozilla::Module::kVersion,
233 : kRemoteCIDs,
234 : kRemoteContracts
235 : };
236 :
237 : NSMODULE_DEFN(RemoteServiceModule) = &kRemoteModule;
|