Debian Codegraph @

contrib/xwintoppm/dsimple.c

raw ◀ ▶
1 Debian patch touch this file
1 /* $XConsortium: dsimple.c /main/17 1995/12/07 10:23:27 kaleb $ */
2 /* $XFree86: xc/programs/xwd/dsimple.c,v 3.0 1996/06/10 09:20:34 dawes Exp $ */
3 /*
4
5 Copyright (c) 1993 X Consortium
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
25
26 Except as contained in this notice, the name of the X Consortium shall
27 not be used in advertising or otherwise to promote the sale, use or
28 other dealings in this Software without prior written authorization
29 from the X Consortium.
30
31 */
32
33 #include <X11/Xos.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/cursorfont.h>
37 #include <stdio.h>
38 /*
39 * Other_stuff.h: Definitions of routines in other_stuff.
40 *
41 * Written by Mark Lillibridge. Last updated 7/1/87
42 */
43
44 #ifdef X_NOT_STDC_ENV
45 char *malloc(), realloc();
46 #else
47 #include <stdlib.h>
48 #endif
49
50 extern Bool silent;
51 unsigned long Resolve_Color();
52 Pixmap Bitmap_To_Pixmap();
53 Window Select_Window();
54 void out();
55 void blip();
56 Window Window_With_Name();
57 void Fatal_Error();
•58 void usage(void) __attribute__((__noreturn__));
59
60 /*
61 * Just_display: A group of routines designed to make the writting of simple
62 * X11 applications which open a display but do not open
63 * any windows much faster and easier. Unless a routine says
64 * otherwise, it may be assumed to require program_name, dpy,
65 * and screen already defined on entry.
66 *
67 * Written by Mark Lillibridge. Last updated 7/1/87
68 */
69
70
71 /* This stuff is defined in the calling program by just_display.h */
72 extern char *program_name;
73 extern Display *dpy;
74 extern int screen;
75
76
77 /*
78 * Malloc: like malloc but handles out of memory using Fatal_Error.
79 */
80 char *Malloc(size)
81 unsigned size;
82 {
83 char *data;
84
85 if (!(data = malloc(size)))
86 Fatal_Error("Out of memory!");
87
88 return(data);
89 }
90
91
92 /*
93 * Realloc: like Malloc except for realloc, handles NULL using Malloc.
94 */
95 char *Realloc(ptr, size)
96 char *ptr;
97 int size;
98 {
99 char *new_ptr;
100
101 if (!ptr)
102 return(Malloc(size));
103
104 if (!(new_ptr = realloc(ptr, size)))
105 Fatal_Error("Out of memory!");
106
107 return(new_ptr);
108 }
109
110
111 /*
112 * Get_Display_Name (argc, argv) Look for -display, -d, or host:dpy (obselete)
113 * If found, remove it from command line. Don't go past a lone -.
114 */
115 char *Get_Display_Name(pargc, argv)
116 int *pargc; /* MODIFIED */
117 char **argv; /* MODIFIED */
118 {
119 int argc = *pargc;
120 char **pargv = argv+1;
121 char *displayname = NULL;
122 int i;
123
124 for (i = 1; i < argc; i++) {
125 char *arg = argv[i];
126
127 if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
128 if (++i >= argc) usage ();
129
130 displayname = argv[i];
131 *pargc -= 2;
132 continue;
133 }
134 if (!strcmp(arg,"-")) {
135 while (i<argc)
136 *pargv++ = argv[i++];
137 break;
138 }
139 *pargv++ = arg;
140 }
141
142 *pargv = NULL;
143 return (displayname);
144 }
145
146
147 /*
148 * Open_Display: Routine to open a display with correct error handling.
149 * Does not require dpy or screen defined on entry.
150 */
151 Display *Open_Display(display_name)
152 char *display_name;
153 {
154 Display *d;
155
156 d = XOpenDisplay(display_name);
157 if (d == NULL) {
158 fprintf (stderr, "%s: unable to open display '%s'\n",
159 program_name, XDisplayName (display_name));
160 usage ();
161 /* doesn't return */
162 }
163
164 return(d);
165 }
166
167
168 /*
169 * Setup_Display_And_Screen: This routine opens up the correct display (i.e.,
170 * it calls Get_Display_Name) and then stores a
171 * pointer to it in dpy. The default screen
172 * for this display is then stored in screen.
173 * Does not require dpy or screen defined.
174 */
175 void Setup_Display_And_Screen(argc, argv)
176 int *argc; /* MODIFIED */
177 char **argv; /* MODIFIED */
178 {
179 dpy = Open_Display (Get_Display_Name(argc, argv));
180 screen = DefaultScreen(dpy);
181 }
182
183
184 /*
185 * Open_Font: This routine opens a font with error handling.
186 */
187 XFontStruct *Open_Font(name)
188 char *name;
189 {
190 XFontStruct *font;
191
192 if (!(font=XLoadQueryFont(dpy, name)))
193 Fatal_Error("Unable to open font %s!", name);
194
195 return(font);
196 }
197
198
199 /*
200 * ReadBitmapFile: same as XReadBitmapFile except it returns the bitmap
201 * directly and handles errors using Fatal_Error.
202 */
203 static void _bitmap_error(status, filename)
204 int status;
205 char *filename;
206 {
207 if (status == BitmapOpenFailed)
208 Fatal_Error("Can't open file %s!", filename);
209 else if (status == BitmapFileInvalid)
210 Fatal_Error("file %s: Bad bitmap format.", filename);
211 else
212 Fatal_Error("Out of memory!");
213 }
214
215 Pixmap ReadBitmapFile(d, filename, width, height, x_hot, y_hot)
216 Drawable d;
217 char *filename;
218 int *width, *height, *x_hot, *y_hot;
219 {
220 Pixmap bitmap;
221 int status;
222
223 status = XReadBitmapFile(dpy, RootWindow(dpy, screen), filename,
224 (unsigned int *)width, (unsigned int *)height,
225 &bitmap, x_hot, y_hot);
226 if (status != BitmapSuccess)
227 _bitmap_error(status, filename);
228
229 return(bitmap);
230 }
231
232
233 /*
234 * WriteBitmapFile: same as XWriteBitmapFile except it handles errors
235 * using Fatal_Error.
236 */
237 void WriteBitmapFile(filename, bitmap, width, height, x_hot, y_hot)
238 char *filename;
239 Pixmap bitmap;
240 int width, height, x_hot, y_hot;
241 {
242 int status;
243
244 status= XWriteBitmapFile(dpy, filename, bitmap, width, height, x_hot,
245 y_hot);
246 if (status != BitmapSuccess)
247 _bitmap_error(status, filename);
248 }
249
250
251 /*
252 * Select_Window_Args: a rountine to provide a common interface for
253 * applications that need to allow the user to select one
254 * window on the screen for special consideration.
255 * This routine implements the following command line
256 * arguments:
257 *
258 * -root Selects the root window.
259 * -id <id> Selects window with id <id>. <id> may
260 * be either in decimal or hex.
261 * -name <name> Selects the window with name <name>.
262 *
263 * Call as Select_Window_Args(&argc, argv) in main before
264 * parsing any of your program's command line arguments.
265 * Select_Window_Args will remove its arguments so that
266 * your program does not have to worry about them.
267 * The window returned is the window selected or 0 if
268 * none of the above arguments was present. If 0 is
269 * returned, Select_Window should probably be called after
270 * all command line arguments, and other setup is done.
271 * For examples of usage, see xwininfo, xwd, or xprop.
272 */
273 Window Select_Window_Args(rargc, argv)
274 int *rargc;
275 char **argv;
276 #define ARGC (*rargc)
277 {
278 int nargc=1;
279 int argc;
280 char **nargv;
281 Window w=0;
282
283 nargv = argv+1; argc = ARGC;
284 #define OPTION argv[0]
285 #define NXTOPTP ++argv, --argc>0
286 #define NXTOPT if (++argv, --argc==0) usage()
287 #define COPYOPT nargv++[0]=OPTION, nargc++
288
289 while (NXTOPTP) {
290 if (!strcmp(OPTION, "-")) {
291 COPYOPT;
292 while (NXTOPTP)
293 COPYOPT;
294 break;
295 }
296 if (!strcmp(OPTION, "-root")) {
297 w=RootWindow(dpy, screen);
298 continue;
299 }
300 if (!strcmp(OPTION, "-name")) {
301 NXTOPT;
302 w = Window_With_Name(dpy, RootWindow(dpy, screen),
303 OPTION);
304 if (!w)
305 Fatal_Error("No window with name %s exists!",OPTION);
306 continue;
307 }
308 if (!strcmp(OPTION, "-id")) {
309 NXTOPT;
310 w=0;
311 sscanf(OPTION, "0x%lx", &w);
312 if (!w)
313 sscanf(OPTION, "%ld", &w);
314 if (!w)
315 Fatal_Error("Invalid window id format: %s.", OPTION);
316 continue;
317 }
318 COPYOPT;
319 }
320 ARGC = nargc;
321
322 return(w);
323 }
324
325 /*
326 * Other_stuff: A group of routines which do common X11 tasks.
327 *
328 * Written by Mark Lillibridge. Last updated 7/1/87
329 */
330
331 extern Display *dpy;
332 extern int screen;
333
334 /*
335 * Resolve_Color: This routine takes a color name and returns the pixel #
336 * that when used in the window w will be of color name.
337 * (WARNING: The colormap of w MAY be modified! )
338 * If colors are run out of, only the first n colors will be
339 * as correct as the hardware can make them where n depends
340 * on the display. This routine does not require wind to
341 * be defined.
342 */
343 unsigned long Resolve_Color(w, name)
344 Window w;
345 char *name;
346 {
347 XColor c;
348 Colormap colormap;
349 XWindowAttributes wind_info;
350
351 /*
352 * The following is a hack to insure machines without a rgb table
353 * handle at least white & black right.
354 */
355 if (!strcmp(name, "white"))
356 name="#ffffffffffff";
357 if (!strcmp(name, "black"))
358 name="#000000000000";
359
360 XGetWindowAttributes(dpy, w, &wind_info);
361 colormap = wind_info.colormap;
362
363 if (!XParseColor(dpy, colormap, name, &c))
364 Fatal_Error("Bad color format '%s'.", name);
365
366 if (!XAllocColor(dpy, colormap, &c))
367 Fatal_Error("XAllocColor failed!");
368
369 return(c.pixel);
370 }
371
372
373 /*
374 * Bitmap_To_Pixmap: Convert a bitmap to a 2 colored pixmap. The colors come
375 * from the foreground and background colors of the gc.
376 * Width and height are required solely for efficiency.
377 * If needed, they can be obtained via. XGetGeometry.
378 */
379 Pixmap Bitmap_To_Pixmap(dpy, d, gc, bitmap, width, height)
380 Display *dpy;
381 Drawable d;
382 GC gc;
383 Pixmap bitmap;
384 int width, height;
385 {
386 Pixmap pix;
387 int x;
388 unsigned int i, depth;
389 Drawable root;
390
391 if (!XGetGeometry(dpy, d, &root, &x, &x, &i, &i, &i, &depth))
392 return(0);
393
394 pix = XCreatePixmap(dpy, d, width, height, (int)depth);
395
396 XCopyPlane(dpy, bitmap, pix, gc, 0, 0, width, height, 0, 0, 1);
397
398 return(pix);
399 }
400
401
•402 #if 0
403 /*
404 * blip: a debugging routine. Prints Blip! on stderr with flushing.
405 */
406 void blip()
407 {
408 outl("blip!");
409 }
•410 #endif
411
412
413 /*
414 * Routine to let user select a window using the mouse
415 */
416
417 Window Select_Window(dpy)
418 Display *dpy;
419 {
420 int status;
421 Cursor cursor;
422 XEvent event;
423 Window target_win = None, root = RootWindow(dpy,screen);
424 int buttons = 0;
425
426 /* Make the target cursor */
427 cursor = XCreateFontCursor(dpy, XC_crosshair);
428
429 /* Grab the pointer using target cursor, letting it room all over */
430 status = XGrabPointer(dpy, root, False,
431 ButtonPressMask|ButtonReleaseMask, GrabModeSync,
432 GrabModeAsync, root, cursor, CurrentTime);
433 if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");
434
435 /* Let the user select a window... */
436 while ((target_win == None) || (buttons != 0)) {
437 /* allow one more event */
438 XAllowEvents(dpy, SyncPointer, CurrentTime);
439 XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
440 switch (event.type) {
441 case ButtonPress:
442 if (target_win == None) {
443 target_win = event.xbutton.subwindow; /* window selected */
444 if (target_win == None) target_win = root;
445 }
446 buttons++;
447 break;
448 case ButtonRelease:
449 if (buttons > 0) /* there may have been some down before we started */
450 buttons--;
451 break;
452 }
453 }
454
455 XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
456
457 return(target_win);
458 }
459
460
461 /*
462 * Window_With_Name: routine to locate a window with a given name on a display.
463 * If no window with the given name is found, 0 is returned.
464 * If more than one window has the given name, the first
465 * one found will be returned. Only top and its subwindows
466 * are looked at. Normally, top should be the RootWindow.
467 */
468 Window Window_With_Name(dpy, top, name)
469 Display *dpy;
470 Window top;
471 char *name;
472 {
473 Window *children, dummy;
474 unsigned int nchildren;
475 int i;
476 Window w=0;
477 char *window_name;
478
479 if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
480 return(top);
481
482 if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
483 return(0);
484
485 for (i=0; i<nchildren; i++) {
486 w = Window_With_Name(dpy, children[i], name);
487 if (w)
488 break;
489 }
490 if (children) XFree ((char *)children);
491 return(w);
492 }
493
494 /*
495 * outl: a debugging routine. Flushes stdout then prints a message on stderr
496 * and flushes stderr. Used to print messages when past certain points
497 * in code so we can tell where we are. Outl may be invoked like
498 * printf with up to 7 arguments.
499 */
500 /* VARARGS1 */
•501 void
502 outl(msg, arg0,arg1,arg2,arg3,arg4,arg5,arg6)
503 char *msg;
504 char *arg0, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6;
505 {
506 fflush(stdout);
507 fprintf(stderr, msg, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
508 fprintf(stderr, "\n");
509 fflush(stderr);
510 }
511
512
513 /*
514 * Standard fatal error routine - call like printf but maximum of 7 arguments.
515 * Does not require dpy or screen defined.
516 */
517 /* VARARGS1 */
518 void Fatal_Error(msg, arg0,arg1,arg2,arg3,arg4,arg5,arg6)
519 char *msg;
520 char *arg0, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6;
521 {
522 fflush(stdout);
523 fflush(stderr);
524 fprintf(stderr, "%s: error: ", program_name);
525 fprintf(stderr, msg, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
526 fprintf(stderr, "\n");
527 exit(1);
528 }