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