Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

js-ctypes cast + constant

85 views
Skip to first unread message

foudfou

unread,
Jul 12, 2011, 8:29:48 AM7/12/11
to
Hi All,

1. is there a way to cast a XUL BaseWindow to a native GdkWindow ? Or how can I port the following XPCOM code to js-ctypes:

NS_IMETHODIMP nsTray::HideWindow(nsIBaseWindow *aBaseWindow) {
nsresult rv;

nativeWindow aNativeWindow;
rv = aBaseWindow->GetParentNativeWindow(&aNativeWindow);

GdkWindow *gdk_win=gdk_window_get_toplevel((GdkWindow*) aNativeWindow);

gdk_window_hide(gdk_win);
}

2. is there a way to access/declare C pre-defined variables/constants with js-ctypes ? For example, how to declare fputs() and write to stderr ? My attempt:

var libc = ctypes.open("libc.so.6");
const FILE_ptr = new ctypes.StructType("FILE").ptr;
var fputs = libc.declare("fputs", /* function name */
ctypes.default_abi, /* call ABI */
ctypes.int32_t, /* return type */
ctypes.char.ptr, /* argument type */
FILE_ptr // ctypes.PointerType("FILE*")
);
var ret = fputs("Hello World from js-ctypes!", 2);


Cheers,
Foudil

foudfou

unread,
Jul 12, 2011, 7:33:18 PM7/12/11
to
> 2. is there a way to access/declare C pre-defined variables/constants with js-ctypes ? For example, how to declare fputs() and write to stderr ? My attempt:

AFAIK, we need to re-declare all constants. see http://philikon.wordpress.com/2010/09/17/mozilla-four-months-later-a-status-update/ for an alternative on OSX.

I managed to write to stderr this way (note the call to fflush()):

var libc = ctypes.open("libc.so.6");

const FILE = new ctypes.StructType("FILE");

var fdopen = libc.declare(
"fdopen", ctypes.default_abi, FILE.ptr,
ctypes.int,
ctypes.char.ptr
);

var fputs = libc.declare(
"fputs", ctypes.default_abi, ctypes.int32_t,
ctypes.char.ptr,
FILE.ptr
);

var fflush = libc.declare(
"fflush", ctypes.default_abi, ctypes.int32_t,
FILE.ptr
);

const stderr = fdopen(2, "a");

fputs("Hello World from js-ctypes!", stderr);
fflush(stderr);

libc.close();


But still no answer to:

foudfou

unread,
Jul 17, 2011, 8:21:01 PM7/17/11
to
Hi all,

I'm trying to port to js-ctypes the 'Firetray' extension (similar to 'MinimizeToTray'), intended for Linux, and which currently contains binary components. For ex. it uses gdk_window_hide() for hiding the native windows.

I tried different approaches for hiding windows:

First, i tried to gather toplevel windows from nsIWindowMediator, but then I'm unable to get some kind of pointer to a nativeParentWindow (GdkWindow) in order to apply gdk_window_hide() to it.

I thought about casting a XUL window to a GdkWindow (hopefully in JS code), but I guess this doesn't make much sense.

Neither could I get any handle on the 'hidden' or 'visible' properties of the provided windows (ex: nsIBaseWindow): either they're undefined or modifying them has no effect.

So I tried to manipulate GTK/GDK windows directly, and gathered them with
- gdk_screen_get_toplevel_windows(), but then I don't know exactly how to find out which GdkWindows belong to the application
- gtk_window_list_toplevels(), but I seem to get too many windows (6)

I'm well aware these are GTK-related questions (for which any help would also be greatly appreciated), but I'm also beginning to wonder if this is a good approach ? Could anyone comment on the different approaches and shortcomings ?


Could anyone also provide a js-ctypes example of a recursive structure declaration ? For ex:

/* FIXME: unable to fix "struct field types must have defined and nonzero
* size" */
this.GtkWidget = ctypes.StructType("GtkWidget");
this.GtkWidget.define([
{ "style": this.GtkStyle.ptr },
{ "requisition": this.GtkRequisition },
{ "allocation": this.GtkAllocation },
{ "window": LibGdkWindow.GdkWindow.ptr },
{ "parent": ctypes.PointerType(ctypes.void_t) } // also tried this.GtkWidget.ptr
]);

Benjamin Smedberg

unread,
Jul 18, 2011, 12:36:18 PM7/18/11
to foudfou, dev-ext...@lists.mozilla.org
On 7/17/2011 8:21 PM, foudfou wrote:
>
> First, i tried to gather toplevel windows from nsIWindowMediator, but
> then I'm unable to get some kind of pointer to a nativeParentWindow
> (GdkWindow) in order to apply gdk_window_hide() to it.
Correct. We've discussed adding an accessor for this pointer (and the
HWND on Windows), but nobody has actually written the patch yet.

>
> Neither could I get any handle on the 'hidden' or 'visible' properties
> of the provided windows (ex: nsIBaseWindow): either they're undefined
> or modifying them has no effect.

I don't see those properties, but I do see a "visibility" property on
nsIBaseWindow. Have you tried using that one?

http://mxr.mozilla.org/mozilla-central/source/widget/public/nsIBaseWindow.idl#193

>
> Could anyone also provide a js-ctypes example of a recursive structure
> declaration ? For ex:
>
> /* FIXME: unable to fix "struct field types must have defined and
> nonzero
> * size" */
> this.GtkWidget = ctypes.StructType("GtkWidget");
> this.GtkWidget.define([
> { "style": this.GtkStyle.ptr },
> { "requisition": this.GtkRequisition },
> { "allocation": this.GtkAllocation },
> { "window": LibGdkWindow.GdkWindow.ptr },
> { "parent": ctypes.PointerType(ctypes.void_t) } // also tried
> this.GtkWidget.ptr
> ]);

Are you sure that the "parent" property is the one causing the problems.
Are GtkRequsition and GtkAllocation defined types? Try narrowing it down
to the one field causing the problem. We have StructType.define
specifically to solve this case of a structure needing to point to itself.

--BDS

foudfou

unread,
Jul 18, 2011, 4:37:06 PM7/18/11
to Benjamin Smedberg, dev-ext...@lists.mozilla.org
>> First, i tried to gather toplevel windows from nsIWindowMediator, but then I'm unable to get some kind of pointer to a nativeParentWindow (GdkWindow) in order to apply gdk_window_hide() to it.
> Correct. We've discussed adding an accessor for this pointer (and the HWND on Windows), but nobody has actually written the patch yet.
Is it somehow still on the agenda ? Or should I file a request on bugzilla ?

>> Neither could I get any handle on the 'hidden' or 'visible' properties of the provided windows (ex: nsIBaseWindow): either they're undefined or modifying them has no effect.
> I don't see those properties, but I do see a "visibility" property on nsIBaseWindow. Have you tried using that one?
> http://mxr.mozilla.org/mozilla-central/source/widget/public/nsIBaseWindow.idl#193

Thank you for correcting. I had tried the "visibility" property on nsIBaseWindow which remains 'true'. Setting it to 'false' has no effect. XUL windows do have a 'hidden' property (inherited from XUL element), but I can't find a means to access it. I managed to setAttribute("hidden", "true") on a nsIDOMChromeWindow but that has the unwanted effect of hiding the DOM content, and keeping the chrome elements visible.

>> Could anyone also provide a js-ctypes example of a recursive structure declaration ? For ex:
>>
>> /* FIXME: unable to fix "struct field types must have defined and nonzero
>> * size" */
>> this.GtkWidget = ctypes.StructType("GtkWidget");
>> this.GtkWidget.define([
>> { "style": this.GtkStyle.ptr },
>> { "requisition": this.GtkRequisition },
>> { "allocation": this.GtkAllocation },
>> { "window": LibGdkWindow.GdkWindow.ptr },
>> { "parent": ctypes.PointerType(ctypes.void_t) } // also tried this.GtkWidget.ptr
>> ]);
> Are you sure that the "parent" property is the one causing the problems. Are GtkRequsition and GtkAllocation defined types? Try narrowing it down to the one field causing the problem. We have StructType.define specifically to solve this case of a structure needing to point to itself.

You were right: GtkRequsition and GtkAllocation were not define()'d.

Benjamin Smedberg

unread,
Jul 18, 2011, 4:46:45 PM7/18/11
to foudfou, dev-ext...@lists.mozilla.org
On 7/18/2011 4:37 PM, foudfou wrote:
>>> First, i tried to gather toplevel windows from nsIWindowMediator,
>>> but then I'm unable to get some kind of pointer to a
>>> nativeParentWindow (GdkWindow) in order to apply gdk_window_hide()
>>> to it.
>> Correct. We've discussed adding an accessor for this pointer (and the
>> HWND on Windows), but nobody has actually written the patch yet.
> Is it somehow still on the agenda ? Or should I file a request on
> bugzilla ?
I don't know if it's filed. I'm quite certain that nobody is currently
working on it. It would be an accessor on nsIBaseWindow like this:

readonly attribute jsval nativeHandle;

If you are interested in writing the patch, feel free to file the bug.

--BDS

foudfou

unread,
Jul 25, 2011, 5:38:52 PM7/25/11
to
Actually, I was wrong: the "visibility" property of nsIBaseWindow can be set (although it always remains 'true'), and this has the desired show/hide effect (except that the position of windows aren't exactly identical)
...I just needed to query it properly:

bw = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIBaseWindow);
bw.visibility = false;


Tanks a lot to Neil for his kind guidance !

0 new messages