Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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_wherex, NULL)
  31. PHP_FE(caca_wherey, 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_UNKNOWN", CACA_KEY_UNKNOWN, CONST_CS | CONST_PERSISTENT);
  244. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_A", CACA_KEY_CTRL_A, CONST_CS | CONST_PERSISTENT);
  245. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_B", CACA_KEY_CTRL_B, CONST_CS | CONST_PERSISTENT);
  246. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_C", CACA_KEY_CTRL_C, CONST_CS | CONST_PERSISTENT);
  247. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_D", CACA_KEY_CTRL_D, CONST_CS | CONST_PERSISTENT);
  248. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_E", CACA_KEY_CTRL_E, CONST_CS | CONST_PERSISTENT);
  249. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_F", CACA_KEY_CTRL_F, CONST_CS | CONST_PERSISTENT);
  250. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_G", CACA_KEY_CTRL_G, CONST_CS | CONST_PERSISTENT);
  251. REGISTER_LONG_CONSTANT("CACA_KEY_BACKSPACE", CACA_KEY_BACKSPACE, CONST_CS | CONST_PERSISTENT);
  252. REGISTER_LONG_CONSTANT("CACA_KEY_TAB", CACA_KEY_TAB, CONST_CS | CONST_PERSISTENT);
  253. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_J", CACA_KEY_CTRL_J, CONST_CS | CONST_PERSISTENT);
  254. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_K", CACA_KEY_CTRL_K, CONST_CS | CONST_PERSISTENT);
  255. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_L", CACA_KEY_CTRL_L, CONST_CS | CONST_PERSISTENT);
  256. REGISTER_LONG_CONSTANT("CACA_KEY_RETURN", CACA_KEY_RETURN, CONST_CS | CONST_PERSISTENT);
  257. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_N", CACA_KEY_CTRL_N, CONST_CS | CONST_PERSISTENT);
  258. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_O", CACA_KEY_CTRL_O, CONST_CS | CONST_PERSISTENT);
  259. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_P", CACA_KEY_CTRL_P, CONST_CS | CONST_PERSISTENT);
  260. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Q", CACA_KEY_CTRL_Q, CONST_CS | CONST_PERSISTENT);
  261. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_R", CACA_KEY_CTRL_R, CONST_CS | CONST_PERSISTENT);
  262. REGISTER_LONG_CONSTANT("CACA_KEY_PAUSE", CACA_KEY_PAUSE, CONST_CS | CONST_PERSISTENT);
  263. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_T", CACA_KEY_CTRL_T, CONST_CS | CONST_PERSISTENT);
  264. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_U", CACA_KEY_CTRL_U, CONST_CS | CONST_PERSISTENT);
  265. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_V", CACA_KEY_CTRL_V, CONST_CS | CONST_PERSISTENT);
  266. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_W", CACA_KEY_CTRL_W, CONST_CS | CONST_PERSISTENT);
  267. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_X", CACA_KEY_CTRL_X, CONST_CS | CONST_PERSISTENT);
  268. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Y", CACA_KEY_CTRL_Y, CONST_CS | CONST_PERSISTENT);
  269. REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Z", CACA_KEY_CTRL_Z, CONST_CS | CONST_PERSISTENT);
  270. REGISTER_LONG_CONSTANT("CACA_KEY_ESCAPE", CACA_KEY_ESCAPE, CONST_CS | CONST_PERSISTENT);
  271. REGISTER_LONG_CONSTANT("CACA_KEY_DELETE", CACA_KEY_DELETE, CONST_CS | CONST_PERSISTENT);
  272. REGISTER_LONG_CONSTANT("CACA_KEY_UP", CACA_KEY_UP, CONST_CS | CONST_PERSISTENT);
  273. REGISTER_LONG_CONSTANT("CACA_KEY_DOWN", CACA_KEY_DOWN, CONST_CS | CONST_PERSISTENT);
  274. REGISTER_LONG_CONSTANT("CACA_KEY_LEFT", CACA_KEY_LEFT, CONST_CS | CONST_PERSISTENT);
  275. REGISTER_LONG_CONSTANT("CACA_KEY_RIGHT", CACA_KEY_RIGHT, CONST_CS | CONST_PERSISTENT);
  276. REGISTER_LONG_CONSTANT("CACA_KEY_INSERT", CACA_KEY_INSERT, CONST_CS | CONST_PERSISTENT);
  277. REGISTER_LONG_CONSTANT("CACA_KEY_HOME", CACA_KEY_HOME, CONST_CS | CONST_PERSISTENT);
  278. REGISTER_LONG_CONSTANT("CACA_KEY_END", CACA_KEY_END, CONST_CS | CONST_PERSISTENT);
  279. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEUP", CACA_KEY_PAGEUP, CONST_CS | CONST_PERSISTENT);
  280. REGISTER_LONG_CONSTANT("CACA_KEY_PAGEDOWN", CACA_KEY_PAGEDOWN, CONST_CS | CONST_PERSISTENT);
  281. REGISTER_LONG_CONSTANT("CACA_KEY_F1", CACA_KEY_F1, CONST_CS | CONST_PERSISTENT);
  282. REGISTER_LONG_CONSTANT("CACA_KEY_F2", CACA_KEY_F2, CONST_CS | CONST_PERSISTENT);
  283. REGISTER_LONG_CONSTANT("CACA_KEY_F3", CACA_KEY_F3, CONST_CS | CONST_PERSISTENT);
  284. REGISTER_LONG_CONSTANT("CACA_KEY_F4", CACA_KEY_F4, CONST_CS | CONST_PERSISTENT);
  285. REGISTER_LONG_CONSTANT("CACA_KEY_F5", CACA_KEY_F5, CONST_CS | CONST_PERSISTENT);
  286. REGISTER_LONG_CONSTANT("CACA_KEY_F6", CACA_KEY_F6, CONST_CS | CONST_PERSISTENT);
  287. REGISTER_LONG_CONSTANT("CACA_KEY_F7", CACA_KEY_F7, CONST_CS | CONST_PERSISTENT);
  288. REGISTER_LONG_CONSTANT("CACA_KEY_F8", CACA_KEY_F8, CONST_CS | CONST_PERSISTENT);
  289. REGISTER_LONG_CONSTANT("CACA_KEY_F9", CACA_KEY_F9, CONST_CS | CONST_PERSISTENT);
  290. REGISTER_LONG_CONSTANT("CACA_KEY_F10", CACA_KEY_F10, CONST_CS | CONST_PERSISTENT);
  291. REGISTER_LONG_CONSTANT("CACA_KEY_F11", CACA_KEY_F11, CONST_CS | CONST_PERSISTENT);
  292. REGISTER_LONG_CONSTANT("CACA_KEY_F12", CACA_KEY_F12, CONST_CS | CONST_PERSISTENT);
  293. REGISTER_LONG_CONSTANT("CACA_KEY_F13", CACA_KEY_F13, CONST_CS | CONST_PERSISTENT);
  294. REGISTER_LONG_CONSTANT("CACA_KEY_F14", CACA_KEY_F14, CONST_CS | CONST_PERSISTENT);
  295. REGISTER_LONG_CONSTANT("CACA_KEY_F15", CACA_KEY_F15, CONST_CS | CONST_PERSISTENT);
  296. return SUCCESS;
  297. }
  298. //---------- Some usefull macros ---------------//
  299. #define FETCH_CANVAS(canvas) \
  300. zval *_zval; \
  301. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  302. RETURN_FALSE; \
  303. } \
  304. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  305. #define FETCH_DISPLAY(display) \
  306. zval *_zval; \
  307. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  308. RETURN_FALSE; \
  309. } \
  310. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  311. #define FETCH_EVENT(event) \
  312. zval *_zval; \
  313. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
  314. RETURN_FALSE; \
  315. } \
  316. ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event);
  317. #define FETCH_LONG(l) \
  318. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &l) == FAILURE) { \
  319. RETURN_FALSE; \
  320. }
  321. #define RETURN_CHAR(c) \
  322. char *str = emalloc(2); \
  323. str[0] = c; \
  324. str[1] = '\0'; \
  325. RETURN_STRING(str, 0);
  326. #define RETURN_SUCCESS(i) \
  327. RETURN_BOOL((i) == 0);
  328. //---------- Some usefull functions --------------------//
  329. //Fetch external php resources such as gd resouces
  330. void *fetch_external_resource(zval *_zval, char const *type_name) {
  331. int resource_id = _zval->value.lval;
  332. int resource_type;
  333. void *result = zend_list_find(resource_id, &resource_type);
  334. if (!result)
  335. return NULL;
  336. char *resource_type_name = zend_rsrc_list_get_rsrc_type(resource_id);
  337. return (strcmp(resource_type_name, type_name) == 0) ? result : NULL;
  338. }
  339. //Fetch buffer of pixels from gdImage
  340. #ifdef HAVE_GD
  341. void *gd_get_pixels(gdImage *img) {
  342. void *result;
  343. int i, j, pitch;
  344. if (img->trueColor) {
  345. pitch = img->sx * sizeof(int);
  346. result = malloc(img->sy * pitch);
  347. if (result)
  348. {
  349. for (i = 0; i < img->sy; i++) {
  350. for (j = 0; j < img->sx; j++) {
  351. int *dst = (int *) (((char *) result) + i * pitch + j * sizeof(int));
  352. *dst = (((255 - (uint8_t) ((((uint32_t) img->tpixels[i][j]) & 0x7f000000) >> 23)) << 24)
  353. |
  354. (img->tpixels[i][j] & 0x00ffffff));
  355. }
  356. }
  357. }
  358. }
  359. else {
  360. pitch = img->sx * sizeof(char);
  361. result = malloc(img->sy * pitch);
  362. for (j = 0; j < img->sy; j++)
  363. memcpy(result + (j * pitch), (const void *) img->pixels[j], pitch);
  364. }
  365. return result;
  366. }
  367. int gd_load_palette(gdImage *img, caca_dither_t *dither) {
  368. if (!img || img->trueColor || gdMaxColors != 256) {
  369. return -1;
  370. }
  371. uint32_t r[256], g[256], b[256], a[256];
  372. int i;
  373. for (i = 0; i < 256; i++) {
  374. r[i] = img->red[i] << 4;
  375. g[i] = img->green[i] << 4;
  376. b[i] = img->blue[i] << 4;
  377. a[i] = img->alpha[i] << 4;
  378. }
  379. return caca_set_dither_palette(dither, &r[0], &g[0], &b[0], &a[0]);
  380. }
  381. #endif
  382. //------- Caca's functions ----------------//
  383. PHP_FUNCTION(caca_create_canvas) {
  384. long width, height = 0;
  385. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &width, &height) == FAILURE) {
  386. RETURN_FALSE;
  387. }
  388. caca_canvas_t *canvas = caca_create_canvas(width, height);
  389. if (!canvas) {
  390. RETURN_FALSE;
  391. }
  392. ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
  393. }
  394. PHP_FUNCTION(caca_set_canvas_size) {
  395. zval *_zval;
  396. long width, height = 0;
  397. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &width, &height) == FAILURE) {
  398. RETURN_FALSE;
  399. }
  400. caca_canvas_t *canvas;
  401. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  402. RETURN_SUCCESS(caca_set_canvas_size(canvas, width, height));
  403. }
  404. PHP_FUNCTION(caca_get_canvas_width) {
  405. caca_canvas_t *canvas;
  406. FETCH_CANVAS(canvas);
  407. RETURN_LONG(caca_get_canvas_width(canvas));
  408. }
  409. PHP_FUNCTION(caca_get_canvas_height) {
  410. caca_canvas_t *canvas;
  411. FETCH_CANVAS(canvas);
  412. RETURN_LONG(caca_get_canvas_height(canvas));
  413. }
  414. PHP_FUNCTION(caca_get_canvas_chars) {
  415. caca_canvas_t *canvas;
  416. FETCH_CANVAS(canvas);
  417. RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1);
  418. }
  419. PHP_FUNCTION(caca_get_canvas_attrs) {
  420. caca_canvas_t *canvas;
  421. FETCH_CANVAS(canvas);
  422. RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1);
  423. }
  424. PHP_FUNCTION(caca_rand) {
  425. long min, max = 0;
  426. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &min, &max) == FAILURE) {
  427. RETURN_FALSE;
  428. }
  429. RETURN_LONG(caca_rand(min, max));
  430. }
  431. PHP_FUNCTION(caca_get_version) {
  432. if (ZEND_NUM_ARGS() != 0) {
  433. WRONG_PARAM_COUNT;
  434. }
  435. RETURN_STRING((char *) caca_get_version(), 1);
  436. }
  437. PHP_FUNCTION(caca_gotoxy) {
  438. zval *_zval;
  439. long x, y = 0;
  440. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  441. RETURN_FALSE;
  442. }
  443. caca_canvas_t *canvas;
  444. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  445. RETURN_SUCCESS(caca_gotoxy(canvas, x, y));
  446. }
  447. PHP_FUNCTION(caca_wherex) {
  448. caca_canvas_t *canvas;
  449. FETCH_CANVAS(canvas);
  450. RETURN_LONG(caca_wherex(canvas));
  451. }
  452. PHP_FUNCTION(caca_wherey) {
  453. caca_canvas_t *canvas;
  454. FETCH_CANVAS(canvas);
  455. RETURN_LONG(caca_wherey(canvas));
  456. }
  457. PHP_FUNCTION(caca_put_char) {
  458. zval *_zval;
  459. long x, y, c;
  460. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == 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_put_char(canvas, x, y, c));
  466. }
  467. PHP_FUNCTION(caca_get_char) {
  468. zval *_zval;
  469. long x, y = 0;
  470. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  471. RETURN_FALSE;
  472. }
  473. caca_canvas_t *canvas;
  474. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  475. RETURN_CHAR(caca_get_char(canvas, x, y));
  476. }
  477. PHP_FUNCTION(caca_put_str) {
  478. zval *_zval;
  479. char *str;
  480. long x, y, str_len = 0;
  481. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &str, &str_len) == FAILURE) {
  482. RETURN_FALSE;
  483. }
  484. caca_canvas_t *canvas;
  485. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  486. RETURN_SUCCESS(caca_put_str(canvas, x, y, str));
  487. }
  488. PHP_FUNCTION(caca_clear_canvas) {
  489. caca_canvas_t *canvas;
  490. FETCH_CANVAS(canvas);
  491. RETURN_LONG(caca_clear_canvas(canvas));
  492. }
  493. PHP_FUNCTION(caca_set_canvas_handle) {
  494. zval *_zval;
  495. long x, y = 0;
  496. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
  497. RETURN_FALSE;
  498. }
  499. caca_canvas_t *canvas;
  500. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  501. RETURN_SUCCESS(caca_set_canvas_handle(canvas, x, y));
  502. }
  503. PHP_FUNCTION(caca_get_canvas_handle_x) {
  504. caca_canvas_t *canvas;
  505. FETCH_CANVAS(canvas);
  506. RETURN_LONG(caca_get_canvas_handle_x(canvas));
  507. }
  508. PHP_FUNCTION(caca_get_canvas_handle_y) {
  509. caca_canvas_t *canvas;
  510. FETCH_CANVAS(canvas);
  511. RETURN_LONG(caca_get_canvas_handle_y(canvas));
  512. }
  513. PHP_FUNCTION(caca_blit) {
  514. zval *_zval1, *_zval2, *_zval3 = NULL;
  515. long x, y = 0;
  516. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllr|r", &_zval1, &x, &y, &_zval2, &_zval3) == FAILURE) {
  517. RETURN_FALSE;
  518. }
  519. caca_canvas_t *dst, *src, *mask = NULL;
  520. ZEND_FETCH_RESOURCE(dst, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  521. ZEND_FETCH_RESOURCE(src, caca_canvas_t*, &_zval2, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  522. if (_zval3) {
  523. ZEND_FETCH_RESOURCE(mask, caca_canvas_t*, &_zval3, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  524. }
  525. RETURN_SUCCESS(caca_blit(dst, x, y, src, NULL));
  526. }
  527. PHP_FUNCTION(caca_set_canvas_boundaries) {
  528. zval *_zval;
  529. long x, y, width, height = 0;
  530. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &width, &height) == FAILURE) {
  531. RETURN_FALSE;
  532. }
  533. caca_canvas_t *canvas;
  534. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  535. RETURN_SUCCESS(caca_set_canvas_boundaries(canvas, x, y, width, height));
  536. }
  537. PHP_FUNCTION(caca_invert) {
  538. caca_canvas_t *canvas;
  539. FETCH_CANVAS(canvas);
  540. RETURN_SUCCESS(caca_invert(canvas));
  541. }
  542. PHP_FUNCTION(caca_flip) {
  543. caca_canvas_t *canvas;
  544. FETCH_CANVAS(canvas);
  545. RETURN_SUCCESS(caca_flip(canvas));
  546. }
  547. PHP_FUNCTION(caca_flop) {
  548. caca_canvas_t *canvas;
  549. FETCH_CANVAS(canvas);
  550. RETURN_SUCCESS(caca_flop(canvas));
  551. }
  552. PHP_FUNCTION(caca_rotate_180) {
  553. caca_canvas_t *canvas;
  554. FETCH_CANVAS(canvas);
  555. RETURN_SUCCESS(caca_rotate_180(canvas));
  556. }
  557. PHP_FUNCTION(caca_rotate_left) {
  558. caca_canvas_t *canvas;
  559. FETCH_CANVAS(canvas);
  560. RETURN_SUCCESS(caca_rotate_left(canvas));
  561. }
  562. PHP_FUNCTION(caca_rotate_right) {
  563. caca_canvas_t *canvas;
  564. FETCH_CANVAS(canvas);
  565. RETURN_SUCCESS(caca_rotate_right(canvas));
  566. }
  567. PHP_FUNCTION(caca_stretch_left) {
  568. caca_canvas_t *canvas;
  569. FETCH_CANVAS(canvas);
  570. RETURN_SUCCESS(caca_stretch_left(canvas));
  571. }
  572. PHP_FUNCTION(caca_stretch_right) {
  573. caca_canvas_t *canvas;
  574. FETCH_CANVAS(canvas);
  575. RETURN_SUCCESS(caca_stretch_right(canvas));
  576. }
  577. PHP_FUNCTION(caca_get_attr) {
  578. zval *_zval;
  579. long x, y = 0;
  580. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == 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_LONG(caca_get_attr(canvas, x, y));
  586. }
  587. PHP_FUNCTION(caca_set_attr) {
  588. zval *_zval;
  589. long attr = 0;
  590. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &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_set_attr(canvas, attr));
  596. }
  597. PHP_FUNCTION(caca_put_attr) {
  598. zval *_zval;
  599. long x, y, attr = 0;
  600. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &attr) == 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_put_attr(canvas, x, y, attr));
  606. }
  607. PHP_FUNCTION(caca_set_color_ansi) {
  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_ansi(canvas, foreground, background));
  616. }
  617. PHP_FUNCTION(caca_set_color_argb) {
  618. zval *_zval;
  619. long foreground, background = 0;
  620. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
  621. RETURN_FALSE;
  622. }
  623. caca_canvas_t *canvas;
  624. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  625. RETURN_SUCCESS(caca_set_color_argb(canvas, foreground, background));
  626. }
  627. PHP_FUNCTION(caca_attr_to_ansi) {
  628. long l;
  629. FETCH_LONG(l);
  630. RETURN_LONG(caca_attr_to_ansi(l));
  631. }
  632. PHP_FUNCTION(caca_attr_to_ansi_fg) {
  633. long l;
  634. FETCH_LONG(l);
  635. RETURN_LONG(caca_attr_to_ansi_fg(l));
  636. }
  637. PHP_FUNCTION(caca_attr_to_ansi_bg) {
  638. long l;
  639. FETCH_LONG(l);
  640. RETURN_LONG(caca_attr_to_ansi_bg(l));
  641. }
  642. PHP_FUNCTION(caca_attr_to_rgb12_fg) {
  643. long l;
  644. FETCH_LONG(l);
  645. RETURN_LONG(caca_attr_to_rgb12_fg(l));
  646. }
  647. PHP_FUNCTION(caca_attr_to_rgb12_bg) {
  648. long l;
  649. FETCH_LONG(l);
  650. RETURN_LONG(caca_attr_to_rgb12_bg(l));
  651. }
  652. PHP_FUNCTION(caca_attr_to_argb64) {
  653. //TODO: write or delete
  654. }
  655. PHP_FUNCTION(caca_utf8_to_utf32) {
  656. //TODO: write or delete
  657. }
  658. PHP_FUNCTION(caca_utf32_to_utf8) {
  659. //TODO: write or delete
  660. }
  661. PHP_FUNCTION(caca_utf32_to_cp437) {
  662. long l;
  663. FETCH_LONG(l);
  664. RETURN_LONG(caca_utf32_to_cp437(l));
  665. }
  666. PHP_FUNCTION(caca_cp437_to_utf32) {
  667. long l;
  668. FETCH_LONG(l);
  669. RETURN_LONG(caca_cp437_to_utf32(l));
  670. }
  671. PHP_FUNCTION(caca_utf32_to_ascii) {
  672. long l;
  673. FETCH_LONG(l);
  674. RETURN_CHAR(caca_utf32_to_ascii(l));
  675. }
  676. PHP_FUNCTION(caca_utf32_is_fullwidth) {
  677. //TODO: write or delete
  678. }
  679. PHP_FUNCTION(caca_draw_line) {
  680. zval *_zval;
  681. long xa, ya, xb, yb, c;
  682. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == FAILURE) {
  683. RETURN_FALSE;
  684. }
  685. caca_canvas_t *canvas;
  686. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  687. RETURN_SUCCESS(caca_draw_line(canvas, xa, ya, xb, yb, c));
  688. }
  689. PHP_FUNCTION(caca_draw_polyline) {
  690. zval *zval_res, *zval_arr;
  691. long c;
  692. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ral", &zval_res, &zval_arr, &c) == FAILURE) {
  693. RETURN_FALSE;
  694. }
  695. caca_canvas_t *canvas;
  696. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  697. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  698. int lenmax = zend_hash_num_elements(arr_hash);
  699. int *tbl_x, *tbl_y;
  700. int tbl_count = 0;
  701. tbl_x = malloc(sizeof(int) * lenmax);
  702. tbl_y = malloc(sizeof(int) * lenmax);
  703. HashPosition pos;
  704. zval **pt, **x, **y;
  705. for (
  706. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  707. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  708. zend_hash_move_forward_ex(arr_hash, &pos)
  709. ) {
  710. if (
  711. Z_TYPE_P(*pt) == IS_ARRAY
  712. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  713. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  714. ) {
  715. convert_to_long_ex(x);
  716. convert_to_long_ex(y);
  717. tbl_x[tbl_count] = Z_LVAL_PP(x);
  718. tbl_y[tbl_count] = Z_LVAL_PP(y);
  719. tbl_count++;
  720. }
  721. }
  722. int res = caca_draw_polyline(canvas, tbl_x, tbl_y, tbl_count - 1, c);
  723. free(tbl_x);
  724. free(tbl_y);
  725. RETURN_SUCCESS(res);
  726. }
  727. PHP_FUNCTION(caca_draw_thin_line) {
  728. zval *_zval;
  729. long xa, ya, xb, yb = 0;
  730. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
  731. RETURN_FALSE;
  732. }
  733. caca_canvas_t *canvas;
  734. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  735. RETURN_SUCCESS(caca_draw_thin_line(canvas, xa, ya, xb, yb));
  736. }
  737. PHP_FUNCTION(caca_draw_thin_polyline) {
  738. zval *zval_res, *zval_arr;
  739. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &zval_arr) == FAILURE) {
  740. RETURN_FALSE;
  741. }
  742. caca_canvas_t *canvas;
  743. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  744. HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
  745. int lenmax = zend_hash_num_elements(arr_hash);
  746. int *tbl_x, *tbl_y;
  747. int tbl_count = 0;
  748. tbl_x = malloc(sizeof(int) * lenmax);
  749. tbl_y = malloc(sizeof(int) * lenmax);
  750. if (!tbl_x || !tbl_y) {
  751. RETURN_FALSE;
  752. }
  753. HashPosition pos;
  754. zval **pt, **x, **y;
  755. for (
  756. zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
  757. zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
  758. zend_hash_move_forward_ex(arr_hash, &pos)
  759. ) {
  760. if (
  761. Z_TYPE_P(*pt) == IS_ARRAY
  762. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
  763. && (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
  764. ) {
  765. convert_to_long_ex(x);
  766. convert_to_long_ex(y);
  767. tbl_x[tbl_count] = Z_LVAL_PP(x);
  768. tbl_y[tbl_count] = Z_LVAL_PP(y);
  769. tbl_count++;
  770. }
  771. }
  772. int res = caca_draw_thin_polyline(canvas, tbl_x, tbl_y, tbl_count - 1);
  773. free(tbl_x);
  774. free(tbl_y);
  775. RETURN_SUCCESS(res);
  776. }
  777. PHP_FUNCTION(caca_draw_circle) {
  778. zval *_zval;
  779. long x, y, r, c;
  780. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &r, &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_circle(canvas, x, y, r, c));
  786. }
  787. PHP_FUNCTION(caca_draw_ellipse) {
  788. zval *_zval;
  789. long xa, ya, xb, yb, c = 0;
  790. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == 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_ellipse(canvas, xa, ya, xb, yb, c));
  796. }
  797. PHP_FUNCTION(caca_draw_thin_ellipse) {
  798. zval *_zval;
  799. long xa, ya, xb, yb = 0;
  800. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == 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_draw_thin_ellipse(canvas, xa, ya, xb, yb));
  806. }
  807. PHP_FUNCTION(caca_fill_ellipse) {
  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_fill_ellipse(canvas, xa, ya, xb, yb, c));
  816. }
  817. PHP_FUNCTION(caca_draw_box) {
  818. zval *_zval;
  819. long xa, ya, xb, yb, c;
  820. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &c) == 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_box(canvas, xa, ya, xb, yb, c));
  826. }
  827. PHP_FUNCTION(caca_draw_thin_box) {
  828. zval *_zval;
  829. long xa, ya, xb, yb = 0;
  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_thin_box(canvas, xa, ya, xb, yb));
  836. }
  837. PHP_FUNCTION(caca_draw_cp437_box) {
  838. zval *_zval;
  839. long xa, ya, xb, yb;
  840. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == 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_draw_cp437_box(canvas, xa, ya, xb, yb));
  846. }
  847. PHP_FUNCTION(caca_fill_box) {
  848. zval *_zval;
  849. long xa, ya, xb, yb, c;
  850. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &_zval, &xa, &ya, &xb, &yb, &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_fill_box(canvas, xa, ya, xb, yb, c));
  856. }
  857. PHP_FUNCTION(caca_draw_triangle) {
  858. zval *_zval;
  859. long xa, ya, xb, yb, xc, yc, c;
  860. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == 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_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  866. }
  867. PHP_FUNCTION(caca_draw_thin_triangle) {
  868. zval *_zval;
  869. long xa, ya, xb, yb, xc, yc = 0;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc) == 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_draw_thin_triangle(canvas, xa, ya, xb, yb, xc, yc));
  876. }
  877. PHP_FUNCTION(caca_fill_triangle) {
  878. zval *_zval;
  879. long xa, ya, xb, yb, xc, yc, c = 0;
  880. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &_zval, &xa, &ya, &xb, &yb, &xc, &yc, &c) == FAILURE) {
  881. RETURN_FALSE;
  882. }
  883. caca_canvas_t *canvas;
  884. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  885. RETURN_SUCCESS(caca_fill_triangle(canvas, xa, ya, xb, yb, xc, yc, c));
  886. }
  887. PHP_FUNCTION(caca_get_frame_count) {
  888. zval *_zval;
  889. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == 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_LONG(caca_get_canvas_count(canvas));
  895. }
  896. PHP_FUNCTION(caca_set_frame) {
  897. zval *_zval;
  898. long id;
  899. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  900. RETURN_FALSE;
  901. }
  902. caca_canvas_t *canvas;
  903. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  904. RETURN_SUCCESS(caca_set_frame(canvas, id));
  905. }
  906. PHP_FUNCTION(caca_set_frame_name) {
  907. zval *_zval;
  908. char *str;
  909. long str_len;
  910. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == 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_set_frame_name(canvas, str));
  916. }
  917. PHP_FUNCTION(caca_create_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_create_frame(canvas, id));
  926. }
  927. PHP_FUNCTION(caca_free_frame) {
  928. zval *_zval;
  929. long id;
  930. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &id) == FAILURE) {
  931. RETURN_FALSE;
  932. }
  933. caca_canvas_t *canvas;
  934. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  935. RETURN_SUCCESS(caca_free_frame(canvas, id));
  936. }
  937. PHP_FUNCTION(caca_create_dither) {
  938. zval *_zval;
  939. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  940. RETURN_FALSE;
  941. }
  942. #ifdef HAVE_GD
  943. gdImage *img = fetch_external_resource(_zval, "gd");
  944. if (!img) {
  945. RETURN_FALSE;
  946. }
  947. caca_dither_t *dither;
  948. if (img->trueColor)
  949. dither = caca_create_dither(sizeof(int) * 8, img->sx, img->sy, img->sx * sizeof(int), 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  950. else
  951. dither = caca_create_dither(sizeof(char) * 8, img->sx, img->sy, img->sx * sizeof(char), 0, 0, 0, 0);
  952. if (!dither) {
  953. RETURN_FALSE;
  954. }
  955. ZEND_REGISTER_RESOURCE(return_value, dither, le_caca_dither);
  956. #else
  957. RETURN_FALSE
  958. #endif
  959. }
  960. PHP_FUNCTION(caca_set_dither_palette) {
  961. zval *zval_res, *arr;
  962. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &arr) == FAILURE) {
  963. RETURN_FALSE;
  964. }
  965. caca_dither_t *dither;
  966. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_dither);
  967. uint32_t tbl[4][256];
  968. zval **color, **value;
  969. int i, j;
  970. for (i = 0; i < 256; i++) {
  971. if (zend_hash_index_find(Z_ARRVAL_P(arr), i, (void**) &color) == FAILURE || Z_TYPE_P(*color) != IS_ARRAY) {
  972. RETURN_FALSE;
  973. }
  974. for (j = 0; j < 4; j++) {
  975. if (zend_hash_index_find(Z_ARRVAL_P(*color), j, (void**) &value) == FAILURE) {
  976. RETURN_FALSE;
  977. }
  978. convert_to_long_ex(value);
  979. tbl[j][i] = Z_LVAL_PP(value);
  980. }
  981. }
  982. RETURN_SUCCESS(caca_set_dither_palette(dither, tbl[0], tbl[1], tbl[2], tbl[3]));
  983. }
  984. PHP_FUNCTION(caca_set_dither_brightness) {
  985. zval *_zval;
  986. double value;
  987. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  988. RETURN_FALSE;
  989. }
  990. caca_dither_t *dither;
  991. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  992. RETURN_SUCCESS(caca_set_dither_brightness(dither, value));
  993. }
  994. PHP_FUNCTION(caca_get_dither_brightness) {
  995. zval *_zval;
  996. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == 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_DOUBLE(caca_get_dither_brightness(dither));
  1002. }
  1003. PHP_FUNCTION(caca_set_dither_gamma) {
  1004. zval *_zval;
  1005. double value;
  1006. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == 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_SUCCESS(caca_set_dither_gamma(dither, value));
  1012. }
  1013. PHP_FUNCTION(caca_get_dither_gamma) {
  1014. zval *_zval;
  1015. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == 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_DOUBLE(caca_get_dither_gamma(dither));
  1021. }
  1022. PHP_FUNCTION(caca_set_dither_contrast) {
  1023. zval *_zval;
  1024. double value;
  1025. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &_zval, &value) == FAILURE) {
  1026. RETURN_FALSE;
  1027. }
  1028. caca_dither_t *dither;
  1029. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1030. RETURN_SUCCESS(caca_set_dither_contrast(dither, value));
  1031. }
  1032. PHP_FUNCTION(caca_get_dither_contrast) {
  1033. zval *_zval;
  1034. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1035. RETURN_FALSE;
  1036. }
  1037. caca_dither_t *dither;
  1038. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1039. RETURN_DOUBLE(caca_get_dither_contrast(dither));
  1040. }
  1041. PHP_FUNCTION(caca_set_dither_antialias) {
  1042. zval *_zval;
  1043. int str_len;
  1044. char *str;
  1045. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1046. RETURN_FALSE;
  1047. }
  1048. caca_dither_t *dither;
  1049. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1050. RETURN_SUCCESS(caca_set_dither_antialias(dither, str));
  1051. }
  1052. PHP_FUNCTION(caca_get_dither_antialias_list) {
  1053. zval *_zval;
  1054. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1055. RETURN_FALSE;
  1056. }
  1057. caca_dither_t *dither;
  1058. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1059. char const * const *list = caca_get_dither_antialias_list(dither);
  1060. int i;
  1061. array_init(return_value);
  1062. for(i = 0; list[i]; i += 1)
  1063. add_next_index_string(return_value, (char*) list[i], 1);
  1064. }
  1065. PHP_FUNCTION(caca_get_dither_antialias) {
  1066. zval *_zval;
  1067. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1068. RETURN_FALSE;
  1069. }
  1070. caca_dither_t *dither;
  1071. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1072. RETURN_STRING((char *) caca_get_dither_antialias(dither), 1);
  1073. }
  1074. PHP_FUNCTION(caca_set_dither_color) {
  1075. zval *_zval;
  1076. int str_len;
  1077. char *str;
  1078. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1079. RETURN_FALSE;
  1080. }
  1081. caca_dither_t *dither;
  1082. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1083. RETURN_SUCCESS(caca_set_dither_color(dither, str));
  1084. }
  1085. PHP_FUNCTION(caca_get_dither_color_list) {
  1086. zval *_zval;
  1087. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1088. RETURN_FALSE;
  1089. }
  1090. caca_dither_t *dither;
  1091. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1092. char const * const *list = caca_get_dither_antialias_list(dither);
  1093. int i;
  1094. array_init(return_value);
  1095. for(i = 0; list[i]; i += 2)
  1096. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1097. }
  1098. PHP_FUNCTION(caca_get_dither_color) {
  1099. zval *_zval;
  1100. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1101. RETURN_FALSE;
  1102. }
  1103. caca_dither_t *dither;
  1104. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1105. RETURN_STRING((char *) caca_get_dither_color(dither), 1);
  1106. }
  1107. PHP_FUNCTION(caca_set_dither_charset) {
  1108. zval *_zval;
  1109. int str_len;
  1110. char *str;
  1111. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1112. RETURN_FALSE;
  1113. }
  1114. caca_dither_t *dither;
  1115. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1116. RETURN_SUCCESS(caca_set_dither_charset(dither, str));
  1117. }
  1118. PHP_FUNCTION(caca_get_dither_charset_list) {
  1119. zval *_zval;
  1120. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1121. RETURN_FALSE;
  1122. }
  1123. caca_dither_t *dither;
  1124. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1125. char const * const *list = caca_get_dither_charset_list(dither);
  1126. int i;
  1127. array_init(return_value);
  1128. for(i = 0; list[i]; i += 2)
  1129. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1130. }
  1131. PHP_FUNCTION(caca_get_dither_charset) {
  1132. zval *_zval;
  1133. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1134. RETURN_FALSE;
  1135. }
  1136. caca_dither_t *dither;
  1137. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1138. RETURN_STRING((char *) caca_get_dither_charset(dither), 1);
  1139. }
  1140. PHP_FUNCTION(caca_set_dither_algorithm) {
  1141. zval *_zval;
  1142. int str_len;
  1143. char *str;
  1144. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1145. RETURN_FALSE;
  1146. }
  1147. caca_dither_t *dither;
  1148. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1149. RETURN_SUCCESS(caca_set_dither_algorithm(dither, str));
  1150. }
  1151. PHP_FUNCTION(caca_get_dither_algorithm_list) {
  1152. zval *_zval;
  1153. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1154. RETURN_FALSE;
  1155. }
  1156. caca_dither_t *dither;
  1157. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1158. char const * const *list = caca_get_dither_algorithm_list(dither);
  1159. int i;
  1160. array_init(return_value);
  1161. for(i = 0; list[i]; i += 2)
  1162. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1163. }
  1164. PHP_FUNCTION(caca_get_dither_algorithm) {
  1165. zval *_zval;
  1166. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1167. RETURN_FALSE;
  1168. }
  1169. caca_dither_t *dither;
  1170. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1171. RETURN_STRING((char *) caca_get_dither_algorithm(dither), 1);
  1172. }
  1173. PHP_FUNCTION(caca_dither_bitmap) {
  1174. zval *_zval1, *_zval2, *_zval3;
  1175. long x, y, w, h = 0;
  1176. zend_bool load_palette = 1;
  1177. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllrr|b", &_zval1, &x, &y, &w, &h, &_zval2, &_zval3, &load_palette) == FAILURE) {
  1178. RETURN_FALSE;
  1179. }
  1180. caca_canvas_t *canvas;
  1181. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1182. caca_dither_t *dither;
  1183. ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval2, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
  1184. #ifdef HAVE_GD
  1185. gdImage *img = fetch_external_resource(_zval3, "gd");
  1186. if (!img) {
  1187. RETURN_FALSE;
  1188. }
  1189. void *pixels = gd_get_pixels(img);
  1190. if (!pixels) {
  1191. RETURN_FALSE;
  1192. }
  1193. //load palette if image is not true color
  1194. if (load_palette && !img->trueColor && gd_load_palette(img, dither) != 0) {
  1195. free(pixels);
  1196. RETURN_FALSE;
  1197. }
  1198. caca_dither_bitmap(canvas, x, y, w, h, dither, pixels);
  1199. free(pixels);
  1200. RETURN_TRUE;
  1201. #else
  1202. RETURN_FALSE;
  1203. #endif
  1204. }
  1205. PHP_FUNCTION(caca_load_font) {
  1206. char *str;
  1207. long str_len;
  1208. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1209. RETURN_FALSE;
  1210. }
  1211. caca_font_t *font = caca_load_font(str, str_len);
  1212. if (!font) {
  1213. RETURN_FALSE;
  1214. }
  1215. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1216. }
  1217. PHP_FUNCTION(caca_load_builtin_font) {
  1218. char *str;
  1219. long str_len;
  1220. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1221. RETURN_FALSE;
  1222. }
  1223. caca_font_t *font = caca_load_font(str, 0);
  1224. if (!font) {
  1225. RETURN_FALSE;
  1226. }
  1227. ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
  1228. }
  1229. PHP_FUNCTION(caca_get_font_list) {
  1230. if (ZEND_NUM_ARGS() != 0) {
  1231. WRONG_PARAM_COUNT;
  1232. }
  1233. char const * const *list = caca_get_font_list();
  1234. int i;
  1235. array_init(return_value);
  1236. for(i = 0; list[i]; i += 1)
  1237. add_next_index_string(return_value, (char*) list[i], 1);
  1238. }
  1239. PHP_FUNCTION(caca_get_font_width) {
  1240. zval *_zval;
  1241. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1242. RETURN_FALSE;
  1243. }
  1244. caca_font_t *font;
  1245. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1246. RETURN_LONG(caca_get_font_width(font));
  1247. }
  1248. PHP_FUNCTION(caca_get_font_height) {
  1249. zval *_zval;
  1250. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1251. RETURN_FALSE;
  1252. }
  1253. caca_font_t *font;
  1254. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1255. RETURN_LONG(caca_get_font_height(font));
  1256. }
  1257. PHP_FUNCTION(caca_get_font_blocks) {
  1258. zval *_zval;
  1259. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1260. RETURN_FALSE;
  1261. }
  1262. caca_font_t *font;
  1263. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1264. uint32_t const *list = caca_get_font_blocks(font);
  1265. int i;
  1266. array_init(return_value);
  1267. for(i = 0; list[i]; i += 1)
  1268. add_next_index_long(return_value, list[i]);
  1269. }
  1270. PHP_FUNCTION(caca_render_canvas) {
  1271. zval *_zval1, *_zval2, *_zval3;
  1272. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrr", &_zval1, &_zval2, &_zval3) == FAILURE) {
  1273. RETURN_FALSE;
  1274. }
  1275. caca_canvas_t *canvas;
  1276. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1277. caca_font_t *font;
  1278. ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval2, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
  1279. #ifdef HAVE_GD
  1280. gdImage *img = fetch_external_resource(_zval3, "gd");
  1281. if (!img || !img->trueColor) {
  1282. RETURN_FALSE;
  1283. }
  1284. int pitch = img->sx * 4;
  1285. uint8_t *buffer = malloc(pitch * img->sy);
  1286. if (!buffer) {
  1287. RETURN_FALSE;
  1288. }
  1289. caca_render_canvas(canvas, font, (void *) buffer, img->sx, img->sy, pitch);
  1290. int i, j;
  1291. for (i = 0; i < img->sy; i++) {
  1292. for (j = 0; j < img->sx; j++) {
  1293. uint8_t *src = buffer + i * pitch + j * 4;
  1294. img->tpixels[i][j] = ((int) src[3]) | (((int) src[2]) << 8) | (((int) src[1]) << 16) | (((255 - (int) src[0]) & 0xfe) << 23);
  1295. }
  1296. }
  1297. free(buffer);
  1298. RETURN_TRUE;
  1299. #else
  1300. RETURN_FALSE;
  1301. #endif
  1302. }
  1303. PHP_FUNCTION(caca_canvas_set_figfont) {
  1304. zval *_zval;
  1305. char *font;
  1306. long font_len;
  1307. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &font, &font_len) == FAILURE) {
  1308. RETURN_FALSE;
  1309. }
  1310. caca_canvas_t *canvas;
  1311. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1312. RETURN_SUCCESS(caca_canvas_set_figfont(canvas, font));
  1313. }
  1314. PHP_FUNCTION(caca_put_figchar) {
  1315. zval *_zval;
  1316. long c;
  1317. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &c) == FAILURE) {
  1318. RETURN_FALSE;
  1319. }
  1320. caca_canvas_t *canvas;
  1321. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1322. RETURN_SUCCESS(caca_put_figchar(canvas, c));
  1323. }
  1324. PHP_FUNCTION(caca_flush_figlet) {
  1325. zval *_zval;
  1326. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1327. RETURN_FALSE;
  1328. }
  1329. caca_canvas_t *canvas;
  1330. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1331. RETURN_SUCCESS(caca_flush_figlet(canvas));
  1332. }
  1333. PHP_FUNCTION(caca_file_open) {
  1334. char *path, *mode;
  1335. long path_len, mode_len;
  1336. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) {
  1337. RETURN_FALSE;
  1338. }
  1339. caca_file_t *file = caca_file_open(path, mode);
  1340. if (!file) {
  1341. RETURN_FALSE;
  1342. }
  1343. ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file);
  1344. }
  1345. PHP_FUNCTION(caca_file_close) {
  1346. zval *_zval;
  1347. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1348. RETURN_FALSE;
  1349. }
  1350. caca_file_t *file;
  1351. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1352. int res = caca_file_close(file);
  1353. //Delete php resource
  1354. zend_list_delete(_zval->value.lval);
  1355. RETURN_SUCCESS(res);
  1356. }
  1357. PHP_FUNCTION(caca_file_tell) {
  1358. zval *_zval;
  1359. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1360. RETURN_FALSE;
  1361. }
  1362. caca_file_t *file;
  1363. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1364. RETURN_LONG(caca_file_tell(file));
  1365. }
  1366. PHP_FUNCTION(caca_file_read) {
  1367. zval *_zval;
  1368. long len;
  1369. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1370. RETURN_FALSE;
  1371. }
  1372. caca_file_t *file;
  1373. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1374. if (len < 1) {
  1375. RETURN_FALSE;
  1376. }
  1377. char *buffer = emalloc(len);
  1378. if (!buffer) {
  1379. RETURN_FALSE;
  1380. }
  1381. caca_file_read(file, buffer, len);
  1382. RETURN_STRINGL(buffer, len, 1);
  1383. }
  1384. PHP_FUNCTION(caca_file_write) {
  1385. zval *_zval;
  1386. char *buf;
  1387. long buf_len = 0;
  1388. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &buf, &buf_len) == FAILURE) {
  1389. RETURN_FALSE;
  1390. }
  1391. caca_file_t *file;
  1392. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1393. RETURN_LONG(caca_file_write(file, buf, buf_len));
  1394. }
  1395. PHP_FUNCTION(caca_file_gets) {
  1396. zval *_zval;
  1397. long len = 0;
  1398. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &len) == FAILURE) {
  1399. RETURN_FALSE;
  1400. }
  1401. caca_file_t *file;
  1402. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1403. if (len < 1) {
  1404. RETURN_FALSE;
  1405. }
  1406. char *buffer = emalloc(len);
  1407. if (!buffer) {
  1408. RETURN_FALSE;
  1409. }
  1410. char *result = caca_file_gets(file, buffer, len);
  1411. if (!result) {
  1412. RETURN_FALSE;
  1413. }
  1414. RETURN_STRINGL(result, len, 1);
  1415. }
  1416. PHP_FUNCTION(caca_file_eof) {
  1417. zval *_zval;
  1418. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1419. RETURN_FALSE;
  1420. }
  1421. caca_file_t *file;
  1422. ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
  1423. RETURN_BOOL(caca_file_eof(file) != 0);
  1424. }
  1425. PHP_FUNCTION(caca_import_string) {
  1426. zval *_zval;
  1427. char *src, *format;
  1428. long src_len, format_len = 0;
  1429. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_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_canvas_from_memory(canvas, src, src_len, format));
  1435. }
  1436. PHP_FUNCTION(caca_import_file) {
  1437. zval *_zval;
  1438. char *filename, *format;
  1439. long filename_len, format_len = 0;
  1440. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &filename, &filename_len, &format, &format_len) == FAILURE) {
  1441. RETURN_FALSE;
  1442. }
  1443. caca_canvas_t *canvas;
  1444. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1445. RETURN_LONG(caca_import_canvas_from_file(canvas, filename, format));
  1446. }
  1447. PHP_FUNCTION(caca_get_import_list) {
  1448. if (ZEND_NUM_ARGS() != 0) {
  1449. WRONG_PARAM_COUNT;
  1450. }
  1451. char const * const *list = caca_get_import_list();
  1452. int i;
  1453. array_init(return_value);
  1454. for(i = 0; list[i]; i += 2)
  1455. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1456. }
  1457. PHP_FUNCTION(caca_export_string) {
  1458. zval *_zval;
  1459. char *type;
  1460. long type_len;
  1461. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &type, &type_len) == FAILURE) {
  1462. RETURN_FALSE;
  1463. }
  1464. caca_canvas_t *canvas;
  1465. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1466. void *buffer;
  1467. size_t len = 0;
  1468. buffer = caca_export_canvas_to_memory(canvas, type, &len);
  1469. if (!buffer) {
  1470. RETURN_FALSE;
  1471. }
  1472. RETVAL_STRINGL((char*) buffer, len, 1);
  1473. free(buffer);
  1474. }
  1475. PHP_FUNCTION(caca_get_export_list) {
  1476. if (ZEND_NUM_ARGS() != 0) {
  1477. WRONG_PARAM_COUNT;
  1478. }
  1479. char const * const *list = caca_get_export_list();
  1480. int i;
  1481. array_init(return_value);
  1482. for(i = 0; list[i]; i += 2)
  1483. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1484. }
  1485. PHP_FUNCTION(caca_create_display) {
  1486. caca_canvas_t *canvas;
  1487. FETCH_CANVAS(canvas);
  1488. caca_display_t *display = caca_create_display(canvas);
  1489. if (!display) {
  1490. RETURN_FALSE;
  1491. }
  1492. ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
  1493. }
  1494. PHP_FUNCTION(caca_create_display_with_driver) {
  1495. zval *_zval;
  1496. char *str;
  1497. long str_len = 0;
  1498. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1499. RETURN_FALSE;
  1500. }
  1501. caca_canvas_t *canvas;
  1502. ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
  1503. caca_display_t *display = caca_create_display_with_driver(canvas, str);
  1504. if (!display) {
  1505. RETURN_FALSE;
  1506. }
  1507. ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
  1508. }
  1509. PHP_FUNCTION(caca_get_display_driver_list) {
  1510. if (ZEND_NUM_ARGS() != 0) {
  1511. WRONG_PARAM_COUNT;
  1512. }
  1513. char const * const *list = caca_get_display_driver_list();
  1514. int i;
  1515. array_init(return_value);
  1516. for(i = 0; list[i]; i += 2)
  1517. add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
  1518. }
  1519. PHP_FUNCTION(caca_get_display_driver) {
  1520. zval *_zval;
  1521. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1522. RETURN_FALSE;
  1523. }
  1524. caca_display_t *display;
  1525. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1526. RETURN_STRING((char *) caca_get_display_driver(display), 1);
  1527. }
  1528. PHP_FUNCTION(caca_set_display_driver) {
  1529. zval *_zval;
  1530. char *str;
  1531. long str_len;
  1532. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == 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. RETURN_SUCESS(caca_set_display_driver(display, str));
  1538. }
  1539. PHP_FUNCTION(caca_get_canvas) {
  1540. zval *_zval;
  1541. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
  1542. RETURN_FALSE;
  1543. }
  1544. caca_display_t *display;
  1545. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1546. caca_canvas_t *canvas = caca_get_canvas(display);
  1547. if (!canvas) {
  1548. RETURN_FALSE;
  1549. }
  1550. ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
  1551. }
  1552. PHP_FUNCTION(caca_refresh_display) {
  1553. caca_display_t *display;
  1554. FETCH_DISPLAY(display);
  1555. RETURN_SUCCESS(caca_refresh_display(display));
  1556. }
  1557. PHP_FUNCTION(caca_set_display_time) {
  1558. zval *_zval;
  1559. long value = 0;
  1560. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1561. RETURN_FALSE;
  1562. }
  1563. caca_display_t *display;
  1564. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1565. RETURN_SUCCESS(caca_set_display_time(display, value));
  1566. }
  1567. PHP_FUNCTION(caca_get_display_time) {
  1568. caca_display_t *display;
  1569. FETCH_DISPLAY(display);
  1570. RETURN_LONG(caca_get_display_time(display));
  1571. }
  1572. PHP_FUNCTION(caca_get_display_width) {
  1573. caca_display_t *display;
  1574. FETCH_DISPLAY(display);
  1575. RETURN_LONG(caca_get_display_width(display));
  1576. }
  1577. PHP_FUNCTION(caca_get_display_height) {
  1578. caca_display_t *display;
  1579. FETCH_DISPLAY(display);
  1580. RETURN_LONG(caca_get_display_height(display));
  1581. }
  1582. PHP_FUNCTION(caca_set_display_title) {
  1583. zval *_zval;
  1584. char *str;
  1585. long str_len = 0;
  1586. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
  1587. RETURN_FALSE;
  1588. }
  1589. caca_display_t *display;
  1590. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1591. RETURN_SUCCESS(caca_set_display_title(display, str));
  1592. }
  1593. PHP_FUNCTION(caca_set_mouse) {
  1594. zval *_zval;
  1595. long value = 0;
  1596. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1597. RETURN_FALSE;
  1598. }
  1599. caca_display_t *display;
  1600. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1601. RETURN_SUCCESS(caca_set_mouse(display, value));
  1602. }
  1603. PHP_FUNCTION(caca_set_cursor) {
  1604. zval *_zval;
  1605. long value = 0;
  1606. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
  1607. RETURN_FALSE;
  1608. }
  1609. caca_display_t *display;
  1610. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1611. RETURN_SUCCESS(caca_set_cursor(display, value));
  1612. }
  1613. PHP_FUNCTION(caca_get_event) {
  1614. zval *_zval = NULL;
  1615. long g, aa = 0;
  1616. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &_zval, &g, &aa) == FAILURE) {
  1617. RETURN_FALSE;
  1618. }
  1619. caca_display_t *display;
  1620. ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
  1621. caca_event_t *event = emalloc(sizeof(caca_event_t));
  1622. if (!event) {
  1623. RETURN_FALSE;
  1624. }
  1625. caca_get_event(display, g, event, aa);
  1626. if (caca_get_event_type(event) == CACA_EVENT_NONE) {
  1627. efree(event);
  1628. RETURN_FALSE;
  1629. }
  1630. ZEND_REGISTER_RESOURCE(return_value, event, le_caca_event);
  1631. }
  1632. PHP_FUNCTION(caca_get_mouse_x) {
  1633. caca_display_t *display;
  1634. FETCH_DISPLAY(display);
  1635. RETURN_LONG(caca_get_mouse_x(display));
  1636. }
  1637. PHP_FUNCTION(caca_get_mouse_y) {
  1638. caca_display_t *display;
  1639. FETCH_DISPLAY(display);
  1640. RETURN_LONG(caca_get_mouse_y(display));
  1641. }
  1642. PHP_FUNCTION(caca_get_event_type) {
  1643. caca_event_t *event;
  1644. FETCH_EVENT(event);
  1645. RETURN_LONG(caca_get_event_type(event));
  1646. }
  1647. PHP_FUNCTION(caca_get_event_key_ch) {
  1648. caca_event_t *event;
  1649. FETCH_EVENT(event);
  1650. RETURN_LONG(caca_get_event_key_ch(event));
  1651. }
  1652. PHP_FUNCTION(caca_get_event_mouse_button) {
  1653. caca_event_t *event;
  1654. FETCH_EVENT(event);
  1655. RETURN_LONG(caca_get_event_mouse_button(event));
  1656. }
  1657. PHP_FUNCTION(caca_get_event_mouse_x) {
  1658. caca_event_t *event;
  1659. FETCH_EVENT(event);
  1660. RETURN_LONG(caca_get_event_mouse_x(event));
  1661. }
  1662. PHP_FUNCTION(caca_get_event_mouse_y) {
  1663. caca_event_t *event;
  1664. FETCH_EVENT(event);
  1665. RETURN_LONG(caca_get_event_mouse_y(event));
  1666. }
  1667. PHP_FUNCTION(caca_get_event_resize_width) {
  1668. caca_event_t *event;
  1669. FETCH_EVENT(event);
  1670. RETURN_LONG(caca_get_event_resize_width(event));
  1671. }
  1672. PHP_FUNCTION(caca_get_event_resize_height) {
  1673. caca_event_t *event;
  1674. FETCH_EVENT(event);
  1675. RETURN_LONG(caca_get_event_resize_height(event));
  1676. }