No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1881 líneas
58 KiB

  1. /*
  2. * php-caca Php binding for Libcaca
  3. * Copyright (c) 2008 Vion Nicolas <nico@picapo.net>
  4. *
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. #include "php.h"
  16. #include "php_caca.h"
  17. #ifdef HAVE_GD
  18. #include <gd.h>
  19. #endif
  20. static function_entry caca_functions[] = {
  21. PHP_FE(caca_create_canvas, NULL)
  22. PHP_FE(caca_set_canvas_size, NULL)
  23. PHP_FE(caca_get_canvas_width, NULL)
  24. PHP_FE(caca_get_canvas_height, NULL)
  25. PHP_FE(caca_get_canvas_chars, NULL)
  26. PHP_FE(caca_get_canvas_attrs, NULL)
  27. PHP_FE(caca_rand, NULL)
  28. PHP_FE(caca_get_version, NULL)
  29. PHP_FE(caca_gotoxy, NULL)
  30. PHP_FE(caca_get_cursor_x, NULL)
  31. PHP_FE(caca_get_cursor_y, NULL)
  32. PHP_FE(caca_put_char, NULL)
  33. PHP_FE(caca_get_char, NULL)
  34. PHP_FE(caca_put_str, NULL)
  35. PHP_FE(caca_clear_canvas, NULL)
  36. PHP_FE(caca_set_canvas_handle, NULL)
  37. PHP_FE(caca_get_canvas_handle_x, NULL)
  38. PHP_FE(caca_get_canvas_handle_y, NULL)
  39. PHP_FE(caca_blit, NULL)
  40. PHP_FE(caca_set_canvas_boundaries, NULL)
  41. PHP_FE(caca_invert, NULL)
  42. PHP_FE(caca_flip, NULL)
  43. PHP_FE(caca_flop, NULL)
  44. PHP_FE(caca_rotate_180, NULL)
  45. PHP_FE(caca_rotate_left, NULL)
  46. PHP_FE(caca_rotate_right, NULL)
  47. PHP_FE(caca_stretch_left, NULL)
  48. PHP_FE(caca_stretch_right, NULL)
  49. PHP_FE(caca_get_attr, NULL)
  50. PHP_FE(caca_set_attr, NULL)
  51. PHP_FE(caca_put_attr, NULL)
  52. PHP_FE(caca_set_color_ansi, NULL)
  53. PHP_FE(caca_set_color_argb, NULL)
  54. PHP_FE(caca_attr_to_ansi, NULL)
  55. PHP_FE(caca_attr_to_ansi_fg, NULL)
  56. PHP_FE(caca_attr_to_ansi_bg, NULL)
  57. PHP_FE(caca_attr_to_rgb12_fg, NULL)
  58. PHP_FE(caca_attr_to_rgb12_bg, NULL)
  59. PHP_FE(caca_attr_to_argb64, NULL)
  60. PHP_FE(caca_utf8_to_utf32, NULL)
  61. PHP_FE(caca_utf32_to_utf8, NULL)
  62. PHP_FE(caca_utf32_to_cp437, NULL)
  63. PHP_FE(caca_cp437_to_utf32, NULL)
  64. PHP_FE(caca_utf32_to_ascii, NULL)
  65. PHP_FE(caca_utf32_is_fullwidth, NULL)
  66. PHP_FE(caca_draw_line, NULL)
  67. PHP_FE(caca_draw_polyline, NULL)
  68. PHP_FE(caca_draw_thin_line, NULL)
  69. PHP_FE(caca_draw_thin_polyline, NULL)
  70. PHP_FE(caca_draw_circle, NULL)
  71. PHP_FE(caca_draw_ellipse, NULL)
  72. PHP_FE(caca_draw_thin_ellipse, NULL)
  73. PHP_FE(caca_fill_ellipse, NULL)
  74. PHP_FE(caca_draw_box, NULL)
  75. PHP_FE(caca_draw_thin_box, NULL)
  76. PHP_FE(caca_draw_cp437_box, NULL)
  77. PHP_FE(caca_fill_box, NULL)
  78. PHP_FE(caca_draw_triangle, NULL)
  79. PHP_FE(caca_draw_thin_triangle, NULL)
  80. PHP_FE(caca_fill_triangle, NULL)
  81. PHP_FE(caca_get_frame_count, NULL)
  82. PHP_FE(caca_set_frame, NULL)
  83. PHP_FE(caca_set_frame_name, NULL)
  84. PHP_FE(caca_create_frame, NULL)
  85. PHP_FE(caca_free_frame, NULL)
  86. PHP_FE(caca_create_dither, NULL)
  87. PHP_FE(caca_set_dither_palette, NULL)
  88. PHP_FE(caca_set_dither_brightness, NULL)
  89. PHP_FE(caca_get_dither_brightness, NULL)
  90. PHP_FE(caca_set_dither_gamma, NULL)
  91. PHP_FE(caca_get_dither_gamma, NULL)
  92. PHP_FE(caca_set_dither_contrast, NULL)
  93. PHP_FE(caca_get_dither_contrast, NULL)
  94. PHP_FE(caca_set_dither_antialias, NULL)
  95. PHP_FE(caca_get_dither_antialias_list, NULL)
  96. PHP_FE(caca_get_dither_antialias, NULL)
  97. PHP_FE(caca_set_dither_color, NULL)
  98. PHP_FE(caca_get_dither_color_list, NULL)
  99. PHP_FE(caca_get_dither_color, NULL)
  100. PHP_FE(caca_set_dither_charset, NULL)
  101. PHP_FE(caca_get_dither_charset_list, NULL)
  102. PHP_FE(caca_get_dither_charset, NULL)
  103. PHP_FE(caca_set_dither_algorithm, NULL)
  104. PHP_FE(caca_get_dither_algorithm_list, NULL)
  105. PHP_FE(caca_get_dither_algorithm, NULL)
  106. PHP_FE(caca_dither_bitmap, NULL)
  107. PHP_FE(caca_load_font, NULL)
  108. PHP_FE(caca_load_builtin_font, NULL)
  109. PHP_FE(caca_get_font_list, NULL)
  110. PHP_FE(caca_get_font_width, NULL)
  111. PHP_FE(caca_get_font_height, NULL)
  112. PHP_FE(caca_get_font_blocks, NULL)
  113. PHP_FE(caca_render_canvas, NULL)
  114. PHP_FE(caca_canvas_set_figfont, NULL)
  115. PHP_FE(caca_put_figchar, NULL)
  116. PHP_FE(caca_flush_figlet, NULL)
  117. PHP_FE(caca_file_open, NULL)
  118. PHP_FE(caca_file_close, NULL)
  119. PHP_FE(caca_file_tell, NULL)
  120. PHP_FE(caca_file_read, NULL)
  121. PHP_FE(caca_file_write, NULL)
  122. PHP_FE(caca_file_gets, NULL)
  123. PHP_FE(caca_file_eof, NULL)
  124. PHP_FE(caca_import_string, NULL)
  125. PHP_FE(caca_import_file, NULL)
  126. PHP_FE(caca_get_import_list, NULL)
  127. PHP_FE(caca_export_string, NULL)
  128. PHP_FE(caca_get_export_list, NULL)
  129. PHP_FE(caca_create_display, NULL)
  130. PHP_FE(caca_create_display_with_driver, NULL)
  131. PHP_FE(caca_get_display_driver_list, NULL)
  132. PHP_FE(caca_get_display_driver, NULL)
  133. PHP_FE(caca_set_display_driver, NULL)
  134. PHP_FE(caca_get_canvas, NULL)
  135. PHP_FE(caca_refresh_display, NULL)
  136. PHP_FE(caca_set_display_time, NULL)
  137. PHP_FE(caca_get_display_time, NULL)
  138. PHP_FE(caca_get_display_width, NULL)
  139. PHP_FE(caca_get_display_height, NULL)
  140. PHP_FE(caca_set_display_title, NULL)
  141. PHP_FE(caca_set_mouse, NULL)
  142. PHP_FE(caca_set_cursor, NULL)
  143. PHP_FE(caca_get_event, NULL)
  144. PHP_FE(caca_get_mouse_x, NULL)
  145. PHP_FE(caca_get_mouse_y, NULL)
  146. PHP_FE(caca_get_event_type, NULL)
  147. PHP_FE(caca_get_event_key_ch, NULL)
  148. PHP_FE(caca_get_event_mouse_button, NULL)
  149. PHP_FE(caca_get_event_mouse_x, NULL)
  150. PHP_FE(caca_get_event_mouse_y, NULL)
  151. PHP_FE(caca_get_event_resize_width, NULL)
  152. PHP_FE(caca_get_event_resize_height, NULL)
  153. {NULL, NULL, NULL}
  154. };
  155. zend_module_entry caca_module_entry = {
  156. #if ZEND_MODULE_API_NO >= 20010901
  157. STANDARD_MODULE_HEADER,
  158. #endif
  159. PHP_CACA_EXTNAME,
  160. caca_functions,
  161. PHP_MINIT(caca),
  162. NULL,
  163. NULL,
  164. NULL,
  165. PHP_MINFO(caca),
  166. #if ZEND_MODULE_API_NO >= 20010901
  167. PHP_CACA_VERSION,
  168. #endif
  169. STANDARD_MODULE_PROPERTIES
  170. };
  171. #ifdef COMPILE_DL_CACA
  172. ZEND_GET_MODULE(caca)
  173. #endif
  174. PHP_MINFO_FUNCTION(caca) {
  175. php_info_print_table_start();
  176. php_info_print_table_row(2, "Caca library version", caca_get_version());
  177. php_info_print_table_end();
  178. }
  179. //-------- Caca's ressources destructors -----------//
  180. static void php_caca_canvas_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  181. caca_free_canvas(rsrc->ptr);
  182. }
  183. static void php_caca_dither_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  184. caca_free_dither(rsrc->ptr);
  185. }
  186. static void php_caca_font_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  187. caca_free_font(rsrc->ptr);
  188. }
  189. static void php_caca_file_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  190. caca_file_close(rsrc->ptr);
  191. }
  192. static void php_caca_display_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  193. caca_free_display(rsrc->ptr);
  194. }
  195. static void php_caca_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  196. efree((caca_event_t *) rsrc->ptr);
  197. }
  198. //-------- Initialization ---------//
  199. PHP_MINIT_FUNCTION(caca) {
  200. le_caca_canvas = zend_register_list_destructors_ex(php_caca_canvas_dtor, NULL, PHP_CACA_CANVAS_RES_NAME, module_number);
  201. le_caca_dither = zend_register_list_destructors_ex(php_caca_dither_dtor, NULL, PHP_CACA_DITHER_RES_NAME, module_number);
  202. le_caca_font = zend_register_list_destructors_ex(php_caca_font_dtor, NULL, PHP_CACA_FONT_RES_NAME, module_number);
  203. le_caca_file = zend_register_list_destructors_ex(php_caca_file_dtor, NULL, PHP_CACA_FILE_RES_NAME, module_number);
  204. le_caca_display = zend_register_list_destructors_ex(php_caca_display_dtor, NULL, PHP_CACA_DISPLAY_RES_NAME, module_number);
  205. le_caca_event = zend_register_list_destructors_ex(php_caca_event_dtor, NULL, PHP_CACA_EVENT_RES_NAME, module_number);
  206. REGISTER_LONG_CONSTANT("CACA_BLACK", CACA_BLACK, CONST_CS | CONST_PERSISTENT);
  207. REGISTER_LONG_CONSTANT("CACA_BLUE", CACA_BLUE, CONST_CS | CONST_PERSISTENT);
  208. REGISTER_LONG_CONSTANT("CACA_GREEN", CACA_GREEN, CONST_CS | CONST_PERSISTENT);
  209. REGISTER_LONG_CONSTANT("CACA_CYAN", CACA_CYAN, CONST_CS | CONST_PERSISTENT);
  210. REGISTER_LONG_CONSTANT("CACA_RED", CACA_RED, CONST_CS | CONST_PERSISTENT);
  211. REGISTER_LONG_CONSTANT("CACA_MAGENTA", CACA_MAGENTA, CONST_CS | CONST_PERSISTENT);
  212. REGISTER_LONG_CONSTANT("CACA_BROWN", CACA_BROWN, CONST_CS | CONST_PERSISTENT);
  213. REGISTER_LONG_CONSTANT("CACA_LIGHTGRAY", CACA_LIGHTGRAY, CONST_CS | CONST_PERSISTENT);
  214. REGISTER_LONG_CONSTANT("CACA_DARKGRAY", CACA_DARKGRAY, CONST_CS | CONST_PERSISTENT);
  215. REGISTER_LONG_CONSTANT("CACA_LIGHTBLUE", CACA_LIGHTBLUE, CONST_CS | CONST_PERSISTENT);
  216. REGISTER_LONG_CONSTANT("CACA_LIGHTGREEN", CACA_LIGHTGREEN, CONST_CS | CONST_PERSISTENT);
  217. REGISTER_LONG_CONSTANT("CACA_LIGHTCYAN", CACA_LIGHTCYAN, CONST_CS | CONST_PERSISTENT);
  218. REGISTER_LONG_CONSTANT("CACA_LIGHTRED", CACA_LIGHTRED, CONST_CS | CONST_PERSISTENT);
  219. REGISTER_LONG_CONSTANT("CACA_LIGHTMAGENTA", CACA_LIGHTMAGENTA, CONST_CS | CONST_PERSISTENT);
  220. REGISTER_LONG_CONSTANT("CACA_YELLOW", CACA_YELLOW, CONST_CS | CONST_PERSISTENT);
  221. REGISTER_LONG_CONSTANT("CACA_WHITE", CACA_WHITE, CONST_CS | CONST_PERSISTENT);
  222. REGISTER_LONG_CONSTANT("CACA_DEFAULT", CACA_DEFAULT, CONST_CS | CONST_PERSISTENT);
  223. REGISTER_LONG_CONSTANT("CACA_TRANSPARENT", CACA_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
  224. REGISTER_LONG_CONSTANT("CACA_BOLD", CACA_BOLD, CONST_CS | CONST_PERSISTENT);
  225. REGISTER_LONG_CONSTANT("CACA_ITALICS", CACA_ITALICS, CONST_CS | CONST_PERSISTENT);
  226. REGISTER_LONG_CONSTANT("CACA_UNDERLINE", CACA_UNDERLINE, CONST_CS | CONST_PERSISTENT);
  227. REGISTER_LONG_CONSTANT("CACA_BLINK", CACA_BLINK, CONST_CS | CONST_PERSISTENT);
  228. REGISTER_LONG_CONSTANT("CACA_EVENT_NONE", CACA_EVENT_NONE, CONST_CS | CONST_PERSISTENT);
  229. REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_PRESS", CACA_EVENT_KEY_PRESS, CONST_CS | CONST_PERSISTENT);
  230. REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_RELEASE", CACA_EVENT_KEY_RELEASE, CONST_CS | CONST_PERSISTENT);
  231. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_PRESS", CACA_EVENT_MOUSE_PRESS, CONST_CS | CONST_PERSISTENT);
  232. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_RELEASE", CACA_EVENT_MOUSE_RELEASE, CONST_CS | CONST_PERSISTENT);
  233. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_MOTION", CACA_EVENT_MOUSE_MOTION, CONST_CS | CONST_PERSISTENT);
  234. REGISTER_LONG_CONSTANT("CACA_EVENT_RESIZE", CACA_EVENT_RESIZE, CONST_CS | CONST_PERSISTENT);
  235. REGISTER_LONG_CONSTANT("CACA_EVENT_QUIT", CACA_EVENT_QUIT, CONST_CS | CONST_PERSISTENT);
  236. REGISTER_LONG_CONSTANT("CACA_EVENT_ANY", CACA_EVENT_ANY, CONST_CS | CONST_PERSISTENT);
  237. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_A", CACA_KEY_CTRL_A, CONST_CS | CONST_PERSISTENT);
  238. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_B", CACA_KEY_CTRL_B, CONST_CS | CONST_PERSISTENT);
  239. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_C", CACA_KEY_CTRL_C, CONST_CS | CONST_PERSISTENT);
  240. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_D", CACA_KEY_CTRL_D, CONST_CS | CONST_PERSISTENT);
  241. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_E", CACA_KEY_CTRL_E, CONST_CS | CONST_PERSISTENT);
  242. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_F", CACA_KEY_CTRL_F, CONST_CS | CONST_PERSISTENT);
  243. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_G", CACA_KEY_CTRL_G, CONST_CS | CONST_PERSISTENT);
  244. REGISTER_LONG_CONSTANT("CACA_KEY_BACKSPACE", CACA_KEY_BACKSPACE, CONST_CS | CONST_PERSISTENT);
  245. REGISTER_LONG_CONSTANT("CACA_KEY_TAB", CACA_KEY_TAB, CONST_CS | CONST_PERSISTENT);
  246. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_J", CACA_KEY_CTRL_J, CONST_CS | CONST_PERSISTENT);
  247. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_K", CACA_KEY_CTRL_K, CONST_CS | CONST_PERSISTENT);
  248. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_L", CACA_KEY_CTRL_L, CONST_CS | CONST_PERSISTENT);
  249. REGISTER_LONG_CONSTANT("CACA_KEY_RETURN", CACA_KEY_RETURN, CONST_CS | CONST_PERSISTENT);
  250. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_N", CACA_KEY_CTRL_N, CONST_CS | CONST_PERSISTENT);
  251. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_O", CACA_KEY_CTRL_O, CONST_CS | CONST_PERSISTENT);
  252. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_P", CACA_KEY_CTRL_P, CONST_CS | CONST_PERSISTENT);
  253. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Q", CACA_KEY_CTRL_Q, CONST_CS | CONST_PERSISTENT);
  254. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_R", CACA_KEY_CTRL_R, CONST_CS | CONST_PERSISTENT);
  255. REGISTER_LONG_CONSTANT("CACA_KEY_PAUSE", CACA_KEY_PAUSE, CONST_CS | CONST_PERSISTENT);
  256. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_T", CACA_KEY_CTRL_T, CONST_CS | CONST_PERSISTENT);
  257. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_U", CACA_KEY_CTRL_U, CONST_CS | CONST_PERSISTENT);
  258. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_V", CACA_KEY_CTRL_V, CONST_CS | CONST_PERSISTENT);
  259. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_W", CACA_KEY_CTRL_W, CONST_CS | CONST_PERSISTENT);
  260. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_X", CACA_KEY_CTRL_X, CONST_CS | CONST_PERSISTENT);
  261. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Y", CACA_KEY_CTRL_Y, CONST_CS | CONST_PERSISTENT);
  262. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Z", CACA_KEY_CTRL_Z, CONST_CS | CONST_PERSISTENT);
  263. REGISTER_LONG_CONSTANT("CACA_KEY_ESCAPE", CACA_KEY_ESCAPE, CONST_CS | CONST_PERSISTENT);
  264. REGISTER_LONG_CONSTANT("CACA_KEY_DELETE", CACA_KEY_DELETE, CONST_CS | CONST_PERSISTENT);
  265. REGISTER_LONG_CONSTANT("CACA_KEY_UP", CACA_KEY_UP, CONST_CS | CONST_PERSISTENT);
  266. REGISTER_LONG_CONSTANT("CACA_KEY_DOWN", CACA_KEY_DOWN, CONST_CS | CONST_PERSISTENT);
  267. REGISTER_LONG_CONSTANT("CACA_KEY_LEFT", CACA_KEY_LEFT, CONST_CS | CONST_PERSISTENT);
  268. REGISTER_LONG_CONSTANT("CACA_KEY_RIGHT", CACA_KEY_RIGHT, CONST_CS | CONST_PERSISTENT);
  269. REGISTER_LONG_CONSTANT("CACA_KEY_INSERT", CACA_KEY_INSERT, CONST_CS | CONST_PERSISTENT);
  270. REGISTER_LONG_CONSTANT("CACA_KEY_HOME", CACA_KEY_HOME, CONST_CS | CONST_PERSISTENT);
  271. REGISTER_LONG_CONSTANT("CACA_KEY_END", CACA_KEY_END, CONST_CS | CONST_PERSISTENT);
  272. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEUP", CACA_KEY_PAGEUP, CONST_CS | CONST_PERSISTENT);
  273. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEDOWN", CACA_KEY_PAGEDOWN, CONST_CS | CONST_PERSISTENT);
  274. REGISTER_LONG_CONSTANT("CACA_KEY_F1", CACA_KEY_F1, CONST_CS | CONST_PERSISTENT);
  275. REGISTER_LONG_CONSTANT("CACA_KEY_F2", CACA_KEY_F2, CONST_CS | CONST_PERSISTENT);
  276. REGISTER_LONG_CONSTANT("CACA_KEY_F3", CACA_KEY_F3, CONST_CS | CONST_PERSISTENT);
  277. REGISTER_LONG_CONSTANT("CACA_KEY_F4", CACA_KEY_F4, CONST_CS | CONST_PERSISTENT);
  278. REGISTER_LONG_CONSTANT("CACA_KEY_F5", CACA_KEY_F5, CONST_CS | CONST_PERSISTENT);
  279. REGISTER_LONG_CONSTANT("CACA_KEY_F6", CACA_KEY_F6, CONST_CS | CONST_PERSISTENT);
  280. REGISTER_LONG_CONSTANT("CACA_KEY_F7", CACA_KEY_F7, CONST_CS | CONST_PERSISTENT);
  281. REGISTER_LONG_CONSTANT("CACA_KEY_F8", CACA_KEY_F8, CONST_CS | CONST_PERSISTENT);
  282. REGISTER_LONG_CONSTANT("CACA_KEY_F9", CACA_KEY_F9, CONST_CS | CONST_PERSISTENT);
  283. REGISTER_LONG_CONSTANT("CACA_KEY_F10", CACA_KEY_F10, CONST_CS | CONST_PERSISTENT);
  284. REGISTER_LONG_CONSTANT("CACA_KEY_F11", CACA_KEY_F11, CONST_CS | CONST_PERSISTENT);
  285. REGISTER_LONG_CONSTANT("CACA_KEY_F12", CACA_KEY_F12, CONST_CS | CONST_PERSISTENT);
  286. REGISTER_LONG_CONSTANT("CACA_KEY_F13", CACA_KEY_F13, CONST_CS | CONST_PERSISTENT);
  287. REGISTER_LONG_CONSTANT("CACA_KEY_F14", CACA_KEY_F14, CONST_CS | CONST_PERSISTENT);
  288. REGISTER_LONG_CONSTANT("CACA_KEY_F15", CACA_KEY_F15, CONST_CS | CONST_PERSISTENT);
  289. return SUCCESS;
  290. }
  291. //---------- Some usefull macros ---------------//
  292. #define FETCH_CANVAS(canvas) \
  293. zval *_zval; \
  294. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  295. RETURN_FALSE; \
  296. } \
  297. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  298. #define FETCH_DISPLAY(display) \
  299. zval *_zval; \
  300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  301. RETURN_FALSE; \
  302. } \
  303. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  304. #define FETCH_EVENT(event) \
  305. zval *_zval; \
  306. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  307. RETURN_FALSE; \
  308. } \
  309. ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event);
  310. #define FETCH_LONG(l) \
  311. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &l) == FAILURE) { \
  312. RETURN_FALSE; \
  313. }
  314. #define RETURN_CHAR(c) \
  315. char *str = emalloc(2); \
  316. str[0] = c; \
  317. str[1] = '\0'; \
  318. RETURN_STRING(str, 0);
  319. #define RETURN_SUCCESS(i) \
  320. RETURN_BOOL((i) == 0);
  321. //---------- Some usefull functions --------------------//
  322. //Fetch external php resources such as gd resouces
  323. void *fetch_external_resource(zval *_zval, char const *type_name) {
  324. int resource_id = _zval->value.lval;
  325. int resource_type;
  326. void *result = zend_list_find(resource_id, &resource_type);
  327. if (!result)
  328. return NULL;
  329. char *resource_type_name = zend_rsrc_list_get_rsrc_type(resource_id);
  330. return (strcmp(resource_type_name, type_name) == 0) ? result : NULL;
  331. }
  332. //Fetch buffer of pixels from gdImage
  333. #ifdef HAVE_GD
  334. void *gd_get_pixels(gdImage *img) {
  335. void *result;
  336. int j, pitch;
  337. if (img->trueColor) {
  338. pitch = img->sx * sizeof(int);
  339. result = malloc(img->sy * pitch);
  340. for (j = 0; j < img->sy; j++)
  341. memcpy(result + (j * pitch), (const void *) img->tpixels[j], pitch);
  342. }
  343. else {
  344. pitch = img->sx * sizeof(char);
  345. result = malloc(img->sy * pitch);
  346. for (j = 0; j < img->sy; j++)
  347. memcpy(result + (j * pitch), (const void *) img->pixels[j], pitch);
  348. }
  349. return result;
  350. }
  351. int gd_load_palette(gdImage *img, caca_dither_t *dither) {
  352. if (!img || img->trueColor || gdMaxColors != 256) {
  353. return -1;
  354. }
  355. uint32_t r[256], g[256], b[256], a[256];
  356. int i;
  357. for (i = 0; i < 256; i++) {
  358. r[i] = img->red[i] << 4;
  359. g[i] = img->green[i] << 4;
  360. b[i] = img->blue[i] << 4;
  361. a[i] = img->alpha[i] << 4;
  362. }
  363. return caca_set_dither_palette(dither, &r[0], &g[0], &b[0], &a[0]);
  364. }
  365. #endif
  366. //------- Caca's functions ----------------//
  367. PHP_FUNCTION(caca_create_canvas) {
  368. long width, height = 0;
  369. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &width, &height) == FAILURE) {
  370. RETURN_FALSE;
  371. }
  372. caca_canvas_t *canvas = caca_create_canvas(width, height);
  373. if (!canvas) {
  374. RETURN_FALSE;
  375. }
  376. ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
  377. }
  378. PHP_FUNCTION(caca_set_canvas_size) {
  379. zval *_zval;
  380. long width, height = 0;
  381. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &width, &height) == FAILURE) {
  382. RETURN_FALSE;
  383. }
  384. caca_canvas_t *canvas;
  385. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  386. RETURN_SUCCESS(caca_set_canvas_size(canvas, width, height));
  387. }
  388. PHP_FUNCTION(caca_get_canvas_width) {
  389. caca_canvas_t *canvas;
  390. FETCH_CANVAS(canvas);
  391. RETURN_LONG(caca_get_canvas_width(canvas));
  392. }
  393. PHP_FUNCTION(caca_get_canvas_height) {
  394. caca_canvas_t *canvas;
  395. FETCH_CANVAS(canvas);
  396. RETURN_LONG(caca_get_canvas_height(canvas));
  397. }
  398. PHP_FUNCTION(caca_get_canvas_chars) {
  399. caca_canvas_t *canvas;
  400. FETCH_CANVAS(canvas);
  401. RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1);
  402. }
  403. PHP_FUNCTION(caca_get_canvas_attrs) {
  404. caca_canvas_t *canvas;
  405. FETCH_CANVAS(canvas);
  406. RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1);
  407. }
  408. PHP_FUNCTION(caca_rand) {
  409. long min, max = 0;
  410. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &min, &max) == FAILURE) {
  411. RETURN_FALSE;
  412. }
  413. RETURN_LONG(caca_rand(min, max));
  414. }
  415. PHP_FUNCTION(caca_get_version) {
  416. if (ZEND_NUM_ARGS() != 0) {
  417. WRONG_PARAM_COUNT;
  418. }
  419. RETURN_STRING((char *) caca_get_version(), 1);
  420. }
  421. PHP_FUNCTION(caca_gotoxy) {
  422. zval *_zval;
  423. long x, y = 0;
  424. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  425. RETURN_FALSE;
  426. }
  427. caca_canvas_t *canvas;
  428. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  429. RETURN_SUCCESS(caca_gotoxy(canvas, x, y));
  430. }
  431. PHP_FUNCTION(caca_get_cursor_x) {
  432. caca_canvas_t *canvas;
  433. FETCH_CANVAS(canvas);
  434. RETURN_LONG(caca_get_cursor_x(canvas));
  435. }
  436. PHP_FUNCTION(caca_get_cursor_y) {
  437. caca_canvas_t *canvas;
  438. FETCH_CANVAS(canvas);
  439. RETURN_LONG(caca_get_cursor_y(canvas));
  440. }
  441. PHP_FUNCTION(caca_put_char) {
  442. zval *_zval;
  443. long x, y, c;
  444. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == FAILURE) {
  445. RETURN_FALSE;
  446. }
  447. caca_canvas_t *canvas;
  448. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  449. RETURN_SUCCESS(caca_put_char(canvas, x, y, c));
  450. }
  451. PHP_FUNCTION(caca_get_char) {
  452. zval *_zval;
  453. long x, y = 0;
  454. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  455. RETURN_FALSE;
  456. }
  457. caca_canvas_t *canvas;
  458. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  459. RETURN_CHAR(caca_get_char(canvas, x, y));
  460. }
  461. PHP_FUNCTION(caca_put_str) {
  462. zval *_zval;
  463. char *str;
  464. long x, y, str_len = 0;
  465. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &str, &str_len) == FAILURE) {
  466. RETURN_FALSE;
  467. }
  468. caca_canvas_t *canvas;
  469. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  470. RETURN_SUCCESS(caca_put_str(canvas, x, y, str));
  471. }
  472. PHP_FUNCTION(caca_clear_canvas) {
  473. caca_canvas_t *canvas;
  474. FETCH_CANVAS(canvas);
  475. RETURN_LONG(caca_clear_canvas(canvas));
  476. }
  477. PHP_FUNCTION(caca_set_canvas_handle) {
  478. zval *_zval;
  479. long x, y = 0;
  480. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  481. RETURN_FALSE;
  482. }
  483. caca_canvas_t *canvas;
  484. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  485. RETURN_SUCCESS(caca_set_canvas_handle(canvas, x, y));
  486. }
  487. PHP_FUNCTION(caca_get_canvas_handle_x) {
  488. caca_canvas_t *canvas;
  489. FETCH_CANVAS(canvas);
  490. RETURN_LONG(caca_get_canvas_handle_x(canvas));
  491. }
  492. PHP_FUNCTION(caca_get_canvas_handle_y) {
  493. caca_canvas_t *canvas;
  494. FETCH_CANVAS(canvas);
  495. RETURN_LONG(caca_get_canvas_handle_y(canvas));
  496. }
  497. PHP_FUNCTION(caca_blit) {
  498. zval *_zval1, *_zval2, *_zval3 = NULL;
  499. long x, y = 0;
  500. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllr|r", &_zval1, &x, &y, &_zval2, &_zval3) == FAILURE) {
  501. RETURN_FALSE;
  502. }
  503. caca_canvas_t *dst, *src, *mask = NULL;
  504. ZEND_FETCH_RESOURCE(dst, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  505. ZEND_FETCH_RESOURCE(src, caca_canvas_t*, &_zval2, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  506. if (_zval3) {
  507. ZEND_FETCH_RESOURCE(mask, caca_canvas_t*, &_zval3, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  508. }
  509. RETURN_SUCCESS(caca_blit(dst, x, y, src, NULL));
  510. }
  511. PHP_FUNCTION(caca_set_canvas_boundaries) {
  512. zval *_zval;
  513. long x, y, width, height = 0;
  514. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &width, &height) == FAILURE) {
  515. RETURN_FALSE;
  516. }
  517. caca_canvas_t *canvas;
  518. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  519. RETURN_SUCCESS(caca_set_canvas_boundaries(canvas, x, y, width, height));
  520. }
  521. PHP_FUNCTION(caca_invert) {
  522. caca_canvas_t *canvas;
  523. FETCH_CANVAS(canvas);
  524. RETURN_SUCCESS(caca_invert(canvas));
  525. }
  526. PHP_FUNCTION(caca_flip) {
  527. caca_canvas_t *canvas;
  528. FETCH_CANVAS(canvas);
  529. RETURN_SUCCESS(caca_flip(canvas));
  530. }
  531. PHP_FUNCTION(caca_flop) {
  532. caca_canvas_t *canvas;
  533. FETCH_CANVAS(canvas);
  534. RETURN_SUCCESS(caca_flop(canvas));
  535. }
  536. PHP_FUNCTION(caca_rotate_180) {
  537. caca_canvas_t *canvas;
  538. FETCH_CANVAS(canvas);
  539. RETURN_SUCCESS(caca_rotate_180(canvas));
  540. }
  541. PHP_FUNCTION(caca_rotate_left) {
  542. caca_canvas_t *canvas;
  543. FETCH_CANVAS(canvas);
  544. RETURN_SUCCESS(caca_rotate_left(canvas));
  545. }
  546. PHP_FUNCTION(caca_rotate_right) {
  547. caca_canvas_t *canvas;
  548. FETCH_CANVAS(canvas);
  549. RETURN_SUCCESS(caca_rotate_right(canvas));
  550. }
  551. PHP_FUNCTION(caca_stretch_left) {
  552. caca_canvas_t *canvas;
  553. FETCH_CANVAS(canvas);
  554. RETURN_SUCCESS(caca_stretch_left(canvas));
  555. }
  556. PHP_FUNCTION(caca_stretch_right) {
  557. caca_canvas_t *canvas;
  558. FETCH_CANVAS(canvas);
  559. RETURN_SUCCESS(caca_stretch_right(canvas));
  560. }
  561. PHP_FUNCTION(caca_get_attr) {
  562. zval *_zval;
  563. long x, y = 0;
  564. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  565. RETURN_FALSE;
  566. }
  567. caca_canvas_t *canvas;
  568. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  569. RETURN_LONG(caca_get_attr(canvas, x, y));
  570. }
  571. PHP_FUNCTION(caca_set_attr) {
  572. zval *_zval;
  573. long attr = 0;
  574. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &attr) == FAILURE) {
  575. RETURN_FALSE;
  576. }
  577. caca_canvas_t *canvas;
  578. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  579. RETURN_SUCCESS(caca_set_attr(canvas, attr));
  580. }
  581. PHP_FUNCTION(caca_put_attr) {
  582. zval *_zval;
  583. long x, y, attr = 0;
  584. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &attr) == FAILURE) {
  585. RETURN_FALSE;
  586. }
  587. caca_canvas_t *canvas;
  588. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  589. RETURN_SUCCESS(caca_put_attr(canvas, x, y, attr));
  590. }
  591. PHP_FUNCTION(caca_set_color_ansi) {
  592. zval *_zval;
  593. long foreground, background = 0;
  594. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
  595. RETURN_FALSE;
  596. }
  597. caca_canvas_t *canvas;
  598. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  599. RETURN_SUCCESS(caca_set_color_ansi(canvas, foreground, background));
  600. }
  601. PHP_FUNCTION(caca_set_color_argb) {
  602. zval *_zval;
  603. long foreground, background = 0;
  604. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
  605. RETURN_FALSE;
  606. }
  607. caca_canvas_t *canvas;
  608. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  609. RETURN_SUCCESS(caca_set_color_argb(canvas, foreground, background));
  610. }
  611. PHP_FUNCTION(caca_attr_to_ansi) {
  612. long l;
  613. FETCH_LONG(l);
  614. RETURN_LONG(caca_attr_to_ansi(l));
  615. }
  616. PHP_FUNCTION(caca_attr_to_ansi_fg) {
  617. long l;
  618. FETCH_LONG(l);
  619. RETURN_LONG(caca_attr_to_ansi_fg(l));
  620. }
  621. PHP_FUNCTION(caca_attr_to_ansi_bg) {
  622. long l;
  623. FETCH_LONG(l);
  624. RETURN_LONG(caca_attr_to_ansi_bg(l));
  625. }
  626. PHP_FUNCTION(caca_attr_to_rgb12_fg) {
  627. long l;
  628. FETCH_LONG(l);
  629. RETURN_LONG(caca_attr_to_rgb12_fg(l));
  630. }
  631. PHP_FUNCTION(caca_attr_to_rgb12_bg) {
  632. long l;
  633. FETCH_LONG(l);
  634. RETURN_LONG(caca_attr_to_rgb12_bg(l));
  635. }
  636. PHP_FUNCTION(caca_attr_to_argb64) {
  637. //TODO: write or delete
  638. }
  639. PHP_FUNCTION(caca_utf8_to_utf32) {
  640. //TODO: write or delete
  641. }
  642. PHP_FUNCTION(caca_utf32_to_utf8) {
  643. //TODO: write or delete
  644. }
  645. PHP_FUNCTION(caca_utf32_to_cp437) {
  646. long l;
  647. FETCH_LONG(l);
  648. RETURN_LONG(caca_utf32_to_cp437(l));
  649. }
  650. PHP_FUNCTION(caca_cp437_to_utf32) {
  651. long l;
  652. FETCH_LONG(l);
  653. RETURN_LONG(caca_cp437_to_utf32(l));
  654. }
  655. PHP_FUNCTION(caca_utf32_to_ascii) {
  656. long l;
  657. FETCH_LONG(l);
  658. RETURN_CHAR(caca_utf32_to_ascii(l));
  659. }
  660. PHP_FUNCTION(caca_utf32_is_fullwidth) {
  661. //TODO: write or delete
  662. }
  663. PHP_FUNCTION(caca_draw_line) {
  664. zval *_zval;
  665. long xa, ya, xb, yb, c;
  666. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  667. RETURN_FALSE;
  668. }
  669. caca_canvas_t *canvas;
  670. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  671. RETURN_SUCCESS(caca_draw_line(canvas, xa, ya, xb, yb, c));
  672. }
  673. PHP_FUNCTION(caca_draw_polyline) {
  674. zval *zval_res, *zval_arr;
  675. long c;
  676. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ral", &zval_res, &zval_arr, &c) == FAILURE) {
  677. RETURN_FALSE;
  678. }
  679. caca_canvas_t *canvas;
  680. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  681. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  682. int lenmax = zend_hash_num_elements(arr_hash);
  683. int *tbl_x, *tbl_y;
  684. int tbl_count = 0;
  685. tbl_x = malloc(sizeof(int) * lenmax);
  686. tbl_y = malloc(sizeof(int) * lenmax);
  687. HashPosition pos;
  688. zval **pt, **x, **y;
  689. for (
  690. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  691. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  692. zend_hash_move_forward_ex(arr_hash, &pos)
  693. ) {
  694. if (
  695. Z_TYPE_P(*pt) == IS_ARRAY
  696. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  697. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  698. ) {
  699. convert_to_long_ex(x);
  700. convert_to_long_ex(y);
  701. tbl_x[tbl_count] = Z_LVAL_PP(x);
  702. tbl_y[tbl_count] = Z_LVAL_PP(y);
  703. tbl_count++;
  704. }
  705. }
  706. int res = caca_draw_polyline(canvas, tbl_x, tbl_y, tbl_count - 1, c);
  707. free(tbl_x);
  708. free(tbl_y);
  709. RETURN_SUCCESS(res);
  710. }
  711. PHP_FUNCTION(caca_draw_thin_line) {
  712. zval *_zval;
  713. long xa, ya, xb, yb = 0;
  714. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  715. RETURN_FALSE;
  716. }
  717. caca_canvas_t *canvas;
  718. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  719. RETURN_SUCCESS(caca_draw_thin_line(canvas, xa, ya, xb, yb));
  720. }
  721. PHP_FUNCTION(caca_draw_thin_polyline) {
  722. zval *zval_res, *zval_arr;
  723. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &zval_arr) == FAILURE) {
  724. RETURN_FALSE;
  725. }
  726. caca_canvas_t *canvas;
  727. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  728. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  729. int lenmax = zend_hash_num_elements(arr_hash);
  730. int *tbl_x, *tbl_y;
  731. int tbl_count = 0;
  732. tbl_x = malloc(sizeof(int) * lenmax);
  733. tbl_y = malloc(sizeof(int) * lenmax);
  734. if (!tbl_x || !tbl_y) {
  735. RETURN_FALSE;
  736. }
  737. HashPosition pos;
  738. zval **pt, **x, **y;
  739. for (
  740. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  741. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  742. zend_hash_move_forward_ex(arr_hash, &pos)
  743. ) {
  744. if (
  745. Z_TYPE_P(*pt) == IS_ARRAY
  746. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  747. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  748. ) {
  749. convert_to_long_ex(x);
  750. convert_to_long_ex(y);
  751. tbl_x[tbl_count] = Z_LVAL_PP(x);
  752. tbl_y[tbl_count] = Z_LVAL_PP(y);
  753. tbl_count++;
  754. }
  755. }
  756. int res = caca_draw_thin_polyline(canvas, tbl_x, tbl_y, tbl_count - 1);
  757. free(tbl_x);
  758. free(tbl_y);
  759. RETURN_SUCCESS(res);
  760. }
  761. PHP_FUNCTION(caca_draw_circle) {
  762. zval *_zval;
  763. long x, y, r, c;
  764. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &r, &c) == FAILURE) {
  765. RETURN_FALSE;
  766. }
  767. caca_canvas_t *canvas;
  768. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  769. RETURN_SUCCESS(caca_draw_circle(canvas, x, y, r, c));
  770. }
  771. PHP_FUNCTION(caca_draw_ellipse) {
  772. zval *_zval;
  773. long xa, ya, xb, yb, c = 0;
  774. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  775. RETURN_FALSE;
  776. }
  777. caca_canvas_t *canvas;
  778. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  779. RETURN_SUCCESS(caca_draw_ellipse(canvas, xa, ya, xb, yb, c));
  780. }
  781. PHP_FUNCTION(caca_draw_thin_ellipse) {
  782. zval *_zval;
  783. long xa, ya, xb, yb = 0;
  784. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  785. RETURN_FALSE;
  786. }
  787. caca_canvas_t *canvas;
  788. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  789. RETURN_SUCCESS(caca_draw_thin_ellipse(canvas, xa, ya, xb, yb));
  790. }
  791. PHP_FUNCTION(caca_fill_ellipse) {
  792. zval *_zval;
  793. long xa, ya, xb, yb, c;
  794. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  795. RETURN_FALSE;
  796. }
  797. caca_canvas_t *canvas;
  798. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  799. RETURN_SUCCESS(caca_fill_ellipse(canvas, xa, ya, xb, yb, c));
  800. }
  801. PHP_FUNCTION(caca_draw_box) {
  802. zval *_zval;
  803. long xa, ya, xb, yb, c;
  804. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  805. RETURN_FALSE;
  806. }
  807. caca_canvas_t *canvas;
  808. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  809. RETURN_SUCCESS(caca_draw_box(canvas, xa, ya, xb, yb, c));
  810. }
  811. PHP_FUNCTION(caca_draw_thin_box) {
  812. zval *_zval;
  813. long xa, ya, xb, yb = 0;
  814. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  815. RETURN_FALSE;
  816. }
  817. caca_canvas_t *canvas;
  818. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  819. RETURN_SUCCESS(caca_draw_thin_box(canvas, xa, ya, xb, yb));
  820. }
  821. PHP_FUNCTION(caca_draw_cp437_box) {
  822. zval *_zval;
  823. long xa, ya, xb, yb;
  824. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  825. RETURN_FALSE;
  826. }
  827. caca_canvas_t *canvas;
  828. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  829. RETURN_SUCCESS(caca_draw_cp437_box(canvas, xa, ya, xb, yb));
  830. }
  831. PHP_FUNCTION(caca_fill_box) {
  832. zval *_zval;
  833. long xa, ya, xb, yb, c;
  834. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  835. RETURN_FALSE;
  836. }
  837. caca_canvas_t *canvas;
  838. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  839. RETURN_SUCCESS(caca_fill_box(canvas, xa, ya, xb, yb, c));
  840. }
  841. PHP_FUNCTION(caca_draw_triangle) {
  842. zval *_zval;
  843. long xa, ya, xb, yb, xc, yc, c;
  844. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == FAILURE) {
  845. RETURN_FALSE;
  846. }
  847. caca_canvas_t *canvas;
  848. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  849. RETURN_SUCCESS(caca_draw_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  850. }
  851. PHP_FUNCTION(caca_draw_thin_triangle) {
  852. zval *_zval;
  853. long xa, ya, xb, yb, xc, yc = 0;
  854. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc) == FAILURE) {
  855. RETURN_FALSE;
  856. }
  857. caca_canvas_t *canvas;
  858. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  859. RETURN_SUCCESS(caca_draw_thin_triangle(canvas, xa, ya, xb, yb, xc, yc));
  860. }
  861. PHP_FUNCTION(caca_fill_triangle) {
  862. zval *_zval;
  863. long xa, ya, xb, yb, xc, yc, c = 0;
  864. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == FAILURE) {
  865. RETURN_FALSE;
  866. }
  867. caca_canvas_t *canvas;
  868. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  869. RETURN_SUCCESS(caca_fill_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  870. }
  871. PHP_FUNCTION(caca_get_frame_count) {
  872. zval *_zval;
  873. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  874. RETURN_FALSE;
  875. }
  876. caca_canvas_t *canvas;
  877. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  878. RETURN_LONG(caca_get_canvas_count(canvas));
  879. }
  880. PHP_FUNCTION(caca_set_frame) {
  881. zval *_zval;
  882. long id;
  883. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  884. RETURN_FALSE;
  885. }
  886. caca_canvas_t *canvas;
  887. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  888. RETURN_SUCCESS(caca_set_frame(canvas, id));
  889. }
  890. PHP_FUNCTION(caca_set_frame_name) {
  891. zval *_zval;
  892. char *str;
  893. long str_len;
  894. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  895. RETURN_FALSE;
  896. }
  897. caca_canvas_t *canvas;
  898. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  899. RETURN_SUCCESS(caca_set_frame_name(canvas, str));
  900. }
  901. PHP_FUNCTION(caca_create_frame) {
  902. zval *_zval;
  903. long id;
  904. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  905. RETURN_FALSE;
  906. }
  907. caca_canvas_t *canvas;
  908. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  909. RETURN_SUCCESS(caca_create_frame(canvas, id));
  910. }
  911. PHP_FUNCTION(caca_free_frame) {
  912. zval *_zval;
  913. long id;
  914. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  915. RETURN_FALSE;
  916. }
  917. caca_canvas_t *canvas;
  918. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  919. RETURN_SUCCESS(caca_free_frame(canvas, id));
  920. }
  921. PHP_FUNCTION(caca_create_dither) {
  922. zval *_zval;
  923. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  924. RETURN_FALSE;
  925. }
  926. #ifdef HAVE_GD
  927. gdImage *img = fetch_external_resource(_zval, "gd");
  928. if (!img) {
  929. RETURN_FALSE;
  930. }
  931. caca_dither_t *dither;
  932. if (img->trueColor)
  933. dither = caca_create_dither(sizeof(int) * 8, img->sx, img->sy, img->sx * sizeof(int), 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000);
  934. else
  935. dither = caca_create_dither(sizeof(char) * 8, img->sx, img->sy, img->sx * sizeof(char), 0, 0, 0, 0);
  936. if (!dither) {
  937. RETURN_FALSE;
  938. }
  939. ZEND_REGISTER_RESOURCE(return_value, dither, le_caca_dither);
  940. #else
  941. RETURN_FALSE
  942. #endif
  943. }
  944. PHP_FUNCTION(caca_set_dither_palette) {
  945. zval *zval_res, *arr;
  946. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &arr) == FAILURE) {
  947. RETURN_FALSE;
  948. }
  949. caca_dither_t *dither;
  950. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_dither);
  951. uint32_t tbl[4][256];
  952. zval **color, **value;
  953. int i, j;
  954. for (i = 0; i < 256; i++) {
  955. if (zend_hash_index_find(Z_ARRVAL_P(arr), i, (void**) &color) == FAILURE || Z_TYPE_P(*color) != IS_ARRAY) {
  956. RETURN_FALSE;
  957. }
  958. for (j = 0; j < 4; j++) {
  959. if (zend_hash_index_find(Z_ARRVAL_P(*color), j, (void**) &value) == FAILURE) {
  960. RETURN_FALSE;
  961. }
  962. convert_to_long_ex(value);
  963. tbl[j][i] = Z_LVAL_PP(value);
  964. }
  965. }
  966. RETURN_SUCCESS(caca_set_dither_palette(dither, tbl[0], tbl[1], tbl[2], tbl[3]));
  967. }
  968. PHP_FUNCTION(caca_set_dither_brightness) {
  969. zval *_zval;
  970. double value;
  971. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  972. RETURN_FALSE;
  973. }
  974. caca_dither_t *dither;
  975. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  976. RETURN_SUCCESS(caca_set_dither_brightness(dither, value));
  977. }
  978. PHP_FUNCTION(caca_get_dither_brightness) {
  979. zval *_zval;
  980. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  981. RETURN_FALSE;
  982. }
  983. caca_dither_t *dither;
  984. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  985. RETURN_DOUBLE(caca_get_dither_brightness(dither));
  986. }
  987. PHP_FUNCTION(caca_set_dither_gamma) {
  988. zval *_zval;
  989. double value;
  990. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  991. RETURN_FALSE;
  992. }
  993. caca_dither_t *dither;
  994. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  995. RETURN_SUCCESS(caca_set_dither_gamma(dither, value));
  996. }
  997. PHP_FUNCTION(caca_get_dither_gamma) {
  998. zval *_zval;
  999. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1000. RETURN_FALSE;
  1001. }
  1002. caca_dither_t *dither;
  1003. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1004. RETURN_DOUBLE(caca_get_dither_gamma(dither));
  1005. }
  1006. PHP_FUNCTION(caca_set_dither_contrast) {
  1007. zval *_zval;
  1008. double value;
  1009. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  1010. RETURN_FALSE;
  1011. }
  1012. caca_dither_t *dither;
  1013. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1014. RETURN_SUCCESS(caca_set_dither_contrast(dither, value));
  1015. }
  1016. PHP_FUNCTION(caca_get_dither_contrast) {
  1017. zval *_zval;
  1018. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1019. RETURN_FALSE;
  1020. }
  1021. caca_dither_t *dither;
  1022. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1023. RETURN_DOUBLE(caca_get_dither_contrast(dither));
  1024. }
  1025. PHP_FUNCTION(caca_set_dither_antialias) {
  1026. zval *_zval;
  1027. int str_len;
  1028. char *str;
  1029. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1030. RETURN_FALSE;
  1031. }
  1032. caca_dither_t *dither;
  1033. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1034. RETURN_SUCCESS(caca_set_dither_antialias(dither, str));
  1035. }
  1036. PHP_FUNCTION(caca_get_dither_antialias_list) {
  1037. zval *_zval;
  1038. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1039. RETURN_FALSE;
  1040. }
  1041. caca_dither_t *dither;
  1042. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1043. char const * const *list = caca_get_dither_antialias_list(dither);
  1044. int i;
  1045. array_init(return_value);
  1046. for(i = 0; list[i]; i += 1)
  1047. add_next_index_string(return_value, (char*) list[i], 1);
  1048. }
  1049. PHP_FUNCTION(caca_get_dither_antialias) {
  1050. zval *_zval;
  1051. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1052. RETURN_FALSE;
  1053. }
  1054. caca_dither_t *dither;
  1055. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1056. RETURN_STRING((char *) caca_get_dither_antialias(dither), 1);
  1057. }
  1058. PHP_FUNCTION(caca_set_dither_color) {
  1059. zval *_zval;
  1060. int str_len;
  1061. char *str;
  1062. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1063. RETURN_FALSE;
  1064. }
  1065. caca_dither_t *dither;
  1066. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1067. RETURN_SUCCESS(caca_set_dither_color(dither, str));
  1068. }
  1069. PHP_FUNCTION(caca_get_dither_color_list) {
  1070. zval *_zval;
  1071. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1072. RETURN_FALSE;
  1073. }
  1074. caca_dither_t *dither;
  1075. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1076. char const * const *list = caca_get_dither_antialias_list(dither);
  1077. int i;
  1078. array_init(return_value);
  1079. for(i = 0; list[i]; i += 2)
  1080. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1081. }
  1082. PHP_FUNCTION(caca_get_dither_color) {
  1083. zval *_zval;
  1084. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1085. RETURN_FALSE;
  1086. }
  1087. caca_dither_t *dither;
  1088. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1089. RETURN_STRING((char *) caca_get_dither_color(dither), 1);
  1090. }
  1091. PHP_FUNCTION(caca_set_dither_charset) {
  1092. zval *_zval;
  1093. int str_len;
  1094. char *str;
  1095. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1096. RETURN_FALSE;
  1097. }
  1098. caca_dither_t *dither;
  1099. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1100. RETURN_SUCCESS(caca_set_dither_charset(dither, str));
  1101. }
  1102. PHP_FUNCTION(caca_get_dither_charset_list) {
  1103. zval *_zval;
  1104. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1105. RETURN_FALSE;
  1106. }
  1107. caca_dither_t *dither;
  1108. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1109. char const * const *list = caca_get_dither_charset_list(dither);
  1110. int i;
  1111. array_init(return_value);
  1112. for(i = 0; list[i]; i += 2)
  1113. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1114. }
  1115. PHP_FUNCTION(caca_get_dither_charset) {
  1116. zval *_zval;
  1117. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1118. RETURN_FALSE;
  1119. }
  1120. caca_dither_t *dither;
  1121. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1122. RETURN_STRING((char *) caca_get_dither_charset(dither), 1);
  1123. }
  1124. PHP_FUNCTION(caca_set_dither_algorithm) {
  1125. zval *_zval;
  1126. int str_len;
  1127. char *str;
  1128. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1129. RETURN_FALSE;
  1130. }
  1131. caca_dither_t *dither;
  1132. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1133. RETURN_SUCCESS(caca_set_dither_algorithm(dither, str));
  1134. }
  1135. PHP_FUNCTION(caca_get_dither_algorithm_list) {
  1136. zval *_zval;
  1137. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1138. RETURN_FALSE;
  1139. }
  1140. caca_dither_t *dither;
  1141. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1142. char const * const *list = caca_get_dither_algorithm_list(dither);
  1143. int i;
  1144. array_init(return_value);
  1145. for(i = 0; list[i]; i += 2)
  1146. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1147. }
  1148. PHP_FUNCTION(caca_get_dither_algorithm) {
  1149. zval *_zval;
  1150. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1151. RETURN_FALSE;
  1152. }
  1153. caca_dither_t *dither;
  1154. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1155. RETURN_STRING((char *) caca_get_dither_algorithm(dither), 1);
  1156. }
  1157. PHP_FUNCTION(caca_dither_bitmap) {
  1158. zval *_zval1, *_zval2, *_zval3;
  1159. long x, y, w, h = 0;
  1160. zend_bool load_palette = 1;
  1161. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllrr|b", &_zval1, &x, &y, &w, &h, &_zval2, &_zval3, &load_palette) == FAILURE) {
  1162. RETURN_FALSE;
  1163. }
  1164. caca_canvas_t *canvas;
  1165. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1166. caca_dither_t *dither;
  1167. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval2, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1168. #ifdef HAVE_GD
  1169. gdImage *img = fetch_external_resource(_zval3, "gd");
  1170. if (!img) {
  1171. RETURN_FALSE;
  1172. }
  1173. void *pixels = gd_get_pixels(img);
  1174. if (!pixels) {
  1175. RETURN_FALSE;
  1176. }
  1177. //load palette if image is not true color
  1178. if (load_palette && !img->trueColor && gd_load_palette(img, dither) != 0) {
  1179. RETURN_FALSE;
  1180. }
  1181. caca_dither_bitmap(canvas, x, y, w, h, dither, pixels);
  1182. free(pixels);
  1183. RETURN_TRUE;
  1184. #else
  1185. RETURN_FALSE;
  1186. #endif
  1187. }
  1188. PHP_FUNCTION(caca_load_font) {
  1189. char *str;
  1190. long str_len;
  1191. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1192. RETURN_FALSE;
  1193. }
  1194. caca_font_t *font = caca_load_font(str, str_len);
  1195. if (!font) {
  1196. RETURN_FALSE;
  1197. }
  1198. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1199. }
  1200. PHP_FUNCTION(caca_load_builtin_font) {
  1201. char *str;
  1202. long str_len;
  1203. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1204. RETURN_FALSE;
  1205. }
  1206. caca_font_t *font = caca_load_font(str, 0);
  1207. if (!font) {
  1208. RETURN_FALSE;
  1209. }
  1210. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1211. }
  1212. PHP_FUNCTION(caca_get_font_list) {
  1213. if (ZEND_NUM_ARGS() != 0) {
  1214. WRONG_PARAM_COUNT;
  1215. }
  1216. char const * const *list = caca_get_font_list();
  1217. int i;
  1218. array_init(return_value);
  1219. for(i = 0; list[i]; i += 1)
  1220. add_next_index_string(return_value, (char*) list[i], 1);
  1221. }
  1222. PHP_FUNCTION(caca_get_font_width) {
  1223. zval *_zval;
  1224. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1225. RETURN_FALSE;
  1226. }
  1227. caca_font_t *font;
  1228. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1229. RETURN_LONG(caca_get_font_width(font));
  1230. }
  1231. PHP_FUNCTION(caca_get_font_height) {
  1232. zval *_zval;
  1233. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1234. RETURN_FALSE;
  1235. }
  1236. caca_font_t *font;
  1237. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1238. RETURN_LONG(caca_get_font_height(font));
  1239. }
  1240. PHP_FUNCTION(caca_get_font_blocks) {
  1241. zval *_zval;
  1242. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1243. RETURN_FALSE;
  1244. }
  1245. caca_font_t *font;
  1246. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1247. uint32_t const *list = caca_get_font_blocks(font);
  1248. int i;
  1249. array_init(return_value);
  1250. for(i = 0; list[i]; i += 1)
  1251. add_next_index_long(return_value, list[i]);
  1252. }
  1253. PHP_FUNCTION(caca_render_canvas) {
  1254. zval *_zval1, *_zval2, *_zval3;
  1255. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrr", &_zval1, &_zval2, &_zval3) == FAILURE) {
  1256. RETURN_FALSE;
  1257. }
  1258. caca_canvas_t *canvas;
  1259. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1260. caca_font_t *font;
  1261. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval2, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1262. #ifdef HAVE_GD
  1263. gdImage *img = fetch_external_resource(_zval3, "gd");
  1264. if (!img || !img->trueColor) {
  1265. RETURN_FALSE;
  1266. }
  1267. int pitch = img->sx * 4;
  1268. uint8_t *buffer = malloc(pitch * img->sy);
  1269. if (!buffer) {
  1270. RETURN_FALSE;
  1271. }
  1272. caca_render_canvas(canvas, font, (void *) buffer, img->sx, img->sy, pitch);
  1273. int i, j;
  1274. for (i = 0; i < img->sy; i++) {
  1275. for (j = 0; j < img->sx; j++) {
  1276. uint8_t *src = buffer + i * pitch + j * 4;
  1277. img->tpixels[i][j] = *(src + 3) | (*(src + 2) << 8) | (*(src + 1) << 16) | (*(src + 0) << 24);
  1278. }
  1279. }
  1280. free(buffer);
  1281. RETURN_TRUE;
  1282. #else
  1283. RETURN_FALSE;
  1284. #endif
  1285. }
  1286. PHP_FUNCTION(caca_canvas_set_figfont) {
  1287. zval *_zval;
  1288. char *font;
  1289. long font_len;
  1290. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &font, &font_len) == FAILURE) {
  1291. RETURN_FALSE;
  1292. }
  1293. caca_canvas_t *canvas;
  1294. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1295. RETURN_SUCCESS(caca_canvas_set_figfont(canvas, font));
  1296. }
  1297. PHP_FUNCTION(caca_put_figchar) {
  1298. zval *_zval;
  1299. long c;
  1300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &c) == FAILURE) {
  1301. RETURN_FALSE;
  1302. }
  1303. caca_canvas_t *canvas;
  1304. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1305. RETURN_SUCCESS(caca_put_figchar(canvas, c));
  1306. }
  1307. PHP_FUNCTION(caca_flush_figlet) {
  1308. zval *_zval;
  1309. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1310. RETURN_FALSE;
  1311. }
  1312. caca_canvas_t *canvas;
  1313. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1314. RETURN_SUCCESS(caca_flush_figlet(canvas));
  1315. }
  1316. PHP_FUNCTION(caca_file_open) {
  1317. char *path, *mode;
  1318. long path_len, mode_len;
  1319. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) {
  1320. RETURN_FALSE;
  1321. }
  1322. caca_file_t *file = caca_file_open(path, mode);
  1323. if (!file) {
  1324. RETURN_FALSE;
  1325. }
  1326. ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file);
  1327. }
  1328. PHP_FUNCTION(caca_file_close) {
  1329. zval *_zval;
  1330. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1331. RETURN_FALSE;
  1332. }
  1333. caca_file_t *file;
  1334. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1335. int res = caca_file_close(file);
  1336. //Delete php resource
  1337. zend_list_delete(_zval->value.lval);
  1338. RETURN_SUCCESS(res);
  1339. }
  1340. PHP_FUNCTION(caca_file_tell) {
  1341. zval *_zval;
  1342. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1343. RETURN_FALSE;
  1344. }
  1345. caca_file_t *file;
  1346. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1347. RETURN_LONG(caca_file_tell(file));
  1348. }
  1349. PHP_FUNCTION(caca_file_read) {
  1350. zval *_zval;
  1351. long len;
  1352. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1353. RETURN_FALSE;
  1354. }
  1355. caca_file_t *file;
  1356. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1357. if (len < 1) {
  1358. RETURN_FALSE;
  1359. }
  1360. char *buffer = emalloc(len);
  1361. if (!buffer) {
  1362. RETURN_FALSE;
  1363. }
  1364. caca_file_read(file, buffer, len);
  1365. return_value->type = IS_STRING;
  1366. return_value->value.str.len = len;
  1367. return_value->value.str.val = buffer;
  1368. }
  1369. PHP_FUNCTION(caca_file_write) {
  1370. zval *_zval;
  1371. char *buf;
  1372. long buf_len = 0;
  1373. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &buf, &buf_len) == FAILURE) {
  1374. RETURN_FALSE;
  1375. }
  1376. caca_file_t *file;
  1377. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1378. RETURN_LONG(caca_file_write(file, buf, buf_len));
  1379. }
  1380. PHP_FUNCTION(caca_file_gets) {
  1381. zval *_zval;
  1382. long len = 0;
  1383. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1384. RETURN_FALSE;
  1385. }
  1386. caca_file_t *file;
  1387. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1388. if (len < 1) {
  1389. RETURN_FALSE;
  1390. }
  1391. char *buffer = emalloc(len);
  1392. if (!buffer) {
  1393. RETURN_FALSE;
  1394. }
  1395. char *result = caca_file_gets(file, buffer, len);
  1396. if (!result) {
  1397. RETURN_FALSE;
  1398. }
  1399. return_value->type = IS_STRING;
  1400. return_value->value.str.len = len;
  1401. return_value->value.str.val = result;
  1402. }
  1403. PHP_FUNCTION(caca_file_eof) {
  1404. zval *_zval;
  1405. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1406. RETURN_FALSE;
  1407. }
  1408. caca_file_t *file;
  1409. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1410. RETURN_BOOL(caca_file_eof(file) != 0);
  1411. }
  1412. PHP_FUNCTION(caca_import_string) {
  1413. zval *_zval;
  1414. char *src, *format;
  1415. long src_len, format_len = 0;
  1416. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &format, &format_len) == FAILURE) {
  1417. RETURN_FALSE;
  1418. }
  1419. caca_canvas_t *canvas;
  1420. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1421. RETURN_LONG(caca_import_memory(canvas, src, src_len, format));
  1422. }
  1423. PHP_FUNCTION(caca_import_file) {
  1424. zval *_zval;
  1425. char *filename, *format;
  1426. long filename_len, format_len = 0;
  1427. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &filename, &filename_len, &format, &format_len) == FAILURE) {
  1428. RETURN_FALSE;
  1429. }
  1430. caca_canvas_t *canvas;
  1431. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1432. RETURN_LONG(caca_import_file(canvas, filename, format));
  1433. }
  1434. PHP_FUNCTION(caca_get_import_list) {
  1435. if (ZEND_NUM_ARGS() != 0) {
  1436. WRONG_PARAM_COUNT;
  1437. }
  1438. char const * const *list = caca_get_import_list();
  1439. int i;
  1440. array_init(return_value);
  1441. for(i = 0; list[i]; i += 2)
  1442. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1443. }
  1444. PHP_FUNCTION(caca_export_string) {
  1445. zval *_zval;
  1446. char *type;
  1447. long type_len;
  1448. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &type, &type_len) == FAILURE) {
  1449. RETURN_FALSE;
  1450. }
  1451. caca_canvas_t *canvas;
  1452. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1453. void *buffer, *copy;
  1454. size_t len;
  1455. buffer = caca_export_memory(canvas, type, &len);
  1456. copy = emalloc(len);
  1457. if (!buffer || !copy) {
  1458. RETURN_FALSE;
  1459. }
  1460. memcpy(copy, buffer, len);
  1461. free(buffer);
  1462. return_value->type = IS_STRING;
  1463. return_value->value.str.len = len;
  1464. return_value->value.str.val = copy;
  1465. }
  1466. PHP_FUNCTION(caca_get_export_list) {
  1467. if (ZEND_NUM_ARGS() != 0) {
  1468. WRONG_PARAM_COUNT;
  1469. }
  1470. char const * const *list = caca_get_export_list();
  1471. int i;
  1472. array_init(return_value);
  1473. for(i = 0; list[i]; i += 2)
  1474. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1475. }
  1476. PHP_FUNCTION(caca_create_display) {
  1477. caca_canvas_t *canvas;
  1478. FETCH_CANVAS(canvas);
  1479. caca_display_t *display = caca_create_display(canvas);
  1480. if (!display) {
  1481. RETURN_FALSE;
  1482. }
  1483. ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
  1484. }
  1485. PHP_FUNCTION(caca_create_display_with_driver) {
  1486. zval *_zval;
  1487. char *str;
  1488. long str_len = 0;
  1489. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1490. RETURN_FALSE;
  1491. }
  1492. caca_canvas_t *canvas;
  1493. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1494. caca_display_t *display = caca_create_display_with_driver(canvas, str);
  1495. if (!display) {
  1496. RETURN_FALSE;
  1497. }
  1498. ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
  1499. }
  1500. PHP_FUNCTION(caca_get_display_driver_list) {
  1501. if (ZEND_NUM_ARGS() != 0) {
  1502. WRONG_PARAM_COUNT;
  1503. }
  1504. char const * const *list = caca_get_display_driver_list();
  1505. int i;
  1506. array_init(return_value);
  1507. for(i = 0; list[i]; i += 2)
  1508. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1509. }
  1510. PHP_FUNCTION(caca_get_display_driver) {
  1511. zval *_zval;
  1512. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1513. RETURN_FALSE;
  1514. }
  1515. caca_display_t *display;
  1516. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1517. RETURN_STRING((char *) caca_get_display_driver(display), 1);
  1518. }
  1519. PHP_FUNCTION(caca_set_display_driver) {
  1520. zval *_zval;
  1521. char *str;
  1522. long str_len;
  1523. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1524. RETURN_FALSE;
  1525. }
  1526. caca_display_t *display;
  1527. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1528. RETURN_SUCESS(caca_set_display_driver(display, str));
  1529. }
  1530. PHP_FUNCTION(caca_get_canvas) {
  1531. zval *_zval;
  1532. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1533. RETURN_FALSE;
  1534. }
  1535. caca_display_t *display;
  1536. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1537. caca_canvas_t *canvas = caca_get_canvas(display);
  1538. if (!canvas) {
  1539. RETURN_FALSE;
  1540. }
  1541. ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
  1542. }
  1543. PHP_FUNCTION(caca_refresh_display) {
  1544. caca_display_t *display;
  1545. FETCH_DISPLAY(display);
  1546. RETURN_SUCCESS(caca_refresh_display(display));
  1547. }
  1548. PHP_FUNCTION(caca_set_display_time) {
  1549. zval *_zval;
  1550. long value = 0;
  1551. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1552. RETURN_FALSE;
  1553. }
  1554. caca_display_t *display;
  1555. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1556. RETURN_SUCCESS(caca_set_display_time(display, value));
  1557. }
  1558. PHP_FUNCTION(caca_get_display_time) {
  1559. caca_display_t *display;
  1560. FETCH_DISPLAY(display);
  1561. RETURN_LONG(caca_get_display_time(display));
  1562. }
  1563. PHP_FUNCTION(caca_get_display_width) {
  1564. caca_display_t *display;
  1565. FETCH_DISPLAY(display);
  1566. RETURN_LONG(caca_get_display_width(display));
  1567. }
  1568. PHP_FUNCTION(caca_get_display_height) {
  1569. caca_display_t *display;
  1570. FETCH_DISPLAY(display);
  1571. RETURN_LONG(caca_get_display_height(display));
  1572. }
  1573. PHP_FUNCTION(caca_set_display_title) {
  1574. zval *_zval;
  1575. char *str;
  1576. long str_len = 0;
  1577. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1578. RETURN_FALSE;
  1579. }
  1580. caca_display_t *display;
  1581. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1582. RETURN_SUCCESS(caca_set_display(display, str));
  1583. }
  1584. PHP_FUNCTION(caca_set_mouse) {
  1585. zval *_zval;
  1586. long value = 0;
  1587. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1588. RETURN_FALSE;
  1589. }
  1590. caca_display_t *display;
  1591. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1592. RETURN_SUCCESS(caca_set_mouse(display, value));
  1593. }
  1594. PHP_FUNCTION(caca_set_cursor) {
  1595. zval *_zval;
  1596. long value = 0;
  1597. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1598. RETURN_FALSE;
  1599. }
  1600. caca_display_t *display;
  1601. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1602. RETURN_SUCCESS(caca_set_cursor(display, value));
  1603. }
  1604. PHP_FUNCTION(caca_get_event) {
  1605. zval *_zval = NULL;
  1606. long g, aa = 0;
  1607. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &_zval, &g, &aa) == FAILURE) {
  1608. RETURN_FALSE;
  1609. }
  1610. caca_display_t *display;
  1611. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1612. caca_event_t *event = emalloc(sizeof(caca_event_t));
  1613. if (!event) {
  1614. RETURN_FALSE;
  1615. }
  1616. caca_get_event(display, g, event, aa);
  1617. if (caca_get_event_type(event) == CACA_EVENT_NONE) {
  1618. efree(event);
  1619. RETURN_FALSE;
  1620. }
  1621. ZEND_REGISTER_RESOURCE(return_value, event, le_caca_event);
  1622. }
  1623. PHP_FUNCTION(caca_get_mouse_x) {
  1624. caca_display_t *display;
  1625. FETCH_DISPLAY(display);
  1626. RETURN_LONG(caca_get_mouse_x(display));
  1627. }
  1628. PHP_FUNCTION(caca_get_mouse_y) {
  1629. caca_display_t *display;
  1630. FETCH_DISPLAY(display);
  1631. RETURN_LONG(caca_get_mouse_y(display));
  1632. }
  1633. PHP_FUNCTION(caca_get_event_type) {
  1634. caca_event_t *event;
  1635. FETCH_EVENT(event);
  1636. RETURN_LONG(caca_get_event_type(event));
  1637. }
  1638. PHP_FUNCTION(caca_get_event_key_ch) {
  1639. caca_event_t *event;
  1640. FETCH_EVENT(event);
  1641. RETURN_LONG(caca_get_event_key_ch(event));
  1642. }
  1643. PHP_FUNCTION(caca_get_event_mouse_button) {
  1644. caca_event_t *event;
  1645. FETCH_EVENT(event);
  1646. RETURN_LONG(caca_get_event_mouse_button(event));
  1647. }
  1648. PHP_FUNCTION(caca_get_event_mouse_x) {
  1649. caca_event_t *event;
  1650. FETCH_EVENT(event);
  1651. RETURN_LONG(caca_get_event_mouse_x(event));
  1652. }
  1653. PHP_FUNCTION(caca_get_event_mouse_y) {
  1654. caca_event_t *event;
  1655. FETCH_EVENT(event);
  1656. RETURN_LONG(caca_get_event_mouse_y(event));
  1657. }
  1658. PHP_FUNCTION(caca_get_event_resize_width) {
  1659. caca_event_t *event;
  1660. FETCH_EVENT(event);
  1661. RETURN_LONG(caca_get_event_resize_width(event));
  1662. }
  1663. PHP_FUNCTION(caca_get_event_resize_height) {
  1664. caca_event_t *event;
  1665. FETCH_EVENT(event);
  1666. RETURN_LONG(caca_get_event_resize_height(event));
  1667. }