From 0a20917152d2986b847e20e3b34dc280a48adddf Mon Sep 17 00:00:00 2001 From: Nicolas Vion Date: Sun, 26 Oct 2008 12:29:10 +0000 Subject: [PATCH] * Add php binding for 9 new functions * Improve examples/demo.php --- caca-php/examples/demo.php | 211 ++++++++++++++++++++++++++++++++----- caca-php/php_caca.c | 115 +++++++++++++++++--- caca-php/php_caca.h | 4 +- 3 files changed, 290 insertions(+), 40 deletions(-) diff --git a/caca-php/examples/demo.php b/caca-php/examples/demo.php index 8b0804e..6e8ebac 100644 --- a/caca-php/examples/demo.php +++ b/caca-php/examples/demo.php @@ -1,6 +1,132 @@ #!/usr/bin/php5 = $w || $y - $b < 0 || $y + $b >= $h); + } + + caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16)); + caca_fill_ellipse($cv, $x, $y, $a, $b, '#'); + + caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK); + if ($outline == 2) + caca_draw_thin_ellipse($cv, $x, $y, $a, $b); + else if ($outline == 1) + caca_draw_ellipse($cv, $x, $y, $a, $b, '#'); +} + + main(); diff --git a/caca-php/php_caca.c b/caca-php/php_caca.c index fc42bd2..2bd319a 100644 --- a/caca-php/php_caca.c +++ b/caca-php/php_caca.c @@ -18,6 +18,7 @@ #include "php_caca.h" static function_entry caca_functions[] = { + PHP_FE(caca_create_event, NULL) PHP_FE(caca_create_canvas, NULL) PHP_FE(caca_manage_canvas, NULL) PHP_FE(caca_unmanage_canvas, NULL) @@ -34,7 +35,6 @@ static function_entry caca_functions[] = { PHP_FE(caca_put_char, NULL) PHP_FE(caca_get_char, NULL) PHP_FE(caca_put_str, NULL) - PHP_FE(caca_printf, NULL) PHP_FE(caca_clear_canvas, NULL) PHP_FE(caca_set_canvas_handle, NULL) PHP_FE(caca_get_canvas_handle_x, NULL) @@ -66,6 +66,7 @@ static function_entry caca_functions[] = { PHP_FE(caca_fill_ellipse, NULL) PHP_FE(caca_draw_box, NULL) PHP_FE(caca_draw_thin_box, NULL) + PHP_FE(caca_draw_cp437_box, NULL) PHP_FE(caca_fill_box, NULL) PHP_FE(caca_draw_triangle, NULL) PHP_FE(caca_draw_thin_triangle, NULL) @@ -167,7 +168,7 @@ PHP_MINFO_FUNCTION(caca) { php_info_print_table_end(); } -//--------CACA'S RESSOURCES DESTRUCTORS-----------// +//-------- Caca's ressources destructors -----------// static void php_caca_canvas_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { caca_free_canvas(rsrc->ptr); @@ -189,9 +190,10 @@ static void php_caca_display_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { } static void php_caca_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { + efree((caca_event_t *) rsrc->ptr); } -//--------INITIALIZATION---------// +//-------- Initialization ---------// PHP_MINIT_FUNCTION(caca) { @@ -291,7 +293,7 @@ PHP_MINIT_FUNCTION(caca) { return SUCCESS; } -//----------SOME USEFULL MACROS---------------// +//---------- Some usefull macros ---------------// #define FETCH_CANVAS(canvas) \ zval *_zval; \ @@ -320,8 +322,15 @@ PHP_MINIT_FUNCTION(caca) { str[1] = '\0'; \ RETURN_STRING(str, 0); +//------- PHP Binding's specific functions ----------// -//-------CACA'S FUNCTIONS----------------// +PHP_FUNCTION(caca_create_event) { + caca_event_t *event; + event = emalloc(sizeof(caca_event_t)); + ZEND_REGISTER_RESOURCE(return_value, event, le_caca_event); +} + +//------- Caca's functions ----------------// PHP_FUNCTION(caca_create_canvas) { long width, height = 0; @@ -413,13 +422,17 @@ PHP_FUNCTION(caca_get_cursor_y) { PHP_FUNCTION(caca_put_char) { zval *_zval; - long x, y, c = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == FAILURE) { + char *str; + long str_len, x, y = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &str, &str_len) == FAILURE) { RETURN_FALSE; } caca_canvas_t *canvas; ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); - RETURN_BOOL(caca_put_char(canvas, x, y, c) == 0); + if (str_len != 1) { + RETURN_FALSE; + } + RETURN_BOOL(caca_put_char(canvas, x, y, str[0]) == 0); } PHP_FUNCTION(caca_get_char) { @@ -445,9 +458,6 @@ PHP_FUNCTION(caca_put_str) { RETURN_BOOL(caca_put_str(canvas, x, y, str) == 0); } -PHP_FUNCTION(caca_printf) { -} - PHP_FUNCTION(caca_clear_canvas) { caca_canvas_t *canvas; FETCH_CANVAS(canvas); @@ -619,21 +629,96 @@ PHP_FUNCTION(caca_draw_circle) { } PHP_FUNCTION(caca_draw_ellipse) { + zval *_zval; + char *str; + long str_len, xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllls", &_zval, &xa, &ya, &xb, &yb, &str, &str_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + if (str_len != 1) { + RETURN_FALSE; + } + RETURN_BOOL(caca_draw_ellipse(canvas, xa, ya, xb, yb, str[0]) == 0); } PHP_FUNCTION(caca_draw_thin_ellipse) { + zval *_zval; + long xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + RETURN_BOOL(caca_draw_thin_ellipse(canvas, xa, ya, xb, yb) == 0); } PHP_FUNCTION(caca_fill_ellipse) { + zval *_zval; + char *str; + long str_len, xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllls", &_zval, &xa, &ya, &xb, &yb, &str, &str_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + if (str_len != 1) { + RETURN_FALSE; + } + RETURN_BOOL(caca_fill_ellipse(canvas, xa, ya, xb, yb, str[0]) == 0); } PHP_FUNCTION(caca_draw_box) { + zval *_zval; + char *str; + long str_len, xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllls", &_zval, &xa, &ya, &xb, &yb, &str, &str_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + if (str_len != 1) { + RETURN_FALSE; + } + RETURN_BOOL(caca_draw_box(canvas, xa, ya, xb, yb, str[0]) == 0); } PHP_FUNCTION(caca_draw_thin_box) { + zval *_zval; + long xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + RETURN_BOOL(caca_draw_thin_box(canvas, xa, ya, xb, yb) == 0); +} + +PHP_FUNCTION(caca_draw_cp437_box) { + zval *_zval; + long xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + RETURN_BOOL(caca_draw_cp437_box(canvas, xa, ya, xb, yb) == 0); } PHP_FUNCTION(caca_fill_box) { + zval *_zval; + char *str; + long str_len, xa, ya, xb, yb = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllls", &_zval, &xa, &ya, &xb, &yb, &str, &str_len) == FAILURE) { + RETURN_FALSE; + } + caca_canvas_t *canvas; + ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); + if (str_len != 1) { + RETURN_FALSE; + } + RETURN_BOOL(caca_fill_box(canvas, xa, ya, xb, yb, str[0]) == 0); } PHP_FUNCTION(caca_draw_triangle) { @@ -900,7 +985,7 @@ PHP_FUNCTION(caca_get_event) { if (_zval2) { ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval2, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event); } - RETURN_BOOL(caca_get_event(display, g, event, aa) == 0); + RETURN_LONG(caca_get_event(display, g, event, aa)); } PHP_FUNCTION(caca_get_mouse_x) { @@ -916,9 +1001,15 @@ PHP_FUNCTION(caca_get_mouse_y) { } PHP_FUNCTION(caca_get_event_type) { + caca_event_t *event; + FETCH_EVENT(event); + RETURN_LONG(caca_get_event_type(event)); } PHP_FUNCTION(caca_get_event_key_ch) { + caca_event_t *event; + FETCH_EVENT(event); + RETURN_LONG(caca_get_event_key_ch(event)); } PHP_FUNCTION(caca_get_event_mouse_button) { diff --git a/caca-php/php_caca.h b/caca-php/php_caca.h index 94e0603..3685554 100644 --- a/caca-php/php_caca.h +++ b/caca-php/php_caca.h @@ -35,6 +35,8 @@ int le_caca_event; PHP_MINIT_FUNCTION(caca); PHP_MINFO_FUNCTION(caca); +PHP_FUNCTION(caca_create_event); + PHP_FUNCTION(caca_create_canvas); PHP_FUNCTION(caca_manage_canvas); PHP_FUNCTION(caca_unmanage_canvas); @@ -51,7 +53,6 @@ PHP_FUNCTION(caca_get_cursor_y); PHP_FUNCTION(caca_put_char); PHP_FUNCTION(caca_get_char); PHP_FUNCTION(caca_put_str); -PHP_FUNCTION(caca_printf); PHP_FUNCTION(caca_clear_canvas); PHP_FUNCTION(caca_set_canvas_handle); PHP_FUNCTION(caca_get_canvas_handle_x); @@ -83,6 +84,7 @@ PHP_FUNCTION(caca_draw_thin_ellipse); PHP_FUNCTION(caca_fill_ellipse); PHP_FUNCTION(caca_draw_box); PHP_FUNCTION(caca_draw_thin_box); +PHP_FUNCTION(caca_draw_cp437_box); PHP_FUNCTION(caca_fill_box); PHP_FUNCTION(caca_draw_triangle); PHP_FUNCTION(caca_draw_thin_triangle);