-
- return (slide > 0);
-}
-
-void
-xresize(int col, int row) {
- xw.tw = MAX(1, col * xw.cw);
- xw.th = MAX(1, row * xw.ch);
-
- if(!usedbe) {
- XFreePixmap(xw.dpy, xw.buf);
- xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
- DefaultDepth(xw.dpy, xw.scr));
- XSetForeground(xw.dpy, dc.gc, dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg].pixel);
- XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
- }
-
- XftDrawChange(xw.draw, xw.buf);
-}
-
-void
-xloadcols(void) {
- int i, r, g, b;
- XRenderColor color = { .alpha = 0xffff };
-
- /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
- for(i = 0; i < LEN(colorname); i++) {
- if(!colorname[i])
- continue;
- if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, colorname[i], &dc.col[i])) {
- die("Could not allocate color '%s'\n", colorname[i]);
- }
- }
-
- /* load colors [16-255] ; same colors as xterm */
- for(i = 16, r = 0; r < 6; r++) {
- for(g = 0; g < 6; g++) {
- for(b = 0; b < 6; b++) {
- color.red = r == 0 ? 0 : 0x3737 + 0x2828 * r;
- color.green = g == 0 ? 0 : 0x3737 + 0x2828 * g;
- color.blue = b == 0 ? 0 : 0x3737 + 0x2828 * b;
- if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) {
- die("Could not allocate color %d\n", i);
- }
- i++;
- }
- }
- }
-
- for(r = 0; r < 24; r++, i++) {
- color.red = color.green = color.blue = 0x0808 + 0x0a0a * r;
- if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color,
- &dc.col[i])) {
- die("Could not allocate color %d\n", i);
- }
- }
-}
-
-void
-xtermclear(int col1, int row1, int col2, int row2) {
- XftDrawRect(xw.draw,
- &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
- borderpx + col1 * xw.cw,
- borderpx + row1 * xw.ch,
- (col2-col1+1) * xw.cw,
- (row2-row1+1) * xw.ch);
-}
-
-/*
- * Absolute coordinates.
- */
-void
-xclear(int x1, int y1, int x2, int y2) {
- XftDrawRect(xw.draw,
- &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
- x1, y1, x2-x1, y2-y1);
-}
-
-void
-xhints(void) {
- XClassHint class = {opt_class ? opt_class : termname, termname};
- XWMHints wm = {.flags = InputHint, .input = 1};
- XSizeHints *sizeh = NULL;
-
- sizeh = XAllocSizeHints();
- if(xw.isfixed == False) {
- sizeh->flags = PSize | PResizeInc | PBaseSize;
- sizeh->height = xw.h;
- sizeh->width = xw.w;
- sizeh->height_inc = xw.ch;
- sizeh->width_inc = xw.cw;
- sizeh->base_height = 2 * borderpx;
- sizeh->base_width = 2 * borderpx;
- } else {
- sizeh->flags = PMaxSize | PMinSize;
- sizeh->min_width = sizeh->max_width = xw.fw;
- sizeh->min_height = sizeh->max_height = xw.fh;
- }
-
- XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
- XFree(sizeh);
-}
-
-int
-xloadfont(Font *f, FcPattern *pattern) {
- FcPattern *match;
- FcResult result;
-
- match = FcFontMatch(NULL, pattern, &result);
- if(!match)
- return 1;
-
- if(!(f->set = FcFontSort(0, match, FcTrue, 0, &result))) {
- FcPatternDestroy(match);
- return 1;
- }
-
- if(!(f->match = XftFontOpenPattern(xw.dpy, match))) {
- FcPatternDestroy(match);
- return 1;
- }
-
- f->pattern = FcPatternDuplicate(pattern);
-
- f->ascent = f->match->ascent;
- f->descent = f->match->descent;
- f->lbearing = 0;
- f->rbearing = f->match->max_advance_width;
-
- f->height = f->match->height;
- f->width = f->lbearing + f->rbearing;
-
- return 0;
-}
-
-void
-xloadfonts(char *fontstr, int fontsize) {
- FcPattern *pattern;
- FcResult result;
- double fontval;
-
- if(fontstr[0] == '-') {
- pattern = XftXlfdParse(fontstr, False, False);
- } else {
- pattern = FcNameParse((FcChar8 *)fontstr);
- }
-
- if(!pattern)
- die("st: can't open font %s\n", fontstr);
-
- if(fontsize > 0) {
- FcPatternDel(pattern, FC_PIXEL_SIZE);
- FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
- usedfontsize = fontsize;
- } else {
- result = FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval);
- if(result == FcResultMatch) {
- usedfontsize = (int)fontval;
- } else {
- /*
- * Default font size is 12, if none given. This is to
- * have a known usedfontsize value.
- */
- FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
- usedfontsize = 12;
- }
- }
-
- FcConfigSubstitute(0, pattern, FcMatchPattern);
- FcDefaultSubstitute(pattern);
-
- if(xloadfont(&dc.font, pattern))
- die("st: can't open font %s\n", fontstr);
-
- /* Setting character width and height. */
- xw.cw = dc.font.width;
- xw.ch = dc.font.height;
-
- FcPatternDel(pattern, FC_SLANT);
- FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
- if(xloadfont(&dc.ifont, pattern))
- die("st: can't open font %s\n", fontstr);
-
- FcPatternDel(pattern, FC_WEIGHT);
- FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
- if(xloadfont(&dc.ibfont, pattern))
- die("st: can't open font %s\n", fontstr);
-
- FcPatternDel(pattern, FC_SLANT);
- FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
- if(xloadfont(&dc.bfont, pattern))
- die("st: can't open font %s\n", fontstr);
-
- FcPatternDestroy(pattern);
-}
-
-void
-xunloadfonts(void) {
- 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;
- XGCValues gcvalues;
- 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 {
- */
- memset(&gcvalues, 0, sizeof(gcvalues));
- gcvalues.graphics_exposures = False;
- dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
- &gcvalues);
- xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
- DefaultDepth(xw.dpy, xw.scr));
- XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
- 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;