Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <X11/X.h>
- #include <X11/Xlib.h>
- int main() {
- Display *dpy;
- Window rootwin;
- Window win;
- Colormap cmap;
- XEvent e;
- int scr;
- GC gc;
- if (!(dpy = XOpenDisplay(NULL))) {
- fprintf(stderr, "ERROR: could not open display\n");
- exit(1);
- }
- scr = DefaultScreen(dpy);
- rootwin = RootWindow(dpy, scr);
- cmap = DefaultColormap(dpy, scr);
- win = XCreateSimpleWindow(dpy, rootwin, 1, 1, 100, 50, 0,
- BlackPixel(dpy, scr), BlackPixel(dpy, scr));
- XStoreName(dpy, win, "hello");
- gc = XCreateGC(dpy, win, 0, NULL);
- XSelectInput(dpy, win, ExposureMask | ButtonPressMask);
- XMapWindow(dpy, win);
- while (1) {
- XNextEvent(dpy, &e);
- if (e.type == Expose && e.xexpose.count < 1) {
- char buf[] = "Hello World\x01, Bye!\x02";
- char cleanBuf[strlen(buf)];
- memset(cleanBuf, 0, strlen(cleanBuf));
- char *copy = strdup(buf);
- char *delim = "\x01\x02";
- char *res = strtok(buf, delim);
- strcat(cleanBuf, res);
- unsigned long color1 = 0xff0000;
- unsigned long color2 = 0x00ff00;
- unsigned long color;
- int x = 10;
- while (res) {
- /* Figure out what delimiter was used */
- // Thanks to http://stackoverflow.com/a/12460511/1612432
- char deli = copy[res - buf + strlen(res)];
- if (deli == '\x01')
- color = color1;
- else if (deli == '\x02')
- color = color2;
- else
- color = 0xffffff;
- XSetForeground(dpy, gc, color);
- XDrawString(dpy, win, gc, x, 10, res, strlen(res));
- x += 50;
- res = strtok(0, delim);
- if (res)
- strcat(cleanBuf, res);
- }
- free(copy);
- } else if (e.type == ButtonPress)
- break;
- }
- XCloseDisplay(dpy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement