completely new mouse system

This commit is contained in:
FelixKratz 2021-09-01 15:32:00 +02:00
parent f515f748a6
commit 0471461571
7 changed files with 34 additions and 115 deletions

View file

@ -154,24 +154,14 @@ out:
return EVENT_SUCCESS;
}
bool is_bar_at_point(CGPoint point) {
CGPoint window_point;
uint32_t window_id;
int window_cid;
SLSFindWindowByGeometry(g_connection, 0, 1, 0, &point, &window_point, &window_id, &window_cid);
return window_cid == g_connection;
}
static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP) {
CGPoint point = CGEventGetLocation(context);
if (!is_bar_at_point(point)) return EVENT_SUCCESS;
uint32_t sid = mission_control_index(display_space_id(display_manager_active_display_id()));
debug("EVENT_HANDLER_MOUSE_UP: S#%d (x: %.0f, y: %.0f) -> ", sid, point.x, point.y);
struct bar_item* bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, sid);
debug("item: %s\n", bar_item ? bar_item->name : "NULL");
bar_item_on_click(bar_item);
CFRelease(context);
return EVENT_SUCCESS;
}

View file

@ -1,61 +0,0 @@
#include "event_tap.h"
extern struct event_loop g_event_loop;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wswitch"
static EVENT_TAP_CALLBACK(mouse_handler)
{
switch (type) {
case kCGEventTapDisabledByTimeout:
case kCGEventTapDisabledByUserInput: {
struct event_tap *event_tap = (struct event_tap *) reference;
CGEventTapEnable(event_tap->handle, 1);
} break;
case kCGEventLeftMouseUp:
case kCGEventRightMouseUp: {
struct event *event = event_create(&g_event_loop, MOUSE_UP, (void *) CFRetain(cgevent));
event_loop_post(&g_event_loop, event);
} break;
}
return cgevent;
}
#pragma clang diagnostic pop
bool event_tap_enabled(struct event_tap *event_tap)
{
bool result = (event_tap->handle && CGEventTapIsEnabled(event_tap->handle));
return result;
}
bool event_tap_begin(struct event_tap *event_tap, uint32_t mask, event_tap_callback *callback)
{
event_tap->mask = mask;
event_tap->handle = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
event_tap->mask,
callback,
event_tap);
bool result = event_tap_enabled(event_tap);
if (result) {
event_tap->runloop_source = CFMachPortCreateRunLoopSource(NULL, event_tap->handle, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), event_tap->runloop_source, kCFRunLoopCommonModes);
}
return result;
}
void event_tap_end(struct event_tap *event_tap)
{
if (event_tap_enabled(event_tap)) {
CGEventTapEnable(event_tap->handle, false);
CFMachPortInvalidate(event_tap->handle);
CFRunLoopRemoveSource(CFRunLoopGetMain(), event_tap->runloop_source, kCFRunLoopCommonModes);
CFRelease(event_tap->runloop_source);
CFRelease(event_tap->handle);
event_tap->handle = NULL;
}
}

View file

@ -1,32 +0,0 @@
#ifndef EVENT_TAP_H
#define EVENT_TAP_H
#define EVENT_MASK_MOUSE (1 << kCGEventLeftMouseUp) | \
(1 << kCGEventRightMouseUp)
#define EVENT_MASK_ALT kCGEventFlagMaskAlternate
#define EVENT_MASK_SHIFT kCGEventFlagMaskShift
#define EVENT_MASK_CMD kCGEventFlagMaskCommand
#define EVENT_MASK_CTRL kCGEventFlagMaskControl
#define EVENT_MASK_FN kCGEventFlagMaskSecondaryFn
struct event_tap
{
CFMachPortRef handle;
CFRunLoopSourceRef runloop_source;
CGEventMask mask;
};
#define EVENT_TAP_CALLBACK(name) \
CGEventRef name(CGEventTapProxy proxy, \
CGEventType type, \
CGEventRef cgevent, \
void *reference)
typedef EVENT_TAP_CALLBACK(event_tap_callback);
static EVENT_TAP_CALLBACK(mouse_handler);
bool event_tap_enabled(struct event_tap *event_tap);
bool event_tap_begin(struct event_tap *event_tap, uint32_t mask, event_tap_callback *callback);
void event_tap_end(struct event_tap *event_tap);
#endif

View file

@ -34,8 +34,8 @@
#include "misc/socket.c"
#include "event_loop.h"
#include "mouse.h"
#include "event.h"
#include "event_tap.h"
#include "workspace.h"
#include "message.h"
#include "display.h"
@ -47,8 +47,8 @@
#include "bar_manager.h"
#include "event_loop.c"
#include "mouse.c"
#include "event.c"
#include "event_tap.c"
#include "workspace.m"
#include "message.c"
#include "display.c"

20
src/mouse.c Normal file
View file

@ -0,0 +1,20 @@
extern struct event_loop g_event_loop;
static pascal OSStatus mouse_handler(EventHandlerCallRef next, EventRef e, void *data) {
switch (GetEventKind(e)) {
case kEventMouseUp: {
struct event *event = event_create(&g_event_loop, MOUSE_UP, (void *) CFRetain(CopyEventCGEvent(e)));
event_loop_post(&g_event_loop, event);
break;
}
default:
break;
}
return CallNextEventHandler(next, e);
}
void mouse_begin(void) {
EventTargetRef target = GetEventDispatcherTarget();
InstallEventHandler(target, NewEventHandlerUPP(mouse_handler), sizeof mouse_events / sizeof *mouse_events, mouse_events, 0, 0);
}

8
src/mouse.h Normal file
View file

@ -0,0 +1,8 @@
static const EventTypeSpec mouse_events [] = {
{ kEventClassMouse, kEventMouseDown },
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged }
};
void mouse_begin(void);

View file

@ -16,17 +16,16 @@
#define PATCH 0
extern int SLSMainConnectionID(void);
extern int RunApplicationEventLoop(void);
#define CONNECTION_CALLBACK(name) void name(uint32_t type, void *data, size_t data_length, void *context, int cid)
typedef CONNECTION_CALLBACK(connection_callback);
extern CGError SLSRegisterConnectionNotifyProc(int cid, connection_callback *handler, uint32_t event, void *context);
struct event_loop g_event_loop;
void *g_workspace_context;
struct display_manager g_display_manager;
struct daemon g_daemon;
struct bar_manager g_bar_manager;
struct event_tap g_event_tap;
int g_connection;
char g_socket_file[MAXLEN];
@ -190,10 +189,6 @@ static inline void init_misc_settings(void)
}
#pragma clang diagnostic pop
static CONNECTION_CALLBACK(connection_handler)
{
}
static void parse_arguments(int argc, char **argv)
{
if ((string_equals(argv[1], VERSION_OPT_LONG)) ||
@ -245,17 +240,16 @@ int main(int argc, char **argv)
bar_manager_init(&g_bar_manager);
event_loop_begin(&g_event_loop);
mouse_begin();
display_manager_begin(&g_display_manager);
workspace_event_handler_begin(&g_workspace_context);
bar_manager_begin(&g_bar_manager);
event_tap_begin(&g_event_tap, EVENT_MASK_MOUSE, mouse_handler);
SLSRegisterConnectionNotifyProc(g_connection, connection_handler, 1204, NULL);
if (!socket_daemon_begin_un(&g_daemon, g_socket_file, message_handler)) {
error("sketchybar: could not initialize daemon! abort..\n");
}
exec_config_file();
CFRunLoopRun();
RunApplicationEventLoop();
return 0;
}