From 49c3b6d4fe28064bb7a0f2a0dadd56ea1f833e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Tue, 12 Jan 2021 00:09:43 +0100 Subject: [PATCH] xevent: OnWindowCreated. --- internal/desktop/xevent.go | 4 ++++ internal/desktop/xevent/xevent.c | 17 +++++++++++++++++ internal/desktop/xevent/xevent.go | 11 +++++++++++ internal/desktop/xevent/xevent.h | 2 ++ internal/types/desktop.go | 1 + internal/websocket/manager.go | 9 +++++++++ 6 files changed, 44 insertions(+) diff --git a/internal/desktop/xevent.go b/internal/desktop/xevent.go index 972c4e43..f59f2025 100644 --- a/internal/desktop/xevent.go +++ b/internal/desktop/xevent.go @@ -12,6 +12,10 @@ func (manager *DesktopManagerCtx) OnClipboardUpdated(listener func()) { xevent.OnClipboardUpdated(listener) } +func (manager *DesktopManagerCtx) OnWindowCreated(listener func(window uint32, name string, role string)) { + xevent.OnWindowCreated(listener) +} + func (manager *DesktopManagerCtx) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) { xevent.OnEventError(listener) } diff --git a/internal/desktop/xevent/xevent.c b/internal/desktop/xevent/xevent.c index 31216026..f7d9e870 100644 --- a/internal/desktop/xevent/xevent.c +++ b/internal/desktop/xevent/xevent.c @@ -27,6 +27,7 @@ void XEventLoop(char *name) { XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0); XFixesSelectSelectionInput(display, root, XA_CLIPBOARD, XFixesSetSelectionOwnerNotifyMask); XFixesSelectCursorInput(display, root, XFixesDisplayCursorNotifyMask); + XSelectInput(display, root, SubstructureNotifyMask); XSync(display, 0); XSetErrorHandler(XEventError); @@ -52,6 +53,22 @@ void XEventLoop(char *name) { continue; } } + + // XFixesSelectionNotifyEvent + if (event.type == CreateNotify) { + char *name; + XFetchName(display, event.xcreatewindow.window, &name); + + char *role; + XTextProperty text_data; + Atom atom = XInternAtom(display, "WM_WINDOW_ROLE", True); + int status = XGetTextProperty(display, event.xcreatewindow.window, &text_data, atom); + role = (char *)text_data.value; + + goXEventWindowCreated(event.xcreatewindow, name, role); + XFree(name); + continue; + } } XCloseDisplay(display); diff --git a/internal/desktop/xevent/xevent.go b/internal/desktop/xevent/xevent.go index 3c24701b..467baa14 100644 --- a/internal/desktop/xevent/xevent.go +++ b/internal/desktop/xevent/xevent.go @@ -38,6 +38,12 @@ func OnClipboardUpdated(listener func()) { }) } +func OnWindowCreated(listener func(window uint32, name string, role string)) { + emmiter.On("window-created", func(payload ...interface{}) { + listener(payload[0].(uint32), payload[1].(string), payload[2].(string)) + }) +} + func OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) { emmiter.On("event-error", func(payload ...interface{}) { listener(payload[0].(uint8), payload[1].(string), payload[2].(uint8), payload[3].(uint8)) @@ -54,6 +60,11 @@ func goXEventClipboardUpdated() { emmiter.Emit("clipboard-updated") } +//export goXEventWindowCreated +func goXEventWindowCreated(event C.XCreateWindowEvent, name *C.char, role *C.char) { + emmiter.Emit("window-created", uint32(event.window), C.GoString(name), C.GoString(role)) +} + //export goXEventError func goXEventError(event *C.XErrorEvent, message *C.char) { emmiter.Emit("event-error", uint8(event.error_code), C.GoString(message), uint8(event.request_code), uint8(event.minor_code)) diff --git a/internal/desktop/xevent/xevent.h b/internal/desktop/xevent/xevent.h index 743061dd..24ee7160 100644 --- a/internal/desktop/xevent/xevent.h +++ b/internal/desktop/xevent/xevent.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -7,6 +8,7 @@ extern void goXEventCursorChanged(XFixesCursorNotifyEvent event); extern void goXEventClipboardUpdated(); +extern void goXEventWindowCreated(XCreateWindowEvent event, char *name, char *role); extern void goXEventError(XErrorEvent *event, char *message); extern int goXEventActive(); diff --git a/internal/types/desktop.go b/internal/types/desktop.go index 60a18403..28c81431 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -45,6 +45,7 @@ type DesktopManager interface { // xevent OnCursorChanged(listener func(serial uint64)) OnClipboardUpdated(listener func()) + OnWindowCreated(listener func(window uint32, name string, role string)) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) // clipboard diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 9f4df2dc..b7b18014 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -133,6 +133,15 @@ func (ws *WebSocketManagerCtx) Start() { ws.logger.Warn().Err(err).Msg("could not sync clipboard") } }) + + ws.desktop.OnWindowCreated(func(window uint32, name string, role string) { + // TODO: Implement. + ws.logger.Info(). + Uint32("window", window). + Str("name", name). + Str("role", role). + Msg("created new window") + }) } func (ws *WebSocketManagerCtx) Shutdown() error {