Skip to content

Commit 83cf9b5

Browse files
committed
prevent application crash if active_window fails
not sure if this works #61
1 parent a82f30b commit 83cf9b5

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/run/display/x11.cr

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ module Run
160160
{% if ! flag?(:release) %}
161161
puts "[debug] x11: root_win = #{@root_win}"
162162
{% end %}
163-
@_NET_ACTIVE_WINDOW = @display.intern_atom("_NET_ACTIVE_WINDOW", false)
163+
@_NET_ACTIVE_WINDOW = @display.intern_atom("_NET_ACTIVE_WINDOW", true)
164+
{% if ! flag?(:release) %}
165+
puts "[debug] x11: _NET_ACTIVE_WINDOW = #{@_NET_ACTIVE_WINDOW}"
166+
{% end %}
164167
root_win_attributes = ::X11::SetWindowAttributes.new
165168
root_win_attributes.event_mask = ::X11::PropertyChangeMask
166169
# So we get notified of active window change
@@ -182,8 +185,18 @@ module Run
182185
end
183186

184187
private def active_window
185-
# TODO: manybe use @x_do.active_window if it's similarly fast?
186-
win = @display.window_property(@root_win, @_NET_ACTIVE_WINDOW, 0_i64, 1_i64, false, ::X11::C::XA_WINDOW.to_u64)[:prop].unsafe_as(Pointer(UInt64)).value
188+
# TODO: manybe use @x_do.active_window if it's similarly fast? (evdev branch)
189+
return 0_u64 if @_NET_ACTIVE_WINDOW < 1
190+
prop = @display.window_property(@root_win, @_NET_ACTIVE_WINDOW, 0_i64, 1_i64, false, ::X11::C::XA_WINDOW.to_u64)
191+
nitems = prop[:nitems]
192+
data = prop[:prop].unsafe_as(Pointer(UInt64))
193+
if data.null? || nitems.nil? || nitems < 1
194+
{% if ! flag?(:release) %}
195+
puts "[debug] x11: active window detection: _NET_ACTIVE_WINDOW returned NULL prop data"
196+
{% end %}
197+
return 0_u64
198+
end
199+
win = data.value
187200
{% if ! flag?(:release) %}
188201
puts "[debug] x11: active window detection: #{win}"
189202
{% end %}

0 commit comments

Comments
 (0)