1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
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
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.
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 ***** */
42 * This file is the Gtk2 implementation of plugin native window.
46 #include "nsPluginNativeWindow.h"
52 #if (MOZ_PLATFORM_MAEMO == 5)
53 #define MOZ_COMPOSITED_PLUGINS
56 #ifdef MOZ_COMPOSITED_PLUGINS
58 #include <X11/extensions/Xdamage.h>
59 #include <X11/extensions/Xcomposite.h>
63 #include "gtk2xtbin.h"
65 class nsPluginNativeWindowGtk2
: public nsPluginNativeWindow
{
67 nsPluginNativeWindowGtk2();
68 virtual ~nsPluginNativeWindowGtk2();
70 virtual nsresult
CallSetWindow(nsCOMPtr
<nsIPluginInstance
> &aPluginInstance
);
72 NPSetWindowCallbackStruct mWsInfo
;
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();
81 PRBool
CanGetValueFromPlugin(nsCOMPtr
<nsIPluginInstance
> &aPluginInstance
);
82 #ifdef MOZ_COMPOSITED_PLUGINS
83 nsresult
CreateXCompositedWindow();
84 static GdkFilterReturn
plugin_composite_filter_func (GdkXEvent
*xevent
,
89 GtkWidget
* mParentWindow
;
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
104 memset(&clipRect
, 0, sizeof(clipRect
));
106 type
= NPWindowTypeWindow
;
109 mWsInfo
.display
= nsnull
;
110 mWsInfo
.visual
= nsnull
;
111 mWsInfo
.colormap
= 0;
113 #ifdef MOZ_COMPOSITED_PLUGINS
119 nsPluginNativeWindowGtk2::~nsPluginNativeWindowGtk2()
122 gtk_widget_destroy(mSocketWidget
);
125 #ifdef MOZ_COMPOSITED_PLUGINS
127 gtk_widget_destroy(mParentWindow
);
131 gdk_window_remove_filter (nsnull
, plugin_composite_filter_func
, this);
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
;
151 #ifdef MOZ_COMPOSITED_PLUGINS
152 /* the base xdamage event number.*/
153 static int xdamage_event_base
;
156 nsPluginNativeWindowGtk2::plugin_composite_filter_func (GdkXEvent
*xevent
,
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 */
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
;
183 nsresult
nsPluginNativeWindowGtk2::CallSetWindow(nsCOMPtr
<nsIPluginInstance
> &aPluginInstance
)
185 if(aPluginInstance
) {
186 if (type
== NPWindowTypeWindow
) {
189 PRBool needXEmbed
= PR_FALSE
;
190 if (CanGetValueFromPlugin(aPluginInstance
)) {
191 rv
= aPluginInstance
->GetValueFromPlugin(NPPVpluginNeedsXEmbed
, &needXEmbed
);
193 printf("nsPluginNativeWindowGtk2: NPPVpluginNeedsXEmbed=%d\n", needXEmbed
);
198 #ifdef MOZ_COMPOSITED_PLUGINS
199 rv
= CreateXCompositedWindow();
201 rv
= CreateXEmbedWindow();
205 rv
= CreateXtWindow();
208 return NS_ERROR_FAILURE
;
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
;
224 window
= (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget
));
227 printf("nsPluginNativeWindowGtk2: call SetWindow with xid=%p\n", (void *)window
);
229 } // NPWindowTypeWindow
230 aPluginInstance
->SetWindow(this);
232 else if (mPluginInstance
)
233 mPluginInstance
->SetWindow(nsnull
);
235 SetPluginInstance(aPluginInstance
);
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.
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
278 gtk_widget_show(mSocketWidget
);
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
);
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
;
298 #ifdef MOZ_COMPOSITED_PLUGINS
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.
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
329 gtk_widget_set_size_request (mSocketWidget
, width
, height
);
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
);
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);
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);
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(),
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
;
386 void nsPluginNativeWindowGtk2::SetAllocation() {
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!");
402 printf("About to create new xtbin of %i X %i from %p...\n",
403 width
, height
, (void*)window
);
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.
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
);
418 printf("About to show xtbin(%p)...\n", (void*)mSocketWidget
); fflush(NULL
);
420 gtk_widget_show(mSocketWidget
);
422 printf("completed gtk_widget_show(%p)\n", (void*)mSocketWidget
); fflush(NULL
);
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
);
439 PRBool
nsPluginNativeWindowGtk2::CanGetValueFromPlugin(nsCOMPtr
<nsIPluginInstance
> &aPluginInstance
)
446 plug_removed_cb (GtkWidget
*widget
, gpointer data
)
448 // Gee, thanks for the info!
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();
466 unsigned int nchildren
;
467 if (!XQueryTree(display
, gdk_x11_drawable_get_xid(socket_window
),
468 &root
, &parent
, &children
, &nchildren
))
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();