entirely remove Carbon, get all events directly through the event mach port

This commit is contained in:
Felix Kratz 2023-06-28 22:00:30 +02:00
parent e05fca28cf
commit 207c4d7b58
6 changed files with 79 additions and 85 deletions

View file

@ -1,6 +1,5 @@
CFLAGS = -std=c99 -Wall -Ofast -ffast-math -fvisibility=hidden -fno-common
LIBS = -framework Carbon \
-framework AppKit \
LIBS = -framework AppKit \
-framework CoreAudio \
-framework CoreWLAN \
-framework IOKit \

View file

@ -1,68 +1,23 @@
#include <Carbon/Carbon.h>
#include "mouse.h"
static const EventTypeSpec mouse_events [] = {
{ kEventClassMouse, kEventMouseDown },
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseDragged },
{ kEventClassMouse, kEventMouseEntered },
{ kEventClassMouse, kEventMouseExited },
{ kEventClassMouse, kEventMouseWheelMoved }
};
static pascal OSStatus mouse_handler(EventHandlerCallRef next, EventRef e, void *data) {
switch (GetEventKind(e)) {
case kEventMouseUp: {
CGEventRef cg_event = CopyEventCGEvent(e);
struct event event = { (void *) cg_event, MOUSE_UP };
event_post(&event);
CFRelease(cg_event);
break;
}
case kEventMouseDragged: {
CGEventRef cg_event = CopyEventCGEvent(e);
struct event event = { (void *) cg_event, MOUSE_DRAGGED };
event_post(&event);
CFRelease(cg_event);
break;
}
case kEventMouseEntered: {
CGEventRef cg_event = CopyEventCGEvent(e);
struct event event = { (void *) cg_event, MOUSE_ENTERED };
event_post(&event);
CFRelease(cg_event);
break;
}
case kEventMouseExited: {
CGEventRef cg_event = CopyEventCGEvent(e);
struct event event = { (void *) cg_event, MOUSE_EXITED };
event_post(&event);
CFRelease(cg_event);
break;
}
case kEventMouseWheelMoved: {
CGEventRef cg_event = CopyEventCGEvent(e);
struct event event = { (void *) cg_event, MOUSE_SCROLLED };
event_post(&event);
CFRelease(cg_event);
break;
}
default:
break;
}
return CallNextEventHandler(next, e);
}
void mouse_begin(void) {
InstallEventHandler(GetEventDispatcherTarget(),
NewEventHandlerUPP(mouse_handler),
GetEventTypeCount(mouse_events),
mouse_events, 0, 0);
bool mouse_handle_event(CGEventType type, CGEventRef cg_event) {
if (type == kCGEventOtherMouseUp
|| type == kCGEventLeftMouseUp
|| type == kCGEventRightMouseUp) {
struct event event = { (void *) cg_event, MOUSE_UP };
event_post(&event);
} else if (type == kCGEventLeftMouseDragged) {
struct event event = { (void *) cg_event, MOUSE_DRAGGED };
event_post(&event);
} else if (type == 0x8) {
struct event event = { (void *) cg_event, MOUSE_ENTERED };
event_post(&event);
} else if (type == 0x9) {
struct event event = { (void *) cg_event, MOUSE_EXITED };
event_post(&event);
} else if (type == kCGEventScrollWheel) {
struct event event = { (void *) cg_event, MOUSE_SCROLLED };
event_post(&event);
} else return false;
return true;
}

View file

@ -1,4 +1,11 @@
#pragma once
#include "event.h"
void mouse_begin(void);
static const CGEventMask g_mouse_events
= CGEventMaskBit(kCGEventLeftMouseUp)
| CGEventMaskBit(kCGEventRightMouseUp)
| CGEventMaskBit(kCGEventOtherMouseUp)
| CGEventMaskBit(kCGEventLeftMouseDragged)
| CGEventMaskBit(kCGEventScrollWheel);
bool mouse_handle_event(CGEventType type, CGEventRef cg_event);

View file

@ -28,9 +28,14 @@
#define MINOR 15
#define PATCH 2
extern CGError SLSRegisterNotifyProc(void* callback, uint32_t event, void* context);
extern int SLSMainConnectionID(void);
extern int RunApplicationEventLoop(void);
extern CGError SLSRegisterNotifyProc(void* callback, uint32_t event, void* context);
extern CGError SLSGetEventPort(int cid, mach_port_t* port_out);
extern CGEventRef SLEventCreateNextEvent(int cid);
extern void _CFMachPortSetOptions(CFMachPortRef mach_port, int options);
extern CGError SLSSetBackgroundEventMask(int cid, int mask);
extern CGError SLSSetEventMask(int cid, int mask);
int g_connection;
CFTypeRef g_transaction;
@ -170,6 +175,19 @@ void system_events(uint32_t event, void* data, size_t data_length, void* context
}
}
void mach_port_callback(CFMachPortRef port, void* message, CFIndex size, void* context) {
int cid = g_connection;
CGEventRef cg_event = SLEventCreateNextEvent(cid);
if (!cg_event) return;
do {
CGEventType type = CGEventGetType(cg_event);
if (type != 0xd) mouse_handle_event(type, cg_event);
CFRelease(cg_event);
cg_event = SLEventCreateNextEvent(cid);
} while (cg_event != NULL);
}
int main(int argc, char **argv) {
snprintf(g_name, sizeof(g_name), "%s", basename(argv[0]));
if (argc > 1) parse_arguments(argc, argv);
@ -189,7 +207,6 @@ int main(int argc, char **argv) {
workspace_event_handler_init(&g_workspace_context);
bar_manager_init(&g_bar_manager);
mouse_begin();
display_begin();
workspace_event_handler_begin(&g_workspace_context);
@ -205,8 +222,29 @@ int main(int argc, char **argv) {
initialize_media_events();
exec_config_file();
begin_receiving_config_change_events();
RunApplicationEventLoop();
SLSSetBackgroundEventMask(g_connection, g_mouse_events);
SLSSetEventMask(g_connection, g_mouse_events);
mach_port_t port;
CGError error = SLSGetEventPort(g_connection, &port);
CFMachPortRef cf_mach_port = NULL;
CFRunLoopSourceRef source = NULL;
if (error == kCGErrorSuccess) {
cf_mach_port = CFMachPortCreateWithPort(NULL,
port,
mach_port_callback,
NULL,
false );
_CFMachPortSetOptions(cf_mach_port, 0x40);
source = CFMachPortCreateRunLoopSource(NULL, cf_mach_port, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
source,
kCFRunLoopDefaultMode);
}
CFRunLoopRun();
return 0;
}

View file

@ -1,5 +1,6 @@
#include "window.h"
#include "bar_manager.h"
#include "mouse.h"
extern struct bar_manager g_bar_manager;
extern int64_t g_disable_capture;
@ -52,18 +53,9 @@ void window_create(struct window* window, CGRect frame) {
SLSSetWindowTags(g_connection, window->id, &set_tags, 64);
SLSClearWindowTags(g_connection, window->id, &clear_tags, 64);
SLSSetWindowOpacity(g_connection, window->id, 0);
SLSSetWindowEventMask(g_connection, window->id, g_mouse_events);
const void* keys[] = { CFSTR("CGWindowContextShouldUseCA") };
const void* values[] = { kCFBooleanTrue };
CFDictionaryRef dict = CFDictionaryCreate(NULL,
keys,
values,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
window->context = SLWindowContextCreate(g_connection, window->id, dict);
CFRelease(dict);
window->context = SLWindowContextCreate(g_connection, window->id, NULL);
CGContextSetInterpolationQuality(window->context, kCGInterpolationNone);
window->needs_move = false;

View file

@ -1,7 +1,10 @@
#pragma once
#include "misc/helpers.h"
CFTypeRef SLSTransactionCreate(int cid);
extern CGError SLSSetWindowEventShape(int cid, int wid, CFTypeRef region);
extern CGError SLSSetWindowEventMask(int cid, int wid, int mask);
extern CFTypeRef SLSTransactionCreate(int cid);
extern CGError SLSTransactionOrderWindow(CFTypeRef transaction, uint32_t wid, int mode, uint32_t relativeToWID);
extern CGError SLSTransactionSetWindowLevel(CFTypeRef transaction, uint32_t wid, int level);
extern CGError SLSTransactionSetWindowShape(CFTypeRef transaction, uint32_t wid, float x_offset, float y_offset, CFTypeRef shape);