<commit_before>var EventEmitter = require('events').EventEmitter;
var util = require('util');
var os = require('os');
var x11 = require('x11');

var EWMH = function(X, root) {
    EventEmitter.call(this);
    this.X = X;
    this.root = root;
    this.X.on('event', this._handle_event.bind(this));
    X.GetWindowAttributes(root, function(err, attrs) {
        X.ChangeWindowAttributes(root, { eventMask: attrs.myEventMask | x11.eventMask.PropertyChange });
    });
};
util.inherits(EWMH, EventEmitter);

module.exports = EWMH;

EWMH.prototype.set_number_of_desktops = function(n, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(n, 0);
    this._change_property(this.root, '_NET_NUMBER_OF_DESKTOPS', this.X.atoms.CARDINAL, 32, raw, cb);
};

EWMH.prototype.set_current_desktop = function(d, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(d, 0);
    this._change_property(this.root, '_NET_CURRENT_DESKTOP', this.X.atoms.CARDINAL, 32, raw, cb);
};

EWMH.prototype.update_window_list = function(list, cb) {
    var length = list.length * 4;
    var raw = new Buffer(new Array(length));
    list.forEach(function(w, i) {
        raw.writeUInt32LE(w, i * 4);
    });

    this._change_property(this.root, '_NET_CLIENT_LIST', this.X.atoms.WINDOW, 32, raw, cb);
};

EWMH.prototype.set_pid = function(wid, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(process.pid);
    this.change_property(wid, '_NET_WM_PID', this.X.atoms.CARDINAL, 32, raw);
};

EWMH.prototype.set_hostname = function(wid, cb) {
    var hosname = os.hostname();
    this.change_property('WM_CLIENT_MACHINE', X.atoms.STRING, 8, hostname, cb);
};

EWMH.prototype._handle_event = function(ev) {
    var self = this;
    switch(ev.name) {
        case 'PropertyNotify':
            self.X.GetAtomName(ev.atom, function(err, name) {
                if (name === '_NET_ACTIVE_WINDOW') {
                    self.X.GetProperty(0, ev.window, ev.atom, self.X.atoms.WINDOW, 0, 4, function(err, prop) {
                        self.emit('ActiveWindowChange', prop.data.readUInt32LE(0));
                    });
                }
            });
        break;
    }
}

EWMH.prototype._change_property = function(wid, property, type, units, data, cb) {
    var self = this;
    this.X.InternAtom(false, property, function(err, atom) {
        if (!err) {<commit_msg>use callback parameter<commit_after>var EventEmitter = require('events').EventEmitter;
var util = require('util');
var os = require('os');
var x11 = require('x11');

var EWMH = function(X, root) {
    EventEmitter.call(this);
    this.X = X;
    this.root = root;
    this.X.on('event', this._handle_event.bind(this));
    X.GetWindowAttributes(root, function(err, attrs) {
        X.ChangeWindowAttributes(root, { eventMask: attrs.myEventMask | x11.eventMask.PropertyChange });
    });
};
util.inherits(EWMH, EventEmitter);

module.exports = EWMH;

EWMH.prototype.set_number_of_desktops = function(n, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(n, 0);
    this._change_property(this.root, '_NET_NUMBER_OF_DESKTOPS', this.X.atoms.CARDINAL, 32, raw, cb);
};

EWMH.prototype.set_current_desktop = function(d, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(d, 0);
    this._change_property(this.root, '_NET_CURRENT_DESKTOP', this.X.atoms.CARDINAL, 32, raw, cb);
};

EWMH.prototype.update_window_list = function(list, cb) {
    var length = list.length * 4;
    var raw = new Buffer(new Array(length));
    list.forEach(function(w, i) {
        raw.writeUInt32LE(w, i * 4);
    });

    this._change_property(this.root, '_NET_CLIENT_LIST', this.X.atoms.WINDOW, 32, raw, cb);
};

EWMH.prototype.set_pid = function(wid, cb) {
    var raw = new Buffer(4);
    raw.writeUInt32LE(process.pid);
    this.change_property(wid, '_NET_WM_PID', this.X.atoms.CARDINAL, 32, raw, cb);
};

EWMH.prototype.set_hostname = function(wid, cb) {
    var hosname = os.hostname();
    this.change_property('WM_CLIENT_MACHINE', X.atoms.STRING, 8, hostname, cb);
};

EWMH.prototype._handle_event = function(ev) {
    var self = this;
    switch(ev.name) {
        case 'PropertyNotify':
            self.X.GetAtomName(ev.atom, function(err, name) {
                if (name === '_NET_ACTIVE_WINDOW') {
                    self.X.GetProperty(0, ev.window, ev.atom, self.X.atoms.WINDOW, 0, 4, function(err, prop) {
                        self.emit('ActiveWindowChange', prop.data.readUInt32LE(0));
                    });
                }
            });
        break;
    }
}

EWMH.prototype._change_property = function(wid, property, type, units, data, cb) {
    var self = this;
    this.X.InternAtom(false, property, function(err, atom) {
        if (!err) {
{'fraction_non_alphanumeric': 0.15166985747713252, 'fraction_numerical': 0.015315890236119975, 'mean_word_length': 3.1684397163120566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2164183', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1676, 'n_words': 307}