home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume21 / xwinpr / part01 / xwinpr.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-20  |  6.5 KB  |  263 lines

  1. /*
  2.  * Copyright (C) 1993    Tetsuji Rai (tetsuji@rai.juice.or.jp)
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided that
  6.  * the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Tetsuji Rai not be used in
  9.  * advertising or publicity pertaining to distribution of the software
  10.  * without specific, written prior permission.  Tetsuji Rai makes
  11.  * no representations about the suitability of this software for any purpose.
  12.  * It is provided "as is" without express or implied warranty.
  13.  *
  14.  * TETSUJI RAI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO
  16.  * EVENT SHALL TETSUJI RAI BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20.  * OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Tetsuji Rai
  23.  *          tetsuji@rai.juice.or.jp
  24.  *          74610.1537@Compuserve.Com
  25.  *          +81-3-3557-3936
  26.  */
  27.  
  28. /*
  29.  * xwinpr.C  ver.1.0
  30.  *
  31.  */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <X11/Xlib.h>
  35. #include <X11/Xutil.h>
  36. #include <X11/Xos.h>
  37. #include <unistd.h>
  38. #include <X11/cursorfont.h>
  39. #include <X11/Xmu/WinUtil.h>
  40.  
  41. #include <fstream.h>
  42.  
  43. #include "patchlevel.h"
  44.  
  45.  
  46.  
  47. XImage *xim;
  48. Display *dp;
  49. Window win, pwin;
  50. XWindowAttributes wattr, pwattr;
  51. int height, width;
  52. int pheight, pwidth;
  53. unsigned long background;
  54. time_t t;
  55.  
  56. #ifndef DEBUG
  57. char *lpr = "lpr";
  58.  
  59. #else
  60. char *lpr = "cat";
  61.  
  62. #endif
  63.  
  64. int dot = 100;
  65. int hflag = !0;            // header flag (true if necessary)
  66. int rflag = 0;            // reverse flag
  67. char *ofile = 0;
  68.  
  69. void Usage (void);
  70.  
  71.  
  72. int main (int c, char **v)
  73. {
  74.   dp = XOpenDisplay (0);
  75.  
  76.   for (int i = 1; i < c; i++) {
  77.     if (!strcmp (v[i], "-help")) {    // display usage
  78.       Usage ();
  79.       exit (0);
  80.     } else if (!strcmp (v[i], "-r")) {    // reverse white/black
  81.       rflag = !0;
  82.     } else if (!strcmp (v[i], "-o")) {    // output file
  83.       ofile = v[++i];
  84.     } else if (!strcmp (v[i], "-p")) {    // output program
  85.       lpr = v[++i];
  86.     } else if (!strcmp (v[i], "-dotpitch")) {    // dotpitch
  87.       dot = atoi (v[++i]);
  88.       if (!dot) {
  89.     cerr << "xwinpr : invalid dotpitch.\n";
  90.     exit (1);
  91.       }
  92.     } else if (!strcmp (v[i], "-nh")) {    // non header printing
  93.       hflag = 0;
  94.     } else {
  95.       cerr << " xwinpr : unknown flag \"" << v[i] << "\"\n";
  96.       Usage ();
  97.       exit (1);
  98.     }
  99.   }
  100.  
  101.   Cursor cursor;
  102.  
  103.   cursor = XCreateFontCursor (dp, XC_crosshair);
  104.   int status =
  105.   XGrabPointer (dp, DefaultRootWindow (dp), False,
  106.         ButtonPressMask, GrabModeSync,
  107.         GrabModeAsync, DefaultRootWindow (dp), cursor, CurrentTime);
  108.  
  109.   pwin = 0;
  110.  
  111.   XEvent xe;
  112.  
  113.   cerr << "\n- Xwinpr : \n\
  114.   Move cursor to the window you want to print, exactly to the\n\
  115.   point which you want to print as white(or black if you specified -r \n\
  116.   option).\n";
  117.  
  118.   while (pwin == 0) {
  119.     XAllowEvents (dp, SyncPointer, CurrentTime);
  120.     XWindowEvent (dp, DefaultRootWindow (dp), ButtonPressMask,
  121.           &xe);
  122.     if (xe.type == ButtonPress)
  123.       pwin = xe.xbutton.subwindow;
  124.   }
  125.   win = XmuClientWindow (dp, pwin);
  126.  
  127.   XUngrabPointer (dp, CurrentTime);
  128.  
  129.   time_t t = time (0);
  130.   struct tm *tm = localtime (&t);
  131.  
  132.   int pipes[2];
  133.  
  134.   if (!ofile)
  135.     if (pipe (pipes)) {
  136.       cerr << "xwinpr : pipe(2) failed.\n";
  137.       exit (1);
  138.     }
  139.   if (!ofile)
  140.     switch (fork ()) {
  141.      case 0:            // You are the child.
  142.       close (0);        // close stdin handle
  143.       dup2 (pipes[0], 0);    // connect pipe output to stdin handle(0)
  144.       close (pipes[1]);        // close input end of the pipe
  145.       system (lpr);        // invole lpr
  146.       exit (0);
  147.      case -1:            // fork failed!
  148.       cerr << "xwinpr : fork(2) failed.\n";
  149.       exit (1);
  150.     }
  151.   if (!ofile)
  152.     close (pipes[0]);
  153.  
  154.  // Well, now you are the parent!
  155.  
  156.   ofstream *clprp;
  157.  
  158.   if (ofile)
  159.     clprp = new ofstream (ofile);    // open output file instead of running
  160.  // program
  161.   else
  162.     clprp = new ofstream (pipes[1]);    // open output stream connected to
  163.  // "lpr"
  164.  
  165.   ofstream & clpr = *clprp;
  166.   char *winName;
  167.  
  168.   XFetchName (dp, win, &winName);
  169.  
  170.   cerr << '\n'
  171.     << "Date        : " << asctime (tm)
  172.     << "Window ID   : " << hex << win << '\n'
  173.     << "Window Name : " << winName << '\n';
  174.  
  175.   if (hflag) {
  176.     clpr << "Date    : " << asctime (tm)
  177.       << "Window  : " << winName
  178.       << "\n\n";
  179.   }
  180.   XGetWindowAttributes (dp, pwin, &pwattr);
  181.   XGetWindowAttributes (dp, win, &wattr);
  182.   width = wattr.width;
  183.   height = wattr.height;
  184.  
  185. #ifdef DEBUG
  186.   cerr << "wid, height = " << width << "," << height << '\n';
  187. #endif
  188.   pwidth = (width + 7) / 8 * 2;
  189.   pheight = height;
  190.  
  191.   xim = XGetImage (dp, win, 0, 0, width, height, 0xffff, ZPixmap);
  192. #ifdef DEBUG
  193.   fprintf (stderr, "xim = %lx\n", xim);
  194. #endif // DEBUG
  195.  
  196.   clpr
  197. #ifndef DEBUG
  198.     << "\x1b["
  199. #else
  200.     << "(ESC)1b["
  201. #endif
  202.     << dec << pwidth * pheight << ';'
  203.     << pwidth / 2 << ';'
  204.     << dot << ";;"
  205.     << pheight
  206.     << "/r";
  207. //   (fp, "\x1b[%d;%d;%d;;%d/r", pwidth * pheight, pwidth / 2, 150, pheight);
  208.  
  209. #ifdef DEBUG
  210.   cerr << "xe, wattr = " << xe.xbutton.x << ',' << wattr.x << ',' << pwattr.x
  211.     << '\n'
  212.     << xe.xbutton.y << ',' << wattr.y << ',' << pwattr.y << '\n';
  213. #endif
  214.   background = XGetPixel (xim, xe.xbutton.x - wattr.x - pwattr.x,
  215.               xe.xbutton.y - wattr.y - pwattr.y);
  216.  
  217. #ifdef DEBUG
  218.   cerr << "background = " << background << '\n';
  219. #endif // DEBUG
  220.  
  221.   clpr << hex;
  222.  
  223.   for (int xy = 0; xy < pheight; xy++) {
  224.     int xx = 0;
  225.  
  226.     for (int ix = 0; ix < pwidth; ix++) {
  227.       unsigned int b = 0;
  228.       int bitx;
  229.  
  230.       for (bitx = 0; bitx < 4; bitx++, xx++) {
  231.     b <<= 1;
  232.     if (xx < width)
  233.       b |= ((XGetPixel (xim, xx, xy) != background) ^ rflag);
  234.       }
  235.       clpr << b;
  236.     }
  237.   }
  238. #ifndef DEBUG
  239.   clpr << '\xc';
  240. #else
  241.   clpr << "(form feed)";
  242. #endif
  243.   exit (0);
  244. }
  245.  
  246.  
  247. void Usage ()
  248. {
  249.   cout <<
  250.   "Xwinpr : " PATCHLEVEL "\n"
  251.   "Usage : xwinpr [options]\n"
  252.   " where options are:\n"
  253.   "  -help              : display this message.\n"
  254.   "  -r                 : reverse black and white.\n"
  255.   "  -o file            : send output to a file.\n"
  256.   "  -p printer         : send output to the printer program.  The default\n"
  257.   "                       is \"lpr\".\n"
  258.   "  -dotpitch (number) : set dotpitch.  The default is 100.\n"
  259.   "  -nh                : Non-header mode.  Header includes the date and the\n"
  260.   "                       name of the window.\n"
  261.   ;
  262. }
  263.