init.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /** Init WMFS
  34. */
  35. void
  36. init(void)
  37. {
  38. /* First init */
  39. ewmh_init_hints();
  40. init_layout();
  41. init_conf();
  42. init_gc();
  43. init_font();
  44. init_cursor();
  45. init_key();
  46. init_root();
  47. screen_init_geo();
  48. infobar_init();
  49. init_status();
  50. ewmh_update_current_tag_prop();
  51. grabkeys();
  52. return;
  53. }
  54. /** Init the font
  55. */
  56. void
  57. init_font(void)
  58. {
  59. font = XftFontOpenName(dpy, SCREEN, conf.font);
  60. if(!font)
  61. {
  62. warnx("WMFS Error: Cannot initialize font");
  63. font = XftFontOpenName(dpy, SCREEN, "sans-10");
  64. }
  65. return;
  66. }
  67. /** Init the graphic context
  68. */
  69. void
  70. init_gc(void)
  71. {
  72. XGCValues gcv;
  73. /* Bits sequences */
  74. const char pix_bits[] =
  75. {
  76. 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
  77. 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
  78. 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
  79. 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55
  80. };
  81. gc = DefaultGC(dpy, SCREEN);
  82. /* Stipple GC */
  83. gcv.function = GXcopy;
  84. gcv.fill_style = FillStippled;
  85. gcv.stipple = XCreateBitmapFromData(dpy, ROOT, pix_bits, 10, 4);
  86. gc_stipple = XCreateGC(dpy, ROOT, GCFunction | GCFillStyle | GCStipple, &gcv);
  87. return;
  88. }
  89. /** Init WMFS cursor
  90. */
  91. void
  92. init_cursor(void)
  93. {
  94. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  95. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  96. cursor[CurRightResize] = XCreateFontCursor(dpy, XC_lr_angle);
  97. cursor[CurLeftResize] = XCreateFontCursor(dpy, XC_ll_angle);
  98. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  99. return;
  100. }
  101. /** Init key modifier
  102. */
  103. void
  104. init_key(void)
  105. {
  106. int i, j;
  107. XModifierKeymap *modmap = XGetModifierMapping(dpy);
  108. for(i = 0; i < 8; i++)
  109. for(j = 0; j < modmap->max_keypermod; ++j)
  110. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  111. == XKeysymToKeycode(dpy, XK_Num_Lock))
  112. numlockmask = (1 << i);
  113. XFreeModifiermap(modmap);
  114. return;
  115. }
  116. /** Init root Window
  117. */
  118. void
  119. init_root(void)
  120. {
  121. XSetWindowAttributes at;
  122. at.event_mask = KeyMask | ButtonMask | MouseMask | PropertyChangeMask
  123. | SubstructureRedirectMask | SubstructureNotifyMask | StructureNotifyMask;
  124. at.cursor = cursor[CurNormal];
  125. XChangeWindowAttributes(dpy, ROOT, CWEventMask | CWCursor, &at);
  126. if(conf.root.background_command)
  127. spawn("%s", conf.root.background_command);
  128. ewmh_init_hints();
  129. ewmh_get_number_of_desktop();
  130. ewmh_get_desktop_names();
  131. return;
  132. }
  133. /** Init layout
  134. */
  135. void
  136. init_layout(void)
  137. {
  138. int i;
  139. const func_name_list_t layout_list_tmp[] =
  140. {
  141. {"tile", tile },
  142. {"tile_right", tile },
  143. {"tile_left", tile_left },
  144. {"tile_top", tile_top },
  145. {"tile_bottom", tile_bottom },
  146. {"tile_grid", grid },
  147. {"grid", grid },
  148. {"mirror_vertical", mirror_vertical },
  149. {"tile_mirror_vertical", mirror_vertical },
  150. {"mirror_horizontal", mirror_horizontal },
  151. {"tile_mirror_horizontal", mirror_horizontal },
  152. {"layer", layer },
  153. {"max", maxlayout },
  154. {"maxlayout", maxlayout },
  155. {"freelayout", freelayout },
  156. {"free", freelayout }
  157. };
  158. layout_list = emalloc(LEN(layout_list_tmp), sizeof(func_name_list_t));
  159. memset(layout_list, 0, LEN(layout_list_tmp));
  160. for(i = 0; i < LEN(layout_list_tmp); ++i)
  161. layout_list[i] = layout_list_tmp[i];
  162. return;
  163. }
  164. /** Init statustext shell script
  165. */
  166. void
  167. init_status(void)
  168. {
  169. int fd;
  170. struct stat st;
  171. status_path = emalloc(strlen(getenv("HOME")) + strlen(DEF_STATUS) + 2, sizeof(char));
  172. sprintf(status_path, "%s/"DEF_STATUS, getenv("HOME"));
  173. if(!(fd = open(status_path, O_RDONLY))
  174. || !fopen(status_path, "r"))
  175. {
  176. free(status_path);
  177. estatus = False;
  178. return;
  179. }
  180. stat(status_path, &st);
  181. if(st.st_size && st.st_mode & S_IXUSR)
  182. {
  183. estatus = True;
  184. system(status_path);
  185. }
  186. else
  187. warnx("status.sh file present in wmfs directory can't be executed, try 'chmod +x %s'.",
  188. status_path);
  189. close(fd);
  190. return;
  191. }