diff options
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | src/mtouch.c | 24 | ||||
-rw-r--r-- | src/mtouch.h | 58 | ||||
-rw-r--r-- | src/skhd.c | 33 |
4 files changed, 116 insertions, 1 deletions
@@ -1,4 +1,4 @@ -FRAMEWORKS = -framework Carbon -framework CoreAudio +FRAMEWORKS = -framework Carbon -framework CoreAudio -F/System/Library/PrivateFrameworks -framework MultitouchSupport BUILD_PATH = ./bin BUILD_FLAGS = -std=c99 -Wall -g -O0 SKHD_SRC = ./src/skhd.c diff --git a/src/mtouch.c b/src/mtouch.c new file mode 100644 index 0000000..5c71a3b --- /dev/null +++ b/src/mtouch.c @@ -0,0 +1,24 @@ +#include "mtouch.h" + +struct cached_finger_data *cached_finger_data[256]; + +void process_cached_finger_data(int identifier) +{ + printf("-- begin processing finger (%d) --\n", identifier); + int frame_count = buf_len(cached_finger_data[identifier]); + for (int i = 0; i < frame_count; ++i) { + struct cached_finger_data cached_data = cached_finger_data[identifier][i]; + printf("finger: %d, pos: (%.5f, %.5f), pressure: %.5f\n", + cached_data.id, + cached_data.pos.x, cached_data.pos.y, + cached_data.pressure); + } + printf("-- end processing (%d frames) --\n", frame_count); +} + +void multitouch_begin(struct multitouch *multitouch, multitouch_callback *callback) +{ + multitouch->dev = MTDeviceCreateDefault(); + MTRegisterContactFrameCallback(multitouch->dev, callback); + MTDeviceStart(multitouch->dev, 0); +} diff --git a/src/mtouch.h b/src/mtouch.h new file mode 100644 index 0000000..70ebd9f --- /dev/null +++ b/src/mtouch.h @@ -0,0 +1,58 @@ +#ifndef SKHD_MTHOUCH_H +#define SKHD_MTHOUCH_H + +struct mt_point; +struct mt_readout; +struct finger; + +#define MULTITOUCH_CALLBACK(name) int name(int device, struct finger *data, int num_fingers, double timestamp, int frame) +typedef MULTITOUCH_CALLBACK(multitouch_callback); + +typedef void *MTDeviceRef; +typedef int (*MTContactCallbackFunction)(int, struct finger *, int, double, int); + +extern MTDeviceRef MTDeviceCreateDefault(); +extern void MTRegisterContactFrameCallback(MTDeviceRef, MTContactCallbackFunction); +extern void MTDeviceStart(MTDeviceRef, int); + +struct mt_point +{ + float x; + float y; +}; + +struct mt_readout +{ + struct mt_point pos; + struct mt_point vel; +}; + +struct finger { + int frame; + double timestamp; + int identifier, state, foo3, foo4; + struct mt_readout normalized; + float size; + int zero1; + float angle, major_axis, minor_axis; + struct mt_readout mm; + int zero2[2]; + float unk2; +}; + +struct cached_finger_data +{ + int id; + struct mt_point pos; + float pressure; +}; + +struct multitouch +{ + MTDeviceRef dev; +}; + +void process_cached_finger_data(int identifier); +void multitouch_begin(struct multitouch *multitouch, multitouch_callback *callback); + +#endif @@ -25,6 +25,7 @@ #include "parse.h" #include "hotkey.h" #include "synthesize.h" +#include "mtouch.h" #include "hotload.c" #include "event_tap.c" @@ -34,6 +35,7 @@ #include "parse.c" #include "hotkey.c" #include "synthesize.c" +#include "mtouch.c" extern CFDictionaryRef CGSCopyCurrentSessionDictionary(void); extern bool CGSIsSecureEventInputSet(void); @@ -178,6 +180,34 @@ internal EVENT_TAP_CALLBACK(key_handler) return event; } +internal MULTITOUCH_CALLBACK(touch_handler) +{ + for (int i = 0; i < num_fingers; ++i) { + struct finger *finger = &data[i]; + struct cached_finger_data cached_data = { + .id = finger->identifier, + .pos = { + .x = finger->normalized.pos.x, + .y = finger->normalized.pos.y + }, + .pressure = finger->size + }; + + if (!cached_finger_data[finger->identifier]) { + printf("finger %d pressed\n", finger->identifier); + } + buf_push(cached_finger_data[finger->identifier], cached_data); + + if (finger->size == 0.0f) { + printf("finger %d released\n", finger->identifier); + process_cached_finger_data(finger->identifier); + buf_free(cached_finger_data[finger->identifier]); + cached_finger_data[finger->identifier] = NULL; + } + } + return 0; +} + internal void sigusr1_handler(int signal) { @@ -435,6 +465,9 @@ int main(int argc, char **argv) event_tap.mask = (1 << kCGEventKeyDown) | (1 << NX_SYSDEFINED); event_tap_begin(&event_tap, key_handler); END_SCOPED_TIMED_BLOCK(); + + struct multitouch multitouch; + multitouch_begin(&multitouch, touch_handler); END_SCOPED_TIMED_BLOCK(); CFRunLoopRun(); |