6 * int getcmap(int id, char *file, unsigned char *buf)
7 * Read a colormap from the given file into the buffer.
8 * Returns 1 on success, 0 otherwise.
9 * Goes to unglaublich length to figure out what the file name means:
10 * If the name is "screen" or "display" or "vga", reads the colormap from /dev/draw/id/colormap.
11 * If the name is "gamma",returns gamma=2.3 colormap
12 * If the name is "gamma###", returns gamma=#### colormap
13 * Ditto for rgamma, for reverse video.
14 * Looks for the file in a list of directories (given below).
24 getcmap(int id, char *f, unsigned char *buf)
27 char *s, *lines[256], *fields[4];
28 int cmap, i, j, n, v, rev;
32 for(i=0; cmapdir[i]!=nil ;i++){
33 snprint(name, sizeof name, "%s%s", cmapdir[i], f);
34 if((cmap = open(name, OREAD)) >= 0)
39 if(strcmp(name, "screen")==0 || strcmp(name, "display")==0 || strcmp(name, "vga")==0){
40 snprint(name, sizeof name, "/dev/draw/%d/colormap", id);
41 cmap = open(name, OREAD);
47 if(cmap==-1){ /* could be gamma or gamma<number> or fb */
48 if(strncmp(f, "gamma", 5)==0){
51 }else if(strncmp(f, "rgamma", 6)==0){
59 if(strspn(f, "0123456789.") != strlen(f))
63 for(i=0; i!=256; i++){
64 v=255.*pow(i/255., 1./gamma);
67 buf[0] = buf[1] = buf[2] = v;
74 n = readn(cmap, s, 20000-1);
78 if(getfields(s, lines, 256, 0, "\n") != 256)
81 if(getfields(lines[i], fields, 4, 1, " \t") != 4)
83 if(atoi(fields[0]) != i)
86 buf[3*i+j] = atoi(fields[j+1]);
91 /* replicate (from top) value in v (n bits) until it fills a ulong */
99 for(o=32-n; o>=0; o-=n)
107 putcmap(int id, uchar cmap[256*3])
113 snprint(name, sizeof name, "/dev/draw/%d/colormap", id);
114 fd = open(name, OWRITE);
116 sysfatal("can't open colormap file: %r");
119 for(i = 0; i<256; i++)
120 t += sprint(t, "%d %d %d %d\n", i, cmap[3*i+0], cmap[3*i+1], cmap[3*i+2]);
121 if(write(fd, s, t-s) != t-s)
122 sysfatal("writing color map: %r");
127 main(int argc, char *argv[])
129 uchar cmapbuf[256*3];
130 char *map, buf[12*12+1];
134 fprint(2, "usage: %s colormap\n", argv[0]);
141 fd = open("/dev/draw/new", OREAD);
142 if(fd < 0 || read(fd, buf, sizeof buf) != 12*12)
143 sysfatal("can't connect to display: %r");
145 if(strncmp(buf+2*12, " m8 ", 12) != 0)
146 sysfatal("can't set colormap except on CMAP8 (m8) displays; this one is %.12s", buf+2*12);
148 if(getcmap(id, map, cmapbuf) == 0){
149 fprint(2, "%s: can't find %s\n", argv[0], map);
153 putcmap(id, cmapbuf);