/* * Copyright (c) 1990, 2015, Oracle and/or its affiliates. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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. * */ #include #ifndef SYSV #include #endif #include #include #include "cmc.h" #include "cmcutil.h" #define CELL_IGNORE 0 #define CELL_READONLY 1 /* ** Handle I/O errors when opening display */ static int badaccess_error; /* ** Handle X request errors. ** Just set a flag to let the routine making an X ** call know that the call resulted in a BadAccess error. ** Any other errors are processed normally. */ static int bad_handler ( Display *dpy, XErrorEvent *err) { if (err->error_code == BadAccess) badaccess_error = 1; else return _XDefaultError(dpy,err); return 0; } /* ** Examines the given colormap to determine the number of ** read-only cells, their locations, and their color values. ** ** All non-read-only cells (privately allocated, unallocated ** or reserved) are ignored. */ static void cmc_record ( Screen *screen, int *ncolors, XColor **colors) { register Colormap cmap = DefaultColormapOfScreen(screen); register int i, nalloc; register int *p; register XColor *c; int totalpix; unsigned long masks; /* NOTUSED */ Pixel *pixels; int *pixtype; XColor color; /* start out assuming all are read-only */ totalpix = 1< 0; nalloc >>= 1) { if(!XAllocColorCells(DisplayOfScreen(screen), cmap, 0, &masks, 0, pixels, nalloc)) continue; for (i=0; ipixel = i; } } XQueryColors(DisplayOfScreen(screen), cmap, *colors, *ncolors); #ifdef SYSV free(pixels); free(pixtype); #endif } /* ** Examines the default colormap with the server grabbed ** to prevent changes during the examination process. */ static void cmc_record_protected ( Screen *screen, int *ncolors, XColor **colors) { XGrabServer(DisplayOfScreen(screen)); cmc_record(screen, ncolors, colors); XUngrabServer(DisplayOfScreen(screen)); } /* ** For each screen for which the default colormap is dynamic indexed ** record all read-only color cells in the default colormap. */ void cmc_save (void) { register int scr_num; Display *dpy; FILE *f; const char *filename; /* Open display */ if (!(dpy = open_display(display_name))) fatal_error("cannot open display '%s'", display_name); /* For some strange reason, colorscells_get_protected fails if not run synchronously */ XSynchronize(dpy, 1); /* Open file to save in */ filename = comp_colors_filename(basename_arg); if ((f = fopen(filename, "w")) == NULL) fatal_error("cannot open file '%s' for writing", filename); /* Save magic number and version */ cmc_header_write(f); /* For each screen of display ... */ for (scr_num = 0; scr_num < ScreenCount(dpy); scr_num++) { Screen *screen = ScreenOfDisplay(dpy, scr_num); int ncolors; XColor *colors; /* Do nothing if default visual is not dynamic */ if (!dynamic_indexed_default_visual(screen)) { if (warn_flag) { warning("default visual for screen %d is not dynamic indexed", scr_num); warning("no colors saved for screen %d", scr_num ); } continue; } /* ** Discover what read-only cells are in the default colormap. ** ** These will be considered the "workspace colors." */ cmc_record_protected(screen, &ncolors, &colors); /* Save read-only cells for screen in workspace color file */ if (!cmc_write(f, scr_num, ncolors, colors)) fatal_error("cannot write to file %s", filename); free((char *)colors); } XCloseDisplay(dpy); }