question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PropertyNotify event.window is null

See original GitHub issue

I’m using this code snippet based on your post here

main.js

var x11 = require('x11');
x11.createClient(function (err, display) {
    var X = display.client;
    X.ChangeWindowAttributes(display.screen[0].root, {eventMask: x11.eventMask.PropertyChange});
    X.on('event', function (ev) {
        if (ev.name === 'PropertyNotify') {
            X.GetAtomName(ev.atom, function (err, name) {
                if (name === '_NET_ACTIVE_WINDOW') {
                    X.GetProperty(0, ev.window, ev.atom, X.atoms.WINDOW, 0, 4, function (err, prop) {
                        console.log('New active window:' + prop.data.readUInt32LE(0));
                    });
                }
            });
        }
    });
});

However, it seems that ev.window is null, as well as prop:

cj@lighthouse:~/workspace/moodring$ node main.js 
/home/cj/workspace/moodring/main.js:10
                        console.log('New active window:' + prop.data.readUInt32LE(0));
                                                               ^

TypeError: Cannot read property 'data' of undefined
    at /home/cj/workspace/moodring/main.js:10:64
    at ReadFixedRequest.callback (/home/cj/workspace/moodring/node_modules/x11/lib/xcore.js:490:39)
    at ReadFixedRequest.execute (/home/cj/workspace/moodring/node_modules/x11/lib/unpackstream.js:41:10)
    at UnpackStream.resume (/home/cj/workspace/moodring/node_modules/x11/lib/unpackstream.js:165:30)
    at UnpackStream.write (/home/cj/workspace/moodring/node_modules/x11/lib/unpackstream.js:102:10)
    at Socket.<anonymous> (/home/cj/workspace/moodring/node_modules/x11/lib/xcore.js:88:21)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)

What’s the proper way to listen for changes in the active (focused) window? I’m running Ubuntu 16.04 with Unity.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
specialblendcommented, Jun 16, 2017

@santigimeno that works perfectly, thank you so much! and thank you @sidorares for your help as well.

I’m quite new to node.js but I will be using both libraries (node-x11 and x11-prop) extensively and I’m happy to contribute to both as I learn more. Regards

1reaction
santigimenocommented, Jun 16, 2017

Back to the OP issue. @SpecialBlend, it’s normal the wid you get is the root as the PropertyNotifythe event comes from the root window. To get the active window wid you need to get the _NET_ACTIVE_WINDOW property and then use that wid to get WM_CLASS, WM_NAME, etc. As an example, something like this would work: (I’m using https://github.com/santigimeno/node-x11-prop, that is just a fancy wrapper over GetProperty()):

var x11 = require('x11');
var x11prop = require('x11-prop');
var get_property = x11prop.get_property;


x11.createClient(function (err, display) {
    var X = display.client;
    X.ChangeWindowAttributes(display.screen[0].root, {eventMask: x11.eventMask.PropertyChange});
    X.on('event', function (ev) {
        console.log(ev);
        if (ev.name === 'PropertyNotify') {
            X.GetAtomName(ev.atom, function (err, name) {
                if (name === '_NET_ACTIVE_WINDOW') {
                   // get active window
                   get_property(X, ev.wid, '_NET_ACTIVE_WINDOW', 'WINDOW', function(err, wid) {
                       if (!err) {
                           console.log('_NET_ACTIVE_WINDOW: ' + wid);
                       }

                       if (wid) {
                           get_property(X, wid, 'WM_CLASS', 'STRING', function(err, data) {
                               if (!err) {
                                   console.log('WM_CLASS: ' + data);
                              }
                          });
                       }
                   });
                }
            });
        }
    });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I capture minimize and maximize events in XWindows?
The program shapes the events into what I consider the important states of a window, that is, maximized (occupying the whole screen), ...
Read more >
web.mit.edu/ghudson/dev/nokrb/third/metacity/src/d...
HAVE_RENDER */ /* Create the leader window here. Set its properties and * use the timestamp from one of the PropertyNotify events *...
Read more >
XGetWindowProperty - X.Org
The XListProperties function returns a pointer to an array of atom properties that are defined for the specified window or returns NULL if...
Read more >
window.event is null when dispatching an event with ... - MSDN
When I use dispatchEvent from the webbrowser control the handler is executed but window.event is null causing a javascript error.
Read more >
desktop_window_tree_host_x11.cc - Google Git
#include "ui/events/devices/x11/device_data_manager_x11.h" ... return host ? host->window()->GetProperty(kHostForRootWindow) : NULL;.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found