libctwm.c revision 0bbfda8a
1/*
2 * Copyright 1992 Claude Lecommandeur.
3 */
4#include <X11/Xlib.h>
5#include <X11/Xatom.h>
6
7#include <stdlib.h>
8#include <string.h>
9#include "ctwm.h"
10#include "ctwm_client.h"
11
12Atom _XA_WM_OCCUPATION;
13Atom _XA_WM_CURRENTWORKSPACE;
14Atom _XA_WM_WORKSPACESLIST;
15
16/* Note that this doesn't assure that ctwm is really running,
17   i should set up a communication via flipping a property */
18
19bool CtwmIsRunning(Display *display, int scrnum)
20{
21	unsigned char       *prop;
22	unsigned long       bytesafter;
23	unsigned long       len;
24	Atom                actual_type;
25	int                 actual_format;
26
27	_XA_WM_WORKSPACESLIST = XInternAtom(display, "WM_WORKSPACESLIST", True);
28	if(_XA_WM_WORKSPACESLIST == None) {
29		return false;
30	}
31	if(XGetWindowProperty(display, RootWindow(display, scrnum),
32	                      _XA_WM_WORKSPACESLIST, 0L, 512,
33	                      False, XA_STRING, &actual_type, &actual_format, &len,
34	                      &bytesafter, &prop) != Success) {
35		return false;
36	}
37	if(len == 0) {
38		return false;
39	}
40	XFree(prop);
41	return true;
42}
43
44char **CtwmListWorkspaces(Display *display, int scrnum)
45{
46	unsigned char       *prop;
47	char                *p;
48	unsigned long       bytesafter;
49	unsigned long       len;
50	Atom                actual_type;
51	int                 actual_format;
52	char                **ret;
53	int                 count;
54	int                 i;
55	unsigned long       l;
56
57	_XA_WM_WORKSPACESLIST = XInternAtom(display, "_WIN_WORKSPACE_NAMES", True);
58
59	if(XGetWindowProperty(display, RootWindow(display, scrnum),
60	                      _XA_WM_WORKSPACESLIST, 0L, 512,
61	                      False, XA_STRING, &actual_type, &actual_format, &len,
62	                      &bytesafter, &prop) != Success) {
63		return 0;
64	}
65	if(len == 0) {
66		return 0;
67	}
68
69	count = 0;
70	p = (char *)prop;
71	l = 0;
72	while(l < len) {
73		l += strlen(p) + 1;
74		p += strlen(p) + 1;
75		count++;
76	}
77	ret = calloc(count + 1, sizeof(char *));
78
79	p = (char *)prop;
80	l = 0;
81	i = 0;
82	while(l < len) {
83		ret [i++] = p;
84		l += strlen(p) + 1;
85		p += strlen(p) + 1;
86	}
87	ret [i] = NULL;
88	XFree(prop);
89	return (ret);
90}
91
92char *CtwmCurrentWorkspace(Display *display, int scrnum)
93{
94	unsigned char       *prop;
95	unsigned long       bytesafter;
96	unsigned long       len;
97	Atom                actual_type;
98	int                 actual_format;
99
100	_XA_WM_CURRENTWORKSPACE = XInternAtom(display, "WM_CURRENTWORKSPACE", True);
101	if(_XA_WM_CURRENTWORKSPACE == None) {
102		return NULL;
103	}
104
105	if(XGetWindowProperty(display, RootWindow(display, scrnum),
106	                      _XA_WM_CURRENTWORKSPACE, 0L, 512,
107	                      False, XA_STRING, &actual_type, &actual_format, &len,
108	                      &bytesafter, &prop) != Success) {
109		return NULL;
110	}
111	if(len == 0) {
112		return NULL;
113	}
114	return ((char *) prop);
115}
116
117int CtwmChangeWorkspace(Display *display, int scrnum, char     *workspace)
118{
119	_XA_WM_CURRENTWORKSPACE = XInternAtom(display, "WM_CURRENTWORKSPACE", True);
120	if(_XA_WM_CURRENTWORKSPACE == None) {
121		return (0);
122	}
123
124	XChangeProperty(display, RootWindow(display, scrnum), _XA_WM_CURRENTWORKSPACE,
125	                XA_STRING, 8,
126	                PropModeReplace, (unsigned char *) workspace, strlen(workspace));
127	XFlush(display);
128	return (1);
129}
130
131char **CtwmCurrentOccupation(Display *display, Window window)
132{
133	unsigned char       *prop;
134	char                *p;
135	unsigned long       bytesafter;
136	unsigned long       len;
137	Atom                actual_type;
138	int                 actual_format;
139	int                 count, i;
140	unsigned long       l;
141	char                **ret;
142
143	_XA_WM_OCCUPATION = XInternAtom(display, "WM_OCCUPATION", True);
144	if(_XA_WM_OCCUPATION == None) {
145		return NULL;
146	}
147
148	if(XGetWindowProperty(display, window, _XA_WM_OCCUPATION, 0L, 512,
149	                      False, XA_STRING, &actual_type, &actual_format, &len,
150	                      &bytesafter, &prop) != Success) {
151		return NULL;
152	}
153	if(len == 0) {
154		return NULL;
155	}
156
157	count = 0;
158	p = (char *)prop;
159	l = 0;
160	while(l < len) {
161		l += strlen(p) + 1;
162		p += strlen(p) + 1;
163		count++;
164	}
165	ret = calloc(count + 1, sizeof(char *));
166
167	p = (char *)prop;
168	l = 0;
169	i = 0;
170	while(l < len) {
171		ret [i++] = p;
172		l += strlen(p) + 1;
173		p += strlen(p) + 1;
174	}
175	ret [i] = NULL;
176	XFree(prop);
177	return ret;
178}
179
180int CtwmSetOccupation(Display *display, Window window, char **occupation)
181{
182	int         len;
183	char        **occ;
184	char        *occup, *o;
185
186	_XA_WM_OCCUPATION = XInternAtom(display, "WM_OCCUPATION", True);
187	if(_XA_WM_OCCUPATION == None) {
188		return (0);
189	}
190
191	occ = occupation;
192	len = 0;
193	while(*occ++) {
194		len += strlen(*occupation) + 1;
195	}
196	occup = calloc(len, sizeof(char));
197
198	o = occup;
199	while(*occupation) {
200		strcpy(o, *occupation);
201		o += strlen(*occupation) + 1;
202		occupation++;
203	}
204	XChangeProperty(display, window, _XA_WM_OCCUPATION, XA_STRING, 8,
205	                PropModeReplace, (unsigned char *) occup, len - 1);
206	XFlush(display);
207	free(occup);
208	return (1);
209}
210
211int CtwmAddToCurrentWorkspace(Display *display, Window window)
212{
213	unsigned char       *prop;
214	unsigned long       bytesafter;
215	unsigned long       len;
216	Atom                actual_type;
217	int                 actual_format;
218	XWindowAttributes   attr;
219	unsigned char       *currentw;
220
221	if(! XGetWindowAttributes(display, window, &attr)) {
222		return (0);
223	}
224
225	_XA_WM_CURRENTWORKSPACE = XInternAtom(display, "WM_CURRENTWORKSPACE", True);
226	if(_XA_WM_CURRENTWORKSPACE == None) {
227		return (0);
228	}
229
230	if(XGetWindowProperty(display, attr.root, _XA_WM_CURRENTWORKSPACE, 0L, 512,
231	                      False, XA_STRING, &actual_type, &actual_format, &len,
232	                      &bytesafter, &currentw) != Success) {
233		return (0);
234	}
235	if(len == 0) {
236		return (0);
237	}
238
239	_XA_WM_OCCUPATION = XInternAtom(display, "WM_OCCUPATION", True);
240	if(_XA_WM_OCCUPATION == None) {
241		return (0);
242	}
243
244	if(XGetWindowProperty(display, window, _XA_WM_OCCUPATION, 0L, 512,
245	                      False, XA_STRING, &actual_type, &actual_format, &len,
246	                      &bytesafter, &prop) != Success) {
247		return (0);
248	}
249	if(len == 0) {
250		return (0);
251	}
252
253	strcpy((char *)prop + len, (char *)currentw);
254	XChangeProperty(display, window, _XA_WM_OCCUPATION, XA_STRING, 8,
255	                PropModeReplace,
256	                prop, (int) len + strlen((char *)currentw));
257	XFree(prop);
258	XFree(currentw);
259	XFlush(display);
260	return (1);
261}
262