/
XGetWindowProperty.txt
346 lines (323 loc) · 17.4 KB
/
XGetWindowProperty.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
Cu.import('resource://gre/modules/ctypes.jsm');
var nixtypesInit = function() {
// BASIC TYPES (ones that arent equal to something predefined by me)
this.ATOM = ctypes.unsigned_long;
this.BOOL = ctypes.int;
this.CHAR = ctypes.char;
this.GDKDRAWABLE = ctypes.StructType('GdkDrawable');
this.GDKWINDOW = ctypes.StructType('GdkWindow');
this.DATA = ctypes.voidptr_t;
this.DISPLAY = new ctypes.StructType('Display');
this.INT = ctypes.int;
this.LONG = ctypes.long;
this.UNSIGNED_CHAR = ctypes.unsigned_char;
this.UNSIGNED_INT = ctypes.unsigned_int;
this.UNSIGNED_LONG = ctypes.unsigned_long;
// ADVANCED TYPES (ones that are equal to something predefined by me, order matters here, as the basic or pre-advanced type needs to be defined before the type)
if (/^(Alpha|hppa|ia64|ppc64|s390|x86_64)-/.test(Services.appinfo.XPCOMABI)) { // https://github.com/foudfou/FireTray/blob/a0c0061cd680a3a92b820969b093cc4780dfb10c/src/modules/ctypes/linux/x11.jsm#L45 // // http://mxr.mozilla.org/mozilla-central/source/configure.in
this.CARD32 = this.UNSIGNED_INT;
} else {
this.CARD32 = this.UNSIGNED_LONG;
}
this.WINDOW = this.CARD32;
this.XID = this.CARD32;
// CONSTANTS
this.ANYPROPERTYTYPE = 0; //AnyPropertyType //this.ATOM(0) // need this jsInt for comparison
this.BADGC = 13;
this.NONE = 0; // leave it at 0 (a jsInt) as simple comparison is done in GetAtom, cuz in GetAtom i do `if (rez == ostypes.NONE)` and if is a number here it works. otherwise its weird. ostypes.ATOM(0) and new ostypes.ATOM(0) both give back CData{ value: UInt64{} } but the XInternAtom even thouh return is ostypes.ATOM it gives back UInt64 &&& doing UInt64 == jsInt seems to work. oin ostypes.ATOM(0).value == returnedUInt64 does not work if i do this then i have to to returnedUInt64.toString() == ostypes.ATOM(0).value.toStrin() so weird
this.SUCCESS = 0;
this.XA_CARDINAL = 6; // can do parseInt(GetAtom('CARDINAL').toString()) but dont as i need this a jsInt for comparisons in post _dec('XGetWindowProperty')() logic, otherwise i have to use ctypes.UInt64.compare for equality checks
}
var ostypes = new nixtypesInit();
var lib = {};
function _lib(path) {
//ensures path is in lib, if its in lib then its open, if its not then it adds it to lib and opens it. returns lib
//path is path to open library
//returns lib so can use straight away
if (!(path in lib)) {
//need to open the library
//default it opens the path, but some things are special like libc in mac is different then linux or like x11 needs to be located based on linux version
switch (path) {
case 'x11':
try {
lib[path] = ctypes.open('libX11.so.6');
} catch (e) {
try {
var libName = ctypes.libraryName('X11');
} catch (e) {
console.error('Integration Level 1: Could not get libX11 name; not activating', 'e:', e);
throw new Error('Integration Level 1: Could not get libX11 name; not activating, e:' + e);
}
try {
lib[path] = ctypes.open(libName);
} catch (e) {
console.error('Integration Level 2: Could not get libX11 name; not activating', 'e:', e);
throw new Error('Integration Level 2: Could not get libX11 name; not activating, e:' + e);
}
}
break;
default:
try {
lib[path] = ctypes.open(path);
} catch (e) {
console.error('Integration Level 1: Could not get open path:', path, 'e:', e);
throw new Error('Integration Level 1: Could not get open path:"' + path + '" e: "' + e + '"');
}
}
}
return lib[path];
}
// declares in this worker, i set them all = to null for the scratchpad because i may declare it wrong and it wont re-declare unless if it first the var is !
var dec = {};
function _dec(declaration) { // it means ensureDeclared and return declare. if its not declared it declares it. else it returns the previously declared.
if (!(declaration in dec)) {
dec[declaration] = preDec[declaration](); //if declaration is not in preDec then dev messed up
}
return dec[declaration];
}
var preDec = { //stands for pre-declare (so its just lazy stuff) //this must be pre-populated by dev // do it alphabateized by key so its ez to look through
gdk_x11_drawable_get_xid: function() {
/* https://developer.gnome.org/gdk2/stable/gdk2-X-Window-System-Interaction.html#gdk-x11-drawable-get-xid
* XID gdk_x11_drawable_get_xid (
* GdkDrawable *drawable
* );
*/
return _lib('libgdk-x11-2.0.so.0').declare('gdk_x11_drawable_get_xid', ctypes.default_abi,
ostypes.XID, // return
ostypes.GDKDRAWABLE.ptr // *drawable
);
},
XCloseDisplay: function() {
/* http://www.xfree86.org/4.4.0/XCloseDisplay.3.html
* int XCloseDisplay(
* Display *display
* );
*/
return _lib('x11').declare('XCloseDisplay', ctypes.default_abi,
ostypes.INT, // return
ostypes.DISPLAY.ptr // *display
);
},
XFree: function() {
/* http://www.xfree86.org/4.4.0/XFree.3.html
* int XFree(
* void *data
* );
*/
return _lib('x11').declare('XFree', ctypes.default_abi,
ostypes.INT, // return
ostypes.DATA // *data
);
},
XGetWindowProperty: function() {
/* http://www.xfree86.org/4.4.0/XChangeProperty.3.html
* int XChangeProperty(
* Display *display,
* Window w,
* Atom property,
* long long_offset,
* long long_length,
* Bool delete,
* Atom req_type,
* Atom *actual_type_return,
* int *actual_format_return,
* unsigned long *nitems_return,
* unsigned long *bytes_after_return,
* unsigned char **prop_return,
* );
*/
return _lib('x11').declare('XGetWindowProperty', ctypes.default_abi,
ostypes.INT, // return
ostypes.DISPLAY.ptr, // *display
ostypes.WINDOW, // w
ostypes.ATOM, // property
ostypes.LONG, // long_offset
ostypes.LONG, // long_length
ostypes.BOOL, // delete
ostypes.ATOM, // req_type // note on note: actually if ANYPROPERTYTYPE and NONE are jsInt then thse can be atoms. note to self: always put a jsInt here because in comparison checks after running `_dec('XGetWindowProperty')` I do `xgwpArg.req_type != xgwpArg.$actual_type_return` to test for mismatch, and i also test to make sure its not ostypes.ANYPROPERTYTYPE. and on return they are `UInt64{}` (even though declared to be `ostypes.ATOM.ptr` in `preDec`) but `new ostypes.ATOM(5)` is `CData{ UInt64{} }`. so doing a simple == for comparison requires one or the other be a jsInt
ostypes.ATOM.ptr, // *actual_type_return
ostypes.INT.ptr, // *actual_format_return
ostypes.UNSIGNED_LONG.ptr, // *nitems_return
ostypes.UNSIGNED_LONG.ptr, // *bytes_after_return
ostypes.UNSIGNED_CHAR.ptr.ptr // **prop_return
);
},
XInternAtom: function() {
/* http://www.xfree86.org/4.4.0/XInternAtom.3.html
* Atom XInternAtom(
* Display *display,
* char *atom_name,
* Bool only_if_exists
* );
*/
return _lib('x11').declare('XInternAtom', ctypes.default_abi,
ostypes.ATOM, // return
ostypes.DISPLAY.ptr, // *display
ostypes.CHAR.ptr, // *atom_name
ostypes.BOOL // only_if_exists
);
},
XOpenDisplay: function() {
/* http://www.xfree86.org/4.4.0/XOpenDisplay.3.html
* Display *XOpenDisplay(
* char *display_name
* );
*/
return _lib('x11').declare('XOpenDisplay', ctypes.default_abi,
ostypes.DISPLAY.ptr, // return
ostypes.CHAR.ptr // *display_name
);
}
}
/* start helper functions */
function OpenNewXDisplay() {
var rez_XOpenDisplay = _dec('XOpenDisplay')(null);
console.log('debug-msg :: rez_XOpenDisplay:', rez_XOpenDisplay, uneval(rez_XOpenDisplay));
// when rez_XOpenDisplay is null it is CData of `Display.ptr(ctypes.UInt64("0x0"))"`
if (rez_XOpenDisplay.isNull()) {
throw new Error('XOpenDisplay failed to open display');
}
return rez_XOpenDisplay;
}
var GetXDisplayConst = undefined; //ostypes.DISPLAY.ptr // runtime defined constants
function GetXDisplay() {
if (!GetXDisplayConst) {
GetXDisplayConst = OpenNewXDisplay(); // returns Display*
}
return GetXDisplayConst;
}
var _GetAtomCache = {};
function GetAtom(name, createIfDNE) {
// createIfDNE is jsBool, true or false. if set to true/1 then the atom is creatd if it doesnt exist. if set to false/0, then an error is thrown when atom does not exist
// default behavior is throw when atom doesnt exist
// name is ostypes.CHAR.ptr
// returns ostypes.ATOM
var onlyIfExists = 1;
if (createIfDNE) {
onlyIfExists = 0;
}
if (!(name in _GetAtomCache)) {
var atom = _dec('XInternAtom')(GetXDisplay(), name, createIfDNE ? 0 : 1); //passing 3rd arg of false, means even if atom doesnt exist it returns a created atom, this can be used with GetProperty to see if its supported etc, this is how Chromium does it
if (atom == ostypes.NONE) { //will never equal ostypes.NONE if i pass 3rd arg of `false` to XInternAtom
console.warn('No atom with name:', name, 'return val of atom:', atom, uneval(atom), atom.toString());
throw new Error('No atom with name "' + name + '"), return val of atom:"' + atom + '" toString:"' + atom.toString() + '"');
}
_GetAtomCache[name] = atom;
}
return _GetAtomCache[name];
}
function xidFromXULWin(aXULWin) {
if (!aXULWin) {
throw new Error('No window found, aXULWin is null');
}
var aBaseWin = aXULWin.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIBaseWindow);
var aGDKWindowPtrString = aBaseWin.nativeHandle;
var aGDKWindowPtr = ostypes.GDKWINDOW.ptr(ctypes.UInt64(aGDKWindowPtrString));
var aGDKDrawablePtr = ctypes.cast(aGDKWindowPtr, ostypes.GDKDRAWABLE.ptr);
var aXID = _dec('gdk_x11_drawable_get_xid')(aGDKDrawablePtr); //no need for error checking here as if it doesnt exist it crashes?
if (aXID == 0) {
throw new Error('aXULWin is no longer open, as aXID is 0');
}
console.info('aXID:', aXID, aXID.toString(), uneval(aXID));
return aXID;
}
/* end helper functions */
// my globals:
var libgdk = 'libgdk-x11-2.0.so.0';
function shutdown() {
if (GetXDisplayConst && GetXDisplayConst.isNull && !GetXDisplayConst.isNull()) {
console.log('closing disp');
var rez_XCloseDisplay = _dec('XCloseDisplay')(GetXDisplay()); //it seems like XCloseDisp returns 0 on success, docs dont clarify that, they just say that XCloseDisplay can "generate" BadGC (they dont clarify what generate means return) // http://stackoverflow.com/questions/23083523/what-does-xclosedisplay-return
console.log('debug-msg :: rez_XCloseDisplay:', rez_XCloseDisplay, uneval(rez_XCloseDisplay));
if (rez_XCloseDisplay != 0) {
throw new Error('XCloseDisplay failed with error code: "' + rez_XCloseDisplay + '"');
}
} else {
console.warn('no need to close disp');
}
for (var l in lib) {
lib[l].close();
}
}
function main() {
var xgwpArg = {
$display: /*DISPLAY.ptr*/ GetXDisplay(),
w: /*WINDOW*/ xidFromXULWin(Services.wm.getMostRecentWindow('navigator:browser')),
property: /*ATOM*/ GetAtom('_NET_WM_ICON'),
long_offset: /*LONG*/ ostypes.LONG(0),
long_length: /*LONG*/ ostypes.LONG(0),
delete: /*BOOL*/ 0,
req_type: /*ATOM - jsInt*/ ostypes.ANYPROPERTYTYPE,
$actual_type_return: /*ATOM.ptr*/ ostypes.ATOM().address(),
$actual_format_return: /*INT.ptr*/ new ostypes.INT(123).address(), // i can set this to whatever i want, im very sure, as it gets set to 0, if prop DNE and it gets set to right format if it exists (in both existance situations [(req_type != AnyPropertyType && req_type != actual_type_return) || (req_type == AnyPropertyType || req_type == actual_type_return]
$nitems_return: /*UNSIGNED_LONG.ptr*/ new ostypes.UNSIGNED_LONG().address(),
$bytes_after_return: /*UNSIGNED_LONG.ptr*/ new ostypes.UNSIGNED_LONG().address(),
$$prop_return: /*UNSIGNED_CHAR.ptr.ptr*/ new ostypes.UNSIGNED_CHAR.ptr().address()
};
var rez_XGetWinProp = _dec('XGetWindowProperty')(xgwpArg.$display, xgwpArg.w, xgwpArg.property, xgwpArg.long_offset, xgwpArg.long_length, xgwpArg.delete, xgwpArg.req_type, xgwpArg.$actual_type_return, xgwpArg.$actual_format_return, xgwpArg.$nitems_return, xgwpArg.$bytes_after_return, xgwpArg.$$prop_return);
console.info('rez_XGetWinProp:', rez_XGetWinProp, rez_XGetWinProp.toString(), uneval(rez_XGetWinProp));
//console.info('xgwpArg:', xgwpArg, uneval(xgwpArg));
//console.info('xgwpArg:', xgwpArg.$$prop_return, uneval(xgwpArg.$$prop_return));
//console.info('xgwpArg:', xgwpArg.$$prop_return.contents, uneval(xgwpArg.$$prop_return.contents));
if (rez_XGetWinProp != ostypes.SUCCESS) {
console.log('XGetWindowProperty failed with reason:', rez_XGetWinProp);
} else {
if(xgwpArg.req_type == ostypes.NONE /* must be jsInt */ && xgwpArg.$actual_format_return.contents == 0 && xgwpArg.$bytes_after_return.contents.value.toString() == 0) {
// nitems_return argument will be empty
console.log('The specified property does not exist for the specified window. The delete argument was ignored. The nitems_return argument will be empty.');
if (xgwpArg.$nitems_return.isNull() == false) {
console.warn('nitems_return argument should be empty but its not!', 'xgwpArg[argNameIndex[\'*nitems_return\']]:', xgwpArg.$nitems_return, xgwpArg.$nitems_return.toString(), uneval(xgwpArg.$nitems_return));
}
} else if (xgwpArg.req_type != ostypes.ANYPROPERTYTYPE /*ANYPROPERTYTYPE should be jsInt*/ && xgwpArg.req_type /*req_type should be jsInt*/ != xgwpArg.$actual_type_return.contents) {
// nitems_return argument will be empty
console.log('Specified property exists but its type does not match the specified type. The delete argument was ignored. The nitems_return argument will be empty.');
var theRetunedPropertyTypeActuallyIs = xgwpArg.$actual_type_return.contents.value.toString();
// if the passed in *actual_format_return didn't match, it is changed to be what the format is, if it matched, then it stays the same
var theReturnedFormat = xgwpArg.$actual_format_return.contents;
var theReturnedPropLengthInBytes = xgwpArg.$bytes_after_return; //its in bytes even if theFormat is 16 or 32 (im guessing if format is 8 then its byte)
if (xgwpArg.$nitems_return.isNull() == false) {
console.warn('nitems_return argument should be empty but its not!', 'xgwpArg[argNameIndex[\'*nitems_return\']]:', xgwpArg.$nitems_return, xgwpArg.$nitems_return.toString(), uneval(xgwpArg.$nitems_return));
}
} else if (xgwpArg.req_type == ostypes.ANYPROPERTYTYPE /*ANYPROPERTYTYPE should be jsInt*/ || xgwpArg.req_type /*req_type should be jsInt*/ == xgwpArg.$actual_type_return) {
// i think nitems_return CAN be empty here, so a check of if nitems_return is empty or not cannot qualify for these two existance situations
console.log('The specified property exists and either you assigned AnyPropertyType to the req_type argument or the specified type matched the actual property type of the returned data.');
if (xgwpArg.req_type == ostypes.ANYPROPERTYTYPE) {
// `xgwpArg.$actual_type_return` was set to what the type of the returned property really is
} else {
// `xgwpArg.req_type` and xgwpArg.$actual_type_return match
}
var theRetunedPropertyTypeActuallyIs = xgwpArg.$actual_type_return.contents.toString(); // if AnyPropertyType was not used then this is just the same as req_type (but it will not be jsInt, like i expect myself to be setting `req_type` to
var theReturnedFormat = xgwpArg.$actual_format_return.contents; // set to the format of the returned property (so there is a chnance this can change) (so this makes me think maybe the arg when being passed in can be passed as anything? unless x11 does some internal checks to see if its 8, 16, or 32
// bytes_after_return should be MIN between ("actual length of the stored property in bytes (even if format is 16 or 32)" - "4*xgwpArg.long_offset" || "4*xgwpArg.long_length") IF THIS VALUE TURNS OUT BE NEGATIVE, THEN ostypes.BADVALUE IS RETURNED BY `_dec('XGetWindowProperty')`
if (xgwpArg.$actual_format_return.contents == 8) {
// then xgwpArg.$$prop_return is represented as a char array
console.log('then xgwpArg.$$prop_return is represented as a char array');
} else if (xgwpArg.$actual_format_return.contents == 16) {
// then xgwpArg.$$prop_return is represented as a short array and should be cast to `xgwpArg.$actual_type_return`'s jsctypes equivalent type to obtain the elements
console.log('then xgwpArg.$$prop_return is represented as a short array and should be cast to `xgwpArg.$actual_type_return`\'s jsctypes equivalent type to obtain the elements');
} else if (xgwpArg.$actual_format_return.contents == 32) {
// then xgwpArg.$$prop_return is represented as a long array and should be cast to `xgwpArg.$actual_type_return`'s jsctypes equivalent type to obtain the elements
console.log('then xgwpArg.$$prop_return is represented as a long array and should be cast to `xgwpArg.$actual_type_return`\'s jsctypes equivalent type to obtain the elements');
} else {
throw new Error('extremely weird, this should NEVER happen, it should always be 8, 16, or 32');
}
// must always XFree, even if prop_return is empty because: XGetWindowProperty always allocates one extra byte in prop_return (even if the property is zero length) and sets it to zero
var rez_XFree = _dec('XFree')(xgwpArg.$$prop_return.contents); //probably should XFree the ** rather then the *. so meaning should probably XFree the .ptr.ptr instead of the .ptr
console.info('rez_XFree:', rez_XFree, rez_XFree.toString(), uneval(rez_XFree));
} else {
console.warn('some unknown combinations returned');
}
}
}
try {
main();
} catch(ex) {
console.error('error:', ex);
} finally {
shutdown();
}