2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=2:tabstop=2:
|
|
|
|
*/
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This file is the Gtk2 implementation of plugin native window.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsPluginNativeWindow.h"
|
|
|
|
#include "npapi.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#include <gdk/gdk.h>
|
2009-02-25 07:25:12 -08:00
|
|
|
|
2012-06-27 17:15:32 -07:00
|
|
|
#include "gtk2compat.h"
|
2007-09-29 00:08:14 -07:00
|
|
|
#include "gtk2xtbin.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsPluginNativeWindowGtk2 : public nsPluginNativeWindow {
|
|
|
|
public:
|
|
|
|
nsPluginNativeWindowGtk2();
|
|
|
|
virtual ~nsPluginNativeWindowGtk2();
|
|
|
|
|
2011-05-17 18:48:34 -07:00
|
|
|
virtual nsresult CallSetWindow(nsRefPtr<nsNPAPIPluginInstance> &aPluginInstance);
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2007-09-29 00:08:14 -07:00
|
|
|
NPSetWindowCallbackStruct mWsInfo;
|
|
|
|
/**
|
|
|
|
* Either a GtkSocket or a special GtkXtBin widget (derived from GtkSocket)
|
|
|
|
* that encapsulates the Xt toolkit within a Gtk Application.
|
|
|
|
*/
|
|
|
|
GtkWidget* mSocketWidget;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult CreateXEmbedWindow();
|
2007-09-29 00:08:14 -07:00
|
|
|
nsresult CreateXtWindow();
|
2007-03-22 10:30:00 -07:00
|
|
|
void SetAllocation();
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean plug_removed_cb (GtkWidget *widget, gpointer data);
|
2010-02-15 13:40:46 -08:00
|
|
|
static void socket_unrealize_cb (GtkWidget *widget, gpointer data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsPluginNativeWindowGtk2::nsPluginNativeWindowGtk2() : nsPluginNativeWindow()
|
|
|
|
{
|
|
|
|
// initialize the struct fields
|
|
|
|
window = nsnull;
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
memset(&clipRect, 0, sizeof(clipRect));
|
2007-09-29 00:08:14 -07:00
|
|
|
ws_info = &mWsInfo;
|
2009-09-16 18:30:26 -07:00
|
|
|
type = NPWindowTypeWindow;
|
2007-09-29 00:08:14 -07:00
|
|
|
mSocketWidget = 0;
|
|
|
|
mWsInfo.type = 0;
|
|
|
|
mWsInfo.display = nsnull;
|
|
|
|
mWsInfo.visual = nsnull;
|
|
|
|
mWsInfo.colormap = 0;
|
|
|
|
mWsInfo.depth = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPluginNativeWindowGtk2::~nsPluginNativeWindowGtk2()
|
|
|
|
{
|
2007-09-29 00:08:14 -07:00
|
|
|
if(mSocketWidget) {
|
|
|
|
gtk_widget_destroy(mSocketWidget);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PLUG_NewPluginNativeWindow(nsPluginNativeWindow ** aPluginNativeWindow)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPluginNativeWindow);
|
|
|
|
*aPluginNativeWindow = new nsPluginNativeWindowGtk2();
|
|
|
|
return *aPluginNativeWindow ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PLUG_DeletePluginNativeWindow(nsPluginNativeWindow * aPluginNativeWindow)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPluginNativeWindow);
|
|
|
|
nsPluginNativeWindowGtk2 *p = (nsPluginNativeWindowGtk2 *)aPluginNativeWindow;
|
|
|
|
delete p;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-17 18:48:34 -07:00
|
|
|
nsresult nsPluginNativeWindowGtk2::CallSetWindow(nsRefPtr<nsNPAPIPluginInstance> &aPluginInstance)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-03-23 11:46:09 -07:00
|
|
|
if (aPluginInstance) {
|
2009-09-16 18:30:26 -07:00
|
|
|
if (type == NPWindowTypeWindow) {
|
2011-03-23 11:46:09 -07:00
|
|
|
if (!mSocketWidget) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-03-01 22:17:26 -08:00
|
|
|
// The documentation on the types for many variables in NP(N|P)_GetValue
|
|
|
|
// is vague. Often boolean values are NPBool (1 byte), but
|
|
|
|
// https://developer.mozilla.org/en/XEmbed_Extension_for_Mozilla_Plugins
|
|
|
|
// treats NPPVpluginNeedsXEmbed as PRBool (int), and
|
|
|
|
// on x86/32-bit, flash stores to this using |movl 0x1,&needsXEmbed|.
|
|
|
|
// thus we can't use NPBool for needsXEmbed, or the three bytes above
|
|
|
|
// it on the stack would get clobbered. so protect with the larger bool.
|
|
|
|
int needsXEmbed = 0;
|
|
|
|
rv = aPluginInstance->GetValueFromPlugin(NPPVpluginNeedsXEmbed, &needsXEmbed);
|
2011-03-23 11:46:09 -07:00
|
|
|
// If the call returned an error code make sure we still use our default value.
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-03-01 22:17:26 -08:00
|
|
|
needsXEmbed = 0;
|
2011-03-23 11:46:09 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
2012-03-01 22:17:26 -08:00
|
|
|
printf("nsPluginNativeWindowGtk2: NPPVpluginNeedsXEmbed=%d\n", needsXEmbed);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2011-03-23 11:46:09 -07:00
|
|
|
|
2012-03-01 22:17:26 -08:00
|
|
|
if (needsXEmbed) {
|
2009-07-21 01:13:38 -07:00
|
|
|
rv = CreateXEmbedWindow();
|
2007-09-29 00:08:14 -07:00
|
|
|
}
|
|
|
|
else {
|
2009-07-21 01:13:38 -07:00
|
|
|
rv = CreateXtWindow();
|
2007-09-29 00:08:14 -07:00
|
|
|
}
|
2011-03-23 11:46:09 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2009-07-21 01:13:38 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-03-23 11:46:09 -07:00
|
|
|
}
|
2007-07-02 20:29:47 -07:00
|
|
|
}
|
|
|
|
|
2011-03-23 11:46:09 -07:00
|
|
|
if (!mSocketWidget) {
|
2007-09-29 00:08:14 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-03-23 11:46:09 -07:00
|
|
|
}
|
2007-09-29 00:08:14 -07:00
|
|
|
|
|
|
|
// Make sure to resize and re-place the window if required.
|
|
|
|
// Need to reset "window" each time as nsObjectFrame::DidReflow sets it
|
|
|
|
// to the ancestor window.
|
2011-03-23 11:46:09 -07:00
|
|
|
if (GTK_IS_XTBIN(mSocketWidget)) {
|
2007-09-29 00:08:14 -07:00
|
|
|
gtk_xtbin_resize(mSocketWidget, width, height);
|
|
|
|
// Point the NPWindow structures window to the actual X window
|
2009-09-16 18:30:26 -07:00
|
|
|
window = (void*)GTK_XTBIN(mSocketWidget)->xtwindow;
|
2007-09-29 00:08:14 -07:00
|
|
|
}
|
|
|
|
else { // XEmbed
|
2007-07-02 20:29:47 -07:00
|
|
|
SetAllocation();
|
2009-09-16 18:30:26 -07:00
|
|
|
window = (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget));
|
2007-07-02 20:29:47 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
2007-07-02 20:29:47 -07:00
|
|
|
printf("nsPluginNativeWindowGtk2: call SetWindow with xid=%p\n", (void *)window);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2009-09-16 18:30:26 -07:00
|
|
|
} // NPWindowTypeWindow
|
2007-03-22 10:30:00 -07:00
|
|
|
aPluginInstance->SetWindow(this);
|
|
|
|
}
|
|
|
|
else if (mPluginInstance)
|
|
|
|
mPluginInstance->SetWindow(nsnull);
|
|
|
|
|
|
|
|
SetPluginInstance(aPluginInstance);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPluginNativeWindowGtk2::CreateXEmbedWindow() {
|
2007-09-29 00:08:14 -07:00
|
|
|
NS_ASSERTION(!mSocketWidget,"Already created a socket widget!");
|
2012-06-27 17:15:32 -07:00
|
|
|
GdkDisplay *display = gdk_display_get_default();
|
|
|
|
GdkWindow *parent_win = gdk_x11_window_lookup_for_display(display, (XID)window);
|
2007-09-29 00:08:14 -07:00
|
|
|
mSocketWidget = gtk_socket_new();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
//attach the socket to the container widget
|
|
|
|
gtk_widget_set_parent_window(mSocketWidget, parent_win);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
// Make sure to handle the plug_removed signal. If we don't the
|
|
|
|
// socket will automatically be destroyed when the plug is
|
|
|
|
// removed, which means we're destroying it more than once.
|
|
|
|
// SYNTAX ERROR.
|
|
|
|
g_signal_connect(mSocketWidget, "plug_removed",
|
|
|
|
G_CALLBACK(plug_removed_cb), NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-15 13:40:46 -08:00
|
|
|
g_signal_connect(mSocketWidget, "unrealize",
|
|
|
|
G_CALLBACK(socket_unrealize_cb), NULL);
|
|
|
|
|
2007-10-02 03:31:18 -07:00
|
|
|
g_signal_connect(mSocketWidget, "destroy",
|
2009-02-02 09:49:58 -08:00
|
|
|
G_CALLBACK(gtk_widget_destroyed), &mSocketWidget);
|
2007-10-02 03:07:17 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
gpointer user_data = NULL;
|
|
|
|
gdk_window_get_user_data(parent_win, &user_data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
GtkContainer *container = GTK_CONTAINER(user_data);
|
|
|
|
gtk_container_add(container, mSocketWidget);
|
|
|
|
gtk_widget_realize(mSocketWidget);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-13 19:38:31 -07:00
|
|
|
// The GtkSocket has a visible window, but the plugin's XEmbed plug will
|
|
|
|
// cover this window. Normally GtkSockets let the X server paint their
|
|
|
|
// background and this would happen immediately (before the plug is
|
|
|
|
// created). Setting the background to None prevents the server from
|
|
|
|
// painting this window, avoiding flicker.
|
2012-06-27 17:15:32 -07:00
|
|
|
// TODO GTK3
|
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
|
|
|
gdk_window_set_back_pixmap(gtk_widget_get_window(mSocketWidget), NULL, FALSE);
|
|
|
|
#endif
|
2009-10-13 19:38:31 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
// Resize before we show
|
|
|
|
SetAllocation();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
gtk_widget_show(mSocketWidget);
|
|
|
|
|
|
|
|
gdk_flush();
|
2009-09-16 18:30:26 -07:00
|
|
|
window = (void*)gtk_socket_get_id(GTK_SOCKET(mSocketWidget));
|
2007-09-29 00:08:14 -07:00
|
|
|
|
|
|
|
// Fill out the ws_info structure.
|
|
|
|
// (The windowless case is done in nsObjectFrame.cpp.)
|
|
|
|
GdkWindow *gdkWindow = gdk_window_lookup((XID)window);
|
2009-07-21 01:13:38 -07:00
|
|
|
if(!gdkWindow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
mWsInfo.display = GDK_WINDOW_XDISPLAY(gdkWindow);
|
2012-06-27 17:15:32 -07:00
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
2007-09-29 00:08:14 -07:00
|
|
|
mWsInfo.colormap = GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(gdkWindow));
|
|
|
|
GdkVisual* gdkVisual = gdk_drawable_get_visual(gdkWindow);
|
2012-06-21 17:56:10 -07:00
|
|
|
mWsInfo.depth = gdkVisual->depth;
|
2012-06-27 17:15:32 -07:00
|
|
|
#else
|
|
|
|
mWsInfo.colormap = None;
|
|
|
|
GdkVisual* gdkVisual = gdk_window_get_visual(gdkWindow);
|
|
|
|
mWsInfo.depth = gdk_visual_get_depth(gdkVisual);
|
|
|
|
#endif
|
|
|
|
mWsInfo.visual = GDK_VISUAL_XVISUAL(gdkVisual);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPluginNativeWindowGtk2::SetAllocation() {
|
2007-09-29 00:08:14 -07:00
|
|
|
if (!mSocketWidget)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
GtkAllocation new_allocation;
|
|
|
|
new_allocation.x = 0;
|
|
|
|
new_allocation.y = 0;
|
|
|
|
new_allocation.width = width;
|
|
|
|
new_allocation.height = height;
|
2007-09-29 00:08:14 -07:00
|
|
|
gtk_widget_size_allocate(mSocketWidget, &new_allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPluginNativeWindowGtk2::CreateXtWindow() {
|
|
|
|
NS_ASSERTION(!mSocketWidget,"Already created a socket widget!");
|
|
|
|
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-09-29 00:08:14 -07:00
|
|
|
printf("About to create new xtbin of %i X %i from %p...\n",
|
|
|
|
width, height, (void*)window);
|
|
|
|
#endif
|
2012-06-27 17:15:32 -07:00
|
|
|
GdkDisplay *display = gdk_display_get_default();
|
|
|
|
GdkWindow *gdkWindow = gdk_x11_window_lookup_for_display(display, (XID)window);
|
2007-09-29 00:08:14 -07:00
|
|
|
mSocketWidget = gtk_xtbin_new(gdkWindow, 0);
|
|
|
|
// Check to see if creating the xtbin failed for some reason.
|
|
|
|
// if it did, we can't go any further.
|
|
|
|
if (!mSocketWidget)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-04-05 18:25:28 -07:00
|
|
|
g_signal_connect(mSocketWidget, "destroy",
|
|
|
|
G_CALLBACK(gtk_widget_destroyed), &mSocketWidget);
|
|
|
|
|
2007-09-29 00:08:14 -07:00
|
|
|
gtk_widget_set_size_request(mSocketWidget, width, height);
|
|
|
|
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-09-29 00:08:14 -07:00
|
|
|
printf("About to show xtbin(%p)...\n", (void*)mSocketWidget); fflush(NULL);
|
|
|
|
#endif
|
|
|
|
gtk_widget_show(mSocketWidget);
|
2012-06-25 12:59:42 -07:00
|
|
|
#ifdef DEBUG
|
2007-09-29 00:08:14 -07:00
|
|
|
printf("completed gtk_widget_show(%p)\n", (void*)mSocketWidget); fflush(NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Fill out the ws_info structure.
|
|
|
|
GtkXtBin* xtbin = GTK_XTBIN(mSocketWidget);
|
|
|
|
// The xtbin has its own Display structure.
|
|
|
|
mWsInfo.display = xtbin->xtdisplay;
|
|
|
|
mWsInfo.colormap = xtbin->xtclient.xtcolormap;
|
|
|
|
mWsInfo.visual = xtbin->xtclient.xtvisual;
|
|
|
|
mWsInfo.depth = xtbin->xtclient.xtdepth;
|
|
|
|
// Leave mWsInfo.type = 0 - Who knows what this is meant to be?
|
|
|
|
|
|
|
|
XFlush(mWsInfo.display);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
gboolean
|
|
|
|
plug_removed_cb (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
|
|
|
// Gee, thanks for the info!
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-15 13:40:46 -08:00
|
|
|
static void
|
|
|
|
socket_unrealize_cb(GtkWidget *widget, gpointer data)
|
|
|
|
{
|
|
|
|
// Unmap and reparent any child windows that GDK does not yet know about.
|
|
|
|
// (See bug 540114 comment 10.)
|
2012-06-27 17:15:32 -07:00
|
|
|
GdkWindow* socket_window = gtk_widget_get_window(widget);
|
|
|
|
GdkDisplay* gdkDisplay = gdk_display_get_default();
|
|
|
|
Display* display = GDK_DISPLAY_XDISPLAY(gdkDisplay);
|
2010-02-15 13:40:46 -08:00
|
|
|
|
|
|
|
// Ignore X errors that may happen if windows get destroyed (possibly
|
|
|
|
// requested by the plugin) between XQueryTree and when we operate on them.
|
|
|
|
gdk_error_trap_push();
|
|
|
|
|
|
|
|
Window root, parent;
|
|
|
|
Window* children;
|
|
|
|
unsigned int nchildren;
|
2012-06-27 17:15:32 -07:00
|
|
|
if (!XQueryTree(display, gdk_x11_window_get_xid(socket_window),
|
2010-02-15 13:40:46 -08:00
|
|
|
&root, &parent, &children, &nchildren))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nchildren; ++i) {
|
|
|
|
Window child = children[i];
|
2012-06-27 17:15:32 -07:00
|
|
|
if (!gdk_x11_window_lookup_for_display(gdkDisplay, child)) {
|
2010-02-15 13:40:46 -08:00
|
|
|
// This window is not known to GDK.
|
|
|
|
XUnmapWindow(display, child);
|
|
|
|
XReparentWindow(display, child, DefaultRootWindow(display), 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-15 13:40:46 -08:00
|
|
|
if (children) XFree(children);
|
|
|
|
|
2010-02-17 14:05:24 -08:00
|
|
|
XSync(display, False);
|
2010-02-15 13:40:46 -08:00
|
|
|
gdk_error_trap_pop();
|
|
|
|
}
|