aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: ed3a9012cad3eec12155918fd00cb9aea088927b (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/****************************************************************************
 * The main entry point and related JWM functions.
 * Copyright (C) 2004 Joe Wingbermuehle
 ****************************************************************************/

#include "jwm.h"
#include "main.h"
#include "lex.h"
#include "parse.h"
#include "help.h"
#include "error.h"
#include "event.h"

#include "border.h"
#include "client.h"
#include "color.h"
#include "command.h"
#include "cursor.h"
#include "confirm.h"
#include "font.h"
#include "hint.h"
#include "group.h"
#include "key.h"
#include "icon.h"
#include "outline.h"
#include "taskbar.h"
#include "tray.h"
#include "traybutton.h"
#include "popup.h"
#include "pager.h"
#include "swallow.h"
#include "screen.h"
#include "root.h"
#include "desktop.h"
#include "place.h"
#include "clock.h"
#include "dock.h"
#include "theme.h"
#include "misc.h"

Display *display = NULL;
Window rootWindow;
int rootWidth, rootHeight;
int rootDepth;
int rootScreen;
Colormap rootColormap;
Visual *rootVisual;
GC rootGC;
int colormapCount;

int shouldExit = 0;
int shouldRestart = 0;
int isRestarting = 0;
int initializing = 0;

unsigned int desktopCount = 4;
unsigned int currentDesktop = 0;

char *exitCommand = NULL;

int borderWidth = DEFAULT_BORDER_WIDTH;
int titleHeight = DEFAULT_TITLE_HEIGHT;

unsigned int doubleClickSpeed;
unsigned int doubleClickDelta;

FocusModelType focusModel = FOCUS_SLOPPY;

XContext clientContext;
XContext frameContext;

#ifdef USE_SHAPE
int haveShape;
int shapeEvent;
#endif


static const char *CONFIG_FILE = "/.jwmrc";

static void Initialize();
static void Startup();
static void Shutdown();
static void Destroy();

static void OpenConnection();
static void CloseConnection();
static void StartupConnection();
static void ShutdownConnection();
static void EventLoop();
static void HandleExit();
static void DoExit(int code);
static void SendRestart();
static void SendExit();

static char *configPath = NULL;
static char *displayString = NULL;

/****************************************************************************
 ****************************************************************************/
int main(int argc, char *argv[]) {
	char *temp;
	int x;

	StartDebug();

	temp = getenv("HOME");
	if(temp) {
		configPath = Allocate(strlen(temp) + strlen(CONFIG_FILE) + 1);
		strcpy(configPath, temp);
		strcat(configPath, CONFIG_FILE);
	} else {
		configPath = CopyString(CONFIG_FILE);
	}

	for(x = 1; x < argc; x++) {
		if(!strcmp(argv[x], "-v")) {
			DisplayAbout();
			DoExit(0);
		} else if(!strcmp(argv[x], "-h")) {
			DisplayHelp();
			DoExit(0);
		} else if(!strcmp(argv[x], "-p")) {
			Initialize();
			ParseConfig(configPath);
			DoExit(0);
		} else if(!strcmp(argv[x], "-restart")) {
			SendRestart();
			DoExit(0);
		} else if(!strcmp(argv[x], "-exit")) {
			SendExit();
			DoExit(0);
		} else if(!strcmp(argv[x], "-display") && x + 1 < argc) {
			displayString = argv[++x];
		} else {
			DisplayUsage();
			DoExit(1);
		}
	}

	StartupConnection();
	do {

		isRestarting = shouldRestart;
		shouldExit = 0;
		shouldRestart = 0;

		Initialize();

		ParseConfig(configPath);

		Startup();

		EventLoop();

		Shutdown();

		Destroy();

	} while(shouldRestart);
	ShutdownConnection();

	if(exitCommand) {
		execl(SHELL_NAME, SHELL_NAME, "-c", exitCommand, NULL);
		Warning("exec failed: (%s) %s", SHELL_NAME, exitCommand);
		DoExit(1);
	} else {
		DoExit(0);
	}

	/* Control shoud never get here. */
	return -1;

}

/****************************************************************************
 ****************************************************************************/
void
DoExit(int code)
{
	Destroy();

	if(configPath) {
		Release(configPath);
		configPath = NULL;
	}
	if(exitCommand) {
		Release(exitCommand);
		exitCommand = NULL;
	}

	StopDebug();
	exit(code);
}

/****************************************************************************
 ****************************************************************************/
void
EventLoop()
{
	XEvent event;

	while(!shouldExit) {
		WaitForEvent(&event);
		ProcessEvent(&event);
	}
}

/****************************************************************************
 ****************************************************************************/
void
OpenConnection()
{

	display = JXOpenDisplay(displayString);
	if(!display) {
		if(displayString) {
			printf("error: could not open display %s\n", displayString);
		} else {
			printf("error: could not open display\n");
		}
		DoExit(1);
	}

	rootScreen = DefaultScreen(display);
	rootWindow = RootWindow(display, rootScreen);
	rootWidth = DisplayWidth(display, rootScreen);
	rootHeight = DisplayHeight(display, rootScreen);
	rootDepth = DefaultDepth(display, rootScreen);
	rootColormap = DefaultColormap(display, rootScreen);
	rootVisual = DefaultVisual(display, rootScreen);
	rootGC = DefaultGC(display, rootScreen);
	colormapCount = MaxCmapsOfScreen(ScreenOfDisplay(display, rootScreen));

	XSetGraphicsExposures(display, rootGC, False);

}

/****************************************************************************
 ****************************************************************************/
void
StartupConnection()
{
	XSetWindowAttributes attr;
	int temp;

	initializing = 1;
	OpenConnection();

#if 0
	XSynchronize(display, True);
#endif

	JXSetErrorHandler(ErrorHandler);

	clientContext = XUniqueContext();
	frameContext = XUniqueContext();

	attr.event_mask
		= SubstructureRedirectMask
		| SubstructureNotifyMask
		| PropertyChangeMask
		| ColormapChangeMask
		| ButtonPressMask
		| ButtonReleaseMask
		| PointerMotionMask | PointerMotionHintMask;
	JXChangeWindowAttributes(display, rootWindow, CWEventMask, &attr);

	signal(SIGTERM, HandleExit);
	signal(SIGINT, HandleExit);
	signal(SIGHUP, HandleExit);

#ifdef USE_SHAPE
	haveShape = JXShapeQueryExtension(display, &shapeEvent, &temp);
	if (haveShape) {
		Debug("shape extension enabled");
	} else {
		Debug("shape extension disabled");
	}
#endif

	initializing = 0;

}

/****************************************************************************
 ****************************************************************************/
void
CloseConnection()
{
	JXFlush(display);
	JXCloseDisplay(display);
}

/****************************************************************************
 ****************************************************************************/
void
ShutdownConnection()
{
	CloseConnection();
}

/****************************************************************************
 ****************************************************************************/
void
HandleExit()
{
	signal(SIGTERM, HandleExit);
	signal(SIGINT, HandleExit);
	signal(SIGHUP, HandleExit);
	shouldExit = 1;
}

/****************************************************************************
 * This is called before the X connection is opened.
 ****************************************************************************/
void
Initialize()
{
	InitializeBorders();
	InitializeClients();
	InitializeClock();
	InitializeColors();
	InitializeCommands();
	InitializeCursors();
	InitializeDesktops();
	#ifndef DISABLE_CONFIRM
		InitializeDialogs();
	#endif
	InitializeDock();
	InitializeFonts();
	InitializeGroups();
	InitializeHints();
	InitializeIcons();
	InitializeKeys();
	InitializeOutline();
	InitializePager();
	InitializePlacement();
	InitializePopup();
	InitializeRootMenu();
	InitializeScreens();
	InitializeSwallow();
	InitializeTaskBar();
	InitializeThemes();
	InitializeTray();
	InitializeTrayButtons();
}

/****************************************************************************
 * This is called after the X connection is opened.
 ****************************************************************************/
void
Startup()
{

	/* This order is important. */

	StartupCommands();

	/* First we grab the server to prevent clients from changing things
	 * while we're still loading. */
	JXGrabServer(display);

	StartupScreens();

	StartupGroups();
	StartupColors();
	StartupIcons();
	StartupFonts();
	StartupCursors();
	StartupOutline();

	StartupThemes();

	StartupPager();
	StartupClock();
	StartupTaskBar();
	StartupTrayButtons();
	StartupDock();
	StartupTray();
	StartupKeys();
	StartupDesktops();
	StartupHints();
	StartupBorders();
	StartupPlacement();
	StartupClients();

	#ifndef DISABLE_CONFIRM
		StartupDialogs();
	#endif
	StartupPopup();

	StartupRootMenu();

	SetDefaultCursor(rootWindow);
	ReadCurrentDesktop();
	JXFlush(display);

	RestackClients();

	/* Allow clients to do their thing. */
	JXSync(display, True);
	JXUngrabServer(display);

	StartupSwallow();

	/* Send expose events. */
	ExposeCurrentDesktop();

}

/****************************************************************************
 * This is called before the X connection is closed.
 ****************************************************************************/
void
Shutdown()
{

	/* This order is important. */

	ShutdownSwallow();

	ShutdownOutline();
	#ifndef DISABLE_CONFIRM
		ShutdownDialogs();
	#endif
	ShutdownPopup();
	ShutdownKeys();
	ShutdownPager();
	ShutdownRootMenu();
	ShutdownDock();
	ShutdownTray();
	ShutdownTrayButtons();
	ShutdownTaskBar();
	ShutdownClock();
	ShutdownBorders();
	ShutdownClients();
	ShutdownThemes();
	ShutdownIcons();
	ShutdownCursors();
	ShutdownFonts();
	ShutdownColors();
	ShutdownGroups();
	ShutdownDesktops();

	ShutdownPlacement();
	ShutdownHints();
	ShutdownScreens();

	ShutdownCommands();

}

/****************************************************************************
 * This is called after the X connection is closed.
 * Note that it is possible for this to be called more than once.
 ****************************************************************************/
void
Destroy()
{
	DestroyBorders();
	DestroyClients();
	DestroyClock();
	DestroyColors();
	DestroyCommands();
	DestroyCursors();
	DestroyDesktops();
	#ifndef DISABLE_CONFIRM
		DestroyDialogs();
	#endif
	DestroyDock();
	DestroyFonts();
	DestroyGroups();
	DestroyHints();
	DestroyIcons();
	DestroyKeys();
	DestroyOutline();
	DestroyPager();
	DestroyPlacement();
	DestroyPopup();
	DestroyRootMenu();
	DestroyScreens();
	DestroySwallow();
	DestroyTaskBar();
	DestroyThemes();
	DestroyTray();
	DestroyTrayButtons();
}

/****************************************************************************
 * Send _JWM_RESTART to the root window.
 ****************************************************************************/
void
SendRestart()
{

	XEvent event;

	OpenConnection();

	memset(&event, 0, sizeof(event));
	event.xclient.type = ClientMessage;
	event.xclient.window = rootWindow;
	event.xclient.message_type = JXInternAtom(display, "_JWM_RESTART", False);
	event.xclient.format = 32;

	JXSendEvent(display, rootWindow, False, SubstructureRedirectMask, &event);

	CloseConnection();

}

/****************************************************************************
 * Send _JWM_EXIT to the root window.
 ****************************************************************************/
void
SendExit()
{

	XEvent event;

	OpenConnection();

	memset(&event, 0, sizeof(event));
	event.xclient.type = ClientMessage;
	event.xclient.window = rootWindow;
	event.xclient.message_type = JXInternAtom(display, "_JWM_EXIT", False);
	event.xclient.format = 32;

	JXSendEvent(display, rootWindow, False, SubstructureRedirectMask, &event);

	CloseConnection();
}