File: sprop.c

package info (click to toggle)
suckless-tools 47-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 768 kB
  • sloc: ansic: 3,434; makefile: 448; sh: 80
file content (72 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

static char *getprop(Atom atom);
static void setprop(Atom atom, char *value);

static Atom utf8;
static Display *dpy;
static Window win;

int
main(int argc, char *argv[])
{
	char *value = NULL;
	Atom atom;

	if(!(dpy = XOpenDisplay(NULL))) {
		fputs("sprop: cannot open display\n", stderr);
		return 1;
	}
	switch(argc) {
	case 4:
		value = argv[3];
	case 3:
		atom = XInternAtom(dpy, argv[2], True);
		utf8 = XInternAtom(dpy, "UTF8_STRING", True);
		win = atol(argv[1]);
		break;
	case 2:
		if(!strcmp(argv[1], "-v")) {
			fputs("sprop-"VERSION", © 2010 Connor Lane Smith\n", stdout);
			return 0;
		}
	default:
		fprintf(stderr, "usage: sprop <xid> <atom> [<value>] [-v]\n");
		return 1;
	}
	if(value)
		setprop(atom, value);
	else {
		if(!(value = getprop(atom))) {
			fputs("sprop: cannot get atom\n", stderr);
			return 1;
		}
		printf("%s\n", value);
		XFree(value);
	}
	XCloseDisplay(dpy);
	return 0;
}

char *
getprop(Atom atom)
{
	int di;
	unsigned long dl;
	unsigned char *p = NULL;
	Atom da;

	XGetWindowProperty(dpy, win, atom, 0, BUFSIZ, False, utf8, &da, &di, &dl, &dl, &p);
	return (char *)p;
}

void
setprop(Atom atom, char *value)
{
	XChangeProperty(dpy, win, atom, utf8, 8, PropModeReplace,
			(unsigned char *)value, strlen(value));
}