From 93b1553b4d65928e15c90d7c9a6277fb3c18d73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 9 Jan 2021 22:58:18 +0100 Subject: [PATCH] xlib get cursor implementation. --- internal/desktop/xorg.go | 4 ++++ internal/desktop/xorg/xorg.c | 5 +++++ internal/desktop/xorg/xorg.go | 24 +++++++++++++++++++++++- internal/desktop/xorg/xorg.h | 2 ++ internal/types/desktop.go | 10 ++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/desktop/xorg.go b/internal/desktop/xorg.go index 90faa4a2..370d02f4 100644 --- a/internal/desktop/xorg.go +++ b/internal/desktop/xorg.go @@ -60,3 +60,7 @@ func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) { func (manager *DesktopManagerCtx) SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) { xorg.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock) } + +func (manager *DesktopManagerCtx) GetCursorImage() *types.CursorImage { + return xorg.GetCursorImage() +} diff --git a/internal/desktop/xorg/xorg.c b/internal/desktop/xorg/xorg.c index f631fc5e..85b0553a 100644 --- a/internal/desktop/xorg/xorg.c +++ b/internal/desktop/xorg/xorg.c @@ -146,3 +146,8 @@ void SetKeyboardModifiers(int num_lock, int caps_lock, int scroll_lock) { XFlush(display); } + +XFixesCursorImage *XGetCursorImage(void) { + Display *display = getXDisplay(); + return XFixesGetCursorImage(display); +} diff --git a/internal/desktop/xorg/xorg.go b/internal/desktop/xorg/xorg.go index 0b5ad44e..d05d3ee0 100644 --- a/internal/desktop/xorg/xorg.go +++ b/internal/desktop/xorg/xorg.go @@ -2,7 +2,7 @@ package xorg /* #cgo linux CFLAGS: -I/usr/src -I/usr/local/include/ -#cgo linux LDFLAGS: -L/usr/src -L/usr/local/lib -lX11 -lXtst -lXrandr -lxcb +#cgo linux LDFLAGS: -L/usr/src -L/usr/local/lib -lX11 -lxcb -lXrandr -lXtst -lXfixes #include "xorg.h" */ @@ -219,6 +219,28 @@ func SetKeyboardModifiers(num_lock int, caps_lock int, scroll_lock int) { C.SetKeyboardModifiers(C.int(num_lock), C.int(caps_lock), C.int(scroll_lock)) } +func GetCursorImage() *types.CursorImage { + mu.Lock() + defer mu.Unlock() + + var cur *C.XFixesCursorImage + cur = C.XGetCursorImage() + defer C.XFree(unsafe.Pointer(cur)) + + width := uint16(cur.width) + height := uint16(cur.height) + + return &types.CursorImage{ + Width: width, + Height: height, + Xhot: uint16(cur.xhot), + Yhot: uint16(cur.yhot), + Serial: uint64(cur.cursor_serial), + // Xlib stores 32-bit data in longs, even if longs are 64-bits long. + Pixels: C.GoBytes(unsafe.Pointer(cur.pixels), C.int(width * height * 8)), + } +} + //export goCreateScreenSize func goCreateScreenSize(index C.int, width C.int, height C.int, mwidth C.int, mheight C.int) { ScreenConfigurations[int(index)] = types.ScreenConfiguration{ diff --git a/internal/desktop/xorg/xorg.h b/internal/desktop/xorg/xorg.h index 426769a0..4b228f9e 100644 --- a/internal/desktop/xorg/xorg.h +++ b/internal/desktop/xorg/xorg.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -30,5 +31,6 @@ short XGetScreenRate(); void SetKeyboardLayout(char *layout); void SetKeyboardModifiers(int num_lock, int caps_lock, int scroll_lock); +XFixesCursorImage *XGetCursorImage(void); #endif diff --git a/internal/types/desktop.go b/internal/types/desktop.go index 1bded734..7acee29e 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -1,5 +1,14 @@ package types +type CursorImage struct { + Width uint16 + Height uint16 + Xhot uint16 + Yhot uint16 + Serial uint64 + Pixels []byte +} + type ScreenSize struct { Width int Height int @@ -31,6 +40,7 @@ type DesktopManager interface { GetScreenSize() *ScreenSize SetKeyboardLayout(layout string) SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) + GetCursorImage() *CursorImage // clipboard ReadClipboard() string