1+ #!/usr/bin/python
2+ #
3+ # examples/xdamage.py -- demonstrate damage extension
4+ #
5+ # Copyright (C) 2002 Peter Liljenberg <petli@ctrl-c.liu.se>
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+
40+ def redraw (win , gc ):
41+ # win.clear_area()
42+ win .fill_rectangle (gc , 0 , 0 , 60 , 60 )
43+
44+ def blink (display , win , gc , cols ):
45+ while 1 :
46+ time .sleep (2 )
47+ print ('Changing color' , cols [0 ])
48+ gc .change (foreground = cols [0 ])
49+ cols = (cols [1 ], cols [0 ])
50+ redraw (win , gc )
51+ display .flush ()
52+
53+ def getImageFromWin (win ,ptW ,ptH ,ptX = 0 ,ptY = 0 ):
54+ try :
55+ raw = win .get_image (ptX ,ptY , ptW ,ptH , X .ZPixmap , 0xffffffff )
56+ image = Image .frombytes ("RGB" , (ptW , ptH ), raw .data , "raw" , "BGRX" )
57+ return image
58+
59+ except Exception :
60+ print ("exception thrown in getImageFromWin" )
61+
62+ def checkExt (disp ):
63+ # Check for extension
64+ if not disp .has_extension ('DAMAGE' ):
65+ sys .stderr .write ('server does not have the DAMAGE extension\n ' )
66+ sys .stderr .write ("\n " .join (disp .list_extensions ()))
67+
68+ if disp .query_extension ('DAMAGE' ) is None :
69+ sys .exit (1 )
70+ else :
71+ r = disp .damage_query_version ()
72+ print ('DAMAGE version %d.%d' % (r .major_version , r .minor_version ))
73+
74+ def main ():
75+ d = display .Display ()
76+ root = d .screen ().root
77+
78+ checkExt (d )
79+ colormap = d .screen ().default_colormap
80+
81+ red = colormap .alloc_named_color ("red" ).pixel
82+ blue = colormap .alloc_named_color ("blue" ).pixel
83+ background = colormap .alloc_named_color ("white" ).pixel
84+
85+ window1 = root .create_window (100 , 100 , 250 , 100 , 1 ,
86+ X .CopyFromParent , X .InputOutput ,
87+ X .CopyFromParent ,
88+ background_pixel = background ,
89+ event_mask = X .StructureNotifyMask | X .ExposureMask )
90+ window1 .set_wm_name ('Changing Window' )
91+ window1 .map ()
92+ gc = window1 .create_gc (foreground = red )
93+
94+ thread .start_new_thread (blink , (d , window1 , gc , (blue , red )))
95+
96+ window1 .damage_create (damage .DamageReportRawRectangles )
97+ window1 .set_wm_normal_hints (
98+ flags = (Xutil .PPosition | Xutil .PSize | Xutil .PMinSize ),
99+ min_width = 50 ,
100+ min_height = 50
101+ )
102+
103+ window2 = root .create_window (100 , 250 , 250 , 100 , 1 ,
104+ X .CopyFromParent , X .InputOutput ,
105+ X .CopyFromParent ,
106+ background_pixel = background ,
107+ event_mask = X .StructureNotifyMask | X .ExposureMask )
108+ window2 .set_wm_normal_hints (
109+ flags = (Xutil .PPosition | Xutil .PSize | Xutil .PMinSize ),
110+ min_width = 50 ,
111+ min_height = 50
112+ )
113+
114+ window2 .set_wm_name ('Tracking Window' )
115+ window2 .map ()
116+
117+ while 1 :
118+ event = d .next_event ()
119+ if event .type == X .Expose :
120+ if event .count == 0 :
121+ redraw (window1 , gc )
122+ elif event .type == d .extension_event .DamageNotify :
123+ image = getImageFromWin (window1 , event .area .width , event .area .height , event .area .x , event .area .y )
124+ bgpm = window2 .create_pixmap (image .width ,image .height ,d .screen ().root_depth )
125+ bggc = window2 .create_gc (foreground = 0 ,background = 0 )
126+ bgpm .put_pil_image (bggc , 0 , 0 , image )
127+ window2 .copy_area (bggc , bgpm , 0 , 0 , image .width , image .height , 0 , 0 )
128+ # bggc.free()
129+ elif event .type == X .DestroyNotify :
130+ sys .exit (0 )
131+
132+ if __name__ == "__main__" :
133+ main ()
0 commit comments