aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/pugl/pugl.h9
-rw-r--r--src/x11.c11
2 files changed, 18 insertions, 2 deletions
diff --git a/include/pugl/pugl.h b/include/pugl/pugl.h
index 6e783e7..59b8847 100644
--- a/include/pugl/pugl.h
+++ b/include/pugl/pugl.h
@@ -1137,7 +1137,14 @@ typedef enum {
PUGL_CURSOR_UP_DOWN, ///< Up/down arrow for vertical resize
} PuglCursor;
-/// Grab the keyboard input focus
+/**
+ Grab the keyboard input focus.
+
+ Note that this will fail if the view is not mapped and so should not, for
+ example, be called immediately after puglShow().
+
+ @return #PUGL_SUCCESS if the focus was successfully grabbed, or an error.
+*/
PUGL_API
PuglStatus
puglGrabFocus(PuglView* view);
diff --git a/src/x11.c b/src/x11.c
index 455ee20..1c022e6 100644
--- a/src/x11.c
+++ b/src/x11.c
@@ -795,7 +795,16 @@ translateEvent(PuglView* const view, XEvent xevent)
PuglStatus
puglGrabFocus(PuglView* const view)
{
- PuglInternals* const impl = view->impl;
+ PuglInternals* const impl = view->impl;
+ XWindowAttributes attrs = {0};
+
+ if (!impl->win || !XGetWindowAttributes(impl->display, impl->win, &attrs)) {
+ return PUGL_UNKNOWN_ERROR;
+ }
+
+ if (attrs.map_state != IsViewable) {
+ return PUGL_FAILURE;
+ }
XSetInputFocus(impl->display, impl->win, RevertToNone, CurrentTime);
return PUGL_SUCCESS;