Get current active application in linux system using nwjs
Posted By : Vivek Joshi | 30-Jan-2018
For creating a desktop application for tracking the Linux users current active application in his system following steps are required.
step 1:- Install the X11 node module for listening to the window change event.
npm install x11 --save
and
nmp install x11-prop --save
step 2:- write the following code into an angular service to listen to the active window change event and get the currently active window name and process id.
(function() {
'use strict';
angular
.module('DesktopApp')
.service('ProcessLinux', ProcessLinux);
ProcessLinux.$inject = ['$q','$rootScope'];
function ProcessLinux($q,$rootScope){
this.initialize = initialize;
this.start=start;
var
_isInitialized = false,
_xClient,
_xClientDisplay;
var X11 = require('x11');
var X11prop = require('x11-prop');
var GetProperty = X11prop.get_property;
function initialize() {
var dfd = $q.defer();
if(!_isInitialized){
X11.createClient(function(err, display) {
_xClient = display.client;
_xClientDisplay = display;
_isInitialized = true;
dfd.resolve();
});
}
else{
dfd.resolve();
}
return dfd.promise;
}
}
function start() {
_xClient.ChangeWindowAttributes(_xClientDisplay.screen[0].root, {
eventMask: X11.eventMask.PropertyChange
});
_xClient.on('event', windowPropertyChangeHandler);
}
function windowPropertyChangeHandler(ev) {
if (ev.name == 'PropertyNotify') {
// check if the property change corresponds to the active window
// i.e is active window changed
_xClient.GetAtomName(ev.atom, function(err, name) {
if (name == '_NET_ACTIVE_WINDOW') {
handleActiveWindowChange(ev);
}
});
}
}
function handleActiveWindowChange(ev) {
var windowTitle,processId;
getWindowProcessIdAndTitle(ev).then(function(windowDetails){
windowTitle = windowDetails[1];
processId = windowDetails[0];
console.log("windowTitle",windowTitle);
console.log("process Id is ",processId);
})
}
function getWindowProcessIdAndTitle(ev) {
var deferred = $q.defer();
GetProperty(_xClient, ev.wid, ev.atom, function(err, data) {
if(!err){
var activeWindowData = data;
GetProperty(_xClient, activeWindowData[0], '_NET_WM_PID', function(err, processId) {
GetProperty(_xClient, activeWindowData[0], '_NET_WM_NAME', function(err, windowTitle) {
deferred.resolve([processId[0],windowTitle[0]]);
});
});
}
else{
throw err;
}
});
return deferred.promise;
}
})();
Description about code:
In the above code we created an
1) initialization() function :- In this function, we created a client object of x11 module which is used for listening event of changing the window in the system.
2) start() function :- In this function we listen active window change events and get the active window object and call the windowPropertyChangeHandler() function and testing event is window changed if it is, then we call another function handleActiveWindowChange() which is used for getting active window's process id and process name.
3) handleActiveWindowChange() function :- in this function we call another function name getWindowProcessIdAndTitle() which is used for getting the active window title and process id from the object it uses the
step 4:- For calling this service we have to create a Controller which is use for initialize the service and start listing window change events.
(function () {
'use strict';
angular
.module('DesktopApp')
.controller("HomeController", HomeController);
function HomeController(['ProcessLinux',function(ProcessLinux){
ProcessLinux.initialize().then(function(){
ProcessLinux.start();
});
}]);
})();
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vivek Joshi
Vivek is Web App Developer in Java Technology and also working on NodeJS.