8 #include "../fb_private_thread.h"
14 #include <sys/ioctl.h>
22 static const char *
seq[] = {
"cm",
"ho",
"cs",
"cl",
"ce",
"WS",
"bl",
"AF",
"AB",
23 "me",
"md",
"SF",
"ve",
"vi",
"dc",
"ks",
"ke" };
32 extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr,
int kind);
34 static pthread_mutex_t __fb_global_mutex;
35 static pthread_mutex_t __fb_string_mutex;
36 FBCALL void fb_Lock (
void ) { pthread_mutex_lock ( &__fb_global_mutex ); }
37 FBCALL void fb_Unlock (
void ) { pthread_mutex_unlock( &__fb_global_mutex ); }
38 FBCALL void fb_StrLock (
void ) { pthread_mutex_lock ( &__fb_string_mutex ); }
39 FBCALL void fb_StrUnlock(
void ) { pthread_mutex_unlock( &__fb_string_mutex ); }
68 return fgetc(__fb_con.
f_in);
79 int fb_hTermQuery(
int code,
int *val1,
int *val2 )
96 if( c == EOF )
return FALSE;
97 if( c ==
'\e' )
break;
104 if( c ==
'[' )
break;
111 if( code == SEQ_QUERY_WINDOW )
116 filled = scanf( format, val1, val2 );
117 }
while (filled != 2);
129 unsigned char *char_buffer, *attr_buffer;
143 ioctl( STDOUT_FILENO, TIOCGWINSZ, &win );
144 if (win.ws_row == 0xFFFF) {
146 if( fb_hTermQuery( SEQ_QUERY_WINDOW, &r, &c ) ) {
158 char_buffer = calloc(1, win.ws_row * win.ws_col * 2);
159 attr_buffer = char_buffer + (win.ws_row * win.ws_col);
161 h = (__fb_con.
h < win.ws_row) ? __fb_con.
h : win.ws_row;
162 w = (__fb_con.
w < win.ws_col) ? __fb_con.
w : win.ws_col;
163 for (r = 0; r < h; r++) {
164 memcpy(char_buffer + (r * win.ws_col), __fb_con.
char_buffer + (r * __fb_con.
w), w);
165 memcpy(attr_buffer + (r * win.ws_col), __fb_con.
attr_buffer + (r * __fb_con.
w), w);
171 __fb_con.
h = win.ws_row;
172 __fb_con.
w = win.ws_col;
174 if( fb_hTermQuery( SEQ_QUERY_CURSOR, &__fb_con.
cur_y, &__fb_con.
cur_x ) ==
FALSE )
193 const char *extra_seq[] = {
"\e(U",
"\e(B",
"\e[6n",
"\e[18t",
194 "\e[?1000h\e[?1003h",
"\e[?1003l\e[?1000l",
"\e[H\e[J\e[0m" };
203 if( fprintf( stdout,
"\e[%dm", param1 ) < 4 )
207 if( fputs( extra_seq[code -
SEQ_EXTRA], stdout ) == EOF )
212 if (!__fb_con.
seq[code])
214 str = tgoto(__fb_con.
seq[code], param1, param2);
217 tputs(str, 1, putchar);
227 struct termios term_out, term_in;
233 if( !isatty( STDOUT_FILENO ) || !isatty( STDIN_FILENO ) )
235 __fb_con.
f_in = fopen(
"/dev/tty",
"r+b");
238 __fb_con.
h_in = fileno(__fb_con.
f_in);
241 if( tcgetpgrp( STDOUT_FILENO ) != getpgid( 0 ) )
245 if( tcgetattr( STDOUT_FILENO, &__fb_con.old_term_out ) )
247 memcpy(&term_out, &__fb_con.old_term_out,
sizeof(term_out));
248 term_out.c_oflag |= OPOST;
249 if( tcsetattr( STDOUT_FILENO, TCSANOW, &term_out ) )
255 memcpy(&term_in, &__fb_con.
old_term_in,
sizeof(term_in));
257 term_in.c_iflag |= BRKINT;
259 term_in.c_iflag &= ~(IXOFF | IXON | IGNBRK);
261 term_in.c_lflag &= ~(ICANON | ECHO);
263 term_in.c_cc[VMIN] = 1;
264 term_in.c_cc[VTIME] = 0;
265 if (tcsetattr(__fb_con.
h_in, TCSANOW, &term_in))
321 tcsetattr( STDOUT_FILENO, TCSANOW, &__fb_con.old_term_out );
328 fclose(__fb_con.
f_in);
336 const int sigs[] = { SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGINT, SIGQUIT, -1 };
342 pthread_mutexattr_t attr;
345 #if defined(__GNUC__) && defined(__i386__)
346 unsigned int control_word;
349 __asm__ __volatile__(
"fstcw %0" :
"=m" (control_word) : );
351 control_word = (control_word & 0xF0FF) | 0x300;
353 __asm__ __volatile__(
"fldcw %0" : :
"m" (control_word) );
358 pthread_mutexattr_init(&attr);
366 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
368 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
372 pthread_mutex_init(&__fb_global_mutex, &attr);
373 pthread_mutex_init(&__fb_string_mutex, &attr);
378 memset(&__fb_con, 0,
sizeof(__fb_con));
381 term = getenv(
"TERM");
382 if ((!term) || (tgetent(buffer, term) <= 0))
385 p = tgetstr(
"pc",
NULL);
387 if (tcgetattr(1, &tty))
389 ospeed = cfgetospeed(&tty);
396 if ((!strcmp(term,
"console")) || (!strncmp(term,
"linux", 5)))
401 if (!strncasecmp(term,
"eterm", 5))
403 else if (!strncmp(term,
"xterm", 5))
415 for (i = 0; sigs[i] >= 0; i++)
431 #if defined HOST_LINUX && (defined HOST_X86 || defined HOST_X86_64)
433 __fb_con.has_perm = ioperm(0, 0x400, 1) ?
FALSE :
TRUE;
449 pthread_mutex_destroy(&__fb_global_mutex);
450 pthread_mutex_destroy(&__fb_string_mutex);