MeshCentral/agents/modules_meshcore_min/monitor-info.min.js
2019-02-07 20:06:33 -08:00

1 line
7.1 KiB
JavaScript

var promise=require("promise");var PPosition=4;var PSize=8;var _NET_WM_STATE_REMOVE=0;var _NET_WM_STATE_ADD=1;var _NET_WM_STATE_TOGGLE=2;var SubstructureRedirectMask=(1<<20);var SubstructureNotifyMask=(1<<19);function getLibInfo(f){if(process.platform!="linux"){throw ("Only supported on linux")}var a=require("child_process").execFile("/bin/sh",["sh"]);a.stdout.str="";a.stdout.on("data",function(i){this.str+=i.toString()});a.stdin.write("ldconfig -p | grep '"+f+".so.'\nexit\n");a.waitExit();var l=[];var g=a.stdout.str.split("\n");for(var b in g){if(g[b]){var c=g[b].split("=>");var k=c[1].trim();var e=c[0].trim().split(" ");var d=e[0];var h=e[1].substring(1,e[1].length-1).split(",");if(d.startsWith(f+".so.")){l.push({lib:d,path:k,info:h})}}}return(l)}function monitorinfo(){this._ObjectID="monitor-info";this._gm=require("_GenericMarshal");if(process.platform=="win32"){this._user32=this._gm.CreateNativeProxy("user32.dll");this._user32.CreateMethod("EnumDisplayMonitors");this._kernel32=this._gm.CreateNativeProxy("kernel32.dll");this._kernel32.CreateMethod("GetLastError");this.getInfo=function h(){var e=this;return(new promise(function(v,u){this._monitorinfo={resolver:v,rejector:u,self:e,callback:e._gm.GetGenericGlobalCallback(4)};this._monitorinfo.callback.info=this._monitorinfo;this._monitorinfo.dwData=e._gm.ObjectToPtr(this._monitorinfo);this._monitorinfo.callback.results=[];this._monitorinfo.callback.on("GlobalCallback",function i(x,w,y,A){if(this.ObjectToPtr_Verify(this.info,A)){var z=y.Deref(0,16).toBuffer();this.results.push({left:z.readInt32LE(0),top:z.readInt32LE(4),right:z.readInt32LE(8),bottom:z.readInt32LE(12)});var y=this.info.self._gm.CreateInteger();y.Val=1;return(y)}});if(e._user32.EnumDisplayMonitors(0,0,this._monitorinfo.callback,this._monitorinfo.dwData).Val==0){u("LastError="+e._kernel32.GetLastError().Val);return}else{v(this._monitorinfo.callback.results)}}))}}else{if(process.platform=="linux"){var a=false;try{if(require("user-sessions").isRoot()){a=true}}catch(b){}if(a){var r=getLibInfo("libX11");var t=getLibInfo("libXtst");var s=getLibInfo("libXext");var n;for(n in r){try{this._gm.CreateNativeProxy(r[n].path);Object.defineProperty(this,"Location_X11LIB",{value:r[n].path});break}catch(c){}}for(n in t){try{this._gm.CreateNativeProxy(t[n].path);Object.defineProperty(this,"Location_X11TST",{value:t[n].path});break}catch(c){}}for(n in s){try{this._gm.CreateNativeProxy(s[n].path);Object.defineProperty(this,"Location_X11EXT",{value:s[n].path});break}catch(c){}}}else{var g=require("fs");var d=g.readdirSync("/usr/lib");var f;for(var l in d){try{if(d[l].split("libX11.so.").length>1&&d[l].split(".").length==3){Object.defineProperty(this,"Location_X11LIB",{value:"/usr/lib/"+d[l]})}if(d[l].split("libXtst.so.").length>1&&d[l].split(".").length==3){Object.defineProperty(this,"Location_X11TST",{value:"/usr/lib/"+d[l]})}if(d[l].split("libXext.so.").length>1&&d[l].split(".").length==3){Object.defineProperty(this,"Location_X11EXT",{value:"/usr/lib/"+d[l]})}if(d[l].split("-linux-").length>1){f=g.readdirSync("/usr/lib/"+d[l]);for(j in f){if(f[j].split("libX11.so.").length>1&&f[j].split(".").length==3){Object.defineProperty(this,"Location_X11LIB",{value:"/usr/lib/"+d[l]+"/"+f[j]})}if(f[j].split("libXtst.so.").length>1&&f[j].split(".").length==3){Object.defineProperty(this,"Location_X11TST",{value:"/usr/lib/"+d[l]+"/"+f[j]})}if(f[j].split("libXext.so.").length>1&&f[j].split(".").length==3){Object.defineProperty(this,"Location_X11EXT",{value:"/usr/lib/"+d[l]+"/"+f[j]})}}}}catch(c){}}}Object.defineProperty(this,"kvm_x11_support",{value:(this.Location_X11LIB&&this.Location_X11TST&&this.Location_X11EXT)?true:false});if(this.Location_X11LIB){this._X11=this._gm.CreateNativeProxy(this.Location_X11LIB);this._X11.CreateMethod("XChangeProperty");this._X11.CreateMethod("XCloseDisplay");this._X11.CreateMethod("XCreateGC");this._X11.CreateMethod("XCreateWindow");this._X11.CreateMethod("XCreateSimpleWindow");this._X11.CreateMethod("XDefaultColormap");this._X11.CreateMethod("XDefaultScreen");this._X11.CreateMethod("XDrawLine");this._X11.CreateMethod("XDisplayHeight");this._X11.CreateMethod("XDisplayWidth");this._X11.CreateMethod("XFetchName");this._X11.CreateMethod("XFlush");this._X11.CreateMethod("XFree");this._X11.CreateMethod("XCreateGC");this._X11.CreateMethod("XGetWindowProperty");this._X11.CreateMethod("XInternAtom");this._X11.CreateMethod("XMapWindow");this._X11.CreateMethod({method:"XNextEvent",threadDispatch:true});this._X11.CreateMethod("XOpenDisplay");this._X11.CreateMethod("XRootWindow");this._X11.CreateMethod("XScreenCount");this._X11.CreateMethod("XScreenOfDisplay");this._X11.CreateMethod("XSelectInput");this._X11.CreateMethod("XSendEvent");this._X11.CreateMethod("XSetForeground");this._X11.CreateMethod("XSetFunction");this._X11.CreateMethod("XSetLineAttributes");this._X11.CreateMethod("XSetNormalHints");this._X11.CreateMethod("XSetSubwindowMode");this._X11.CreateMethod("XBlackPixel");this._X11.CreateMethod("XWhitePixel")}this.isUnity=function m(){return(process.env.XDG_CURRENT_DESKTOP=="Unity")};this.unDecorateWindow=function q(e,v){var i=this._gm.CreateVariable(40);var u=this._X11.XInternAtom(e,this._gm.CreateVariable("_MOTIF_WM_HINTS"),0);i.Deref(0,4).toBuffer().writeUInt32LE(1<<1);this._X11.XChangeProperty(e,v,u,u,32,0,i,5)};this.setWindowSizeHints=function p(e,w,z,A,v,i){var u=this._gm.CreateVariable(80);u.Deref(0,4).toBuffer().writeUInt32LE(PPosition|PSize);u.Deref(8,4).toBuffer().writeUInt32LE(z);u.Deref(12,4).toBuffer().writeUInt32LE(A);u.Deref(16,4).toBuffer().writeUInt32LE(v);u.Deref(20,4).toBuffer().writeUInt32LE(i);this._X11.XSetNormalHints(e,w,u)};this.setAlwaysOnTop=function o(e,i,u){var v=this._X11.XInternAtom(e,this._gm.CreateVariable("_NET_WM_STATE"),1);var w=this._X11.XInternAtom(e,this._gm.CreateVariable("_NET_WM_STATE_ABOVE"),1);var x=this._gm.CreateVariable(96);x.Deref(0,4).toBuffer().writeUInt32LE(33);x.Deref(48,4).toBuffer().writeUInt32LE(32);v.pointerBuffer().copy(x.Deref(40,8).toBuffer());x.Deref(56,8).toBuffer().writeUInt32LE(_NET_WM_STATE_ADD);w.pointerBuffer().copy(x.Deref(64,8).toBuffer());u.pointerBuffer().copy(x.Deref(32,8).toBuffer());this._X11.XSendEvent(e,i,0,SubstructureRedirectMask|SubstructureNotifyMask,x)};this.hideWindowIcon=function k(e,i,u){var v=this._X11.XInternAtom(e,this._gm.CreateVariable("_NET_WM_STATE"),1);var w=this._X11.XInternAtom(e,this._gm.CreateVariable("_NET_WM_STATE_SKIP_TASKBAR"),1);var x=this._gm.CreateVariable(96);x.Deref(0,4).toBuffer().writeUInt32LE(33);x.Deref(48,4).toBuffer().writeUInt32LE(32);v.pointerBuffer().copy(x.Deref(40,8).toBuffer());x.Deref(56,8).toBuffer().writeUInt32LE(_NET_WM_STATE_ADD);w.pointerBuffer().copy(x.Deref(64,8).toBuffer());u.pointerBuffer().copy(x.Deref(32,8).toBuffer());this._X11.XSendEvent(e,i,0,SubstructureRedirectMask|SubstructureNotifyMask,x)};this.getInfo=function h(){var e=this;return(new promise(function(x,w){var u=e._X11.XOpenDisplay(e._gm.CreateVariable(":0"));var A=e._X11.XScreenCount(u).Val;var y=[];for(var v=0;v<A;++v){var z=e._X11.XScreenOfDisplay(u,v);y.push({left:0,top:0,right:e._X11.XDisplayWidth(u,v).Val,bottom:e._X11.XDisplayHeight(u,v).Val,screen:z,screenId:v,display:u})}x(y)}))}}else{throw (process.platform+" not supported")}}}module.exports=new monitorinfo();