您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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