alloc-colors.c 2.48 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1
/* Allocate X colors.  Used for testing with dense colormaps.
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 2001-2020 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
19

Gerd Moellmann's avatar
Gerd Moellmann committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>

void
fatal (const char *fmt, ...)
{
  va_list ap;

  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  fputc ('\n', stderr);
  va_end (ap);
  exit (1);
}

void
usage (const char *progname)
{
  fprintf (stderr, "Usage %s options\n", progname);
Paul Eggert's avatar
Paul Eggert committed
43
  fprintf (stderr, "-n NCOLORS  allocate NCOLORS colors\n");
Gerd Moellmann's avatar
Gerd Moellmann committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  exit (1);
}

int
main (int argc, char **argv)
{
  Display *dpy;
  int opt, ncolors = 0, i;
  XColor *allocated;
  int nallocated;
  XColor color;
  Colormap cmap;

  while ((opt = getopt (argc, argv, "n:")) != EOF)
    switch (opt)
      {
      case 'n':
	ncolors = atoi (optarg);
	break;
63

Gerd Moellmann's avatar
Gerd Moellmann committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      case '?':
	usage (argv[0]);
      }

  if (ncolors == 0)
    usage (argv[0]);

  dpy = XOpenDisplay ("");
  if (dpy == NULL)
    fatal ("Cannot open display");
  cmap = DefaultColormap (dpy, 0);

  allocated = malloc (ncolors * sizeof *allocated);
  nallocated = 0;
  memset (&color, 0, sizeof color);

  while (nallocated < ncolors
	 && color.red < 65536)
    {
      allocated[nallocated] = color;
      if (XAllocColor (dpy, cmap, &allocated[nallocated]))
	{
	  for (i = 0; i < nallocated; ++i)
	    if (allocated[i].red == allocated[nallocated].red
		&& allocated[i].green == allocated[nallocated].green
		&& allocated[i].blue == allocated[nallocated].blue)
	      break;

	  if (i == nallocated)
	    {
	      printf ("allocated %d/%d/%d\n",
		      allocated[nallocated].red,
		      allocated[nallocated].green,
		      allocated[nallocated].blue);
	      ++nallocated;
	    }
	}
101

Gerd Moellmann's avatar
Gerd Moellmann committed
102 103 104 105 106 107 108 109 110 111 112 113
      ++color.red;
      ++color.green;
      ++color.blue;
    }

  fprintf (stderr, "Waiting.  Press ^C to stop.\n");
  while (1)
    sleep (10);

  XCloseDisplay (dpy);
  return 0;
}