event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2007 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. DO_COMMAND(do_event)
  26. {
  27. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  28. struct listnode *node;
  29. int cnt;
  30. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  31. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  32. if (*arg1 == 0)
  33. {
  34. tintin_header(ses, " EVENTS ");
  35. for (cnt = 0 ; *event_table[cnt].name != 0 ; cnt++)
  36. {
  37. tintin_printf2(ses, "%s [%-27s] %s", search_node_list(ses->list[LIST_EVENT], event_table[cnt].name) ? "+" : " ", event_table[cnt].name, event_table[cnt].desc);
  38. }
  39. tintin_header(ses, "");
  40. }
  41. else if (*arg2 == 0)
  42. {
  43. if (show_node_with_wild(ses, arg1, ses->list[LIST_EVENT]) == FALSE)
  44. {
  45. show_message(ses, LIST_ALIAS, "#EVENT: NO MATCH(ES) FOUND FOR {%s}.", arg1);
  46. }
  47. }
  48. else
  49. {
  50. for (cnt = 0 ; *event_table[cnt].name != 0 ; cnt++)
  51. {
  52. if (!strncmp(event_table[cnt].name, arg1, strlen(event_table[cnt].name)))
  53. {
  54. show_message(ses, LIST_EVENT, "#EVENT {%s} HAS BEEN SET TO {%s}.", arg1, arg2);
  55. SET_BIT(ses->event_flags, event_table[cnt].flags);
  56. node = update_node_list(ses->list[LIST_EVENT], arg1, arg2, "", "");
  57. node->val64 = event_table[cnt].flags;
  58. return ses;
  59. }
  60. }
  61. show_error(ses, LIST_EVENT, "#EVENT {%s} IS NOT AN EXISTING EVENT.", arg1);
  62. }
  63. return ses;
  64. }
  65. DO_COMMAND(do_unevent)
  66. {
  67. delete_node_with_wild(ses, LIST_EVENT, arg);
  68. return ses;
  69. }
  70. int check_all_events(struct session *ses, int flags, int args, int vars, char *fmt, ...)
  71. {
  72. struct session *ses_ptr;
  73. struct listnode *node;
  74. char *name, buf[BUFFER_SIZE];
  75. va_list list;
  76. int cnt;
  77. if (gtd->level->ignore)
  78. {
  79. return 0;
  80. }
  81. if (args)
  82. {
  83. va_start(list, fmt);
  84. vasprintf(&name, fmt, list);
  85. va_end(list);
  86. }
  87. else
  88. {
  89. name = strdup(fmt);
  90. }
  91. push_call("check_all_events(%p,%d,%d,%d,%s, ...)",ses,flags,args,vars,name);
  92. for (ses_ptr = ses ? ses : gts ; ses_ptr ; ses_ptr = ses_ptr->next)
  93. {
  94. if (!HAS_BIT(ses_ptr->list[LIST_EVENT]->flags, LIST_FLAG_IGNORE))
  95. {
  96. show_info(ses_ptr, LIST_EVENT, "#INFO EVENT {%s}", name);
  97. node = search_node_list(ses_ptr->list[LIST_EVENT], name);
  98. if (node)
  99. {
  100. if (vars)
  101. {
  102. va_start(list, fmt);
  103. for (cnt = 0 ; cnt < args ; cnt++)
  104. {
  105. va_arg(list, char *);
  106. }
  107. for (cnt = 0 ; cnt < vars ; cnt++)
  108. {
  109. RESTRING(gtd->vars[cnt], va_arg(list, char *));
  110. }
  111. va_end(list);
  112. }
  113. substitute(ses_ptr, node->arg2, buf, flags);
  114. if (HAS_BIT(ses_ptr->list[LIST_EVENT]->flags, LIST_FLAG_DEBUG))
  115. {
  116. show_debug(ses_ptr, LIST_EVENT, "#DEBUG EVENT {%s} (%s}", node->arg1, node->arg2);
  117. }
  118. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  119. {
  120. delete_node_list(ses, LIST_EVENT, node);
  121. }
  122. script_driver(ses_ptr, LIST_EVENT, buf);
  123. if (ses)
  124. {
  125. free(name);
  126. pop_call();
  127. return 1;
  128. }
  129. }
  130. }
  131. if (ses)
  132. {
  133. free(name);
  134. pop_call();
  135. return 0;
  136. }
  137. }
  138. free(name);
  139. pop_call();
  140. return 0;
  141. }
  142. void mouse_handler(struct session *ses, int flags, int row, int col, char type)
  143. {
  144. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], line[BUFFER_SIZE], word[BUFFER_SIZE];
  145. static char last[100], dir[10];
  146. static long long click[3];
  147. static int swipe[10];
  148. int debug, info, rev_row, rev_col;
  149. push_call("mouse_handler(%p,%d,%d,%d,%c)",ses,flags,row,col,type);
  150. if (!HAS_BIT(ses->event_flags, EVENT_FLAG_MOUSE))
  151. {
  152. if (!HAS_BIT(ses->flags, SES_FLAG_MOUSEINFO) && !HAS_BIT(ses->list[LIST_EVENT]->flags, LIST_FLAG_INFO))
  153. {
  154. return;
  155. }
  156. }
  157. if (HAS_BIT(flags, MOUSE_FLAG_MOTION))
  158. {
  159. strcpy(arg1, "MOVED");
  160. }
  161. else
  162. {
  163. switch (type)
  164. {
  165. case 'M':
  166. if (HAS_BIT(flags, MOUSE_FLAG_WHEEL))
  167. {
  168. strcpy(arg1, "SCROLLED");
  169. }
  170. else
  171. {
  172. strcpy(arg1, "PRESSED");
  173. }
  174. break;
  175. case 'm':
  176. strcpy(arg1, "RELEASED");
  177. break;
  178. default:
  179. strcpy(arg1, "UNKNOWN");
  180. break;
  181. }
  182. }
  183. arg2[0] = 0;
  184. if (HAS_BIT(flags, MOUSE_FLAG_CTRL))
  185. {
  186. strcat(arg2, "CTRL ");
  187. }
  188. if (HAS_BIT(flags, MOUSE_FLAG_ALT))
  189. {
  190. strcat(arg2, "ALT ");
  191. }
  192. if (HAS_BIT(flags, MOUSE_FLAG_SHIFT))
  193. {
  194. strcat(arg2, "SHIFT ");
  195. }
  196. if (HAS_BIT(flags, MOUSE_FLAG_EXTRA))
  197. {
  198. strcat(arg2, "EXTRA ");
  199. }
  200. if (HAS_BIT(flags, MOUSE_FLAG_UNKNOWN))
  201. {
  202. strcat(arg2, "256 ");
  203. }
  204. if (HAS_BIT(flags, MOUSE_FLAG_WHEEL))
  205. {
  206. strcat(arg2, "MOUSE WHEEL ");
  207. }
  208. else
  209. {
  210. strcat(arg2, "MOUSE BUTTON ");
  211. }
  212. if (row - 1 < 0)
  213. {
  214. tintin_printf2(ses, "mouse_handler: bad row: (row,col)=(%d,%d)", row, col);
  215. pop_call();
  216. return;
  217. }
  218. else if (row - 1 > gtd->screen->rows)
  219. {
  220. tintin_printf2(ses, "mouse_handler: bad col: (row,col)=(%d,%d)", row, col);
  221. pop_call();
  222. return;
  223. }
  224. else
  225. {
  226. strcpy(line, "under development");
  227. strcpy(word, "under development");
  228. // get_line_screen(line, row - 1);
  229. // get_word_screen(word, row - 1, col - 1);
  230. }
  231. if (HAS_BIT(flags, MOUSE_FLAG_WHEEL))
  232. {
  233. if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_A) && HAS_BIT(flags, MOUSE_FLAG_BUTTON_B))
  234. {
  235. strcat(arg2, "RIGHT");
  236. }
  237. else if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_B))
  238. {
  239. strcat(arg2, "LEFT");
  240. }
  241. else if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_A))
  242. {
  243. strcat(arg2, "DOWN");
  244. }
  245. else
  246. {
  247. strcat(arg2, "UP");
  248. }
  249. }
  250. else
  251. {
  252. if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_A) && HAS_BIT(flags, MOUSE_FLAG_BUTTON_B))
  253. {
  254. strcat(arg2, "FOUR");
  255. }
  256. else if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_B))
  257. {
  258. strcat(arg2, "THREE");
  259. }
  260. else if (HAS_BIT(flags, MOUSE_FLAG_BUTTON_A))
  261. {
  262. strcat(arg2, "TWO");
  263. }
  264. else
  265. {
  266. strcat(arg2, "ONE");
  267. }
  268. }
  269. debug = HAS_BIT(ses->flags, SES_FLAG_MOUSEDEBUG) ? 1 : 0;
  270. info = HAS_BIT(ses->flags, SES_FLAG_MOUSEINFO) ? 1 : 0;
  271. gtd->level->debug += debug;
  272. gtd->level->info += info;
  273. check_all_buttons(ses, row, col, arg1, arg2, word, line);
  274. rev_row = -1 - (gtd->screen->rows - row);
  275. rev_col = -1 - (gtd->screen->cols - col);
  276. check_all_events(ses, SUB_ARG, 2, 6, "%s %s", arg1, arg2, ntos(row), ntos(col), ntos(rev_row), ntos(-1 - (gtd->screen->cols - col)), word, line);
  277. check_all_events(ses, SUB_ARG, 3, 6, "%s %s %d", arg1, arg2, row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  278. check_all_events(ses, SUB_ARG, 3, 6, "%s %s %d", arg1, arg2, rev_row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  279. map_mouse_handler(ses, arg1, arg2, col, row, 0, 0);
  280. if (!strcmp(arg1, "PRESSED"))
  281. {
  282. sprintf(arg1, "PRESSED %s %d %d", arg2, col, row);
  283. swipe[0] = row;
  284. swipe[1] = col;
  285. swipe[2] = rev_row;
  286. swipe[3] = rev_col;
  287. if (!strcmp(arg1, last))
  288. {
  289. click[2] = click[1];
  290. click[1] = click[0];
  291. click[0] = utime();
  292. if (click[0] - click[1] < 500000)
  293. {
  294. if (click[0] - click[2] < 500000)
  295. {
  296. check_all_buttons(ses, row, col, "TRIPLE-CLICKED", arg2, word, line);
  297. check_all_events(ses, SUB_ARG, 1, 6, "TRIPLE-CLICKED %s", arg2, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  298. check_all_events(ses, SUB_ARG, 2, 6, "TRIPLE-CLICKED %s %d", arg2, row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  299. check_all_events(ses, SUB_ARG, 2, 6, "TRIPLE-CLICKED %s %d", arg2, rev_row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  300. map_mouse_handler(ses, "TRIPPLE-CLICKED", arg2, col, row, 0, 0);
  301. strcpy(last, "");
  302. }
  303. else
  304. {
  305. check_all_buttons(ses, row, col, "DOUBLE-CLICKED", arg2, word, line);
  306. check_all_events(ses, SUB_ARG, 1, 6, "DOUBLE-CLICKED %s", arg2, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  307. check_all_events(ses, SUB_ARG, 2, 6, "DOUBLE-CLICKED %s %d", arg2, row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  308. check_all_events(ses, SUB_ARG, 2, 6, "DOUBLE-CLICKED %s %d", arg2, rev_row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  309. map_mouse_handler(ses, "DOUBLE-CLICKED", arg2, col, row, 0, 0);
  310. }
  311. }
  312. }
  313. else
  314. {
  315. click[2] = 0;
  316. click[1] = 0;
  317. click[0] = utime();
  318. sprintf(last, "PRESSED %s %d %d", arg2, col, row);
  319. }
  320. }
  321. else if (!strcmp(arg1, "RELEASED"))
  322. {
  323. swipe[4] = row;
  324. swipe[5] = col;
  325. swipe[6] = rev_row;
  326. swipe[7] = rev_col;
  327. swipe[8] = swipe[4] - swipe[0];
  328. swipe[9] = swipe[5] - swipe[1];
  329. if (abs(swipe[8]) > 3 || abs(swipe[9]) > 3)
  330. {
  331. if (abs(swipe[8]) <= 3)
  332. {
  333. strcpy(dir, swipe[9] > 0 ? "E" : "W");
  334. }
  335. else if (abs(swipe[9]) <= 3)
  336. {
  337. strcpy(dir, swipe[8] > 0 ? "S" : "N");
  338. }
  339. else if (swipe[8] > 0)
  340. {
  341. strcpy(dir, swipe[9] > 0 ? "SE" : "SW");
  342. }
  343. else
  344. {
  345. strcpy(dir, swipe[9] > 0 ? "NE" : "NW");
  346. }
  347. check_all_events(ses, SUB_ARG, 0, 12, "SWIPED", dir, arg2, ntos(swipe[0]), ntos(swipe[1]), ntos(swipe[2]), ntos(swipe[3]), ntos(swipe[4]), ntos(swipe[5]), ntos(swipe[6]), ntos(swipe[7]), ntos(swipe[8]), ntos(swipe[9]));
  348. check_all_events(ses, SUB_ARG, 1, 12, "SWIPED %s", dir, dir, arg2, ntos(swipe[0]), ntos(swipe[1]), ntos(swipe[2]), ntos(swipe[3]), ntos(swipe[4]), ntos(swipe[5]), ntos(swipe[6]), ntos(swipe[7]), ntos(swipe[8]), ntos(swipe[9]));
  349. check_all_events(ses, SUB_ARG, 2, 12, "SWIPED %s %s", arg2, dir, dir, arg2, ntos(swipe[0]), ntos(swipe[1]), ntos(swipe[2]), ntos(swipe[3]), ntos(swipe[4]), ntos(swipe[5]), ntos(swipe[6]), ntos(swipe[7]), ntos(swipe[8]), ntos(swipe[9]));
  350. }
  351. else if (utime() - click[0] >= 500000)
  352. {
  353. check_all_buttons(ses, row, col, "LONG-CLICKED", arg2, word, line);
  354. check_all_events(ses, SUB_ARG, 1, 6, "LONG-CLICKED %s", arg2, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  355. check_all_events(ses, SUB_ARG, 2, 6, "LONG-CLICKED %s %d", arg2, row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  356. check_all_events(ses, SUB_ARG, 2, 6, "LONG-CLICKED %s %d", arg2, rev_row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  357. map_mouse_handler(ses, "LONG-CLICKED", arg2, col, row, 0, 0);
  358. }
  359. else if (click[0] - click[1] >= 500000)
  360. {
  361. if (abs(swipe[0] - swipe[4]) <= 3 && abs(swipe[1] - swipe[5]) <= 3)
  362. {
  363. check_all_buttons(ses, row, col, "SHORT-CLICKED", arg2, word, line);
  364. check_all_events(ses, SUB_ARG, 1, 6, "SHORT-CLICKED %s", arg2, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  365. check_all_events(ses, SUB_ARG, 2, 6, "SHORT-CLICKED %s %d", arg2, row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  366. check_all_events(ses, SUB_ARG, 2, 6, "SHORT-CLICKED %s %d", arg2, rev_row, ntos(row), ntos(col), ntos(rev_row), ntos(rev_col), word, line);
  367. map_mouse_handler(ses, "SHORT-CLICKED", arg2, col, row, 0, 0);
  368. }
  369. }
  370. }
  371. gtd->level->debug -= debug;
  372. gtd->level->info -= info;
  373. pop_call();
  374. return;
  375. }