aboutsummaryrefslogtreecommitdiff
path: root/src/root.c
blob: b110018fe4e858399b2ef52caa6059337b607b40 (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
/***************************************************************************
 * Functions to handle the root menu.
 * Copyright (C) 2004 Joe Wingbermuehle
 ***************************************************************************/

#include "jwm.h"
#include "root.h"
#include "menu.h"
#include "client.h"
#include "main.h"
#include "error.h"
#include "confirm.h"
#include "desktop.h"
#include "misc.h"
#include "winmenu.h"

/* Allow for menus 0 to 9. */
#define ROOT_MENU_COUNT 10

static Menu *rootMenu[ROOT_MENU_COUNT];
static int showExitConfirmation = 1;

static void ExitHandler(ClientNode *np);
static void PatchRootMenu(Menu *menu);
static void UnpatchRootMenu(Menu *menu);

static void RunRootCommand(const MenuAction *action);

/***************************************************************************
 ***************************************************************************/
void InitializeRootMenu() {

	int x;

	for(x = 0; x < ROOT_MENU_COUNT; x++) {
		rootMenu[x] = NULL;
	}

}

/***************************************************************************
 ***************************************************************************/
void StartupRootMenu() {

	int x, y;
	int found;

	for(x = 0; x < ROOT_MENU_COUNT; x++) {
		if(rootMenu[x]) {
			found = 0;
			for(y = 0; y < x; y++) {
				if(rootMenu[y] == rootMenu[x]) {
					found = 1;
					break;
				}
			}
			if(!found) {
				InitializeMenu(rootMenu[x]);
			}
		}
	}

}

/***************************************************************************
 ***************************************************************************/
void ShutdownRootMenu() {
}

/***************************************************************************
 ***************************************************************************/
void DestroyRootMenu() {

	int x, y;

	for(x = 0; x < ROOT_MENU_COUNT; x++) {
		if(rootMenu[x]) {
			DestroyMenu(rootMenu[x]);
			for(y = x + 1; y < ROOT_MENU_COUNT; y++) {
				if(rootMenu[x] == rootMenu[y]) {
					rootMenu[y] = NULL;
				}
			}
			rootMenu[x] = NULL;
		}
	}

}

/***************************************************************************
 ***************************************************************************/
void SetRootMenu(const char *indexes, Menu *m) {

	int x, y;
	int index;
	int found;

	/* Loop over each index to consider. */
	for(x = 0; indexes[x]; x++) {

		/* Get the index and make sure it's in range. */
		index = indexes[x] - '0';
		if(index < 0 || index >= ROOT_MENU_COUNT) {
			Warning("invalid root menu specified: \"%c\"", indexes[x]);
			continue;
		}

		if(rootMenu[index] && rootMenu[index] != m) {

			/* See if replacing this value will cause an orphan. */
			found = 0;
			for(y = 0; y < ROOT_MENU_COUNT; y++) {
				if(x != y && rootMenu[y] == rootMenu[x]) {
					found = 1;
					break;
				}
			}

			/* If we have an orphan, destroy it. */
			if(!found) {
				DestroyMenu(rootMenu[index]);
			}

		}

		rootMenu[index] = m;

	}

}

/***************************************************************************
 ***************************************************************************/
void SetShowExitConfirmation(int v) {
	showExitConfirmation = v;
}

/***************************************************************************
 ***************************************************************************/
int IsRootMenuDefined(int index) {
	if(index >= 0 && index < ROOT_MENU_COUNT && rootMenu[index]) {
		return 1;
	} else {
		return 0;
	}
}

/***************************************************************************
 ***************************************************************************/
void GetRootMenuSize(int index, int *width, int *height) {

	if(!rootMenu[index]) {
		*width = 0;
		*height = 0;
		return;
	}

	PatchRootMenu(rootMenu[index]);
	*width = rootMenu[index]->width;
	*height = rootMenu[index]->height;
	UnpatchRootMenu(rootMenu[index]);

}

/***************************************************************************
 ***************************************************************************/
int ShowRootMenu(int index, int x, int y) {

	if(!rootMenu[index]) {
		return 0;
	}

	PatchRootMenu(rootMenu[index]);
	ShowMenu(rootMenu[index], RunRootCommand, x, y);
	UnpatchRootMenu(rootMenu[index]);

	return 1;

}

/***************************************************************************
 ***************************************************************************/
void PatchRootMenu(Menu *menu) {

	MenuItem *item;

	for(item = menu->items; item; item = item->next) {
		if(item->submenu) {
			PatchRootMenu(item->submenu);
		}
		if(item->action.type == MA_DESKTOP) {
			item->submenu = CreateDesktopMenu(1 << currentDesktop);
			InitializeMenu(item->submenu);
		}
	}

}

/***************************************************************************
 ***************************************************************************/
void UnpatchRootMenu(Menu *menu) {

	MenuItem *item;

	for(item = menu->items; item; item = item->next) {
		if(item->action.type == MA_DESKTOP) {
			DestroyMenu(item->submenu);
			item->submenu = NULL;
		} else if(item->submenu) {
			UnpatchRootMenu(item->submenu);
		}
	}

}

/***************************************************************************
 ***************************************************************************/
void ExitHandler(ClientNode *np) {
	shouldExit = 1;
}

/***************************************************************************
 ***************************************************************************/
void Restart() {
	shouldRestart = 1;
	shouldExit = 1;
}

/***************************************************************************
 ***************************************************************************/
void Exit() {
	if(showExitConfirmation) {
		ShowConfirmDialog(NULL, ExitHandler,
			"Exit JWM",
			"Are you sure?",
			NULL);
	} else {
		ExitHandler(NULL);
	}
}

/***************************************************************************
 ***************************************************************************/
void RunRootCommand(const MenuAction *action) {

	switch(action->type) {

	case MA_EXECUTE:
		RunCommand(action->data.str);
		break;
	case MA_RESTART:
		Restart();
		break;
	case MA_EXIT:
		if(exitCommand) {
			Release(exitCommand);
		}
		exitCommand = CopyString(action->data.str);
		Exit();
		break;
	case MA_DESKTOP:
		ChangeDesktop(action->data.i);
		break;

	case MA_SENDTO:
	case MA_LAYER:
	case MA_MAXIMIZE:
	case MA_MINIMIZE:
	case MA_RESTORE:
	case MA_SHADE:
	case MA_MOVE:
	case MA_RESIZE:
	case MA_KILL:
	case MA_CLOSE:
		ChooseWindow(action);
		break;

	default:
		Debug("invalid RunRootCommand action: %d", action->type);
		break;
	}

}

/***************************************************************************
 ***************************************************************************/
void RunCommand(const char *command) {
	char *displayString;
	char *str;

	if(!command) {
		return;
	}

	displayString = DisplayString(display);

	if(!fork()) {
		if(!fork()) {
			close(ConnectionNumber(display));
			if(displayString && displayString[0]) {
				str = malloc(strlen(displayString) + 9);
				sprintf(str, "DISPLAY=%s", displayString);
				putenv(str);
			}
			execl(SHELL_NAME, SHELL_NAME, "-c", command, NULL);
			Warning("exec failed: (%s) %s", SHELL_NAME, command);
			exit(1);
		}
		exit(0);
	}

	wait(NULL);

}