25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

1881 satır
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. #ifdef HAVE_GD
  178. php_info_print_table_row(2, "Gd support", "enabled");
  179. php_info_print_table_row(2, "Gd API version", GD_VERSION_STRING);
  180. #else
  181. php_info_print_table_row(2, "Compiled with gd support", "disabled");
  182. #endif
  183. php_info_print_table_end();
  184. }
  185. //-------- Caca's ressources destructors -----------//
  186. static void php_caca_canvas_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  187. caca_free_canvas(rsrc->ptr);
  188. }
  189. static void php_caca_dither_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  190. caca_free_dither(rsrc->ptr);
  191. }
  192. static void php_caca_font_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  193. caca_free_font(rsrc->ptr);
  194. }
  195. static void php_caca_file_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  196. caca_file_close(rsrc->ptr);
  197. }
  198. static void php_caca_display_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  199. caca_free_display(rsrc->ptr);
  200. }
  201. static void php_caca_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
  202. efree((caca_event_t *) rsrc->ptr);
  203. }
  204. //-------- Initialization ---------//
  205. PHP_MINIT_FUNCTION(caca) {
  206. le_caca_canvas = zend_register_list_destructors_ex(php_caca_canvas_dtor, NULL, PHP_CACA_CANVAS_RES_NAME, module_number);
  207. le_caca_dither = zend_register_list_destructors_ex(php_caca_dither_dtor, NULL, PHP_CACA_DITHER_RES_NAME, module_number);
  208. le_caca_font = zend_register_list_destructors_ex(php_caca_font_dtor, NULL, PHP_CACA_FONT_RES_NAME, module_number);
  209. le_caca_file = zend_register_list_destructors_ex(php_caca_file_dtor, NULL, PHP_CACA_FILE_RES_NAME, module_number);
  210. le_caca_display = zend_register_list_destructors_ex(php_caca_display_dtor, NULL, PHP_CACA_DISPLAY_RES_NAME, module_number);
  211. le_caca_event = zend_register_list_destructors_ex(php_caca_event_dtor, NULL, PHP_CACA_EVENT_RES_NAME, module_number);
  212. REGISTER_LONG_CONSTANT("CACA_BLACK", CACA_BLACK, CONST_CS | CONST_PERSISTENT);
  213. REGISTER_LONG_CONSTANT("CACA_BLUE", CACA_BLUE, CONST_CS | CONST_PERSISTENT);
  214. REGISTER_LONG_CONSTANT("CACA_GREEN", CACA_GREEN, CONST_CS | CONST_PERSISTENT);
  215. REGISTER_LONG_CONSTANT("CACA_CYAN", CACA_CYAN, CONST_CS | CONST_PERSISTENT);
  216. REGISTER_LONG_CONSTANT("CACA_RED", CACA_RED, CONST_CS | CONST_PERSISTENT);
  217. REGISTER_LONG_CONSTANT("CACA_MAGENTA", CACA_MAGENTA, CONST_CS | CONST_PERSISTENT);
  218. REGISTER_LONG_CONSTANT("CACA_BROWN", CACA_BROWN, CONST_CS | CONST_PERSISTENT);
  219. REGISTER_LONG_CONSTANT("CACA_LIGHTGRAY", CACA_LIGHTGRAY, CONST_CS | CONST_PERSISTENT);
  220. REGISTER_LONG_CONSTANT("CACA_DARKGRAY", CACA_DARKGRAY, CONST_CS | CONST_PERSISTENT);
  221. REGISTER_LONG_CONSTANT("CACA_LIGHTBLUE", CACA_LIGHTBLUE, CONST_CS | CONST_PERSISTENT);
  222. REGISTER_LONG_CONSTANT("CACA_LIGHTGREEN", CACA_LIGHTGREEN, CONST_CS | CONST_PERSISTENT);
  223. REGISTER_LONG_CONSTANT("CACA_LIGHTCYAN", CACA_LIGHTCYAN, CONST_CS | CONST_PERSISTENT);
  224. REGISTER_LONG_CONSTANT("CACA_LIGHTRED", CACA_LIGHTRED, CONST_CS | CONST_PERSISTENT);
  225. REGISTER_LONG_CONSTANT("CACA_LIGHTMAGENTA", CACA_LIGHTMAGENTA, CONST_CS | CONST_PERSISTENT);
  226. REGISTER_LONG_CONSTANT("CACA_YELLOW", CACA_YELLOW, CONST_CS | CONST_PERSISTENT);
  227. REGISTER_LONG_CONSTANT("CACA_WHITE", CACA_WHITE, CONST_CS | CONST_PERSISTENT);
  228. REGISTER_LONG_CONSTANT("CACA_DEFAULT", CACA_DEFAULT, CONST_CS | CONST_PERSISTENT);
  229. REGISTER_LONG_CONSTANT("CACA_TRANSPARENT", CACA_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
  230. REGISTER_LONG_CONSTANT("CACA_BOLD", CACA_BOLD, CONST_CS | CONST_PERSISTENT);
  231. REGISTER_LONG_CONSTANT("CACA_ITALICS", CACA_ITALICS, CONST_CS | CONST_PERSISTENT);
  232. REGISTER_LONG_CONSTANT("CACA_UNDERLINE", CACA_UNDERLINE, CONST_CS | CONST_PERSISTENT);
  233. REGISTER_LONG_CONSTANT("CACA_BLINK", CACA_BLINK, CONST_CS | CONST_PERSISTENT);
  234. REGISTER_LONG_CONSTANT("CACA_EVENT_NONE", CACA_EVENT_NONE, CONST_CS | CONST_PERSISTENT);
  235. REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_PRESS", CACA_EVENT_KEY_PRESS, CONST_CS | CONST_PERSISTENT);
  236. REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_RELEASE", CACA_EVENT_KEY_RELEASE, CONST_CS | CONST_PERSISTENT);
  237. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_PRESS", CACA_EVENT_MOUSE_PRESS, CONST_CS | CONST_PERSISTENT);
  238. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_RELEASE", CACA_EVENT_MOUSE_RELEASE, CONST_CS | CONST_PERSISTENT);
  239. REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_MOTION", CACA_EVENT_MOUSE_MOTION, CONST_CS | CONST_PERSISTENT);
  240. REGISTER_LONG_CONSTANT("CACA_EVENT_RESIZE", CACA_EVENT_RESIZE, CONST_CS | CONST_PERSISTENT);
  241. REGISTER_LONG_CONSTANT("CACA_EVENT_QUIT", CACA_EVENT_QUIT, CONST_CS | CONST_PERSISTENT);
  242. REGISTER_LONG_CONSTANT("CACA_EVENT_ANY", CACA_EVENT_ANY, CONST_CS | CONST_PERSISTENT);
  243. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_A", CACA_KEY_CTRL_A, CONST_CS | CONST_PERSISTENT);
  244. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_B", CACA_KEY_CTRL_B, CONST_CS | CONST_PERSISTENT);
  245. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_C", CACA_KEY_CTRL_C, CONST_CS | CONST_PERSISTENT);
  246. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_D", CACA_KEY_CTRL_D, CONST_CS | CONST_PERSISTENT);
  247. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_E", CACA_KEY_CTRL_E, CONST_CS | CONST_PERSISTENT);
  248. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_F", CACA_KEY_CTRL_F, CONST_CS | CONST_PERSISTENT);
  249. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_G", CACA_KEY_CTRL_G, CONST_CS | CONST_PERSISTENT);
  250. REGISTER_LONG_CONSTANT("CACA_KEY_BACKSPACE", CACA_KEY_BACKSPACE, CONST_CS | CONST_PERSISTENT);
  251. REGISTER_LONG_CONSTANT("CACA_KEY_TAB", CACA_KEY_TAB, CONST_CS | CONST_PERSISTENT);
  252. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_J", CACA_KEY_CTRL_J, CONST_CS | CONST_PERSISTENT);
  253. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_K", CACA_KEY_CTRL_K, CONST_CS | CONST_PERSISTENT);
  254. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_L", CACA_KEY_CTRL_L, CONST_CS | CONST_PERSISTENT);
  255. REGISTER_LONG_CONSTANT("CACA_KEY_RETURN", CACA_KEY_RETURN, CONST_CS | CONST_PERSISTENT);
  256. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_N", CACA_KEY_CTRL_N, CONST_CS | CONST_PERSISTENT);
  257. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_O", CACA_KEY_CTRL_O, CONST_CS | CONST_PERSISTENT);
  258. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_P", CACA_KEY_CTRL_P, CONST_CS | CONST_PERSISTENT);
  259. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Q", CACA_KEY_CTRL_Q, CONST_CS | CONST_PERSISTENT);
  260. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_R", CACA_KEY_CTRL_R, CONST_CS | CONST_PERSISTENT);
  261. REGISTER_LONG_CONSTANT("CACA_KEY_PAUSE", CACA_KEY_PAUSE, CONST_CS | CONST_PERSISTENT);
  262. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_T", CACA_KEY_CTRL_T, CONST_CS | CONST_PERSISTENT);
  263. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_U", CACA_KEY_CTRL_U, CONST_CS | CONST_PERSISTENT);
  264. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_V", CACA_KEY_CTRL_V, CONST_CS | CONST_PERSISTENT);
  265. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_W", CACA_KEY_CTRL_W, CONST_CS | CONST_PERSISTENT);
  266. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_X", CACA_KEY_CTRL_X, CONST_CS | CONST_PERSISTENT);
  267. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Y", CACA_KEY_CTRL_Y, CONST_CS | CONST_PERSISTENT);
  268. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Z", CACA_KEY_CTRL_Z, CONST_CS | CONST_PERSISTENT);
  269. REGISTER_LONG_CONSTANT("CACA_KEY_ESCAPE", CACA_KEY_ESCAPE, CONST_CS | CONST_PERSISTENT);
  270. REGISTER_LONG_CONSTANT("CACA_KEY_DELETE", CACA_KEY_DELETE, CONST_CS | CONST_PERSISTENT);
  271. REGISTER_LONG_CONSTANT("CACA_KEY_UP", CACA_KEY_UP, CONST_CS | CONST_PERSISTENT);
  272. REGISTER_LONG_CONSTANT("CACA_KEY_DOWN", CACA_KEY_DOWN, CONST_CS | CONST_PERSISTENT);
  273. REGISTER_LONG_CONSTANT("CACA_KEY_LEFT", CACA_KEY_LEFT, CONST_CS | CONST_PERSISTENT);
  274. REGISTER_LONG_CONSTANT("CACA_KEY_RIGHT", CACA_KEY_RIGHT, CONST_CS | CONST_PERSISTENT);
  275. REGISTER_LONG_CONSTANT("CACA_KEY_INSERT", CACA_KEY_INSERT, CONST_CS | CONST_PERSISTENT);
  276. REGISTER_LONG_CONSTANT("CACA_KEY_HOME", CACA_KEY_HOME, CONST_CS | CONST_PERSISTENT);
  277. REGISTER_LONG_CONSTANT("CACA_KEY_END", CACA_KEY_END, CONST_CS | CONST_PERSISTENT);
  278. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEUP", CACA_KEY_PAGEUP, CONST_CS | CONST_PERSISTENT);
  279. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEDOWN", CACA_KEY_PAGEDOWN, CONST_CS | CONST_PERSISTENT);
  280. REGISTER_LONG_CONSTANT("CACA_KEY_F1", CACA_KEY_F1, CONST_CS | CONST_PERSISTENT);
  281. REGISTER_LONG_CONSTANT("CACA_KEY_F2", CACA_KEY_F2, CONST_CS | CONST_PERSISTENT);
  282. REGISTER_LONG_CONSTANT("CACA_KEY_F3", CACA_KEY_F3, CONST_CS | CONST_PERSISTENT);
  283. REGISTER_LONG_CONSTANT("CACA_KEY_F4", CACA_KEY_F4, CONST_CS | CONST_PERSISTENT);
  284. REGISTER_LONG_CONSTANT("CACA_KEY_F5", CACA_KEY_F5, CONST_CS | CONST_PERSISTENT);
  285. REGISTER_LONG_CONSTANT("CACA_KEY_F6", CACA_KEY_F6, CONST_CS | CONST_PERSISTENT);
  286. REGISTER_LONG_CONSTANT("CACA_KEY_F7", CACA_KEY_F7, CONST_CS | CONST_PERSISTENT);
  287. REGISTER_LONG_CONSTANT("CACA_KEY_F8", CACA_KEY_F8, CONST_CS | CONST_PERSISTENT);
  288. REGISTER_LONG_CONSTANT("CACA_KEY_F9", CACA_KEY_F9, CONST_CS | CONST_PERSISTENT);
  289. REGISTER_LONG_CONSTANT("CACA_KEY_F10", CACA_KEY_F10, CONST_CS | CONST_PERSISTENT);
  290. REGISTER_LONG_CONSTANT("CACA_KEY_F11", CACA_KEY_F11, CONST_CS | CONST_PERSISTENT);
  291. REGISTER_LONG_CONSTANT("CACA_KEY_F12", CACA_KEY_F12, CONST_CS | CONST_PERSISTENT);
  292. REGISTER_LONG_CONSTANT("CACA_KEY_F13", CACA_KEY_F13, CONST_CS | CONST_PERSISTENT);
  293. REGISTER_LONG_CONSTANT("CACA_KEY_F14", CACA_KEY_F14, CONST_CS | CONST_PERSISTENT);
  294. REGISTER_LONG_CONSTANT("CACA_KEY_F15", CACA_KEY_F15, CONST_CS | CONST_PERSISTENT);
  295. return SUCCESS;
  296. }
  297. //---------- Some usefull macros ---------------//
  298. #define FETCH_CANVAS(canvas) \
  299. zval *_zval; \
  300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  301. RETURN_FALSE; \
  302. } \
  303. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  304. #define FETCH_DISPLAY(display) \
  305. zval *_zval; \
  306. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  307. RETURN_FALSE; \
  308. } \
  309. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  310. #define FETCH_EVENT(event) \
  311. zval *_zval; \
  312. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  313. RETURN_FALSE; \
  314. } \
  315. ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event);
  316. #define FETCH_LONG(l) \
  317. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &l) == FAILURE) { \
  318. RETURN_FALSE; \
  319. }
  320. #define RETURN_CHAR(c) \
  321. char *str = emalloc(2); \
  322. str[0] = c; \
  323. str[1] = '\0'; \
  324. RETURN_STRING(str, 0);
  325. #define RETURN_SUCCESS(i) \
  326. RETURN_BOOL((i) == 0);
  327. //---------- Some usefull functions --------------------//
  328. //Fetch external php resources such as gd resouces
  329. void *fetch_external_resource(zval *_zval, char const *type_name) {
  330. int resource_id = _zval->value.lval;
  331. int resource_type;
  332. void *result = zend_list_find(resource_id, &resource_type);
  333. if (!result)
  334. return NULL;
  335. char *resource_type_name = zend_rsrc_list_get_rsrc_type(resource_id);
  336. return (strcmp(resource_type_name, type_name) == 0) ? result : NULL;
  337. }
  338. //Fetch buffer of pixels from gdImage
  339. #ifdef HAVE_GD
  340. void *gd_get_pixels(gdImage *img) {
  341. void *result;
  342. int j, pitch;
  343. if (img->trueColor) {
  344. pitch = img->sx * sizeof(int);
  345. result = malloc(img->sy * pitch);
  346. for (j = 0; j < img->sy; j++)
  347. memcpy(result + (j * pitch), (const void *) img->tpixels[j], pitch);
  348. }
  349. else {
  350. pitch = img->sx * sizeof(char);
  351. result = malloc(img->sy * pitch);
  352. for (j = 0; j < img->sy; j++)
  353. memcpy(result + (j * pitch), (const void *) img->pixels[j], pitch);
  354. }
  355. return result;
  356. }
  357. int gd_load_palette(gdImage *img, caca_dither_t *dither) {
  358. if (!img || img->trueColor || gdMaxColors != 256) {
  359. return -1;
  360. }
  361. uint32_t r[256], g[256], b[256], a[256];
  362. int i;
  363. for (i = 0; i < 256; i++) {
  364. r[i] = img->red[i] << 4;
  365. g[i] = img->green[i] << 4;
  366. b[i] = img->blue[i] << 4;
  367. a[i] = img->alpha[i] << 4;
  368. }
  369. return caca_set_dither_palette(dither, &r[0], &g[0], &b[0], &a[0]);
  370. }
  371. #endif
  372. //------- Caca's functions ----------------//
  373. PHP_FUNCTION(caca_create_canvas) {
  374. long width, height = 0;
  375. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &width, &height) == FAILURE) {
  376. RETURN_FALSE;
  377. }
  378. caca_canvas_t *canvas = caca_create_canvas(width, height);
  379. if (!canvas) {
  380. RETURN_FALSE;
  381. }
  382. ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
  383. }
  384. PHP_FUNCTION(caca_set_canvas_size) {
  385. zval *_zval;
  386. long width, height = 0;
  387. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &width, &height) == FAILURE) {
  388. RETURN_FALSE;
  389. }
  390. caca_canvas_t *canvas;
  391. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  392. RETURN_SUCCESS(caca_set_canvas_size(canvas, width, height));
  393. }
  394. PHP_FUNCTION(caca_get_canvas_width) {
  395. caca_canvas_t *canvas;
  396. FETCH_CANVAS(canvas);
  397. RETURN_LONG(caca_get_canvas_width(canvas));
  398. }
  399. PHP_FUNCTION(caca_get_canvas_height) {
  400. caca_canvas_t *canvas;
  401. FETCH_CANVAS(canvas);
  402. RETURN_LONG(caca_get_canvas_height(canvas));
  403. }
  404. PHP_FUNCTION(caca_get_canvas_chars) {
  405. caca_canvas_t *canvas;
  406. FETCH_CANVAS(canvas);
  407. RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1);
  408. }
  409. PHP_FUNCTION(caca_get_canvas_attrs) {
  410. caca_canvas_t *canvas;
  411. FETCH_CANVAS(canvas);
  412. RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1);
  413. }
  414. PHP_FUNCTION(caca_rand) {
  415. long min, max = 0;
  416. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &min, &max) == FAILURE) {
  417. RETURN_FALSE;
  418. }
  419. RETURN_LONG(caca_rand(min, max));
  420. }
  421. PHP_FUNCTION(caca_get_version) {
  422. if (ZEND_NUM_ARGS() != 0) {
  423. WRONG_PARAM_COUNT;
  424. }
  425. RETURN_STRING((char *) caca_get_version(), 1);
  426. }
  427. PHP_FUNCTION(caca_gotoxy) {
  428. zval *_zval;
  429. long x, y = 0;
  430. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  431. RETURN_FALSE;
  432. }
  433. caca_canvas_t *canvas;
  434. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  435. RETURN_SUCCESS(caca_gotoxy(canvas, x, y));
  436. }
  437. PHP_FUNCTION(caca_get_cursor_x) {
  438. caca_canvas_t *canvas;
  439. FETCH_CANVAS(canvas);
  440. RETURN_LONG(caca_get_cursor_x(canvas));
  441. }
  442. PHP_FUNCTION(caca_get_cursor_y) {
  443. caca_canvas_t *canvas;
  444. FETCH_CANVAS(canvas);
  445. RETURN_LONG(caca_get_cursor_y(canvas));
  446. }
  447. PHP_FUNCTION(caca_put_char) {
  448. zval *_zval;
  449. long x, y, c;
  450. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == FAILURE) {
  451. RETURN_FALSE;
  452. }
  453. caca_canvas_t *canvas;
  454. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  455. RETURN_SUCCESS(caca_put_char(canvas, x, y, c));
  456. }
  457. PHP_FUNCTION(caca_get_char) {
  458. zval *_zval;
  459. long x, y = 0;
  460. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  461. RETURN_FALSE;
  462. }
  463. caca_canvas_t *canvas;
  464. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  465. RETURN_CHAR(caca_get_char(canvas, x, y));
  466. }
  467. PHP_FUNCTION(caca_put_str) {
  468. zval *_zval;
  469. char *str;
  470. long x, y, str_len = 0;
  471. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &str, &str_len) == FAILURE) {
  472. RETURN_FALSE;
  473. }
  474. caca_canvas_t *canvas;
  475. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  476. RETURN_SUCCESS(caca_put_str(canvas, x, y, str));
  477. }
  478. PHP_FUNCTION(caca_clear_canvas) {
  479. caca_canvas_t *canvas;
  480. FETCH_CANVAS(canvas);
  481. RETURN_LONG(caca_clear_canvas(canvas));
  482. }
  483. PHP_FUNCTION(caca_set_canvas_handle) {
  484. zval *_zval;
  485. long x, y = 0;
  486. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  487. RETURN_FALSE;
  488. }
  489. caca_canvas_t *canvas;
  490. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  491. RETURN_SUCCESS(caca_set_canvas_handle(canvas, x, y));
  492. }
  493. PHP_FUNCTION(caca_get_canvas_handle_x) {
  494. caca_canvas_t *canvas;
  495. FETCH_CANVAS(canvas);
  496. RETURN_LONG(caca_get_canvas_handle_x(canvas));
  497. }
  498. PHP_FUNCTION(caca_get_canvas_handle_y) {
  499. caca_canvas_t *canvas;
  500. FETCH_CANVAS(canvas);
  501. RETURN_LONG(caca_get_canvas_handle_y(canvas));
  502. }
  503. PHP_FUNCTION(caca_blit) {
  504. zval *_zval1, *_zval2, *_zval3 = NULL;
  505. long x, y = 0;
  506. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllr|r", &_zval1, &x, &y, &_zval2, &_zval3) == FAILURE) {
  507. RETURN_FALSE;
  508. }
  509. caca_canvas_t *dst, *src, *mask = NULL;
  510. ZEND_FETCH_RESOURCE(dst, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  511. ZEND_FETCH_RESOURCE(src, caca_canvas_t*, &_zval2, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  512. if (_zval3) {
  513. ZEND_FETCH_RESOURCE(mask, caca_canvas_t*, &_zval3, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  514. }
  515. RETURN_SUCCESS(caca_blit(dst, x, y, src, NULL));
  516. }
  517. PHP_FUNCTION(caca_set_canvas_boundaries) {
  518. zval *_zval;
  519. long x, y, width, height = 0;
  520. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &width, &height) == FAILURE) {
  521. RETURN_FALSE;
  522. }
  523. caca_canvas_t *canvas;
  524. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  525. RETURN_SUCCESS(caca_set_canvas_boundaries(canvas, x, y, width, height));
  526. }
  527. PHP_FUNCTION(caca_invert) {
  528. caca_canvas_t *canvas;
  529. FETCH_CANVAS(canvas);
  530. RETURN_SUCCESS(caca_invert(canvas));
  531. }
  532. PHP_FUNCTION(caca_flip) {
  533. caca_canvas_t *canvas;
  534. FETCH_CANVAS(canvas);
  535. RETURN_SUCCESS(caca_flip(canvas));
  536. }
  537. PHP_FUNCTION(caca_flop) {
  538. caca_canvas_t *canvas;
  539. FETCH_CANVAS(canvas);
  540. RETURN_SUCCESS(caca_flop(canvas));
  541. }
  542. PHP_FUNCTION(caca_rotate_180) {
  543. caca_canvas_t *canvas;
  544. FETCH_CANVAS(canvas);
  545. RETURN_SUCCESS(caca_rotate_180(canvas));
  546. }
  547. PHP_FUNCTION(caca_rotate_left) {
  548. caca_canvas_t *canvas;
  549. FETCH_CANVAS(canvas);
  550. RETURN_SUCCESS(caca_rotate_left(canvas));
  551. }
  552. PHP_FUNCTION(caca_rotate_right) {
  553. caca_canvas_t *canvas;
  554. FETCH_CANVAS(canvas);
  555. RETURN_SUCCESS(caca_rotate_right(canvas));
  556. }
  557. PHP_FUNCTION(caca_stretch_left) {
  558. caca_canvas_t *canvas;
  559. FETCH_CANVAS(canvas);
  560. RETURN_SUCCESS(caca_stretch_left(canvas));
  561. }
  562. PHP_FUNCTION(caca_stretch_right) {
  563. caca_canvas_t *canvas;
  564. FETCH_CANVAS(canvas);
  565. RETURN_SUCCESS(caca_stretch_right(canvas));
  566. }
  567. PHP_FUNCTION(caca_get_attr) {
  568. zval *_zval;
  569. long x, y = 0;
  570. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  571. RETURN_FALSE;
  572. }
  573. caca_canvas_t *canvas;
  574. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  575. RETURN_LONG(caca_get_attr(canvas, x, y));
  576. }
  577. PHP_FUNCTION(caca_set_attr) {
  578. zval *_zval;
  579. long attr = 0;
  580. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &attr) == FAILURE) {
  581. RETURN_FALSE;
  582. }
  583. caca_canvas_t *canvas;
  584. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  585. RETURN_SUCCESS(caca_set_attr(canvas, attr));
  586. }
  587. PHP_FUNCTION(caca_put_attr) {
  588. zval *_zval;
  589. long x, y, attr = 0;
  590. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &attr) == FAILURE) {
  591. RETURN_FALSE;
  592. }
  593. caca_canvas_t *canvas;
  594. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  595. RETURN_SUCCESS(caca_put_attr(canvas, x, y, attr));
  596. }
  597. PHP_FUNCTION(caca_set_color_ansi) {
  598. zval *_zval;
  599. long foreground, background = 0;
  600. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
  601. RETURN_FALSE;
  602. }
  603. caca_canvas_t *canvas;
  604. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  605. RETURN_SUCCESS(caca_set_color_ansi(canvas, foreground, background));
  606. }
  607. PHP_FUNCTION(caca_set_color_argb) {
  608. zval *_zval;
  609. long foreground, background = 0;
  610. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
  611. RETURN_FALSE;
  612. }
  613. caca_canvas_t *canvas;
  614. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  615. RETURN_SUCCESS(caca_set_color_argb(canvas, foreground, background));
  616. }
  617. PHP_FUNCTION(caca_attr_to_ansi) {
  618. long l;
  619. FETCH_LONG(l);
  620. RETURN_LONG(caca_attr_to_ansi(l));
  621. }
  622. PHP_FUNCTION(caca_attr_to_ansi_fg) {
  623. long l;
  624. FETCH_LONG(l);
  625. RETURN_LONG(caca_attr_to_ansi_fg(l));
  626. }
  627. PHP_FUNCTION(caca_attr_to_ansi_bg) {
  628. long l;
  629. FETCH_LONG(l);
  630. RETURN_LONG(caca_attr_to_ansi_bg(l));
  631. }
  632. PHP_FUNCTION(caca_attr_to_rgb12_fg) {
  633. long l;
  634. FETCH_LONG(l);
  635. RETURN_LONG(caca_attr_to_rgb12_fg(l));
  636. }
  637. PHP_FUNCTION(caca_attr_to_rgb12_bg) {
  638. long l;
  639. FETCH_LONG(l);
  640. RETURN_LONG(caca_attr_to_rgb12_bg(l));
  641. }
  642. PHP_FUNCTION(caca_attr_to_argb64) {
  643. //TODO: write or delete
  644. }
  645. PHP_FUNCTION(caca_utf8_to_utf32) {
  646. //TODO: write or delete
  647. }
  648. PHP_FUNCTION(caca_utf32_to_utf8) {
  649. //TODO: write or delete
  650. }
  651. PHP_FUNCTION(caca_utf32_to_cp437) {
  652. long l;
  653. FETCH_LONG(l);
  654. RETURN_LONG(caca_utf32_to_cp437(l));
  655. }
  656. PHP_FUNCTION(caca_cp437_to_utf32) {
  657. long l;
  658. FETCH_LONG(l);
  659. RETURN_LONG(caca_cp437_to_utf32(l));
  660. }
  661. PHP_FUNCTION(caca_utf32_to_ascii) {
  662. long l;
  663. FETCH_LONG(l);
  664. RETURN_CHAR(caca_utf32_to_ascii(l));
  665. }
  666. PHP_FUNCTION(caca_utf32_is_fullwidth) {
  667. //TODO: write or delete
  668. }
  669. PHP_FUNCTION(caca_draw_line) {
  670. zval *_zval;
  671. long xa, ya, xb, yb, c;
  672. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  673. RETURN_FALSE;
  674. }
  675. caca_canvas_t *canvas;
  676. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  677. RETURN_SUCCESS(caca_draw_line(canvas, xa, ya, xb, yb, c));
  678. }
  679. PHP_FUNCTION(caca_draw_polyline) {
  680. zval *zval_res, *zval_arr;
  681. long c;
  682. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ral", &zval_res, &zval_arr, &c) == FAILURE) {
  683. RETURN_FALSE;
  684. }
  685. caca_canvas_t *canvas;
  686. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  687. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  688. int lenmax = zend_hash_num_elements(arr_hash);
  689. int *tbl_x, *tbl_y;
  690. int tbl_count = 0;
  691. tbl_x = malloc(sizeof(int) * lenmax);
  692. tbl_y = malloc(sizeof(int) * lenmax);
  693. HashPosition pos;
  694. zval **pt, **x, **y;
  695. for (
  696. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  697. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  698. zend_hash_move_forward_ex(arr_hash, &pos)
  699. ) {
  700. if (
  701. Z_TYPE_P(*pt) == IS_ARRAY
  702. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  703. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  704. ) {
  705. convert_to_long_ex(x);
  706. convert_to_long_ex(y);
  707. tbl_x[tbl_count] = Z_LVAL_PP(x);
  708. tbl_y[tbl_count] = Z_LVAL_PP(y);
  709. tbl_count++;
  710. }
  711. }
  712. int res = caca_draw_polyline(canvas, tbl_x, tbl_y, tbl_count - 1, c);
  713. free(tbl_x);
  714. free(tbl_y);
  715. RETURN_SUCCESS(res);
  716. }
  717. PHP_FUNCTION(caca_draw_thin_line) {
  718. zval *_zval;
  719. long xa, ya, xb, yb = 0;
  720. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  721. RETURN_FALSE;
  722. }
  723. caca_canvas_t *canvas;
  724. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  725. RETURN_SUCCESS(caca_draw_thin_line(canvas, xa, ya, xb, yb));
  726. }
  727. PHP_FUNCTION(caca_draw_thin_polyline) {
  728. zval *zval_res, *zval_arr;
  729. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &zval_arr) == FAILURE) {
  730. RETURN_FALSE;
  731. }
  732. caca_canvas_t *canvas;
  733. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  734. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  735. int lenmax = zend_hash_num_elements(arr_hash);
  736. int *tbl_x, *tbl_y;
  737. int tbl_count = 0;
  738. tbl_x = malloc(sizeof(int) * lenmax);
  739. tbl_y = malloc(sizeof(int) * lenmax);
  740. if (!tbl_x || !tbl_y) {
  741. RETURN_FALSE;
  742. }
  743. HashPosition pos;
  744. zval **pt, **x, **y;
  745. for (
  746. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  747. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  748. zend_hash_move_forward_ex(arr_hash, &pos)
  749. ) {
  750. if (
  751. Z_TYPE_P(*pt) == IS_ARRAY
  752. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  753. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  754. ) {
  755. convert_to_long_ex(x);
  756. convert_to_long_ex(y);
  757. tbl_x[tbl_count] = Z_LVAL_PP(x);
  758. tbl_y[tbl_count] = Z_LVAL_PP(y);
  759. tbl_count++;
  760. }
  761. }
  762. int res = caca_draw_thin_polyline(canvas, tbl_x, tbl_y, tbl_count - 1);
  763. free(tbl_x);
  764. free(tbl_y);
  765. RETURN_SUCCESS(res);
  766. }
  767. PHP_FUNCTION(caca_draw_circle) {
  768. zval *_zval;
  769. long x, y, r, c;
  770. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &r, &c) == FAILURE) {
  771. RETURN_FALSE;
  772. }
  773. caca_canvas_t *canvas;
  774. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  775. RETURN_SUCCESS(caca_draw_circle(canvas, x, y, r, c));
  776. }
  777. PHP_FUNCTION(caca_draw_ellipse) {
  778. zval *_zval;
  779. long xa, ya, xb, yb, c = 0;
  780. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  781. RETURN_FALSE;
  782. }
  783. caca_canvas_t *canvas;
  784. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  785. RETURN_SUCCESS(caca_draw_ellipse(canvas, xa, ya, xb, yb, c));
  786. }
  787. PHP_FUNCTION(caca_draw_thin_ellipse) {
  788. zval *_zval;
  789. long xa, ya, xb, yb = 0;
  790. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  791. RETURN_FALSE;
  792. }
  793. caca_canvas_t *canvas;
  794. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  795. RETURN_SUCCESS(caca_draw_thin_ellipse(canvas, xa, ya, xb, yb));
  796. }
  797. PHP_FUNCTION(caca_fill_ellipse) {
  798. zval *_zval;
  799. long xa, ya, xb, yb, c;
  800. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  801. RETURN_FALSE;
  802. }
  803. caca_canvas_t *canvas;
  804. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  805. RETURN_SUCCESS(caca_fill_ellipse(canvas, xa, ya, xb, yb, c));
  806. }
  807. PHP_FUNCTION(caca_draw_box) {
  808. zval *_zval;
  809. long xa, ya, xb, yb, c;
  810. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  811. RETURN_FALSE;
  812. }
  813. caca_canvas_t *canvas;
  814. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  815. RETURN_SUCCESS(caca_draw_box(canvas, xa, ya, xb, yb, c));
  816. }
  817. PHP_FUNCTION(caca_draw_thin_box) {
  818. zval *_zval;
  819. long xa, ya, xb, yb = 0;
  820. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  821. RETURN_FALSE;
  822. }
  823. caca_canvas_t *canvas;
  824. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  825. RETURN_SUCCESS(caca_draw_thin_box(canvas, xa, ya, xb, yb));
  826. }
  827. PHP_FUNCTION(caca_draw_cp437_box) {
  828. zval *_zval;
  829. long xa, ya, xb, yb;
  830. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  831. RETURN_FALSE;
  832. }
  833. caca_canvas_t *canvas;
  834. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  835. RETURN_SUCCESS(caca_draw_cp437_box(canvas, xa, ya, xb, yb));
  836. }
  837. PHP_FUNCTION(caca_fill_box) {
  838. zval *_zval;
  839. long xa, ya, xb, yb, c;
  840. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  841. RETURN_FALSE;
  842. }
  843. caca_canvas_t *canvas;
  844. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  845. RETURN_SUCCESS(caca_fill_box(canvas, xa, ya, xb, yb, c));
  846. }
  847. PHP_FUNCTION(caca_draw_triangle) {
  848. zval *_zval;
  849. long xa, ya, xb, yb, xc, yc, c;
  850. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == FAILURE) {
  851. RETURN_FALSE;
  852. }
  853. caca_canvas_t *canvas;
  854. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  855. RETURN_SUCCESS(caca_draw_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  856. }
  857. PHP_FUNCTION(caca_draw_thin_triangle) {
  858. zval *_zval;
  859. long xa, ya, xb, yb, xc, yc = 0;
  860. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc) == FAILURE) {
  861. RETURN_FALSE;
  862. }
  863. caca_canvas_t *canvas;
  864. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  865. RETURN_SUCCESS(caca_draw_thin_triangle(canvas, xa, ya, xb, yb, xc, yc));
  866. }
  867. PHP_FUNCTION(caca_fill_triangle) {
  868. zval *_zval;
  869. long xa, ya, xb, yb, xc, yc, c = 0;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == FAILURE) {
  871. RETURN_FALSE;
  872. }
  873. caca_canvas_t *canvas;
  874. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  875. RETURN_SUCCESS(caca_fill_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  876. }
  877. PHP_FUNCTION(caca_get_frame_count) {
  878. zval *_zval;
  879. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  880. RETURN_FALSE;
  881. }
  882. caca_canvas_t *canvas;
  883. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  884. RETURN_LONG(caca_get_canvas_count(canvas));
  885. }
  886. PHP_FUNCTION(caca_set_frame) {
  887. zval *_zval;
  888. long id;
  889. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  890. RETURN_FALSE;
  891. }
  892. caca_canvas_t *canvas;
  893. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  894. RETURN_SUCCESS(caca_set_frame(canvas, id));
  895. }
  896. PHP_FUNCTION(caca_set_frame_name) {
  897. zval *_zval;
  898. char *str;
  899. long str_len;
  900. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  901. RETURN_FALSE;
  902. }
  903. caca_canvas_t *canvas;
  904. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  905. RETURN_SUCCESS(caca_set_frame_name(canvas, str));
  906. }
  907. PHP_FUNCTION(caca_create_frame) {
  908. zval *_zval;
  909. long id;
  910. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  911. RETURN_FALSE;
  912. }
  913. caca_canvas_t *canvas;
  914. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  915. RETURN_SUCCESS(caca_create_frame(canvas, id));
  916. }
  917. PHP_FUNCTION(caca_free_frame) {
  918. zval *_zval;
  919. long id;
  920. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  921. RETURN_FALSE;
  922. }
  923. caca_canvas_t *canvas;
  924. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  925. RETURN_SUCCESS(caca_free_frame(canvas, id));
  926. }
  927. PHP_FUNCTION(caca_create_dither) {
  928. zval *_zval;
  929. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  930. RETURN_FALSE;
  931. }
  932. #ifdef HAVE_GD
  933. gdImage *img = fetch_external_resource(_zval, "gd");
  934. if (!img) {
  935. RETURN_FALSE;
  936. }
  937. caca_dither_t *dither;
  938. if (img->trueColor)
  939. dither = caca_create_dither(sizeof(int) * 8, img->sx, img->sy, img->sx * sizeof(int), 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000);
  940. else
  941. dither = caca_create_dither(sizeof(char) * 8, img->sx, img->sy, img->sx * sizeof(char), 0, 0, 0, 0);
  942. if (!dither) {
  943. RETURN_FALSE;
  944. }
  945. ZEND_REGISTER_RESOURCE(return_value, dither, le_caca_dither);
  946. #else
  947. RETURN_FALSE
  948. #endif
  949. }
  950. PHP_FUNCTION(caca_set_dither_palette) {
  951. zval *zval_res, *arr;
  952. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &arr) == FAILURE) {
  953. RETURN_FALSE;
  954. }
  955. caca_dither_t *dither;
  956. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_dither);
  957. uint32_t tbl[4][256];
  958. zval **color, **value;
  959. int i, j;
  960. for (i = 0; i < 256; i++) {
  961. if (zend_hash_index_find(Z_ARRVAL_P(arr), i, (void**) &color) == FAILURE || Z_TYPE_P(*color) != IS_ARRAY) {
  962. RETURN_FALSE;
  963. }
  964. for (j = 0; j < 4; j++) {
  965. if (zend_hash_index_find(Z_ARRVAL_P(*color), j, (void**) &value) == FAILURE) {
  966. RETURN_FALSE;
  967. }
  968. convert_to_long_ex(value);
  969. tbl[j][i] = Z_LVAL_PP(value);
  970. }
  971. }
  972. RETURN_SUCCESS(caca_set_dither_palette(dither, tbl[0], tbl[1], tbl[2], tbl[3]));
  973. }
  974. PHP_FUNCTION(caca_set_dither_brightness) {
  975. zval *_zval;
  976. double value;
  977. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  978. RETURN_FALSE;
  979. }
  980. caca_dither_t *dither;
  981. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  982. RETURN_SUCCESS(caca_set_dither_brightness(dither, value));
  983. }
  984. PHP_FUNCTION(caca_get_dither_brightness) {
  985. zval *_zval;
  986. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  987. RETURN_FALSE;
  988. }
  989. caca_dither_t *dither;
  990. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  991. RETURN_DOUBLE(caca_get_dither_brightness(dither));
  992. }
  993. PHP_FUNCTION(caca_set_dither_gamma) {
  994. zval *_zval;
  995. double value;
  996. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  997. RETURN_FALSE;
  998. }
  999. caca_dither_t *dither;
  1000. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1001. RETURN_SUCCESS(caca_set_dither_gamma(dither, value));
  1002. }
  1003. PHP_FUNCTION(caca_get_dither_gamma) {
  1004. zval *_zval;
  1005. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1006. RETURN_FALSE;
  1007. }
  1008. caca_dither_t *dither;
  1009. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1010. RETURN_DOUBLE(caca_get_dither_gamma(dither));
  1011. }
  1012. PHP_FUNCTION(caca_set_dither_contrast) {
  1013. zval *_zval;
  1014. double value;
  1015. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  1016. RETURN_FALSE;
  1017. }
  1018. caca_dither_t *dither;
  1019. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1020. RETURN_SUCCESS(caca_set_dither_contrast(dither, value));
  1021. }
  1022. PHP_FUNCTION(caca_get_dither_contrast) {
  1023. zval *_zval;
  1024. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1025. RETURN_FALSE;
  1026. }
  1027. caca_dither_t *dither;
  1028. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1029. RETURN_DOUBLE(caca_get_dither_contrast(dither));
  1030. }
  1031. PHP_FUNCTION(caca_set_dither_antialias) {
  1032. zval *_zval;
  1033. int str_len;
  1034. char *str;
  1035. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1036. RETURN_FALSE;
  1037. }
  1038. caca_dither_t *dither;
  1039. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1040. RETURN_SUCCESS(caca_set_dither_antialias(dither, str));
  1041. }
  1042. PHP_FUNCTION(caca_get_dither_antialias_list) {
  1043. zval *_zval;
  1044. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1045. RETURN_FALSE;
  1046. }
  1047. caca_dither_t *dither;
  1048. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1049. char const * const *list = caca_get_dither_antialias_list(dither);
  1050. int i;
  1051. array_init(return_value);
  1052. for(i = 0; list[i]; i += 1)
  1053. add_next_index_string(return_value, (char*) list[i], 1);
  1054. }
  1055. PHP_FUNCTION(caca_get_dither_antialias) {
  1056. zval *_zval;
  1057. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1058. RETURN_FALSE;
  1059. }
  1060. caca_dither_t *dither;
  1061. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1062. RETURN_STRING((char *) caca_get_dither_antialias(dither), 1);
  1063. }
  1064. PHP_FUNCTION(caca_set_dither_color) {
  1065. zval *_zval;
  1066. int str_len;
  1067. char *str;
  1068. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1069. RETURN_FALSE;
  1070. }
  1071. caca_dither_t *dither;
  1072. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1073. RETURN_SUCCESS(caca_set_dither_color(dither, str));
  1074. }
  1075. PHP_FUNCTION(caca_get_dither_color_list) {
  1076. zval *_zval;
  1077. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1078. RETURN_FALSE;
  1079. }
  1080. caca_dither_t *dither;
  1081. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1082. char const * const *list = caca_get_dither_antialias_list(dither);
  1083. int i;
  1084. array_init(return_value);
  1085. for(i = 0; list[i]; i += 2)
  1086. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1087. }
  1088. PHP_FUNCTION(caca_get_dither_color) {
  1089. zval *_zval;
  1090. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1091. RETURN_FALSE;
  1092. }
  1093. caca_dither_t *dither;
  1094. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1095. RETURN_STRING((char *) caca_get_dither_color(dither), 1);
  1096. }
  1097. PHP_FUNCTION(caca_set_dither_charset) {
  1098. zval *_zval;
  1099. int str_len;
  1100. char *str;
  1101. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1102. RETURN_FALSE;
  1103. }
  1104. caca_dither_t *dither;
  1105. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1106. RETURN_SUCCESS(caca_set_dither_charset(dither, str));
  1107. }
  1108. PHP_FUNCTION(caca_get_dither_charset_list) {
  1109. zval *_zval;
  1110. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1111. RETURN_FALSE;
  1112. }
  1113. caca_dither_t *dither;
  1114. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1115. char const * const *list = caca_get_dither_charset_list(dither);
  1116. int i;
  1117. array_init(return_value);
  1118. for(i = 0; list[i]; i += 2)
  1119. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1120. }
  1121. PHP_FUNCTION(caca_get_dither_charset) {
  1122. zval *_zval;
  1123. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1124. RETURN_FALSE;
  1125. }
  1126. caca_dither_t *dither;
  1127. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1128. RETURN_STRING((char *) caca_get_dither_charset(dither), 1);
  1129. }
  1130. PHP_FUNCTION(caca_set_dither_algorithm) {
  1131. zval *_zval;
  1132. int str_len;
  1133. char *str;
  1134. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1135. RETURN_FALSE;
  1136. }
  1137. caca_dither_t *dither;
  1138. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1139. RETURN_SUCCESS(caca_set_dither_algorithm(dither, str));
  1140. }
  1141. PHP_FUNCTION(caca_get_dither_algorithm_list) {
  1142. zval *_zval;
  1143. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1144. RETURN_FALSE;
  1145. }
  1146. caca_dither_t *dither;
  1147. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1148. char const * const *list = caca_get_dither_algorithm_list(dither);
  1149. int i;
  1150. array_init(return_value);
  1151. for(i = 0; list[i]; i += 2)
  1152. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1153. }
  1154. PHP_FUNCTION(caca_get_dither_algorithm) {
  1155. zval *_zval;
  1156. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1157. RETURN_FALSE;
  1158. }
  1159. caca_dither_t *dither;
  1160. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1161. RETURN_STRING((char *) caca_get_dither_algorithm(dither), 1);
  1162. }
  1163. PHP_FUNCTION(caca_dither_bitmap) {
  1164. zval *_zval1, *_zval2, *_zval3;
  1165. long x, y, w, h = 0;
  1166. zend_bool load_palette = 1;
  1167. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllrr|b", &_zval1, &x, &y, &w, &h, &_zval2, &_zval3, &load_palette) == FAILURE) {
  1168. RETURN_FALSE;
  1169. }
  1170. caca_canvas_t *canvas;
  1171. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1172. caca_dither_t *dither;
  1173. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval2, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1174. #ifdef HAVE_GD
  1175. gdImage *img = fetch_external_resource(_zval3, "gd");
  1176. if (!img) {
  1177. RETURN_FALSE;
  1178. }
  1179. void *pixels = gd_get_pixels(img);
  1180. if (!pixels) {
  1181. RETURN_FALSE;
  1182. }
  1183. //load palette if image is not true color
  1184. if (load_palette && !img->trueColor && gd_load_palette(img, dither) != 0) {
  1185. RETURN_FALSE;
  1186. }
  1187. caca_dither_bitmap(canvas, x, y, w, h, dither, pixels);
  1188. free(pixels);
  1189. RETURN_TRUE;
  1190. #else
  1191. RETURN_FALSE;
  1192. #endif
  1193. }
  1194. PHP_FUNCTION(caca_load_font) {
  1195. char *str;
  1196. long str_len;
  1197. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1198. RETURN_FALSE;
  1199. }
  1200. caca_font_t *font = caca_load_font(str, str_len);
  1201. if (!font) {
  1202. RETURN_FALSE;
  1203. }
  1204. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1205. }
  1206. PHP_FUNCTION(caca_load_builtin_font) {
  1207. char *str;
  1208. long str_len;
  1209. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1210. RETURN_FALSE;
  1211. }
  1212. caca_font_t *font = caca_load_font(str, 0);
  1213. if (!font) {
  1214. RETURN_FALSE;
  1215. }
  1216. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1217. }
  1218. PHP_FUNCTION(caca_get_font_list) {
  1219. if (ZEND_NUM_ARGS() != 0) {
  1220. WRONG_PARAM_COUNT;
  1221. }
  1222. char const * const *list = caca_get_font_list();
  1223. int i;
  1224. array_init(return_value);
  1225. for(i = 0; list[i]; i += 1)
  1226. add_next_index_string(return_value, (char*) list[i], 1);
  1227. }
  1228. PHP_FUNCTION(caca_get_font_width) {
  1229. zval *_zval;
  1230. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1231. RETURN_FALSE;
  1232. }
  1233. caca_font_t *font;
  1234. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1235. RETURN_LONG(caca_get_font_width(font));
  1236. }
  1237. PHP_FUNCTION(caca_get_font_height) {
  1238. zval *_zval;
  1239. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1240. RETURN_FALSE;
  1241. }
  1242. caca_font_t *font;
  1243. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1244. RETURN_LONG(caca_get_font_height(font));
  1245. }
  1246. PHP_FUNCTION(caca_get_font_blocks) {
  1247. zval *_zval;
  1248. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1249. RETURN_FALSE;
  1250. }
  1251. caca_font_t *font;
  1252. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1253. uint32_t const *list = caca_get_font_blocks(font);
  1254. int i;
  1255. array_init(return_value);
  1256. for(i = 0; list[i]; i += 1)
  1257. add_next_index_long(return_value, list[i]);
  1258. }
  1259. PHP_FUNCTION(caca_render_canvas) {
  1260. zval *_zval1, *_zval2, *_zval3;
  1261. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrr", &_zval1, &_zval2, &_zval3) == FAILURE) {
  1262. RETURN_FALSE;
  1263. }
  1264. caca_canvas_t *canvas;
  1265. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1266. caca_font_t *font;
  1267. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval2, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1268. #ifdef HAVE_GD
  1269. gdImage *img = fetch_external_resource(_zval3, "gd");
  1270. if (!img || !img->trueColor) {
  1271. RETURN_FALSE;
  1272. }
  1273. int pitch = img->sx * 4;
  1274. uint8_t *buffer = malloc(pitch * img->sy);
  1275. if (!buffer) {
  1276. RETURN_FALSE;
  1277. }
  1278. caca_render_canvas(canvas, font, (void *) buffer, img->sx, img->sy, pitch);
  1279. int i, j;
  1280. for (i = 0; i < img->sy; i++) {
  1281. for (j = 0; j < img->sx; j++) {
  1282. uint8_t *src = buffer + i * pitch + j * 4;
  1283. img->tpixels[i][j] = *(src + 3) | (*(src + 2) << 8) | (*(src + 1) << 16) | (*(src + 0) << 24);
  1284. }
  1285. }
  1286. free(buffer);
  1287. RETURN_TRUE;
  1288. #else
  1289. RETURN_FALSE;
  1290. #endif
  1291. }
  1292. PHP_FUNCTION(caca_canvas_set_figfont) {
  1293. zval *_zval;
  1294. char *font;
  1295. long font_len;
  1296. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &font, &font_len) == FAILURE) {
  1297. RETURN_FALSE;
  1298. }
  1299. caca_canvas_t *canvas;
  1300. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1301. RETURN_SUCCESS(caca_canvas_set_figfont(canvas, font));
  1302. }
  1303. PHP_FUNCTION(caca_put_figchar) {
  1304. zval *_zval;
  1305. long c;
  1306. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &c) == FAILURE) {
  1307. RETURN_FALSE;
  1308. }
  1309. caca_canvas_t *canvas;
  1310. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1311. RETURN_SUCCESS(caca_put_figchar(canvas, c));
  1312. }
  1313. PHP_FUNCTION(caca_flush_figlet) {
  1314. zval *_zval;
  1315. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1316. RETURN_FALSE;
  1317. }
  1318. caca_canvas_t *canvas;
  1319. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1320. RETURN_SUCCESS(caca_flush_figlet(canvas));
  1321. }
  1322. PHP_FUNCTION(caca_file_open) {
  1323. char *path, *mode;
  1324. long path_len, mode_len;
  1325. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) {
  1326. RETURN_FALSE;
  1327. }
  1328. caca_file_t *file = caca_file_open(path, mode);
  1329. if (!file) {
  1330. RETURN_FALSE;
  1331. }
  1332. ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file);
  1333. }
  1334. PHP_FUNCTION(caca_file_close) {
  1335. zval *_zval;
  1336. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1337. RETURN_FALSE;
  1338. }
  1339. caca_file_t *file;
  1340. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1341. int res = caca_file_close(file);
  1342. //Delete php resource
  1343. zend_list_delete(_zval->value.lval);
  1344. RETURN_SUCCESS(res);
  1345. }
  1346. PHP_FUNCTION(caca_file_tell) {
  1347. zval *_zval;
  1348. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1349. RETURN_FALSE;
  1350. }
  1351. caca_file_t *file;
  1352. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1353. RETURN_LONG(caca_file_tell(file));
  1354. }
  1355. PHP_FUNCTION(caca_file_read) {
  1356. zval *_zval;
  1357. long len;
  1358. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1359. RETURN_FALSE;
  1360. }
  1361. caca_file_t *file;
  1362. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1363. if (len < 1) {
  1364. RETURN_FALSE;
  1365. }
  1366. char *buffer = emalloc(len);
  1367. if (!buffer) {
  1368. RETURN_FALSE;
  1369. }
  1370. caca_file_read(file, buffer, len);
  1371. RETURN_STRINGL(buffer, len, 1);
  1372. }
  1373. PHP_FUNCTION(caca_file_write) {
  1374. zval *_zval;
  1375. char *buf;
  1376. long buf_len = 0;
  1377. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &buf, &buf_len) == FAILURE) {
  1378. RETURN_FALSE;
  1379. }
  1380. caca_file_t *file;
  1381. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1382. RETURN_LONG(caca_file_write(file, buf, buf_len));
  1383. }
  1384. PHP_FUNCTION(caca_file_gets) {
  1385. zval *_zval;
  1386. long len = 0;
  1387. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1388. RETURN_FALSE;
  1389. }
  1390. caca_file_t *file;
  1391. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1392. if (len < 1) {
  1393. RETURN_FALSE;
  1394. }
  1395. char *buffer = emalloc(len);
  1396. if (!buffer) {
  1397. RETURN_FALSE;
  1398. }
  1399. char *result = caca_file_gets(file, buffer, len);
  1400. if (!result) {
  1401. RETURN_FALSE;
  1402. }
  1403. RETURN_STRINGL(result, len, 1);
  1404. }
  1405. PHP_FUNCTION(caca_file_eof) {
  1406. zval *_zval;
  1407. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1408. RETURN_FALSE;
  1409. }
  1410. caca_file_t *file;
  1411. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1412. RETURN_BOOL(caca_file_eof(file) != 0);
  1413. }
  1414. PHP_FUNCTION(caca_import_string) {
  1415. zval *_zval;
  1416. char *src, *format;
  1417. long src_len, format_len = 0;
  1418. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &format, &format_len) == FAILURE) {
  1419. RETURN_FALSE;
  1420. }
  1421. caca_canvas_t *canvas;
  1422. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1423. RETURN_LONG(caca_import_memory(canvas, src, src_len, format));
  1424. }
  1425. PHP_FUNCTION(caca_import_file) {
  1426. zval *_zval;
  1427. char *filename, *format;
  1428. long filename_len, format_len = 0;
  1429. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &filename, &filename_len, &format, &format_len) == FAILURE) {
  1430. RETURN_FALSE;
  1431. }
  1432. caca_canvas_t *canvas;
  1433. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1434. RETURN_LONG(caca_import_file(canvas, filename, format));
  1435. }
  1436. PHP_FUNCTION(caca_get_import_list) {
  1437. if (ZEND_NUM_ARGS() != 0) {
  1438. WRONG_PARAM_COUNT;
  1439. }
  1440. char const * const *list = caca_get_import_list();
  1441. int i;
  1442. array_init(return_value);
  1443. for(i = 0; list[i]; i += 2)
  1444. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1445. }
  1446. PHP_FUNCTION(caca_export_string) {
  1447. zval *_zval;
  1448. char *type;
  1449. long type_len;
  1450. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &type, &type_len) == FAILURE) {
  1451. RETURN_FALSE;
  1452. }
  1453. caca_canvas_t *canvas;
  1454. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1455. void *buffer, *copy;
  1456. size_t len = 0;
  1457. buffer = caca_export_memory(canvas, type, &len);
  1458. copy = emalloc(len);
  1459. if (!buffer || !copy) {
  1460. RETURN_FALSE;
  1461. }
  1462. memcpy(copy, buffer, len);
  1463. free(buffer);
  1464. RETURN_STRINGL((char*) copy, len, 0);
  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. }