forked from deter0/xborder
/
xborder
executable file
·273 lines (233 loc) · 7.19 KB
/
xborder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/python3
import argparse
import sys
import cairo
import gi
from xdo import Xdo
xdo = Xdo()
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk
import math
import typing as t
import subprocess
import queue
DESKTOP_DRAG_STRESS_TOY = True;
BORDER_RADIUS = 14;
BORDER_WIDTH = 4.0;
BORDER_COLOR = [100,78,248,1]
def set_color(color_in):
global BORDER_COLOR
alpha = 1
color_in = color_in.lstrip("#")
if len(color_in) == 6:
color = [int(color_in[i:i+2], 16) for i in (0, 2, 4)]
elif len(color_in) == 8:
color = [int(color_in[i:i+2], 16) for i in (0, 2, 4)]
alpha = int(color_in[6:8], 16) / 255
else:
raise Exception("Bad color value!")
BORDER_COLOR = color + [alpha]
def get_win_xdo():
win = None
try:
win = xdo.get_active_window()
except:
return None, None;
if (win):
size = xdo.get_window_size(win)
position = xdo.get_window_location(win)
return position, size, True;
else:
return None, None;
def i3_get_data(win):
win_rect, offset = win.window_rect, win.rect
return {
"x": win_rect.x + offset.x,
"y": win_rect.y + offset.y,
"width": win_rect.width or offset.width,
"height": win_rect.height or offset.height,
"fullscreen": win.fullscreen_mode > 0,
"window": win.window
}
def init_i3():
from multiprocessing import Process, Queue
global i3_win_queue
i3_win_queue = Queue()
def run_proc(q):
i3_con = i3ipc.Connection()
def window_event_handler(con, e):
win = con.get_tree().find_focused()
q.put(i3_get_data(win))
i3_con.on(i3ipc.Event.WINDOW, window_event_handler)
i3_con.on(i3ipc.Event.WORKSPACE_FOCUS, window_event_handler)
window_event_handler(i3_con, None)
i3_con.main()
Process(target=run_proc, args=(i3_win_queue,)).start()
i3_win_queue = None
i3_win_cached = None
def get_win_i3():
global i3_win_cached
try:
win = i3_win_queue.get(block=False)
i3_win_cached = win
if not win["window"]: #no window in workspace
global rectangle
rectangle = cairo.RectangleInt(win["x"], win["y"], win["width"], win["height"])
win = i3_win_cached = None
except queue.Empty:
win = i3_win_cached
except:
raise
return None, None
if (win):
win_rect = i3ipc.Rect(win)
return win_rect, win_rect, not win["fullscreen"]
else:
return None, None;
def get_screen_size(display):
mon_geoms = [
display.get_monitor(i).get_geometry()
for i in range(display.get_n_monitors())
]
x0 = min(r.x for r in mon_geoms)
y0 = min(r.y for r in mon_geoms)
x1 = max(r.x + r.width for r in mon_geoms)
y1 = max(r.y + r.height for r in mon_geoms)
return x1 - x0, y1 - y0
def draw_border(ctx, x: int, y: int,
width: int, height: int,
border_radius:int, border_size: int,
color: t.List[int], border_color:t.Optional[t.List[int]]) -> None:
ctx.set_source_rgba(color[0] / 255, color[1] / 255, color[2] / 255, color[3]);
if (border_radius > 0):
corner_radius = border_radius
radius = corner_radius;
degrees = math.pi / 180.0;
ctx.new_sub_path();
ctx.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
ctx.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
ctx.close_path();
ctx.fill_preserve()
if (border_size != 0):
ctx.set_source_rgba (border_color[0] / 255, border_color[1] / 255, border_color[2] / 255, border_color[3]);
ctx.set_line_width (border_size);
ctx.stroke();
else:
ctx.rectangle(x, y, width, height);
if (border_size != 0):
ctx.fill_preserve();
ctx.set_source_rgba(border_color[0] / 255, border_color[1] / 255, border_color[2] / 255, border_color[3]);
ctx.set_line_width(border_size);
ctx.stroke();
else:
ctx.fill();
return None;
root = Gdk.get_default_root_window()
screen = root.get_screen()
screen_width, screen_height = get_screen_size(Gdk.Display.get_default());
rectangle = cairo.RectangleInt(0, 0, screen_width, screen_height);
class Highlight (Gtk.Window):
def __init__(self):
super().__init__(type=Gtk.WindowType.POPUP)
self.set_app_paintable(True);
screen = self.get_screen();
visual = screen.get_rgba_visual();
self.set_visual(visual);
self.show_all();
self.resize(screen_width, screen_height);
self.move(0, 0);
self.fullscreen();
self.set_decorated(False);
self.set_skip_taskbar_hint(True)
self.set_skip_pager_hint(True)
self.set_keep_above(True)
self.set_type_hint(Gdk.WindowTypeHint.NOTIFICATION)
self.set_accept_focus(False)
self.set_focus_on_map(False)
self.input_shape_combine_region(cairo.Region())
self.drawingarea = Gtk.DrawingArea()
self.drawingarea.connect ('button-press-event',self.callback)
self.drawingarea.connect ('button-release-event',self.callback2)
Gdk.EventMask.POINTER_MOTION_MASK
self.drawingarea.set_events(Gdk.EventMask.EXPOSURE_MASK
| Gdk.EventMask.BUTTON_PRESS_MASK
| Gdk.EventMask.BUTTON_RELEASE_MASK )
self.add(self.drawingarea)
self.connect('draw', self._on_draw)
def callback(self, widget, event):
if event.button == 1:
self.rect_x = event.x
self.rect_y = event.y
return True
def callback2(self, widget, event):
if event.button == 1:
self.rect_x = None
self.rect_y = None
return True
rect_x = None;
rect_y = None;
def _on_draw(self, _wid, ctx):
if not screen.is_composited():
subprocess.Popen(['notify-send', "xborder", "xborders terminated due to no compositor."])
exit(1)
ctx.save()
position, size, *draw = get_win();
draw = draw[0] if len(draw) > 0 else None
if (position != None):
if draw:
draw_border(
ctx, position.x - (BORDER_WIDTH/2), position.y - (BORDER_WIDTH/2),
size.width + BORDER_WIDTH, size.height + BORDER_WIDTH,
BORDER_RADIUS, BORDER_WIDTH,
[0, 0, 0, 0],
BORDER_COLOR);
self.set_accept_focus(False)
self.set_focus_on_map(False)
self.input_shape_combine_region(cairo.Region())
self.rect_x = None;
self.rect_y = None;
else:
self.set_accept_focus(True)
self.set_focus_on_map(True)
self.input_shape_combine_region(cairo.Region(rectangle))
if (self.rect_x and self.rect_y):
if (DESKTOP_DRAG_STRESS_TOY):
current = xdo.get_mouse_location()
draw_border(
ctx, self.rect_x, self.rect_y,
current.x - self.rect_x, current.y - self.rect_y,
0, 4,
BORDER_COLOR[:3] + [BORDER_COLOR[3] * 0.5],
BORDER_COLOR[:3] + [BORDER_COLOR[3] * 0.85]
)
ctx.restore()
self.queue_draw()
def main():
win = Highlight();
win.connect("destroy", Gtk.main_quit);
win.set_keep_above(True);
win.set_title("Highlight")
win.show_all()
Gtk.main()
if __name__=="__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument("wm", action="store", nargs="?", default="")
parser.add_argument("--color", action="store", default="#644ef8")
args = parser.parse_args()
set_color(args.color)
global get_win
get_win = get_win_xdo
if args.wm == "i3":
import i3ipc
print("i3-specific support enabled")
init_i3()
get_win = get_win_i3
main()
except KeyboardInterrupt:
exit(0)
else:
print("This program is not meant to be imported to other Python modules. Please run xborders as a standalone script!")