[JAEGER] Merge from tracemonkey.
[mozilla-central.git] / modules / plugin / base / src / nsPluginNativeWindowGtk2.cpp
blob31e8f721c3c0fe2f77af67005941f81e3d2b01bb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is
20 * Sun Microsystems, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 2003
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Robin Lu <robin.lu@sun.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 /**
42 * This file is the Gtk2 implementation of plugin native window.
45 #include "nsDebug.h"
46 #include "nsPluginNativeWindow.h"
47 #include "npapi.h"
48 #include <gtk/gtk.h>
49 #include <gdk/gdkx.h>
50 #include <gdk/gdk.h>
52 #if (MOZ_PLATFORM_MAEMO == 5)
53 #define MOZ_COMPOSITED_PLUGINS
54 #endif
56 #ifdef MOZ_COMPOSITED_PLUGINS
57 extern "C" {
58 #include <X11/extensions/Xdamage.h>
59 #include <X11/extensions/Xcomposite.h>
61 #endif
63 #include "gtk2xtbin.h"
65 class nsPluginNativeWindowGtk2 : public nsPluginNativeWindow {
66 public:
67 nsPluginNativeWindowGtk2();
68 virtual ~nsPluginNativeWindowGtk2();
70 virtual nsresult CallSetWindow(nsCOMPtr<nsIPluginInstance> &aPluginInstance);
71 private:
72 NPSetWindowCallbackStruct mWsInfo;
73 /**
74 * Either a GtkSocket or a special GtkXtBin widget (derived from GtkSocket)
75 * that encapsulates the Xt toolkit within a Gtk Application.
77 GtkWidget* mSocketWidget;
78 nsresult CreateXEmbedWindow();
79 nsresult CreateXtWindow();
80 void SetAllocation();
81 PRBool CanGetValueFromPlugin(nsCOMPtr<nsIPluginInstance> &aPluginInstance);
82 #ifdef MOZ_COMPOSITED_PLUGINS
83 nsresult CreateXCompositedWindow();
84 static GdkFilterReturn plugin_composite_filter_func (GdkXEvent *xevent,
85 GdkEvent *event,
86 gpointer data);
88 Damage mDamage;
89 GtkWidget* mParentWindow;
90 #endif
93 static gboolean plug_removed_cb (GtkWidget *widget, gpointer data);
94 static void socket_unrealize_cb (GtkWidget *widget, gpointer data);
96 nsPluginNativeWindowGtk2::nsPluginNativeWindowGtk2() : nsPluginNativeWindow()
98 // initialize the struct fields
99 window = nsnull;
100 x = 0;
101 y = 0;
102 width = 0;
103 height = 0;
104 memset(&clipRect, 0, sizeof(clipRect));
105 ws_info = &mWsInfo;
106 type = NPWindowTypeWindow;
107 mSocketWidget = 0;
108 mWsInfo.type = 0;
109 mWsInfo.display = nsnull;
110 mWsInfo.visual = nsnull;
111 mWsInfo.colormap = 0;
112 mWsInfo.depth = 0;
113 #ifdef MOZ_COMPOSITED_PLUGINS
114 mDamage = 0;
115 mParentWindow = 0;
116 #endif
119 nsPluginNativeWindowGtk2::~nsPluginNativeWindowGtk2()
121 if(mSocketWidget) {
122 gtk_widget_destroy(mSocketWidget);
125 #ifdef MOZ_COMPOSITED_PLUGINS
126 if (mParentWindow) {
127 gtk_widget_destroy(mParentWindow);
130 if (mDamage) {
131 gdk_window_remove_filter (nsnull, plugin_composite_filter_func, this);
133 #endif
136 nsresult PLUG_NewPluginNativeWindow(nsPluginNativeWindow ** aPluginNativeWindow)
138 NS_ENSURE_ARG_POINTER(aPluginNativeWindow);
139 *aPluginNativeWindow = new nsPluginNativeWindowGtk2();
140 return *aPluginNativeWindow ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
143 nsresult PLUG_DeletePluginNativeWindow(nsPluginNativeWindow * aPluginNativeWindow)
145 NS_ENSURE_ARG_POINTER(aPluginNativeWindow);
146 nsPluginNativeWindowGtk2 *p = (nsPluginNativeWindowGtk2 *)aPluginNativeWindow;
147 delete p;
148 return NS_OK;
151 #ifdef MOZ_COMPOSITED_PLUGINS
152 /* the base xdamage event number.*/
153 static int xdamage_event_base;
155 GdkFilterReturn
156 nsPluginNativeWindowGtk2::plugin_composite_filter_func (GdkXEvent *xevent,
157 GdkEvent *event,
158 gpointer data)
160 nsPluginNativeWindowGtk2 *native_window = (nsPluginNativeWindowGtk2*)data;
161 XDamageNotifyEvent *ev;
162 ev = (XDamageNotifyEvent *) xevent;
163 if (ev->type != xdamage_event_base + XDamageNotify)
164 return GDK_FILTER_CONTINUE;
166 //printf("Damage event %d %d %d %d\n",ev->area.x, ev->area.y, ev->area.width, ev->area.height);
167 XDamageSubtract (GDK_DISPLAY(), native_window->mDamage, None, None);
169 /* We try to do our area invalidation here */
170 NPRect rect;
171 rect.top = ev->area.x;
172 rect.left = ev->area.y;
173 rect.right = ev->area.x + ev->area.width;
174 rect.bottom = ev->area.y + ev->area.height;
176 if (native_window->mPluginInstance)
177 native_window->mPluginInstance->InvalidateRect(&rect);
179 return GDK_FILTER_REMOVE;
181 #endif
183 nsresult nsPluginNativeWindowGtk2::CallSetWindow(nsCOMPtr<nsIPluginInstance> &aPluginInstance)
185 if(aPluginInstance) {
186 if (type == NPWindowTypeWindow) {
187 nsresult rv;
188 if(!mSocketWidget) {
189 PRBool needXEmbed = PR_FALSE;
190 if (CanGetValueFromPlugin(aPluginInstance)) {
191 rv = aPluginInstance->GetValueFromPlugin(NPPVpluginNeedsXEmbed, &needXEmbed);
192 #ifdef DEBUG
193 printf("nsPluginNativeWindowGtk2: NPPVpluginNeedsXEmbed=%d\n", needXEmbed);
194 #endif
196 nsresult rv;
197 if(needXEmbed) {
198 #ifdef MOZ_COMPOSITED_PLUGINS
199 rv = CreateXCompositedWindow();
200 #else
201 rv = CreateXEmbedWindow();
202 #endif
204 else {
205 rv = CreateXtWindow();
207 if(NS_FAILED(rv))
208 return NS_ERROR_FAILURE;
211 if(!mSocketWidget)
212 return NS_ERROR_FAILURE;
214 // Make sure to resize and re-place the window if required.
215 // Need to reset "window" each time as nsObjectFrame::DidReflow sets it
216 // to the ancestor window.
217 if(GTK_IS_XTBIN(mSocketWidget)) {
218 gtk_xtbin_resize(mSocketWidget, width, height);
219 // Point the NPWindow structures window to the actual X window
220 window = (void*)GTK_XTBIN(mSocketWidget)->xtwindow;
222 else { // XEmbed
223 SetAllocation();
224 window = (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget));
226 #ifdef DEBUG
227 printf("nsPluginNativeWindowGtk2: call SetWindow with xid=%p\n", (void *)window);
228 #endif
229 } // NPWindowTypeWindow
230 aPluginInstance->SetWindow(this);
232 else if (mPluginInstance)
233 mPluginInstance->SetWindow(nsnull);
235 SetPluginInstance(aPluginInstance);
236 return NS_OK;
239 nsresult nsPluginNativeWindowGtk2::CreateXEmbedWindow() {
240 NS_ASSERTION(!mSocketWidget,"Already created a socket widget!");
242 GdkWindow *parent_win = gdk_window_lookup((XID)window);
243 mSocketWidget = gtk_socket_new();
245 //attach the socket to the container widget
246 gtk_widget_set_parent_window(mSocketWidget, parent_win);
248 // Make sure to handle the plug_removed signal. If we don't the
249 // socket will automatically be destroyed when the plug is
250 // removed, which means we're destroying it more than once.
251 // SYNTAX ERROR.
252 g_signal_connect(mSocketWidget, "plug_removed",
253 G_CALLBACK(plug_removed_cb), NULL);
255 g_signal_connect(mSocketWidget, "unrealize",
256 G_CALLBACK(socket_unrealize_cb), NULL);
258 g_signal_connect(mSocketWidget, "destroy",
259 G_CALLBACK(gtk_widget_destroyed), &mSocketWidget);
261 gpointer user_data = NULL;
262 gdk_window_get_user_data(parent_win, &user_data);
264 GtkContainer *container = GTK_CONTAINER(user_data);
265 gtk_container_add(container, mSocketWidget);
266 gtk_widget_realize(mSocketWidget);
268 // The GtkSocket has a visible window, but the plugin's XEmbed plug will
269 // cover this window. Normally GtkSockets let the X server paint their
270 // background and this would happen immediately (before the plug is
271 // created). Setting the background to None prevents the server from
272 // painting this window, avoiding flicker.
273 gdk_window_set_back_pixmap(mSocketWidget->window, NULL, FALSE);
275 // Resize before we show
276 SetAllocation();
278 gtk_widget_show(mSocketWidget);
280 gdk_flush();
281 window = (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget));
283 // Fill out the ws_info structure.
284 // (The windowless case is done in nsObjectFrame.cpp.)
285 GdkWindow *gdkWindow = gdk_window_lookup((XID)window);
286 if(!gdkWindow)
287 return NS_ERROR_FAILURE;
289 mWsInfo.display = GDK_WINDOW_XDISPLAY(gdkWindow);
290 mWsInfo.colormap = GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(gdkWindow));
291 GdkVisual* gdkVisual = gdk_drawable_get_visual(gdkWindow);
292 mWsInfo.visual = GDK_VISUAL_XVISUAL(gdkVisual);
293 mWsInfo.depth = gdkVisual->depth;
295 return NS_OK;
298 #ifdef MOZ_COMPOSITED_PLUGINS
299 #include <dlfcn.h>
300 nsresult nsPluginNativeWindowGtk2::CreateXCompositedWindow() {
301 NS_ASSERTION(!mSocketWidget,"Already created a socket widget!");
303 mParentWindow = gtk_window_new(GTK_WINDOW_POPUP);
304 mSocketWidget = gtk_socket_new();
305 GdkWindow *parent_win = mParentWindow->window;
307 //attach the socket to the container widget
308 gtk_widget_set_parent_window(mSocketWidget, parent_win);
310 // Make sure to handle the plug_removed signal. If we don't the
311 // socket will automatically be destroyed when the plug is
312 // removed, which means we're destroying it more than once.
313 // SYNTAX ERROR.
314 g_signal_connect(mSocketWidget, "plug_removed",
315 G_CALLBACK(plug_removed_cb), NULL);
317 g_signal_connect(mSocketWidget, "destroy",
318 G_CALLBACK(gtk_widget_destroyed), &mSocketWidget);
320 /*gpointer user_data = NULL;
321 gdk_window_get_user_data(parent_win, &user_data);
323 GtkContainer *container = GTK_CONTAINER(mParentWindow);
324 gtk_container_add(container, mSocketWidget);
325 gtk_widget_realize(mSocketWidget);
327 // Resize before we show
328 SetAllocation();
329 gtk_widget_set_size_request (mSocketWidget, width, height);
330 /* move offscreen */
331 gtk_window_move (GTK_WINDOW(mParentWindow), width+1000, height+1000);
334 gtk_widget_show(mSocketWidget);
335 gtk_widget_show_all(mParentWindow);
337 /* store away a reference to the socketwidget */
338 mPlugWindow = (mSocketWidget);
340 gdk_flush();
341 window = (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget));
343 /* This is useful if we still have the plugin window inline
344 * i.e. firefox vs. fennec */
345 // gdk_window_set_composited(mSocketWidget->window, TRUE);
347 if (!mDamage) {
348 /* we install a general handler instead of one specific to a particular window
349 * because we don't have a GdkWindow for the plugin window */
350 gdk_window_add_filter (parent_win, plugin_composite_filter_func, this);
352 int junk;
353 if (!XDamageQueryExtension (GDK_DISPLAY (), &xdamage_event_base, &junk))
354 printf ("This requires the XDamage extension");
356 mDamage = XDamageCreate(GDK_DISPLAY(), (Drawable)window, XDamageReportNonEmpty);
357 XCompositeRedirectWindow (GDK_DISPLAY(),
358 (Drawable)window,
359 CompositeRedirectManual);
361 /* this is a hack to avoid having flash causing a crash when it is unloaded.
362 * libplayback sets up dbus_connection_filters. When flash is unloaded it takes
363 * libplayback with it, however the connection filters are not removed
364 * which causes a crash when dbus tries to execute them. dlopening libplayback
365 * ensures that those functions stay around even after flash is gone. */
366 static void *libplayback_handle;
367 if (!libplayback_handle) {
368 libplayback_handle = dlopen("libplayback-1.so.0", RTLD_NOW);
373 // Fill out the ws_info structure.
374 // (The windowless case is done in nsObjectFrame.cpp.)
375 GdkWindow *gdkWindow = gdk_window_lookup((XID)window);
376 mWsInfo.display = GDK_WINDOW_XDISPLAY(gdkWindow);
377 mWsInfo.colormap = GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(gdkWindow));
378 GdkVisual* gdkVisual = gdk_drawable_get_visual(gdkWindow);
379 mWsInfo.visual = GDK_VISUAL_XVISUAL(gdkVisual);
380 mWsInfo.depth = gdkVisual->depth;
382 return NS_OK;
384 #endif
386 void nsPluginNativeWindowGtk2::SetAllocation() {
387 if (!mSocketWidget)
388 return;
390 GtkAllocation new_allocation;
391 new_allocation.x = 0;
392 new_allocation.y = 0;
393 new_allocation.width = width;
394 new_allocation.height = height;
395 gtk_widget_size_allocate(mSocketWidget, &new_allocation);
398 nsresult nsPluginNativeWindowGtk2::CreateXtWindow() {
399 NS_ASSERTION(!mSocketWidget,"Already created a socket widget!");
401 #ifdef NS_DEBUG
402 printf("About to create new xtbin of %i X %i from %p...\n",
403 width, height, (void*)window);
404 #endif
405 GdkWindow *gdkWindow = gdk_window_lookup((XID)window);
406 mSocketWidget = gtk_xtbin_new(gdkWindow, 0);
407 // Check to see if creating the xtbin failed for some reason.
408 // if it did, we can't go any further.
409 if (!mSocketWidget)
410 return NS_ERROR_FAILURE;
412 g_signal_connect(mSocketWidget, "destroy",
413 G_CALLBACK(gtk_widget_destroyed), &mSocketWidget);
415 gtk_widget_set_size_request(mSocketWidget, width, height);
417 #ifdef NS_DEBUG
418 printf("About to show xtbin(%p)...\n", (void*)mSocketWidget); fflush(NULL);
419 #endif
420 gtk_widget_show(mSocketWidget);
421 #ifdef NS_DEBUG
422 printf("completed gtk_widget_show(%p)\n", (void*)mSocketWidget); fflush(NULL);
423 #endif
425 // Fill out the ws_info structure.
426 GtkXtBin* xtbin = GTK_XTBIN(mSocketWidget);
427 // The xtbin has its own Display structure.
428 mWsInfo.display = xtbin->xtdisplay;
429 mWsInfo.colormap = xtbin->xtclient.xtcolormap;
430 mWsInfo.visual = xtbin->xtclient.xtvisual;
431 mWsInfo.depth = xtbin->xtclient.xtdepth;
432 // Leave mWsInfo.type = 0 - Who knows what this is meant to be?
434 XFlush(mWsInfo.display);
436 return NS_OK;
439 PRBool nsPluginNativeWindowGtk2::CanGetValueFromPlugin(nsCOMPtr<nsIPluginInstance> &aPluginInstance)
441 return PR_TRUE;
444 /* static */
445 gboolean
446 plug_removed_cb (GtkWidget *widget, gpointer data)
448 // Gee, thanks for the info!
449 return TRUE;
452 static void
453 socket_unrealize_cb(GtkWidget *widget, gpointer data)
455 // Unmap and reparent any child windows that GDK does not yet know about.
456 // (See bug 540114 comment 10.)
457 GdkWindow* socket_window = widget->window;
458 Display* display = GDK_DISPLAY();
460 // Ignore X errors that may happen if windows get destroyed (possibly
461 // requested by the plugin) between XQueryTree and when we operate on them.
462 gdk_error_trap_push();
464 Window root, parent;
465 Window* children;
466 unsigned int nchildren;
467 if (!XQueryTree(display, gdk_x11_drawable_get_xid(socket_window),
468 &root, &parent, &children, &nchildren))
469 return;
471 for (unsigned int i = 0; i < nchildren; ++i) {
472 Window child = children[i];
473 if (!gdk_window_lookup(child)) {
474 // This window is not known to GDK.
475 XUnmapWindow(display, child);
476 XReparentWindow(display, child, DefaultRootWindow(display), 0, 0);
480 if (children) XFree(children);
482 XSync(display, False);
483 gdk_error_trap_pop();