JWM Source Documentation
root.c
Go to the documentation of this file.
1 
10 #include "jwm.h"
11 #include "root.h"
12 #include "menu.h"
13 #include "client.h"
14 #include "error.h"
15 #include "confirm.h"
16 #include "misc.h"
17 #include "winmenu.h"
18 #include "command.h"
19 #include "parse.h"
20 #include "settings.h"
21 #include "desktop.h"
22 
24 #define ROOT_MENU_COUNT 36
25 
27 
28 static void ExitHandler(ClientNode *np);
29 
30 static void RunRootCommand(MenuAction *action, unsigned button);
31 
34 {
35  unsigned int x;
36  for(x = 0; x < ROOT_MENU_COUNT; x++) {
37  rootMenu[x] = NULL;
38  }
39 }
40 
42 void StartupRootMenu(void)
43 {
44 
45  unsigned int x, y;
46  char found;
47 
48  for(x = 0; x < ROOT_MENU_COUNT; x++) {
49  if(rootMenu[x]) {
50  found = 0;
51  for(y = 0; y < x; y++) {
52  if(rootMenu[y] == rootMenu[x]) {
53  found = 1;
54  break;
55  }
56  }
57  if(!found) {
58  InitializeMenu(rootMenu[x]);
59  }
60  }
61  }
62 
63 }
64 
66 void DestroyRootMenu(void)
67 {
68 
69  unsigned int x, y;
70 
71  for(x = 0; x < ROOT_MENU_COUNT; x++) {
72  if(rootMenu[x]) {
73  DestroyMenu(rootMenu[x]);
74  for(y = x + 1; y < ROOT_MENU_COUNT; y++) {
75  if(rootMenu[x] == rootMenu[y]) {
76  rootMenu[y] = NULL;
77  }
78  }
79  rootMenu[x] = NULL;
80  }
81  }
82 
83 }
84 
86 int GetRootMenuIndex(char ch)
87 {
88  if(ch >= '0' && ch <= '9') {
89  return ch - '0';
90  } else if(ch >= 'A' && ch <= 'Z') {
91  return ch - 'A' + 10;
92  } else if(ch >= 'a' && ch <= 'z') {
93  return ch - 'a' + 10;
94  } else {
95  return -1;
96  }
97 }
98 
100 int GetRootMenuIndexFromString(const char *str)
101 {
102  unsigned int temp = 0;
103  while(*str && IsSpace(*str, &temp)) {
104  str += 1;
105  }
106  if(JUNLIKELY(!*str)) {
107  return -1;
108  }
109  const int result = GetRootMenuIndex(*str);
110  str += 1;
111  while(*str && IsSpace(*str, &temp)) {
112  str += 1;
113  }
114  return *str ? -1 : result;
115 }
116 
118 void SetRootMenu(const char *indexes, Menu *m)
119 {
120 
121  unsigned int x, y;
122  int index;
123  char found;
124 
125  /* Loop over each index to consider. */
126  for(x = 0; indexes[x]; x++) {
127 
128  /* Get the index and make sure it's in range. */
129  index = GetRootMenuIndex(indexes[x]);
130  if(JUNLIKELY(index < 0)) {
131  Warning(_("invalid root menu specified: \"%c\""), indexes[x]);
132  continue;
133  }
134 
135  if(rootMenu[index] && rootMenu[index] != m) {
136 
137  /* See if replacing this value will cause an orphan. */
138  found = 0;
139  for(y = 0; y < ROOT_MENU_COUNT; y++) {
140  if(x != y && rootMenu[y] == rootMenu[x]) {
141  found = 1;
142  break;
143  }
144  }
145 
146  /* If we have an orphan, destroy it. */
147  if(!found) {
148  DestroyMenu(rootMenu[index]);
149  }
150 
151  }
152 
153  rootMenu[index] = m;
154 
155  }
156 
157 }
158 
160 char IsRootMenuDefined(int index)
161 {
162  if(index >= 0 && index < ROOT_MENU_COUNT && rootMenu[index]) {
163  return 1;
164  } else {
165  return 0;
166  }
167 }
168 
170 void GetRootMenuSize(int index, int *width, int *height)
171 {
172 
173  if(!rootMenu[index]) {
174  *width = 0;
175  *height = 0;
176  return;
177  }
178  *width = rootMenu[index]->width;
179  *height = rootMenu[index]->height;
180 
181 }
182 
184 char ShowRootMenu(int index, int x, int y, char keyboard)
185 {
186  if(!rootMenu[index]) {
187  return 0;
188  }
189  if(menuShown) {
190  return 0;
191  }
192  if (rootMenu[index]->dynamic) {
193  Menu *menu = ParseDynamicMenu(rootMenu[index]->dynamic);
194  if(menu) {
195  char ret;
196  InitializeMenu(menu);
197  ret = ShowMenu(menu, RunRootCommand, x, y, keyboard);
198  DestroyMenu(menu);
199  return ret;
200  }
201  }
202  return ShowMenu(rootMenu[index], RunRootCommand, x, y, keyboard);
203 }
204 
207 {
208  shouldExit = 1;
209 }
210 
212 void Restart(void)
213 {
214  shouldRestart = 1;
215  shouldExit = 1;
216 }
217 
219 void Exit(char confirm)
220 {
221  if(confirm) {
223  _("Exit JWM"),
224  _("Are you sure?"),
225  NULL);
226  } else {
227  ExitHandler(NULL);
228  }
229 }
230 
232 void ReloadMenu(void)
233 {
234  shouldReload = 1;
235  if(!menuShown) {
237  DestroyRootMenu();
240  StartupRootMenu();
241  shouldReload = 0;
242  }
243 }
244 
246 void RunRootCommand(MenuAction *action, unsigned button)
247 {
248 
249  switch(action->type) {
250  case MA_EXECUTE:
251  RunCommand(action->str);
252  break;
253  case MA_RESTART:
254  Restart();
255  break;
256  case MA_EXIT:
257  if(exitCommand) {
259  }
260  exitCommand = CopyString(action->str);
261  Exit(action->value);
262  break;
263  case MA_DESKTOP:
264  ChangeDesktop(action->value);
265  break;
266 
267  case MA_SENDTO:
268  case MA_LAYER:
269  case MA_MAXIMIZE:
270  case MA_MINIMIZE:
271  case MA_RESTORE:
272  case MA_SHADE:
273  case MA_MOVE:
274  case MA_RESIZE:
275  case MA_KILL:
276  case MA_CLOSE:
277  ChooseWindow(action);
278  break;
279 
280  default:
281  break;
282  }
283 
284 }
285 

joewing.net / Projects / JWM