Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#78) Grab keyboard and pointer instead of set focus spamming #103

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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