neko/pkg/xevent/xevent.go
tt2468 00c7e6dfb2
Move X11 error handler registration to before screen sets (#62)
If X11 throws an error during screen config get/set, the default error
handler at the time will be one which causes a program exit. This
splits the error handler registration in xevent into its own call,
allowing us to register our error handler earlier on. This results in
the safe error handling logic actually being called instead of a full
program exit.
2024-03-27 21:38:33 +01:00

97 lines
2.3 KiB
Go

package xevent
/*
#cgo LDFLAGS: -lX11 -lXfixes
#include "xevent.h"
*/
import "C"
import (
"strings"
"unsafe"
"github.com/kataras/go-events"
)
var Emmiter events.EventEmmiter
var Unminimize bool = false
var FileChooserDialog bool = false
var fileChooserDialogWindow uint32 = 0
func init() {
Emmiter = events.New()
}
func SetupErrorHandler() {
C.XSetupErrorHandler()
}
func EventLoop(display string) {
displayUnsafe := C.CString(display)
defer C.free(unsafe.Pointer(displayUnsafe))
C.XEventLoop(displayUnsafe)
}
//export goXEventCursorChanged
func goXEventCursorChanged(event C.XFixesCursorNotifyEvent) {
Emmiter.Emit("cursor-changed", uint64(event.cursor_serial))
}
//export goXEventClipboardUpdated
func goXEventClipboardUpdated() {
Emmiter.Emit("clipboard-updated")
}
//export goXEventConfigureNotify
func goXEventConfigureNotify(display *C.Display, window C.Window, name *C.char, role *C.char) {
if C.GoString(role) != "GtkFileChooserDialog" || !FileChooserDialog {
return
}
// TODO: Refactor. Right now processing of this dialog relies on identifying
// via its name. When that changes to role, this condition should be removed.
if !strings.HasPrefix(C.GoString(name), "Open File") {
return
}
C.XFileChooserHide(display, window)
if fileChooserDialogWindow == 0 {
fileChooserDialogWindow = uint32(window)
Emmiter.Emit("file-chooser-dialog-opened")
}
}
//export goXEventUnmapNotify
func goXEventUnmapNotify(window C.Window) {
if uint32(window) != fileChooserDialogWindow || !FileChooserDialog {
return
}
fileChooserDialogWindow = 0
Emmiter.Emit("file-chooser-dialog-closed")
}
//export goXEventWMChangeState
func goXEventWMChangeState(display *C.Display, window C.Window, window_state C.ulong) {
// if we just realized that window is minimized and we want it to be unminimized
if window_state != C.NormalState && Unminimize {
// we want to unmap and map the window to force it to redraw
C.XUnmapWindow(display, window)
C.XMapWindow(display, window)
C.XFlush(display)
}
}
//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))
}
//export goXEventActive
func goXEventActive() C.int {
return C.int(1)
}