/* * dtcursor.c -- MCDE: set a Solaris cursor (.cursor) on the root window * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * A .cursor file of Solaris 2.6 is the X11 cursor text format: * * /* Format_version=1, Width=16, Height=16, Depth=1, * Valid_bits_per_item=16 *\/ * 0x...., ... (Width*Height/Valid_bits_per_item source values) * 0x...., ... (the same count for the mask) * * The source and mask become two bitmaps; XCreatePixmapCursor makes the * cursor, XDefineCursor puts it on the root window (the clients that do * not set one of their own inherit it). The choice is kept in * ~/.dt/cursor; "dtcursor" without an argument sets the saved one. * * C99, POSIX and Xlib only (ext/src/mcde/AGENTS.md). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #define CURSOR_DIR CDE_INSTALLATION_TOP "/share/cursors" #define MAX_SIDE 64 static void Usage(void) { fprintf(stderr, "usage: dtcursor [-set NAME | NAME]\n"); } /* read the .cursor file; 0 on success */ static int ReadCursor(const char *path, unsigned short *src, unsigned short *mask, int *wp, int *hp) { FILE *f; char line[512]; int width = 0, height = 0, valid = 0; int n = 0, total, i, count; if ((f = fopen(path, "r")) == NULL) return -1; if (fgets(line, sizeof line, f) == NULL) { fclose(f); return -1; } if (sscanf(strstr(line, "Width=") != NULL ? strstr(line, "Width=") : line, "Width=%d", &width) != 1 || sscanf(strstr(line, "Height=") != NULL ? strstr(line, "Height=") : line, "Height=%d", &height) != 1 || sscanf(strstr(line, "Valid_bits_per_item=") != NULL ? strstr(line, "Valid_bits_per_item=") : line, "Valid_bits_per_item=%d", &valid) != 1) { fclose(f); return -1; } if (width < 1 || width > MAX_SIDE || height < 1 || height > MAX_SIDE || valid != 16) { fclose(f); return -1; } /* the values of one bitmap: width * height / 16 */ count = width * height / valid; total = 2 * count; /* source, then the mask when the file has one */ while (n < total && fgets(line, sizeof line, f) != NULL) { char *p = line; while (n < total && (p = strstr(p, "0x")) != NULL) { unsigned int v; if (sscanf(p, "0x%x", &v) != 1) break; if (n < count) src[n] = (unsigned short) v; else mask[n - count] = (unsigned short) v; n++; p += 2; } } fclose(f); if (n < count) return -1; /* a file without a mask: the form itself is the mask */ if (n < total) for (i = 0; i < count; i++) mask[i] = src[i]; *wp = width; *hp = height; return 0; } static Cursor MakeCursor(Display *dpy, const char *path, int *wp, int *hp) { unsigned short src[MAX_SIDE], mask[MAX_SIDE]; unsigned char sbits[MAX_SIDE * MAX_SIDE / 8], mbits[MAX_SIDE * MAX_SIDE / 8]; Pixmap s, m; XColor fg, bg; Cursor c; int w, h, i; if (ReadCursor(path, src, mask, &w, &h) != 0) return None; memset(sbits, 0, sizeof sbits); memset(mbits, 0, sizeof mbits); for (i = 0; i < w * h / 16; i++) { sbits[i * 2] = (unsigned char) (src[i] >> 8); sbits[i * 2 + 1] = (unsigned char) (src[i] & 0xff); mbits[i * 2] = (unsigned char) (mask[i] >> 8); mbits[i * 2 + 1] = (unsigned char) (mask[i] & 0xff); } s = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (const char *) sbits, (unsigned) w, (unsigned) h); m = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (const char *) mbits, (unsigned) w, (unsigned) h); memset(&fg, 0, sizeof fg); memset(&bg, 0, sizeof bg); fg.flags = DoRed | DoGreen | DoBlue; bg.flags = DoRed | DoGreen | DoBlue; bg.red = bg.green = bg.blue = 0xffff; c = XCreatePixmapCursor(dpy, s, m, &fg, &bg, w / 2, h / 2); XFreePixmap(dpy, s); XFreePixmap(dpy, m); if (wp != NULL) *wp = w; if (hp != NULL) *hp = h; return c; } /* the saved cursor's name, or "" */ static void SavedName(char *buf, size_t len) { const char *home = getenv("HOME"); char path[512]; FILE *f; buf[0] = '\0'; if (home == NULL) return; snprintf(path, sizeof path, "%s/.dt/cursor", home); if ((f = fopen(path, "r")) == NULL) return; if (fgets(buf, (int) len, f) != NULL) { char *nl = strchr(buf, '\n'); if (nl != NULL) *nl = '\0'; } fclose(f); } static void SaveName(const char *name) { const char *home = getenv("HOME"); char path[512]; FILE *f; if (home == NULL) return; snprintf(path, sizeof path, "%s/.dt/cursor", home); if ((f = fopen(path, "w")) != NULL) { fprintf(f, "%s\n", name); fclose(f); } } int main(int argc, char **argv) { Display *dpy; const char *name = NULL; char path[768], saved[128]; Cursor c; if (argc > 1 && strcmp(argv[1], "-set") == 0 && argc > 2) name = argv[2]; else if (argc > 1 && argv[1][0] != '-') name = argv[1]; if (argc > 1 && argv[1][0] == '-' && strcmp(argv[1], "-set") != 0) { Usage(); return 2; } if (name == NULL) { SavedName(saved, sizeof saved); if (saved[0] == '\0') return 1; name = saved; } if ((dpy = XOpenDisplay(NULL)) == NULL) { fprintf(stderr, "dtcursor: no display\n"); return 1; } snprintf(path, sizeof path, "%s/%s.cursor", CURSOR_DIR, name); c = MakeCursor(dpy, path, NULL, NULL); if (c == None) { fprintf(stderr, "dtcursor: cannot read %s\n", path); XCloseDisplay(dpy); return 1; } XDefineCursor(dpy, DefaultRootWindow(dpy), c); XSync(dpy, False); XFreeCursor(dpy, c); XCloseDisplay(dpy); SaveName(name); printf("dtcursor: %s\n", name); return 0; }