gdev_x_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_X *xdev = (gx_device_X *) dev;
    /*
     * Provide copies of values of parameters being set:
     * is_open, width, height, HWResolution, IsPageDevice, Max*.
     */
    gx_device_X values;
    int orig_MaxBitmap = xdev->space_params.MaxBitmap;

    long pwin = (long)xdev->pwin;
    bool save_is_page = xdev->IsPageDevice;
    int ecode = 0, code;
    bool clear_window = false;

    values = *xdev;

    /* Handle extra parameters */

    ecode = param_put_long(plist, "WindowID", &pwin, ecode);
    ecode = param_put_bool(plist, ".IsPageDevice", &values.IsPageDevice, ecode);
    ecode = param_put_int(plist, "MaxTempPixmap", &values.MaxTempPixmap, ecode);
    ecode = param_put_int(plist, "MaxTempImage", &values.MaxTempImage, ecode);

    if (ecode < 0)
        return ecode;

    /* Unless we specified a new window ID, */
    /* prevent gx_default_put_params from closing the device. */
    if (pwin == (long)xdev->pwin)
        dev->is_open = false;
    xdev->IsPageDevice = values.IsPageDevice;
    code = gx_default_put_params(dev, plist);
    dev->is_open = values.is_open; /* saved value */
    if (code < 0) {		/* Undo setting of .IsPageDevice */
        xdev->IsPageDevice = save_is_page;
        return code;
    }
    if (pwin != (long)xdev->pwin) {
        if (xdev->is_open)
            gs_closedevice(dev);
        xdev->pwin = (Window) pwin;
    }
    /* Restore the original page size if it was set by Ghostview */
    /* This gives the Ghostview user control over the /setpage entry */
    if (xdev->is_open && xdev->ghostview) {
       dev->width = values.width;
       dev->height = values.height;
       dev->x_pixels_per_inch = values.x_pixels_per_inch;
       dev->y_pixels_per_inch = values.y_pixels_per_inch;
       dev->HWResolution[0] = values.HWResolution[0];
       dev->HWResolution[1] = values.HWResolution[1];
       dev->MediaSize[0] = values.MediaSize[0];
       dev->MediaSize[1] = values.MediaSize[1];
    }
    /* If the device is open, resize the window. */
    /* Don't do this if Ghostview is active. */
    if (xdev->is_open && !xdev->ghostview &&
        (dev->width != values.width || dev->height != values.height ||
         dev->HWResolution[0] != values.HWResolution[0] ||
         dev->HWResolution[1] != values.HWResolution[1])
        ) {
        int area_width = WidthOfScreen(xdev->scr), area_height = HeightOfScreen(xdev->scr);
        int dw, dh;

        /* Get work area */
        x_get_work_area(xdev, &area_width, &area_height);

        /* Preserve screen resolution */
        dev->x_pixels_per_inch = values.x_pixels_per_inch;
        dev->y_pixels_per_inch = values.y_pixels_per_inch;
        dev->HWResolution[0] = values.HWResolution[0];
        dev->HWResolution[1] = values.HWResolution[1];

        /* Recompute window size using screen resolution and available work area size*/
        /* pixels */
        dev->width = min(dev->width, area_width);
        dev->height = min(dev->height, area_height);

        if (dev->width <= 0 || dev->height <= 0) {
            emprintf3(dev->memory, "Requested pagesize %d x %d not supported by %s device\n",
                                  dev->width, dev->height, dev->dname);
            return_error(gs_error_rangecheck);
        }
        
        /* points */
        dev->MediaSize[0] = (float)dev->width / xdev->x_pixels_per_inch * 72;
        dev->MediaSize[1] = (float)dev->height / xdev->y_pixels_per_inch * 72;

        dw = dev->width - values.width;
        dh = dev->height - values.height;
        if (dw || dh) {
            XResizeWindow(xdev->dpy, xdev->win,
                          dev->width, dev->height);
            if (xdev->bpixmap != (Pixmap) 0) {
                XFreePixmap(xdev->dpy, xdev->bpixmap);
                xdev->bpixmap = (Pixmap) 0;
            }
            xdev->dest = 0;
            clear_window = true;
        }
        /* Attempt to update the initial matrix in a sensible way. */
        /* The whole handling of the initial matrix is a hack! */
        if (xdev->initial_matrix.xy == 0) {
            if (xdev->initial_matrix.xx < 0) {	/* 180 degree rotation */
                xdev->initial_matrix.tx += dw;
            } else {		/* no rotation */
                xdev->initial_matrix.ty += dh;
            }
        } else {
            if (xdev->initial_matrix.xy < 0) {	/* 90 degree rotation */
                xdev->initial_matrix.tx += dh;
                xdev->initial_matrix.ty += dw;
            } else {		/* 270 degree rotation */
            }
        }
    }
    xdev->MaxTempPixmap = values.MaxTempPixmap;
    xdev->MaxTempImage = values.MaxTempImage;
    
    if (clear_window || xdev->space_params.MaxBitmap != orig_MaxBitmap) {
        if (xdev->is_open)
            gdev_x_clear_window(xdev);
    }
    return 0;
}
gdev_x_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_X *xdev = (gx_device_X *) dev;
     
    gx_device_X values;
    int orig_MaxBitmap = xdev->space_params.MaxBitmap;

    long pwin = (long)xdev->pwin;
    bool save_is_page = xdev->IsPageDevice;
    int ecode = 0, code;
    bool clear_window = false;

    values = *xdev;

     

    ecode = param_put_long(plist, "WindowID", &pwin, ecode);
    ecode = param_put_bool(plist, ".IsPageDevice", &values.IsPageDevice, ecode);
    ecode = param_put_int(plist, "MaxTempPixmap", &values.MaxTempPixmap, ecode);
    ecode = param_put_int(plist, "MaxTempImage", &values.MaxTempImage, ecode);

    if (ecode < 0)
        return ecode;

     
     
    if (pwin == (long)xdev->pwin)
        dev->is_open = false;
    xdev->IsPageDevice = values.IsPageDevice;
    code = gx_default_put_params(dev, plist);
    dev->is_open = values.is_open;  
    if (code < 0) {		 
        xdev->IsPageDevice = save_is_page;
        return code;
    }
    if (pwin != (long)xdev->pwin) {
        if (xdev->is_open)
            gs_closedevice(dev);
        xdev->pwin = (Window) pwin;
    }
     
     
    if (xdev->is_open && xdev->ghostview) {
       dev->width = values.width;
       dev->height = values.height;
       dev->x_pixels_per_inch = values.x_pixels_per_inch;
       dev->y_pixels_per_inch = values.y_pixels_per_inch;
       dev->HWResolution[0] = values.HWResolution[0];
       dev->HWResolution[1] = values.HWResolution[1];
       dev->MediaSize[0] = values.MediaSize[0];
       dev->MediaSize[1] = values.MediaSize[1];
    }
     
     
    if (xdev->is_open && !xdev->ghostview &&
        (dev->width != values.width || dev->height != values.height ||
         dev->HWResolution[0] != values.HWResolution[0] ||
         dev->HWResolution[1] != values.HWResolution[1])
        ) {
        int area_width = WidthOfScreen(xdev->scr), area_height = HeightOfScreen(xdev->scr);
        int dw, dh;

         
        x_get_work_area(xdev, &area_width, &area_height);

         
        dev->x_pixels_per_inch = values.x_pixels_per_inch;
        dev->y_pixels_per_inch = values.y_pixels_per_inch;
        dev->HWResolution[0] = values.HWResolution[0];
        dev->HWResolution[1] = values.HWResolution[1];

         
         
        dev->width = min(dev->width, area_width);
        dev->height = min(dev->height, area_height);

        if (dev->width <= 0 || dev->height <= 0) {
            emprintf3(dev->memory, "Requested pagesize %d x %d not supported by %s device\n",
                                  dev->width, dev->height, dev->dname);
            return_error(gs_error_rangecheck);
        }
        
         
        dev->MediaSize[0] = (float)dev->width / xdev->x_pixels_per_inch * 72;
        dev->MediaSize[1] = (float)dev->height / xdev->y_pixels_per_inch * 72;

        dw = dev->width - values.width;
        dh = dev->height - values.height;
        if (dw || dh) {
            XResizeWindow(xdev->dpy, xdev->win,
                          dev->width, dev->height);
            if (xdev->bpixmap != (Pixmap) 0) {
                XFreePixmap(xdev->dpy, xdev->bpixmap);
                xdev->bpixmap = (Pixmap) 0;
            }
            xdev->dest = 0;
            clear_window = true;
        }
         
         
        if (xdev->initial_matrix.xy == 0) {
            if (xdev->initial_matrix.xx < 0) {	 
                xdev->initial_matrix.tx += dw;
            } else {		 
                xdev->initial_matrix.ty += dh;
            }
        } else {
            if (xdev->initial_matrix.xy < 0) {	 
                xdev->initial_matrix.tx += dh;
                xdev->initial_matrix.ty += dw;
            } else {		 
            }
        }
    }
    xdev->MaxTempPixmap = values.MaxTempPixmap;
    xdev->MaxTempImage = values.MaxTempImage;
    
    if (clear_window || xdev->space_params.MaxBitmap != orig_MaxBitmap) {
        if (xdev->is_open)
            gdev_x_clear_window(xdev);
    }
    return 0;
}