Firefox addon X11-XChangeProperty\u网络\u WM\u图标

Firefox addon X11-XChangeProperty\u网络\u WM\u图标,firefox-addon,x11,jsctypes,Firefox Addon,X11,Jsctypes,我加载一个图像,然后将其转换为ARGB数据。我使用XChangeProperty,而不是将数据设置为unsigned_char我将其设置为long,当我从firefox scratchpad、environment>浏览器复制粘贴并运行此代码时,XChangeProperty的值为1表示成功,但图标没有改变 我在Mint 17.1肉桂版和Ubuntu14.04上用默认窗口管理器和icewm进行了测试。这个代码有什么问题吗?XChangeProperty在Cinamon和ubuntu上不起作用吗

我加载一个图像,然后将其转换为ARGB数据。我使用
XChangeProperty
,而不是将数据设置为
unsigned_char
我将其设置为
long
,当我从firefox scratchpad、environment>浏览器复制粘贴并运行此代码时,XChangeProperty的值为1表示成功,但图标没有改变

我在Mint 17.1肉桂版和Ubuntu14.04上用默认窗口管理器和icewm进行了测试。这个代码有什么问题吗?XChangeProperty在Cinamon和ubuntu上不起作用吗

谢谢

复制粘贴代码如下:

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.DISPLAY = new ctypes.StructType('Display');
    this.INT = ctypes.int;
    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.BADGC = 13;
    this.PROPMODEREPLACE = 0; // PropModeReplace
  this.XA_CARDINAL = 6;

}
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
        );
    },
    XChangeProperty: function() {
        /* http://www.xfree86.org/4.4.0/XChangeProperty.3.html
         * int XChangeProperty(
         *   Display *display,
         *   Window w,
         *   Atom property,
         *   Atom type,
         *   int format,
         *   int mode,
         *   unsigned char *data,
         *   int nelements
         * );
         */
        return _lib('x11').declare('XChangeProperty', ctypes.default_abi,
            ostypes.INT,                // return
            ostypes.DISPLAY.ptr,        // *display
            ostypes.WINDOW,             // w
            ostypes.ATOM,               // property
            ostypes.ATOM,               // type
            ostypes.INT,                // format
            ostypes.INT,                // mode
            ctypes.long.ptr,            // *data // is supposed to be `ostypes.UNSIGNED_CHAR.ptr` but for _NET_WM_ICON i guess i need cardinal cuz i cant figure out how to cast card to unsigned_char
            ostypes.INT                 // nelements
        );
    },
    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.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 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];
}

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() {
    /*
    // this block here works, it changes the title of the window
    // but have to change line 116 back to `ostypes.UNSIGNED_CHAR.ptr`
    var myXData = ostypes.UNSIGNED_CHAR.array()('ppbeta');
    console.info('myXData:', myXData, myXData.toString(), uneval(myXData));
    var rez_XChangeProp = _dec('XChangeProperty')(GetXDisplay(), xidFromXULWin(Services.wm.getMostRecentWindow('navigator:browser')), GetAtom('WM_NAME'), GetAtom('UTF8_STRING'), 8, ostypes.PROPMODEREPLACE, myXData, myXData.length);
    console.info('rez_XChangeProp:', rez_XChangeProp, rez_XChangeProp.toString(), uneval(rez_XChangeProp));
    */

    var doc = gBrowser.contentDocument;

    var img = new Image();
    img.onload = function() {
        var canvas = doc.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
        doc.body.appendChild(canvas);

        var ctx = canvas.getContext('2d');

        var size = this.width;
        canvas.width = size;
        canvas.height = size;

        ctx.clearRect(0, 0, size, size);

        ctx.drawImage(this, 0, 0);

        // Getting pixels as a byte (uint8) array
        var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        var pixels8BitsRGBA = imageData.data;

        // Reverting bytes from RGBA to ARGB
        var pixels8BitsARGB = new Uint8Array(pixels8BitsRGBA.length + 8); // +8 bytes for the two leading 32 bytes integers
        for(var i = 0 ; i < pixels8BitsRGBA.length ; i += 4) {
            pixels8BitsARGB[i+8 ] = pixels8BitsRGBA[i+3];
            pixels8BitsARGB[i+9 ] = pixels8BitsRGBA[i  ];
            pixels8BitsARGB[i+10] = pixels8BitsRGBA[i+1];
            pixels8BitsARGB[i+11] = pixels8BitsRGBA[i+2];
        }

        // Converting array buffer to a uint32 one, and adding leading width and height
        var pixelsAs32Bits = new Uint32Array(pixels8BitsARGB.buffer);
        pixelsAs32Bits[0] = canvas.width;
        pixelsAs32Bits[1] = canvas.height;

        console.log(pixelsAs32Bits);
        var jsarr = Array.prototype.slice.call(pixelsAs32Bits);
        console.log('jsarr:', jsarr);

        var myXData = ctypes.long.array()(jsarr);
        //myXData = ctypes.cast(myXData, ostypes.UNSIGNED_CHAR.ptr);
        //console.info('myXData:', myXData); // loging after casting is crashing it i have no idea why
        var rez_XChangeProp = _dec('XChangeProperty')(GetXDisplay(), xidFromXULWin(Services.wm.getMostRecentWindow('navigator:browser')), GetAtom('_NET_WM_ICON'), ostypes.XA_CARDINAL, 32, ostypes.PROPMODEREPLACE, myXData, myXData.length);
        console.info('rez_XChangeProp:', rez_XChangeProp, rez_XChangeProp.toString(), uneval(rez_XChangeProp));
    };
    img.src = OS.Path.toFileURI(OS.Path.join(OS.Constants.Path.desktopDir, 'profilist-ff-channel-logos', 'release64.png'));
}

try {
    main();
} catch(ex) {
    console.error('error:', ex);
} finally {
    shutdown();
}
Cu.import('resource://gre/modules/ctypes.jsm')
var nixtypesInit=函数(){
//基本类型(不等于我预定义的类型)
this.ATOM=ctypes.unsigned_long;
this.BOOL=ctypes.int;
this.CHAR=ctypes.CHAR;
this.GDKDRAWABLE=ctypes.StructType('GDKDRAWABLE');
this.GDKWINDOW=ctypes.StructType('GDKWINDOW');
this.DISPLAY=new ctypes.StructType('DISPLAY');
this.INT=ctypes.INT;
this.UNSIGNED_CHAR=ctypes.UNSIGNED_CHAR;
this.UNSIGNED_INT=ctypes.UNSIGNED_INT;
this.UNSIGNED_LONG=ctypes.UNSIGNED_LONG;
//高级类型(与我预定义的类型相同的类型,这里的顺序很重要,因为基本类型或预高级类型需要在类型之前定义)
如果(/^(Alpha | hppa | ia64 | ppc64 | s390 | x86 | u 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;
}否则{
this.CARD32=this.UNSIGNED_LONG;
}
this.WINDOW=this.CARD32;
this.XID=this.CARD32;
//常数
这个.BADGC=13;
this.PROPMODEREPLACE=0;//PROPMODEREPLACE
this.XA_基数=6;
}
var ostypes=new-nixtypesInit();
var lib={};
函数库(路径){
//确保路径在库中,如果路径在库中,则路径打开;如果路径不在库中,则路径添加到库中并打开路径。返回库
//路径是打开库的路径
//返回lib以便可以直接使用
if(!(库中的路径)){
//需要打开图书馆吗
//默认情况下,它会打开路径,但有些东西是特殊的,比如mac中的libc与linux不同,或者需要根据linux版本定位x11
交换机(路径){
案例“x11”:
试一试{
lib[path]=ctypes.open('libX11.so.6');
}捕获(e){
试一试{
var libName=ctypes.libraryName('X11');
}捕获(e){
console.error('Integration Level 1:无法获取libX11名称;未激活','e:',e');
抛出新错误('集成级别1:无法获取libX11名称;未激活,e:'+e);
}
试一试{
lib[path]=ctypes.open(libName);
}捕获(e){
console.error('Integration Level 2:无法获取libX11名称;未激活','e:',e');
抛出新错误('集成级别2:无法获取libX11名称;未激活,e:'+e);
}
}
打破
违约:
试一试{
lib[path]=ctypes.open(路径);
}捕获(e){
console.error('集成级别1:无法获取打开的路径:',路径'e:',e';
抛出新错误('集成级别1:无法获取开放路径:“+path+”“e:“+e+”);
}
}
}
返回lib[path];
}
//在这个worker中声明,我将scratchpad的它们都设置为null,因为我可能会声明错误,并且它不会重新声明,除非它首先是var!
var-dec={};
函数_dec(declaration){//表示ensureDeclared并返回declare。如果未声明它,则声明它。否则返回先前声明的。
如果(!(12月申报)){
dec[declaration]=preDec[declaration]();//若声明不在preDec中,那个么开发人员就搞砸了
}
返回十二月[声明];
}
var preDec={//代表pre-declare(所以它只是懒惰的东西)//这必须由dev//预先填充,并按键对其进行字母化,这样它的ez就可以查看了
gdk_x11_可绘制_get_xid:function(){
/* https://developer.gnome.org/gdk2/stable/gdk2-X-Window-System-Interaction.html#gdk-x11可拉深的get xid
*XID gdk_x11_可拉伸_get_XID(
*GdkDrawable*可绘制
* );
*/
返回_lib('libgdk-x11-2.0.so.0')。声明('gdk_x11\u drawable\u get\u xid',ctypes.default\u abi,
ostypes.XID,//返回
ostypes.GDKDRAWABLE.ptr//*可绘制
);
},
XChangeProperty:函数(){
/* http://www.xfree86.org/4.4.0/XChangeProperty.3.html
*int XChangeProperty(
*显示*显示,
*窗口w,
*原子属性,
*原子型,
*int格式,
*int模式,
*无符号字符*数据,
*内部元素
* );
*/
返回_lib('x11')。声明('XChangeProperty',ctypes.default_abi,
ostypes.INT,//返回
ostypes.DISPLAY.ptr,//*显示
ostypes.WINDOW,//w
ostypes.ATOM,//属性
ostypes.ATOM,//类型
ostypes.INT,//格式
ostypes.INT,//模式
ctypes.long.ptr,//*data//应该是`ostypes.UNSIGNED_CHAR.ptr`但是对于_NET_WM_ICON,我想我需要cardinal,因为我不知道如何将卡强制转换为UNSIGNED_CHAR
ostypes.INT//n元素
);
},
XCloseDisplay:函数(){
/* http://www.xfree86.org