Class: WindowGeometry

Inherits:
Object
  • Object
show all
Defined in:
lib/window_geometry.rb,
lib/window_geometry/x.rb,
lib/window_geometry/version.rb

Defined Under Namespace

Modules: X, X11

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWindowGeometry

Returns a new instance of WindowGeometry.



11
12
13
14
# File 'lib/window_geometry.rb', line 11

def initialize
  @display = X::Display.new
  @root = @display.default_root_window
end

Class Method Details

.startObject



7
8
9
# File 'lib/window_geometry.rb', line 7

def self.start
  self.new.start
end

Instance Method Details

#display_osdObject



96
97
98
99
100
101
102
# File 'lib/window_geometry.rb', line 96

def display_osd
  @osd.map_window
  @osd.clear
  @gc.draw_string(10, 30, @osd_str)
  @osd.move_window(@osd_x, @osd_y)
  @display.flush
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/window_geometry.rb', line 16

def start
  watch_window(@root)
  @root.select_input(X11::SUBSTRUCTURE_NOTIFY_MASK)
  @osd = @root.create_simple_window(100, 100, 200, 50, 0, 0, 0xe0e0e0)
  @osd.change_property(:_NET_WM_WINDOW_TYPE, X11::PROP_MODE_REPLACE, :_NET_WM_WINDOW_TYPE_NOTIFICATION)
  @osd.select_input(X11::EXPOSURE_MASK)
  @gc = @osd.create_gc
  @gc.set_background(0xe0e0e0)
  @gc.set_foreground(0)
  @gc.set_font(@display.load_font("fixed"))

  while true
    ev = @display.next_event
    case ev
    when X::CreateWindowEvent
      win = ev.window
      watch_window(win)
    when X::ExposeEvent
      display_osd if ev.window.id == @osd.id
    when X::ConfigureEvent
      win = ev.window
      hints = win.wm_normal_hints
      next if (hints.max_width == hints.min_width && hints.max_height == hints.min_height) || !win_title(win)
      width, height = ev.width, ev.height
      next if ev.x == 0 && ev.y == 0
      @osd_x = ev.x + ((ev.width - 200) / 2)
      @osd_y = ev.y + ((ev.height - 50) / 2)
      @osd_str = "#{ev.width} x #{ev.height} +#{ev.x}+#{ev.y}"
      if hints.width_inc > 0 && hints.height_inc > 0
        width -= hints.base_width
        height -= hints.base_height
        @osd_str += "  (#{width / hints.width_inc} x #{height / hints.height_inc})"
      end
      display_osd
      map_time = Time.now
      Thread.new do
        sleep 1
        if Time.now >= map_time + 1
          @osd.unmap_window
          @display.flush
        end
      end
    end
  end
end

#watch_resize_event(win) ⇒ Object



92
93
94
# File 'lib/window_geometry.rb', line 92

def watch_resize_event(win)
  win.select_input(X11::STRUCTURE_NOTIFY_MASK)
end

#watch_window(win) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/window_geometry.rb', line 62

def watch_window(win)
  win.query_tree&.each do |w|
    if win_resizable?(w) && win_title(w)
      # p [win_class_name(w), win_name(w), win_title(w)]
      watch_resize_event(w)
      next
    end
    watch_window(w)
  end
end

#win_class_name(win) ⇒ Object



78
79
80
81
# File 'lib/window_geometry.rb', line 78

def win_class_name(win)
  class_hint = win.class_hint
  class_hint.class_name.null? ? nil : class_hint.class_name.to_s
end

#win_name(win) ⇒ Object



83
84
85
86
# File 'lib/window_geometry.rb', line 83

def win_name(win)
  class_hint = win.class_hint
  class_hint.name.null? ? nil : class_hint.name.to_s
end

#win_resizable?(win) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/window_geometry.rb', line 73

def win_resizable?(win)
  hint = win.wm_normal_hints
  hint.max_width != hint.min_width || hint.max_height != hint.min_height
end

#win_title(win) ⇒ Object



88
89
90
# File 'lib/window_geometry.rb', line 88

def win_title(win)
  win.wm_name
end