initdisplay.c (2129B)
1 /* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include "x11.h" 5 6 int (*xlib_errorhandler) (Display*, XErrorEvent*); 7 8 Map windowmap; 9 Map atommap; 10 Map atomnamemap; 11 static MapEnt* wbucket[137]; 12 static MapEnt* abucket[137]; 13 static MapEnt* anamebucket[137]; 14 15 static int 16 Afmt(Fmt *f) { 17 18 return fmtstrcpy(f, atomname(va_arg(f->args, Atom))); 19 } 20 21 static int 22 Lfmt(Fmt *f) { 23 Color c; 24 25 #define fix(c, m) (ushort)((c.alpha ? (ulong)c.m * 0xffff / c.alpha : 0) >> 8) 26 c = va_arg(f->args, Color); 27 return fmtprint(f, c.alpha < 0xff00 ? "rgba:%02uhx/%02uhx/%02uhx/%02uhx" 28 : "#%02uhx%02uhx%02uhx", 29 fix(c, red), fix(c, green), fix(c, blue), c.alpha >> 8); 30 #undef fix 31 } 32 33 static int 34 Pfmt(Fmt *f) { 35 Point p; 36 37 p = va_arg(f->args, Point); 38 return fmtprint(f, "(%d,%d)", p.x, p.y); 39 } 40 41 static int 42 Rfmt(Fmt *f) { 43 Rectangle r; 44 45 r = va_arg(f->args, Rectangle); 46 return fmtprint(f, "%P+%dx%d", r.min, Dx(r), Dy(r)); 47 } 48 49 static int 50 Wfmt(Fmt *f) { 51 Window *w; 52 53 w = va_arg(f->args, Window*); 54 if(w == nil) 55 return fmtstrcpy(f, "<nil>"); 56 return fmtprint(f, "0x%ulx", w->xid); 57 } 58 59 void 60 initdisplay(void) { 61 display = XOpenDisplay(nil); 62 if(display == nil) 63 fatal("Can't open display"); 64 scr.screen = DefaultScreen(display); 65 scr.colormap = DefaultColormap(display, scr.screen); 66 scr.visual = DefaultVisual(display, scr.screen); 67 scr.gc = DefaultGC(display, scr.screen); 68 scr.depth = DefaultDepth(display, scr.screen); 69 70 scr.root.xid = RootWindow(display, scr.screen); 71 scr.root.visual = scr.visual; 72 scr.root.r = Rect(0, 0, 73 DisplayWidth(display, scr.screen), 74 DisplayHeight(display, scr.screen)); 75 scr.rect = scr.root.r; 76 77 scr.root.parent = &scr.root; 78 79 scr.xim = XOpenIM(display, nil, nil, nil); 80 81 windowmap.bucket = wbucket; 82 windowmap.nhash = nelem(wbucket); 83 atommap.bucket = abucket; 84 atommap.nhash = nelem(abucket); 85 atomnamemap.bucket = anamebucket; 86 atomnamemap.nhash = nelem(anamebucket); 87 88 fmtinstall('A', Afmt); 89 fmtinstall('L', Lfmt); 90 fmtinstall('R', Rfmt); 91 fmtinstall('P', Pfmt); 92 fmtinstall('W', Wfmt); 93 94 xlib_errorhandler = XSetErrorHandler(errorhandler); 95 }