diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index 050f266d3..391df724e 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -1,3 +1,4 @@ +"use strict"; /* ***** BEGIN LICENSE BLOCK ***** @@ -54,9 +55,9 @@ Zotero.Integration = new function() { // For Carbon and X11 var _carbon, ProcessSerialNumber, SetFrontProcessWithOptions, - _x11, _x11Display, XClientMessageEvent, XFetchName, XFree, XQueryTree, XOpenDisplay, - XCloseDisplay, XFlush, XDefaultRootWindow, XInternAtom, XSendEvent, XMapRaised, - XGetWindowProperty; + _x11, _x11Display, _x11RootWindow, XClientMessageEvent, XFetchName, XFree, XQueryTree, + XOpenDisplay, XCloseDisplay, XFlush, XDefaultRootWindow, XInternAtom, XSendEvent, + XMapRaised, XGetWindowProperty; var _inProgress = false; this.currentWindow = false; @@ -495,46 +496,57 @@ Zotero.Integration = new function() { Zotero.addShutdownListener(function() { XCloseDisplay(_x11Display); }); - } - - var rootWindow = XDefaultRootWindow(_x11Display), - intervalID; - - function _X11BringToForeground() { - var windowTitle = win.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIBaseWindow).title; - var x11Window = _X11FindWindow(_x11Display, rootWindow, windowTitle); - if(!x11Window) return; - win.clearInterval(intervalID); - - var event = new XClientMessageEvent(); - event.type = 33; /* ClientMessage*/ - event.serial = 0; - event.send_event = 1; - event.message_type = XInternAtom(_x11Display, "_NET_ACTIVE_WINDOW", 0); - event.display = _x11Display; - event.window = x11Window; - event.format = 32; - event.l0 = 2; - var mask = 1<<20 /* SubstructureRedirectMask */ | 1<<19 /* SubstructureNotifyMask */; - - if(XSendEvent(_x11Display, rootWindow, 0, mask, event.address())) { - XMapRaised(_x11Display, x11Window); - XFlush(_x11Display); - Zotero.debug("Activated successfully"); - } else { - Zotero.debug("Integration: An error occurred activating the window"); + _x11RootWindow = XDefaultRootWindow(_x11Display); + if(!_x11RootWindow) { + Zotero.debug("Integration: Could not get root window; not activating"); + _x11 = false; } } - + win.addEventListener("load", function() { - intervalID = win.setInterval(_X11BringToForeground, 50); + intervalID = win.setInterval(function() { + _X11BringToForeground(win, intervalID); + }, 50); }, false); } } - function _X11FindWindow(display, w, searchName) { + /** + * Bring a window to the foreground by interfacing directly with X11 + */ + function _X11BringToForeground(win, intervalID) { + var windowTitle = win.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIBaseWindow).title; + + var x11Window = _X11FindWindow(_x11RootWindow, windowTitle); + if(!x11Window) return; + win.clearInterval(intervalID); + + var event = new XClientMessageEvent(); + event.type = 33; /* ClientMessage*/ + event.serial = 0; + event.send_event = 1; + event.message_type = XInternAtom(_x11Display, "_NET_ACTIVE_WINDOW", 0); + event.display = _x11Display; + event.window = x11Window; + event.format = 32; + event.l0 = 2; + var mask = 1<<20 /* SubstructureRedirectMask */ | 1<<19 /* SubstructureNotifyMask */; + + if(XSendEvent(_x11Display, _x11RootWindow, 0, mask, event.address())) { + XMapRaised(_x11Display, x11Window); + XFlush(_x11Display); + Zotero.debug("Activated successfully"); + } else { + Zotero.debug("Integration: An error occurred activating the window"); + } + } + + /** + * Find an X11 window given a name + */ + function _X11FindWindow(w, searchName) { Components.utils.import("resource://gre/modules/ctypes.jsm"); var childrenPtr = new ctypes.unsigned_long.ptr(), @@ -542,14 +554,14 @@ Zotero.Integration = new function() { foundName = new ctypes.char.ptr(), nChildren = new ctypes.unsigned_int(); - if(XFetchName(display, w, foundName.address())) { + if(XFetchName(_x11Display, w, foundName.address())) { var foundNameString = foundName.readString(); XFree(foundName); if(foundNameString === searchName) return w; } var dummyPtr = dummy.address(); - if(!XQueryTree(display, w, dummyPtr, dummyPtr, childrenPtr.address(), + if(!XQueryTree(_x11Display, w, dummyPtr, dummyPtr, childrenPtr.address(), nChildren.address()) || childrenPtr.isNull()) { return false; } @@ -560,7 +572,7 @@ Zotero.Integration = new function() { for(var i=0; i - -