source: libcaca/trunk/caca-php/php_caca.c @ 3104

Last change on this file since 3104 was 3104, checked in by nico, 11 years ago
  • Add binding for 6 new caca's functions
  • Add a nice php sample file examples/example1.php that shows a smart pink ping
File size: 27.4 KB
Line 
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
13#ifdef HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include "php.h"
18#include "php_caca.h"
19
20static function_entry caca_functions[] = {
21        PHP_FE(caca_create_canvas, NULL)
22        PHP_FE(caca_manage_canvas, NULL)
23        PHP_FE(caca_unmanage_canvas, NULL)
24        PHP_FE(caca_set_canvas_size, NULL)
25        PHP_FE(caca_get_canvas_width, NULL)
26        PHP_FE(caca_get_canvas_height, NULL)
27        PHP_FE(caca_get_canvas_chars, NULL)
28        PHP_FE(caca_get_canvas_attrs, NULL)
29        PHP_FE(caca_rand, NULL)
30        PHP_FE(caca_get_version, NULL)
31        PHP_FE(caca_gotoxy, NULL)
32        PHP_FE(caca_get_cursor_x, NULL)
33        PHP_FE(caca_get_cursor_y, NULL)
34        PHP_FE(caca_put_char, NULL)
35        PHP_FE(caca_get_char, NULL)
36        PHP_FE(caca_put_str, NULL)
37        PHP_FE(caca_printf, NULL)
38        PHP_FE(caca_clear_canvas, NULL)
39        PHP_FE(caca_set_canvas_handle, NULL)
40        PHP_FE(caca_get_canvas_handle_x, NULL)
41        PHP_FE(caca_get_canvas_handle_y, NULL)
42        PHP_FE(caca_blit, NULL)
43        PHP_FE(caca_set_canvas_boundaries, NULL)
44        PHP_FE(caca_invert, NULL)
45        PHP_FE(caca_flip, NULL)
46        PHP_FE(caca_flop, NULL)
47        PHP_FE(caca_rotate_left, NULL)
48        PHP_FE(caca_rotate_right, NULL)
49        PHP_FE(caca_stretch_left, NULL)
50        PHP_FE(caca_stretch_right, NULL)
51        PHP_FE(caca_get_attr, NULL)
52        PHP_FE(caca_set_attr, NULL)
53        PHP_FE(caca_put_attr, NULL)
54        PHP_FE(caca_set_color_ansi, NULL)
55        PHP_FE(caca_set_color_argb, NULL)
56        PHP_FE(caca_attr_to_ansi, NULL)
57        PHP_FE(caca_attr_to_ansi_fg, NULL)
58        PHP_FE(caca_attr_to_ansi_bg, NULL)
59        PHP_FE(caca_draw_line, NULL)
60        PHP_FE(caca_draw_polyline, NULL)
61        PHP_FE(caca_draw_thin_line, NULL)
62        PHP_FE(caca_draw_thin_polyline, NULL)
63        PHP_FE(caca_draw_circle, NULL)
64        PHP_FE(caca_draw_ellipse, NULL)
65        PHP_FE(caca_draw_thin_ellipse, NULL)
66        PHP_FE(caca_fill_ellipse, NULL)
67        PHP_FE(caca_draw_box, NULL)
68        PHP_FE(caca_draw_thin_box, NULL)
69        PHP_FE(caca_fill_box, NULL)
70        PHP_FE(caca_draw_triangle, NULL)
71        PHP_FE(caca_draw_thin_triangle, NULL)
72        PHP_FE(caca_fill_triangle, NULL)
73        PHP_FE(caca_get_frame_count, NULL)
74        PHP_FE(caca_set_frame, NULL)
75        PHP_FE(caca_set_frame_name, NULL)
76        PHP_FE(caca_create_frame, NULL)
77        PHP_FE(caca_free_frame, NULL)
78        PHP_FE(caca_set_dither_palette, NULL)
79        PHP_FE(caca_set_dither_brightness, NULL)
80        PHP_FE(caca_get_dither_brightness, NULL)
81        PHP_FE(caca_set_dither_gamma, NULL)
82        PHP_FE(caca_get_dither_gamma, NULL)
83        PHP_FE(caca_set_dither_contrast, NULL)
84        PHP_FE(caca_get_dither_contrast, NULL)
85        PHP_FE(caca_set_dither_antialias, NULL)
86        PHP_FE(caca_get_dither_antialias_list, NULL)
87        PHP_FE(caca_get_dither_antialias, NULL)
88        PHP_FE(caca_set_dither_color, NULL)
89        PHP_FE(caca_get_dither_color_list, NULL)
90        PHP_FE(caca_get_dither_color, NULL)
91        PHP_FE(caca_set_dither_charset, NULL)
92        PHP_FE(caca_get_dither_charset_list, NULL)
93        PHP_FE(caca_get_dither_charset, NULL)
94        PHP_FE(caca_set_dither_algorithm, NULL)
95        PHP_FE(caca_get_dither_algorithm_list, NULL)
96        PHP_FE(caca_get_dither_algorithm, NULL)
97        PHP_FE(caca_dither_bitmap, NULL)
98        PHP_FE(caca_get_font_list, NULL)
99        PHP_FE(caca_get_font_width, NULL)
100        PHP_FE(caca_get_font_height, NULL)
101        PHP_FE(caca_render_canvas, NULL)
102        PHP_FE(caca_canvas_set_figfont, NULL)
103        PHP_FE(caca_put_figchar, NULL)
104        PHP_FE(caca_flush_figlet, NULL)
105        PHP_FE(caca_file_close, NULL)
106        PHP_FE(caca_file_tell, NULL)
107        PHP_FE(caca_file_read, NULL)
108        PHP_FE(caca_file_write, NULL)
109        PHP_FE(caca_file_gets, NULL)
110        PHP_FE(caca_file_eof, NULL)
111        PHP_FE(caca_import_memory, NULL)
112        PHP_FE(caca_import_file, NULL)
113        PHP_FE(caca_get_import_list, NULL)
114        PHP_FE(caca_get_export_list, NULL)
115        PHP_FE(caca_create_display, NULL)
116        PHP_FE(caca_create_display_with_driver, NULL)
117        PHP_FE(caca_get_display_driver_list, NULL)
118        PHP_FE(caca_get_display_driver, NULL)
119        PHP_FE(caca_set_display_driver, NULL)
120        PHP_FE(caca_get_canvas, NULL)
121        PHP_FE(caca_refresh_display, NULL)
122        PHP_FE(caca_set_display_time, NULL)
123        PHP_FE(caca_get_display_time, NULL)
124        PHP_FE(caca_get_display_width, NULL)
125        PHP_FE(caca_get_display_height, NULL)
126        PHP_FE(caca_set_display_title, NULL)
127        PHP_FE(caca_set_mouse, NULL)
128        PHP_FE(caca_set_cursor, NULL)
129        PHP_FE(caca_get_event, NULL)
130        PHP_FE(caca_get_mouse_x, NULL)
131        PHP_FE(caca_get_mouse_y, NULL)
132        PHP_FE(caca_get_event_type, NULL)
133        PHP_FE(caca_get_event_key_ch, NULL)
134        PHP_FE(caca_get_event_mouse_button, NULL)
135        PHP_FE(caca_get_event_mouse_x, NULL)
136        PHP_FE(caca_get_event_mouse_y, NULL)
137        PHP_FE(caca_get_event_resize_width, NULL)
138        PHP_FE(caca_get_event_resize_height, NULL)
139
140        {NULL, NULL, NULL}
141};
142
143zend_module_entry caca_module_entry = {
144#if ZEND_MODULE_API_NO >= 20010901
145        STANDARD_MODULE_HEADER,
146#endif
147        PHP_CACA_EXTNAME,
148        caca_functions,
149        PHP_MINIT(caca),
150        NULL,
151        NULL,
152        NULL,
153        PHP_MINFO(caca),
154#if ZEND_MODULE_API_NO >= 20010901
155        PHP_CACA_VERSION,
156#endif
157        STANDARD_MODULE_PROPERTIES
158};
159
160#ifdef COMPILE_DL_CACA
161ZEND_GET_MODULE(caca)
162#endif
163
164PHP_MINFO_FUNCTION(caca) {
165        php_info_print_table_start();
166        php_info_print_table_row(2, "Caca library version", caca_get_version());
167        php_info_print_table_end();
168}
169
170//--------CACA'S RESSOURCES DESTRUCTORS-----------//
171
172static void php_caca_canvas_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
173        caca_free_canvas(rsrc->ptr);
174}
175
176static void php_caca_dither_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
177        caca_free_dither(rsrc->ptr);
178}
179
180static void php_caca_font_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
181        caca_free_font(rsrc->ptr);
182}
183
184static void php_caca_file_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
185}
186
187static void php_caca_display_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
188        caca_free_display(rsrc->ptr);
189}
190
191static void php_caca_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
192}
193
194//--------INITIALIZATION---------//
195
196PHP_MINIT_FUNCTION(caca) {
197
198        le_caca_canvas = zend_register_list_destructors_ex(php_caca_canvas_dtor, NULL, PHP_CACA_CANVAS_RES_NAME, module_number);
199        le_caca_dither = zend_register_list_destructors_ex(php_caca_dither_dtor, NULL, PHP_CACA_DITHER_RES_NAME, module_number);
200        le_caca_font = zend_register_list_destructors_ex(php_caca_font_dtor, NULL, PHP_CACA_FONT_RES_NAME, module_number);
201        le_caca_file = zend_register_list_destructors_ex(php_caca_file_dtor, NULL, PHP_CACA_FILE_RES_NAME, module_number);
202        le_caca_display = zend_register_list_destructors_ex(php_caca_display_dtor, NULL, PHP_CACA_DISPLAY_RES_NAME, module_number);
203        le_caca_event = zend_register_list_destructors_ex(php_caca_event_dtor, NULL, PHP_CACA_EVENT_RES_NAME, module_number);
204       
205        REGISTER_LONG_CONSTANT("CACA_BLACK", CACA_BLACK, CONST_CS | CONST_PERSISTENT);
206        REGISTER_LONG_CONSTANT("CACA_BLUE", CACA_BLUE, CONST_CS | CONST_PERSISTENT);
207        REGISTER_LONG_CONSTANT("CACA_GREEN", CACA_GREEN, CONST_CS | CONST_PERSISTENT);
208        REGISTER_LONG_CONSTANT("CACA_CYAN", CACA_CYAN, CONST_CS | CONST_PERSISTENT);
209        REGISTER_LONG_CONSTANT("CACA_RED", CACA_RED, CONST_CS | CONST_PERSISTENT);
210        REGISTER_LONG_CONSTANT("CACA_MAGENTA", CACA_MAGENTA, CONST_CS | CONST_PERSISTENT);
211        REGISTER_LONG_CONSTANT("CACA_BROWN", CACA_BROWN, CONST_CS | CONST_PERSISTENT);
212        REGISTER_LONG_CONSTANT("CACA_LIGHTGRAY", CACA_LIGHTGRAY, CONST_CS | CONST_PERSISTENT);
213        REGISTER_LONG_CONSTANT("CACA_DARKGRAY", CACA_DARKGRAY, CONST_CS | CONST_PERSISTENT);
214        REGISTER_LONG_CONSTANT("CACA_LIGHTBLUE", CACA_LIGHTBLUE, CONST_CS | CONST_PERSISTENT);
215        REGISTER_LONG_CONSTANT("CACA_LIGHTGREEN", CACA_LIGHTGREEN, CONST_CS | CONST_PERSISTENT);
216        REGISTER_LONG_CONSTANT("CACA_LIGHTCYAN", CACA_LIGHTCYAN, CONST_CS | CONST_PERSISTENT);
217        REGISTER_LONG_CONSTANT("CACA_LIGHTRED", CACA_LIGHTRED, CONST_CS | CONST_PERSISTENT);
218        REGISTER_LONG_CONSTANT("CACA_LIGHTMAGENTA", CACA_LIGHTMAGENTA, CONST_CS | CONST_PERSISTENT);
219        REGISTER_LONG_CONSTANT("CACA_YELLOW", CACA_YELLOW, CONST_CS | CONST_PERSISTENT);
220        REGISTER_LONG_CONSTANT("CACA_WHITE", CACA_WHITE, CONST_CS | CONST_PERSISTENT);
221        REGISTER_LONG_CONSTANT("CACA_DEFAULT", CACA_DEFAULT, CONST_CS | CONST_PERSISTENT);
222        REGISTER_LONG_CONSTANT("CACA_TRANSPARENT", CACA_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
223        REGISTER_LONG_CONSTANT("CACA_BOLD", CACA_BOLD, CONST_CS | CONST_PERSISTENT);
224        REGISTER_LONG_CONSTANT("CACA_ITALICS", CACA_ITALICS, CONST_CS | CONST_PERSISTENT);
225        REGISTER_LONG_CONSTANT("CACA_UNDERLINE", CACA_UNDERLINE, CONST_CS | CONST_PERSISTENT);
226        REGISTER_LONG_CONSTANT("CACA_BLINK", CACA_BLINK, CONST_CS | CONST_PERSISTENT);
227
228        REGISTER_LONG_CONSTANT("CACA_EVENT_NONE", CACA_EVENT_NONE, CONST_CS | CONST_PERSISTENT);
229        REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_PRESS", CACA_EVENT_KEY_PRESS, CONST_CS | CONST_PERSISTENT);
230        REGISTER_LONG_CONSTANT("CACA_EVENT_KEY_RELEASE", CACA_EVENT_KEY_RELEASE, CONST_CS | CONST_PERSISTENT);
231        REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_PRESS", CACA_EVENT_MOUSE_PRESS, CONST_CS | CONST_PERSISTENT);
232        REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_RELEASE", CACA_EVENT_MOUSE_RELEASE, CONST_CS | CONST_PERSISTENT);
233        REGISTER_LONG_CONSTANT("CACA_EVENT_MOUSE_MOTION", CACA_EVENT_MOUSE_MOTION, CONST_CS | CONST_PERSISTENT);
234        REGISTER_LONG_CONSTANT("CACA_EVENT_RESIZE", CACA_EVENT_RESIZE, CONST_CS | CONST_PERSISTENT);
235        REGISTER_LONG_CONSTANT("CACA_EVENT_QUIT", CACA_EVENT_QUIT, CONST_CS | CONST_PERSISTENT);
236        REGISTER_LONG_CONSTANT("CACA_EVENT_ANY", CACA_EVENT_ANY, CONST_CS | CONST_PERSISTENT);
237
238        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_A", CACA_KEY_CTRL_A, CONST_CS | CONST_PERSISTENT);
239        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_B", CACA_KEY_CTRL_B, CONST_CS | CONST_PERSISTENT);
240        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_C", CACA_KEY_CTRL_C, CONST_CS | CONST_PERSISTENT);
241        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_D", CACA_KEY_CTRL_D, CONST_CS | CONST_PERSISTENT);
242        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_E", CACA_KEY_CTRL_E, CONST_CS | CONST_PERSISTENT);
243        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_F", CACA_KEY_CTRL_F, CONST_CS | CONST_PERSISTENT);
244        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_G", CACA_KEY_CTRL_G, CONST_CS | CONST_PERSISTENT);
245        REGISTER_LONG_CONSTANT("CACA_KEY_BACKSPACE", CACA_KEY_BACKSPACE, CONST_CS | CONST_PERSISTENT);
246        REGISTER_LONG_CONSTANT("CACA_KEY_TAB", CACA_KEY_TAB, CONST_CS | CONST_PERSISTENT);
247        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_J", CACA_KEY_CTRL_J, CONST_CS | CONST_PERSISTENT);
248        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_K", CACA_KEY_CTRL_K, CONST_CS | CONST_PERSISTENT);
249        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_L", CACA_KEY_CTRL_L, CONST_CS | CONST_PERSISTENT);
250        REGISTER_LONG_CONSTANT("CACA_KEY_RETURN", CACA_KEY_RETURN, CONST_CS | CONST_PERSISTENT);
251        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_N", CACA_KEY_CTRL_N, CONST_CS | CONST_PERSISTENT);
252        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_O", CACA_KEY_CTRL_O, CONST_CS | CONST_PERSISTENT);
253        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_P", CACA_KEY_CTRL_P, CONST_CS | CONST_PERSISTENT);
254        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Q", CACA_KEY_CTRL_Q, CONST_CS | CONST_PERSISTENT);
255        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_R", CACA_KEY_CTRL_R, CONST_CS | CONST_PERSISTENT);
256        REGISTER_LONG_CONSTANT("CACA_KEY_PAUSE", CACA_KEY_PAUSE, CONST_CS | CONST_PERSISTENT);
257        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_T", CACA_KEY_CTRL_T, CONST_CS | CONST_PERSISTENT);
258        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_U", CACA_KEY_CTRL_U, CONST_CS | CONST_PERSISTENT);
259        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_V", CACA_KEY_CTRL_V, CONST_CS | CONST_PERSISTENT);
260        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_W", CACA_KEY_CTRL_W, CONST_CS | CONST_PERSISTENT);
261        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_X", CACA_KEY_CTRL_X, CONST_CS | CONST_PERSISTENT);
262        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Y", CACA_KEY_CTRL_Y, CONST_CS | CONST_PERSISTENT);
263        REGISTER_LONG_CONSTANT("CACA_KEY_CTRL_Z", CACA_KEY_CTRL_Z, CONST_CS | CONST_PERSISTENT);
264        REGISTER_LONG_CONSTANT("CACA_KEY_ESCAPE", CACA_KEY_ESCAPE, CONST_CS | CONST_PERSISTENT);
265        REGISTER_LONG_CONSTANT("CACA_KEY_DELETE", CACA_KEY_DELETE, CONST_CS | CONST_PERSISTENT);
266        REGISTER_LONG_CONSTANT("CACA_KEY_UP", CACA_KEY_UP, CONST_CS | CONST_PERSISTENT);
267        REGISTER_LONG_CONSTANT("CACA_KEY_DOWN", CACA_KEY_DOWN, CONST_CS | CONST_PERSISTENT);
268        REGISTER_LONG_CONSTANT("CACA_KEY_LEFT", CACA_KEY_LEFT, CONST_CS | CONST_PERSISTENT);
269        REGISTER_LONG_CONSTANT("CACA_KEY_RIGHT", CACA_KEY_RIGHT, CONST_CS | CONST_PERSISTENT);
270        REGISTER_LONG_CONSTANT("CACA_KEY_INSERT", CACA_KEY_INSERT, CONST_CS | CONST_PERSISTENT);
271        REGISTER_LONG_CONSTANT("CACA_KEY_HOME", CACA_KEY_HOME, CONST_CS | CONST_PERSISTENT);
272        REGISTER_LONG_CONSTANT("CACA_KEY_END", CACA_KEY_END, CONST_CS | CONST_PERSISTENT);
273        REGISTER_LONG_CONSTANT("CACA_KEY_PAGEUP", CACA_KEY_PAGEUP, CONST_CS | CONST_PERSISTENT);
274        REGISTER_LONG_CONSTANT("CACA_KEY_PAGEDOWN", CACA_KEY_PAGEDOWN, CONST_CS | CONST_PERSISTENT);
275        REGISTER_LONG_CONSTANT("CACA_KEY_F1", CACA_KEY_F1, CONST_CS | CONST_PERSISTENT);
276        REGISTER_LONG_CONSTANT("CACA_KEY_F2", CACA_KEY_F2, CONST_CS | CONST_PERSISTENT);
277        REGISTER_LONG_CONSTANT("CACA_KEY_F3", CACA_KEY_F3, CONST_CS | CONST_PERSISTENT);
278        REGISTER_LONG_CONSTANT("CACA_KEY_F4", CACA_KEY_F4, CONST_CS | CONST_PERSISTENT);
279        REGISTER_LONG_CONSTANT("CACA_KEY_F5", CACA_KEY_F5, CONST_CS | CONST_PERSISTENT);
280        REGISTER_LONG_CONSTANT("CACA_KEY_F6", CACA_KEY_F6, CONST_CS | CONST_PERSISTENT);
281        REGISTER_LONG_CONSTANT("CACA_KEY_F7", CACA_KEY_F7, CONST_CS | CONST_PERSISTENT);
282        REGISTER_LONG_CONSTANT("CACA_KEY_F8", CACA_KEY_F8, CONST_CS | CONST_PERSISTENT);
283        REGISTER_LONG_CONSTANT("CACA_KEY_F9", CACA_KEY_F9, CONST_CS | CONST_PERSISTENT);
284        REGISTER_LONG_CONSTANT("CACA_KEY_F10", CACA_KEY_F10, CONST_CS | CONST_PERSISTENT);
285        REGISTER_LONG_CONSTANT("CACA_KEY_F11", CACA_KEY_F11, CONST_CS | CONST_PERSISTENT);
286        REGISTER_LONG_CONSTANT("CACA_KEY_F12", CACA_KEY_F12, CONST_CS | CONST_PERSISTENT);
287        REGISTER_LONG_CONSTANT("CACA_KEY_F13", CACA_KEY_F13, CONST_CS | CONST_PERSISTENT);
288        REGISTER_LONG_CONSTANT("CACA_KEY_F14", CACA_KEY_F14, CONST_CS | CONST_PERSISTENT);
289        REGISTER_LONG_CONSTANT("CACA_KEY_F15", CACA_KEY_F15, CONST_CS | CONST_PERSISTENT);
290
291        return SUCCESS;
292}
293
294//----------SOME USEFULL MACROS---------------//
295
296#define FETCH_CANVAS(canvas) \
297        zval *_zval; \
298        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
299                RETURN_FALSE; \
300        } \
301        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
302
303#define FETCH_DISPLAY(display) \
304        zval *_zval; \
305        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
306                RETURN_FALSE; \
307        } \
308        ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
309
310#define FETCH_EVENT(event) \
311        zval *_zval; \
312        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) { \
313                RETURN_FALSE; \
314        } \
315        ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event);
316
317#define RETURN_CHAR(c) \
318        char *str = emalloc(2); \
319        str[0] = c; \
320        str[1] = '\0'; \
321        RETURN_STRING(str, 0);
322
323
324//-------CACA'S FUNCTIONS----------------//
325
326PHP_FUNCTION(caca_create_canvas) {
327        long width, height = 0;
328        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &width, &height) == FAILURE) {
329                RETURN_FALSE;
330        }
331        caca_canvas_t *canvas = caca_create_canvas(width, height);
332        if (!canvas) {
333                RETURN_FALSE;
334        }
335        ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
336}
337
338PHP_FUNCTION(caca_manage_canvas) {
339}
340
341PHP_FUNCTION(caca_unmanage_canvas) {
342}
343
344PHP_FUNCTION(caca_set_canvas_size) {
345        zval *_zval;
346        long width, height = 0;
347        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &width, &height) == FAILURE) {
348                RETURN_FALSE;
349        }
350        caca_canvas_t *canvas;
351        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
352        RETURN_BOOL(caca_set_canvas_size(canvas, width, height) == 0);
353}
354
355PHP_FUNCTION(caca_get_canvas_width) {
356        caca_canvas_t *canvas;
357        FETCH_CANVAS(canvas);
358        RETURN_LONG(caca_get_canvas_width(canvas));
359}
360
361PHP_FUNCTION(caca_get_canvas_height) {
362        caca_canvas_t *canvas;
363        FETCH_CANVAS(canvas);
364        RETURN_LONG(caca_get_canvas_height(canvas));
365}
366
367PHP_FUNCTION(caca_get_canvas_chars) {
368        caca_canvas_t *canvas;
369        FETCH_CANVAS(canvas);
370        RETURN_STRING(estrdup(caca_get_canvas_chars(canvas)), 0); //TODO: check that return \0 terminated string
371}
372
373PHP_FUNCTION(caca_get_canvas_attrs) {
374        caca_canvas_t *canvas;
375        FETCH_CANVAS(canvas);
376        RETURN_STRING(estrdup(caca_get_canvas_attrs(canvas)), 0); //TODO: check that return \0 terminated string
377}
378
379PHP_FUNCTION(caca_rand) {
380        long min, max = 0;
381        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &min, &max) == FAILURE) {
382                RETURN_FALSE;
383        }
384        RETURN_LONG(caca_rand(min, max));
385}
386
387PHP_FUNCTION(caca_get_version) {
388        RETURN_STRING(estrdup(caca_get_version()), 0);
389}
390
391PHP_FUNCTION(caca_gotoxy) {
392        zval *_zval;
393        long x, y = 0;
394        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
395                RETURN_FALSE;
396        }
397        caca_canvas_t *canvas;
398        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
399        RETURN_BOOL(caca_gotoxy(canvas, x, y) == 0);
400}
401
402PHP_FUNCTION(caca_get_cursor_x) {
403        caca_canvas_t *canvas;
404        FETCH_CANVAS(canvas);
405        RETURN_LONG(caca_get_cursor_x(canvas));
406}
407
408PHP_FUNCTION(caca_get_cursor_y) {
409        caca_canvas_t *canvas;
410        FETCH_CANVAS(canvas);
411        RETURN_LONG(caca_get_cursor_y(canvas));
412}
413
414PHP_FUNCTION(caca_put_char) {
415        zval *_zval;
416        long x, y, c = 0;
417        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == FAILURE) {
418                RETURN_FALSE;
419        }
420        caca_canvas_t *canvas;
421        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
422        RETURN_BOOL(caca_put_char(canvas, x, y, c) == 0);
423}
424
425PHP_FUNCTION(caca_get_char) {
426        zval *_zval;
427        long x, y = 0;
428        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &x, &y) == FAILURE) {
429                RETURN_FALSE;
430        }
431        caca_canvas_t *canvas;
432        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
433        RETURN_CHAR(caca_get_char(canvas, x, y));
434}
435
436PHP_FUNCTION(caca_put_str) {
437        zval *_zval;
438        char *str;
439        long x, y, str_len = 0;
440        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &str, &str_len) == 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_BOOL(caca_put_str(canvas, x, y, str) == 0);
446}
447
448PHP_FUNCTION(caca_printf) {
449}
450
451PHP_FUNCTION(caca_clear_canvas) {
452        caca_canvas_t *canvas;
453        FETCH_CANVAS(canvas);
454        RETURN_LONG(caca_clear_canvas(canvas));
455}
456
457PHP_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_BOOL(caca_set_canvas_handle(canvas, x, y) == 0);
466}
467
468PHP_FUNCTION(caca_get_canvas_handle_x) {
469        caca_canvas_t *canvas;
470        FETCH_CANVAS(canvas);
471        RETURN_LONG(caca_get_canvas_handle_x(canvas));
472}
473
474PHP_FUNCTION(caca_get_canvas_handle_y) {
475        caca_canvas_t *canvas;
476        FETCH_CANVAS(canvas);
477        RETURN_LONG(caca_get_canvas_handle_y(canvas));
478}
479
480PHP_FUNCTION(caca_blit) {
481        zval *_zval1, *_zval2, *_zval3 = NULL;
482        long x, y = 0;
483        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllr|r", &_zval1, &x, &y, &_zval2, &_zval3) == FAILURE) {
484                RETURN_FALSE;
485        }
486        caca_canvas_t *dst, *src, *mask = NULL;
487        ZEND_FETCH_RESOURCE(dst, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
488        ZEND_FETCH_RESOURCE(src, caca_canvas_t*, &_zval2, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
489        if (_zval3) {
490                ZEND_FETCH_RESOURCE(mask, caca_canvas_t*, &_zval3, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
491        }
492        RETURN_BOOL(caca_blit(dst, x, y, src, NULL) == 0);
493}
494
495PHP_FUNCTION(caca_set_canvas_boundaries) {
496        zval *_zval;
497        long x, y, width, height = 0;
498        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &x, &y, &width, &height) == FAILURE) {
499                RETURN_FALSE;
500        }
501        caca_canvas_t *canvas;
502        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
503        RETURN_BOOL(caca_set_canvas_boundaries(canvas, x, y, width, height) == 0);
504}
505
506PHP_FUNCTION(caca_invert) {
507        caca_canvas_t *canvas;
508        FETCH_CANVAS(canvas);
509        RETURN_BOOL(caca_invert(canvas) == 0);
510}
511
512PHP_FUNCTION(caca_flip) {
513        caca_canvas_t *canvas;
514        FETCH_CANVAS(canvas);
515        RETURN_BOOL(caca_flip(canvas) == 0);
516}
517
518PHP_FUNCTION(caca_flop) {
519        caca_canvas_t *canvas;
520        FETCH_CANVAS(canvas);
521        RETURN_BOOL(caca_flop(canvas) == 0);
522}
523
524PHP_FUNCTION(caca_rotate_180) {
525        caca_canvas_t *canvas;
526        FETCH_CANVAS(canvas);
527        RETURN_BOOL(caca_rotate_180(canvas) == 0);
528}
529
530PHP_FUNCTION(caca_rotate_left) {
531        caca_canvas_t *canvas;
532        FETCH_CANVAS(canvas);
533        RETURN_BOOL(caca_rotate_left(canvas) == 0);
534}
535
536PHP_FUNCTION(caca_rotate_right) {
537        caca_canvas_t *canvas;
538        FETCH_CANVAS(canvas);
539        RETURN_BOOL(caca_rotate_right(canvas) == 0);
540}
541
542PHP_FUNCTION(caca_stretch_left) {
543        caca_canvas_t *canvas;
544        FETCH_CANVAS(canvas);
545        RETURN_BOOL(caca_stretch_left(canvas) == 0);
546}
547
548PHP_FUNCTION(caca_stretch_right) {
549        caca_canvas_t *canvas;
550        FETCH_CANVAS(canvas);
551        RETURN_BOOL(caca_stretch_right(canvas) == 0);
552}
553
554PHP_FUNCTION(caca_get_attr) {
555}
556
557PHP_FUNCTION(caca_set_attr) {
558}
559
560PHP_FUNCTION(caca_put_attr) {
561}
562
563PHP_FUNCTION(caca_set_color_ansi) {
564        zval *_zval;
565        long foreground, background = 0;
566        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &_zval, &foreground, &background) == FAILURE) {
567                RETURN_FALSE;
568        }
569        caca_canvas_t *canvas;
570        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
571        RETURN_BOOL(caca_set_color_ansi(canvas, foreground, background) == 0);
572}
573
574PHP_FUNCTION(caca_set_color_argb) {
575}
576
577PHP_FUNCTION(caca_attr_to_ansi) {
578}
579
580PHP_FUNCTION(caca_attr_to_ansi_fg) {
581}
582
583PHP_FUNCTION(caca_attr_to_ansi_bg) {
584}
585
586PHP_FUNCTION(caca_draw_line) {
587}
588
589PHP_FUNCTION(caca_draw_polyline) {
590}
591
592PHP_FUNCTION(caca_draw_thin_line) {
593}
594
595PHP_FUNCTION(caca_draw_thin_polyline) {
596}
597
598PHP_FUNCTION(caca_draw_circle) {
599}
600
601PHP_FUNCTION(caca_draw_ellipse) {
602}
603
604PHP_FUNCTION(caca_draw_thin_ellipse) {
605}
606
607PHP_FUNCTION(caca_fill_ellipse) {
608}
609
610PHP_FUNCTION(caca_draw_box) {
611}
612
613PHP_FUNCTION(caca_draw_thin_box) {
614}
615
616PHP_FUNCTION(caca_fill_box) {
617}
618
619PHP_FUNCTION(caca_draw_triangle) {
620}
621
622PHP_FUNCTION(caca_draw_thin_triangle) {
623}
624
625PHP_FUNCTION(caca_fill_triangle) {
626}
627
628PHP_FUNCTION(caca_get_frame_count) {
629}
630
631PHP_FUNCTION(caca_set_frame) {
632}
633
634PHP_FUNCTION(caca_set_frame_name) {
635}
636
637PHP_FUNCTION(caca_create_frame) {
638}
639
640PHP_FUNCTION(caca_free_frame) {
641}
642
643PHP_FUNCTION(caca_set_dither_palette) {
644}
645
646PHP_FUNCTION(caca_set_dither_brightness) {
647}
648
649PHP_FUNCTION(caca_get_dither_brightness) {
650}
651
652PHP_FUNCTION(caca_set_dither_gamma) {
653}
654
655PHP_FUNCTION(caca_get_dither_gamma) {
656}
657
658PHP_FUNCTION(caca_set_dither_contrast) {
659}
660
661PHP_FUNCTION(caca_get_dither_contrast) {
662}
663
664PHP_FUNCTION(caca_set_dither_antialias) {
665}
666
667PHP_FUNCTION(caca_get_dither_antialias_list) {
668}
669
670PHP_FUNCTION(caca_get_dither_antialias) {
671}
672
673PHP_FUNCTION(caca_set_dither_color) {
674}
675
676PHP_FUNCTION(caca_get_dither_color_list) {
677}
678
679PHP_FUNCTION(caca_get_dither_color) {
680}
681
682PHP_FUNCTION(caca_set_dither_charset) {
683}
684
685PHP_FUNCTION(caca_get_dither_charset_list) {
686}
687
688PHP_FUNCTION(caca_get_dither_charset) {
689}
690
691PHP_FUNCTION(caca_set_dither_algorithm) {
692}
693
694PHP_FUNCTION(caca_get_dither_algorithm_list) {
695}
696
697PHP_FUNCTION(caca_get_dither_algorithm) {
698}
699
700PHP_FUNCTION(caca_dither_bitmap) {
701}
702
703PHP_FUNCTION(caca_get_font_list) {
704}
705
706PHP_FUNCTION(caca_get_font_width) {
707}
708
709PHP_FUNCTION(caca_get_font_height) {
710}
711
712PHP_FUNCTION(caca_render_canvas) {
713}
714
715PHP_FUNCTION(caca_canvas_set_figfont) {
716}
717
718PHP_FUNCTION(caca_put_figchar) {
719}
720
721PHP_FUNCTION(caca_flush_figlet) {
722}
723
724PHP_FUNCTION(caca_file_close) {
725}
726
727PHP_FUNCTION(caca_file_tell) {
728}
729
730PHP_FUNCTION(caca_file_read) {
731}
732
733PHP_FUNCTION(caca_file_write) {
734}
735
736PHP_FUNCTION(caca_file_gets) {
737}
738
739PHP_FUNCTION(caca_file_eof) {
740}
741
742PHP_FUNCTION(caca_import_memory) {
743        zval *_zval;
744        char *src, *type;
745        long src_len, type_len = 0;
746        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &type, &type_len) == FAILURE) {
747                RETURN_FALSE;
748        }
749        caca_canvas_t *canvas;
750        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
751
752        RETURN_LONG(caca_import_memory(canvas, src, src_len, type));
753}
754
755PHP_FUNCTION(caca_import_file) {
756}
757
758PHP_FUNCTION(caca_get_import_list) {
759}
760
761PHP_FUNCTION(caca_get_export_list) {
762}
763
764PHP_FUNCTION(caca_create_display) {
765        caca_canvas_t *canvas;
766        FETCH_CANVAS(canvas);
767
768        caca_display_t *display = caca_create_display(canvas);
769        ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
770}
771
772PHP_FUNCTION(caca_create_display_with_driver) {
773        zval *_zval;
774        char *str;
775        long str_len = 0;
776        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
777                RETURN_FALSE;
778        }
779        caca_canvas_t *canvas;
780        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
781
782        caca_display_t *display = caca_create_display_with_driver(canvas, str);
783        if (!display) {
784                RETURN_FALSE;
785        }
786        ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
787}
788
789PHP_FUNCTION(caca_get_display_driver_list) {
790}
791
792PHP_FUNCTION(caca_get_display_driver) {
793}
794
795PHP_FUNCTION(caca_set_display_driver) {
796}
797
798PHP_FUNCTION(caca_get_canvas) {
799}
800
801PHP_FUNCTION(caca_refresh_display) {
802        caca_display_t *display;
803        FETCH_DISPLAY(display);
804        RETURN_BOOL(caca_refresh_display(display) == 0);
805}
806
807PHP_FUNCTION(caca_set_display_time) {
808}
809
810PHP_FUNCTION(caca_get_display_time) {
811        caca_display_t *display;
812        FETCH_DISPLAY(display);
813        RETURN_LONG(caca_get_display_time(display)); //TODO: check return value
814}
815
816PHP_FUNCTION(caca_get_display_width) {
817        caca_display_t *display;
818        FETCH_DISPLAY(display);
819        RETURN_LONG(caca_get_display_width(display));
820}
821
822PHP_FUNCTION(caca_get_display_height) {
823        caca_display_t *display;
824        FETCH_DISPLAY(display);
825        RETURN_LONG(caca_get_display_height(display));
826}
827
828PHP_FUNCTION(caca_set_display_title) {
829        zval *_zval;
830        char *str;
831        long str_len = 0;
832        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &str, &str_len) == FAILURE) {
833                RETURN_FALSE;
834        }
835        caca_display_t *display;
836        ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
837        RETURN_BOOL(caca_set_display(display, str) == 0);
838}
839
840PHP_FUNCTION(caca_set_mouse) {
841        zval *_zval;
842        long value = 0;
843        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
844                RETURN_FALSE;
845        }
846        caca_display_t *display;
847        ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
848        RETURN_BOOL(caca_set_display(display, value) == 0);
849}
850
851PHP_FUNCTION(caca_set_cursor) {
852        zval *_zval;
853        long value = 0;
854        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &_zval, &value) == FAILURE) {
855                RETURN_FALSE;
856        }
857        caca_display_t *display;
858        ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
859        RETURN_BOOL(caca_set_cursor(display, value) == 0);
860}
861
862PHP_FUNCTION(caca_get_event) {
863}
864
865PHP_FUNCTION(caca_get_mouse_x) {
866        caca_display_t *display;
867        FETCH_DISPLAY(display);
868        RETURN_LONG(caca_get_mouse_x(display));
869}
870
871PHP_FUNCTION(caca_get_mouse_y) {
872        caca_display_t *display;
873        FETCH_DISPLAY(display);
874        RETURN_LONG(caca_get_mouse_y(display));
875}
876
877PHP_FUNCTION(caca_get_event_type) {
878}
879
880PHP_FUNCTION(caca_get_event_key_ch) {
881}
882
883PHP_FUNCTION(caca_get_event_mouse_button) {
884        caca_event_t *event;
885        FETCH_EVENT(event);
886        RETURN_LONG(caca_get_event_mouse_button(event));
887}
888
889PHP_FUNCTION(caca_get_event_mouse_x) {
890        caca_event_t *event;
891        FETCH_EVENT(event);
892        RETURN_LONG(caca_get_event_mouse_x(event));
893}
894
895PHP_FUNCTION(caca_get_event_mouse_y) {
896        caca_event_t *event;
897        FETCH_EVENT(event);
898        RETURN_LONG(caca_get_event_mouse_y(event));
899}
900
901PHP_FUNCTION(caca_get_event_resize_width) {
902        caca_event_t *event;
903        FETCH_EVENT(event);
904        RETURN_LONG(caca_get_event_resize_width(event));
905}
906
907PHP_FUNCTION(caca_get_event_resize_height) {
908        caca_event_t *event;
909        FETCH_EVENT(event);
910        RETURN_LONG(caca_get_event_resize_height(event));
911}
912
Note: See TracBrowser for help on using the repository browser.