/*
 * Called from signal handler with lock held
 */
static void
repaint (cairo_5c_surface_t *c5s, int x, int y, int w, int h)
{
    cairo_5c_gui_t  *gui = c5s->u.window.gui;
    if (gui->window && 
	(gui->new_width != c5s->width ||
	 gui->new_height != c5s->height))
    {
	allocate_pixmap (c5s);
    }
    
    if (gui->window && gui->pixmap && gui->gc)
    {
	Display	    *dpy = gui->global->dpy;
    
	if (w == 0)
	    w = c5s->width - x;
	if (h == 0)
	    h = c5s->height - y;
	gui->dirty = 0;
	XCopyArea (dpy, gui->pixmap, gui->window, gui->gc,
		   x, y, w, h, x, y);
	XFlush (dpy);
    }
}
void
cairo_5c_gui_check_size (cairo_5c_surface_t *c5s)
{
    cairo_5c_gui_t *gui = c5s->u.window.gui;
    
    if (gui->disable == 0 && 
	(gui->new_width != c5s->width ||
	 gui->new_height != c5s->height))
	allocate_pixmap (c5s);
}
void *img_read_quantel(const T_CHAR *fname, int *w, int *h, int type) {
  FILE *fileyuv;
  IMAGE *image;
  LPIXEL *bufout1, *bufout2, *buf;
  int xsize, ysize, imgoffs, quantel_xsize;
  int y, exit = 0;

  fileyuv = _wfopen(fname, L"rb");
  if (fileyuv == NULL) {
    /*printf("img_read_quantel error: unable to open file %s\n", fname);*/
    return NIL;
  }
  if (type == VPB_FORMAT) {
    if (!vpb_get_info(fileyuv, &xsize, &ysize, &imgoffs)) {
      fclose(fileyuv);
      return NIL;
    }
    fseek(fileyuv, imgoffs, SEEK_SET);
    quantel_xsize = xsize;
  } else {
    if (!quantel_get_info(fname, type, &xsize, &ysize)) {
      fclose(fileyuv);
      return NIL;
    }
    quantel_xsize = QUANTEL_XSIZE;
  }
  image       = new_img();
  image->type = TOONZRGB;
  if (!allocate_pixmap(image, xsize, ysize)) return NIL;
  bufout1 = bufout2 = (LPIXEL *)image->buffer;
  bufout1 += ysize * xsize - 1;
  bufout2 += (ysize - 1) * xsize - 1;

  if (type == SDL_FORMAT) {
    for (y = 0; y < ysize; y += 2) {
      buf = bufout1 - (quantel_xsize - 1);
      QUANTEL_GET_YUV_LINE(fileyuv, buf, quantel_xsize)
      if (exit) break;
      bufout1 -= quantel_xsize * 2;
    }
    for (y = 1; y < ysize; y += 2) {
      buf = bufout2 - (quantel_xsize - 1);
      QUANTEL_GET_YUV_LINE(fileyuv, buf, quantel_xsize)
      if (exit) break;
      bufout2 -= quantel_xsize * 2;
    }
  } else {
    for (y = 0; y < ysize; y++) {
Bool
cairo_5c_gui_create (cairo_5c_surface_t *c5s, char *name, int width, int height, Bool shown)
{
    ENTER ();
    x_global_t		    *xg;
    cairo_5c_gui_t	    *gui;
    Display		    *dpy;
    XGCValues		    gcv;
    int			    screen;
    XSizeHints		    sizeHints;
    XWMHints		    wmHints;
    XClassHint		    classHints;
    XSetWindowAttributes    attr;
    
    xg = x_global_create ();

    if (aborting)
    {
	EXIT ();
	return False;
    }

    dpy = xg->dpy;
    screen = DefaultScreen (dpy);
    
    if (!width)
	width = XDisplayWidth (dpy, screen) / 3;
    if (!height)
	height = XDisplayWidth (dpy, screen) / 3;

    gui = malloc (sizeof (cairo_5c_gui_t));
    
    xg->ref_count++;
    gui->global = xg;
    gui->pixmap = None;

    gui->root = RootWindow (dpy, screen);
    gui->dirty = 0;
    gui->disable = 0;
    gui->depth = DefaultDepth (dpy, screen);
    
    gui->new_width = width;
    gui->new_height = height;
    
    gui->send_events = NULL;
    
    attr.background_pixmap = None;
    attr.event_mask = (KeyPressMask|
		       KeyReleaseMask|
		       ButtonPressMask|
		       ButtonReleaseMask|
		       PointerMotionMask|
		       EnterWindowMask|
		       LeaveWindowMask|
		       ExposureMask|
		       StructureNotifyMask|
		       FocusChangeMask);
    
    gui->window = XCreateWindow (dpy, gui->root,
				 0, 0, gui->new_width, gui->new_height, 0,
				 gui->depth,
				 InputOutput, CopyFromParent,
				 CWEventMask|CWBackPixmap, &attr);

    gcv.foreground = 0xffffffff;
    gcv.graphics_exposures = False;
    gui->gc = XCreateGC (dpy, gui->window,
			 GCForeground | GCGraphicsExposures,
			 &gcv);
    
    set_window_surface (xg, gui->window, c5s);
    
    sizeHints.flags = 0;
    wmHints.flags = InputHint;
    wmHints.input = True;
    classHints.res_name = name;
    classHints.res_class = name;
    
    xg->wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    xg->wm_protocols = XInternAtom (dpy, "WM_PROTOCOLS", False);

    Xutf8SetWMProperties (dpy, gui->window, name, name,
			  NULL, 0, &sizeHints, &wmHints, &classHints);
    XSetWMProtocols (dpy, gui->window, &xg->wm_delete_window, 1);
			  
    if (shown)
	XMapWindow (dpy, gui->window);

    c5s->u.window.gui = gui;
    
    /* create the pixmap */
    allocate_pixmap (c5s);
    
    EXIT ();
    return True;
}