Skip to content

Commit

Permalink
Merge pull request #103 from tsoding/78
Browse files Browse the repository at this point in the history
 (#78) Grab keyboard and pointer instead of set focus spamming
  • Loading branch information
rexim authored Jan 29, 2020
2 parents 37e480e + 7d79f36 commit eb3f30e
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/boomer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,17 @@ proc selectWindow(display: PDisplay): TWindow =

return root

proc xElevenErrorHandler(display: PDisplay, errorEvent: PXErrorEvent): cint{.cdecl.} =
# NOTE: This is needed primarily for -d:select along with
# -d:live. When we are constantly taking a screenshot of a resizing
# window sometimes you may not know exact shape of the window (because
# X11 request may lag behind) and you are trying to access invalid
# memory. X11 does not like that and simply crashes. With this error
# handler we force it not to crash.
proc customX11ErrorHandler(display: PDisplay, errorEvent: PXErrorEvent): cint{.cdecl.} =
const CAPACITY = 256
var errorMessage: array[CAPACITY, char]
discard XGetErrorText(display, errorEvent.error_code.cint, addr errorMessage, CAPACITY)
echo "X ELEVEN ERROR: ", $(addr errorMessage)
echo "X11: ", $(addr errorMessage)

proc main() =
let boomerDir = getConfigDir() / "boomer"
Expand Down Expand Up @@ -272,7 +278,7 @@ proc main() =
defer:
discard XCloseDisplay(display)

discard XSetErrorHandler(xElevenErrorHandler)
discard XSetErrorHandler(customX11ErrorHandler)

when defined(select):
echo "Please select window:"
Expand Down Expand Up @@ -305,17 +311,38 @@ proc main() =
if vi == nil:
quit "No appropriate visual found"


echo "Visual ", vi.visualid, " selected"
var swa: TXSetWindowAttributes
swa.colormap = XCreateColormap(display, DefaultRootWindow(display),
vi.visual, AllocNone)
swa.event_mask = ButtonPressMask or ButtonReleaseMask or
KeyPressMask or KeyReleaseMask or
PointerMotionMask or ExposureMask or ClientMessage
if not windowed:
swa.event_mask = ExposureMask or ClientMessage
swa.override_redirect = 1
swa.save_under = 1
else:
swa.event_mask = ExposureMask or ClientMessage or
PointerMotionMask or ButtonPressMask or
ButtonReleaseMask or KeyPressMask or
KeyReleaseMask

var cursor = XCreateFontCursor(display, XC_arrow)
defer: discard XFreeCursor(display, cursor)

if not windowed:
discard XGrabPointer(display, DefaultRootWindow(display), 0,
PointerMotionMask or
ButtonPressMask or
ButtonReleaseMask,
GrabModeAsync, GrabModeAsync,
DefaultRootWindow(display), cursor,
CurrentTime)
discard XGrabKeyboard(display, DefaultRootWindow(display), 0,
GrabModeAsync, GrabModeAsync,
CurrentTime)
defer:
if not windowed:
discard XUngrabPointer(display, CurrentTime)
discard XUngrabKeyboard(display, CurrentTime)

var attributes: TXWindowAttributes
discard XGetWindowAttributes(
Expand Down Expand Up @@ -434,10 +461,6 @@ proc main() =

let dt = 1.0 / rate.float
while not quitting:
# TODO(#78): Is there a better solution to keep the focus always on the window?
if not windowed:
discard XSetInputFocus(display, win, RevertToParent, CurrentTime);

var wa: TXWindowAttributes
discard XGetWindowAttributes(display, win, addr wa)
glViewport(0, 0, wa.width, wa.height)
Expand Down

0 comments on commit eb3f30e

Please sign in to comment.