* add LibC for logging to stderr

* try different approaches for hiding windows:
  - gather toplevel windows from nsIWindowMediator, but then unable to get
    NativeParentWindow (GdkWindow) in order to apply gdk_window_hide - damn it !
  - gather toplevel GdkWindows, but don't know exactly how to find out which
    belong to the application...
  - gather toplevel GtkWindows, but seem to get too many windows from
    gtk_window_list_toplevels()...
* problems understanding js-ctypes:
  - arguments passed to callbacks (see FunctionType), for ex: implementation of
    GFunc
  - definition of recursive structures, for ex: GtkWidget
This commit is contained in:
foudfou 2011-07-18 00:58:03 +02:00
parent 1b68487af8
commit f2245d1ca0
7 changed files with 503 additions and 21 deletions

View File

@ -1,24 +1,130 @@
/* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// TODO: Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://moztray/commons.js");
Components.utils.import("resource://moztray/LibGtkStatusIcon.js");
Components.utils.import("resource://gre/modules/ctypes.jsm");
Components.utils.import("resource://moztray/LibC.js");
Components.utils.import("resource://moztray/LibGObject.js");
Components.utils.import("resource://moztray/LibGdkWindow.js");
Components.utils.import("resource://moztray/LibGtkStatusIcon.js");
Components.utils.import("resource://moztray/commons.js");
const MOZT_ICON_DIR = "chrome/skin/";
const MOZT_ICON_SUFFIX = "32.png";
var mozt_hideWinCb;
/* NOTE: arguments come obviously from the GCallbackFunction definition:
* [gpointer, guint, gpointer]
*/
var mozt_hideWinJs = function(aInstance, aTimestamp, aUserData) {
var mozt_getBaseWindow = function(win) {
var bw;
try {
alert("Hide");
} catch(e) {Cu.reportError(ex);}
bw = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIXULWindow)
.docShell
.QueryInterface(Components.interfaces.nsIBaseWindow);
} catch (ex) {
bw = null;
setTimeout(function() {throw ex; }, 0);
// ignore no-interface exception
}
return bw;
};
var mozt_getAllWindows = function() {
try {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
} catch (err) {
alert(err);
return;
}
var baseWindows = new Array();
var e = wm.getEnumerator(null);
while (e.hasMoreElements()) {
var w = e.getNext();
baseWindows[baseWindows.length] = mozt_getBaseWindow(w);
}
return baseWindows;
};
var mozt_hideToTray = function() {
mozt.Debug.debug("mozt_hideToTray");
/*
var toto = gBrowser.getBrowserForDocument(content.document)
.docShell
.QueryInterface(Components.interfaces.nsIBaseWindow)
.parentNativeWindow;
mozt.Debug.debug("toto: " + toto);
*/
var baseWindows = mozt_getAllWindows();
mozt.Debug.dump("baseWindows: " + baseWindows.length);
for(var i=0; i<baseWindows.length; i++) {
var bw = baseWindows[i];
// bw.visibility = false;
// mozt.Debug.dumpObj(bw);
if (bw instanceof Ci.nsIBaseWindow) {
mozt.Debug.debug("bw.visibility: " + bw.visibility);
mozt.Debug.debug("bw.title: " + bw.title);
mozt.Debug.debug("bw.parentNativeWindow: " + bw.parentNativeWindow);
// try {
// bw.visibility = false;
// // bw.parentNativeWindow = null;
// } catch (x) {
// mozt.Debug.debug(x);
// }
}
// var parentWin = bw.parentNativeWindow;
// var gdkWin = new LibGdkWindow.GdkWindow.ptr;
// try {
// // gdkWin = ctypes.cast(tmp, LibGdkWindow.GdkWindow.ptr);
// } catch (x) {
// mozt.Debug.debug(x);
// }
// if (!gdkWin) mozt.Debug.debug("gdkWin undefined");
// mozt.Debug.dumpObj(gdkWin);
// LibGdkWindow.GdkWindowHide(gdkWin);
}
}
var mozt_trayCb;
var mozt_isHidden = false;
var mozt_trayCbJS = function() {
if (mozt_isHidden) {
mozt_isHidden = false;
mozt_restoreFromTray();
} else {
mozt_isHidden = true;
mozt_hideToTray();
}
};
var mozt_func;
var mozt_funcGdkJS = function(a1, a2, a3) {
try {
mozt.Debug.debug("GDK Window");
mozt.Debug.debug(a1);
// mozt.Debug.debug(a2);
// mozt.Debug.debug(a3);
} catch(e) {mozt.Debug.debug(ex);}
};
var mozt_funcGtkJS = function(win) {
try {
ctypes.cast(win, LibGtkStatusIcon.GtkWidget.ptr);
mozt.Debug.debug("GTK Window " + win);
LibGtkStatusIcon.gtk_widget_hide(win);
} catch(e) {mozt.Debug.debug(ex);}
};
mozt.Main = {
onLoad: function() {
@ -46,13 +152,38 @@ mozt.Main = {
// "Example Tray Icon");
// gtk_status_icon_set_visible(tray_icon, TRUE);
mozt_hideWinCb = LibGObject.GCallbackFunction(mozt_hideWinJs);
mozt_trayCb = LibGObject.GCallbackFunction(mozt_trayCbJS);
LibGObject.g_signal_connect(this.tray_icon, "activate",
mozt_hideWinCb, null);
mozt_trayCb, null);
try {
// Experimental stuff...
mozt.Debug.dump('Moztray LOADED !');
var gdkScreen = LibGdkWindow.GdkScreenGetDefault();
var tl = LibGdkWindow.GdkScreenGetToplevelWindows(gdkScreen);
mozt.Debug.debug(tl);
// gboolean gdk_window_is_visible (GdkWindow *window);
mozt_func = LibGObject.GFunc_t(mozt_funcGdkJS);
LibGObject.g_list_foreach(tl, mozt_func, null);
var gdkWinCount = LibGObject.g_list_length(tl);
mozt.Debug.debug('gdkWinCount: ' + gdkWinCount);
var pid = LibC.getpid();
mozt.Debug.debug(pid);
tl = LibGtkStatusIcon.gtk_window_list_toplevels();
mozt_func = LibGObject.GFunc_t(mozt_funcGtkJS);
LibGObject.g_list_foreach(tl, mozt_func, null);
var gtkWinCount = LibGObject.g_list_length(tl);
mozt.Debug.debug('gtkWinCount: ' + gtkWinCount);
} catch (x) {
mozt.Debug.debug(x);
}
mozt.Debug.debug('Moztray LOADED !');
this.initialized = true;
return true;
},

View File

@ -2,7 +2,7 @@
<?xml-stylesheet href="chrome://moztray/skin/overlay.css" type="text/css"?>
<!DOCTYPE overlay SYSTEM "chrome://moztray/locale/overlay.dtd">
<overlay id="moztray-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="overlay.js"/>
<script type="application/javascript;version=1.7" src="overlay.js"/>
<stringbundleset id="stringbundleset">
<stringbundle id="moztray-strings" src="chrome://moztray/locale/overlay.properties"/>

View File

@ -23,6 +23,10 @@ XPCOMUtils.defineLazyGetter(this, "FILE", function() {
return ctypes.StructType("FILE");
});
XPCOMUtils.defineLazyGetter(this, "pid_t", function() {
return ctypes.int;
});
XPCOMUtils.defineLazyGetter(this, "fdopen", function() {
var fdopen = libc.declare(
"fdopen", ctypes.default_abi, FILE.ptr,
@ -74,12 +78,27 @@ XPCOMUtils.defineLazyGetter(this, "fflush", function() {
return fflush;
});
// pid_t getpid(void);
XPCOMUtils.defineLazyGetter(this, "getpid", function() {
var getpid = libc.declare(
"getpid", ctypes.default_abi, pid_t
);
if (!getpid)
throw "getpid is unavailable";
return getpid;
});
var LibC = {
stderr: this.fdopen(2, "a"),
FILE: FILE,
pid_t: pid_t,
fdopen: fdopen,
puts: puts,
fputs: fputs,
fflush: fflush,
getpid: getpid,
}

View File

@ -56,11 +56,11 @@ XPCOMUtils.defineLazyGetter(this, "libgobject", function() {
});
XPCOMUtils.defineLazyGetter(this, "GCallback", function() {
return ctypes.voidptr_t;
return ctypes.void_t.ptr;
});
XPCOMUtils.defineLazyGetter(this, "gpointer", function() {
return ctypes.voidptr_t;
return ctypes.void_t.ptr;
});
XPCOMUtils.defineLazyGetter(this, "gulong", function() {
@ -139,6 +139,69 @@ XPCOMUtils.defineLazyGetter(this, "g_object_unref", function() {
return g_object_unref;
});
XPCOMUtils.defineLazyGetter(this, "GFunc", function() {
return ctypes.void_t.ptr;
});
// intended for g_list_foreach.
/* NOTE: if we needed more/different args, we'd need to implement another
FunctionType */
XPCOMUtils.defineLazyGetter(this, "GFunc_t", function() {
var GFunc_t = ctypes.FunctionType(
ctypes.default_abi, ctypes.void_t,
[gpointer]
).ptr;
if (!GFunc_t)
throw "GFunc_t is unavailable";
return GFunc_t;
});
XPCOMUtils.defineLazyGetter(this, "GList", function() {
return ctypes.StructType("GList");
});
// void g_list_free (GList *list);
XPCOMUtils.defineLazyGetter(this, "g_list_free", function() {
var g_list_free = libgobject.declare(
"g_list_free", ctypes.default_abi, ctypes.void_t,
GList.ptr
);
if (!g_list_free)
throw "g_list_free is unavailable";
return g_list_free;
});
// guint g_list_length (GList *list);
XPCOMUtils.defineLazyGetter(this, "g_list_length", function() {
var g_list_length = libgobject.declare(
"g_list_length", ctypes.default_abi, guint,
GList.ptr
);
if (!g_list_length)
throw "g_list_length is unavailable";
return g_list_length;
});
XPCOMUtils.defineLazyGetter(this, "g_list_foreach", function() {
var g_list_foreach = libgobject.declare(
"g_list_foreach", ctypes.default_abi, ctypes.void_t,
GList.ptr,
GFunc, // func
gpointer // user_data
);
if (!g_list_foreach)
throw "g_list_foreach is unavailable";
return g_list_foreach;
});
var LibGObject = {
GCallback: GCallback,
GCallbackFunction: GCallbackFunction,
@ -156,5 +219,12 @@ var LibGObject = {
g_signal_connect: function(instance, detailed_signal, handler, data) {
return g_signal_connect_data(instance, detailed_signal,
handler, data, null, 0);
}
},
GList: GList,
GFunc: GFunc,
GFunc_t: GFunc_t,
g_list_free: g_list_free,
g_list_length: g_list_length,
g_list_foreach: g_list_foreach,
};

201
src/modules/LibGdkWindow.js Normal file
View File

@ -0,0 +1,201 @@
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is messagingmenu-extension
*
* The Initial Developer of the Original Code is
* Mozilla Messaging, Ltd.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mike Conley <mconley@mozillamessaging.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var EXPORTED_SYMBOLS = ["LibGdkWindow"];
const LIB_GDKWINDOW = "libgdk-x11-2.0.so.0";
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://moztray/LibGObject.js");
XPCOMUtils.defineLazyGetter(this, "libgdkwindow", function() {
var libgdkwindow = ctypes.open(LIB_GDKWINDOW);
if (!libgdkwindow)
throw "libgdkwindow is unavailable";
return libgdkwindow;
});
XPCOMUtils.defineLazyGetter(this, "GdkWindow", function() {
return ctypes.StructType("GdkWindow");
});
XPCOMUtils.defineLazyGetter(this, "GdkVisual", function() {
return ctypes.StructType("GdkVisual");
});
XPCOMUtils.defineLazyGetter(this, "GdkColormap", function() {
return ctypes.StructType("GdkColormap");
});
XPCOMUtils.defineLazyGetter(this, "GdkWindowType", function() {
return ctypes.StructType("GdkWindowType");
});
XPCOMUtils.defineLazyGetter(this, "GdkCursor", function() {
return ctypes.StructType("GdkCursor");
});
XPCOMUtils.defineLazyGetter(this, "GdkWindowTypeHint", function() {
return ctypes.StructType("GdkWindowTypeHint");
});
XPCOMUtils.defineLazyGetter(this, "GdkWindowClass", function() {
return ctypes.StructType("GdkWindowClass");
});
XPCOMUtils.defineLazyGetter(this, "GdkWindowAttributes", function() {
return ctypes.StructType("GdkWindowAttributes",
[ { "title": LibGObject.gchar },
{ "event_mask": LibGObject.gint },
{ "x": LibGObject.gint },
{ "y": LibGObject.gint },
{ "width": LibGObject.gint },
{ "height": LibGObject.gint },
{ "wclass": LibGObject.gint },
{ "visual": GdkVisual.ptr },
{ "colormap": GdkColormap.ptr },
{ "window_type": LibGObject.gint },
{ "cursor": GdkCursor.ptr },
{ "wmclass_name": LibGObject.gchar },
{ "wmclass_class": LibGObject.gchar },
{ "override_redirect": LibGObject.gboolean },
{ "type_hint": LibGObject.gint }]);
});
XPCOMUtils.defineLazyGetter(this, "gdk_window_new", function() {
var gdk_window_new =
libgdkwindow.declare("gdk_window_new",
ctypes.default_abi,
GdkWindow.ptr,
GdkWindow.ptr,
GdkWindowAttributes.ptr,
LibGObject.gint);
if (!gdk_window_new)
throw "gdk_window_new is unavailable";
return gdk_window_new;
});
XPCOMUtils.defineLazyGetter(this, "gdk_window_destroy", function() {
var gdk_window_destroy =
libgdkwindow.declare("gdk_window_destroy",
ctypes.default_abi,
ctypes.void_t,
GdkWindow.ptr);
if (!gdk_window_destroy)
throw "gdk_window_destroy is unavailable";
return gdk_window_destroy;
});
XPCOMUtils.defineLazyGetter(this, "gdk_x11_window_set_user_time", function() {
var gdk_x11_window_set_user_time =
libgdkwindow.declare("gdk_x11_window_set_user_time",
ctypes.default_abi,
ctypes.void_t,
GdkWindow.ptr,
LibGObject.guint32);
if (!gdk_x11_window_set_user_time)
throw "gdk_x11_window_set_user_time is unavailable";
return gdk_x11_window_set_user_time;
});
XPCOMUtils.defineLazyGetter(this, "gdk_window_hide", function() {
var gdk_window_hide =
libgdkwindow.declare("gdk_window_hide",
ctypes.default_abi,
ctypes.void_t,
GdkWindow.ptr);
if (!gdk_window_hide)
throw "gdk_window_hide is unavailable";
return gdk_window_hide;
});
XPCOMUtils.defineLazyGetter(this, "GdkScreen", function() {
return ctypes.StructType("GdkScreen");
});
// GdkScreen * gdk_screen_get_default (void);
XPCOMUtils.defineLazyGetter(this, "gdk_screen_get_default", function() {
var gdk_screen_get_default =
libgdkwindow.declare("gdk_screen_get_default", ctypes.default_abi, GdkScreen.ptr);
if (!gdk_screen_get_default)
throw "gdk_screen_get_default is unavailable";
return gdk_screen_get_default;
});
// GList * gdk_screen_get_toplevel_windows (GdkScreen *screen);
XPCOMUtils.defineLazyGetter(this, "gdk_screen_get_toplevel_windows", function() {
var gdk_screen_get_toplevel_windows = libgdkwindow.declare(
"gdk_screen_get_toplevel_windows", ctypes.default_abi, LibGObject.GList.ptr,
GdkScreen.ptr
);
if (!gdk_screen_get_toplevel_windows)
throw "gdk_screen_get_toplevel_windows is unavailable";
return gdk_screen_get_toplevel_windows;
});
var LibGdkWindow = {
GdkWindow: GdkWindow,
GdkWindowAttributes: GdkWindowAttributes,
GdkX11WindowSetUserTime: gdk_x11_window_set_user_time,
GdkWindowNew: gdk_window_new,
GdkWindowDestroy: gdk_window_destroy,
GdkWindowHide: gdk_window_hide,
GdkScreen: GdkScreen,
GdkScreenGetDefault: gdk_screen_get_default,
GdkScreenGetToplevelWindows: gdk_screen_get_toplevel_windows,
}

View File

@ -15,14 +15,17 @@ var LibGtkStatusIcon = {
init: function() {
// If ctypes doesn't exist, try to get it
Cu.import("resource://gre/modules/ctypes.jsm");
// If we still don't have ctypes, this isn't going to work...
// If we still don't have ctypes, this isn't going to work...
if (typeof(ctypes) == "undefined") {
throw ("Could not load JS-Ctypes");
}
Cu.import("resource://moztray/LibGObject.js");
Cu.import("resource://moztray/LibGdkWindow.js");
try {
// Try to start up dependencies - if they fail, they'll throw
// exceptions. ex: GObjectLib.init();
// exceptions. ex: LibGObject.init();
this._lib = ctypes.open(LIB_GTK);
if (!this._lib)
@ -47,8 +50,24 @@ var LibGtkStatusIcon = {
// Types
this.GtkStatusIcon = ctypes.StructType("GtkStatusIcon");
this.GtkStatusIconRef = ctypes.PointerType(this.GtkStatusIcon);
this.GdkPixbuf = ctypes.StructType("GdkPixbuf");
this.GdkPixbufRef = ctypes.PointerType(this.GdkPixbuf);
this.GtkWindow = ctypes.StructType(
"GtkWindow", [
]);
this.GtkStyle = ctypes.StructType("GtkStyle");
this.GtkRequisition = ctypes.StructType("GtkRequisition");
this.GtkAllocation = ctypes.StructType("GtkAllocation");
this.GtkWidget = ctypes.StructType("GtkWidget");
/* FIXME: unable to fix "struct field types must have defined and nonzero
* size" */
// this.GtkWidget.define([
// { "style": this.GtkStyle.ptr },
// { "requisition": this.GtkRequisition },
// { "allocation": this.GtkAllocation },
// { "window": LibGdkWindow.GdkWindow.ptr },
// { "parent": ctypes.PointerType(ctypes.void_t) } // this.GtkWidget.ptr
// ]);
// Consts
// this.INDICATOR_MESSAGES_SERVER_TYPE = "message";
@ -69,6 +88,20 @@ var LibGtkStatusIcon = {
ctypes.char.ptr
);
this.gtk_window_list_toplevels = this._lib.declare(
"gtk_window_list_toplevels", ctypes.default_abi, LibGObject.GList.ptr
);
this.gtk_widget_show = this._lib.declare(
"gtk_widget_show", ctypes.default_abi, ctypes.void_t,
this.GtkWidget.ptr
);
this.gtk_widget_hide = this._lib.declare(
"gtk_widget_hide", ctypes.default_abi, ctypes.void_t,
this.GtkWidget.ptr
);
}
};

View File

@ -6,7 +6,7 @@
* http://developer.mozilla.org/en/XUL_School/JavaScript_Object_Management.html
*/
var EXPORTED_SYMBOLS = [ "mozt", "Cc", "Ci" ];
var EXPORTED_SYMBOLS = [ "mozt", "Cc", "Ci", "Cu" ];
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -84,4 +84,32 @@ mozt.Utils = {
appInfoService: Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo), // appInfoService.name.toLower
observerService: Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService),
};
// var xpcomShutdownObserver = {
// observe: function(subject, topic, data) {
// if (topic == "xpcom-will-shutdown") {
// mozt.Debug.debug('event: '
// + 'subj: ' + subject
// + 'topic ' + topic
// + 'data ' + data);
// }
// },
// get observerService() {
// return Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
// },
// register: function() {
// this.observerService.addObserver(this, "xpcom-will-shutdown", false);
// },
// unregister: function() {
// this.observerService.removeObserver(this, "xpcom-will-shutdown");
// },
// };
// xpcomShutdownObserver.register();