1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/*
* unclutter: remove idle cursor image from screen so that it doesnt
* obstruct the area you are looking at.
* doesn't do it if cursor is in root window or a button is down.
* polls mouse to see if is stationary, or waits for a keyup event on the
* screen. These will only arrive in windows of applications that dont
* wait for keyup themselves. We could only do better by using the XTest
* extensions and so getting all keystroke events.
* Tries to cope with jitter if you have a mouse that twitches.
* Unfortunately, clients like emacs set different text cursor
* shapes depending on whether they have pointer focus or not.
* Try to kid them with a synthetic EnterNotify event.
* Whereas version 1 did a grab cursor, version 2 creates a small subwindow.
* This may work better with some window managers.
* Some servers return a Visibility event when the subwindow is mapped.
* Sometimes this is Unobscured, or even FullyObscured. Ignore these and
* rely on LeaveNotify events. (An InputOnly window is not supposed to get
* visibility events.)
* Mark M Martin. cetia feb 1994 mmm@cetia.fr
* keystroke code from Bill Trost trost@cloud.rain.com
*/
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <stdio.h>
#include "vroot.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
char *progname;
void
pexit(char *str){
fprintf(stderr,"%s: %s\n",progname,str);
exit(1);
}
void
usage(){
pexit("usage:\n\
-display <display>\n\
-idle <seconds> time between polls to detect idleness.\n\
-keystroke wait for keystroke before idling.\n\
-jitter <pixels> pixels mouse can twitch without moving\n\
-grab use grabpointer method not createwindow\n\
-reset reset the timer whenever cursor becomes\n\
visible even if it hasn't moved\n\
-root apply to cursor on root window too\n\
-onescreen apply only to given screen of display\n\
-visible ignore visibility events\n\
-noevents don't send pseudo events\n\
-regex name or class below is a regular expression\n\
-not names... don't apply to windows whose wm-name begins.\n\
(must be last argument)\n\
-notname names... same as -not names...\n\
-notclass classes... don't apply to windows whose wm-class begins.\n\
(must be last argument, cannot be used with\n\
-not or -notname)");
}
static void dsleep(float t)
{
struct timeval tv;
assert(t >= 0);
tv.tv_sec = (int) t;
tv.tv_usec = (t - tv.tv_sec) * 1000000;
select(0, NULL, NULL, NULL, &tv);
}
#define ALMOSTEQUAL(a,b) (abs(a-b)<=jitter)
#define ANYBUTTON (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
/* Since the small window we create is a child of the window the pointer is
* in, it can be destroyed by its adoptive parent. Hence our destroywindow()
* can return an error, saying it no longer exists. Similarly, the parent
* window can disappear while we are trying to create the child. Trap and
* ignore these errors.
*/
int (*defaulthandler)(Display *display, XErrorEvent *error);
int errorhandler(Display *display, XErrorEvent *error)
{
if(error->error_code!=BadWindow)
(*defaulthandler)(display,error);
}
char **names = 0; /* -> argv list of names to avoid */
char **classes = 0; /* -> argv list of classes to avoid */
regex_t *nc_re = 0; /* regex for list of classes/names to avoid */
/*
* return true if window has a wm_name (class) and the start of it matches
* one of the given names (classes) to avoid
*/
int
nameinlist(Display *display, Window window)
{
char **cpp;
char *name = 0;
if(names)
XFetchName (display, window, &name);
else if(classes){
XClassHint *xch = XAllocClassHint();
if(XGetClassHint (display, window, xch))
name = strdup(xch->res_class);
if(xch)
XFree(xch);
}else
return 0;
if(name){
if(nc_re){
if(!regexec(nc_re, name, 0, 0, 0)) {
XFree(name);
return 1;
}
}else{
for(cpp = names!=0 ? names : classes;*cpp!=0;cpp++){
if(strncmp(*cpp,name,strlen(*cpp))==0)
break;
}
XFree(name);
return(*cpp!=0);
}
}
return 0;
}
/*
* create a small 1x1 curssor with all pixels masked out on the given screen.
*/
Cursor
createnullcursor(Display *display, Window root)
{
Pixmap cursormask;
XGCValues xgc;
GC gc;
XColor dummycolour;
Cursor cursor;
cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
xgc.function = GXclear;
gc = XCreateGC(display, cursormask, GCFunction, &xgc);
XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
dummycolour.pixel = 0;
dummycolour.red = 0;
dummycolour.flags = 04;
cursor = XCreatePixmapCursor(display, cursormask, cursormask,
&dummycolour,&dummycolour, 0,0);
XFreePixmap(display,cursormask);
XFreeGC(display,gc);
return cursor;
}
int
main(int argc, char **argv){
Display *display;
int screen,oldx = -99,oldy = -99,numscreens;
int doroot = 0, jitter = 0, usegrabmethod = 0, waitagain = 0,
dovisible = 1, doevents = 1, onescreen = 0;
float idletime = 5.0;
Cursor *cursor;
Window *realroot;
Window root;
char *displayname = 0;
progname = *argv;
argc--;
while(argv++,argc-->0){
if(strcmp(*argv,"-idle")==0){
argc--,argv++;
if(argc<0)usage();
idletime = atof(*argv);
}else if(strcmp(*argv,"-keystroke")==0){
idletime = -1;
}else if(strcmp(*argv,"-jitter")==0){
argc--,argv++;
if(argc<0)usage();
jitter = atoi(*argv);
}else if(strcmp(*argv,"-noevents")==0){
doevents = 0;
}else if(strcmp(*argv,"-root")==0){
doroot = 1;
}else if(strcmp(*argv,"-grab")==0){
usegrabmethod = 1;
}else if(strcmp(*argv,"-reset")==0){
waitagain = 1;
}else if(strcmp(*argv,"-onescreen")==0){
onescreen = 1;
}else if(strcmp(*argv,"-visible")==0){
dovisible = 0;
}else if(strcmp(*argv,"-regex")==0){
nc_re = (regex_t *)malloc(sizeof(regex_t));
}else if(strcmp(*argv,"-not")==0 || strcmp(*argv,"-notname")==0){
/* take rest of srg list */
names = ++argv;
if(*names==0)names = 0; /* no args follow */
argc = 0;
}else if(strcmp(*argv,"-notclass")==0){
/* take rest of arg list */
classes = ++argv;
if(*classes==0)classes = 0; /* no args follow */
argc = 0;
}else if(strcmp(*argv,"-display")==0 || strcmp(*argv,"-d")==0){
argc--,argv++;
if(argc<0)usage();
displayname = *argv;
}else usage();
}
/* compile a regex from the first name or class */
if(nc_re){
if(names || classes){
if (regcomp(nc_re, (names != 0 ? *names : *classes),
REG_EXTENDED | REG_NOSUB)) { /* error */
free(nc_re);
names = classes = 0;
nc_re = 0;
}
}else{ /* -regex without -not... ... */
free(nc_re);
nc_re = 0;
}
}
display = XOpenDisplay(displayname);
if(display==0)pexit("could not open display");
numscreens = ScreenCount(display);
cursor = (Cursor*) malloc(numscreens*sizeof(Cursor));
realroot = (Window*) malloc(numscreens*sizeof(Window));
/* each screen needs its own empty cursor.
* note each real root id so can find which screen we are on
*/
for(screen = 0;screen<numscreens;screen++)
if(onescreen && screen!=DefaultScreen(display)){
realroot[screen] = -1;
cursor[screen] = -1;
}else{
realroot[screen] = XRootWindow(display,screen);
cursor[screen] = createnullcursor(display,realroot[screen]);
if(idletime<0)
XSelectInput(display,realroot[screen],KeyReleaseMask);
}
screen = DefaultScreen(display);
root = VirtualRootWindow(display,screen);
if(!usegrabmethod)
defaulthandler = XSetErrorHandler(errorhandler);
/*
* create a small unmapped window on a screen just so xdm can use
* it as a handle on which to killclient() us.
*/
XCreateWindow(display, realroot[screen], 0,0,1,1, 0, CopyFromParent,
InputOutput, CopyFromParent, 0, (XSetWindowAttributes*)0);
while(1){
Window dummywin,windowin,newroot;
int rootx,rooty,winx,winy;
unsigned int modifs;
Window lastwindowavoided = None;
/*
* wait for pointer to not move and no buttons down
* or if triggered by keystroke check no buttons down
*/
while(1){
if(idletime<0){ /* wait for keystroke trigger */
XEvent event;
do{
XNextEvent(display,&event);
}while(event.type != KeyRelease ||
(event.xkey.state & ANYBUTTON));
oldx = event.xkey.x_root;
oldy = event.xkey.y_root;
}
if(!XQueryPointer(display, root, &newroot, &windowin,
&rootx, &rooty, &winx, &winy, &modifs)){
/* window manager with virtual root may have restarted
* or we have changed screens */
if(!onescreen){
for(screen = 0;screen<numscreens;screen++)
if(newroot==realroot[screen])break;
if(screen>=numscreens)
pexit("not on a known screen");
}
root = VirtualRootWindow(display,screen);
}else if((!doroot && windowin==None) || (modifs & ANYBUTTON) ||
!(ALMOSTEQUAL(rootx,oldx) && ALMOSTEQUAL(rooty,oldy))){
oldx = rootx, oldy = rooty;
}else if(windowin==None){
windowin = root;
break;
}else if(windowin!=lastwindowavoided){
/* descend tree of windows under cursor to bottommost */
Window childin;
int toavoid = xFalse;
lastwindowavoided = childin = windowin;
do{
windowin = childin;
if(nameinlist (display, windowin)){
toavoid = xTrue;
break;
}
}while(XQueryPointer(display, windowin, &dummywin,
&childin, &rootx, &rooty, &winx, &winy, &modifs)
&& childin!=None);
if(!toavoid){
lastwindowavoided = None;
break;
}
}
if(idletime>=0)
dsleep(idletime);
}
/* wait again next time */
if(waitagain)
oldx = -1-jitter;
if(usegrabmethod){
if(XGrabPointer(display, root, 0,
PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, cursor[screen],
CurrentTime)==GrabSuccess){
/* wait for a button event or large cursor motion */
XEvent event;
do{
XNextEvent(display,&event);
}while(event.type==KeyRelease ||
(event.type==MotionNotify &&
ALMOSTEQUAL(rootx,event.xmotion.x) &&
ALMOSTEQUAL(rooty,event.xmotion.y)));
XUngrabPointer(display, CurrentTime);
}else{
/* go to sleep to prevent tight loops */
if(idletime>=0)
dsleep(idletime);
}
}else{
XSetWindowAttributes attributes;
XEvent event;
Window cursorwindow;
/* create small input-only window under cursor
* as a sub window of the window currently under the cursor
*/
attributes.event_mask = LeaveWindowMask |
EnterWindowMask |
StructureNotifyMask |
FocusChangeMask;
if(dovisible)
attributes.event_mask |= VisibilityChangeMask;
attributes.override_redirect = True;
attributes.cursor = cursor[screen];
cursorwindow = XCreateWindow
(display, windowin,
winx-jitter, winy-jitter,
jitter*2+1, jitter*2+1, 0, CopyFromParent,
InputOnly, CopyFromParent,
CWOverrideRedirect | CWEventMask | CWCursor,
&attributes);
/* discard old events for previously created windows */
XSync(display,True);
XMapWindow(display,cursorwindow);
/*
* Dont wait for expose/map cos override and inputonly(?).
* Check that created window captured the pointer by looking
* for inevitable EnterNotify event that must follow MapNotify.
* [Bug fix thanks to Charles Hannum <mycroft@ai.mit.edu>]
*/
XSync(display,False);
if(!XCheckTypedWindowEvent(display, cursorwindow, EnterNotify,
&event))
oldx = -1-jitter; /* slow down retry */
else{
if(doevents){
/*
* send a pseudo EnterNotify event to the parent window
* to try to convince application that we didnt really leave it
*/
event.xcrossing.type = EnterNotify;
event.xcrossing.display = display;
event.xcrossing.window = windowin;
event.xcrossing.root = root;
event.xcrossing.subwindow = None;
event.xcrossing.time = CurrentTime;
event.xcrossing.x = winx;
event.xcrossing.y = winy;
event.xcrossing.x_root = rootx;
event.xcrossing.y_root = rooty;
event.xcrossing.mode = NotifyNormal;
event.xcrossing.same_screen = True;
event.xcrossing.focus = True;
event.xcrossing.state = modifs;
(void)XSendEvent(display,windowin,
True/*propagate*/,EnterWindowMask,&event);
}
/* wait till pointer leaves window */
do{
XNextEvent(display,&event);
}while(event.type!=LeaveNotify &&
/* Some gtk applications seem not to like this:
* event.type!=FocusOut &&
*/
event.type!=UnmapNotify &&
event.type!=ConfigureNotify &&
event.type!=CirculateNotify &&
event.type!=ReparentNotify &&
event.type!=DestroyNotify &&
(event.type!=VisibilityNotify ||
event.xvisibility.state==VisibilityUnobscured)
);
/* check if a second unclutter is running cos they thrash */
if(event.type==LeaveNotify &&
event.xcrossing.window==cursorwindow &&
event.xcrossing.detail==NotifyInferior)
pexit("someone created a sub-window to my sub-window! giving up");
}
XDestroyWindow(display, cursorwindow);
}
}
}
|