|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:expandtab:shiftwidth=2:tabstop=8: |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "nsGTKRemoteService.h" |
|
9 |
|
10 #include <gtk/gtk.h> |
|
11 #include <gdk/gdk.h> |
|
12 #include <gdk/gdkx.h> |
|
13 |
|
14 #include "nsIBaseWindow.h" |
|
15 #include "nsIDocShell.h" |
|
16 #include "nsPIDOMWindow.h" |
|
17 #include "mozilla/ModuleUtils.h" |
|
18 #include "nsIServiceManager.h" |
|
19 #include "nsIWeakReference.h" |
|
20 #include "nsIWidget.h" |
|
21 #include "nsIAppShellService.h" |
|
22 #include "nsAppShellCID.h" |
|
23 |
|
24 #include "nsCOMPtr.h" |
|
25 |
|
26 #include "nsGTKToolkit.h" |
|
27 |
|
28 NS_IMPL_ISUPPORTS(nsGTKRemoteService, |
|
29 nsIRemoteService, |
|
30 nsIObserver) |
|
31 |
|
32 NS_IMETHODIMP |
|
33 nsGTKRemoteService::Startup(const char* aAppName, const char* aProfileName) |
|
34 { |
|
35 NS_ASSERTION(aAppName, "Don't pass a null appname!"); |
|
36 sRemoteImplementation = this; |
|
37 |
|
38 if (mServerWindow) return NS_ERROR_ALREADY_INITIALIZED; |
|
39 |
|
40 XRemoteBaseStartup(aAppName, aProfileName); |
|
41 |
|
42 mServerWindow = gtk_invisible_new(); |
|
43 gtk_widget_realize(mServerWindow); |
|
44 HandleCommandsFor(mServerWindow, nullptr); |
|
45 |
|
46 mWindows.EnumerateRead(StartupHandler, this); |
|
47 |
|
48 return NS_OK; |
|
49 } |
|
50 |
|
51 PLDHashOperator |
|
52 nsGTKRemoteService::StartupHandler(GtkWidget* aKey, |
|
53 nsIWeakReference* aData, |
|
54 void* aClosure) |
|
55 { |
|
56 GtkWidget* widget = (GtkWidget*) aKey; |
|
57 nsGTKRemoteService* aThis = (nsGTKRemoteService*) aClosure; |
|
58 |
|
59 aThis->HandleCommandsFor(widget, aData); |
|
60 return PL_DHASH_NEXT; |
|
61 } |
|
62 |
|
63 static nsIWidget* GetMainWidget(nsIDOMWindow* aWindow) |
|
64 { |
|
65 // get the native window for this instance |
|
66 nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow)); |
|
67 NS_ENSURE_TRUE(window, nullptr); |
|
68 |
|
69 nsCOMPtr<nsIBaseWindow> baseWindow |
|
70 (do_QueryInterface(window->GetDocShell())); |
|
71 NS_ENSURE_TRUE(baseWindow, nullptr); |
|
72 |
|
73 nsCOMPtr<nsIWidget> mainWidget; |
|
74 baseWindow->GetMainWidget(getter_AddRefs(mainWidget)); |
|
75 return mainWidget; |
|
76 } |
|
77 |
|
78 NS_IMETHODIMP |
|
79 nsGTKRemoteService::RegisterWindow(nsIDOMWindow* aWindow) |
|
80 { |
|
81 nsIWidget* mainWidget = GetMainWidget(aWindow); |
|
82 NS_ENSURE_TRUE(mainWidget, NS_ERROR_FAILURE); |
|
83 |
|
84 GtkWidget* widget = |
|
85 (GtkWidget*) mainWidget->GetNativeData(NS_NATIVE_SHELLWIDGET); |
|
86 NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE); |
|
87 |
|
88 nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(aWindow); |
|
89 NS_ENSURE_TRUE(weak, NS_ERROR_FAILURE); |
|
90 |
|
91 mWindows.Put(widget, weak); |
|
92 |
|
93 // If Startup() has already been called, immediately register this window. |
|
94 if (mServerWindow) { |
|
95 HandleCommandsFor(widget, weak); |
|
96 } |
|
97 |
|
98 return NS_OK; |
|
99 } |
|
100 |
|
101 NS_IMETHODIMP |
|
102 nsGTKRemoteService::Shutdown() |
|
103 { |
|
104 if (!mServerWindow) |
|
105 return NS_ERROR_NOT_INITIALIZED; |
|
106 |
|
107 gtk_widget_destroy(mServerWindow); |
|
108 mServerWindow = nullptr; |
|
109 return NS_OK; |
|
110 } |
|
111 |
|
112 // Set desktop startup ID to the passed ID, if there is one, so that any created |
|
113 // windows get created with the right window manager metadata, and any windows |
|
114 // that get new tabs and are activated also get the right WM metadata. |
|
115 // The timestamp will be used if there is no desktop startup ID, or if we're |
|
116 // raising an existing window rather than showing a new window for the first time. |
|
117 void |
|
118 nsGTKRemoteService::SetDesktopStartupIDOrTimestamp(const nsACString& aDesktopStartupID, |
|
119 uint32_t aTimestamp) { |
|
120 nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit(); |
|
121 if (!toolkit) |
|
122 return; |
|
123 |
|
124 if (!aDesktopStartupID.IsEmpty()) { |
|
125 toolkit->SetDesktopStartupID(aDesktopStartupID); |
|
126 } |
|
127 |
|
128 toolkit->SetFocusTimestamp(aTimestamp); |
|
129 } |
|
130 |
|
131 |
|
132 void |
|
133 nsGTKRemoteService::HandleCommandsFor(GtkWidget* widget, |
|
134 nsIWeakReference* aWindow) |
|
135 { |
|
136 g_signal_connect(G_OBJECT(widget), "property_notify_event", |
|
137 G_CALLBACK(HandlePropertyChange), aWindow); |
|
138 |
|
139 gtk_widget_add_events(widget, GDK_PROPERTY_CHANGE_MASK); |
|
140 |
|
141 #if (MOZ_WIDGET_GTK == 2) |
|
142 Window window = GDK_WINDOW_XWINDOW(widget->window); |
|
143 #else |
|
144 Window window = gdk_x11_window_get_xid(gtk_widget_get_window(widget)); |
|
145 #endif |
|
146 nsXRemoteService::HandleCommandsFor(window); |
|
147 |
|
148 } |
|
149 |
|
150 gboolean |
|
151 nsGTKRemoteService::HandlePropertyChange(GtkWidget *aWidget, |
|
152 GdkEventProperty *pevent, |
|
153 nsIWeakReference *aThis) |
|
154 { |
|
155 if (pevent->state == GDK_PROPERTY_NEW_VALUE) { |
|
156 Atom changedAtom = gdk_x11_atom_to_xatom(pevent->atom); |
|
157 |
|
158 #if (MOZ_WIDGET_GTK == 2) |
|
159 XID window = GDK_WINDOW_XWINDOW(pevent->window); |
|
160 #else |
|
161 XID window = gdk_x11_window_get_xid(gtk_widget_get_window(aWidget)); |
|
162 #endif |
|
163 return HandleNewProperty(window, |
|
164 GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), |
|
165 pevent->time, changedAtom, aThis); |
|
166 } |
|
167 return FALSE; |
|
168 } |
|
169 |
|
170 |
|
171 // {C0773E90-5799-4eff-AD03-3EBCD85624AC} |
|
172 #define NS_REMOTESERVICE_CID \ |
|
173 { 0xc0773e90, 0x5799, 0x4eff, { 0xad, 0x3, 0x3e, 0xbc, 0xd8, 0x56, 0x24, 0xac } } |
|
174 |
|
175 NS_GENERIC_FACTORY_CONSTRUCTOR(nsGTKRemoteService) |
|
176 NS_DEFINE_NAMED_CID(NS_REMOTESERVICE_CID); |
|
177 |
|
178 static const mozilla::Module::CIDEntry kRemoteCIDs[] = { |
|
179 { &kNS_REMOTESERVICE_CID, false, nullptr, nsGTKRemoteServiceConstructor }, |
|
180 { nullptr } |
|
181 }; |
|
182 |
|
183 static const mozilla::Module::ContractIDEntry kRemoteContracts[] = { |
|
184 { "@mozilla.org/toolkit/remote-service;1", &kNS_REMOTESERVICE_CID }, |
|
185 { nullptr } |
|
186 }; |
|
187 |
|
188 static const mozilla::Module kRemoteModule = { |
|
189 mozilla::Module::kVersion, |
|
190 kRemoteCIDs, |
|
191 kRemoteContracts |
|
192 }; |
|
193 |
|
194 NSMODULE_DEFN(RemoteServiceModule) = &kRemoteModule; |