Class: Wmadd

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

Overview

Wmadd

Defined Under Namespace

Modules: X, X11

Constant Summary collapse

DEFAULT_OPACITY =
0xFFFFFFFF
OVERWRAP_OPACITY =
0x80000000
VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWmadd

Returns a new instance of Wmadd.



17
18
19
20
21
# File 'lib/wmadd.rb', line 17

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

Class Method Details

.startObject



13
14
15
# File 'lib/wmadd.rb', line 13

def self.start
  self.new.start
end

Instance Method Details

#display_osd(win = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/wmadd.rb', line 61

def display_osd(win=nil)
  if win
    wm_win = wm_window(win) or return
    hints = wm_win.wm_normal_hints or return
    top_win = top_window(win) or return

    x, y = top_win.geometry.then{[_1.x, _1.y]}
    width, height = win.geometry.then{[_1.width, _1.height]}

    @osd_x = x + ((width - 200) / 2)
    @osd_y = y + ((height - 50) / 2)
    @osd_str = "#{width} x #{height} +#{x}+#{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
  end

  @osd.map_window
  @osd.clear
  @gc.draw_string(10, 30, @osd_str)
  @osd.move_window(@osd_x, @osd_y)
  @display.flush
  @job&.cancel
  @job = @timer.set(1) do
    @osd.unmap_window
    @display.flush
  end
end

#lazy_reset_opacityObject



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

def lazy_reset_opacity
  @reset_opacity_job&.cancel
  @reset_opacity_job = @timer.set(0.1){reset_opacity}
end

#overwrap?(w1, w2) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/wmadd.rb', line 134

def overwrap?(w1, w2)
  g1 = top_window(w1).geometry
  g2 = top_window(w2).geometry
  g1.x < g2.x+g2.width && g1.x+g1.width > g2.x && g1.y < g2.y+g2.height && g1.y+g1.height > g2.y
end

#reset_opacityObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wmadd.rb', line 115

def reset_opacity
  return unless @root.active_window
  active_win = wm_window(@root.active_window) or return

  f = false
  window_list.each do |w|
    if active_win.id == w.id
      f = true
      set_default_opacity(w)
      next
    end
    if f && overwrap?(w, active_win)
      set_overwrap_opacity(w)
      next
    end
    set_default_opacity(w)
  end
end

#set_default_opacity(win) ⇒ Object



140
141
142
# File 'lib/wmadd.rb', line 140

def set_default_opacity(win)
  win.change_property_cardinal(:_NET_WM_WINDOW_OPACITY, X::PROP_MODE_REPLACE, DEFAULT_OPACITY)
end

#set_overwrap_opacity(win) ⇒ Object



144
145
146
# File 'lib/wmadd.rb', line 144

def set_overwrap_opacity(win)
  win.change_property_cardinal(:_NET_WM_WINDOW_OPACITY, X::PROP_MODE_REPLACE, OVERWRAP_OPACITY)
end

#startObject



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
# File 'lib/wmadd.rb', line 23

def start
  window_list.each do |w|
    watch_resize_event(w)
  end

  @root.select_input(X::SUBSTRUCTURE_NOTIFY_MASK | X::PROPERTY_CHANGE_MASK)
  @osd = @root.create_simple_window(100, 100, 200, 50, 0, 0, 0xe0e0e0)
  @osd.change_property_atom(:_NET_WM_WINDOW_TYPE, X::PROP_MODE_REPLACE, :_NET_WM_WINDOW_TYPE_NOTIFICATION)
  @osd.select_input(X::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
      @watch_resize_job&.cancel
      @watch_resize_job = @timer.set(0.1) do
        window_list.each do |w|
          watch_resize_event(w)
        end
      end
    when X::ExposeEvent
      display_osd if ev.window == @osd.id
    when X::PropertyEvent
      if ev.atom == @display.intern_atom(:_NET_ACTIVE_WINDOW)
        reset_opacity
      end
    when X::ConfigureEvent
      lazy_reset_opacity
      win = X::Window.new(@display, ev.window)
      display_osd(win)
    end
  end
end

#top_window(win) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/wmadd.rb', line 97

def top_window(win)
  top = win
  while top.parent && top.parent.id != @root.id
    top = top.parent
  end
  top
end

#watch_resize_event(win) ⇒ Object



160
161
162
# File 'lib/wmadd.rb', line 160

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

#window_list(win = @root) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/wmadd.rb', line 148

def window_list(win=@root)
  list = []
  win.query_tree&.each do |w|
    if w.window_attributes.map_state == X::IS_VIEWABLE && w.window_type == :normal
      list.push w
    else
      list.push window_list(w)
    end
  end
  list.flatten
end

#wm_window(win) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/wmadd.rb', line 105

def wm_window(win)
  while win && win.parent&.id != @root.id
    if win.window_attributes.map_state == X::IS_VIEWABLE && win.window_type == :normal
      return win
    end
    win = win.parent
  end
  nil
end