00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdio.h>
00035 #include <X11/Xlib.h>
00036 #include <X11/Xatom.h>
00037 #include <X11/Xutil.h>
00038 #include <X11/Xmu/StdCmap.h>
00039
00040 #define lowbit(x) ((x) & (~(x) + 1))
00041
00042
00043
00044
00045
00046 static Status valid_args(XVisualInfo*, unsigned long, unsigned long,
00047 unsigned long, Atom);
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 XStandardColormap *
00065 XmuStandardColormap(Display *dpy, int screen, VisualID visualid,
00066 unsigned int depth, Atom property, Colormap cmap,
00067 unsigned long red_max, unsigned long green_max,
00068 unsigned long blue_max)
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 {
00079 XStandardColormap *stdcmap;
00080 Status status;
00081 XVisualInfo vinfo_template, *vinfo;
00082 long vinfo_mask;
00083 int n;
00084
00085
00086 vinfo_template.visualid = visualid;
00087 vinfo_template.screen = screen;
00088 vinfo_template.depth = depth;
00089 vinfo_mask = VisualIDMask | VisualScreenMask | VisualDepthMask;
00090 if ((vinfo = XGetVisualInfo(dpy, vinfo_mask, &vinfo_template, &n)) == NULL)
00091 return 0;
00092
00093
00094
00095
00096
00097
00098 if (! valid_args(vinfo, red_max, green_max, blue_max, property)
00099 || ((stdcmap = XAllocStandardColormap()) == NULL)) {
00100 XFree((char *) vinfo);
00101 return 0;
00102 }
00103
00104
00105
00106 if (cmap == DefaultColormap(dpy, screen)) {
00107
00108 Window win = XCreateWindow(dpy, RootWindow(dpy, screen), 1, 1, 1, 1,
00109 0, 0, InputOnly, vinfo->visual,
00110 (unsigned long) 0,
00111 (XSetWindowAttributes *)NULL);
00112 stdcmap->killid = (XID) XCreatePixmap(dpy, win, 1, 1, depth);
00113 XDestroyWindow(dpy, win);
00114 stdcmap->colormap = cmap;
00115 } else {
00116 stdcmap->killid = ReleaseByFreeingColormap;
00117 stdcmap->colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
00118 vinfo->visual, AllocNone);
00119 }
00120 stdcmap->red_max = red_max;
00121 stdcmap->green_max = green_max;
00122 stdcmap->blue_max = blue_max;
00123 if (property == XA_RGB_GRAY_MAP)
00124 stdcmap->red_mult = stdcmap->green_mult = stdcmap->blue_mult = 1;
00125 else if (vinfo->class == TrueColor || vinfo->class == DirectColor) {
00126 stdcmap->red_mult = lowbit(vinfo->red_mask);
00127 stdcmap->green_mult = lowbit(vinfo->green_mask);
00128 stdcmap->blue_mult = lowbit(vinfo->blue_mask);
00129 } else {
00130 stdcmap->red_mult = (red_max > 0)
00131 ? (green_max + 1) * (blue_max + 1) : 0;
00132 stdcmap->green_mult = (green_max > 0) ? blue_max + 1 : 0;
00133 stdcmap->blue_mult = (blue_max > 0) ? 1 : 0;
00134 }
00135 stdcmap->base_pixel = 0;
00136 stdcmap->visualid = vinfo->visualid;
00137
00138
00139
00140 status = XmuCreateColormap(dpy, stdcmap);
00141
00142
00143
00144 XFree((char *) vinfo);
00145 if (!status) {
00146
00147
00148 if (stdcmap->killid == ReleaseByFreeingColormap)
00149 XFreeColormap(dpy, stdcmap->colormap);
00150 else if (stdcmap->killid != None)
00151 XFreePixmap(dpy, stdcmap->killid);
00152
00153 XFree((char *) stdcmap);
00154 return (XStandardColormap *) NULL;
00155 }
00156 return stdcmap;
00157 }
00158
00159
00160 static Status
00161 valid_args(XVisualInfo *vinfo, unsigned long red_max, unsigned long green_max,
00162 unsigned long blue_max, Atom property)
00163
00164
00165
00166
00167
00168 {
00169 unsigned long ncolors;
00170
00171
00172
00173 if ((vinfo->class == DirectColor) || (vinfo->class == TrueColor)) {
00174 unsigned long mask;
00175
00176 mask = vinfo->red_mask;
00177 while (!(mask & 1))
00178 mask >>= 1;
00179 if (red_max > mask)
00180 return 0;
00181 mask = vinfo->green_mask;
00182 while (!(mask & 1))
00183 mask >>= 1;
00184 if (green_max > mask)
00185 return 0;
00186 mask = vinfo->blue_mask;
00187 while (!(mask & 1))
00188 mask >>= 1;
00189 if (blue_max > mask)
00190 return 0;
00191 } else if (property == XA_RGB_GRAY_MAP) {
00192 ncolors = red_max + green_max + blue_max + 1;
00193 if (ncolors > vinfo->colormap_size)
00194 return 0;
00195 } else {
00196 ncolors = (red_max + 1) * (green_max + 1) * (blue_max + 1);
00197 if (ncolors > vinfo->colormap_size)
00198 return 0;
00199 }
00200
00201
00202
00203 switch (property)
00204 {
00205 case XA_RGB_DEFAULT_MAP:
00206 if (red_max == 0 || green_max == 0 || blue_max == 0)
00207 return 0;
00208 break;
00209 case XA_RGB_RED_MAP:
00210 if (red_max == 0)
00211 return 0;
00212 break;
00213 case XA_RGB_GREEN_MAP:
00214 if (green_max == 0)
00215 return 0;
00216 break;
00217 case XA_RGB_BLUE_MAP:
00218 if (blue_max == 0)
00219 return 0;
00220 break;
00221 case XA_RGB_BEST_MAP:
00222 if (red_max == 0 || green_max == 0 || blue_max == 0)
00223 return 0;
00224 break;
00225 case XA_RGB_GRAY_MAP:
00226 if (red_max == 0 || blue_max == 0 || green_max == 0)
00227 return 0;
00228 break;
00229 default:
00230 return 0;
00231 }
00232 return 1;
00233 }