/*
  =========================================================================
  Copyright 1987, 1998  The Open Group
  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.
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  Except as contained in this notice, the name of The Open Group shall not be
  used in advertising or otherwise to promote the sale, use or other dealings
  in this Software without prior written authorization from The Open Group.
  ========================================================================= */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/XWDFile.h>
#include <stdio.h>
#include <stdlib.h>

#define lowbit(x) ((x) & (~(x) + 1))

static int ReadColors(Display * display,
		      Visual *vis,
		      Colormap cmap,
		      XColor **colors)
{
  int i , ncolors ;

  ncolors = vis->map_entries;

  if (!(*colors = (XColor *) malloc (sizeof(XColor) * ncolors))) {
    printf("Out of memory!\n");
  }

  if (vis->class == DirectColor ||
      vis->class == TrueColor) {
    unsigned long red, green, blue, red1, green1, blue1;

    red = green = blue = 0;
    red1 = lowbit(vis->red_mask);
    green1 = lowbit(vis->green_mask);
    blue1 = lowbit(vis->blue_mask);
    for (i=0; i<ncolors; i++) {
      (*colors)[i].pixel = red|green|blue;
      (*colors)[i].pad = 0;
      red += red1;
      if (red > vis->red_mask)
	red = 0;
      green += green1;
      if (green > vis->green_mask)
	green = 0;
      blue += blue1;
      if (blue > vis->blue_mask)
	blue = 0;
    }
  } else {
    for (i=0; i<ncolors; i++) {
      (*colors)[i].pixel = i;
      (*colors)[i].pad = 0;
    }
  }

  XQueryColors(display, cmap, *colors, ncolors);
    
  return(ncolors);
}
/*
 * Get the XColors of all pixels in image - returns # of colors
 */
int Get_XColors(Display * display,XWindowAttributes *win_info,XColor **colors)
{
  int ncolors;
  Colormap cmap = win_info->colormap;

  /* assume the visual will be OK ... */
  //cmap = XListInstalledColormaps(dpy, win_info->root, &i)[0];
  if (!cmap)
    return(0);
  ncolors = ReadColors(display,win_info->visual,cmap,colors) ;
  return ncolors ;
}

void _swapshort (register char *bp, register unsigned n)
{
  register char c;
  register char *ep = bp + n;

  while (bp < ep) {
    c = *bp;
    *bp = *(bp + 1);
    bp++;
    *bp++ = c;
  }
}

void _swaplong (register char *bp,register unsigned n)
{
  register char c;
  register char *ep = bp + n;

  while (bp < ep) {
    c = bp[3];
    bp[3] = bp[0];
    bp[0] = c;
    c = bp[2];
    bp[2] = bp[1];
    bp[1] = c;
    bp += 4;
  }
}

int window_capture(Display * display, unsigned int window, const char * fn) {

  XImage * image;
  XColor * colors;
  XWindowAttributes win_info;
  XWDFileHeader header;
  XWDColor xwdcolor;
  Visual * vis;
  Window dummywin;

  FILE * out;

  unsigned long swaptest = 1;
  unsigned int buffer_size;

  const char * win_name = "xwdump";

  int header_size;
  int win_name_size;
  int ncolors;
  int format = ZPixmap;
  int absx, absy;
  int screen=0;

  int i;

  out = fopen(fn,"wb");

  if (!out) { 
    perror("fopen:");
    printf("Can't open file %s\n", fn); return 1;
  }

  if (!XGetWindowAttributes(display,window,&win_info)) {
    printf("Can't get attributes\n");
    fclose(out);
    return 1;
  }

  /* handle any frame window */
  if (!XTranslateCoordinates (display, window, 
			      RootWindow (display, screen), 0, 0,
			      &absx, &absy, &dummywin)) {
    printf("Can't translate coordinates\n");
    fclose(out);
    return 1;
  }

  win_info.x = absx;
  win_info.y = absy;

  image = XGetImage(display,
		    window,
		    absx - win_info.x,
		    absy - win_info.y,
		    win_info.width,
		    win_info.height,
		    AllPlanes,
		    format);
  /*
   * Determine the pixmap size.
   */
  if (image->format != ZPixmap) {
    buffer_size = (image->bytes_per_line * image->height * image->depth);
  } else {
    buffer_size = (image->bytes_per_line * image->height);
  }

  ncolors = Get_XColors(display,&win_info, &colors);
  vis = win_info.visual ;

  win_name_size = strlen(win_name) + sizeof(char);

  /*
   * Calculate header size.
   */
  header_size = SIZEOF(XWDheader) + win_name_size;

  /*
   * Write out header information.
   */
  header.header_size = (CARD32) header_size;
  header.file_version = (CARD32) XWD_FILE_VERSION;
  header.pixmap_format = (CARD32) format;
  header.pixmap_depth = (CARD32) image->depth;
  header.pixmap_width = (CARD32) image->width;
  header.pixmap_height = (CARD32) image->height;
  header.xoffset = (CARD32) image->xoffset;
  header.byte_order = (CARD32) image->byte_order;
  header.bitmap_unit = (CARD32) image->bitmap_unit;
  header.bitmap_bit_order = (CARD32) image->bitmap_bit_order;
  header.bitmap_pad = (CARD32) image->bitmap_pad;
  header.bits_per_pixel = (CARD32) image->bits_per_pixel;
  header.bytes_per_line = (CARD32) image->bytes_per_line;
  /****
       header.visual_class = (CARD32) win_info.visual->class;
       header.red_mask = (CARD32) win_info.visual->red_mask;
       header.green_mask = (CARD32) win_info.visual->green_mask;
       header.blue_mask = (CARD32) win_info.visual->blue_mask;
       header.bits_per_rgb = (CARD32) win_info.visual->bits_per_rgb;
       header.colormap_entries = (CARD32) win_info.visual->map_entries;
  *****/
  header.visual_class = (CARD32) vis->class;
  header.red_mask = (CARD32) vis->red_mask;
  header.green_mask = (CARD32) vis->green_mask;
  header.blue_mask = (CARD32) vis->blue_mask;
  header.bits_per_rgb = (CARD32) vis->bits_per_rgb;
  header.colormap_entries = (CARD32) vis->map_entries;

  header.ncolors = ncolors;
  header.window_width = (CARD32) win_info.width;
  header.window_height = (CARD32) win_info.height;
  header.window_x = absx;
  header.window_y = absy;
  header.window_bdrwidth = (CARD32) win_info.border_width;

  if (*(char *) &swaptest) {
    _swaplong((char *) &header, sizeof(header));
    for (i = 0; i < ncolors; i++) {
      _swaplong((char *) &colors[i].pixel, sizeof(long));
      _swapshort((char *) &colors[i].red, 3 * sizeof(short));
    }
  }

  if (fwrite((char *)&header, SIZEOF(XWDheader), 1, out) != 1 ||
      fwrite(win_name, win_name_size, 1, out) != 1) {
    perror("xwd");
    fclose(out);
    return 1;
  }

  /*
   * Write out the color maps, if any
   */

  for (i = 0; i < ncolors; i++) {
    xwdcolor.pixel = colors[i].pixel;
    xwdcolor.red = colors[i].red;
    xwdcolor.green = colors[i].green;
    xwdcolor.blue = colors[i].blue;
    xwdcolor.flags = colors[i].flags;
    if (fwrite((char *) &xwdcolor, SIZEOF(XWDColor), 1, out) != 1) {
      perror("xwd");
      fclose(out);
      return 1;
    }
  }

  /*
   *    This copying of the bit stream (data) to a file is to be replaced
   *  by an Xlib call which hasn't been written yet.  It is not clear
   *  what other functions of xwd will be taken over by this (as yet)
   *  non-existant X function.
   */
  if (fwrite(image->data, (int) buffer_size, 1, out) != 1) {
    perror("xwd");
    fclose(out);
    return 1;
  }

  /*
   * free the color buffer.
   */

  if(ncolors > 0) free(colors);

  /*
   * Free image
   */
  XDestroyImage(image);
  fclose(out);
  return 0;
}

