11#include <stdio.h>
22#include <stdlib.h>
3+ #include <stdbool.h>
4+ #include <unistd.h>
35#include <X11/Xlib.h>
46#include <X11/Xutil.h>
57#include <X11/X.h>
68#include <X11/cursorfont.h>
9+ #include <cairo/cairo.h>
10+ #include <cairo/cairo-xlib.h>
711
812struct MousePosition {
913 int x1 ,
@@ -38,7 +42,7 @@ struct MousePosition *getMouse(void) {
3842 mousePosition -> y2 = event .xbutton .y_root ;
3943
4044 }
41-
45+
4246 break ;
4347 }
4448
@@ -49,6 +53,52 @@ struct MousePosition *getMouse(void) {
4953 }
5054 }
5155
56+ XVisualInfo vinfo ;
57+ if (!XMatchVisualInfo (display , DefaultScreen (display ), 32 , TrueColor , & vinfo )) {
58+ printf ("No visual found supporting 32 bit color, terminating\n" );
59+ exit (EXIT_FAILURE );
60+ }
61+
62+ attrs .colormap = XCreateColormap (display , root , vinfo .visual , AllocNone );
63+ attrs .background_pixel = 0 ;
64+ attrs .border_pixel = 0 ;
65+
66+ Window overlay ;
67+
68+ if (mousePosition -> x1 < mousePosition -> x2 ) {
69+ overlay = XCreateWindow (
70+ display , root ,
71+ mousePosition -> x1 , mousePosition -> y1 , mousePosition -> x2 - mousePosition -> x1 , mousePosition -> y2 - mousePosition -> y1 , 0 ,
72+ vinfo .depth , InputOutput ,
73+ vinfo .visual ,
74+ CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel , & attrs
75+ );
76+
77+ } else {
78+ overlay = XCreateWindow (
79+ display , root ,
80+ mousePosition -> x2 , mousePosition -> y2 , mousePosition -> x1 - mousePosition -> x2 , mousePosition -> y1 - mousePosition -> y2 , 0 ,
81+ vinfo .depth , InputOutput ,
82+ vinfo .visual ,
83+ CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel , & attrs
84+ );
85+ }
86+
87+ XMapWindow (display , overlay );
88+
89+ cairo_surface_t * surf = cairo_xlib_surface_create (display , overlay , vinfo .visual , 0 , 0 );
90+ cairo_t * cr = cairo_create (surf );
91+
92+ XFlush (display );
93+
94+ sleep (1 );
95+
96+ cairo_destroy (cr );
97+ cairo_surface_destroy (surf );
98+
99+ XUnmapWindow (display , overlay );
100+ XCloseDisplay (display );
101+
52102 XUngrabPointer (display , CurrentTime );
53103
54104 return mousePosition ;
@@ -59,6 +109,9 @@ struct MousePosition *getMouseAndKeyboard(void) {
59109 Window root = XRootWindow (display , 0 );
60110 XEvent event ;
61111
112+ XSetWindowAttributes attrs ;
113+ attrs .override_redirect = true;
114+
62115 Cursor cursor = XCreateFontCursor (display , XC_crosshair );
63116 XGrabPointer (display , root , False , ButtonPressMask , GrabModeAsync , GrabModeAsync , None , cursor , CurrentTime );
64117 XGrabKeyboard (display , root , KeyPressMask , GrabModeAsync , GrabModeAsync , CurrentTime );
@@ -98,6 +151,52 @@ struct MousePosition *getMouseAndKeyboard(void) {
98151 }
99152 }
100153
154+ XVisualInfo vinfo ;
155+ if (!XMatchVisualInfo (display , DefaultScreen (display ), 32 , TrueColor , & vinfo )) {
156+ printf ("No visual found supporting 32 bit color, terminating\n" );
157+ exit (EXIT_FAILURE );
158+ }
159+
160+ attrs .colormap = XCreateColormap (display , root , vinfo .visual , AllocNone );
161+ attrs .background_pixel = 0 ;
162+ attrs .border_pixel = 0 ;
163+
164+ Window overlay ;
165+
166+ if (mousePosition -> x1 < mousePosition -> x2 ) {
167+ overlay = XCreateWindow (
168+ display , root ,
169+ mousePosition -> x1 , mousePosition -> y1 , mousePosition -> x2 - mousePosition -> x1 , mousePosition -> y2 - mousePosition -> y1 , 0 ,
170+ vinfo .depth , InputOutput ,
171+ vinfo .visual ,
172+ CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel , & attrs
173+ );
174+
175+ } else {
176+ overlay = XCreateWindow (
177+ display , root ,
178+ mousePosition -> x2 , mousePosition -> y2 , mousePosition -> x1 - mousePosition -> x2 , mousePosition -> y1 - mousePosition -> y2 , 0 ,
179+ vinfo .depth , InputOutput ,
180+ vinfo .visual ,
181+ CWOverrideRedirect | CWColormap | CWBackPixel | CWBorderPixel , & attrs
182+ );
183+ }
184+
185+ XMapWindow (display , overlay );
186+
187+ cairo_surface_t * surf = cairo_xlib_surface_create (display , overlay , vinfo .visual , 0 , 0 );
188+ cairo_t * cr = cairo_create (surf );
189+
190+ XFlush (display );
191+
192+ sleep (1 );
193+
194+ cairo_destroy (cr );
195+ cairo_surface_destroy (surf );
196+
197+ XUnmapWindow (display , overlay );
198+ XCloseDisplay (display );
199+
101200 XUngrabPointer (display , CurrentTime );
102201 XUngrabKeyboard (display , CurrentTime );
103202
0 commit comments