summaryrefslogtreecommitdiff
path: root/src/devices/TouchDevice.cpp
blob: 64c521a23f99f77f8f45fd350877460414d285e7 (plain)
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
#include "TouchDevice.hpp"
#include "../defines.hpp"

SP<CTouchDevice> CTouchDevice::create(wlr_touch* touch) {
    SP<CTouchDevice> pTouch = SP<CTouchDevice>(new CTouchDevice(touch));

    pTouch->self = pTouch;

    return pTouch;
}

CTouchDevice::CTouchDevice(wlr_touch* touch_) : touch(touch_) {
    if (!touch)
        return;

    // clang-format off
    hyprListener_destroy.initCallback(&touch->base.events.destroy, [this] (void* owner, void* data) {
        events.destroy.emit();
        disconnectCallbacks();
        touch = nullptr;
    }, this, "CTouchDevice");

    hyprListener_down.initCallback(&touch->events.down, [this] (void* owner, void* data) {
        auto E = (wlr_touch_down_event*)data;

        touchEvents.down.emit(SDownEvent{
            .timeMs  = E->time_msec,
            .touchID = E->touch_id,
            .pos     = {E->x, E->y},
            .device  = self.lock(),
        });
    }, this, "CTouchDevice");

    hyprListener_up.initCallback(&touch->events.up, [this] (void* owner, void* data) {
        auto E = (wlr_touch_up_event*)data;

        touchEvents.up.emit(SUpEvent{
            .timeMs  = E->time_msec,
            .touchID = E->touch_id
        });
    }, this, "CTouchDevice");

    hyprListener_motion.initCallback(&touch->events.motion, [this] (void* owner, void* data) {
        auto E = (wlr_touch_motion_event*)data;

        touchEvents.motion.emit(SMotionEvent{
            .timeMs  = E->time_msec,
            .touchID = E->touch_id,
            .pos     = {E->x, E->y},
        });
    }, this, "CTouchDevice");

    hyprListener_cancel.initCallback(&touch->events.cancel, [this] (void* owner, void* data) {
        auto E = (wlr_touch_cancel_event*)data;

        touchEvents.cancel.emit(SCancelEvent{
            .timeMs  = E->time_msec,
            .touchID = E->touch_id
        });
    }, this, "CTouchDevice");

    hyprListener_frame.initCallback(&touch->events.frame, [this] (void* owner, void* data) {
        touchEvents.frame.emit();
    }, this, "CTouchDevice");

    // clang-format on

    deviceName = touch->base.name ? touch->base.name : "UNKNOWN";
}

bool CTouchDevice::isVirtual() {
    return false;
}

wlr_touch* CTouchDevice::wlr() {
    return touch;
}

void CTouchDevice::disconnectCallbacks() {
    hyprListener_destroy.removeCallback();
    hyprListener_down.removeCallback();
    hyprListener_up.removeCallback();
    hyprListener_motion.removeCallback();
    hyprListener_cancel.removeCallback();
    hyprListener_frame.removeCallback();
}