Advertisement
Guest User

Xlib color text

a guest
Jul 23rd, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <X11/X.h>
  6. #include <X11/Xlib.h>
  7.  
  8. int main() {
  9.   Display *dpy;
  10.   Window rootwin;
  11.   Window win;
  12.   Colormap cmap;
  13.   XEvent e;
  14.   int scr;
  15.   GC gc;
  16.  
  17.   if (!(dpy = XOpenDisplay(NULL))) {
  18.     fprintf(stderr, "ERROR: could not open display\n");
  19.     exit(1);
  20.   }
  21.   scr = DefaultScreen(dpy);
  22.   rootwin = RootWindow(dpy, scr);
  23.   cmap = DefaultColormap(dpy, scr);
  24.  
  25.   win = XCreateSimpleWindow(dpy, rootwin, 1, 1, 100, 50, 0,
  26.                             BlackPixel(dpy, scr), BlackPixel(dpy, scr));
  27.   XStoreName(dpy, win, "hello");
  28.   gc = XCreateGC(dpy, win, 0, NULL);
  29.  
  30.   XSelectInput(dpy, win, ExposureMask | ButtonPressMask);
  31.   XMapWindow(dpy, win);
  32.  
  33.     while (1) {
  34.       XNextEvent(dpy, &e);
  35.  
  36.       if (e.type == Expose && e.xexpose.count < 1) {
  37.  
  38.         char buf[] = "Hello World\x01, Bye!\x02";
  39.         char cleanBuf[strlen(buf)];
  40.         memset(cleanBuf, 0, strlen(cleanBuf));
  41.         char *copy = strdup(buf);
  42.         char *delim = "\x01\x02";
  43.         char *res = strtok(buf, delim);
  44.         strcat(cleanBuf, res);
  45.         unsigned long color1 = 0xff0000;
  46.         unsigned long color2 = 0x00ff00;
  47.         unsigned long color;
  48.         int x = 10;
  49.         while (res) {
  50.           /* Figure out what delimiter was used */
  51.           // Thanks to http://stackoverflow.com/a/12460511/1612432
  52.           char deli = copy[res - buf + strlen(res)];
  53.           if (deli == '\x01')
  54.             color = color1;
  55.           else if (deli == '\x02')
  56.             color = color2;
  57.           else
  58.             color = 0xffffff;
  59.           XSetForeground(dpy, gc, color);
  60.           XDrawString(dpy, win, gc, x, 10, res, strlen(res));
  61.           x += 50;
  62.           res = strtok(0, delim);
  63.           if (res)
  64.             strcat(cleanBuf, res);
  65.         }
  66.         free(copy);
  67.  
  68.       } else if (e.type == ButtonPress)
  69.         break;
  70.  
  71.     }
  72.  
  73.   XCloseDisplay(dpy);
  74.   return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement