Annotation of 43BSD/contrib/X/xwud/xwud.c, revision 1.1.1.1

1.1       root        1: #include <X/mit-copyright.h>
                      2: 
                      3: /* Copyright 1985, Massachusetts Institute of Technology */
                      4: 
                      5: /*
                      6:  * xwud.c - MIT Project Athena, X Window system window raster image
                      7:  *         undumper.
                      8:  *
                      9:  * This program will read a raster image of a window from stdin or a file
                     10:  * and display it on an X display.
                     11:  *
                     12:  *  Author:    Tony Della Fera, DEC
                     13:  */
                     14: 
                     15: #ifndef lint
                     16: static char *rcsid_xwud_c = "$Header: xwud.c,v 10.7 86/02/01 15:23:08 tony Rel $";
                     17: #endif
                     18: 
                     19: #include <X/Xlib.h>
                     20: #include <stdio.h>
                     21: #include <strings.h>
                     22: 
                     23: extern char *calloc();
                     24: 
                     25: #include "../xwd/XWDFile.h"
                     26: 
                     27: typedef enum _bool {FALSE, TRUE} Bool;
                     28: 
                     29: #define MAX(a, b) (a) > (b) ? (a) : (b)
                     30: #define MIN(a, b) (a) < (b) ? (a) : (b)
                     31: #define ABS(a) (a) < 0 ? -(a) : (a)
                     32: 
                     33: #define FAILURE 0
                     34: 
                     35: extern int errno;
                     36: 
                     37: main(argc, argv)
                     38:     int argc;
                     39:     char **argv;
                     40: {
                     41:     register int i;
                     42:     int status;
                     43:     unsigned buffer_size;
                     44:     int win_name_size;
                     45:     char *str_index;
                     46:     char *file_name;
                     47:     char display[256];
                     48:     char *win_name;
                     49:     char *buffer;
                     50:     Bool standard_in = TRUE;
                     51: 
                     52:     Display *dpy;
                     53:     Window image_win;
                     54:     Pixmap image_pixmap;
                     55:     XEvent event;
                     56: 
                     57:     XWDFileHeader header;
                     58: 
                     59:     FILE *in_file = stdin;
                     60: 
                     61:     for (i = 1; i < argc; i++) {
                     62:        str_index = (char *)index (argv[i], ':');
                     63:        if(str_index != NULL) {
                     64:            (void) strncpy(display,argv[i],sizeof(display));
                     65:            continue;
                     66:         }
                     67:        str_index = (char *) index (argv [i], '-');
                     68:        if (str_index == NULL) Syntax(argv[0]);
                     69:        if (strncmp(argv[i], "-help", 5) == 0) {
                     70:            Syntax(argv[0]);
                     71:        }
                     72:        if (strncmp(argv[i], "-in", 4) == 0) {
                     73:            if (++i >= argc) Syntax(argv[0]);
                     74:            file_name = argv[i];
                     75:            standard_in = FALSE;
                     76:            continue;
                     77:        }
                     78:        Syntax(argv[0]);
                     79:     }
                     80:     
                     81:     if (!standard_in) {
                     82:        /*
                     83:         * Open the output file.
                     84:         */
                     85:        in_file = fopen(file_name, "r");
                     86:        if (in_file == NULL) {
                     87:            Error("Can't open output file as specified.");
                     88:        }
                     89:     }
                     90:     
                     91:     /*
                     92:      * Open the display.
                     93:      */
                     94:     if ((dpy = XOpenDisplay(display)) == NULL) {
                     95:        Error("Error occured while trying open display.");
                     96:     }
                     97: 
                     98:     /*
                     99:      * Read in header information.
                    100:      */
                    101:     status = fread((char *)&header, sizeof(header), 1, in_file);
                    102:     if (status != 1) Error("Unable to read dump file header.");
                    103: 
                    104:     /*
                    105:      * check to see if the dump file is in the proper format.
                    106:      */
                    107:     if (header.file_version != XWD_FILE_VERSION) {
                    108:        Error("XWD file format version missmatch.");
                    109:     }
                    110: 
                    111:     /*
                    112:      * Check to see if we are in the right pixmap format for the
                    113:      * display type.
                    114:      */
                    115:     if ((dpy->dplanes == 1) && (header.pixmap_format != XYFormat)) {
                    116:        Error("Windump is in ZFormat which is not valid on a monochrome display.");
                    117:     }
                    118: 
                    119:     /*
                    120:      * Calloc window name.
                    121:      */
                    122:     win_name_size = abs(header.header_size - sizeof(header));
                    123:     win_name = calloc((unsigned) win_name_size, sizeof(char));
                    124:     if (win_name == NULL) Error("Can't calloc window name storage.");
                    125: 
                    126:     /*
                    127:      * Read in window name.
                    128:      */
                    129:     status = fread(win_name, sizeof(char), win_name_size, in_file);
                    130:     if (status != win_name_size) {
                    131:        Error("Unable to read window name from dump file.");
                    132:     }
                    133: 
                    134:     /*
                    135:      * Determine the pixmap size.
                    136:      */
                    137:     if (header.pixmap_format == XYFormat) {
                    138:        buffer_size = XYPixmapSize(
                    139:            header.pixmap_width,
                    140:            header.pixmap_height,
                    141:            header.display_planes
                    142:        );
                    143:     }
                    144:     else if (header.display_planes < 9) {
                    145:        buffer_size = BZPixmapSize(
                    146:            header.pixmap_width,
                    147:            header.pixmap_height
                    148:        );
                    149:     }
                    150:     else {
                    151:        buffer_size = WZPixmapSize(
                    152:            header.pixmap_width,
                    153:            header.pixmap_height
                    154:        );
                    155:     }
                    156: 
                    157:     /*
                    158:      * Calloc the buffer.
                    159:      */
                    160:     buffer = calloc(buffer_size, 1);
                    161:     if (buffer == NULL) Error("Can't calloc data buffer.");
                    162: 
                    163:     /*
                    164:      * Read in the pixmap buffer.
                    165:      */
                    166:     status = fread(buffer, sizeof(char), (int)buffer_size, in_file);
                    167:     if (status != buffer_size) Error("Unable to read pixmap from dump file.");
                    168: 
                    169:     /*
                    170:      * Close the input file.
                    171:      */
                    172:     (void) fclose(in_file);
                    173: 
                    174:     /*
                    175:      * Undump pixmap.
                    176:      */
                    177:     if (header.pixmap_format == XYFormat) {
                    178:        image_pixmap = XStorePixmapXY(
                    179:            header.pixmap_width,
                    180:            header.pixmap_height,
                    181:            buffer
                    182:        );
                    183:     }
                    184:     else {
                    185:        image_pixmap = XStorePixmapZ(
                    186:            header.pixmap_width,
                    187:            header.pixmap_height,
                    188:            buffer
                    189:        );
                    190:     }
                    191: 
                    192:     /*
                    193:      * Create the image window.
                    194:      */
                    195:     image_win = XCreateWindow(
                    196:        RootWindow,
                    197:        header.window_x, header.window_y,
                    198:        header.pixmap_width, header.pixmap_height,
                    199:        0, (Pixmap) 0,
                    200:        (Pixmap) 0
                    201:     );
                    202:     if (image_win == FAILURE) Error("Can't create image window.");
                    203: 
                    204:     /*
                    205:      * Select mouse ButtonPressed on the window, this is how we determine
                    206:      * when to stop displaying the window.
                    207:      */
                    208:     XSelectInput(image_win, (ButtonPressed | ExposeWindow));
                    209:      
                    210:     /*
                    211:      * Store the window name string.
                    212:      */
                    213:     XStoreName(image_win, win_name);
                    214:     
                    215:     /*
                    216:      * Map the image window.
                    217:      */
                    218:     XMapWindow(image_win);
                    219: 
                    220:     /*
                    221:      * Set up a while loop to maintain the image.
                    222:      */
                    223:     while (TRUE) {
                    224:        /*
                    225:         * Blit the pixmap into the window.
                    226:         */
                    227:        XPixmapPut(
                    228:            image_win,
                    229:            0, 0, 0, 0,
                    230:            header.pixmap_width, header.pixmap_height,
                    231:            image_pixmap,
                    232:            GXcopy,
                    233:            AllPlanes
                    234:        );
                    235: 
                    236:        /*
                    237:         * Wait on mouse input event to terminate.
                    238:         */
                    239:        XNextEvent(&event);
                    240:        if (event.type == ButtonPressed) break;
                    241:     }
                    242: 
                    243:     /*
                    244:      * Destroy the image window.
                    245:      */
                    246:     XDestroyWindow(image_win);
                    247:     
                    248:     /*
                    249:      * Free the pixmap buffer.
                    250:      */
                    251:     free(buffer);
                    252: 
                    253:     /*
                    254:      * Free window name string.
                    255:      */
                    256:     free(win_name);
                    257:     exit(0);
                    258: }
                    259: 
                    260: 
                    261: /*
                    262:  * Report the syntax for calling xwud.
                    263:  */
                    264: Syntax(call)
                    265:     char *call;
                    266: {
                    267:     fprintf(
                    268:        stderr,
                    269:        "\nUsage: %s [-debug] [-help] [-in <file>] [[host]:vs]\n\n",
                    270:        call
                    271:     );
                    272:     exit(0);
                    273: }
                    274: 
                    275: 
                    276: /*
                    277:  * Error - Fatal xwud error.
                    278:  */
                    279: Error(string)
                    280:        char *string;   /* Error description string. */
                    281: {
                    282:        fprintf(stderr, "\nxwud: Error => %s", string);
                    283:        fprintf(stderr, "\n\n");
                    284: 
                    285:        if (errno != 0) {
                    286:                perror("xwud");
                    287:                fprintf(stderr, "\n");
                    288:        }
                    289: 
                    290:        exit(1);
                    291: }
                    292: 
                    293: /* End of xwud.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.