swallow.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <X11/Xlib-xcb.h>
  2. #include <xcb/res.h>
  3. #ifdef __OpenBSD__
  4. #include <sys/sysctl.h>
  5. #include <kvm.h>
  6. #endif /* __OpenBSD__ */
  7. static int scanner;
  8. static xcb_connection_t *xcon;
  9. int
  10. swallow(Client *p, Client *c)
  11. {
  12. Client *s;
  13. XWindowChanges wc;
  14. if (c->noswallow > 0 || c->isterminal)
  15. return 0;
  16. if (c->noswallow < 0 && !swallowfloating && c->isfloating)
  17. return 0;
  18. XMapWindow(dpy, c->win);
  19. detach(c);
  20. detachstack(c);
  21. setclientstate(c, WithdrawnState);
  22. XUnmapWindow(dpy, p->win);
  23. p->swallowing = c;
  24. c->mon = p->mon;
  25. Window w = p->win;
  26. p->win = c->win;
  27. c->win = w;
  28. XChangeProperty(dpy, c->win, netatom[NetClientList], XA_WINDOW, 32, PropModeReplace,
  29. (unsigned char *) &(p->win), 1);
  30. updatetitle(p);
  31. s = scanner ? c : p;
  32. setfloatinghint(s);
  33. wc.border_width = p->bw;
  34. XConfigureWindow(dpy, p->win, CWBorderWidth, &wc);
  35. XMoveResizeWindow(dpy, p->win, s->x, s->y, s->w, s->h);
  36. XSetWindowBorder(dpy, p->win, scheme[SchemeNorm][ColBorder].pixel);
  37. arrange(p->mon);
  38. configure(p);
  39. updateclientlist();
  40. return 1;
  41. }
  42. void
  43. unswallow(Client *c)
  44. {
  45. XWindowChanges wc;
  46. c->win = c->swallowing->win;
  47. free(c->swallowing);
  48. c->swallowing = NULL;
  49. XDeleteProperty(dpy, c->win, netatom[NetClientList]);
  50. /* unfullscreen the client */
  51. setfullscreen(c, 0);
  52. updatetitle(c);
  53. arrange(c->mon);
  54. XMapWindow(dpy, c->win);
  55. wc.border_width = c->bw;
  56. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc);
  57. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  58. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  59. setfloatinghint(c);
  60. setclientstate(c, NormalState);
  61. arrange(c->mon);
  62. focus(NULL);
  63. }
  64. pid_t
  65. winpid(Window w)
  66. {
  67. pid_t result = 0;
  68. #ifdef __linux__
  69. xcb_res_client_id_spec_t spec = {0};
  70. spec.client = w;
  71. spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
  72. xcb_generic_error_t *e = NULL;
  73. xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
  74. xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
  75. if (!r)
  76. return (pid_t)0;
  77. xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
  78. for (; i.rem; xcb_res_client_id_value_next(&i)) {
  79. spec = i.data->spec;
  80. if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
  81. uint32_t *t = xcb_res_client_id_value_value(i.data);
  82. result = *t;
  83. break;
  84. }
  85. }
  86. free(r);
  87. if (result == (pid_t)-1)
  88. result = 0;
  89. #endif /* __linux__ */
  90. #ifdef __OpenBSD__
  91. Atom type;
  92. int format;
  93. unsigned long len, bytes;
  94. unsigned char *prop;
  95. pid_t ret;
  96. if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
  97. return 0;
  98. ret = *(pid_t*)prop;
  99. XFree(prop);
  100. result = ret;
  101. #endif /* __OpenBSD__ */
  102. return result;
  103. }
  104. pid_t
  105. getparentprocess(pid_t p)
  106. {
  107. unsigned int v = 0;
  108. #ifdef __linux__
  109. FILE *f;
  110. char buf[256];
  111. snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
  112. if (!(f = fopen(buf, "r")))
  113. return (pid_t)0;
  114. if (fscanf(f, "%*u %*s %*c %u", (unsigned *)&v) != 1)
  115. v = (pid_t)0;
  116. fclose(f);
  117. #endif /* __linux__ */
  118. #ifdef __OpenBSD__
  119. int n;
  120. kvm_t *kd;
  121. struct kinfo_proc *kp;
  122. kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
  123. if (!kd)
  124. return 0;
  125. kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
  126. v = kp->p_ppid;
  127. #endif /* __OpenBSD__ */
  128. return (pid_t)v;
  129. }
  130. int
  131. isdescprocess(pid_t p, pid_t c)
  132. {
  133. while (p != c && c != 0)
  134. c = getparentprocess(c);
  135. return (int)c;
  136. }
  137. Client *
  138. termforwin(const Client *w)
  139. {
  140. Client *c;
  141. Monitor *m;
  142. if (!w->pid || w->isterminal)
  143. return NULL;
  144. c = selmon->sel;
  145. if (c && c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
  146. return c;
  147. for (m = mons; m; m = m->next) {
  148. for (c = m->clients; c; c = c->next) {
  149. if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
  150. return c;
  151. }
  152. }
  153. return NULL;
  154. }
  155. Client *
  156. swallowingclient(Window w)
  157. {
  158. Client *c;
  159. Monitor *m;
  160. for (m = mons; m; m = m->next) {
  161. for (c = m->clients; c; c = c->next) {
  162. if (c->swallowing && c->swallowing->win == w)
  163. return c;
  164. }
  165. }
  166. return NULL;
  167. }