home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1993 Tetsuji Rai (tetsuji@rai.juice.or.jp)
- *
- * Permission to use, copy, modify, distribute, and sell this software and its
- * documentation for any purpose is hereby granted without fee, provided that
- * the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of Tetsuji Rai not be used in
- * advertising or publicity pertaining to distribution of the software
- * without specific, written prior permission. Tetsuji Rai makes
- * no representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- * TETSUJI RAI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
- * EVENT SHALL TETSUJI RAI BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
- * OF THIS SOFTWARE.
- *
- * Author: Tetsuji Rai
- * tetsuji@rai.juice.or.jp
- * 74610.1537@Compuserve.Com
- * +81-3-3557-3936
- */
-
- /*
- * xwinpr.C ver.1.0
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/Xos.h>
- #include <unistd.h>
- #include <X11/cursorfont.h>
- #include <X11/Xmu/WinUtil.h>
-
- #include <fstream.h>
-
- #include "patchlevel.h"
-
-
-
- XImage *xim;
- Display *dp;
- Window win, pwin;
- XWindowAttributes wattr, pwattr;
- int height, width;
- int pheight, pwidth;
- unsigned long background;
- time_t t;
-
- #ifndef DEBUG
- char *lpr = "lpr";
-
- #else
- char *lpr = "cat";
-
- #endif
-
- int dot = 100;
- int hflag = !0; // header flag (true if necessary)
- int rflag = 0; // reverse flag
- char *ofile = 0;
-
- void Usage (void);
-
-
- int main (int c, char **v)
- {
- dp = XOpenDisplay (0);
-
- for (int i = 1; i < c; i++) {
- if (!strcmp (v[i], "-help")) { // display usage
- Usage ();
- exit (0);
- } else if (!strcmp (v[i], "-r")) { // reverse white/black
- rflag = !0;
- } else if (!strcmp (v[i], "-o")) { // output file
- ofile = v[++i];
- } else if (!strcmp (v[i], "-p")) { // output program
- lpr = v[++i];
- } else if (!strcmp (v[i], "-dotpitch")) { // dotpitch
- dot = atoi (v[++i]);
- if (!dot) {
- cerr << "xwinpr : invalid dotpitch.\n";
- exit (1);
- }
- } else if (!strcmp (v[i], "-nh")) { // non header printing
- hflag = 0;
- } else {
- cerr << " xwinpr : unknown flag \"" << v[i] << "\"\n";
- Usage ();
- exit (1);
- }
- }
-
- Cursor cursor;
-
- cursor = XCreateFontCursor (dp, XC_crosshair);
- int status =
- XGrabPointer (dp, DefaultRootWindow (dp), False,
- ButtonPressMask, GrabModeSync,
- GrabModeAsync, DefaultRootWindow (dp), cursor, CurrentTime);
-
- pwin = 0;
-
- XEvent xe;
-
- cerr << "\n- Xwinpr : \n\
- Move cursor to the window you want to print, exactly to the\n\
- point which you want to print as white(or black if you specified -r \n\
- option).\n";
-
- while (pwin == 0) {
- XAllowEvents (dp, SyncPointer, CurrentTime);
- XWindowEvent (dp, DefaultRootWindow (dp), ButtonPressMask,
- &xe);
- if (xe.type == ButtonPress)
- pwin = xe.xbutton.subwindow;
- }
- win = XmuClientWindow (dp, pwin);
-
- XUngrabPointer (dp, CurrentTime);
-
- time_t t = time (0);
- struct tm *tm = localtime (&t);
-
- int pipes[2];
-
- if (!ofile)
- if (pipe (pipes)) {
- cerr << "xwinpr : pipe(2) failed.\n";
- exit (1);
- }
- if (!ofile)
- switch (fork ()) {
- case 0: // You are the child.
- close (0); // close stdin handle
- dup2 (pipes[0], 0); // connect pipe output to stdin handle(0)
- close (pipes[1]); // close input end of the pipe
- system (lpr); // invole lpr
- exit (0);
- case -1: // fork failed!
- cerr << "xwinpr : fork(2) failed.\n";
- exit (1);
- }
- if (!ofile)
- close (pipes[0]);
-
- // Well, now you are the parent!
-
- ofstream *clprp;
-
- if (ofile)
- clprp = new ofstream (ofile); // open output file instead of running
- // program
- else
- clprp = new ofstream (pipes[1]); // open output stream connected to
- // "lpr"
-
- ofstream & clpr = *clprp;
- char *winName;
-
- XFetchName (dp, win, &winName);
-
- cerr << '\n'
- << "Date : " << asctime (tm)
- << "Window ID : " << hex << win << '\n'
- << "Window Name : " << winName << '\n';
-
- if (hflag) {
- clpr << "Date : " << asctime (tm)
- << "Window : " << winName
- << "\n\n";
- }
- XGetWindowAttributes (dp, pwin, &pwattr);
- XGetWindowAttributes (dp, win, &wattr);
- width = wattr.width;
- height = wattr.height;
-
- #ifdef DEBUG
- cerr << "wid, height = " << width << "," << height << '\n';
- #endif
- pwidth = (width + 7) / 8 * 2;
- pheight = height;
-
- xim = XGetImage (dp, win, 0, 0, width, height, 0xffff, ZPixmap);
- #ifdef DEBUG
- fprintf (stderr, "xim = %lx\n", xim);
- #endif // DEBUG
-
- clpr
- #ifndef DEBUG
- << "\x1b["
- #else
- << "(ESC)1b["
- #endif
- << dec << pwidth * pheight << ';'
- << pwidth / 2 << ';'
- << dot << ";;"
- << pheight
- << "/r";
- // (fp, "\x1b[%d;%d;%d;;%d/r", pwidth * pheight, pwidth / 2, 150, pheight);
-
- #ifdef DEBUG
- cerr << "xe, wattr = " << xe.xbutton.x << ',' << wattr.x << ',' << pwattr.x
- << '\n'
- << xe.xbutton.y << ',' << wattr.y << ',' << pwattr.y << '\n';
- #endif
- background = XGetPixel (xim, xe.xbutton.x - wattr.x - pwattr.x,
- xe.xbutton.y - wattr.y - pwattr.y);
-
- #ifdef DEBUG
- cerr << "background = " << background << '\n';
- #endif // DEBUG
-
- clpr << hex;
-
- for (int xy = 0; xy < pheight; xy++) {
- int xx = 0;
-
- for (int ix = 0; ix < pwidth; ix++) {
- unsigned int b = 0;
- int bitx;
-
- for (bitx = 0; bitx < 4; bitx++, xx++) {
- b <<= 1;
- if (xx < width)
- b |= ((XGetPixel (xim, xx, xy) != background) ^ rflag);
- }
- clpr << b;
- }
- }
- #ifndef DEBUG
- clpr << '\xc';
- #else
- clpr << "(form feed)";
- #endif
- exit (0);
- }
-
-
- void Usage ()
- {
- cout <<
- "Xwinpr : " PATCHLEVEL "\n"
- "Usage : xwinpr [options]\n"
- " where options are:\n"
- " -help : display this message.\n"
- " -r : reverse black and white.\n"
- " -o file : send output to a file.\n"
- " -p printer : send output to the printer program. The default\n"
- " is \"lpr\".\n"
- " -dotpitch (number) : set dotpitch. The default is 100.\n"
- " -nh : Non-header mode. Header includes the date and the\n"
- " name of the window.\n"
- ;
- }
-