summaryrefslogtreecommitdiff
path: root/src/vwm.c
blob: c2e51269744921e9d5099c77f93435c1c414f6c1 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *                                  \/\/\
 *
 *  Copyright (C) 2012-2016  Vito Caputo - <vcaputo@gnugeneration.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#include <X11/extensions/sync.h>	/* SYNC extension, enables us to give vwm the highest X client priority, helps keep vwm responsive at all times */
#include <X11/extensions/Xinerama.h>	/* XINERAMA extension, facilitates easy multihead awareness */
#include <X11/extensions/Xrandr.h>	/* RANDR extension, facilitates display configuration change awareness */
#include <X11/extensions/Xdamage.h>	/* Damage extension, enables receipt of damage events, reports visible regions needing updating (compositing) */
#include <X11/extensions/Xrender.h>     /* Render extension, enables use of alpha channels and accelerated rendering of surfaces having alpha (compositing) */
#include <X11/extensions/Xcomposite.h>	/* Composite extension, enables off-screen redirection of window rendering (compositing) */
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <values.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <poll.h>

#include "composite.h"
#include "desktop.h"
#include "launch.h"
#include "logo.h"
#include "vwm.h"
#include "xevent.h"
#include "xwindow.h"

static vwm_t	vwm;

	/* Sync */
static int	sync_event, sync_error;

	/* Xinerama */
static int	xinerama_event, xinerama_error;
static int	randr_event, randr_error;

	/* Compositing */
static int	composite_event, composite_error, composite_opcode;

static int errhandler(Display *display, XErrorEvent *err)
{
	/* TODO */
	return 1;
}

int main(int argc, char *argv[])
{
	int		err = 0;
	int		done = 0;
	XEvent		event;
	Cursor		pointer;
	struct pollfd	pfd;
	char		*console_args[] = {"xterm", "-class", CONSOLE_WM_CLASS, "-e", "/bin/sh", "-c", "screen -D -RR " CONSOLE_SESSION_STRING, NULL};

#define reterr_if(_cond, _fmt, _args...) \
	err++;\
	if (_cond) {\
		VWM_ERROR(_fmt, ##_args);\
		return err;\
	}

	INIT_LIST_HEAD(&vwm.desktops);
	INIT_LIST_HEAD(&vwm.desktops_mru);
	INIT_LIST_HEAD(&vwm.windows_mru);
	INIT_LIST_HEAD(&vwm.xwindows);

	/* open connection with the server */
	reterr_if((vwm.display = XOpenDisplay(NULL)) == NULL, "Cannot open display");

	/* prevent children from inheriting the X connection */
	reterr_if(fcntl(ConnectionNumber(vwm.display), F_SETFD, FD_CLOEXEC) < 0, "Cannot set FD_CLOEXEC on X connection");

	/* get our scheduling priority, clients are launched with a priority LAUNCHED_RELATIVE_PRIORITY nicer than this */
	reterr_if((vwm.priority = getpriority(PRIO_PROCESS, getpid())) == -1, "Cannot get scheduling priority");

	XSetErrorHandler(errhandler);

	vwm.screen_num = DefaultScreen(vwm.display);

	/* query the needed X extensions */
	reterr_if(!XQueryExtension (vwm.display, COMPOSITE_NAME, &composite_opcode, &composite_event, &composite_error), "No composite extension available");
	reterr_if(!XDamageQueryExtension(vwm.display, &vwm.damage_event, &vwm.damage_error), "No damage extension available");
	if (XSyncQueryExtension(vwm.display, &sync_event, &sync_error)) {
		/* set the window manager to the maximum X client priority */
		XSyncSetPriority(vwm.display, VWM_XROOT(&vwm), 0x7fffffff);
	}

	if (XineramaQueryExtension(vwm.display, &xinerama_event, &xinerama_error)) {
		vwm.xinerama_screens = XineramaQueryScreens(vwm.display, &vwm.xinerama_screens_cnt);
	}

	if (XRRQueryExtension(vwm.display, &randr_event, &randr_error)) {
		XRRSelectInput(vwm.display, VWM_XROOT(&vwm), RRScreenChangeNotifyMask);
	}

	/* allocate colors, I make assumptions about the X server's color capabilities since I'll only use this on modern-ish computers... */
	vwm.cmap = DefaultColormap(vwm.display, vwm.screen_num);

#define color(_sym, _str) \
	XAllocNamedColor(vwm.display, vwm.cmap, _str, &vwm.colors._sym ## _color, &vwm.colors._sym ## _color);
#include "colors.def"
#undef color

	vwm.wm_delete_atom = XInternAtom(vwm.display, "WM_DELETE_WINDOW", False);
	vwm.wm_protocols_atom = XInternAtom(vwm.display, "WM_PROTOCOLS", False);
	vwm.wm_pid_atom = XInternAtom(vwm.display, "_NET_WM_PID", False);

	XSelectInput(vwm.display, VWM_XROOT(&vwm),
		     PropertyChangeMask | SubstructureNotifyMask | SubstructureRedirectMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
	XGrabKey(vwm.display, AnyKey, WM_GRAB_MODIFIER, VWM_XROOT(&vwm), False, GrabModeAsync, GrabModeAsync);

	XFlush(vwm.display);

	XSetInputFocus(vwm.display, VWM_XROOT(&vwm), RevertToPointerRoot, CurrentTime);

	/* create initial virtual desktop */
	vwm_desktop_focus(&vwm, vwm_desktop_create(&vwm, NULL));
	vwm_desktop_mru(&vwm, vwm.focused_desktop);

	/* manage all preexisting windows */
	vwm_xwin_create_existing(&vwm);

	/* create GC for logo drawing and window rubber-banding */
	vwm.gc = XCreateGC(vwm.display, VWM_XROOT(&vwm), 0, NULL);
	XSetSubwindowMode(vwm.display, vwm.gc, IncludeInferiors);
	XSetFunction(vwm.display, vwm.gc, GXxor);

	/* launch the console here so it's likely ready by the time the logo animation finishes (there's no need to synchronize with it currently) */
	vwm_launch(&vwm, console_args, VWM_LAUNCH_MODE_BG);

	/* first the logo color is the foreground */
	XSetForeground(vwm.display, vwm.gc, vwm.colors.logo_color.pixel);
	vwm_draw_logo(&vwm);

	/* change to the rubber-banding foreground color */
	XSetForeground(vwm.display, vwm.gc, vwm.colors.rubberband_color.pixel);

	XClearWindow(vwm.display, VWM_XROOT(&vwm));

	/* set the pointer */
	pointer = XCreateFontCursor(vwm.display, XC_X_cursor);
	XDefineCursor(vwm.display, VWM_XROOT(&vwm), pointer);

	pfd.events = POLLIN;
	pfd.revents = 0;
	pfd.fd = ConnectionNumber(vwm.display);

	while (!done) {
		do {
			int	delay;

			vwm_overlay_update(&vwm, &delay);
			XFlush(vwm.display);

			if (!XPending(vwm.display)) {
				if (poll(&pfd, 1, delay) == 0) break;
			}

			XNextEvent(vwm.display, &event);
			switch (event.type) {
				case KeyPress:
					VWM_TRACE("keypress");
					vwm_xevent_handle_key_press(&vwm, &event.xkey);
					break;

				case KeyRelease:
					VWM_TRACE("keyrelease");
					vwm_xevent_handle_key_release(&vwm, &event.xkey);
					break;

				case ButtonPress:
					VWM_TRACE("buttonpresss");
					vwm_xevent_handle_button_press(&vwm, &event.xbutton);
					break;

				case MotionNotify:
					//VWM_TRACE("motionnotify");
					vwm_xevent_handle_motion_notify(&vwm, &event.xmotion);
					break;

				case ButtonRelease:
					VWM_TRACE("buttonrelease");
					vwm_xevent_handle_button_release(&vwm, &event.xbutton);
					break;

				case CreateNotify:
					VWM_TRACE("createnotify");
					vwm_xevent_handle_create_notify(&vwm, &event.xcreatewindow);
					break;

				case DestroyNotify:
					VWM_TRACE("destroynotify");
					vwm_xevent_handle_destroy_notify(&vwm, &event.xdestroywindow);
					break;

				case ConfigureRequest:
					VWM_TRACE("configurerequest");
					vwm_xevent_handle_configure_request(&vwm, &event.xconfigurerequest);
					break;

				case ConfigureNotify:
					VWM_TRACE("configurenotify");
					vwm_xevent_handle_configure_notify(&vwm, &event.xconfigure);
					break;

				case UnmapNotify:
					VWM_TRACE("unmapnotify");
					vwm_xevent_handle_unmap_notify(&vwm, &event.xunmap);
					break;

				case MapNotify:
					VWM_TRACE("mapnotify");
					vwm_xevent_handle_map_notify(&vwm, &event.xmap);
					break;

				case MapRequest:
					VWM_TRACE("maprequest");
					vwm_xevent_handle_map_request(&vwm, &event.xmaprequest);
					break;

				case PropertyNotify:
					VWM_TRACE("property notify");
					vwm_xevent_handle_property_notify(&vwm, &event.xproperty);
					break;

				case MappingNotify:
					VWM_TRACE("mapping notify");
					vwm_xevent_handle_mapping_notify(&vwm, &event.xmapping);
					break;

				case Expose:
					VWM_TRACE("expose");
					break;

				case GravityNotify:
					VWM_TRACE("gravitynotify");
					break;

				case ReparentNotify:
					VWM_TRACE("reparentnotify");
					break;

				default:
					if (event.type == randr_event + RRScreenChangeNotify) {
						VWM_TRACE("rrscreenchangenotify");
						if (vwm.xinerama_screens) XFree(vwm.xinerama_screens);
						vwm.xinerama_screens = XineramaQueryScreens(vwm.display, &vwm.xinerama_screens_cnt);

						vwm_composite_invalidate_root(&vwm);
					} else if (event.type == vwm.damage_event + XDamageNotify) {
						//VWM_TRACE("damagenotify");
						vwm_composite_damage_event(&vwm, (XDamageNotifyEvent *)&event);
					} else {
						VWM_ERROR("Unhandled X op %i", event.type);
					}
					break;
			}
		} while (QLength(vwm.display));

		vwm_composite_paint_all(&vwm);
	}

	/* close connection to server */
	XFlush(vwm.display);
	XCloseDisplay(vwm.display);

	return 0;
}
© All Rights Reserved