util.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #ifndef UTIL_H
  6. #define UTIL_H
  7. #include "wmfs.h"
  8. #include <string.h>
  9. /* Todo FREE_LIST(type, head, function_remove) */
  10. #define FREE_LIST(type, head) \
  11. do { \
  12. struct type *Z; \
  13. while(!SLIST_EMPTY(&head)) { \
  14. Z = SLIST_FIRST(&head); \
  15. SLIST_REMOVE_HEAD(&head, next); \
  16. free(Z); /* function_remove(t)*/ \
  17. } \
  18. } while(/* CONSTCOND */ 0);
  19. /* Insert at the end with SLIST */
  20. #define SLIST_INSERT_TAIL(head, elem, field, prev) \
  21. if(!prev) \
  22. SLIST_INSERT_HEAD(head, elem, field); \
  23. else \
  24. SLIST_INSERT_AFTER(prev, elem, field);
  25. /* t is Map or Unmap */
  26. #define WIN_STATE(w, t) do { \
  27. X##t##Subwindows(W->dpy, w); \
  28. X##t##Window(W->dpy, w); \
  29. } while( /* CONSTCOND */ 0);
  30. #define ATOM(a) XInternAtom(W->dpy, (a), False)
  31. #define LEN(x) (sizeof(x) / sizeof(*x))
  32. #define FLAGINT(i) (1 << i)
  33. #define FLAGAPPLY(f, b, m) (f |= (b ? m : 0))
  34. #define ATOI(s) strtol(s, NULL, 10)
  35. #define ABS(j) (j < 0 ? -j : j)
  36. #define INAREA(i, j, a) ((i) >= (a).x && (i) <= (a).x + (a).w && (j) >= (a).y && (j) <= (a).y + (a).h)
  37. #define GEOCMP(g1, g2) ((g1).x == (g2).x && (g1).y == (g2).y && (g1).w == (g2).w && (g1).h == (g2).h)
  38. /*
  39. * "#RRGGBB" -> 0xRRGGBB
  40. */
  41. static inline Color
  42. color_atoh(const char *col)
  43. {
  44. XColor xcolor;
  45. if(!XAllocNamedColor(W->dpy, DefaultColormap(W->dpy, W->xscreen), col, &xcolor, &xcolor))
  46. warnl("Error: cannot allocate color \"%s\".", col);
  47. return xcolor.pixel;
  48. }
  49. static inline void
  50. swap_ptr(void **x, void **y)
  51. {
  52. void *t = *x;
  53. *x = *y;
  54. *y = t;
  55. }
  56. static inline void
  57. swap_int(int *x, int *y)
  58. {
  59. *y = *x ^ *y;
  60. *x = *y ^ *x;
  61. *y = *x ^ *y;
  62. }
  63. static inline enum position
  64. str_to_position(char *str)
  65. {
  66. enum position i;
  67. static const char index[PositionLast][8] = { "right", "left", "top", "bottom", "center" };
  68. for(i = 0; i < PositionLast; ++i)
  69. if(!strcmp(index[i], str))
  70. return i;
  71. return Right;
  72. }
  73. void *xmalloc(size_t nmemb, size_t size);
  74. void *xcalloc(size_t nmemb, size_t size);
  75. void *xrealloc(void *ptr, size_t nmemb, size_t size);
  76. int xasprintf(char **strp, const char *fmt, ...);
  77. char *xstrdup(const char *str);
  78. pid_t spawn(const char *format, ...);
  79. int parse_args(char *str, char delim, char end, int narg, char *args[]);
  80. void uicb_spawn(Uicb cmd);
  81. #endif /* UTIL_H */