From b0ae758d7b1f7aff6d203c72ba79d79bb36d7557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 4 Nov 2020 00:09:52 +0100 Subject: [PATCH] refactor xorg race condition. --- internal/desktop/manager.go | 8 ++++++- internal/desktop/xorg/xorg.c | 39 +++++------------------------------ internal/desktop/xorg/xorg.go | 20 +++++++++++++++--- internal/desktop/xorg/xorg.h | 13 +++--------- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/internal/desktop/manager.go b/internal/desktop/manager.go index 3982ee86..aac9304b 100644 --- a/internal/desktop/manager.go +++ b/internal/desktop/manager.go @@ -3,6 +3,7 @@ package desktop import ( "time" + "github.com/kataras/go-events" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -26,10 +27,15 @@ func New(display string) *DesktopManagerCtx { } func (manager *DesktopManagerCtx) Start() { - xorg.Display(manager.display) + if err := xorg.DisplayOpen(manager.display); err != nil { + manager.logger.Warn().Err(err).Msg("unable to open dispaly") + } + + xorg.GetScreenConfigurations() go func() { defer func() { + xorg.DisplayClose() manager.logger.Info().Msg("shutdown") }() diff --git a/internal/desktop/xorg/xorg.c b/internal/desktop/xorg/xorg.c index ace9a1ff..f631fc5e 100644 --- a/internal/desktop/xorg/xorg.c +++ b/internal/desktop/xorg/xorg.c @@ -1,47 +1,18 @@ #include "xorg.h" static Display *DISPLAY = NULL; -static char *NAME = ":0.0"; -static int REGISTERED = 0; -static int DIRTY = 0; Display *getXDisplay(void) { - /* Close the display if displayName has changed */ - if (DIRTY) { - XDisplayClose(); - DIRTY = 0; - } - - if (DISPLAY == NULL) { - /* First try the user set displayName */ - DISPLAY = XOpenDisplay(NAME); - - /* Then try using environment variable DISPLAY */ - if (DISPLAY == NULL) { - DISPLAY = XOpenDisplay(NULL); - } - - if (DISPLAY == NULL) { - fputs("Could not open main display\n", stderr); - } else if (!REGISTERED) { - atexit(&XDisplayClose); - REGISTERED = 1; - } - } - return DISPLAY; } -void XDisplayClose(void) { - if (DISPLAY != NULL) { - XCloseDisplay(DISPLAY); - DISPLAY = NULL; - } +int XDisplayOpen(char *name) { + DISPLAY = XOpenDisplay(name); + return DISPLAY == NULL; } -void XDisplaySet(char *input) { - NAME = strdup(input); - DIRTY = 1; +void XDisplayClose(void) { + XCloseDisplay(DISPLAY); } void XMove(int x, int y) { diff --git a/internal/desktop/xorg/xorg.go b/internal/desktop/xorg/xorg.go index 4063d352..09805d88 100644 --- a/internal/desktop/xorg/xorg.go +++ b/internal/desktop/xorg/xorg.go @@ -24,18 +24,32 @@ var debounce_button = make(map[int]time.Time) var debounce_key = make(map[uint64]time.Time) var mu = sync.Mutex{} -func init() { +func GetScreenConfigurations() { C.XGetScreenConfigurations() } -func Display(display string) { +func DisplayOpen(display string) error { mu.Lock() defer mu.Unlock() displayUnsafe := C.CString(display) defer C.free(unsafe.Pointer(displayUnsafe)) - C.XDisplaySet(displayUnsafe) + var err C.int + err = C.XDisplayOpen(displayUnsafe) + + if int(err) == 1 { + return fmt.Errorf("Could not open display %s.", display) + } + + return nil +} + +func DisplayClose() { + mu.Lock() + defer mu.Unlock() + + C.XDisplayClose() } func Move(x, y int) { diff --git a/internal/desktop/xorg/xorg.h b/internal/desktop/xorg/xorg.h index 4a7b49a2..426769a0 100644 --- a/internal/desktop/xorg/xorg.h +++ b/internal/desktop/xorg/xorg.h @@ -9,18 +9,14 @@ #include #include #include -#include /* For fputs() */ -#include /* For strdup() */ +#include extern void goCreateScreenSize(int index, int width, int height, int mwidth, int mheight); extern void goSetScreenRates(int index, int rate_index, short rate); -/* Returns the main display, closed either on exit or when closeMainDisplay() -* is invoked. This removes a bit of the overhead of calling XOpenDisplay() & -* XCloseDisplay() everytime the main display needs to be used. -* -* Note that this is almost certainly not thread safe. */ Display *getXDisplay(void); +int XDisplayOpen(char *input); +void XDisplayClose(void); void XMove(int x, int y); void XScroll(int x, int y); @@ -32,9 +28,6 @@ void XSetScreenConfiguration(int index, short rate); int XGetScreenSize(); short XGetScreenRate(); -void XDisplayClose(void); -void XDisplaySet(char *input); - void SetKeyboardLayout(char *layout); void SetKeyboardModifiers(int num_lock, int caps_lock, int scroll_lock);