alloc-colors.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Allocate X colors. Used for testing with dense colormaps.
  2. Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <X11/Xlib.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <unistd.h>
  19. void
  20. fatal (const char *fmt, ...)
  21. {
  22. va_list ap;
  23. va_start (ap, fmt);
  24. vfprintf (stderr, fmt, ap);
  25. fputc ('\n', stderr);
  26. va_end (ap);
  27. exit (1);
  28. }
  29. void
  30. usage (const char *progname)
  31. {
  32. fprintf (stderr, "Usage %s options\n", progname);
  33. fprintf (stderr, "-n NCOLORS allocate NCOLORS colors\n");
  34. exit (1);
  35. }
  36. int
  37. main (int argc, char **argv)
  38. {
  39. Display *dpy;
  40. int opt, ncolors = 0, i;
  41. XColor *allocated;
  42. int nallocated;
  43. XColor color;
  44. Colormap cmap;
  45. while ((opt = getopt (argc, argv, "n:")) != EOF)
  46. switch (opt)
  47. {
  48. case 'n':
  49. ncolors = atoi (optarg);
  50. break;
  51. case '?':
  52. usage (argv[0]);
  53. }
  54. if (ncolors == 0)
  55. usage (argv[0]);
  56. dpy = XOpenDisplay ("");
  57. if (dpy == NULL)
  58. fatal ("Cannot open display");
  59. cmap = DefaultColormap (dpy, 0);
  60. allocated = malloc (ncolors * sizeof *allocated);
  61. nallocated = 0;
  62. memset (&color, 0, sizeof color);
  63. while (nallocated < ncolors
  64. && color.red < 65536)
  65. {
  66. allocated[nallocated] = color;
  67. if (XAllocColor (dpy, cmap, &allocated[nallocated]))
  68. {
  69. for (i = 0; i < nallocated; ++i)
  70. if (allocated[i].red == allocated[nallocated].red
  71. && allocated[i].green == allocated[nallocated].green
  72. && allocated[i].blue == allocated[nallocated].blue)
  73. break;
  74. if (i == nallocated)
  75. {
  76. printf ("allocated %d/%d/%d\n",
  77. allocated[nallocated].red,
  78. allocated[nallocated].green,
  79. allocated[nallocated].blue);
  80. ++nallocated;
  81. }
  82. }
  83. ++color.red;
  84. ++color.green;
  85. ++color.blue;
  86. }
  87. fprintf (stderr, "Waiting. Press ^C to stop.\n");
  88. while (1)
  89. sleep (10);
  90. XCloseDisplay (dpy);
  91. return 0;
  92. }