1- #!/usr/bin/env python
2-
31import sys
42from Xlib import X , display , Xcursorfont
53
64
75def coordinates (start_point , end_point ):
6+ """
7+ :param start_point: Start co-ordinates of rectangle
8+ :param end_point: End co-ordinates of rectangle
9+ :return: dict of start co-ordinates, end co-ordinates, width, height of rectangle.
10+ """
811 X = dict (x = 0 , y = 0 )
912 Y = dict (x = 0 , y = 0 )
1013
@@ -18,11 +21,21 @@ def coordinates(start_point, end_point):
1821
1922
2023class XRectSel (object ):
21- def __init__ (self , _display = None , screen = None ):
24+ """
25+ Base class for python-xrectsel
26+
27+ TODO: Enhancement like colors, pixels customization.
28+ """
29+
30+ def __init__ (self , _display = None ):
31+ """
32+ :param _display: custom display else it will take default
33+ """
2234 self .display = _display if _display else display .Display ()
23- self .screen = screen if screen else self .display .screen ()
35+ self .screen = self .display .screen ()
2436 self .window = self .screen .root
2537
38+ # Grab mouse pointer
2639 self .window .grab_pointer (
2740 1 ,
2841 X .PointerMotionMask | X .ButtonReleaseMask | X .ButtonPressMask ,
@@ -33,6 +46,7 @@ def __init__(self, _display=None, screen=None):
3346 X .CurrentTime ,
3447 )
3548
49+ # Grab keyboard
3650 self .window .grab_keyboard (1 , X .GrabModeAsync , X .GrabModeAsync , X .CurrentTime )
3751
3852 self .gc = self .window .create_gc (
@@ -61,13 +75,17 @@ def _cursor(self):
6175 font , Xcursorfont .crosshair , Xcursorfont .crosshair + 1 , (65535 , 65535 , 65535 ), (0 , 0 , 0 )
6276 )
6377
64- def draw_rectangle (self , start_point , end_point ):
65- coords = coordinates (start_point , end_point )
78+ def draw_rectangle (self , coords ):
6679 self .window .rectangle (
6780 self .gc , coords ["start" ]["x" ], coords ["start" ]["y" ], coords ["width" ], coords ["height" ]
6881 )
6982
7083 def select (self ):
84+ """
85+ Draw rectangle with mouse events
86+
87+ :return: dict of start co-ordinates, end co-ordinates, width, height of rectangle.
88+ """
7189 start_point = {}
7290 end_point = {}
7391 tmp_point = {}
@@ -98,18 +116,25 @@ def select(self):
98116 continue
99117
100118 if tmp_point :
101- self .draw_rectangle (start_point , tmp_point )
119+ coords = coordinates (start_point , tmp_point )
120+ self .draw_rectangle (coords )
102121 tmp_point = None
103122
104123 tmp_point = dict (x = event .root_x , y = event .root_y )
105- self .draw_rectangle (start_point , tmp_point )
124+ coords = coordinates (start_point , tmp_point )
125+ self .draw_rectangle (coords )
106126
107127 # Mouse button release
108128 elif event .type == X .ButtonRelease :
109129 if tmp_point :
110- self . draw_rectangle (start_point , tmp_point )
111-
130+ coords = coordinates (start_point , tmp_point )
131+ self . draw_rectangle ( coords )
112132 end_point = dict (x = event .root_x , y = event .root_y )
113133
114134 self .display .flush ()
115- return start_point , end_point
135+ self .display .close ()
136+ return coordinates (start_point , end_point )
137+
138+
139+ if __name__ == "__main__" :
140+ print (XRectSel ().select ())
0 commit comments