History

connect to x display and setup color map

yiyus committed 15 years ago

Commit 097a9761 parent cf2796d10 files · +422 −3Browse files

Unified · Split

10 files
  1. .hgignore +1 −0
  2. devdraw/devdraw.h +4 −0
  3. devdraw/main.c +7 −1
  4. devdraw/mkfile +6 −1
  5. devdraw/x11/inc.h +34 −0
  6. devdraw/x11/init.c +338 −0
  7. devdraw/x11/x.h +21 −0
  8. mkfiles/mkfile-Linux-386 +2 −0
  9. mkfiles/mkone-sh +4 −1
  10. mkfiles/mksyslib-sh +5 −0
.hgignore+1−0
@@ -1,3 +1,4 @@
1\.o$1\.o$
2\.a$2\.a$
3/o\.out$3/o\.out$
4/bin/
devdraw/devdraw.h+4−0
@@ -26,3 +26,7 @@ struct Win · 25 unmodified lines
26extern Ixp9Srv p9srv;26extern Ixp9Srv p9srv;
27extern Fileinfo files[];27extern Fileinfo files[];
28extern int debuglevel;28extern int debuglevel;
29
30/* Window server functions */
31extern int winsrvinit(void);
32
devdraw/main.c+7−1
@@ -19,10 +19,12 @@ main(int argc, char **argv) · 18 unmodified lines
19{19{
20 IxpServer srv = {0};20 IxpServer srv = {0};
21 char *address, *cp, buf[512];21 char *address, *cp, buf[512];
22 char *argv0;
22 int fd, i;23 int fd, i;
23 24
24 address = getenv("IXP_ADDRESS");25 address = getenv("IXP_ADDRESS");
25 /* XXX TODO: use ARGBEGIN, ... from <ixp_local.h> */26 /* XXX TODO: use ARGBEGIN, ... from <ixp_local.h> */
27 argv0 = argv[0];
26 while(*++argv) {28 while(*++argv) {
27 if(strcmp(*argv, "--") == 0 || !(**argv == '-')) {29 if(strcmp(*argv, "--") == 0 || !(**argv == '-')) {
28 break;30 break;
@@ -35,7 +37,7 @@ main(int argc, char **argv) · 6 unmodified lines
35 address = GETARG();37 address = GETARG();
36 break;38 break;
37 } else {39 } else {
38 errx(1, "usage: %s [-d] [ -a ADDRESS ]", argv[0]);40 errx(1, "usage: %s [-d] [ -a ADDRESS ]", argv0);
39 }41 }
40 }42 }
41 }43 }
@@ -48,6 +50,10 @@ main(int argc, char **argv) · 6 unmodified lines
48 address = buf;50 address = buf;
49 }51 }
50 52
53 /* Initialize the window server */
54 if(winsrvinit() != 0)
55 errx(1, "unable to connect to window server");
56
51 fd = ixp_announce(address);57 fd = ixp_announce(address);
52 if(fd < 0) {58 if(fd < 0) {
53 err(1, "ixp_announce");59 err(1, "ixp_announce");
devdraw/mkfile+6−1
@@ -3,15 +3,20 @@ · 2 unmodified lines
3TARG=devdraw3TARG=devdraw
4 4
5XFILES=\5XFILES=\
6 init.$O\
6 7
7OFILES=\8OFILES=\
8 fs.$O\9 fs.$O\
9 main.$O\10 main.$O\
10 $LIBIXP\11 $LIBIXP\
11# ${XFILES:%=x11/%}\12 ${XFILES:%=x11/%}\
13
14XHFILES=\
15 inc.h\
12 16
13HFILES=\17HFILES=\
14 devdraw.h\18 devdraw.h\
19 ${XHFILES:%=x11/%}\
15 20
16LIBS= 9 draw memdraw memlayer21LIBS= 9 draw memdraw memlayer
17 22
devdraw/x11/inc.h+34−0
@@ -0,0 +1,34 @@
1#define Colormap XColormap
2#define Cursor XCursor
3#define Display XDisplay
4#define Drawable XDrawable
5#define Font XFont
6#define GC XGC
7#define Point XPoint
8#define Rectangle XRectangle
9#define Screen XScreen
10#define Visual XVisual
11#define Window XWindow
12
13#include <X11/Xlib.h>
14#include <X11/Xatom.h>
15#include <X11/Xutil.h>
16#include <X11/keysym.h>
17#include <X11/IntrinsicP.h>
18#include <X11/StringDefs.h>
19#ifdef SHOWEVENT
20#include "../rio/showevent/ShowEvent.c"
21#endif
22
23#undef Colormap
24#undef Cursor
25#undef Display
26#undef Drawable
27#undef Font
28#undef GC
29#undef Point
30#undef Rectangle
31#undef Screen
32#undef Visual
33#undef Window
34
devdraw/x11/init.c+338−0
@@ -0,0 +1,338 @@
1#include <lib9.h>
2#include "draw.h"
3#include "inc.h"
4#include "x.h"
5
6static void plan9cmap(void);
7static int setupcmap(XWindow);
8
9Xconn xconn;
10
11static int
12xerror(XDisplay *d, XErrorEvent *e)
13{
14 char buf[200];
15
16 if(e->request_code == 42) /* XSetInputFocus */
17 return 0;
18 if(e->request_code == 18) /* XChangeProperty */
19 return 0;
20 /*
21 * BadDrawable happens in apps that get resized a LOT,
22 * e.g. when KDE is configured to resize continuously
23 * during a window drag.
24 */
25 if(e->error_code == 9) /* BadDrawable */
26 return 0;
27
28 fprint(2, "X error: error_code=%d, request_code=%d, minor=%d disp=%p\n",
29 e->error_code, e->request_code, e->minor_code, d);
30 XGetErrorText(d, e->error_code, buf, sizeof buf);
31 fprint(2, "%s\n", buf);
32 return 0;
33}
34
35static int
36xioerror(XDisplay *d)
37{
38 /*print("X I/O error\n"); */
39 exit(0);
40 /*sysfatal("X I/O error\n");*/
41 abort();
42 return -1;
43}
44
45/*
46 * Connect to the X server.
47 */
48int
49winsrvinit(void)
50{
51 char *disp;
52 int i, n, xrootid;
53 XPixmapFormatValues *pfmt;
54 XScreen *xscreen;
55 XWindow xrootwin;
56 XVisualInfo xvi;
57
58 /*
59 * Connect to X server.
60 */
61 xconn.display = XOpenDisplay(NULL);
62 if(xconn.display == nil){
63 disp = getenv("DISPLAY");
64 werrstr("XOpenDisplay %s: %r", disp ? disp : ":0");
65 free(disp);
66 return -1;
67 }
68 xconn.fd = ConnectionNumber(xconn.display);
69 XSetErrorHandler(xerror);
70 XSetIOErrorHandler(xioerror);
71 xrootid = DefaultScreen(xconn.display);
72 xrootwin = DefaultRootWindow(xconn.display);
73
74 /*
75 * Figure out underlying screen format.
76 */
77 if(XMatchVisualInfo(xconn.display, xrootid, 16, TrueColor, &xvi)
78 || XMatchVisualInfo(xconn.display, xrootid, 16, DirectColor, &xvi)){
79 xconn.vis = xvi.visual;
80 xconn.depth = 16;
81 }
82 else
83 if(XMatchVisualInfo(xconn.display, xrootid, 15, TrueColor, &xvi)
84 || XMatchVisualInfo(xconn.display, xrootid, 15, DirectColor, &xvi)){
85 xconn.vis = xvi.visual;
86 xconn.depth = 15;
87 }
88 else
89 if(XMatchVisualInfo(xconn.display, xrootid, 24, TrueColor, &xvi)
90 || XMatchVisualInfo(xconn.display, xrootid, 24, DirectColor, &xvi)){
91 xconn.vis = xvi.visual;
92 xconn.depth = 24;
93 }
94 else
95 if(XMatchVisualInfo(xconn.display, xrootid, 8, PseudoColor, &xvi)
96 || XMatchVisualInfo(xconn.display, xrootid, 8, StaticColor, &xvi)){
97 if(xconn.depth > 8){
98 werrstr("can't deal with colormapped depth %d screens",
99 xconn.depth);
100 goto err0;
101 }
102 xconn.vis = xvi.visual;
103 xconn.depth = 8;
104 }
105 else{
106 xconn.depth = DefaultDepth(xconn.display, xrootid);
107 if(xconn.depth != 8){
108 werrstr("can't understand depth %d screen", xconn.depth);
109 goto err0;
110 }
111 xconn.vis = DefaultVisual(xconn.display, xrootid);
112 }
113
114 if(DefaultDepth(xconn.display, xrootid) == xconn.depth)
115 xconn.usetable = 1;
116
117 /*
118 * xconn.depth is only the number of significant pixel bits,
119 * not the total number of pixel bits. We need to walk the
120 * display list to find how many actual bits are used
121 * per pixel.
122 */
123 xconn.chan = 0;
124 pfmt = XListPixmapFormats(xconn.display, &n);
125 for(i=0; i<n; i++){
126 if(pfmt[i].depth == xconn.depth){
127 switch(pfmt[i].bits_per_pixel){
128 case 1: /* untested */
129 xconn.chan = GREY1;
130 break;
131 case 2: /* untested */
132 xconn.chan = GREY2;
133 break;
134 case 4: /* untested */
135 xconn.chan = GREY4;
136 break;
137 case 8:
138 xconn.chan = CMAP8;
139 break;
140 case 15:
141 xconn.chan = RGB15;
142 break;
143 case 16: /* how to tell RGB15? */
144 xconn.chan = RGB16;
145 break;
146 case 24: /* untested (impossible?) */
147 xconn.chan = RGB24;
148 break;
149 case 32:
150 xconn.chan = XRGB32;
151 break;
152 }
153 }
154 }
155 if(xconn.chan == 0){
156 werrstr("could not determine screen pixel format");
157 goto err0;
158 }
159
160 /*
161 * Set up color map if necessary.
162 */
163 xscreen = DefaultScreenOfDisplay(xconn.display);
164 xconn.cmap = DefaultColormapOfScreen(xscreen);
165 if(xconn.vis->class != StaticColor){
166 plan9cmap();
167 setupcmap(xrootwin);
168 }
169 return 0;
170
171err0:
172 /*
173 * Should do a better job of cleaning up here.
174 */
175 XCloseDisplay(xconn.display);
176 return -1;
177}
178
179/*
180 * Initialize map with the Plan 9 rgbv color map.
181 */
182static void
183plan9cmap(void)
184{
185 int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
186 static int once;
187
188 if(once)
189 return;
190 once = 1;
191
192 for(r=0; r!=4; r++)
193 for(g = 0; g != 4; g++)
194 for(b = 0; b!=4; b++)
195 for(v = 0; v!=4; v++){
196 den=r;
197 if(g > den)
198 den=g;
199 if(b > den)
200 den=b;
201 /* divide check -- pick grey shades */
202 if(den==0)
203 cr=cg=cb=v*17;
204 else {
205 num=17*(4*den+v);
206 cr=r*num/den;
207 cg=g*num/den;
208 cb=b*num/den;
209 }
210 idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
211 xconn.map[idx].red = cr*0x0101;
212 xconn.map[idx].green = cg*0x0101;
213 xconn.map[idx].blue = cb*0x0101;
214 xconn.map[idx].pixel = idx;
215 xconn.map[idx].flags = DoRed|DoGreen|DoBlue;
216
217 v7 = v >> 1;
218 idx7 = r*32 + v7*16 + g*4 + b;
219 if((v & 1) == v7){
220 xconn.map7to8[idx7][0] = idx;
221 if(den == 0) { /* divide check -- pick grey shades */
222 cr = ((255.0/7.0)*v7)+0.5;
223 cg = cr;
224 cb = cr;
225 }
226 else {
227 num=17*15*(4*den+v7*2)/14;
228 cr=r*num/den;
229 cg=g*num/den;
230 cb=b*num/den;
231 }
232 xconn.map7[idx7].red = cr*0x0101;
233 xconn.map7[idx7].green = cg*0x0101;
234 xconn.map7[idx7].blue = cb*0x0101;
235 xconn.map7[idx7].pixel = idx7;
236 xconn.map7[idx7].flags = DoRed|DoGreen|DoBlue;
237 }
238 else
239 xconn.map7to8[idx7][1] = idx;
240 }
241}
242
243/*
244 * Initialize and install the rgbv color map as a private color map
245 * for this application. It gets the best colors when it has the
246 * cursor focus.
247 *
248 * We always choose the best depth possible, but that might not
249 * be the default depth. On such "suboptimal" systems, we have to allocate an
250 * empty color map anyway, according to Axel Belinfante.
251 */
252static int
253setupcmap(XWindow w)
254{
255 char buf[30];
256 int i;
257 u32int p, pp;
258 XColor c;
259
260 if(xconn.depth <= 1)
261 return 0;
262
263 if(xconn.depth >= 24) {
264 if(xconn.usetable == 0)
265 xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocNone);
266
267 /*
268 * The pixel value returned from XGetPixel needs to
269 * be converted to RGB so we can call rgb2cmap()
270 * to translate between 24 bit X and our color. Unfortunately,
271 * the return value appears to be display server endian
272 * dependant. Therefore, we run some heuristics to later
273 * determine how to mask the int value correctly.
274 * Yeah, I know we can look at xconn.vis->byte_order but
275 * some displays say MSB even though they run on LSB.
276 * Besides, this is more anal.
277 */
278 c = xconn.map[19]; /* known to have different R, G, B values */
279 if(!XAllocColor(xconn.display, xconn.cmap, &c)){
280 werrstr("XAllocColor: %r");
281 return -1;
282 }
283 p = c.pixel;
284 pp = rgb2cmap((p>>16)&0xff,(p>>8)&0xff,p&0xff);
285 if(pp != xconn.map[19].pixel) {
286 /* check if endian is other way */
287 pp = rgb2cmap(p&0xff,(p>>8)&0xff,(p>>16)&0xff);
288 if(pp != xconn.map[19].pixel){
289 werrstr("cannot detect X server byte order");
290 return -1;
291 }
292
293 switch(xconn.chan){
294 case RGB24:
295 xconn.chan = BGR24;
296 break;
297 case XRGB32:
298 xconn.chan = XBGR32;
299 break;
300 default:
301 werrstr("cannot byteswap channel %s",
302 chantostr(buf, xconn.chan));
303 break;
304 }
305 }
306 }else if(xconn.vis->class == TrueColor || xconn.vis->class == DirectColor){
307 /*
308 * Do nothing. We have no way to express a
309 * mixed-endian 16-bit screen, so pretend they don't exist.
310 */
311 if(xconn.usetable == 0)
312 xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocNone);
313 }else if(xconn.vis->class == PseudoColor){
314 if(xconn.usetable == 0){
315 xconn.cmap = XCreateColormap(xconn.display, w, xconn.vis, AllocAll);
316 XStoreColors(xconn.display, xconn.cmap, xconn.map, 256);
317 for(i = 0; i < 256; i++){
318 xconn.tox11[i] = i;
319 xconn.toplan9[i] = i;
320 }
321 }else{
322 for(i = 0; i < 128; i++){
323 c = xconn.map7[i];
324 if(!XAllocColor(xconn.display, xconn.cmap, &c)){
325 werrstr("can't allocate colors in 7-bit map");
326 return -1;
327 }
328 xconn.tox11[xconn.map7to8[i][0]] = c.pixel;
329 xconn.tox11[xconn.map7to8[i][1]] = c.pixel;
330 xconn.toplan9[c.pixel] = xconn.map7to8[i][0];
331 }
332 }
333 }else{
334 werrstr("unsupported visual class %d", xconn.vis->class);
335 return -1;
336 }
337 return 0;
338}
devdraw/x11/x.h+21−0
@@ -0,0 +1,21 @@
1typedef struct Xconn Xconn;
2typedef struct Xwin Xwin;
3
4struct Xconn {
5 u32int chan;
6 XColormap cmap;
7 XDisplay *display;
8 int fd; /* of display */
9 int depth; /* of screen */
10 XColor map[256];
11 XColor map7[128];
12 uchar map7to8[128][2];
13 int toplan9[256];
14 int tox11[256];
15 int usetable;
16 XVisual *vis;
17};
18
19struct Xwin {};
20
21extern Xconn xconn;
mkfiles/mkfile-Linux-386+2−0
@@ -24,6 +24,8 @@ ANSICPP= · 23 unmodified lines
24LD= cc -m3224LD= cc -m32
25LDFLAGS= -L/usr/openwin/lib\25LDFLAGS= -L/usr/openwin/lib\
26 -L/usr/X11R6/lib\26 -L/usr/X11R6/lib\
27 -L/usr/lib\
28 -lX11\
27 29
28SYSLIBS=30SYSLIBS=
29 31
mkfiles/mkone-sh+4−1
@@ -2,7 +2,7 @@ libs=${LIBS:%=$ROOT/$OBJDIR/lib/lib%.a} · 1 unmodified line
2 2
3all:V: $O.out3all:V: $O.out
4 4
5install:V: $BIN/$TARG5install:V: mkdir $BIN/$TARG
6 6
7installall:V:7installall:V:
8 for objtype in $CPUS8 for objtype in $CPUS
@@ -14,6 +14,9 @@ nuke:V: nuke-std · 5 unmodified lines
14 14
15clean:V: clean-std15clean:V: clean-std
16 16
17mkdir:V:
18 mkdir -p $ROOT/$OBJDIR/bin
19
17$O.out: $OFILES $libs20$O.out: $OFILES $libs
18 $LD $LDFLAGS -o $target $OFILES $libs $SYSLIBS21 $LD $LDFLAGS -o $target $OFILES $libs $SYSLIBS
19 22
mkfiles/mksyslib-sh+5−0
@@ -7,6 +7,8 @@ LIBOBJ=${OFILES:%=$LIBRARY(%)} · 6 unmodified lines
7 7
8default:V: all8default:V: all
9 9
10install:V: mkdir
11
10all install:V: $LIBRARY12all install:V: $LIBRARY
11 13
12installall:V:14installall:V:
@@ -19,6 +21,9 @@ clean:V: clean-std · 6 unmodified lines
19 21
20nuke:V: nuke-std22nuke:V: nuke-std
21 23
24mkdir:V:
25 mkdir -p $ROOT/$OBJDIR/lib
26
22$LIBRARY: $LIBOBJ27$LIBRARY: $LIBOBJ
23 $AR $ARFLAGS $target $newmember28 $AR $ARFLAGS $target $newmember
24 29

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t