zori_console.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include <assert.h>
  2. #include "zori.h"
  3. #include "zori_widget.h"
  4. #include "zori_console.h"
  5. #include "draw.h"
  6. struct zori_console * zori_widget_to_console(struct zori_widget * widget) {
  7. if (!zori_widget_is_type(widget, ZORI_WIDGET_TYPE_CONSOLE)) return NULL;
  8. return ZORI_CONTAINER_OF(widget, struct zori_console, widget);
  9. }
  10. /* Helper struct that keeps track of the BYTE positions within
  11. a c string or USTR where a line or word starts or ends with a given maxwidth. */
  12. struct zori_textinfo {
  13. int from_char;
  14. int start_char;
  15. int stop_char;
  16. int maxwidth;
  17. };
  18. /* Creates a temporary ustr as per al_ref_ustr but with
  19. start and stop as code positions, not byte positions. */
  20. static const USTR *
  21. ustrinfo_newref(
  22. USTR_INFO * uinfo, const USTR * ustr, int start, int stop) {
  23. return ustr_refustr(uinfo, ustr,
  24. ustr_offset(ustr, start),
  25. ustr_offset(ustr, stop)
  26. );
  27. }
  28. /* Creates a temporary ustr that refers ustr but respecds the bounds of the
  29. textinfo (start_char and enc_char) */
  30. const USTR * zori_textinfo_refustr(struct zori_textinfo * self,
  31. USTR_INFO * uinfo,
  32. const USTR * ustr) {
  33. return ustrinfo_newref(uinfo, ustr, self->start_char, self->stop_char);
  34. }
  35. struct zori_textinfo *
  36. zori_textinfo_wordfromtext(struct zori_textinfo * self, USTR * ustr, zori_font * font) {
  37. int found;
  38. int start_pos;
  39. int end_pos;
  40. int now_char;
  41. int end_char;
  42. int len;
  43. int ch;
  44. if(!self) return NULL;
  45. now_char = self->from_char;
  46. self->start_char = now_char;
  47. ch = ustr_getnext(ustr, &now_char);
  48. while (ch > 0) {
  49. switch(ch) {
  50. case ' ': /* Found a space, here the word ends, include the space. */
  51. self->stop_char = now_char;
  52. return self;
  53. case '\n': /* A newline ends a word, include the newline. */
  54. self->stop_char = now_char;
  55. return self;
  56. default: /* Other characters mean the word is not finished yet. */
  57. break;
  58. }
  59. /* XXX: Should handle the case for languages that use no spaces,
  60. * by checking with al_get_ustr_width but it's not a pressing matter yet.
  61. */
  62. ch = ustr_getnext(ustr, &now_char);
  63. }
  64. // no word found, just set end here and be done.
  65. self->stop_char = now_char;
  66. /* return nULL to signify end */
  67. return NULL;
  68. }
  69. /** Prints a ustring, since puts or printf print too much some
  70. times for a refstring.*/
  71. static int ustr_print(USTR * word) {
  72. size_t index;
  73. for(index = 0; index < ustr_length(word) ; index++) {
  74. putchar(ustr_get(word, index));
  75. }
  76. return index;
  77. }
  78. /** Gets the positions of the next line of text fort he given Unicode string
  79. and store them in the given info. If the info's from is set to 0 or less,
  80. the first line is assumed, otherwise, the line will be started from "from".
  81. Uses the given font to determine the width of the text as it is built.
  82. */
  83. struct zori_textinfo *
  84. zori_textinfo_linefromtext(struct zori_textinfo * self, USTR * ustr, zori_font * font) {
  85. struct zori_textinfo wordinfo;
  86. USTR_INFO lineuinfo;
  87. const USTR * line;
  88. USTR_INFO worduinfo = { 0, 0, 0};
  89. const USTR * word;
  90. int ch;
  91. int index;
  92. int width;
  93. int last_stop;
  94. self->start_char = self->from_char;
  95. wordinfo.from_char = self->from_char;
  96. while(zori_textinfo_wordfromtext(&wordinfo, ustr, font)) {
  97. word = zori_textinfo_refustr(&wordinfo, &worduinfo, ustr);
  98. line = ustrinfo_newref(&lineuinfo, ustr, self->start_char, wordinfo.stop_char);
  99. width = al_get_ustr_width(font, line);
  100. if (width > self->maxwidth) {
  101. /* XXX: handle case of text overflow by bluntly retuning the word as is.
  102. Should split single word based on length too.
  103. There is overflow if this is still the first word as see from wordinfo_start_char.
  104. */
  105. if (wordinfo.start_char == self->start_char) {
  106. self->stop_char = wordinfo.stop_char;
  107. } else {
  108. self->stop_char = wordinfo.start_char;
  109. }
  110. return self;
  111. }
  112. // If we get here, the word way still end on a newline character
  113. // check this case. XXX: It works like this because
  114. // stop_char is a bit wonky... it points at the first character of the
  115. // next word in this case...
  116. ch = ustr_get(ustr, wordinfo.stop_char - 1);
  117. if (ch == '\n') {
  118. self->start_char = self->from_char;
  119. self->stop_char = wordinfo.stop_char - 1;
  120. return self;
  121. }
  122. wordinfo.from_char = wordinfo.stop_char;
  123. }
  124. /* if we get here, the whole string fits. */
  125. self->start_char = self->from_char;
  126. self->stop_char = wordinfo.stop_char;
  127. /* Return NULL to tell caller text has been completely split up. */
  128. return NULL;
  129. }
  130. #define ZORI_WIDGET_BORDER 3
  131. /** Draws a rounded frame as background for a widget. */
  132. void zori_widget_drawroundframe(struct zori_widget * self) {
  133. if(!self) return;
  134. draw_roundframe(zori_widget_x(self), zori_widget_y(self),
  135. zori_widget_w(self), zori_widget_h(self),
  136. ZORI_WIDGET_BORDER,
  137. zori_widget_forecolor(self), zori_widget_backcolor(self));
  138. }
  139. /** Skips the text info to the next word or line of text. Must be called
  140. when looping over zori_textinfo_linefromtext. */
  141. struct zori_textinfo * zori_textinfo_next(struct zori_textinfo * self) {
  142. if(!self) return NULL;
  143. self->from_char = self->stop_char + 1;
  144. self->start_char = self->from_char;
  145. return self;
  146. }
  147. /* Converts a widget to a console. Only works if the pointer is wrapped correctly,
  148. by a console. */
  149. struct zori_console * zori_widget_console(struct zori_widget * widget) {
  150. if (!widget) return NULL;
  151. return ZORI_CONTAINER_OF(widget, struct zori_console, widget);
  152. }
  153. /** Sets the console's command function and data. */
  154. void zori_console_command_(struct zori_console * self, zori_console_command * command, void * data) {
  155. self->command = command;
  156. self->command_data = data;
  157. }
  158. /** Let the console perform a command if possible. returns nonzero on error,
  159. zero if OK. */
  160. int zori_console_docommand(struct zori_console * self, const char * text) {
  161. if(!self) return -1;
  162. if(!self->command) return -2;
  163. return self->command(&self->widget, text, self->command_data);
  164. }
  165. /** Adds a line of text to the console. */
  166. int zori_console_addstr(struct zori_console * self, const char * str) {
  167. if(!self) return -1;
  168. if(!ustrlist_shiftcstr(&self->text, str)) {
  169. return -3;
  170. }
  171. while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
  172. ustrlist_droplast(&self->text);
  173. }
  174. return ustrlist_size(&self->text);
  175. }
  176. /** Adds a line of text to the console. */
  177. int zori_console_addustr(struct zori_console * self, const USTR * ustr) {
  178. if(!self) return -1;
  179. if(!ustrlist_shiftustr(&self->text, ustr)) {
  180. return -3;
  181. }
  182. while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
  183. ustrlist_droplast(&self->text);
  184. }
  185. return ustrlist_size(&self->text);
  186. }
  187. /** Puts a string on the console .*/
  188. int zori_console_puts(struct zori_console * self, const char * str) {
  189. int index;
  190. int size = strlen(str);
  191. int leftsize = size;
  192. int lines = 0;
  193. USTR_INFO uinfo;
  194. struct zori_textinfo info = { 0, 0, 0, 0};
  195. info.maxwidth = zori_widget_w(&self->widget) - 10;
  196. USTR * ustr;
  197. const USTR * uline;
  198. ustr = ustr_new(str);
  199. while(zori_textinfo_linefromtext(&info, ustr, self->widget.style.text.font)) {
  200. uline = zori_textinfo_refustr(&info, &uinfo, ustr);
  201. zori_console_addustr(self, uline);
  202. // don't forget to skip to next line!!!
  203. zori_textinfo_next(&info);
  204. }
  205. uline = zori_textinfo_refustr(&info, &uinfo, ustr);
  206. zori_console_addustr(self, uline);
  207. ustr_free(ustr);
  208. return lines;
  209. }
  210. #define ZORI_CONSOLE_VPRINTF_MAX 1024
  211. /** Prints a formatted string on the console, truncaded to 1024 characters. */
  212. int zori_console_vprintf(struct zori_console * self, const char * format, va_list args) {
  213. char buffer[ZORI_CONSOLE_VPRINTF_MAX] = { '\0' };
  214. vsnprintf(buffer, sizeof(buffer), format, args);
  215. return zori_console_puts(self, buffer);
  216. }
  217. /** Prints a formatted string on the console, truncaded to 1024 characters. */
  218. int zori_console_printf(struct zori_console * self, const char * format, ...) {
  219. int result;
  220. va_list args;
  221. va_start(args, format);
  222. result = zori_console_vprintf(self, format, args);
  223. va_end(args);
  224. return result;
  225. }
  226. /** Draws a console. */
  227. int zori_console_draw(union zori_event * zevent) {
  228. struct zori_console * self ;
  229. zori_font * font ;
  230. zori_color color ;
  231. USTRListNode * now;
  232. int high, linehigh, index, x, y, skip;
  233. int linew;
  234. struct zori_widget * widget = zevent->any.widget;
  235. if (!zori_widget_visible(widget)) return ZORI_HANDLE_IGNORE;
  236. self = zori_widget_console(widget);
  237. font = zori_widget_font(widget);
  238. color = zori_widget_forecolor(widget);
  239. zori_widget_drawroundframe(widget);
  240. high = zori_widget_h(widget) - 10;
  241. x = zori_widget_x(widget) + 5;
  242. y = zori_widget_y(widget) - 5;
  243. linehigh = zori_font_lineheight(font);
  244. /* now = ustrlist_head(&self->text); */
  245. /* skip start lines (to allow scrolling backwards) */
  246. now = ustrlist_skipnode(&self->text, self->start);
  247. for (index = high-(linehigh*2); index > 0; index -= linehigh) {
  248. USTR * textstr;
  249. if(!now) break;
  250. textstr = ustrlistnode_ustr(now);
  251. if(textstr) {
  252. zori_font_drawstr(font, color, x, y + index, 0, textstr);
  253. }
  254. now = ustrlistnode_next(now);
  255. }
  256. // draw input string
  257. if (self->input) {
  258. zori_font_drawstr(font, color, x, y + high - linehigh, 0, self->input);
  259. }
  260. // Draw cursor
  261. linew = al_get_ustr_width(font, self->input);
  262. al_draw_line(x + linew, y + high - linehigh, x + linew, y + high, color, 1);
  263. // draw start for debugging
  264. al_draw_textf(font, color, x, y, 0, "start: %d, size: %d", self->start,
  265. ustrlist_size(&self->text));
  266. return ZORI_HANDLE_PASS;
  267. }
  268. /** Activates or deactivates the console. */
  269. void zori_console_active_(struct zori_console * self, int active) {
  270. if(!self) return;
  271. zori_widget_live_(&self->widget, active);
  272. }
  273. /** Returns nonzero if console is active zero if not. */
  274. int zori_console_active(struct zori_console * self) {
  275. if(!self) return 0;
  276. return zori_widget_active(&self->widget);
  277. }
  278. /** scrolls the console 1 step in the given direction. */
  279. int zori_console_scroll(struct zori_console * self, int direction) {
  280. if((!self) || (!direction)) return FALSE;
  281. if(direction < 0) self->start--;
  282. if(direction > 0) self->start++;
  283. /* Clamp start between 0 and size of list. */
  284. self->start = bad_clampi(self->start, 0, ustrlist_size(&self->text));
  285. return ZORI_HANDLE_DONE;
  286. }
  287. /* Key input event handler for console. */
  288. int zori_console_handle_keychar(union zori_event * zevent) {
  289. struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
  290. zori_system_event * event = zori_event_system_event(zevent);
  291. int ch = event->keyboard.unichar;
  292. int kc = event->keyboard.keycode;
  293. switch(kc) {
  294. // ignore the start-console key
  295. case ALLEGRO_KEY_F1:
  296. case ALLEGRO_KEY_F3:
  297. return ZORI_HANDLE_DONE;
  298. case ALLEGRO_KEY_PGUP: return zori_console_scroll(self, 1);
  299. case ALLEGRO_KEY_PGDN: return zori_console_scroll(self, -1);
  300. case ALLEGRO_KEY_BACKSPACE:
  301. // remove last character typed.
  302. ustr_remove_chr(self->input, ustr_offset(self->input, -1));
  303. return ZORI_HANDLE_DONE;
  304. break;
  305. case ALLEGRO_KEY_ENTER: {
  306. const char * command = ustr_c(self->input);
  307. // execute command
  308. if(zori_console_docommand(self, command)) {
  309. zori_console_puts(self, "Error in running comand");
  310. zori_console_puts(self, command);
  311. }
  312. ustr_truncate(self->input, 0);
  313. // empty string by truncating it
  314. return ZORI_HANDLE_DONE;
  315. }
  316. default:
  317. break;
  318. }
  319. ustr_appendch(self->input, ch);
  320. return ZORI_HANDLE_DONE;
  321. }
  322. /* Key down event handler for console. */
  323. int zori_console_handle_keydown(union zori_event * zevent) {
  324. struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
  325. zori_system_event * event = zori_event_system_event(zevent);
  326. int ch = event->keyboard.unichar;
  327. int kc = event->keyboard.keycode;
  328. switch(kc) {
  329. case ALLEGRO_KEY_F1:
  330. case ALLEGRO_KEY_F3:
  331. zori_console_active_(self, false);
  332. /* disable console if F1 is pressed.
  333. * Note: this shouldnever happen if react is set up well.
  334. */
  335. return ZORI_HANDLE_DONE;
  336. default:
  337. break;
  338. }
  339. return ZORI_HANDLE_IGNORE;
  340. }
  341. /* Mouse axe event handler for console */
  342. int zori_console_handle_mouseaxes(union zori_event * zevent) {
  343. struct zori_console * self = zori_widget_console(zori_event_widget(zevent));
  344. zori_system_event * event = zori_event_system_event(zevent);
  345. int z = event->mouse.dz;
  346. // only capture mouse scroll wheel...
  347. if(z == 0) return ZORI_HANDLE_IGNORE;
  348. if(z < 0) return zori_console_scroll(self, -1);
  349. if(z > 0) return zori_console_scroll(self, +1);
  350. return ZORI_HANDLE_DONE;
  351. }
  352. int zori_console_handle_ignore(union zori_event * zevent) {
  353. return ZORI_HANDLE_IGNORE;
  354. }
  355. static struct zori_handler zori_console_actions[] = {
  356. { ZORI_SYSTEM_EVENT_KEY_DOWN , zori_console_handle_keydown , NULL },
  357. { ZORI_SYSTEM_EVENT_KEY_UP , zori_console_handle_ignore , NULL },
  358. { ZORI_SYSTEM_EVENT_KEY_CHAR , zori_console_handle_keychar , NULL },
  359. { ZORI_SYSTEM_EVENT_MOUSE_AXES, zori_console_handle_mouseaxes , NULL },
  360. { ZORI_EVENT_DRAW , zori_console_draw , NULL },
  361. { ZORI_EVENT_DONE , zori_console_handle_ignore , NULL },
  362. { ZORI_EVENT_FREE , zori_console_handle_ignore , NULL },
  363. { -1, NULL, NULL }
  364. };
  365. /** Let the console handle allegro events. Returns 0 if event was consumed,
  366. positive if not, and negative on error. */
  367. int zori_console_handle(struct zori_widget * widget, zori_system_event * sevent) {
  368. union zori_event zevent;
  369. if (!widget) return ZORI_HANDLE_ERROR;
  370. if (!zori_widget_active(widget)) return ZORI_HANDLE_IGNORE;
  371. return zori_widget_raise_system_event(widget, sevent);
  372. }
  373. /** Cleans up a console. */
  374. int zori_console_done(struct zori_widget * widget) {
  375. struct zori_console * self = zori_widget_console(widget);
  376. if(!self) return ZORI_HANDLE_IGNORE;
  377. free(self->buf);
  378. self->buf = NULL;
  379. ustr_free(self->input);
  380. self->input = NULL;
  381. ustrlist_done(&self->text);
  382. return ZORI_HANDLE_DONE;
  383. }
  384. /** Deallocates a console. */
  385. int zori_console_free(struct zori_widget * widget) {
  386. struct zori_console * self = zori_widget_console(widget);
  387. zori_console_done(&self->widget);
  388. free(self);
  389. return ZORI_HANDLE_DONE;
  390. }
  391. /** Allocates a console. */
  392. struct zori_console * zori_console_alloc() {
  393. return calloc(1 , sizeof(struct zori_console));
  394. }
  395. /** Destructor for console. */
  396. void zori_console_destroy(struct zori_widget * widget) {
  397. /* struct zori_console * console = zori_widget_to_console(widget); */
  398. zori_console_done(widget);
  399. }
  400. /* Amount of lines of display the console hat o keep track of. */
  401. #define ZORI_CONSOLE_MAX 200
  402. /** Initializes a console. */
  403. struct zori_console * zori_console_initall(struct zori_console * self, int id, zori_rebox * bounds, struct zori_style * style) {
  404. struct zori_root * root = zori_get_root();
  405. if(!self) return NULL;
  406. assert(root);
  407. /* Allow only a single console. */
  408. if (root->console) return NULL;
  409. if(!zori_widget_initall(&self->widget, ZORI_WIDGET_TYPE_CONSOLE, id, &root->widget, bounds, style, ZORI_ARRAY_AND_AMOUNT(zori_console_actions) ) ) {
  410. return NULL;
  411. }
  412. ustrlist_init(&self->text);
  413. // ustrlist_shiftcstr(&self->text, "empty line");
  414. zori_widget_active_(&self->widget, FALSE);
  415. zori_widget_visible_(&self->widget, FALSE);
  416. self->count = 0;
  417. // max MUST be at least 2, 3 to see anything...
  418. self->max = ZORI_CONSOLE_MAX;
  419. self->start = 0;
  420. self->charw = 80;
  421. self->buf = calloc(self->charw + 1, 1);
  422. // one extra for NULL at end .
  423. if (!self->buf) { zori_console_done(&self->widget); return NULL; }
  424. self->input = ustr_new("");
  425. self->cursor= 0;
  426. if (!self->input) { zori_console_done(&self->widget); return NULL; }
  427. self->command = NULL;
  428. self->command_data = NULL;
  429. /* Ony one console may be active. */
  430. root->console = self;
  431. self->widget.destroy = zori_console_destroy;
  432. return self;
  433. }
  434. /** Initializes a new console. */
  435. struct zori_console * zori_console_new(int id, zori_rebox * bounds, struct zori_style * style) {
  436. struct zori_console * self = zori_console_alloc();
  437. if(!zori_console_initall(self, id, bounds, style)) {
  438. zori_console_free(&self->widget);
  439. return NULL;
  440. }
  441. return self;
  442. }