dwm-cool-autostart-6.2.diff 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. diff --git a/config.def.h b/config.def.h
  2. index 1c0b587..ed056a4 100644
  3. --- a/config.def.h
  4. +++ b/config.def.h
  5. @@ -18,6 +18,11 @@ static const char *colors[][3] = {
  6. [SchemeSel] = { col_gray4, col_cyan, col_cyan },
  7. };
  8. +static const char *const autostart[] = {
  9. + "st", NULL,
  10. + NULL /* terminate */
  11. +};
  12. +
  13. /* tagging */
  14. static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  15. diff --git a/dwm.c b/dwm.c
  16. index 9fd0286..1facd56 100644
  17. --- a/dwm.c
  18. +++ b/dwm.c
  19. @@ -234,6 +234,7 @@ static int xerror(Display *dpy, XErrorEvent *ee);
  20. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  21. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  22. static void zoom(const Arg *arg);
  23. +static void autostart_exec(void);
  24. /* variables */
  25. static const char broken[] = "broken";
  26. @@ -275,6 +276,34 @@ static Window root, wmcheckwin;
  27. /* compile-time check if all tags fit into an unsigned int bit array. */
  28. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  29. +/* dwm will keep pid's of processes from autostart array and kill them at quit */
  30. +static pid_t *autostart_pids;
  31. +static size_t autostart_len;
  32. +
  33. +/* execute command from autostart array */
  34. +static void
  35. +autostart_exec() {
  36. + const char *const *p;
  37. + size_t i = 0;
  38. +
  39. + /* count entries */
  40. + for (p = autostart; *p; autostart_len++, p++)
  41. + while (*++p);
  42. +
  43. + autostart_pids = malloc(autostart_len * sizeof(pid_t));
  44. + for (p = autostart; *p; i++, p++) {
  45. + if ((autostart_pids[i] = fork()) == 0) {
  46. + setsid();
  47. + execvp(*p, (char *const *)p);
  48. + fprintf(stderr, "dwm: execvp %s\n", *p);
  49. + perror(" failed");
  50. + _exit(EXIT_FAILURE);
  51. + }
  52. + /* skip arguments */
  53. + while (*++p);
  54. + }
  55. +}
  56. +
  57. /* function implementations */
  58. void
  59. applyrules(Client *c)
  60. @@ -1249,6 +1278,16 @@ propertynotify(XEvent *e)
  61. void
  62. quit(const Arg *arg)
  63. {
  64. + size_t i;
  65. +
  66. + /* kill child processes */
  67. + for (i = 0; i < autostart_len; i++) {
  68. + if (0 < autostart_pids[i]) {
  69. + kill(autostart_pids[i], SIGTERM);
  70. + waitpid(autostart_pids[i], NULL, 0);
  71. + }
  72. + }
  73. +
  74. running = 0;
  75. }
  76. @@ -1632,9 +1671,25 @@ showhide(Client *c)
  77. void
  78. sigchld(int unused)
  79. {
  80. + pid_t pid;
  81. +
  82. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  83. die("can't install SIGCHLD handler:");
  84. - while (0 < waitpid(-1, NULL, WNOHANG));
  85. + while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
  86. + pid_t *p, *lim;
  87. +
  88. + if (!(p = autostart_pids))
  89. + continue;
  90. + lim = &p[autostart_len];
  91. +
  92. + for (; p < lim; p++) {
  93. + if (*p == pid) {
  94. + *p = -1;
  95. + break;
  96. + }
  97. + }
  98. +
  99. + }
  100. }
  101. void
  102. @@ -2139,6 +2194,7 @@ main(int argc, char *argv[])
  103. if (!(dpy = XOpenDisplay(NULL)))
  104. die("dwm: cannot open display");
  105. checkotherwm();
  106. + autostart_exec();
  107. setup();
  108. #ifdef __OpenBSD__
  109. if (pledge("stdio rpath proc exec", NULL) == -1)