xwd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. */
  20. /*
  21. * xwd.c MIT Project Athena, X Window system window raster image dumper.
  22. *
  23. * This program will dump a raster image of the contents of a window into a
  24. * file for output on graphics printers or for other uses.
  25. *
  26. * Author: Tony Della Fera, DEC
  27. * 17-Jun-85
  28. *
  29. * Modification history:
  30. *
  31. * 11/14/86 Bill Wyatt, Smithsonian Astrophysical Observatory
  32. * - Removed Z format option, changing it to an XY option. Monochrome
  33. * windows will always dump in XY format. Color windows will dump
  34. * in Z format by default, but can be dumped in XY format with the
  35. * -xy option.
  36. *
  37. * 11/18/86 Bill Wyatt
  38. * - VERSION 6 is same as version 5 for monchrome. For colors, the
  39. * appropriate number of Color structs are dumped after the header,
  40. * which has the number of colors (=0 for monochrome) in place of the
  41. * V5 padding at the end. Up to 16-bit displays are supported. I
  42. * don't yet know how 24- to 32-bit displays will be handled under
  43. * the Version 11 protocol.
  44. *
  45. * 6/15/87 David Krikorian, MIT Project Athena
  46. * - VERSION 7 runs under the X Version 11 servers, while the previous
  47. * versions of xwd were are for X Version 10. This version is based
  48. * on xwd version 6, and should eventually have the same color
  49. * abilities. (Xwd V7 has yet to be tested on a color machine, so
  50. * all color-related code is commented out until color support
  51. * becomes practical.)
  52. */
  53. /*%
  54. *% This is the format for commenting out color-related code until
  55. *% color can be supported.
  56. %*/
  57. #ifdef HAVE_CONFIG_H
  58. #include "config.h"
  59. #endif
  60. #include <stdio.h>
  61. #include <errno.h>
  62. #include <X11/Xos.h>
  63. #include <stdlib.h>
  64. #include <X11/Xlib.h>
  65. #include <X11/Xutil.h>
  66. #include "X11/XWDFile.h"
  67. #define FEEP_VOLUME 0
  68. /* Include routines to do parsing */
  69. #include "dsimple.h"
  70. #include "list.h"
  71. #include "wsutils.h"
  72. #include "multiVis.h"
  73. #ifdef XKB
  74. #include <X11/extensions/XKBbells.h>
  75. #endif
  76. /* Setable Options */
  77. static int format = ZPixmap;
  78. static Bool nobdrs = False;
  79. static Bool on_root = False;
  80. static Bool standard_out = True;
  81. static Bool debug = False;
  82. static Bool silent = False;
  83. static Bool use_installed = False;
  84. static long add_pixel_value = 0;
  85. extern int main(int, char **);
  86. extern void Window_Dump(Window, FILE *);
  87. extern int Image_Size(XImage *);
  88. extern int Get_XColors(XWindowAttributes *, XColor **);
  89. extern void _swapshort(register char *, register unsigned);
  90. extern void _swaplong(register char *, register unsigned);
  91. static long parse_long(char *);
  92. static int Get24bitDirectColors(XColor **);
  93. static int ReadColors(Visual *, Colormap, XColor **);
  94. static long parse_long (char *s)
  95. {
  96. long retval = 0L;
  97. int thesign = 1;
  98. if (s && s[0]) {
  99. switch(s[0]) {
  100. case '-':
  101. (void) sscanf (s + 1, "%lu", &retval);
  102. thesign = -1;
  103. break;
  104. case '0':
  105. (void) sscanf (s + 1, "%lo", &retval);
  106. break;
  107. case 'x':
  108. case 'X':
  109. (void) sscanf (s + 1, "%lx", &retval);
  110. break;
  111. default:
  112. (void) sscanf (s, "%lu", &retval);
  113. break;
  114. }
  115. }
  116. return (thesign * retval);
  117. }
  118. int
  119. main(int argc, char **argv)
  120. {
  121. register int i;
  122. Window target_win;
  123. FILE *out_file = stdout;
  124. Bool frame_only = False;
  125. INIT_NAME;
  126. Setup_Display_And_Screen(&argc, argv);
  127. /* Get window select on command line, if any */
  128. target_win = Select_Window_Args(&argc, argv);
  129. for (i = 1; i < argc; i++) {
  130. if (!strcmp(argv[i], "-nobdrs")) {
  131. nobdrs = True;
  132. continue;
  133. }
  134. if (!strcmp(argv[i], "-debug")) {
  135. debug = True;
  136. continue;
  137. }
  138. if (!strcmp(argv[i], "-help"))
  139. usage();
  140. if (!strcmp(argv[i], "-out")) {
  141. if (++i >= argc) usage();
  142. if (!(out_file = fopen(argv[i], "wb")))
  143. Fatal_Error("Can't open output file as specified.");
  144. standard_out = False;
  145. continue;
  146. }
  147. if (!strcmp(argv[i], "-xy")) {
  148. format = XYPixmap;
  149. continue;
  150. }
  151. if (!strcmp(argv[i], "-screen")) {
  152. on_root = True;
  153. continue;
  154. }
  155. if (!strcmp(argv[i], "-icmap")) {
  156. use_installed = True;
  157. continue;
  158. }
  159. if (!strcmp(argv[i], "-add")) {
  160. if (++i >= argc) usage();
  161. add_pixel_value = parse_long (argv[i]);
  162. continue;
  163. }
  164. if (!strcmp(argv[i], "-frame")) {
  165. frame_only = True;
  166. continue;
  167. }
  168. if (!strcmp(argv[i], "-silent")) {
  169. silent = True;
  170. continue;
  171. }
  172. usage();
  173. }
  174. #ifdef WIN32
  175. if (standard_out)
  176. _setmode(fileno(out_file), _O_BINARY);
  177. #endif
  178. /*
  179. * Let the user select the target window.
  180. */
  181. if (target_win == None)
  182. target_win = Select_Window(dpy, !frame_only);
  183. /*
  184. * Dump it!
  185. */
  186. Window_Dump(target_win, out_file);
  187. XCloseDisplay(dpy);
  188. if (fclose(out_file)) {
  189. perror("xwd");
  190. exit(1);
  191. }
  192. exit(0);
  193. }
  194. static int
  195. Get24bitDirectColors(XColor **colors)
  196. {
  197. int i , ncolors = 256 ;
  198. XColor *tcol ;
  199. *colors = tcol = (XColor *)malloc(sizeof(XColor) * ncolors) ;
  200. for(i=0 ; i < ncolors ; i++)
  201. {
  202. tcol[i].pixel = i << 16 | i << 8 | i ;
  203. tcol[i].red = tcol[i].green = tcol[i].blue = i << 8 | i ;
  204. }
  205. return ncolors ;
  206. }
  207. /*
  208. * Window_Dump: dump a window to a file which must already be open for
  209. * writting.
  210. */
  211. void
  212. Window_Dump(Window window, FILE *out)
  213. {
  214. unsigned long swaptest = 1;
  215. XColor *colors;
  216. unsigned buffer_size;
  217. size_t win_name_size;
  218. CARD32 header_size;
  219. int ncolors, i;
  220. char *win_name;
  221. char default_win_name[] = "xwdump";
  222. Bool got_win_name;
  223. XWindowAttributes win_info;
  224. XImage *image;
  225. int absx, absy, x, y;
  226. unsigned width, height;
  227. int dwidth, dheight;
  228. Window dummywin;
  229. XWDFileHeader header;
  230. XWDColor xwdcolor;
  231. int transparentOverlays , multiVis;
  232. int numVisuals;
  233. XVisualInfo *pVisuals;
  234. int numOverlayVisuals;
  235. OverlayInfo *pOverlayVisuals;
  236. int numImageVisuals;
  237. XVisualInfo **pImageVisuals;
  238. list_ptr vis_regions; /* list of regions to read from */
  239. list_ptr vis_image_regions ;
  240. Visual vis_h,*vis ;
  241. int allImage = 0 ;
  242. /*
  243. * Inform the user not to alter the screen.
  244. */
  245. if (!silent) {
  246. #ifdef XKB
  247. XkbStdBell(dpy,None,50,XkbBI_Wait);
  248. #else
  249. XBell(dpy,FEEP_VOLUME);
  250. #endif
  251. XFlush(dpy);
  252. }
  253. /*
  254. * Get the parameters of the window being dumped.
  255. */
  256. if (debug) outl("xwd: Getting target window information.\n");
  257. if(!XGetWindowAttributes(dpy, window, &win_info))
  258. Fatal_Error("Can't get target window attributes.");
  259. /* handle any frame window */
  260. if (!XTranslateCoordinates (dpy, window, RootWindow (dpy, screen), 0, 0,
  261. &absx, &absy, &dummywin)) {
  262. fprintf (stderr,
  263. "%s: unable to translate window coordinates (%d,%d)\n",
  264. program_name, absx, absy);
  265. exit (1);
  266. }
  267. win_info.x = absx;
  268. win_info.y = absy;
  269. width = win_info.width;
  270. height = win_info.height;
  271. if (!nobdrs) {
  272. absx -= win_info.border_width;
  273. absy -= win_info.border_width;
  274. width += (2 * win_info.border_width);
  275. height += (2 * win_info.border_width);
  276. }
  277. dwidth = DisplayWidth (dpy, screen);
  278. dheight = DisplayHeight (dpy, screen);
  279. /* clip to window */
  280. if (absx < 0) width += absx, absx = 0;
  281. if (absy < 0) height += absy, absy = 0;
  282. if (absx + width > dwidth) width = dwidth - absx;
  283. if (absy + height > dheight) height = dheight - absy;
  284. XFetchName(dpy, window, &win_name);
  285. if (!win_name || !win_name[0]) {
  286. win_name = default_win_name;
  287. got_win_name = False;
  288. } else {
  289. got_win_name = True;
  290. }
  291. /* sizeof(char) is included for the null string terminator. */
  292. win_name_size = strlen(win_name) + sizeof(char);
  293. /*
  294. * Snarf the pixmap with XGetImage.
  295. */
  296. x = absx - win_info.x;
  297. y = absy - win_info.y;
  298. multiVis = GetMultiVisualRegions(dpy,RootWindow(dpy, screen),
  299. absx, absy,
  300. width, height,&transparentOverlays,&numVisuals, &pVisuals,
  301. &numOverlayVisuals,&pOverlayVisuals,&numImageVisuals,
  302. &pImageVisuals,&vis_regions,&vis_image_regions,&allImage) ;
  303. if (on_root || multiVis)
  304. {
  305. if(!multiVis)
  306. image = XGetImage (dpy, RootWindow(dpy, screen), absx, absy,
  307. width, height, AllPlanes, format);
  308. else
  309. image = ReadAreaToImage(dpy, RootWindow(dpy, screen), absx, absy,
  310. width, height,
  311. numVisuals,pVisuals,numOverlayVisuals,pOverlayVisuals,
  312. numImageVisuals, pImageVisuals,vis_regions,
  313. vis_image_regions,format,allImage);
  314. }
  315. else
  316. image = XGetImage (dpy, window, x, y, width, height, AllPlanes, format);
  317. if (!image) {
  318. fprintf (stderr, "%s: unable to get image at %dx%d+%d+%d\n",
  319. program_name, width, height, x, y);
  320. exit (1);
  321. }
  322. if (add_pixel_value != 0) XAddPixel (image, add_pixel_value);
  323. /*
  324. * Determine the pixmap size.
  325. */
  326. buffer_size = Image_Size(image);
  327. if (debug) outl("xwd: Getting Colors.\n");
  328. if( !multiVis)
  329. {
  330. ncolors = Get_XColors(&win_info, &colors);
  331. vis = win_info.visual ;
  332. }
  333. else
  334. {
  335. ncolors = Get24bitDirectColors(&colors) ;
  336. initFakeVisual(&vis_h) ;
  337. vis = &vis_h ;
  338. }
  339. /*
  340. * Inform the user that the image has been retrieved.
  341. */
  342. if (!silent) {
  343. #ifdef XKB
  344. XkbStdBell(dpy,window,FEEP_VOLUME,XkbBI_Proceed);
  345. XkbStdBell(dpy,window,FEEP_VOLUME,XkbBI_RepeatingLastBell);
  346. #else
  347. XBell(dpy, FEEP_VOLUME);
  348. XBell(dpy, FEEP_VOLUME);
  349. #endif
  350. XFlush(dpy);
  351. }
  352. /*
  353. * Calculate header size.
  354. */
  355. if (debug) outl("xwd: Calculating header size.\n");
  356. header_size = SIZEOF(XWDheader) + (CARD32) win_name_size;
  357. /*
  358. * Write out header information.
  359. */
  360. if (debug) outl("xwd: Constructing and dumping file header.\n");
  361. header.header_size = (CARD32) header_size;
  362. header.file_version = (CARD32) XWD_FILE_VERSION;
  363. header.pixmap_format = (CARD32) format;
  364. header.pixmap_depth = (CARD32) image->depth;
  365. header.pixmap_width = (CARD32) image->width;
  366. header.pixmap_height = (CARD32) image->height;
  367. header.xoffset = (CARD32) image->xoffset;
  368. header.byte_order = (CARD32) image->byte_order;
  369. header.bitmap_unit = (CARD32) image->bitmap_unit;
  370. header.bitmap_bit_order = (CARD32) image->bitmap_bit_order;
  371. header.bitmap_pad = (CARD32) image->bitmap_pad;
  372. header.bits_per_pixel = (CARD32) image->bits_per_pixel;
  373. header.bytes_per_line = (CARD32) image->bytes_per_line;
  374. /****
  375. header.visual_class = (CARD32) win_info.visual->class;
  376. header.red_mask = (CARD32) win_info.visual->red_mask;
  377. header.green_mask = (CARD32) win_info.visual->green_mask;
  378. header.blue_mask = (CARD32) win_info.visual->blue_mask;
  379. header.bits_per_rgb = (CARD32) win_info.visual->bits_per_rgb;
  380. header.colormap_entries = (CARD32) win_info.visual->map_entries;
  381. *****/
  382. header.visual_class = (CARD32) vis->class;
  383. header.red_mask = (CARD32) vis->red_mask;
  384. header.green_mask = (CARD32) vis->green_mask;
  385. header.blue_mask = (CARD32) vis->blue_mask;
  386. header.bits_per_rgb = (CARD32) vis->bits_per_rgb;
  387. header.colormap_entries = (CARD32) vis->map_entries;
  388. header.ncolors = ncolors;
  389. header.window_width = (CARD32) win_info.width;
  390. header.window_height = (CARD32) win_info.height;
  391. header.window_x = absx;
  392. header.window_y = absy;
  393. header.window_bdrwidth = (CARD32) win_info.border_width;
  394. if (*(char *) &swaptest) {
  395. _swaplong((char *) &header, sizeof(header));
  396. for (i = 0; i < ncolors; i++) {
  397. _swaplong((char *) &colors[i].pixel, sizeof(CARD32));
  398. _swapshort((char *) &colors[i].red, 3 * sizeof(short));
  399. }
  400. }
  401. if (fwrite((char *)&header, SIZEOF(XWDheader), 1, out) != 1 ||
  402. fwrite(win_name, win_name_size, 1, out) != 1) {
  403. perror("xwd");
  404. exit(1);
  405. }
  406. /*
  407. * Write out the color maps, if any
  408. */
  409. if (debug) outl("xwd: Dumping %d colors.\n", ncolors);
  410. for (i = 0; i < ncolors; i++) {
  411. xwdcolor.pixel = colors[i].pixel;
  412. xwdcolor.red = colors[i].red;
  413. xwdcolor.green = colors[i].green;
  414. xwdcolor.blue = colors[i].blue;
  415. xwdcolor.flags = colors[i].flags;
  416. if (fwrite((char *) &xwdcolor, SIZEOF(XWDColor), 1, out) != 1) {
  417. perror("xwd");
  418. exit(1);
  419. }
  420. }
  421. /*
  422. * Write out the buffer.
  423. */
  424. if (debug) outl("xwd: Dumping pixmap. bufsize=%d\n",buffer_size);
  425. /*
  426. * This copying of the bit stream (data) to a file is to be replaced
  427. * by an Xlib call which hasn't been written yet. It is not clear
  428. * what other functions of xwd will be taken over by this (as yet)
  429. * non-existant X function.
  430. */
  431. if (fwrite(image->data, (int) buffer_size, 1, out) != 1) {
  432. perror("xwd");
  433. exit(1);
  434. }
  435. /*
  436. * free the color buffer.
  437. */
  438. if(debug && ncolors > 0) outl("xwd: Freeing colors.\n");
  439. if(ncolors > 0) free(colors);
  440. /*
  441. * Free window name string.
  442. */
  443. if (debug) outl("xwd: Freeing window name string.\n");
  444. if (got_win_name) XFree(win_name);
  445. /*
  446. * Free image
  447. */
  448. XDestroyImage(image);
  449. }
  450. /*
  451. * Report the syntax for calling xwd.
  452. */
  453. void
  454. usage(void)
  455. {
  456. fprintf (stderr,
  457. "usage: %s [-display host:dpy] [-debug] [-help] %s [-nobdrs] [-out <file>]",
  458. program_name, "[{-root|-id <id>|-name <name>}]");
  459. fprintf (stderr, " [-xy] [-add value] [-frame]\n");
  460. exit(1);
  461. }
  462. /*
  463. * Determine the pixmap size.
  464. */
  465. int Image_Size(XImage *image)
  466. {
  467. if (image->format != ZPixmap)
  468. return(image->bytes_per_line * image->height * image->depth);
  469. return(image->bytes_per_line * image->height);
  470. }
  471. #define lowbit(x) ((x) & (~(x) + 1))
  472. static int
  473. ReadColors(Visual *vis, Colormap cmap, XColor **colors)
  474. {
  475. int i,ncolors ;
  476. ncolors = vis->map_entries;
  477. if (!(*colors = (XColor *) malloc (sizeof(XColor) * ncolors)))
  478. Fatal_Error("Out of memory!");
  479. if (vis->class == DirectColor ||
  480. vis->class == TrueColor) {
  481. Pixel red, green, blue, red1, green1, blue1;
  482. red = green = blue = 0;
  483. red1 = lowbit(vis->red_mask);
  484. green1 = lowbit(vis->green_mask);
  485. blue1 = lowbit(vis->blue_mask);
  486. for (i=0; i<ncolors; i++) {
  487. (*colors)[i].pixel = red|green|blue;
  488. (*colors)[i].pad = 0;
  489. red += red1;
  490. if (red > vis->red_mask)
  491. red = 0;
  492. green += green1;
  493. if (green > vis->green_mask)
  494. green = 0;
  495. blue += blue1;
  496. if (blue > vis->blue_mask)
  497. blue = 0;
  498. }
  499. } else {
  500. for (i=0; i<ncolors; i++) {
  501. (*colors)[i].pixel = i;
  502. (*colors)[i].pad = 0;
  503. }
  504. }
  505. XQueryColors(dpy, cmap, *colors, ncolors);
  506. return(ncolors);
  507. }
  508. /*
  509. * Get the XColors of all pixels in image - returns # of colors
  510. */
  511. int Get_XColors(XWindowAttributes *win_info, XColor **colors)
  512. {
  513. int i, ncolors;
  514. Colormap cmap = win_info->colormap;
  515. if (use_installed)
  516. /* assume the visual will be OK ... */
  517. cmap = XListInstalledColormaps(dpy, win_info->root, &i)[0];
  518. if (!cmap)
  519. return(0);
  520. ncolors = ReadColors(win_info->visual,cmap,colors) ;
  521. return ncolors ;
  522. }
  523. void
  524. _swapshort (register char *bp, register unsigned n)
  525. {
  526. register char c;
  527. register char *ep = bp + n;
  528. while (bp < ep) {
  529. c = *bp;
  530. *bp = *(bp + 1);
  531. bp++;
  532. *bp++ = c;
  533. }
  534. }
  535. void
  536. _swaplong (register char *bp, register unsigned n)
  537. {
  538. register char c;
  539. register char *ep = bp + n;
  540. while (bp < ep) {
  541. c = bp[3];
  542. bp[3] = bp[0];
  543. bp[0] = c;
  544. c = bp[2];
  545. bp[2] = bp[1];
  546. bp[1] = c;
  547. bp += 4;
  548. }
  549. }