Skip to content

Commit

Permalink
Revert "entirely remove Carbon, get all events directly through the e…
Browse files Browse the repository at this point in the history
…vent mach port"

This reverts commit 207c4d7.

..

..
  • Loading branch information
FelixKratz committed Sep 30, 2023
1 parent 893ed8c commit 0e4ccc1
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 86 deletions.
3 changes: 2 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CFLAGS = -std=c99 -Wall -Ofast -ffast-math -fvisibility=hidden -fno-common
LIBS = -framework AppKit \
LIBS = -framework Carbon \
-framework AppKit \
-framework CoreAudio \
-framework CoreWLAN \
-framework CoreVideo \
Expand Down
2 changes: 0 additions & 2 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,12 @@ void bar_manager_cancel_drag(struct bar_manager* bar_manager) {
}

void bar_manager_handle_mouse_entered_global(struct bar_manager* bar_manager) {
SLSSetBackgroundEventMask(g_connection, g_mouse_events);
bar_manager_custom_events_trigger(bar_manager,
COMMAND_SUBSCRIBE_MOUSE_ENTERED_GLOBAL,
NULL );
}

void bar_manager_handle_mouse_exited_global(struct bar_manager* bar_manager) {
SLSSetBackgroundEventMask(g_connection, 0);
bar_manager_custom_events_trigger(bar_manager,
COMMAND_SUBSCRIBE_MOUSE_EXITED_GLOBAL,
NULL );
Expand Down
85 changes: 65 additions & 20 deletions src/mouse.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
#include <Carbon/Carbon.h>
#include "mouse.h"

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;
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);
}
9 changes: 1 addition & 8 deletions src/mouse.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#pragma once
#include "event.h"

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);
void mouse_begin(void);
54 changes: 5 additions & 49 deletions src/sketchybar.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@
#define MINOR 16
#define PATCH 3

extern int SLSMainConnectionID(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);
extern CGError SLSGetCoalesceEventsMask(uint32_t cid, int64_t* mask_out);
extern CGError SLSCoalesceEventsInMask(uint32_t cid, int64_t mask);
extern int SLSMainConnectionID(void);
extern int RunApplicationEventLoop(void);

int g_connection;
CFTypeRef g_transaction;
Expand Down Expand Up @@ -178,19 +171,6 @@ 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);
Expand All @@ -215,6 +195,7 @@ 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);

Expand All @@ -230,33 +211,8 @@ int main(int argc, char **argv) {
initialize_media_events();

exec_config_file();
begin_receiving_config_change_events();
SLSSetEventMask(g_connection, g_mouse_events);

int64_t mask;
SLSGetCoalesceEventsMask(g_connection, &mask);
mask = (uint32_t)mask & 0xf67fff1f;
SLSCoalesceEventsInMask(g_connection, mask);

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();
begin_receiving_config_change_events();
RunApplicationEventLoop();
return 0;
}
2 changes: 0 additions & 2 deletions src/window.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "window.h"
#include "bar_manager.h"
#include "mouse.h"

extern struct bar_manager g_bar_manager;
extern int64_t g_disable_capture;
Expand Down Expand Up @@ -49,7 +48,6 @@ void window_create(struct window* window, CGRect frame) {
window->id = (uint32_t)id;
CFRelease(frame_region);

SLSSetWindowEventMask(g_connection, window->id, g_mouse_events);
SLSSetWindowResolution(g_connection, window->id, 2.0f);
SLSSetWindowTags(g_connection, window->id, &set_tags, 64);
SLSClearWindowTags(g_connection, window->id, &clear_tags, 64);
Expand Down
4 changes: 0 additions & 4 deletions src/window.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#pragma once
#include "misc/helpers.h"

extern CGError SLSSetWindowEventShape(int cid, int wid, CFTypeRef region);
extern CGError SLSSetWindowEventMask(int cid, int wid, int mask);
extern CGError SLSSetBackgroundEventMask(int cid, 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);
Expand Down

0 comments on commit 0e4ccc1

Please sign in to comment.