- int i, ip;
-
- /*
- * Free the loaded fonts in the font cache. This is done backwards
- * from the frccur.
- */
- for (i = 0, ip = frccur; i < frclen; i++, ip--) {
- if (ip < 0)
- ip = LEN(frc) - 1;
- XftFontClose(xw.dpy, frc[ip].font);
- }
- frccur = -1;
- frclen = 0;
-
- XftFontClose(xw.dpy, dc.font.match);
- FcPatternDestroy(dc.font.pattern);
- FcFontSetDestroy(dc.font.set);
- XftFontClose(xw.dpy, dc.bfont.match);
- FcPatternDestroy(dc.bfont.pattern);
- FcFontSetDestroy(dc.bfont.set);
- XftFontClose(xw.dpy, dc.ifont.match);
- FcPatternDestroy(dc.ifont.pattern);
- FcFontSetDestroy(dc.ifont.set);
- XftFontClose(xw.dpy, dc.ibfont.match);
- FcPatternDestroy(dc.ibfont.pattern);
- FcFontSetDestroy(dc.ibfont.set);
-}
-
-void
-xzoom(const Arg *arg)
-{
- xunloadfonts();
- xloadfonts(usedfont, usedfontsize + arg->i);
- cresize(0, 0);
- redraw(0);
-}
-
-void
-xinit(void) {
- XSetWindowAttributes attrs;
- Cursor cursor;
- Window parent;
- int sw, sh, major, minor;
-
- if(!(xw.dpy = XOpenDisplay(NULL)))
- die("Can't open display\n");
- xw.scr = XDefaultScreen(xw.dpy);
- xw.vis = XDefaultVisual(xw.dpy, xw.scr);
-
- /* font */
- if (!FcInit())
- die("Could not init fontconfig.\n");
-
- usedfont = (opt_font == NULL)? font : opt_font;
- xloadfonts(usedfont, 0);
-
- /* colors */
- xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
- xloadcols();
-
- /* adjust fixed window geometry */
- if(xw.isfixed) {
- sw = DisplayWidth(xw.dpy, xw.scr);
- sh = DisplayHeight(xw.dpy, xw.scr);
- if(xw.fx < 0)
- xw.fx = sw + xw.fx - xw.fw - 1;
- if(xw.fy < 0)
- xw.fy = sh + xw.fy - xw.fh - 1;
-
- xw.h = xw.fh;
- xw.w = xw.fw;
- } else {
- /* window - default size */
- xw.h = 2 * borderpx + term.row * xw.ch;
- xw.w = 2 * borderpx + term.col * xw.cw;
- xw.fx = 0;
- xw.fy = 0;
- }
-
- /* Events */
- attrs.background_pixel = dc.col[defaultbg].pixel;
- attrs.border_pixel = dc.col[defaultbg].pixel;
- attrs.bit_gravity = NorthWestGravity;
- attrs.event_mask = FocusChangeMask | KeyPressMask
- | ExposureMask | VisibilityChangeMask | StructureNotifyMask
- | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
- attrs.colormap = xw.cmap;
-
- parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
- XRootWindow(xw.dpy, xw.scr);
- xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
- xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
- xw.vis,
- CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
- | CWColormap,
- &attrs);
-
- /* double buffering */
- /*
- if(XdbeQueryExtension(xw.dpy, &major, &minor)) {
- xw.buf = XdbeAllocateBackBufferName(xw.dpy, xw.win,
- XdbeBackground);
- usedbe = True;
- } else {
- */
- dc.gc = XCreateGC(xw.dpy, parent, 0, 0);
- xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
- DefaultDepth(xw.dpy, xw.scr));
- XSetForeground(xw.dpy, dc.gc, 0);
- XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
- //xw.buf = xw.win;
- /*
- }
- */
-
- /* Xft rendering context */
- xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
-
- /* input methods */
- if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
- XSetLocaleModifiers("@im=local");
- if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
- XSetLocaleModifiers("@im=");
- if((xw.xim = XOpenIM(xw.dpy,
- NULL, NULL, NULL)) == NULL) {
- die("XOpenIM failed. Could not open input"
- " device.\n");
- }
- }
- }
- xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
- | XIMStatusNothing, XNClientWindow, xw.win,
- XNFocusWindow, xw.win, NULL);
- if(xw.xic == NULL)
- die("XCreateIC failed. Could not obtain input method.\n");
-
- /* white cursor, black outline */
- cursor = XCreateFontCursor(xw.dpy, XC_xterm);
- XDefineCursor(xw.dpy, xw.win, cursor);
- XRecolorCursor(xw.dpy, cursor,
- &(XColor){.red = 0xffff, .green = 0xffff, .blue = 0xffff},
- &(XColor){.red = 0x0000, .green = 0x0000, .blue = 0x0000});
-
- xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
- xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
- XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
-
- xresettitle();
- XMapWindow(xw.dpy, xw.win);
- xhints();
- XSync(xw.dpy, 0);
-}
-
-void
-xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
- int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
- width = charlen * xw.cw, xp, i;
- int frp, frcflags;
- int u8fl, u8fblen, u8cblen, doesexist;
- char *u8c, *u8fs;
- long u8char;
- Font *font = &dc.font;
- FcResult fcres;
- FcPattern *fcpattern, *fontpattern;
- FcFontSet *fcsets[] = { NULL };
- FcCharSet *fccharset;
- Colour *fg = &dc.col[base.fg], *bg = &dc.col[base.bg],
- *temp, revfg, revbg;
- XRenderColor colfg, colbg;
-
- frcflags = FRC_NORMAL;
-
- if(base.mode & ATTR_BOLD) {
- if(BETWEEN(base.fg, 0, 7)) {
- /* basic system colors */
- fg = &dc.col[base.fg + 8];
- } else if(BETWEEN(base.fg, 16, 195)) {
- /* 256 colors */
- fg = &dc.col[base.fg + 36];
- } else if(BETWEEN(base.fg, 232, 251)) {
- /* greyscale */
- fg = &dc.col[base.fg + 4];
- }
- /*
- * Those ranges will not be brightened:
- * 8 - 15 – bright system colors
- * 196 - 231 – highest 256 color cube
- * 252 - 255 – brightest colors in greyscale
- */
- font = &dc.bfont;
- frcflags = FRC_BOLD;
- }
-
- if(base.mode & ATTR_ITALIC) {
- font = &dc.ifont;
- frcflags = FRC_ITALIC;
- }
- if((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD)) {
- font = &dc.ibfont;
- frcflags = FRC_ITALICBOLD;
- }
-
- if(IS_SET(MODE_REVERSE)) {
- if(fg == &dc.col[defaultfg]) {
- fg = &dc.col[defaultbg];
- } else {
- colfg.red = ~fg->color.red;
- colfg.green = ~fg->color.green;
- colfg.blue = ~fg->color.blue;
- colfg.alpha = fg->color.alpha;
- XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
- fg = &revfg;
- }
-
- if(bg == &dc.col[defaultbg]) {
- bg = &dc.col[defaultfg];
- } else {
- colbg.red = ~bg->color.red;
- colbg.green = ~bg->color.green;
- colbg.blue = ~bg->color.blue;
- colbg.alpha = bg->color.alpha;
- XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &revbg);
- bg = &revbg;
- }
- }
-
- if(base.mode & ATTR_REVERSE) {
- temp = fg;
- fg = bg;
- bg = temp;
- }
-
- /* Intelligent cleaning up of the borders. */
- if(x == 0) {
- xclear(0, (y == 0)? 0 : winy, borderpx,
- winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
- }
- if(x + charlen >= term.col) {
- xclear(winx + width, (y == 0)? 0 : winy, xw.w,
- ((y >= term.row-1)? xw.h : (winy + xw.ch)));
- }
- if(y == 0)
- xclear(winx, 0, winx + width, borderpx);
- if(y == term.row-1)
- xclear(winx, winy + xw.ch, winx + width, xw.h);
-
- /* Clean up the region we want to draw to. */
- XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
-
- fcsets[0] = font->set;
- for (xp = winx; bytelen > 0;) {
- /*
- * Search for the range in the to be printed string of glyphs
- * that are in the main font. Then print that range. If
- * some glyph is found that is not in the font, do the
- * fallback dance.
- */
- u8fs = s;
- u8fblen = 0;
- u8fl = 0;
- for (;;) {
- u8c = s;
- u8cblen = utf8decode(s, &u8char);
- s += u8cblen;
- bytelen -= u8cblen;
-
- doesexist = XftCharIndex(xw.dpy, font->match, u8char);
- if (!doesexist || bytelen <= 0) {
- if (bytelen <= 0) {
- if (doesexist) {
- u8fl++;
- u8fblen += u8cblen;
- }
- }
-
- if (u8fl > 0) {
- XftDrawStringUtf8(xw.draw, fg,
- font->match, xp,
- winy + font->ascent,
- (FcChar8 *)u8fs,
- u8fblen);
- xp += font->width * u8fl;
- }
- break;
- }
-
- u8fl++;
- u8fblen += u8cblen;
- }
- if (doesexist)
- break;
-
- frp = frccur;
- /* Search the font cache. */
- for (i = 0; i < frclen; i++, frp--) {
- if (frp <= 0)
- frp = LEN(frc) - 1;
-
- if (frc[frp].c == u8char
- && frc[frp].flags == frcflags) {
- break;
- }
- }
-
- /* Nothing was found. */
- if (i >= frclen) {
- /*
- * Nothing was found in the cache. Now use
- * some dozen of Fontconfig calls to get the
- * font for one single character.
- */
- fcpattern = FcPatternDuplicate(font->pattern);
- fccharset = FcCharSetCreate();
-
- FcCharSetAddChar(fccharset, u8char);
- FcPatternAddCharSet(fcpattern, FC_CHARSET,
- fccharset);
- FcPatternAddBool(fcpattern, FC_SCALABLE,
- FcTrue);
-
- FcConfigSubstitute(0, fcpattern,
- FcMatchPattern);
- FcDefaultSubstitute(fcpattern);
-
- fontpattern = FcFontSetMatch(0, fcsets,
- FcTrue, fcpattern, &fcres);
-
- /*
- * Overwrite or create the new cache entry
- * entry.
- */
- frccur++;
- frclen++;
- if (frccur >= LEN(frc))
- frccur = 0;
- if (frclen > LEN(frc)) {
- frclen = LEN(frc);
- XftFontClose(xw.dpy, frc[frccur].font);
- }
-
- frc[frccur].font = XftFontOpenPattern(xw.dpy,
- fontpattern);
- frc[frccur].c = u8char;
- frc[frccur].flags = frcflags;
-
- FcPatternDestroy(fcpattern);
- FcCharSetDestroy(fccharset);
-
- frp = frccur;
- }
-
- XftDrawStringUtf8(xw.draw, fg, frc[frp].font,
- xp, winy + frc[frp].font->ascent,
- (FcChar8 *)u8c, u8cblen);
-
- xp += font->width;
- }
-
- /*
- XftDrawStringUtf8(xw.draw, fg, font->set, winx,
- winy + font->ascent, (FcChar8 *)s, bytelen);
- */
-
- if(base.mode & ATTR_UNDERLINE) {
- XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
- width, 1);
- }
-}
-
-void
-xdrawcursor(void) {
- static int oldx = 0, oldy = 0;
- int sl;
- Glyph g = {{' '}, ATTR_NULL, defaultbg, defaultcs, 0};
-
- LIMIT(oldx, 0, term.col-1);
- LIMIT(oldy, 0, term.row-1);
-
- if(term.line[term.c.y][term.c.x].state & GLYPH_SET)
- memcpy(g.c, term.line[term.c.y][term.c.x].c, UTF_SIZ);
-
- /* remove the old cursor */
- if(term.line[oldy][oldx].state & GLYPH_SET) {
- sl = utf8size(term.line[oldy][oldx].c);
- xdraws(term.line[oldy][oldx].c, term.line[oldy][oldx], oldx,
- oldy, 1, sl);
- } else {
- xtermclear(oldx, oldy, oldx, oldy);
- }
-
- /* draw the new one */
- if(!(IS_SET(MODE_HIDE))) {
- if(!(xw.state & WIN_FOCUSED))
- g.bg = defaultucs;
-
- if(IS_SET(MODE_REVERSE))
- g.mode |= ATTR_REVERSE, g.fg = defaultcs, g.bg = defaultfg;
-
- sl = utf8size(g.c);
- xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
- oldx = term.c.x, oldy = term.c.y;
- }
-}
-
-void
-xresettitle(void) {
- XStoreName(xw.dpy, xw.win, opt_title ? opt_title : "st");
-}
-
-void
-redraw(int timeout) {
- struct timespec tv = {0, timeout * 1000};
-