dsimple.c revision 29cb5710
1/* 2 3Copyright 1993, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included 12in all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20OTHER DEALINGS IN THE SOFTWARE. 21 22Except as contained in this notice, the name of The Open Group shall 23not be used in advertising or otherwise to promote the sale, use or 24other dealings in this Software without prior written authorization 25from The Open Group. 26 27*/ 28 29#include <X11/Xos.h> 30#include <X11/Xlib.h> 31#include <X11/Xutil.h> 32#include <X11/cursorfont.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <stdarg.h> 36/* 37 * Other_stuff.h: Definitions of routines in other_stuff. 38 * 39 * Written by Mark Lillibridge. Last updated 7/1/87 40 */ 41 42#include "clientwin.h" 43#include "dsimple.h" 44 45/* 46 * Just_display: A group of routines designed to make the writting of simple 47 * X11 applications which open a display but do not open 48 * any windows much faster and easier. Unless a routine says 49 * otherwise, it may be assumed to require program_name, dpy, 50 * and screen already defined on entry. 51 * 52 * Written by Mark Lillibridge. Last updated 7/1/87 53 */ 54 55 56/* This stuff is defined in the calling program by just_display.h */ 57char *program_name = "unknown_program"; 58Display *dpy = NULL; 59int screen = 0; 60 61 62/* 63 * Get_Display_Name (argc, argv) Look for -display, -d, or host:dpy (obselete) 64 * If found, remove it from command line. Don't go past a lone -. 65 */ 66char *Get_Display_Name( 67 int *pargc, /* MODIFIED */ 68 char **argv) /* MODIFIED */ 69{ 70 int argc = *pargc; 71 char **pargv = argv+1; 72 char *displayname = NULL; 73 int i; 74 75 for (i = 1; i < argc; i++) { 76 char *arg = argv[i]; 77 78 if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) { 79 if (++i >= argc) usage (); 80 81 displayname = argv[i]; 82 *pargc -= 2; 83 continue; 84 } 85 if (!strcmp(arg,"-")) { 86 while (i<argc) 87 *pargv++ = argv[i++]; 88 break; 89 } 90 *pargv++ = arg; 91 } 92 93 *pargv = NULL; 94 return (displayname); 95} 96 97 98/* 99 * Open_Display: Routine to open a display with correct error handling. 100 * Does not require dpy or screen defined on entry. 101 */ 102Display *Open_Display(const char *display_name) 103{ 104 Display *d; 105 106 d = XOpenDisplay(display_name); 107 if (d == NULL) { 108 fprintf (stderr, "%s: unable to open display '%s'\n", 109 program_name, XDisplayName (display_name)); 110 exit(1); 111 } 112 113 return(d); 114} 115 116 117/* 118 * Setup_Display_And_Screen: This routine opens up the correct display (i.e., 119 * it calls Get_Display_Name) and then stores a 120 * pointer to it in dpy. The default screen 121 * for this display is then stored in screen. 122 * Does not require dpy or screen defined. 123 */ 124void Setup_Display_And_Screen( 125 int *argc, /* MODIFIED */ 126 char **argv) /* MODIFIED */ 127{ 128 char *displayname = NULL; 129 130 displayname = Get_Display_Name(argc, argv); 131 dpy = Open_Display (displayname); 132 screen = XDefaultScreen(dpy); 133} 134 135/* 136 * Close_Display: Close display 137 */ 138void Close_Display(void) 139{ 140 if (dpy == NULL) 141 return; 142 143 XCloseDisplay(dpy); 144 dpy = NULL; 145} 146 147 148/* 149 * Select_Window_Args: a rountine to provide a common interface for 150 * applications that need to allow the user to select one 151 * window on the screen for special consideration. 152 * This routine implements the following command line 153 * arguments: 154 * 155 * -root Selects the root window. 156 * -id <id> Selects window with id <id>. <id> may 157 * be either in decimal or hex. 158 * -name <name> Selects the window with name <name>. 159 * 160 * Call as Select_Window_Args(&argc, argv) in main before 161 * parsing any of your program's command line arguments. 162 * Select_Window_Args will remove its arguments so that 163 * your program does not have to worry about them. 164 * The window returned is the window selected or 0 if 165 * none of the above arguments was present. If 0 is 166 * returned, Select_Window should probably be called after 167 * all command line arguments, and other setup is done. 168 * For examples of usage, see xwininfo, xwd, or xprop. 169 */ 170Window Select_Window_Args( 171 int *rargc, 172 char **argv) 173#define ARGC (*rargc) 174{ 175 int nargc=1; 176 int argc; 177 char **nargv; 178 Window w=0; 179 180 nargv = argv+1; argc = ARGC; 181#define OPTION argv[0] 182#define NXTOPTP ++argv, --argc>0 183#define NXTOPT if (++argv, --argc==0) usage() 184#define COPYOPT nargv++[0]=OPTION, nargc++ 185 186 while (NXTOPTP) { 187 if (!strcmp(OPTION, "-")) { 188 COPYOPT; 189 while (NXTOPTP) 190 COPYOPT; 191 break; 192 } 193 if (!strcmp(OPTION, "-root")) { 194 w=RootWindow(dpy, screen); 195 continue; 196 } 197 if (!strcmp(OPTION, "-name")) { 198 NXTOPT; 199 w = Window_With_Name(dpy, RootWindow(dpy, screen), 200 OPTION); 201 if (!w) 202 Fatal_Error("No window with name %s exists!",OPTION); 203 continue; 204 } 205 if (!strcmp(OPTION, "-id")) { 206 NXTOPT; 207 w=0; 208 sscanf(OPTION, "0x%lx", &w); 209 if (!w) 210 sscanf(OPTION, "%lu", &w); 211 if (!w) 212 Fatal_Error("Invalid window id format: %s.", OPTION); 213 continue; 214 } 215 COPYOPT; 216 } 217 ARGC = nargc; 218 219 return(w); 220} 221 222/* 223 * Other_stuff: A group of routines which do common X11 tasks. 224 * 225 * Written by Mark Lillibridge. Last updated 7/1/87 226 */ 227 228 229/* 230 * Routine to let user select a window using the mouse 231 */ 232 233Window Select_Window(Display *dpy, int descend) 234{ 235 int status; 236 Cursor cursor; 237 XEvent event; 238 Window target_win = None, root = RootWindow(dpy,screen); 239 int buttons = 0; 240 241 /* Make the target cursor */ 242 cursor = XCreateFontCursor(dpy, XC_crosshair); 243 244 /* Grab the pointer using target cursor, letting it room all over */ 245 status = XGrabPointer(dpy, root, False, 246 ButtonPressMask|ButtonReleaseMask, GrabModeSync, 247 GrabModeAsync, root, cursor, CurrentTime); 248 if (status != GrabSuccess) Fatal_Error("Can't grab the mouse."); 249 250 /* Let the user select a window... */ 251 while ((target_win == None) || (buttons != 0)) { 252 /* allow one more event */ 253 XAllowEvents(dpy, SyncPointer, CurrentTime); 254 XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event); 255 switch (event.type) { 256 case ButtonPress: 257 if (target_win == None) { 258 target_win = event.xbutton.subwindow; /* window selected */ 259 if (target_win == None) target_win = root; 260 } 261 buttons++; 262 break; 263 case ButtonRelease: 264 if (buttons > 0) /* there may have been some down before we started */ 265 buttons--; 266 break; 267 } 268 } 269 270 XUngrabPointer(dpy, CurrentTime); /* Done with pointer */ 271 272 if (!descend || (target_win == root)) 273 return(target_win); 274 275 target_win = Find_Client(dpy, root, target_win); 276 277 return(target_win); 278} 279 280 281/* 282 * Window_With_Name: routine to locate a window with a given name on a display. 283 * If no window with the given name is found, 0 is returned. 284 * If more than one window has the given name, the first 285 * one found will be returned. Only top and its subwindows 286 * are looked at. Normally, top should be the RootWindow. 287 */ 288Window Window_With_Name( 289 Display *dpy, 290 Window top, 291 const char *name) 292{ 293 Window *children, dummy; 294 unsigned int nchildren; 295 int i; 296 Window w=0; 297 char *window_name; 298 299 if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name)) 300 return(top); 301 302 if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren)) 303 return(0); 304 305 for (i=0; i<nchildren; i++) { 306 w = Window_With_Name(dpy, children[i], name); 307 if (w) 308 break; 309 } 310 if (children) XFree ((char *)children); 311 return(w); 312} 313 314/* 315 * outl: a debugging routine. Flushes stdout then prints a message on stderr 316 * and flushes stderr. Used to print messages when past certain points 317 * in code so we can tell where we are. Outl may be invoked like 318 * printf with up to 7 arguments. 319 */ 320void 321outl(char *msg, ...) 322{ 323 va_list args; 324 fflush(stdout); 325 va_start(args, msg); 326 vfprintf(stderr, msg, args); 327 va_end(args); 328 fprintf(stderr, "\n"); 329 fflush(stderr); 330} 331 332 333/* 334 * Standard fatal error routine - call like printf but maximum of 7 arguments. 335 * Does not require dpy or screen defined. 336 */ 337void Fatal_Error(char *msg, ...) 338{ 339 va_list args; 340 fflush(stdout); 341 fflush(stderr); 342 fprintf(stderr, "%s: error: ", program_name); 343 va_start(args, msg); 344 vfprintf(stderr, msg, args); 345 va_end(args); 346 fprintf(stderr, "\n"); 347 Close_Display(); 348 exit(EXIT_FAILURE); 349} 350