slock

Kris's build of slock (slock-flexipatch)
git clone git clone https://git.krisyotam.com/krisyotam/slock.git
Log | Files | Refs | README | LICENSE

message.c (2216B)


      1 #include <X11/extensions/Xinerama.h>
      2 
      3 /* global count to prevent repeated error messages */
      4 int count_error = 0;
      5 
      6 static void
      7 writemessage(Display *dpy, Window win, int screen)
      8 {
      9 	int len, line_len, width, height, s_width, s_height, i, j, k, tab_replace, tab_size;
     10 	XGCValues gr_values;
     11 	XFontStruct *fontinfo;
     12 	XColor color, dummy;
     13 	XineramaScreenInfo *xsi;
     14 	GC gc;
     15 	fontinfo = XLoadQueryFont(dpy, font_name);
     16 
     17 	if (fontinfo == NULL) {
     18 		if (count_error == 0) {
     19 			fprintf(stderr, "slock: Unable to load font \"%s\"\n", font_name);
     20 			fprintf(stderr, "slock: Try listing fonts with 'slock -f'\n");
     21 			count_error++;
     22 		}
     23 		return;
     24 	}
     25 
     26 	tab_size = 8 * XTextWidth(fontinfo, " ", 1);
     27 
     28 	XAllocNamedColor(dpy, DefaultColormap(dpy, screen),
     29 		text_color, &color, &dummy);
     30 
     31 	gr_values.font = fontinfo->fid;
     32 	gr_values.foreground = color.pixel;
     33 	gc=XCreateGC(dpy,win,GCFont+GCForeground, &gr_values);
     34 
     35 	/*  To prevent "Uninitialized" warnings. */
     36 	xsi = NULL;
     37 
     38 	/*
     39 	 * Start formatting and drawing text
     40 	 */
     41 
     42 	len = strlen(message);
     43 
     44 	/* Max max line length (cut at '\n') */
     45 	line_len = 0;
     46 	k = 0;
     47 	for (i = j = 0; i < len; i++) {
     48 		if (message[i] == '\n') {
     49 			if (i - j > line_len)
     50 				line_len = i - j;
     51 			k++;
     52 			i++;
     53 			j = i;
     54 		}
     55 	}
     56 	/* If there is only one line */
     57 	if (line_len == 0)
     58 		line_len = len;
     59 
     60 	if (XineramaIsActive(dpy)) {
     61 		xsi = XineramaQueryScreens(dpy, &i);
     62 		s_width = xsi[0].width;
     63 		s_height = xsi[0].height;
     64 	} else {
     65 		s_width = DisplayWidth(dpy, screen);
     66 		s_height = DisplayHeight(dpy, screen);
     67 	}
     68 
     69 	height = s_height*3/7 - (k*20)/3;
     70 	width  = (s_width - XTextWidth(fontinfo, message, line_len))/2;
     71 
     72 	/* Look for '\n' and print the text between them. */
     73 	for (i = j = k = 0; i <= len; i++) {
     74 		/* i == len is the special case for the last line */
     75 		if (i == len || message[i] == '\n') {
     76 			tab_replace = 0;
     77 			while (message[j] == '\t' && j < i) {
     78 				tab_replace++;
     79 				j++;
     80 			}
     81 
     82 			XDrawString(dpy, win, gc, width + tab_size*tab_replace, height + 20*k, message + j, i - j);
     83 			while (i < len && message[i] == '\n') {
     84 				i++;
     85 				j = i;
     86 				k++;
     87 			}
     88 		}
     89 	}
     90 
     91 	/* xsi should not be NULL anyway if Xinerama is active, but to be safe */
     92 	if (XineramaIsActive(dpy) && xsi != NULL)
     93 		XFree(xsi);
     94 }