|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# examples/xdamage.py -- demonstrate damage extension |
| 4 | +# |
| 5 | +# Copyright (C) 2019 Mohit Garg <mrmohitgarg1990@gmail.com> |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public License |
| 9 | +# as published by the Free Software Foundation; either version 2.1 |
| 10 | +# of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | +# See the GNU Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the |
| 19 | +# Free Software Foundation, Inc., |
| 20 | +# 59 Temple Place, |
| 21 | +# Suite 330, |
| 22 | +# Boston, MA 02111-1307 USA |
| 23 | + |
| 24 | + |
| 25 | +# Python 2/3 compatibility. |
| 26 | +from __future__ import print_function |
| 27 | + |
| 28 | +import sys |
| 29 | +import os |
| 30 | + |
| 31 | +# Change path so we find Xlib |
| 32 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 33 | + |
| 34 | +from Xlib import display, X, threaded,Xutil |
| 35 | +import time |
| 36 | +import thread |
| 37 | +from Xlib.ext import damage |
| 38 | +from PIL import Image, ImageTk |
| 39 | +import traceback |
| 40 | + |
| 41 | +def redraw(win, gc): |
| 42 | + # win.clear_area() |
| 43 | + win.fill_rectangle(gc, 0, 0, 60, 60) |
| 44 | + |
| 45 | +def blink(display, win, gc, cols): |
| 46 | + while 1: |
| 47 | + time.sleep(2) |
| 48 | + print('Changing color', cols[0]) |
| 49 | + gc.change(foreground = cols[0]) |
| 50 | + cols = (cols[1], cols[0]) |
| 51 | + redraw(win, gc) |
| 52 | + display.flush() |
| 53 | + |
| 54 | +def get_image_from_win(win, pt_w, pt_h, pt_x=0, pt_y=0): |
| 55 | + try: |
| 56 | + raw = win.get_image(ptX,ptY, ptW,ptH, X.ZPixmap, 0xffffffff) |
| 57 | + image = Image.frombytes("RGB", (ptW, ptH), raw.data, "raw", "BGRX") |
| 58 | + return image |
| 59 | + |
| 60 | + except Exception: |
| 61 | + traceback.print_exc() |
| 62 | + |
| 63 | +def check_ext(disp): |
| 64 | + # Check for extension |
| 65 | + if not disp.has_extension('DAMAGE'): |
| 66 | + sys.stderr.write('server does not have the DAMAGE extension\n') |
| 67 | + sys.stderr.write("\n".join(disp.list_extensions())) |
| 68 | + |
| 69 | + if disp.query_extension('DAMAGE') is None: |
| 70 | + sys.exit(1) |
| 71 | + else: |
| 72 | + r = disp.damage_query_version() |
| 73 | + print('DAMAGE version {}.{}'.format(r.major_version, r.minor_version)) |
| 74 | + |
| 75 | +def main(): |
| 76 | + d = display.Display() |
| 77 | + root = d.screen().root |
| 78 | + |
| 79 | + check_ext(d) |
| 80 | + colormap = d.screen().default_colormap |
| 81 | + |
| 82 | + red = colormap.alloc_named_color("red").pixel |
| 83 | + blue = colormap.alloc_named_color("blue").pixel |
| 84 | + background = colormap.alloc_named_color("white").pixel |
| 85 | + |
| 86 | + window1 = root.create_window(100, 100, 250, 100, 1, |
| 87 | + X.CopyFromParent, X.InputOutput, |
| 88 | + X.CopyFromParent, |
| 89 | + background_pixel = background, |
| 90 | + event_mask = X.StructureNotifyMask | X.ExposureMask) |
| 91 | + window1.set_wm_name('Changing Window') |
| 92 | + window1.map() |
| 93 | + gc = window1.create_gc(foreground = red) |
| 94 | + |
| 95 | + thread.start_new_thread(blink, (d, window1, gc, (blue, red))) |
| 96 | + |
| 97 | + window1.damage_create(damage.DamageReportRawRectangles) |
| 98 | + window1.set_wm_normal_hints( |
| 99 | + flags=(Xutil.PPosition | Xutil.PSize | Xutil.PMinSize), |
| 100 | + min_width=50, |
| 101 | + min_height=50 |
| 102 | + ) |
| 103 | + |
| 104 | + window2 = root.create_window(100, 250, 250, 100, 1, |
| 105 | + X.CopyFromParent, X.InputOutput, |
| 106 | + X.CopyFromParent, |
| 107 | + background_pixel = background, |
| 108 | + event_mask = X.StructureNotifyMask | X.ExposureMask) |
| 109 | + window2.set_wm_normal_hints( |
| 110 | + flags=(Xutil.PPosition | Xutil.PSize | Xutil.PMinSize), |
| 111 | + min_width=50, |
| 112 | + min_height=50 |
| 113 | + ) |
| 114 | + |
| 115 | + window2.set_wm_name('Tracking Window') |
| 116 | + window2.map() |
| 117 | + |
| 118 | + while 1: |
| 119 | + event = d.next_event() |
| 120 | + if event.type == X.Expose: |
| 121 | + if event.count == 0: |
| 122 | + redraw(window1, gc) |
| 123 | + elif event.type == d.extension_event.DamageNotify: |
| 124 | + image = get_image_from_win(window1, event.area.width, event.area.height, event.area.x, event.area.y) |
| 125 | + bgpm = window2.create_pixmap(image.width, image.height, d.screen().root_depth) |
| 126 | + bggc = window2.create_gc(foreground=0, background=0) |
| 127 | + bgpm.put_pil_image(bggc, 0, 0, image) |
| 128 | + window2.copy_area(bggc, bgpm, 0, 0, image.width, image.height, 0, 0) |
| 129 | + # bggc.free() |
| 130 | + elif event.type == X.DestroyNotify: |
| 131 | + sys.exit(0) |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
0 commit comments