FBCALL int fb_IsRedirected ( int is_input ) { FB_LOCK( ); if( __fb_ctx.hooks.isredirproc != NULL ) return __fb_ctx.hooks.isredirproc( is_input ); else return fb_ConsoleIsRedirected( is_input ); FB_LOCK( ); }
int fb_DevFileReadLineWstr( FB_FILE *handle, FB_WCHAR *dst, int dst_chars ) { int res; FILE *fp; FBSTRING temp = { 0 }; FB_LOCK(); fp = (FILE *)handle->opaque; if( fp == stdout || fp == stderr ) fp = stdin; if( fp == NULL ) { FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } res = fb_DevFileReadLineDumb( fp, &temp, NULL ); /* convert to wchar, file should be opened with the ENCODING option to allow UTF characters to be read */ if( (res == FB_RTERROR_OK) || (res == FB_RTERROR_ENDOFFILE) ) fb_WstrAssignFromA( dst, dst_chars, (void *)&temp, -1 ); fb_StrDelete( &temp ); FB_UNLOCK(); return res; }
int fb_DevFileReadEncodWstr( FB_FILE *handle, FB_WCHAR *dst, size_t *max_chars ) { FILE *fp; size_t chars; FB_LOCK(); chars = *max_chars; fp = (FILE *)handle->opaque; if( fp == stdout || fp == stderr ) fp = stdin; if( fp == NULL ) { FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } /* do read */ chars = fb_hFileRead_UTFToWchar( fp, handle->encod, dst, chars ); /* fill with nulls if at eof */ if( chars != *max_chars ) memset( (char *)&dst[chars], 0, (*max_chars - chars) * sizeof( FB_WCHAR ) ); *max_chars = chars; FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_OK ); }
int fb_DevConsOpen( FB_FILE *handle, const char *filename, size_t filename_len ) { switch ( handle->mode ) { case FB_FILE_MODE_INPUT: case FB_FILE_MODE_OUTPUT: case FB_FILE_MODE_APPEND: break; default: return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } FB_LOCK(); handle->hooks = &hooks_dev_cons; if ( handle->access == FB_FILE_ACCESS_ANY) handle->access = FB_FILE_ACCESS_WRITE; handle->opaque = (handle->mode == FB_FILE_MODE_INPUT? stdin : stdout); handle->type = FB_FILE_TYPE_PIPE; FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_OK ); }
int fb_FileSeekEx( FB_FILE *handle, fb_off_t newpos ) { int res; if( !FB_HANDLE_USED(handle) ) return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); FB_LOCK(); /* clear put back buffer for every modifying non-read operation */ handle->putback_size = 0; /* convert to 0 based file i/o */ --newpos; if( handle->mode == FB_FILE_MODE_RANDOM ) newpos = newpos * handle->len; if (handle->hooks->pfnSeek!=NULL) { res = handle->hooks->pfnSeek(handle, newpos, SEEK_SET ); } else { res = fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } FB_UNLOCK(); return res; }
/*:::::*/ int fb_ConsoleLineInputWstr( const FB_WCHAR *text, FB_WCHAR *dst, int max_chars, int addquestion, int addnewline ) { FBSTRING *tmp_result; /* !!!FIXME!!! no support for unicode input */ FB_LOCK(); fb_PrintBufferEx( NULL, 0, FB_PRINT_FORCE_ADJUST ); if( text != NULL ) { fb_PrintWstr( 0, text, 0 ); if( addquestion != FB_FALSE ) fb_PrintFixString( 0, pszDefaultQuestion, 0 ); } FB_UNLOCK(); tmp_result = fb_ConReadLine( FALSE ); if( addnewline ) fb_PrintVoid( 0, FB_PRINT_NEWLINE ); if( tmp_result == NULL ) return fb_ErrorSetNum( FB_RTERROR_OUTOFMEM ); fb_WstrAssignFromA( dst, max_chars, tmp_result, -1 ); return fb_ErrorSetNum( FB_RTERROR_OK ); }
FBSTRING *fb_DrvIntlGetMonthName( int month, int short_names ) { const char *pszName; FBSTRING *result; size_t name_len; nl_item index; if( month < 1 || month > 12 ) return NULL; if( short_names ) { index = (nl_item) (ABMON_1 + month - 1); } else { index = (nl_item) (MON_1 + month - 1); } FB_LOCK(); pszName = nl_langinfo( index ); if( pszName==NULL ) { FB_UNLOCK(); return NULL; } name_len = strlen( pszName ); result = fb_hStrAllocTemp( NULL, name_len ); if( result!=NULL ) { FB_MEMCPY( result->data, pszName, name_len + 1 ); } FB_UNLOCK(); return result; }
int fb_DevFileReadLineEncodWstr( FB_FILE *handle, FB_WCHAR *dst, ssize_t max_chars ) { int res; FB_LOCK(); FILE* fp = (FILE *)handle->opaque; if( fp == stdout || fp == stderr ) fp = stdin; if( fp == NULL ) { FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } /* Clear string first, we're only using += concat assign below... */ dst[0] = _LC('\0'); /* Read one byte at a time until CR and/or LF is found. The fb_FileGetDataEx() will handle the decoding. The length to read is specified in wchars, not bytes, because we're passing TRUE for is_unicode. */ while( TRUE ) { FB_WCHAR c[2]; size_t len; res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, TRUE ); if( (res != FB_RTERROR_OK) || (len == 0) ) break; /* CR? Check for following LF too, and skip it if it's there */ if( c[0] == _LC('\r') ) { res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, TRUE ); if( (res != FB_RTERROR_OK) || (len == 0) ) break; /* No LF? Ok then, don't skip it yet */ if( c[0] != _LC('\n') ) fb_FilePutBackEx( handle, c, 1 ); break; } /* LF? */ if( c[0] == _LC('\n') ) { break; } /* Any other char? Append to string, and continue... */ c[1] = _LC('\0'); fb_WstrConcatAssign( dst, max_chars, c ); } FB_UNLOCK(); return res; }
FBCALL int fb_GfxScreen ( int mode, int depth, int num_pages, int flags, int refresh_rate ) { if( (mode < 0) || (mode >= NUM_MODES) ) return fb_ErrorSetNum(FB_RTERROR_ILLEGALFUNCTIONCALL); const MODEINFO *info = &mode_info[mode]; /* One of the unsupported modes? */ if( (mode > 0) && (info->w == 0) ) return fb_ErrorSetNum(FB_RTERROR_ILLEGALFUNCTIONCALL); switch( depth ) { case 8: case 15: case 16: case 24: case 32: /* user's depth overrides default for mode > 13 */ if( mode <= 13 ) { depth = info->depth; } break; default: depth = info->depth; break; } if( num_pages <= 0 ) { num_pages = info->num_pages; } FB_GRAPHICS_LOCK( ); int res = set_mode( mode, info->w, info->h, depth, info->scanline_size, num_pages, refresh_rate, info->palette, info->font, flags, 0.0, info->text_w, info->text_h ); if( res == FB_RTERROR_OK ) { FB_LOCK( ); FB_HANDLE_SCREEN->line_length = 0; FB_UNLOCK( ); } FB_GRAPHICS_UNLOCK( ); return fb_ErrorSetNum( FB_RTERROR_OK ); }
/*:::::*/ int fb_ConsoleLineInputWstr( const FB_WCHAR *text, FB_WCHAR *dst, int max_chars, int addquestion, int addnewline ) { int res; size_t len; int old_x, old_y; /* !!!FIXME!!! no support for unicode input */ fb_PrintBufferEx( NULL, 0, FB_PRINT_FORCE_ADJUST ); fb_GetXY( &old_x, &old_y ); FB_LOCK(); if( text != NULL ) { fb_PrintWstr( 0, text, 0 ); if( addquestion != FB_FALSE ) fb_PrintFixString( 0, pszDefaultQuestion, 0 ); } { FBSTRING str_result = { 0 }; res = fb_DevFileReadLineDumb( stdin, &str_result, hWrapper ); len = FB_STRSIZE(&str_result); if( !addnewline ) { int cols, rows; int old_y; fb_GetSize( &cols, &rows ); fb_GetXY( NULL, &old_y ); old_x += len - 1; old_x %= cols; old_x += 1; old_y -= 1; fb_Locate( old_y, old_x, -1, 0, 0 ); } fb_WstrAssignFromA( dst, max_chars, (void *)&str_result, -1 ); fb_StrDelete( &str_result ); } FB_UNLOCK(); return res; }
/*:::::*/ FBCALL void fb_GetXY( int *col, int *row ) { FB_LOCK(); if( __fb_ctx.hooks.getxyproc ) __fb_ctx.hooks.getxyproc( col, row ); else fb_ConsoleGetXY( col, row ); FB_UNLOCK(); }
/*:::::*/ FBCALL void fb_PrintBufferEx( const void *buffer, size_t len, int mask ) { FB_LOCK(); if( __fb_ctx.hooks.printbuffproc ) __fb_ctx.hooks.printbuffproc( buffer, len, mask ); else fb_ConsolePrintBufferEx( buffer, len, mask ); FB_UNLOCK(); }
/*:::::*/ int fb_hConsoleInputBufferChanged(void) { int result; fb_ConsoleProcessEvents( ); FB_LOCK(); result = key_buffer_changed; key_buffer_changed = FALSE; FB_UNLOCK(); return result; }
static int fb_DevComRead( FB_FILE *handle, void* value, size_t *pValuelen ) { int res; DEV_COM_INFO *pInfo; FB_LOCK(); pInfo = (DEV_COM_INFO*) handle->opaque; res = fb_SerialRead( handle, pInfo->hSerial, value, pValuelen ); FB_UNLOCK(); return res; }
/*:::::*/ FBCALL int fb_LineInputWstr( const FB_WCHAR *text, FB_WCHAR *dst, int max_chars, int addquestion, int addnewline ) { FB_LINEINPUTWPROC fn; FB_LOCK(); fn = __fb_ctx.hooks.lineinputwproc; FB_UNLOCK(); if( fn ) return fn( text, dst, max_chars, addquestion, addnewline ); else return fb_ConsoleLineInputWstr( text, dst, max_chars, addquestion, addnewline ); }
/*:::::*/ static void fb_hPrintStrEx( FB_FILE *handle, const char *s, size_t len, int mask ) { /* add a lock here or the new-line won't be printed in the right place if PRINT is been used in multiple threads and a context switch happens between FB_PRINT_EX() and PrintVoidEx() */ FB_LOCK( ); if( len != 0 ) FB_PRINT_EX(handle, s, len, 0); fb_PrintVoidEx( handle, mask ); FB_UNLOCK( ); }
static int fb_DevComWrite( FB_FILE *handle, const void* value, size_t valuelen ) { int res; DEV_COM_INFO *pInfo; FB_LOCK(); pInfo = (DEV_COM_INFO*) handle->opaque; res = fb_SerialWrite( handle, pInfo->hSerial, value, valuelen ); FB_UNLOCK(); return res; }
FBCALL int fb_IsRedirected ( int is_input ) { int result; FB_LOCK( ); if( __fb_ctx.hooks.isredirproc != NULL ) result = __fb_ctx.hooks.isredirproc( is_input ); else result = fb_ConsoleIsRedirected( is_input ); FB_UNLOCK( ); return result; }
FBCALL int fb_GetMouse( int *x, int *y, int *z, int *buttons, int *clip ) { int res; FB_LOCK(); if( __fb_ctx.hooks.getmouseproc ) res = __fb_ctx.hooks.getmouseproc( x, y, z, buttons, clip ); else res = fb_ConsoleGetMouse( x, y, z, buttons, clip ); FB_UNLOCK(); return res; }
/*:::::*/ FBCALL int fb_GetX( void ) { int res; FB_LOCK(); if( __fb_ctx.hooks.getxproc ) res = __fb_ctx.hooks.getxproc( ); else res = fb_ConsoleGetX( ); FB_UNLOCK(); return res; }
FBCALL int fb_GfxScreenRes ( int w, int h, int depth, int num_pages, int flags, int refresh_rate ) { if ((w <= 0) || (h <= 0)) return fb_ErrorSetNum(FB_RTERROR_ILLEGALFUNCTIONCALL); switch (depth) { case 1: case 2: case 4: case 8: case 15: case 16: case 24: case 32: break; default: return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } if( num_pages <= 0 ) { num_pages = 1; } FB_GRAPHICS_LOCK( ); int res = set_mode( -1, w, h, depth, 1, num_pages, refresh_rate, FB_PALETTE_256, FB_FONT_8, flags, 1.0, w / __fb_font[FB_FONT_8].w, h / __fb_font[FB_FONT_8].h ); if( res == FB_RTERROR_OK ) { FB_LOCK( ); FB_HANDLE_SCREEN->line_length = 0; FB_UNLOCK( ); } FB_GRAPHICS_UNLOCK( ); return res; }
void fb_DevScrnInit_NoOpen( void ) { FB_LOCK(); if ( FB_HANDLE_SCREEN->hooks == NULL ) { memset(FB_HANDLE_SCREEN, 0, sizeof(*FB_HANDLE_SCREEN)); FB_HANDLE_SCREEN->mode = FB_FILE_MODE_APPEND; FB_HANDLE_SCREEN->type = FB_FILE_TYPE_VFS; FB_HANDLE_SCREEN->access = FB_FILE_ACCESS_READWRITE; fb_DevScrnInit_Screen( ); FB_HANDLE_SCREEN->hooks = &hooks_dev_scrn_null; } FB_UNLOCK(); }
static int fb_DevComTell( FB_FILE *handle, fb_off_t *pOffset ) { int res; DEV_COM_INFO *pInfo; DBG_ASSERT( pOffset!=NULL ); FB_LOCK(); pInfo = (DEV_COM_INFO*) handle->opaque; res = fb_SerialGetRemaining( handle, pInfo->hSerial, pOffset ); FB_UNLOCK(); return res; }
FBCALL void fb_FileResetEx( int streamno ) { FILE *result; /* streamno 0 Reset stdin 1 Reset stdout */ if( streamno != 0 && streamno != 1 ) { fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); return; } FB_LOCK(); #if defined( HOST_DOS ) if( streamno == 0 ) { result = freopen( "CON", "r", stdin ); } else { result = freopen( "CON", "w", stdout ); } #elif defined( HOST_WIN32 ) /* in io_gethnd.c */ fb_hConsoleResetHandles(); if( streamno == 0 ) { result = freopen( "CONIN$", "r", stdin ); } else { result = freopen( "CONOUT$", "w", stdout ); } /* force handles to be reinitialized now */ fb_hConsoleGetHandle( TRUE ); #else if( streamno == 0 ) { result = freopen( "/dev/tty", "r", stdin ); } else { result = freopen( "/dev/tty", "w", stdout ); } #endif FB_UNLOCK(); fb_ErrorSetNum( result ? FB_RTERROR_OK : FB_RTERROR_FILEIO ); }
int fb_DevScrnRead( FB_FILE *handle, void* value, size_t *pLength ) { size_t length; DEV_SCRN_INFO *info; int copy_length; char *pachBuffer = (char*) value; FB_LOCK(); DBG_ASSERT(pLength!=NULL); length = *pLength; info = (DEV_SCRN_INFO*) FB_HANDLE_DEREF(handle)->opaque; while( length > 0 ) { copy_length = (length > info->length) ? info->length : length; if (copy_length==0) { while( fb_KeyHit( ) == 0 ) fb_Delay( 25 ); /* release time slice */ fb_DevScrnFillInput( info ); if( info->length != 0 ) continue; break; } memcpy(pachBuffer, info->buffer, copy_length); info->length -= copy_length; if (info->length!=0) { memmove(info->buffer, info->buffer + copy_length, info->length); } length -= copy_length; pachBuffer += copy_length; } FB_UNLOCK(); if (length!=0) memset(pachBuffer, 0, length); *pLength -= length; return fb_ErrorSetNum( FB_RTERROR_OK ); }
FBCALL void fb_PrintSPC( int fnum, ssize_t n ) { FB_FILE *handle; int col, row, cols, rows, newcol; if( n==0 ) return; fb_DevScrnInit_NoOpen( ); FB_LOCK(); handle = FB_FILE_TO_HANDLE(fnum); if( FB_HANDLE_IS_SCREEN(handle) || handle->type == FB_FILE_TYPE_CONSOLE ) { if( n == 0 ) return; if( handle->type == FB_FILE_TYPE_CONSOLE ) { if( handle->hooks && handle->hooks->pfnFlush ) handle->hooks->pfnFlush( handle ); } /* Ensure that we get the "real" cursor position - this quirk is * required for cursor positions at the right bottom of the screen */ fb_PrintBufferEx( NULL, 0, FB_PRINT_FORCE_ADJUST ); fb_GetXY( &col, &row ); fb_GetSize( &cols, &rows ); newcol = col + n; if( newcol > cols ) { fb_PrintVoidEx ( handle, FB_PRINT_NEWLINE ); newcol %= cols; } fb_Locate( 0, newcol, -1, 0, 0 ); } else { fb_PrintStringEx( handle, fb_StrFill1( n, ' ' ), 0 ); } FB_UNLOCK(); }
/*:::::*/ FBCALL void fb_Cls( int mode ) { fb_DevScrnInit_NoOpen( ); FB_LOCK(); if( __fb_ctx.hooks.clsproc ) __fb_ctx.hooks.clsproc( mode ); else fb_ConsoleClear( mode ); FB_HANDLE_SCREEN->line_length = 0; FB_UNLOCK(); }
/*:::::*/ FBCALL int fb_WidthFile( int fnum, int width ) { int cur = width; FB_FILE *handle; FB_LOCK(); handle = FB_HANDLE_DEREF(FB_FILE_TO_HANDLE(fnum)); if( !FB_HANDLE_USED(handle) ) { /* invalid file handle */ FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } if( handle->hooks==NULL ) { /* not opened yet */ FB_UNLOCK(); return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL ); } if( handle==FB_HANDLE_SCREEN ) { /* SCREEN device */ if( width!=-1 ) { fb_Width( width, -1 ); } cur = FB_HANDLE_SCREEN->width; } else { if( width!=-1 ) { handle->width = width; if( handle->hooks->pfnSetWidth!=NULL ) handle->hooks->pfnSetWidth( handle, width ); } cur = handle->width; } FB_UNLOCK(); if( width==-1 ) { return cur; } return fb_ErrorSetNum( FB_RTERROR_OK ); }
/*:::::*/ int fb_hConsoleGetKeyEx(int full, int allow_remove) { int key = -1; fb_ConsoleProcessEvents( ); FB_LOCK(); if (key_head != key_tail) { int do_remove = allow_remove; key = key_buffer[key_head]; if( key > 255 ) { if( !full ) { key_buffer[key_head] = (key >> 8); key = (unsigned) (unsigned char) FB_EXT_CHAR; do_remove = FALSE; } }
static int fb_DevComClose( FB_FILE *handle ) { int res; DEV_COM_INFO *pInfo; FB_LOCK(); pInfo = (DEV_COM_INFO*) handle->opaque; res = fb_SerialClose( handle, pInfo->hSerial ); if( res==FB_RTERROR_OK ) { free(pInfo->pszDevice); free(pInfo); } FB_UNLOCK(); return res; }