📄 ncurses_functions.c
字号:
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Hartmut Holzgraefe <hartmut@six.de> | | Georg Richter <georg.richter@php-ev.de> | +----------------------------------------------------------------------+*/#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "php.h"#include "php_ini.h"#include "php_ncurses.h"#define FETCH_WINRES(r, z) ZEND_FETCH_RESOURCE(r, WINDOW **, z, -1, "ncurses_window", le_ncurses_windows)#if HAVE_NCURSES_PANEL# define FETCH_PANEL(r, z) ZEND_FETCH_RESOURCE(r, PANEL **, z, -1, "ncurses_panel", le_ncurses_panels)#endif#define IS_NCURSES_INITIALIZED() \ if (!NCURSES_G(registered_constants)) { \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must initialize ncruses via ncurses_init(), before calling any ncurses functions."); \ RETURN_FALSE; \ }/* {{{ proto int ncurses_addch(int ch) Adds character at current position and advance cursor */PHP_FUNCTION(ncurses_addch){ long ch; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",&ch)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); RETURN_LONG(addch(ch));}/* }}} *//* {{{ proto int ncurses_waddch(resource window, int ch) Adds character at current position in a window and advance cursor */PHP_FUNCTION(ncurses_waddch){ long ch; zval *handle; WINDOW **win; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &handle, &ch)==FAILURE) { return; } FETCH_WINRES(win, &handle); RETURN_LONG(waddch(*win, ch));}/* }}} *//* {{{ proto int ncurses_color_set(int pair) Sets fore- and background color */PHP_FUNCTION(ncurses_color_set){#ifdef HAVE_NCURSES_COLOR_SET long pair; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",&pair)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); RETURN_LONG(color_set(pair,NULL));#else php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported in this build"); RETURN_FALSE;#endif}/* }}} *//* {{{ proto int ncurses_delwin(resource window) Deletes a ncurses window */PHP_FUNCTION(ncurses_delwin){ zval **handle; WINDOW **w; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &handle) == FAILURE){ WRONG_PARAM_COUNT; } FETCH_WINRES(w,handle); zend_list_delete(Z_LVAL_PP(handle));}/* }}} *//* {{{ proto int ncurses_end(void) Stops using ncurses, clean up the screen */PHP_FUNCTION(ncurses_end){ IS_NCURSES_INITIALIZED(); RETURN_LONG(endwin()); /* endialize the curses library */}/* }}} *//* {{{ proto int ncurses_getch(void) Reads a character from keyboard */PHP_FUNCTION(ncurses_getch){ IS_NCURSES_INITIALIZED(); RETURN_LONG(getch());}/* }}} *//* {{{ proto bool ncurses_has_colors(void) Checks if terminal has colors */PHP_FUNCTION(ncurses_has_colors){ IS_NCURSES_INITIALIZED(); RETURN_BOOL(has_colors());}/* }}} *//* {{{ proto int ncurses_init(void) Initializes ncurses */PHP_FUNCTION(ncurses_init){ initscr(); /* initialize the curses library */ keypad(stdscr, TRUE); /* enable keyboard mapping */ (void) nonl(); /* tell curses not to do NL->CR/NL on output */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ if (!NCURSES_G(registered_constants)) { zend_constant c; WINDOW **pscr = (WINDOW**)emalloc(sizeof(WINDOW *)); zval *zscr; *pscr = stdscr; MAKE_STD_ZVAL(zscr); ZEND_REGISTER_RESOURCE(zscr, pscr, le_ncurses_windows); c.value = *zscr; zval_copy_ctor(&c.value); c.flags = CONST_CS; c.name = zend_strndup("STDSCR", 7); c.name_len = 7; zend_register_constant(&c TSRMLS_CC); /* we need this "interesting" arrangement because the * underlying values of the ACS_XXX defines are not * initialized until after ncurses has been initialized */ #define PHP_NCURSES_DEF_CONST(x) \ ZVAL_LONG(zscr, x); \ c.value = *zscr; \ zval_copy_ctor(&c.value); \ c.flags = CONST_CS; \ c.name = zend_strndup("NCURSES_" #x, sizeof("NCURSES_" #x)); \ c.name_len = sizeof("NCURSES_" #x); \ zend_register_constant(&c TSRMLS_CC) PHP_NCURSES_DEF_CONST(ACS_ULCORNER); PHP_NCURSES_DEF_CONST(ACS_LLCORNER); PHP_NCURSES_DEF_CONST(ACS_URCORNER); PHP_NCURSES_DEF_CONST(ACS_LRCORNER); PHP_NCURSES_DEF_CONST(ACS_LTEE); PHP_NCURSES_DEF_CONST(ACS_RTEE); PHP_NCURSES_DEF_CONST(ACS_BTEE); PHP_NCURSES_DEF_CONST(ACS_TTEE); PHP_NCURSES_DEF_CONST(ACS_HLINE); PHP_NCURSES_DEF_CONST(ACS_VLINE); PHP_NCURSES_DEF_CONST(ACS_PLUS); PHP_NCURSES_DEF_CONST(ACS_S1); PHP_NCURSES_DEF_CONST(ACS_S9); PHP_NCURSES_DEF_CONST(ACS_DIAMOND); PHP_NCURSES_DEF_CONST(ACS_CKBOARD); PHP_NCURSES_DEF_CONST(ACS_DEGREE); PHP_NCURSES_DEF_CONST(ACS_PLMINUS); PHP_NCURSES_DEF_CONST(ACS_BULLET); PHP_NCURSES_DEF_CONST(ACS_LARROW); PHP_NCURSES_DEF_CONST(ACS_RARROW); PHP_NCURSES_DEF_CONST(ACS_DARROW); PHP_NCURSES_DEF_CONST(ACS_UARROW); PHP_NCURSES_DEF_CONST(ACS_BOARD); PHP_NCURSES_DEF_CONST(ACS_LANTERN); PHP_NCURSES_DEF_CONST(ACS_BLOCK); FREE_ZVAL(zscr); NCURSES_G(registered_constants) = 1; }}/* }}} *//* {{{ proto int ncurses_init_pair(int pair, int fg, int bg) Allocates a color pair */PHP_FUNCTION(ncurses_init_pair){ long pair, fg, bg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",&pair,&fg,&bg)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); RETURN_LONG(init_pair(pair,fg,bg));}/* }}} *//* {{{ proto int ncurses_move(int y, int x) Moves output position */PHP_FUNCTION(ncurses_move){ long x, y; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll",&y,&x)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); RETURN_LONG(move(y,x));}/* }}} *//* {{{ proto resource ncurses_newpad(int rows, int cols) Creates a new pad (window) */PHP_FUNCTION(ncurses_newpad){ long rows,cols; WINDOW **pwin; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll",&rows,&cols)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); pwin = (WINDOW **)emalloc(sizeof(WINDOW *)); *pwin = newpad(rows,cols); if(!*pwin) { efree(pwin); RETURN_FALSE; } ZEND_REGISTER_RESOURCE(return_value, pwin, le_ncurses_windows);}/* }}} *//* {{{ proto int ncurses_prefresh(resource pad, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol) Copys a region from a pad into the virtual screen */PHP_FUNCTION(ncurses_prefresh){ WINDOW **pwin; zval *phandle; long pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllll", &phandle, &pminrow, &pmincol, &sminrow, &smincol, &smaxrow, &smaxcol) == FAILURE) { return; } FETCH_WINRES(pwin, &phandle); RETURN_LONG(prefresh(*pwin, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));}/* }}} *//* {{{ proto int ncurses_pnoutrefresh(resource pad, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol) Copys a region from a pad into the virtual screen */PHP_FUNCTION(ncurses_pnoutrefresh){ WINDOW **pwin; zval *phandle; long pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllll", &phandle, &pminrow, &pmincol, &sminrow, &smincol, &smaxrow, &smaxcol) == FAILURE) { return; } FETCH_WINRES(pwin, &phandle); RETURN_LONG(pnoutrefresh(*pwin, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));}/* }}} *//* {{{ proto int ncurses_newwin(int rows, int cols, int y, int x) Creates a new window */PHP_FUNCTION(ncurses_newwin){ long rows,cols,y,x; WINDOW **pwin; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llll",&rows,&cols,&y,&x)==FAILURE) { return; } IS_NCURSES_INITIALIZED(); pwin = (WINDOW **)emalloc(sizeof(WINDOW *)); *pwin=newwin(rows,cols,y,x); if(!*pwin) { efree(pwin); RETURN_FALSE; } ZEND_REGISTER_RESOURCE(return_value, pwin, le_ncurses_windows);}/* }}} *//* {{{ proto int ncurses_refresh(int ch) Refresh screen */PHP_FUNCTION(ncurses_refresh){ IS_NCURSES_INITIALIZED(); RETURN_LONG(refresh());}/* }}} *//* {{{ proto int ncurses_start_color(void) Starts using colors */PHP_FUNCTION(ncurses_start_color){ IS_NCURSES_INITIALIZED(); RETURN_LONG(start_color());}/* }}} *//* {{{ proto int ncurses_standout(void) Starts using 'standout' attribute */PHP_FUNCTION(ncurses_standout){ IS_NCURSES_INITIALIZED(); RETURN_LONG(standout());}/* }}} *//* {{{ proto int ncurses_standend(void) Stops using 'standout' attribute */PHP_FUNCTION(ncurses_standend){ IS_NCURSES_INITIALIZED(); RETURN_LONG(standend());}/* }}} *//* {{{ proto int ncurses_baudrate(void) Returns baudrate of terminal */PHP_FUNCTION(ncurses_baudrate){ IS_NCURSES_INITIALIZED(); RETURN_LONG(baudrate());}/* }}} *//* {{{ proto int ncurses_beep(void) Let the terminal beep */PHP_FUNCTION(ncurses_beep){ IS_NCURSES_INITIALIZED(); RETURN_LONG(beep());}/* }}} *//* {{{ proto bool ncurses_can_change_color(void) Checks if we can change terminals colors */PHP_FUNCTION(ncurses_can_change_color){ IS_NCURSES_INITIALIZED(); RETURN_LONG(can_change_color());}/* }}} *//* {{{ proto bool ncurses_cbreak(void) Switches of input buffering */PHP_FUNCTION(ncurses_cbreak){ IS_NCURSES_INITIALIZED(); RETURN_LONG(cbreak());}/* }}} *//* {{{ proto bool ncurses_clear(void) Clears screen */PHP_FUNCTION(ncurses_clear){ IS_NCURSES_INITIALIZED(); RETURN_LONG(clear());}/* }}} *//* {{{ proto bool ncurses_clrtobot(void) Clears screen from current position to bottom */PHP_FUNCTION(ncurses_clrtobot){ IS_NCURSES_INITIALIZED(); RETURN_LONG(clrtobot());}/* }}} *//* {{{ proto bool ncurses_clrtoeol(void) Clears screen from current position to end of line */PHP_FUNCTION(ncurses_clrtoeol){ IS_NCURSES_INITIALIZED(); RETURN_LONG(clrtoeol());}/* }}} *//* {{{ proto int ncurses_reset_prog_mode(void) Resets the prog mode saved by def_prog_mode */PHP_FUNCTION(ncurses_reset_prog_mode){ if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } IS_NCURSES_INITIALIZED(); RETURN_LONG(reset_prog_mode());}/* }}} *//* {{{ proto int ncurses_reset_shell_mode(void) Resets the shell mode saved by def_shell_mode */PHP_FUNCTION(ncurses_reset_shell_mode){ if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } IS_NCURSES_INITIALIZED(); RETURN_LONG(reset_shell_mode());}/* }}} *//* {{{ proto bool ncurses_def_prog_mode(void) Saves terminals (program) mode */PHP_FUNCTION(ncurses_def_prog_mode){ IS_NCURSES_INITIALIZED(); RETURN_LONG(def_prog_mode());}/* }}} *//* {{{ proto bool ncurses_def_shell_mode(void) Saves terminal (shell) mode*/PHP_FUNCTION(ncurses_def_shell_mode){ IS_NCURSES_INITIALIZED(); RETURN_LONG(def_shell_mode());}/* }}} *//* {{{ proto bool ncurses_delch(void) Deletes character at current position, move rest of line left */PHP_FUNCTION(ncurses_delch){ IS_NCURSES_INITIALIZED(); RETURN_LONG(delch());}/* }}} *//* {{{ proto bool ncurses_deleteln(void) Deletes line at current position, move rest of screen up */PHP_FUNCTION(ncurses_deleteln){ IS_NCURSES_INITIALIZED(); RETURN_LONG(deleteln());}/* }}} *//* {{{ proto bool ncurses_doupdate(void) Writes all prepared refreshes to terminal */PHP_FUNCTION(ncurses_doupdate){ IS_NCURSES_INITIALIZED(); RETURN_LONG(doupdate());}/* }}} *//* {{{ proto bool ncurses_echo(void) Activates keyboard input echo */PHP_FUNCTION(ncurses_echo){ IS_NCURSES_INITIALIZED(); RETURN_LONG(echo());}/* }}} *//* {{{ proto bool ncurses_erase(void) Erases terminal screen */PHP_FUNCTION(ncurses_erase){ IS_NCURSES_INITIALIZED(); RETURN_LONG(erase());}/* }}} *//* {{{ proto string ncurses_erasechar(void) Returns current erase character */PHP_FUNCTION(ncurses_erasechar){ char temp[2]; IS_NCURSES_INITIALIZED(); temp[0] = erasechar(); temp[1] = '\0'; RETURN_STRINGL (temp, 1, 1);}/* }}} *//* {{{ proto bool ncurses_flash(void) Flashes terminal screen (visual bell) */PHP_FUNCTION(ncurses_flash){ IS_NCURSES_INITIALIZED(); RETURN_LONG(flash());}/* }}} *//* {{{ proto bool ncurses_flushinp(void) Flushes keyboard input buffer */PHP_FUNCTION(ncurses_flushinp){ IS_NCURSES_INITIALIZED(); RETURN_LONG(flushinp());}/* }}} *//* {{{ proto bool ncurses_has_ic(void) Checks for insert- and delete-capabilities */PHP_FUNCTION(ncurses_has_ic){ IS_NCURSES_INITIALIZED(); RETURN_LONG(has_ic());}/* }}} *//* {{{ proto bool ncurses_has_il(void) Checks for line insert- and delete-capabilities */PHP_FUNCTION(ncurses_has_il){ IS_NCURSES_INITIALIZED(); RETURN_LONG(has_il());}/* }}} *//* {{{ proto string ncurses_inch(void) Gets character and attribute at current position */PHP_FUNCTION(ncurses_inch){ char temp[2]; IS_NCURSES_INITIALIZED(); temp[0] = inch(); temp[1] = '\0'; RETURN_STRINGL (temp, 1, 1);}/* }}} *//* {{{ proto bool ncurses_insertln(void) Inserts a line, move rest of screen down */PHP_FUNCTION(ncurses_insertln){ IS_NCURSES_INITIALIZED(); RETURN_LONG(insertln());}/* }}} *//* {{{ proto bool ncurses_isendwin(void) Ncurses is in endwin mode, normal screen output may be performed */PHP_FUNCTION(ncurses_isendwin){ IS_NCURSES_INITIALIZED(); RETURN_LONG(isendwin());}/* }}} *//* {{{ proto string ncurses_killchar(void) Returns current line kill character */PHP_FUNCTION(ncurses_killchar){ char temp[2]; IS_NCURSES_INITIALIZED(); temp[0] = killchar(); temp[1] = '\0'; RETURN_STRINGL (temp, 1, 1);}/* }}} *//* {{{ proto bool ncurses_nl(void) Translates newline and carriage return / line feed */PHP_FUNCTION(ncurses_nl){ IS_NCURSES_INITIALIZED(); RETURN_LONG(nl());}/* }}} *//* {{{ proto bool ncurses_nocbreak(void) Switches terminal to cooked mode */PHP_FUNCTION(ncurses_nocbreak){ IS_NCURSES_INITIALIZED(); RETURN_LONG(nocbreak());}/* }}} *//* {{{ proto bool ncurses_noecho(void) Switches off keyboard input echo */PHP_FUNCTION(ncurses_noecho){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -