fittsen.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Fittsen: map mouse button events on screen corners to commands.
  3. *
  4. * Based on fittstool taken from https://github.com/jeanCarloMachado/fittstool
  5. *
  6. * Copyright (C) 2009 Yasen Atanasov <yasen.atanasov@gmail.com>
  7. * Copyright (C) 2019 Lucas Skoldqvist <frusen@gungre.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. */
  13. #include <xcb/xcb.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "ini.h"
  18. #define str_defined(str) ( (strlen(str) > 0) ? 1 : 0 )
  19. #define get_cmd(win,cmd) win_opt[win].commands[cmd]
  20. #define N_GROUPS 8
  21. #define N_ACTIONS 9
  22. enum eScreenCorners {
  23. TopLeft, TopCentre, TopRight, Right, BottomRight, BottomCentre, BottomLeft,
  24. Left
  25. };
  26. enum eMouseButtons {
  27. LeftButton, MiddleButton, RightButton, WheelUp, WheelDown, WheelUpOnce,
  28. WheelDownOnce, Enter, Leave
  29. };
  30. enum eXCBButtonIndexes {
  31. LEFT_BUTTON=1, MIDDLE_BUTTON, RIGHT_BUTTON, WHEEL_UP_BUTTON,
  32. WHEEL_DOWN_BUTTON
  33. };
  34. struct str_window_options {
  35. char enabled;
  36. int x;
  37. int y;
  38. int h;
  39. int w;
  40. char commands[N_ACTIONS][200]; // TODO: ARG_MAX
  41. xcb_window_t xcb_window; // pointer to the newly created window
  42. time_t last_time_up; // last time a wheel up has been made
  43. time_t last_time_down; // last time a wheel down has been made
  44. } win_opt[N_GROUPS];
  45. char draw_areas = 0;
  46. void
  47. server_create_windows(xcb_connection_t *connection, xcb_screen_t *screen)
  48. {
  49. uint32_t values[2];
  50. values[0] = 1; // XCB_CW_OVERRIDE_REDIRECT: inform WM not to tamper with window
  51. values[1] = XCB_EVENT_MASK_BUTTON_PRESS |
  52. XCB_EVENT_MASK_ENTER_WINDOW |
  53. XCB_EVENT_MASK_LEAVE_WINDOW;
  54. for (int i=0; i<N_GROUPS; i++) {
  55. // Skip unenabled windows
  56. if (win_opt[i].enabled == 0)
  57. continue;
  58. win_opt[i].xcb_window = xcb_generate_id (connection);
  59. xcb_create_window(connection, 0, win_opt[i].xcb_window,
  60. screen->root, win_opt[i].x, win_opt[i].y,
  61. win_opt[i].w, win_opt[i].h, 0,
  62. XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual,
  63. XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK, values);
  64. // Draw windows with white pixels if enabled
  65. if (draw_areas == 1)
  66. xcb_change_window_attributes(connection, win_opt[i].xcb_window,
  67. XCB_CW_BACK_PIXEL, &screen->white_pixel);
  68. xcb_map_window(connection, win_opt[i].xcb_window);
  69. }
  70. xcb_flush(connection);
  71. }
  72. int
  73. server_find_window(xcb_window_t win)
  74. {
  75. for (int i=0; i<N_GROUPS; i++)
  76. if (win_opt[i].xcb_window == win)
  77. return i;
  78. return -1;
  79. }
  80. int
  81. can_execute(const int corner, int direction)
  82. {
  83. time_t current_time;
  84. time_t *last_exec;
  85. long int diff;
  86. if (!direction)
  87. last_exec = &win_opt[corner].last_time_up;
  88. else
  89. last_exec = &win_opt[corner].last_time_down;
  90. time(&current_time);
  91. diff = (long int) current_time - (long int) *last_exec;
  92. if (!last_exec || (diff > 2)) {
  93. *last_exec = current_time;
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. void
  99. server_event_loop (xcb_connection_t *connection)
  100. {
  101. xcb_generic_event_t *event;
  102. xcb_button_press_event_t *bp;
  103. xcb_enter_notify_event_t *enter;
  104. xcb_leave_notify_event_t *leave;
  105. while ((event = xcb_wait_for_event (connection))) {
  106. switch (event->response_type & ~0x80) {
  107. case XCB_BUTTON_PRESS:
  108. bp = (xcb_button_press_event_t *)event;
  109. int cur_win = server_find_window(bp->event);
  110. switch (bp->detail) {
  111. case LEFT_BUTTON:
  112. if (str_defined(get_cmd(cur_win, LeftButton)))
  113. system(get_cmd(cur_win, LeftButton));
  114. break;
  115. case MIDDLE_BUTTON:
  116. if (str_defined(get_cmd(cur_win,MiddleButton)))
  117. system(get_cmd(cur_win,MiddleButton));
  118. break;
  119. case RIGHT_BUTTON:
  120. if (str_defined(get_cmd(cur_win,RightButton)))
  121. system(get_cmd(cur_win,RightButton));
  122. break;
  123. case WHEEL_UP_BUTTON:
  124. if (str_defined(get_cmd(cur_win,WheelUp)))
  125. system(get_cmd(cur_win,WheelUp));
  126. if (str_defined(get_cmd(cur_win,WheelUpOnce)) &&
  127. can_execute(cur_win, 0) )
  128. system(get_cmd(cur_win,WheelUpOnce));
  129. break;
  130. case WHEEL_DOWN_BUTTON:
  131. if (str_defined(get_cmd(cur_win,WheelDown)))
  132. system(get_cmd(cur_win,WheelDown));
  133. if (str_defined(get_cmd(cur_win,WheelDownOnce)) &&
  134. can_execute(cur_win, 1) )
  135. system(get_cmd(cur_win,WheelDownOnce));
  136. break;
  137. }
  138. break;
  139. case XCB_ENTER_NOTIFY:
  140. enter = (xcb_enter_notify_event_t *)event;
  141. cur_win = server_find_window(enter->event);
  142. if (str_defined(get_cmd(cur_win,Enter)))
  143. system(get_cmd(cur_win,Enter));
  144. break;
  145. case XCB_LEAVE_NOTIFY:
  146. leave = (xcb_leave_notify_event_t *)event;
  147. cur_win = server_find_window(leave->event);
  148. if (str_defined(get_cmd(cur_win,Leave)))
  149. system(get_cmd(cur_win,Leave));
  150. break;
  151. default:
  152. break;
  153. }
  154. free (event);
  155. }
  156. }
  157. void
  158. init_options (const int scr_w, const int scr_h)
  159. {
  160. for (int i=0; i<N_GROUPS; i++) {
  161. win_opt[i].enabled = 0;
  162. win_opt[i].last_time_down = (time_t)0;
  163. win_opt[i].last_time_up = (time_t)0;
  164. }
  165. win_opt[TopLeft].w = 5;
  166. win_opt[TopLeft].h = 5;
  167. win_opt[TopLeft].x = 0;
  168. win_opt[TopLeft].y = 0;
  169. win_opt[TopCentre].w = scr_w*0.4;
  170. win_opt[TopCentre].h = 2;
  171. win_opt[TopCentre].x = (scr_w - win_opt[TopCentre].w)/2;
  172. win_opt[TopCentre].y = 0;
  173. win_opt[TopRight].w = 6;
  174. win_opt[TopRight].h = 5;
  175. win_opt[TopRight].x = scr_w-5;
  176. win_opt[TopRight].y = 0;
  177. win_opt[Right].w = 3;
  178. win_opt[Right].h = scr_h*0.4;
  179. win_opt[Right].x = scr_w-2;
  180. win_opt[Right].y = (scr_h - win_opt[Right].h)/2;
  181. win_opt[BottomRight].w = 6;
  182. win_opt[BottomRight].h = 6;
  183. win_opt[BottomRight].x = scr_w-5;
  184. win_opt[BottomRight].y = scr_h-5;
  185. win_opt[BottomCentre].w = scr_w*0.4;
  186. win_opt[BottomCentre].h = 2;
  187. win_opt[BottomCentre].x = (scr_w - win_opt[BottomCentre].w)/2;
  188. win_opt[BottomCentre].y = scr_h-2;
  189. win_opt[BottomLeft].w = 5;
  190. win_opt[BottomLeft].h = 5;
  191. win_opt[BottomLeft].x = 0;
  192. win_opt[BottomLeft].y = scr_h-5;
  193. win_opt[Left].w = 2;
  194. win_opt[Left].h = scr_h*0.4;
  195. win_opt[Left].x = 0;
  196. win_opt[Left].y = (scr_h - win_opt[Left].h)/2;
  197. }
  198. int
  199. get_section(const char *section)
  200. {
  201. if (strcmp(section, "TopLeft") == 0)
  202. return 0;
  203. else if (strcmp(section, "TopCentre") == 0)
  204. return 1;
  205. else if (strcmp(section, "TopRight") == 0)
  206. return 2;
  207. else if (strcmp(section, "Right") == 0)
  208. return 3;
  209. else if (strcmp(section, "BottomRight") == 0)
  210. return 4;
  211. else if (strcmp(section, "BottomCentre") == 0)
  212. return 5;
  213. else if (strcmp(section, "BottomLeft") == 0)
  214. return 6;
  215. else if (strcmp(section, "Left") == 0)
  216. return 7;
  217. else
  218. return -1;
  219. }
  220. int
  221. get_name(const char *name)
  222. {
  223. if (strcmp(name, "LeftButton") == 0)
  224. return 0;
  225. else if (strcmp(name, "MiddleButton") == 0)
  226. return 1;
  227. else if (strcmp(name, "RightButton") == 0)
  228. return 2;
  229. else if (strcmp(name, "WheelUp") == 0)
  230. return 3;
  231. else if (strcmp(name, "WheelDown") == 0)
  232. return 4;
  233. else if (strcmp(name, "WheelUpOnce") == 0)
  234. return 5;
  235. else if (strcmp(name, "WheelDownOnce") == 0)
  236. return 6;
  237. else if (strcmp(name, "Enter") == 0)
  238. return 7;
  239. else if (strcmp(name, "Leave") == 0)
  240. return 8;
  241. else
  242. return -1;
  243. }
  244. static int
  245. handler(void *user, const char *section, const char *name, const char *value)
  246. {
  247. if (strcmp(section, "Options") == 0) {
  248. if (strcmp(name, "DrawAreas") == 0)
  249. draw_areas = 1;
  250. return 1;
  251. }
  252. int s = get_section(section);
  253. if (s == -1) return 0; // unknown section
  254. int n = get_name(name);
  255. if (n == -1) return 0; // unknown name
  256. win_opt[s].enabled = 1;
  257. // TODO: bounds check
  258. strcpy(win_opt[s].commands[n], value);
  259. strcat(win_opt[s].commands[n], " &");
  260. printf("%s %s : %s\n", section, name, win_opt[s].commands[n]);
  261. return 1;
  262. }
  263. int
  264. config_read()
  265. {
  266. char *home = getenv("HOME");
  267. if (home == NULL) {
  268. fprintf(stderr, "Variable HOME is not in the current environment\n");
  269. return -1;
  270. }
  271. char *post = "/.fittsen.cfg";
  272. char *fp = calloc(strlen(home)+strlen(post)+1, 1);
  273. if (fp == NULL) {
  274. fprintf(stderr, "Failure to allocate\n");
  275. return -1;
  276. }
  277. // TODO: bounds check
  278. strcat(fp, home);
  279. strcat(fp, post);
  280. if (ini_parse(fp, handler, NULL) < 0) {
  281. fprintf(stderr, "Cannot load ~/.fittsen.cfg\n");
  282. free(fp);
  283. return -1;
  284. }
  285. free(fp);
  286. return 0;
  287. }
  288. int
  289. main(int argc, char *argv[])
  290. {
  291. xcb_connection_t *conn = xcb_connect(NULL, NULL);
  292. if (xcb_connection_has_error(conn) > 0) {
  293. fprintf(stderr, "Cannot connect to X\n");
  294. return 1;
  295. }
  296. xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
  297. if (screen == NULL) {
  298. fprintf(stderr, "Cannot get screen\n");
  299. xcb_disconnect(conn);
  300. return 1;
  301. }
  302. init_options(screen->width_in_pixels, screen->height_in_pixels);
  303. if (config_read() == -1) {
  304. return 1;
  305. }
  306. server_create_windows(conn, screen);
  307. server_event_loop(conn);
  308. xcb_disconnect(conn);
  309. return 0;
  310. }