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
|
#include "xb_config.h"
#include <stdio.h>
#ifndef vms
#include <unistd.h>
#else
#include <ssdef.h>
#endif
#include <string.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include "xbanner.h"
/* for now I need this */
#undef DEBUG
typedef enum { CommandLineSpecified, Unspecified } DispType;
/* Timeout times */
#define TMOUT_ONE (LINGER_DELAY*4)
#define TMOUT_ALL (TMOUT_ONE*20)
int main(int argc, char *argv[])
{
Display *disp;
Atom xprop,proptype;
char dispname[80];
DispType dtype = Unspecified;
Bool done=False,exited=False;
Window root;
unsigned long items,bytesleft;
unsigned char *propval;
int last,format;
unsigned long waittime,totalwait;
dispname[0]='\0';
if(argc==3)
{
if(strcmpi("-display",argv[1])==0)
{
strcpy(dispname,argv[2]);
dtype = CommandLineSpecified;
}
else
{
fprintf(stderr,"Freetemp %s - by Amit Margalit %s\nUsage: freetemp [-display <host>:<display>]\n",
VERSION,DATE);
exit(1);
}
}
else if(argc!=1)
{
fprintf(stderr,"Freetemp %s - by Amit Margalit %s\nUsage: freetemp [-display <host>:<display>]\n",
VERSION,DATE);
exit(1);
}
disp = XOpenDisplay(dtype==CommandLineSpecified ? dispname : NULL);
if(disp==NULL)
{
fprintf(stderr,"%s %s - could not open the display\n",argv[0],VERSION);
exit(1);
}
/* which is the root window ? */
root = DefaultRootWindow(disp);
#if !defined(vms)
#ifdef DEBUG
printf("Does prop exist?\n");
#endif
xprop = XInternAtom(disp,XPROPERTY,True);
if(xprop == None) /* nobody's waiting for us... */
done=True;
else
if(!XBPropExists(disp,root,xprop))
{
xprop = None;
done=True;
}
#ifdef DEBUG
if(done)
printf("Nope.\n");
else
printf("Yes.\n");
#endif
/* initialize the overall timer */
totalwait=0;
while(!done)
{
XGetWindowProperty(disp,root,xprop,0,20,False,XA_STRING,&proptype,
&format,&items,&bytesleft,&propval);
#ifdef DEBUG
printf("Got property: \"%s\"\n",propval);
#endif
last = propval[strlen((char*)propval)-1] - '0';
#ifdef DEBUG
printf("Last: %d\n",last);
#endif
if(propval[0]=='D' && propval[1]==last+'0')
{
#ifdef DEBUG
printf("Exited, propval=\"%s\"\n",propval);
#endif
if(last>0)
{
#ifdef DEBUG
printf("Remove one\n");
#endif
XChangeProperty(disp,root,xprop,XA_STRING,8,PropModeReplace,
propval,strlen((char*)propval)-1);
last--;
XFree(propval);
continue;
}
if(last<=0)
done=True;
XFree(propval);
continue;
}
/* write 'X%d' */
propval[0]='X';
propval[1]=last+'0';
XChangeProperty(disp,root,xprop,XA_STRING,8,PropModeReplace,
propval,strlen((char*)propval));
#ifdef DEBUG
printf("Asked #%d to exit.\n",last);
#endif
XFree(propval);
exited = False;
waittime = 0;
#ifdef DEBUG
printf("Wait till he exits.\n");
#endif
while(!exited && waittime <= TMOUT_ONE && totalwait <= TMOUT_ALL)
{
usleep(LINGER_DELAY);
waittime+=LINGER_DELAY;
totalwait+=LINGER_DELAY;
XGetWindowProperty(disp,root,xprop,0,20,False,XA_STRING,&proptype,
&format,&items,&bytesleft,&propval);
#ifdef DEBUG
printf("Got property: \"%s\"\n",propval);
#endif
if(propval[0]=='D' && propval[1]==last+'0')
{
exited=True;
#ifdef DEBUG
printf("Exited, propval=\"%s\"\n",propval);
#endif
}
else /* otherwise we can't use it when the while() ends */
XFree(propval);
}
#ifdef DEBUG
if(waittime>TMOUT_ONE)
printf("Timed out waiting for #%d\n",last);
printf("Exited=%s\n",(exited?"True":"False"));
#endif
if(totalwait > TMOUT_ALL)
{
#ifdef DEBUG
printf("TotalWait > TMOUT_ALL!!!\n");
#endif
break;
}
if(exited && last>0)
{
#ifdef DEBUG
printf("Remove one\n");
#endif
XChangeProperty(disp,root,xprop,XA_STRING,8,PropModeReplace,
propval,strlen((char*)propval)-1);
last--;
XFree(propval);
continue;
}
if(exited && last<=0)
done=True;
XFree(propval);
}
/* sleep as a safety margin */
usleep(LINGER_DELAY);
if(xprop!=None)
{
#ifdef DEBUG
printf("Del property\n");
#endif
XDeleteProperty(disp,root,xprop);
}
#endif /* !defined(vms) */
/* now free up the temporary resources */
XKillClient(disp,AllTemporary);
XSync(disp,True);
XCloseDisplay(disp);
#ifdef vms
return SS$_NORMAL;
#else
return 0;
#endif
}
|