Author: smaisonneuve Date: 2015-04-27 09:48:55 +0000 (Mon, 27 Apr 2015) New Revision: 1257 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1257 Log: [Wit] - Timeline: register events on active window so that we can listen to window name property change Modified: wit/js/services/UserActivityService.js Modified: wit/js/services/UserActivityService.js =================================================================== --- wit/js/services/UserActivityService.js 2015-04-27 09:22:05 UTC (rev 1256) +++ wit/js/services/UserActivityService.js 2015-04-27 09:48:55 UTC (rev 1257) @@ -27,7 +27,7 @@ } /* Inspired by : -http://stackoverflow.com/questions/19840459/linux-get-notification-on-focuse... window-change/19847998#19847998 +http://stackoverflow.com/questions/19840459/linux-get-notification-on-focuse... */ x11.createClient(function(err, display) { var X = display.client; @@ -38,11 +38,11 @@ // Init windows session with the currently active window - updateWindowSessions(X, display.screen[0].root) + addWindowSession(X, display.screen[0].root) // Listen to client display events to identify when the currently active window has change - X.on('event', onActiveWindowChange.bind(null, X)); + X.on('event', onActiveWindowChange.bind(null, X)); }); @@ -54,22 +54,51 @@ // if the property change refers to the currently active window, retrieve new active window name if (name == '_NET_ACTIVE_WINDOW') { - updateWindowSessions(X, ev.wid) + + registerWindowEvents(X, ev.wid); + addWindowSession(X, ev.wid); + + } else if (name == '_NET_WM_NAME') { + + updateWindowSession(ev.wid); } }); } }; -var updateWindowSessions = function (X, wid) { - return getWindowId(X, wid) +var registerWindowEvents = function (X, parentId) { + getActiveWindowId(X, parentId) + .then(function (wid) { + X.ChangeWindowAttributes(wid, { + eventMask:x11.eventMask.PropertyChange + }); + }); +}; + +var addWindowSession = function (X, parentId) { + return getActiveWindowId(X, parentId) .then(getWindowName) .then(createNewSession); }; -var getWindowId = function (X, wid) { +var updateWindowSession = function (wid) { + return getWindowName(wid) + .then(function (windowName) { + + // Let's create a new seesion but with same color attributes as the last one, since the active window has not change, only some of its attibutes + + var last = windowSessions[windowSessions.length - 1]; + if (last.name != windowName) { + createNewSession(windowName, last.color); + } + + }); +}; + +var getActiveWindowId = function (X, parentId) { return new Promise(function (resolve, reject) { - X.GetProperty(0, wid, _NET_ACTIVE_WINDOW, X.atoms.WINDOW, 0, 4, function(err, prop) { + X.GetProperty(0, parentId, _NET_ACTIVE_WINDOW, X.atoms.WINDOW, 0, 4, function(err, prop) { if (err) { console.log("Couldn't retrieve active window property"); @@ -103,7 +132,7 @@ }); }; -var createNewSession = function (windowName) { +var createNewSession = function (windowName, color, startDate) { if (windowSessions.length) { var now = Date.now(); var last = windowSessions[windowSessions.length - 1]; @@ -118,8 +147,8 @@ windowSessions.push({ name: windowName, - color: prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), - startDate: moment() + color: color || prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), + startDate: startDate || moment() }); };
participants (1)
-
smaisonneuveďź users.nuiton.org