File: TabletInput_macos.mm

package info (click to toggle)
vimix 0.8.5c%2Bgit20251123%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,340 kB
  • sloc: cpp: 94,323; ansic: 34,427; makefile: 373; objc: 97; xml: 83; sh: 45
file content (122 lines) | stat: -rw-r--r-- 3,901 bytes parent folder | download
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
/*
 * This file is part of vimix - video live mixer
 *
 * **Copyright** (C) 2019-2025 Bruno Herbelin <bruno.herbelin@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
**/

#ifdef APPLE

#include "TabletInput.h"
#include "Log.h"
#import <Cocoa/Cocoa.h>

TabletInput::TabletInput()
    : data_({0.0f, false, 0.0f, 0.0f, false, false})
    , active_(false)
    , monitor_(nullptr)
{
}

TabletInput::~TabletInput()
{
    terminate();
}

bool TabletInput::init()
{
    // Create event monitor for tablet events
    NSEventMask mask = NSEventMaskTabletPoint |
                       NSEventMaskTabletProximity |
                       NSEventMaskLeftMouseDown |
                       NSEventMaskLeftMouseUp |
                       NSEventMaskLeftMouseDragged;

    // Capture reference to data_ for the block
    TabletData *dataPtr = &data_;

    id monitor = [NSEvent addLocalMonitorForEventsMatchingMask:mask
        handler:^NSEvent *(NSEvent *event) {

            // Check for tablet point events
            if (event.type == NSEventTypeTabletPoint ||
                (event.subtype == NSEventSubtypeTabletPoint)) {

                dataPtr->has_pressure = true;
                dataPtr->pressure = event.pressure;
                dataPtr->tilt_x = event.tilt.x;
                dataPtr->tilt_y = event.tilt.y;
                dataPtr->tip_down = (event.pressure > 0.0f);
                dataPtr->in_proximity = true;
            }
            // Handle proximity events
            else if (event.type == NSEventTypeTabletProximity) {
                dataPtr->in_proximity = event.isEnteringProximity;
                if (!dataPtr->in_proximity) {
                    dataPtr->pressure = 0.0f;
                    dataPtr->tilt_x = 0.0f;
                    dataPtr->tilt_y = 0.0f;
                    dataPtr->tip_down = false;
                }
            }
            // Fallback to regular mouse events if tablet is in proximity
            else if (event.type == NSEventTypeLeftMouseDown) {
                if (dataPtr->in_proximity && dataPtr->pressure == 0.0f) {
                    // Set minimal pressure if tablet is near but not reporting pressure
                    dataPtr->pressure = 0.1f;
                    dataPtr->tip_down = true;
                }
            }
            else if (event.type == NSEventTypeLeftMouseUp) {
                if (dataPtr->in_proximity) {
                    dataPtr->tip_down = false;
                    if (!dataPtr->in_proximity) {
                        dataPtr->pressure = 0.0f;
                    }
                }
            }

            return event;
        }];

    monitor_ = (__bridge_retained void*)monitor;
    active_ = true;

    Log::Info("TabletInput: macOS tablet input initialized (NSEvent)");
    return true;
}

void TabletInput::pollEvents()
{
    // Events are handled automatically by the monitor callback
    // Nothing to do here for macOS
}

void TabletInput::terminate()
{
    if (monitor_) {
        id monitor = (__bridge_transfer id)monitor_;
        [NSEvent removeMonitor:monitor];
        monitor_ = nullptr;
    }
    data_.pressure = 0.0f;
    data_.tilt_x = 0.0f;
    data_.tilt_y = 0.0f;
    data_.in_proximity = false;
    data_.tip_down = false;
    active_ = false;
}

#endif // APPLE