Debian Codegraph @

contrib/xwintoppm/dsimple.c

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