File: pointerTracker.js

package info (click to toggle)
cinnamon 6.4.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,300 kB
  • sloc: javascript: 54,298; ansic: 51,499; python: 21,971; xml: 2,803; sh: 96; makefile: 27; perl: 13
file content (123 lines) | stat: -rw-r--r-- 3,682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const Gio = imports.gi.Gio;
const Main = imports.ui.main;
const Clutter = imports.gi.Clutter;

function PointerTracker(){
    this._init();
}

PointerTracker.prototype = {
    _init: function() {
        let [lastPointerX, lastPointerY] = global.get_pointer();

        this.hasMoved = function() {
            let [pointerX, pointerY] = global.get_pointer();
            try {
                return !(pointerX == lastPointerX && pointerY == lastPointerY);
            } finally {
                [lastPointerX, lastPointerY] = [pointerX, pointerY];
            }
        };
        this.getPosition = function() {
            [lastPointerX, lastPointerY] = global.get_pointer();
            return [lastPointerX, lastPointerY];
        };
        this.setPosition = function(x, y) {
            let [pointerX, pointerY] = global.get_pointer();

            const seat = Clutter.get_default_backend().get_default_seat();
            seat.warp_pointer(Math.round(x), Math.round(y));

            [lastPointerX, lastPointerY] = [pointerX, pointerY];
        };
    }
};


var PointerSwitcher = class {
    constructor(wm) {
        this.settings = new Gio.Settings({ schema_id: "org.cinnamon.desktop.keybindings" });
        this.settings.connect("changed", (settings, key) => {
            if (["pointer-next-monitor", "pointer-previous-monitor"].includes(key)) {
                this.update_settings();
            }
        });

        this.update_settings()
    }

    update_settings() {
        Main.keybindingManager.addHotKeyArray(
            "pointer-next-monitor",
            this.settings.get_strv("pointer-next-monitor"),
            () => this.next_monitor()
        );

        Main.keybindingManager.addHotKeyArray(
            "pointer-previous-monitor",
            this.settings.get_strv("pointer-previous-monitor"),
            () => this.previous_monitor()
        );
    }

    get_normalized_pointer_position_for_monitor(index) {
        let [global_x, global_y, mods] = global.get_pointer();
        const monitor = Main.layoutManager.monitors[index];

        let rx = global_x - monitor.x;
        let ry = global_y - monitor.y;

        let nx = rx / monitor.width;
        let ny = ry / monitor.height;

        if (nx < 0 || nx > 1.0)
            nx = 0.5;
        if (ny < 0 || ny > 1.0)
            ny = 0.5;

        return [nx, ny];
    }

    get_real_pointer_position(index, nx, ny) {
        const monitor = Main.layoutManager.monitors[index];

        let real_x = (nx * monitor.width) + monitor.x;
        let real_y = (nx * monitor.height) + monitor.y;

        return [real_x, real_y];
    }

    next_monitor() {
        const current = global.display.get_current_monitor();
        const max = global.display.get_n_monitors() - 1;
        const new_mon = current + 1 <= max ? current + 1 : 0;

        this.move_from_to_monitor(current, new_mon);
    }

    previous_monitor() {
        const current = global.display.get_current_monitor();
        const max = global.display.get_n_monitors() - 1;
        const new_mon = current - 1 >= 0 ? current - 1 : max;

        this.move_from_to_monitor(current, new_mon);
    }

    move_from_to_monitor(from_i, to_i) {
        const center = global.settings.get_boolean("center-warped-pointer")

        let nx, ny;

        if (global.settings.get_boolean("center-warped-pointer")) {
            nx = ny = 0.5;
        } else {
            [nx, ny] = this.get_normalized_pointer_position_for_monitor(from_i);
        }

        let [x, y] = this.get_real_pointer_position(to_i, nx, ny);

        global.set_pointer(x, y);
    }
};