| 1 |
|
| 2 |
|
| 3 |
import setproctitle |
| 4 |
import sys |
| 5 |
from functools import lru_cache |
| 6 |
import gi |
| 7 |
gi.require_version('Gtk', '3.0') |
| 8 |
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, Gio |
| 9 |
|
| 10 |
class Main(object): |
| 11 |
|
| 12 |
def __init__(self): |
| 13 |
|
| 14 |
self.window_x = 0 |
| 15 |
self.window_y = 0 |
| 16 |
self.w = Gtk.Window.new(Gtk.WindowType.TOPLEVEL) |
| 17 |
self.w.set_size_request(300, 300) |
| 18 |
self.w.set_title("Magnus") |
| 19 |
self.w.set_decorated(False) |
| 20 |
self.w.connect("destroy", lambda a: Gtk.main_quit()) |
| 21 |
self.w.connect("configure-event", self.window_configure) |
| 22 |
self.img = Gtk.Image() |
| 23 |
scrolled_window = Gtk.ScrolledWindow() |
| 24 |
scrolled_window.add(self.img) |
| 25 |
self.w.add(scrolled_window) |
| 26 |
self.w.show_all() |
| 27 |
sys.stdout.write('%d\n' % self.w.get_window().get_xid()) |
| 28 |
sys.stdout.flush() |
| 29 |
GLib.timeout_add(250, self.poll) |
| 30 |
Gtk.main() |
| 31 |
|
| 32 |
def poll(self, *args): |
| 33 |
|
| 34 |
loc = self.w.get_size() |
| 35 |
width = loc.width |
| 36 |
height = loc.height |
| 37 |
(screen, x, y) = Gdk.Display.get_default().get_default_seat().get_pointer().get_position() |
| 38 |
|
| 39 |
if (x > self.window_x and x <= (self.window_x + width + 0) and y > self.window_y and y <= (self.window_y + height + 0)): |
| 40 |
|
| 41 |
white = self.get_white_pixbuf(width, height) |
| 42 |
self.img.set_from_pixbuf(white) |
| 43 |
|
| 44 |
else: |
| 45 |
|
| 46 |
root = Gdk.get_default_root_window() |
| 47 |
scaled_width = width // 2 |
| 48 |
scaled_height = height // 2 |
| 49 |
scaled_xoff = scaled_width // 2 |
| 50 |
scaled_yoff = scaled_height // 2 |
| 51 |
screenshot = Gdk.pixbuf_get_from_window(root, x - scaled_xoff, y - scaled_yoff, scaled_width, scaled_height) |
| 52 |
scaled_pb = screenshot.scale_simple(width, height, GdkPixbuf.InterpType.NEAREST) |
| 53 |
self.img.set_from_pixbuf(scaled_pb) |
| 54 |
|
| 55 |
return True |
| 56 |
|
| 57 |
@lru_cache() |
| 58 |
def makesquares(self, overall_width, overall_height, square_size, value_on, value_off): |
| 59 |
|
| 60 |
on_sq = list(value_on) * square_size |
| 61 |
off_sq = list(value_off) * square_size |
| 62 |
on_row = [] |
| 63 |
off_row = [] |
| 64 |
|
| 65 |
while len(on_row) < overall_width * len(value_on): |
| 66 |
|
| 67 |
on_row += on_sq |
| 68 |
on_row += off_sq |
| 69 |
off_row += off_sq |
| 70 |
off_row += on_sq |
| 71 |
|
| 72 |
on_row = on_row[:overall_width * len(value_on)] |
| 73 |
off_row = off_row[:overall_width * len(value_on)] |
| 74 |
on_sq_row = on_row * square_size |
| 75 |
off_sq_row = off_row * square_size |
| 76 |
overall = [] |
| 77 |
count = 0 |
| 78 |
|
| 79 |
while len(overall) < overall_width * overall_height * len(value_on): |
| 80 |
|
| 81 |
overall += on_sq_row |
| 82 |
overall += off_sq_row |
| 83 |
count += 2 |
| 84 |
|
| 85 |
overall = overall[:overall_width * overall_height * len(value_on)] |
| 86 |
|
| 87 |
return overall |
| 88 |
|
| 89 |
@lru_cache() |
| 90 |
def get_white_pixbuf(self, width, height): |
| 91 |
|
| 92 |
square_size = 16 |
| 93 |
light = (153, 153, 153, 255) |
| 94 |
dark = (102, 102, 102, 255) |
| 95 |
whole = self.makesquares(width, height, square_size, light, dark) |
| 96 |
arr = GLib.Bytes.new(whole) |
| 97 |
|
| 98 |
return GdkPixbuf.Pixbuf.new_from_bytes(arr, GdkPixbuf.Colorspace.RGB, True, 8, width, height, width * len(light)) |
| 99 |
|
| 100 |
def window_configure(self, window, ev): |
| 101 |
|
| 102 |
self.window_x = ev.x |
| 103 |
self.window_y = ev.y |
| 104 |
|
| 105 |
if __name__ == "__main__": |
| 106 |
|
| 107 |
setproctitle.setproctitle('magnus') |
| 108 |
Main() |