Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created March 4, 2015 20:33
Show Gist options
  • Save Noitidart/442af04325582a51d56f to your computer and use it in GitHub Desktop.
Save Noitidart/442af04325582a51d56f to your computer and use it in GitHub Desktop.
_ff-addon-snippet-X11-SkelPlus - Based on skelenton but plus in that it has my GetAtom and cache functionality
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.DISPLAY = new ctypes.StructType('Display');
this.INT = ctypes.int;
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.XID = this.CARD32;
// CONSTANTS
this.BADGC = 13;
}
var ostypes = new nixtypesInit();
console.log('ostypes.NULL:', ostypes.NULL, uneval(ostypes.NULL))
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
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
);
},
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 // runtime defined constants
function GetXDisplay() {
if (!GetXDisplayConst) {
GetXDisplayConst = OpenNewXDisplay();
}
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 because 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];
}
/* end helper functions */
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() {
//GetXDisplay();
}
try {
main();
} catch(ex) {
console.error('error:', ex);
} finally {
shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment