dmxcmap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation on the rights to use, copy, modify, merge,
  10. * publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. /*
  28. * Authors:
  29. * Kevin E. Martin <kem@redhat.com>
  30. *
  31. */
  32. /** \file
  33. * Colormap support. */
  34. #ifdef HAVE_DMX_CONFIG_H
  35. #include <dmx-config.h>
  36. #endif
  37. #include "dmx.h"
  38. #include "dmxlog.h"
  39. #include "dmxsync.h"
  40. #include "dmxcmap.h"
  41. #include "dmxvisual.h"
  42. #include "micmap.h"
  43. static Bool
  44. dmxAllocateColormapPrivates(ColormapPtr pColormap)
  45. {
  46. dmxColormapPrivPtr pCmapPriv;
  47. pCmapPriv = (dmxColormapPrivPtr) malloc(sizeof(*pCmapPriv));
  48. if (!pCmapPriv)
  49. return FALSE;
  50. pCmapPriv->cmap = (Colormap) 0;
  51. DMX_SET_COLORMAP_PRIV(pColormap, pCmapPriv);
  52. return TRUE;
  53. }
  54. /** Create \a pColormap on the back-end server. */
  55. Bool
  56. dmxBECreateColormap(ColormapPtr pColormap)
  57. {
  58. ScreenPtr pScreen = pColormap->pScreen;
  59. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  60. dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
  61. VisualPtr pVisual = pColormap->pVisual;
  62. Visual *visual = dmxLookupVisual(pScreen, pVisual);
  63. if (visual) {
  64. pCmapPriv->cmap = XCreateColormap(dmxScreen->beDisplay,
  65. dmxScreen->scrnWin,
  66. visual,
  67. (pVisual->class & DynamicClass ?
  68. AllocAll : AllocNone));
  69. return pCmapPriv->cmap != 0;
  70. }
  71. else {
  72. dmxLog(dmxWarning, "dmxBECreateColormap: No visual found\n");
  73. return 0;
  74. }
  75. }
  76. /** Create colormap on back-end server associated with \a pColormap's
  77. * screen. */
  78. Bool
  79. dmxCreateColormap(ColormapPtr pColormap)
  80. {
  81. ScreenPtr pScreen = pColormap->pScreen;
  82. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  83. Bool ret = TRUE;
  84. if (!dmxAllocateColormapPrivates(pColormap))
  85. return FALSE;
  86. if (dmxScreen->beDisplay) {
  87. if (!dmxBECreateColormap(pColormap))
  88. return FALSE;
  89. }
  90. DMX_UNWRAP(CreateColormap, dmxScreen, pScreen);
  91. if (pScreen->CreateColormap)
  92. ret = pScreen->CreateColormap(pColormap);
  93. DMX_WRAP(CreateColormap, dmxCreateColormap, dmxScreen, pScreen);
  94. return ret;
  95. }
  96. /** Destroy \a pColormap on the back-end server. */
  97. Bool
  98. dmxBEFreeColormap(ColormapPtr pColormap)
  99. {
  100. ScreenPtr pScreen = pColormap->pScreen;
  101. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  102. dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
  103. if (pCmapPriv->cmap) {
  104. XFreeColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
  105. pCmapPriv->cmap = (Colormap) 0;
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. /** Destroy colormap on back-end server associated with \a pColormap's
  111. * screen. */
  112. void
  113. dmxDestroyColormap(ColormapPtr pColormap)
  114. {
  115. ScreenPtr pScreen = pColormap->pScreen;
  116. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  117. dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
  118. if (dmxScreen->beDisplay)
  119. dmxBEFreeColormap(pColormap);
  120. free(pCmapPriv);
  121. DMX_SET_COLORMAP_PRIV(pColormap, NULL);
  122. DMX_UNWRAP(DestroyColormap, dmxScreen, pScreen);
  123. if (pScreen->DestroyColormap)
  124. pScreen->DestroyColormap(pColormap);
  125. DMX_WRAP(DestroyColormap, dmxDestroyColormap, dmxScreen, pScreen);
  126. }
  127. /** Install colormap on back-end server associated with \a pColormap's
  128. * screen. */
  129. void
  130. dmxInstallColormap(ColormapPtr pColormap)
  131. {
  132. ScreenPtr pScreen = pColormap->pScreen;
  133. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  134. dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
  135. DMX_UNWRAP(InstallColormap, dmxScreen, pScreen);
  136. if (pScreen->InstallColormap)
  137. pScreen->InstallColormap(pColormap);
  138. DMX_WRAP(InstallColormap, dmxInstallColormap, dmxScreen, pScreen);
  139. if (dmxScreen->beDisplay) {
  140. XInstallColormap(dmxScreen->beDisplay, pCmapPriv->cmap);
  141. dmxSync(dmxScreen, FALSE);
  142. }
  143. }
  144. /** Store colors in \a pColormap on back-end server associated with \a
  145. * pColormap's screen. */
  146. void
  147. dmxStoreColors(ColormapPtr pColormap, int ndef, xColorItem * pdef)
  148. {
  149. ScreenPtr pScreen = pColormap->pScreen;
  150. DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
  151. dmxColormapPrivPtr pCmapPriv = DMX_GET_COLORMAP_PRIV(pColormap);
  152. if (dmxScreen->beDisplay && (pColormap->pVisual->class & DynamicClass)) {
  153. XColor *color = malloc(sizeof(*color) * ndef);
  154. int i;
  155. if (color) {
  156. for (i = 0; i < ndef; i++) {
  157. color[i].pixel = pdef[i].pixel;
  158. color[i].red = pdef[i].red;
  159. color[i].blue = pdef[i].blue;
  160. color[i].green = pdef[i].green;
  161. color[i].flags = pdef[i].flags;
  162. color[i].pad = pdef[i].pad;
  163. }
  164. XStoreColors(dmxScreen->beDisplay, pCmapPriv->cmap, color, ndef);
  165. free(color);
  166. }
  167. else { /* xalloc failed, so fallback */
  168. XColor c;
  169. for (i = 0; i < ndef; i++) {
  170. c.pixel = pdef[i].pixel;
  171. c.red = pdef[i].red;
  172. c.blue = pdef[i].blue;
  173. c.green = pdef[i].green;
  174. c.flags = pdef[i].flags;
  175. c.pad = pdef[i].pad;
  176. XStoreColor(dmxScreen->beDisplay, pCmapPriv->cmap, &c);
  177. }
  178. }
  179. dmxSync(dmxScreen, FALSE);
  180. }
  181. DMX_UNWRAP(StoreColors, dmxScreen, pScreen);
  182. if (pScreen->StoreColors)
  183. pScreen->StoreColors(pColormap, ndef, pdef);
  184. DMX_WRAP(StoreColors, dmxStoreColors, dmxScreen, pScreen);
  185. }
  186. /** Create the DMX server's default colormap. */
  187. Bool
  188. dmxCreateDefColormap(ScreenPtr pScreen)
  189. {
  190. return miCreateDefColormap(pScreen);
  191. }