wmfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. errx(EXIT_FAILURE, "Another Window Manager is already running.");
  42. /* Ignore focus change error for unmapped client */
  43. /* Too lazy to add Xproto.h so:
  44. * 42 = X_SetInputFocus
  45. * 28 = X_GrabButton
  46. */
  47. if((c = client_gb_win(event->resourceid)))
  48. if(event->error_code == BadWindow
  49. || event->request_code == 42
  50. || event->request_code == 28)
  51. return 0;
  52. XGetErrorText(d, event->error_code, mess, 128);
  53. warnx("%s(%d) opcodes %d/%d\n resource #%lx\n", mess,
  54. event->error_code,
  55. event->request_code,
  56. event->minor_code,
  57. event->resourceid);
  58. return 1;
  59. }
  60. int
  61. errorhandlerdummy(Display *d, XErrorEvent *event)
  62. {
  63. return 0;
  64. }
  65. /** Clean wmfs before the exit
  66. */
  67. void
  68. quit(void)
  69. {
  70. Client *c;
  71. int i;
  72. /* Set the silent error handler */
  73. XSetErrorHandler(errorhandlerdummy);
  74. /* Unmanage all clients */
  75. for(c = clients; c; c = c->next)
  76. {
  77. client_unhide(c);
  78. XReparentWindow(dpy, c->win, ROOT, c->geo.x, c->geo.y);
  79. }
  80. IFREE(tags);
  81. IFREE(seltag);
  82. XftFontClose(dpy, font);
  83. for(i = 0; i < CurLast; ++i)
  84. XFreeCursor(dpy, cursor[i]);
  85. XFreeGC(dpy, gc_stipple);
  86. infobar_destroy();
  87. IFREE(sgeo);
  88. IFREE(spgeo);
  89. IFREE(infobar);
  90. IFREE(keys);
  91. IFREE(func_list);
  92. IFREE(layout_list);
  93. IFREE(net_atom);
  94. /* Clean conf alloced thing */
  95. IFREE(menulayout.item);
  96. if(conf.menu)
  97. {
  98. for(i = 0; i < LEN(conf.menu); ++i)
  99. IFREE(conf.menu[i].item);
  100. IFREE(conf.menu);
  101. }
  102. IFREE(conf.launcher);
  103. IFREE(conf.ntag);
  104. IFREE(conf.titlebar.mouse);
  105. for(i = 0; i < conf.titlebar.nbutton; ++i)
  106. {
  107. IFREE(conf.titlebar.button[i].mouse);
  108. IFREE(conf.titlebar.button[i].linecoord);
  109. }
  110. IFREE(conf.bars.mouse);
  111. IFREE(conf.titlebar.button);
  112. IFREE(conf.client.mouse);
  113. IFREE(conf.root.mouse);
  114. XSync(dpy, False);
  115. XCloseDisplay(dpy);
  116. return;
  117. }
  118. void *
  119. thread_process(void *arg)
  120. {
  121. XEvent ev;
  122. /* X event loop */
  123. if(!(int*)arg)
  124. {
  125. while(!exiting && !XNextEvent(dpy, &ev))
  126. getevent(ev);
  127. pthread_exit(0);
  128. }
  129. /* Status checking loop with timing */
  130. else
  131. {
  132. while(!exiting)
  133. {
  134. spawn(status_path);
  135. sleep(conf.status_timing);
  136. }
  137. pthread_exit(0);
  138. }
  139. }
  140. /** WMFS main loop.
  141. */
  142. void
  143. mainloop(void)
  144. {
  145. XEvent ev;
  146. pthread_t evloop, evstatus;
  147. void *ret;
  148. if(!estatus)
  149. while(!exiting && !XNextEvent(dpy, &ev))
  150. getevent(ev);
  151. else
  152. {
  153. pthread_create(&evloop, NULL, thread_process, "1");
  154. pthread_create(&evstatus, NULL, thread_process, NULL);
  155. (void)pthread_join(evloop, &ret);
  156. (void)pthread_join(evstatus, &ret);
  157. }
  158. return;
  159. }
  160. /** Set the exiting variable to True
  161. * for stop the main loop
  162. * \param cmd unused uicb_t
  163. */
  164. void
  165. uicb_quit(uicb_t cmd)
  166. {
  167. exiting = True;
  168. return;
  169. }
  170. /** Scan if there are windows on X
  171. * for manage it
  172. */
  173. void
  174. scan(void)
  175. {
  176. uint i, n;
  177. XWindowAttributes wa;
  178. Window usl, usl2, *w = NULL;
  179. Atom rt;
  180. int rf, tag = -1, screen = -1, free = -1;
  181. ulong ir, il;
  182. uchar *ret;
  183. Client *c;
  184. if(XQueryTree(dpy, ROOT, &usl, &usl2, &w, &n))
  185. for(i = n - 1; i != -1; --i)
  186. if(XGetWindowAttributes(dpy, w[i], &wa)
  187. && !(wa.override_redirect || XGetTransientForHint(dpy, w[i], &usl))
  188. && wa.map_state == IsViewable)
  189. {
  190. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_TAG"), 0, 32,
  191. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  192. {
  193. tag = *ret;
  194. XFree(ret);
  195. }
  196. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_SCREEN"), 0, 32,
  197. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  198. {
  199. screen = *ret;
  200. XFree(ret);
  201. }
  202. if(XGetWindowProperty(dpy, w[i], ATOM("_WMFS_ISFREE"), 0, 32,
  203. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret) == Success && ret)
  204. {
  205. free = *ret;
  206. XFree(ret);
  207. }
  208. c = client_manage(w[i], &wa, False);
  209. if(tag != -1)
  210. c->tag = tag;
  211. if(screen != -1 && screen <= screen_count() - 1)
  212. c->screen = screen;
  213. if(free != -1)
  214. c->flags |= (free) ? FreeFlag : 0;
  215. client_update_attributes(c);
  216. }
  217. /* Set update layout request */
  218. for(c = clients; c; c = c->next)
  219. {
  220. if(c->tag > conf.ntag[c->screen])
  221. c->tag = conf.ntag[c->screen];
  222. tags[c->screen][c->tag].request_update = True;
  223. }
  224. for(i = 0; i < screen_count(); ++i)
  225. arrange(i, True);
  226. XFree(w);
  227. return;
  228. }
  229. /** Reload the WMFS with the new configuration
  230. * file changement *TESTING*
  231. */
  232. void
  233. uicb_reload(uicb_t cmd)
  234. {
  235. quit();
  236. for(; argv_global[0] && argv_global[0] == ' '; ++argv_global);
  237. execlp(argv_global, argv_global, NULL);
  238. return;
  239. }
  240. /** Check if wmfs is running (for functions that will be
  241. execute when wmfs will be already running).
  242. \return False if wmfs is not running
  243. */
  244. Bool
  245. check_wmfs_running(void)
  246. {
  247. Atom rt;
  248. int rf;
  249. ulong ir, il;
  250. uchar *ret;
  251. XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_RUNNING"), 0L, 4096,
  252. False, XA_CARDINAL, &rt, &rf, &ir, &il, &ret);
  253. if(!ret)
  254. {
  255. XFree(ret);
  256. warnx("Wmfs is not running. (_WMFS_RUNNING not present)");
  257. return False;
  258. }
  259. XFree(ret);
  260. return True;
  261. }
  262. /** Execute an uicb function
  263. *\param func Function name
  264. *\param cmd Function's command
  265. *\return 0 if there is an error
  266. */
  267. void
  268. exec_uicb_function(char *func, char *cmd)
  269. {
  270. long data[5];
  271. /* Check if wmfs is running (this function is executed when wmfs
  272. is already running normally...) */
  273. if(!check_wmfs_running())
  274. return;
  275. data[4] = True;
  276. XChangeProperty(dpy, ROOT, ATOM("_WMFS_FUNCTION"), ATOM("UTF8_STRING"),
  277. 8, PropModeReplace, (uchar*)func, strlen(func));
  278. if(cmd == NULL)
  279. cmd = "";
  280. XChangeProperty(dpy, ROOT, ATOM("_WMFS_CMD"), ATOM("UTF8_STRING"),
  281. 8, PropModeReplace, (uchar*)cmd, strlen(cmd));
  282. send_client_event(data, "_WMFS_FUNCTION");
  283. return;
  284. }
  285. /** Set statustext
  286. *\param str Statustext string
  287. */
  288. void
  289. set_statustext(int s, char *str)
  290. {
  291. int i;
  292. long data[5];
  293. char atom_name[64];
  294. data[4] = True;
  295. if(!str)
  296. return;
  297. if(s == -1)
  298. {
  299. for(i = 0; i < screen_count(); ++i)
  300. {
  301. sprintf(atom_name, "_WMFS_STATUSTEXT_%d", i);
  302. XChangeProperty(dpy, ROOT, ATOM(atom_name), ATOM("UTF8_STRING"),
  303. 8, PropModeReplace, (uchar*)str, strlen(str));
  304. send_client_event(data, atom_name);
  305. }
  306. }
  307. else
  308. {
  309. sprintf(atom_name, "_WMFS_STATUSTEXT_%d", s);
  310. XChangeProperty(dpy, ROOT, ATOM(atom_name), ATOM("UTF8_STRING"),
  311. 8, PropModeReplace, (uchar*)str, strlen(str));
  312. send_client_event(data, atom_name);
  313. }
  314. return;
  315. }
  316. /** Update status script by ewmh hint
  317. */
  318. void
  319. update_status(void)
  320. {
  321. long data[5];
  322. if(!check_wmfs_running())
  323. return;
  324. data[4] = True;
  325. send_client_event(data, "_WMFS_UPDATE_STATUS");
  326. return;
  327. }
  328. /** Signal handle function
  329. */
  330. void
  331. signal_handle(int sig)
  332. {
  333. exiting = True;
  334. quit();
  335. exit(EXIT_SUCCESS);
  336. return;
  337. }
  338. /** main function
  339. * \param argc ?
  340. * \param argv ?
  341. * \return 0
  342. */
  343. int
  344. main(int argc, char **argv)
  345. {
  346. int i;
  347. char *ol = "csgVS";
  348. argv_global = _strdup(argv[0]);
  349. sprintf(conf.confpath, "%s/"DEF_CONF, getenv("HOME"));
  350. while((i = getopt(argc, argv, "hviSc:s:g:C:V:")) != -1)
  351. {
  352. /* For options who need WMFS running */
  353. if(strchr(ol, i) && !(dpy = XOpenDisplay(NULL)))
  354. errx(EXIT_FAILURE, "cannot open X server.");
  355. switch(i)
  356. {
  357. case 'h':
  358. default:
  359. printf("usage: %s [-ihvS] [-C <file>] [-c <uicb function> <cmd> ] [-g <argument>] [-s <screen_num> <string>] [-V <viwmfs cmd]\n"
  360. " -C <file> Load a configuration file\n"
  361. " -c <uicb_function> <cmd> Execute an uicb function to control WMFS\n"
  362. " -g <argument> Show information about wmfs status\n"
  363. " -s <screen_num> <string> Set the bar(s) statustext\n"
  364. " -V <viwmfs cmd> Manage WMFS with vi-like command\n"
  365. " -S Update status script\n"
  366. " -h Show this page\n"
  367. " -i Show informations\n"
  368. " -v Show WMFS version\n", argv[0]);
  369. exit(EXIT_SUCCESS);
  370. break;
  371. case 'i':
  372. printf("WMFS - Window Manager From Scratch By Martin Duquesnoy\n");
  373. exit(EXIT_SUCCESS);
  374. break;
  375. case 'v':
  376. printf("WMFS version : "WMFS_VERSION"\n"
  377. " Compilation settings :\n"
  378. " - Flags : "WMFS_COMPILE_FLAGS"\n"
  379. " - Linked Libs : "WMFS_LINKED_LIBS"\n"
  380. " - On "WMFS_COMPILE_MACHINE" by "WMFS_COMPILE_BY".\n");
  381. exit(EXIT_SUCCESS);
  382. break;
  383. case 'S':
  384. update_status();
  385. XCloseDisplay(dpy);
  386. exit(EXIT_SUCCESS);
  387. break;
  388. case 'C':
  389. strcpy(conf.confpath, optarg);
  390. break;
  391. case 'c':
  392. exec_uicb_function(argv[2], ((argv[3]) ? argv[3] : NULL));
  393. XCloseDisplay(dpy);
  394. exit(EXIT_SUCCESS);
  395. break;
  396. case 's':
  397. if(argc > 3)
  398. set_statustext(atoi(optarg), argv[3]);
  399. else
  400. set_statustext(-1, optarg);
  401. XCloseDisplay(dpy);
  402. exit(EXIT_SUCCESS);
  403. break;
  404. case 'g':
  405. getinfo(optarg);
  406. XCloseDisplay(dpy);
  407. exit(EXIT_SUCCESS);
  408. break;
  409. case 'V':
  410. viwmfs(argc, argv);
  411. XCloseDisplay(dpy);
  412. exit(EXIT_SUCCESS);
  413. break;
  414. }
  415. }
  416. /* Check if WMFS can open X server */
  417. if(!(dpy = XOpenDisplay(NULL)))
  418. errx(EXIT_FAILURE, "cannot open X server.");
  419. /* Set signal handler */
  420. (void)signal(SIGTERM, &signal_handle);
  421. (void)signal(SIGINT, &signal_handle);
  422. /* Check if an other WM is already running; set the error handler */
  423. XSetErrorHandler(errorhandler);
  424. /* Let's Go ! */
  425. init();
  426. scan();
  427. mainloop();
  428. quit();
  429. return 0;
  430. }