Skip to content

Instantly share code, notes, and snippets.

@emdeeeks
Forked from ohac/xlib.rb
Created May 27, 2024 22:35
Show Gist options
  • Save emdeeeks/4e8439704dbc53d121f1f7e889385039 to your computer and use it in GitHub Desktop.
Save emdeeeks/4e8439704dbc53d121f1f7e889385039 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'ffi'
module X11
extend FFI::Library
ffi_lib 'X11'
attach_function :open_display, :XOpenDisplay, [:pointer], :pointer
attach_function :query_tree, :XQueryTree,
[:pointer, :int, :pointer, :pointer, :pointer, :pointer], :int
attach_function :get_window_attributes, :XGetWindowAttributes,
[:pointer, :int, :pointer], :pointer
attach_function :create_gc, :XCreateGC, [:pointer, :int, :int, :pointer], :pointer
attach_function :set_foreground, :XSetForeground, [:pointer, :pointer, :ulong], :int
attach_function :fill_rectangle, :XFillRectangle,
[:pointer, :int, :pointer, :int, :int, :uint, :uint], :int
attach_function :sync, :XSync, [:pointer, :int], :int
attach_function :free_gc, :XFreeGC, [:pointer, :pointer], :int
end
class XWindowAttributes < FFI::Struct
layout :x, :int, :y, :int,
:width, :int, :height, :int,
:border_width, :int,
:depth, :int,
:visual, :pointer,
:root, :int,
:class, :int,
:bit_gravity, :int,
:win_gravity, :int,
:backing_store, :int,
:backing_planes, :ulong,
:backing_pixel, :ulong,
:save_under, :int,
:colormap, :ulong,
:map_installed, :int,
:map_state, :int,
:all_event_masks, :long,
:your_event_mask, :long,
:do_not_propagate_mask, :long,
:override_redirect, :int,
:screen, :pointer
end
display = X11.open_display(nil)
window = ENV["WINDOWID"].to_i
attr = XWindowAttributes.new
X11.get_window_attributes(display, window, attr.to_ptr)
width = attr[:width]
height = attr[:height]
p [width, height]
root = FFI::MemoryPointer.new(:int32, 1)
parent = FFI::MemoryPointer.new(:int32, 1)
children = FFI::MemoryPointer.new(:pointer, 1)
nchildren = FFI::MemoryPointer.new(:int32, 1)
loop do
p_window = window
X11.query_tree(display, window, root, parent, children, nchildren)
n = nchildren.get_int32(0)
p n
n.times do |i|
child = children.get_pointer(0).get_int32(i)
X11.get_window_attributes(display, child, attr.to_ptr)
p [attr[:width], attr[:height]]
if attr[:width] > width * 0.7 && attr[:height] > height * 0.7
p :change
width = attr[:width]
height = attr[:height]
window = child
end
end
break if p_window == window
end
offset_x = 2
n = nchildren.get_int32(0)
n.times do |i|
child = children.get_pointer(0).get_int32(i)
X11.get_window_attributes(display, child, attr.to_ptr)
if attr[:x] <= 0 && attr[:width] < 30 && attr[:height] > height * 0.7 # scrollbar of xterm/kterm ?
offset_x += attr[:x] + attr[:width] + attr[:border_width] * 2
break
end
end
offset_y = 2
p [:offset, offset_x, offset_y]
screen = attr[:screen]
red = 0x00ff0000
gc = X11.create_gc(display, window, 0, nil)
X11.set_foreground(display, gc, red)
X11.fill_rectangle(display, window, gc, 0, 0, 100, 100)
X11.sync(display, 0)
X11.free_gc(display, gc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment