wmfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * wmfs.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. int
  34. errorhandler(Display *d, XErrorEvent *event)
  35. {
  36. char mess[256];
  37. Client *c;
  38. /* Check if there is another WM running */
  39. if(BadAccess == event->error_code
  40. && ROOT == event->resourceid)
  41. {
  42. fprintf(stderr, "WMFS Error: Another Window Manager is already running.\n");
  43. exit(EXIT_FAILURE);
  44. }
  45. /* Ignore focus change error for unmapped client */
  46. /* Too lazy to add Xproto.h so:
  47. * 42 = X_SetInputFocus
  48. * 28 = X_GrabButton
  49. */
  50. if((c = client_gb_win(event->resourceid)))
  51. if(event->error_code == BadWindow
  52. || event->request_code == 42
  53. || event->request_code == 28)
  54. return 0;
  55. XGetErrorText(d, event->error_code, mess, 128);
  56. fprintf(stderr, "WMFS error: %s(%d) opcodes %d/%d\n resource #%lx\n", mess,
  57. event->error_code,
  58. event->request_code,
  59. event->minor_code,
  60. event->resourceid);
  61. return 1;
  62. }
  63. int
  64. errorhandlerdummy(Display *d, XErrorEvent *event)
  65. {
  66. return 0;
  67. }
  68. /** Clean wmfs before the exit
  69. */
  70. void
  71. quit(void)
  72. {
  73. Client *c;
  74. int i;
  75. /* Set the silent error handler */
  76. XSetErrorHandler(errorhandlerdummy);
  77. /* Unmanage all clients */
  78. for(c = clients; c; c = c->next)
  79. {
  80. client_unhide(c);
  81. XReparentWindow(dpy, c->win, ROOT, c->geo.x, c->geo.y);
  82. }
  83. IFREE(tags);
  84. IFREE(seltag);
  85. XftFontClose(dpy, font);
  86. for(i = 0; i < CurLast; ++i)
  87. XFreeCursor(dpy, cursor[i]);
  88. XFreeGC(dpy, gc_stipple);
  89. infobar_destroy();
  90. IFREE(sgeo);
  91. IFREE(spgeo);
  92. IFREE(infobar);
  93. IFREE(keys);
  94. IFREE(func_list);
  95. IFREE(layout_list);
  96. /* Clean conf alloced thing */
  97. IFREE(menulayout.item);
  98. if(conf.menu)
  99. {
  100. for(i = 0; i < LEN(conf.menu); ++i)
  101. IFREE(conf.menu[i].item);
  102. IFREE(conf.menu);
  103. }
  104. IFREE(conf.launcher);
  105. IFREE(conf.ntag);
  106. IFREE(conf.titlebar.mouse);
  107. for(i = 0; i < conf.titlebar.nbutton; ++i)
  108. {
  109. IFREE(conf.titlebar.button[i].mouse);
  110. IFREE(conf.titlebar.button[i].linecoord);
  111. }
  112. IFREE(conf.bars.mouse);
  113. IFREE(conf.titlebar.button);
  114. IFREE(conf.client.mouse);
  115. IFREE(conf.root.mouse);
  116. XSync(dpy, False);
  117. XCloseDisplay(dpy);
  118. return;
  119. }
  120. /** WMFS main loop.
  121. */
  122. void
  123. mainloop(void)
  124. {
  125. XEvent ev;
  126. while(!exiting && !XNextEvent(dpy, &ev))
  127. getevent(ev);
  128. return;
  129. }
  130. /** Set the exiting variable to True
  131. * for stop the main loop
  132. * \param cmd unused uicb_t
  133. */
  134. void
  135. uicb_quit(uicb_t cmd)
  136. {
  137. exiting = True;
  138. return;
  139. }
  140. /** Scan if there are windows on X
  141. * for manage it
  142. */
  143. void
  144. scan(void)
  145. {
  146. uint i, n;
  147. XWindowAttributes wa;
  148. Window usl, usl2, *w = NULL;
  149. Atom rt;
  150. int rf, tag = -1, screen = -1, free = -1;
  151. ulong ir, il;
  152. uchar *ret;
  153. Client *c;
  154. if(XQueryTree(dpy, ROOT, &usl, &usl2, &w, &n))
  155. for(i = 0; i < n; ++i)
  156. if(XGetWindowAttributes(dpy, w[i], &wa)
  157. && !(wa.override_redirect || XGetTransientForHint(dpy, w[i], &usl))
  158. && wa.map_state == IsViewable)
  159. {
  160. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_TAG"), 0, 32,
  161. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  162. {
  163. tag = *ret;
  164. XFree(ret);
  165. }
  166. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_SCREEN"), 0, 32,
  167. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  168. {
  169. screen = *ret;
  170. XFree(ret);
  171. }
  172. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_ISFREE"), 0, 32,
  173. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  174. {
  175. free = *ret;
  176. XFree(ret);
  177. }
  178. c = client_manage(w[i], &wa, False);
  179. if(tag != -1)
  180. c->tag = tag;
  181. if(screen != -1 && screen <= screen_count() - 1)
  182. c->screen = screen;
  183. if(free != -1)
  184. c->flags |= (free) ? FreeFlag : 0;
  185. client_update_attributes(c);
  186. }
  187. /* Set update layout request */
  188. for(c = clients; c; c = c->next)
  189. {
  190. if(c->tag > conf.ntag[c->screen])
  191. c->tag = conf.ntag[c->screen];
  192. tags[c->screen][c->tag].request_update = True;
  193. }
  194. for(i = 0; i < screen_count(); ++i)
  195. arrange(i, True);
  196. XFree(w);
  197. return;
  198. }
  199. /** Reload the WMFS with the new configuration
  200. * file changement *TESTING*
  201. */
  202. void
  203. uicb_reload(uicb_t cmd)
  204. {
  205. quit();
  206. for(; argv_global[0] && argv_global[0] == ' '; ++argv_global);
  207. execlp(argv_global, argv_global, NULL);
  208. return;
  209. }
  210. /** Check if wmfs is running (for functions that will be
  211. execute when wmfs will be already running).
  212. \return False if wmfs is not running
  213. */
  214. Bool
  215. check_wmfs_running(void)
  216. {
  217. Atom rt;
  218. int rf;
  219. ulong ir, il;
  220. uchar *ret;
  221. XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_RUNNING"), 0L, 4096,
  222. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret);
  223. if(!ret)
  224. {
  225. XFree(ret);
  226. fprintf(stderr, "Wmfs is not running. (_WMFS_RUNNING not present)\n");
  227. return False;
  228. }
  229. XFree(ret);
  230. return True;
  231. }
  232. /** Execute an uicb function
  233. *\param func Function name
  234. *\param cmd Function's command
  235. *\return 0 if there is an error
  236. */
  237. void
  238. exec_uicb_function(char *func, char *cmd)
  239. {
  240. long data[5];
  241. /* Check if wmfs is running (this function is executed when wmfs
  242. is already running normally...) */
  243. if(!check_wmfs_running())
  244. return;
  245. data[4] = True;
  246. XChangeProperty(dpy, ROOT, ATOM("_WMFS_FUNCTION"), ATOM("UTF8_STRING"),
  247. 8, PropModeReplace, (uchar*)func, strlen(func));
  248. if(cmd == NULL)
  249. cmd = "";
  250. XChangeProperty(dpy, ROOT, ATOM("_WMFS_CMD"), ATOM("UTF8_STRING"),
  251. 8, PropModeReplace, (uchar*)cmd, strlen(cmd));
  252. send_client_event(data, "_WMFS_FUNCTION");
  253. return;
  254. }
  255. /** Set statustext
  256. *\param str Statustext string
  257. */
  258. void
  259. set_statustext(char *str)
  260. {
  261. long data[5];
  262. data[4] = True;
  263. XChangeProperty(dpy, ROOT, ATOM("_WMFS_STATUSTEXT"), ATOM("UTF8_STRING"),
  264. 8, PropModeReplace, (uchar*)str, strlen(str));
  265. send_client_event(data, "_WMFS_STATUSTEXT");
  266. return;
  267. }
  268. /** Signal handle function
  269. */
  270. void
  271. signal_handle(int sig)
  272. {
  273. exiting = True;
  274. quit();
  275. exit(EXIT_SUCCESS);
  276. return;
  277. }
  278. /** main function
  279. * \param argc ?
  280. * \param argv ?
  281. * \return 0
  282. */
  283. int
  284. main(int argc, char **argv)
  285. {
  286. int i;
  287. argv_global = _strdup(argv[0]);
  288. sprintf(conf.confpath, "%s/"DEF_CONF, getenv("HOME"));
  289. while((i = getopt(argc, argv, "hvic:s:g:C:V:")) != -1)
  290. {
  291. /* For options who need WMFS running */
  292. if((i == 'c' || i == 's' || i == 'g' || i == 'V')
  293. && !(dpy = XOpenDisplay(NULL)))
  294. {
  295. fprintf(stderr, "WMFS: cannot open X server.\n");
  296. exit(EXIT_FAILURE);
  297. }
  298. switch(i)
  299. {
  300. case 'h':
  301. default:
  302. printf("usage: %s [-ihv] [-C <file>] [-c <uicb function> <cmd> ] [-g <argument>] [-s <string>] [-V <viwmfs cmd]\n"
  303. " -C <file> Load a configuration file\n"
  304. " -c <uicb_function> <cmd> Execute an uicb function to control WMFS\n"
  305. " -g <argument> Show information about wmfs status\n"
  306. " -s <string> Set the bar(s) statustext\n"
  307. " -V <viwmfs cmd> Manage WMFS with vi-like command\n"
  308. " -h Show this page\n"
  309. " -i Show informations\n"
  310. " -v Show WMFS version\n", argv[0]);
  311. exit(EXIT_SUCCESS);
  312. break;
  313. case 'i':
  314. printf("WMFS - Window Manager From Scratch By Martin Duquesnoy\n");
  315. exit(EXIT_SUCCESS);
  316. break;
  317. case 'v':
  318. printf("WMFS version : "WMFS_VERSION".\n"
  319. " Compilation settings :\n"
  320. " - Flags : "WMFS_COMPILE_FLAGS"\n"
  321. " - Linked Libs : "WMFS_LINKED_LIBS"\n"
  322. " - On "WMFS_COMPILE_MACHINE" by "WMFS_COMPILE_BY".\n");
  323. exit(EXIT_SUCCESS);
  324. break;
  325. case 'C':
  326. strcpy(conf.confpath, optarg);
  327. break;
  328. case 'c':
  329. exec_uicb_function(argv[2], ((argv[3]) ? argv[3] : NULL));
  330. XCloseDisplay(dpy);
  331. exit(EXIT_SUCCESS);
  332. break;
  333. case 's':
  334. set_statustext(optarg);
  335. XCloseDisplay(dpy);
  336. exit(EXIT_SUCCESS);
  337. break;
  338. case 'g':
  339. getinfo(optarg);
  340. XCloseDisplay(dpy);
  341. exit(EXIT_SUCCESS);
  342. break;
  343. case 'V':
  344. viwmfs(argc, argv);
  345. XCloseDisplay(dpy);
  346. exit(EXIT_SUCCESS);
  347. break;
  348. }
  349. }
  350. /* Check if WMFS can open X server */
  351. if(!(dpy = XOpenDisplay(NULL)))
  352. {
  353. fprintf(stderr, "WMFS: cannot open X server.\n");
  354. exit(EXIT_FAILURE);
  355. }
  356. /* Set signal handler */
  357. (void)signal(SIGTERM, &signal_handle);
  358. (void)signal(SIGINT, &signal_handle);
  359. /* Check if an other WM is already running; set the error handler */
  360. XSetErrorHandler(errorhandler);
  361. /* Let's Go ! */
  362. init();
  363. scan();
  364. mainloop();
  365. quit();
  366. return 0;
  367. }