util.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * util.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. /** Calloc with an error message if there is a probleme
  34. * \param element Element
  35. * \param size Size
  36. * \return void pointer
  37. */
  38. void*
  39. emalloc(uint element, uint size)
  40. {
  41. void *ret = calloc(element, size);
  42. if(!ret)
  43. warn("calloc()");
  44. return ret;
  45. }
  46. /** Get a color with a string
  47. * \param color Color string
  48. * \return Color pixel
  49. */
  50. long
  51. getcolor(char *color)
  52. {
  53. XColor xcolor;
  54. if(!XAllocNamedColor(dpy, DefaultColormap(dpy, SCREEN), color, &xcolor, &xcolor))
  55. warnx("Error: cannot allocate color \"%s\".", color);
  56. return xcolor.pixel;
  57. }
  58. /** Enlight an hexadecimal color
  59. * \param col Color
  60. * \return The clarified color
  61. */
  62. ulong
  63. color_enlight(ulong col)
  64. {
  65. if((col + 0x330000) < 0xffffff
  66. && (col + 0x003300) < 0xffffff
  67. && (col + 0x000033) < 0xffffff)
  68. return col + 0x333333;
  69. else
  70. return col;
  71. }
  72. /** Set the window WM State
  73. * \param win Window target
  74. * \param state WM State
  75. */
  76. void
  77. setwinstate(Window win, long state)
  78. {
  79. long data[] = {state, None};
  80. XChangeProperty(dpy, win, ATOM("WM_STATE"), ATOM("WM_STATE"), 32,
  81. PropModeReplace, (uchar *)data, 2);
  82. return;
  83. }
  84. /** My strdup. the strdup of string.h isn't ansi compatible..
  85. * Thanks linkdd.
  86. * \param str char pointer
  87. */
  88. char*
  89. _strdup(const char *str)
  90. {
  91. char *ret = emalloc(strlen(str) + 1, sizeof(char));
  92. strcpy(ret, str);
  93. return ret;
  94. }
  95. /* The following function are for configuration
  96. usage. {{{
  97. */
  98. void*
  99. name_to_func(char *name, func_name_list_t *l)
  100. {
  101. int i;
  102. if(name)
  103. for(i = 0; l[i].name ; ++i)
  104. if(!strcmp(name, l[i].name))
  105. return l[i].func;
  106. return NULL;
  107. }
  108. ulong
  109. char_to_modkey(char *name, key_name_list_t key_l[])
  110. {
  111. int i;
  112. if(name)
  113. for(i = 0; key_l[i].name; ++i)
  114. if(!strcmp(name, key_l[i].name))
  115. return key_l[i].keysym;
  116. return NoSymbol;
  117. }
  118. uint
  119. char_to_button(char *name, name_to_uint_t blist[])
  120. {
  121. int i;
  122. if(name)
  123. for(i = 0; blist[i].name; ++i)
  124. if(!strcmp(name, blist[i].name))
  125. return blist[i].button;
  126. return 0;
  127. }
  128. Layout
  129. layout_name_to_struct(Layout lt[], char *name, int n, func_name_list_t llist[])
  130. {
  131. int i;
  132. for(i = 0; i < n; ++i)
  133. if(lt[i].func == name_to_func(name, llist))
  134. return lt[i];
  135. return lt[0];
  136. }
  137. char*
  138. alias_to_str(char *conf_choice)
  139. {
  140. int i;
  141. char *tmpchar = NULL;
  142. if(!conf_choice)
  143. return 0;
  144. if(conf.alias)
  145. for(i = 0; conf.alias[i].name; i++)
  146. if(!strcmp(conf_choice, conf.alias[i].name))
  147. tmpchar = conf.alias[i].content;
  148. if(tmpchar)
  149. return _strdup(tmpchar);
  150. else
  151. return _strdup(conf_choice);
  152. return NULL;
  153. }
  154. /* }}} */
  155. /** Get the mouse pointer position.
  156. */
  157. XRectangle
  158. get_mouse_pos(void)
  159. {
  160. Window dum;
  161. int d, u;
  162. XRectangle ret;
  163. XQueryPointer(dpy, ROOT, &dum, &dum, (int*)&ret.x, (int*)&ret.y, &d, &d, (uint *)&u);
  164. return ret;
  165. }
  166. /** Execute a sh command
  167. * \param cmd Command
  168. */
  169. void
  170. spawn(const char *format, ...)
  171. {
  172. char *sh = NULL;
  173. char cmd[512];
  174. va_list ap;
  175. if(strlen(format) > 511)
  176. {
  177. warnx("spawn(): Error, command too long.");
  178. return;
  179. }
  180. va_start(ap, format);
  181. vsprintf(cmd, format, ap);
  182. va_end(ap);
  183. if(!strlen(cmd))
  184. return;
  185. if(!(sh = getenv("SHELL")))
  186. sh = "/bin/sh";
  187. if(fork() == 0)
  188. {
  189. if(fork() == 0)
  190. {
  191. if(dpy)
  192. close(ConnectionNumber(dpy));
  193. setsid();
  194. execl(sh, sh, "-c", cmd, (char*)NULL);
  195. exit(EXIT_SUCCESS);
  196. }
  197. exit(EXIT_SUCCESS);
  198. }
  199. return;
  200. }
  201. /** Swap two pointer.
  202. *\param x First pointer
  203. *\param y Second pointer
  204. */
  205. void
  206. swap_ptr(void **x, void **y)
  207. {
  208. void *t = *x;
  209. *x = *y;
  210. *y = t;
  211. return;
  212. }
  213. /** Execute a sh command
  214. * \param cmd Command (uicb_t type)
  215. */
  216. void
  217. uicb_spawn(uicb_t cmd)
  218. {
  219. spawn("%s", cmd);
  220. }