git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/Term/main.c

      1 
      2 /*=========================================================================
      3  *
      4  * fbterm, a terminal emulator based on ggiterm for FBUI.
      5  * Copyright (C) 2004 Zachary T Smith, fbui@comcast.net
      6  * Portions from ggiterm are Copyright (C) by Aurelien Reynaud.
      7  *
      8  * This module is free software; you can redistribute it and/or modify
      9  * it under the terms of the GNU General Public License as published by
     10  * the Free Software Foundation; either version 2 of the License, or
     11  * (at your option) any later version.
     12  *
     13  * This module is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  * GNU General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU General Public License
     19  * along with this module; if not, write to the Free Software
     20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     21  *
     22  * (See the file COPYING in the main directory of this archive for
     23  * more details.)
     24  *
     25  *=======================================================================*/
     26 
     27 #include <linux/fb.h>
     28 
     29 #include "libfbui.h"
     30 #include "fbterm.h"
     31 
     32 #define BLINK_TIME 500
     33 
     34 static Display *dpy = NULL;
     35 static Window *win = NULL;
     36 static Font *font = NULL;
     37 
     38 extern struct winsize ws;
     39 extern int master_fd;
     40 
     41 extern int terminal_width, terminal_height;
     42 
     43 static wchar_t *exposebuffer;
     44 static char *exposebuffer_fg;
     45 static char *exposebuffer_bg;
     46 
     47 /* Global variables */
     48 int vis_w, vis_h;
     49 int cursor_x, cursor_y, cursor_x0, cursor_y0, cell_w, cell_h;
     50 static RGB color[16] = {
     51 	0,
     52 	0xb00000,
     53 	0xb000,
     54 	0xb0b000,
     55 	0xb0,
     56 	0xb000b0,
     57 	0xb0b0,
     58 	0xb0b0b0,
     59 
     60 	0,
     61 	0xff0000,
     62 	0xff00,
     63 	0xffff00,
     64 	0xff,
     65 	0xff00ff,
     66 	0xffff,
     67 	0xffffff,
     68 };
     69 
     70 int default_bgcolor, default_fgcolor, fgcolor, bgcolor;
     71 int blink_mode = 0, bold_mode = 0, invisible_mode = 0, reverse_mode = 0, underline_mode = 0, altcharset_mode = 0;
     72 int autowrap = 1, force_cursor_mode = 0;
     73 int region_top, region_bottom;
     74 extern int linewrap_pending;
     75 
     76 extern int (*fbtermPutShellChar) (unsigned char *, size_t *, wchar_t);
     77 
     78 void redraw ()
     79 {
     80 	int j;
     81 	for (j=0; j<terminal_height; j++) {
     82 		int i;
     83 		for (i=0; i<terminal_width; ) {
     84 			int ix =i + j*terminal_width;
     85 			wchar_t ch = exposebuffer[ix];
     86 			char s[terminal_width];
     87 			unsigned long fg, bg;
     88 			char my_fg = exposebuffer_fg [ix];
     89 			char my_bg = exposebuffer_bg [ix];
     90 			fg = color [my_fg];
     91 			bg = color [my_bg];
     92 
     93 			short x0,y0;
     94 			x0 = cell_w*i;
     95 			y0 = cell_h*j;
     96 
     97 			char all_blanks = 1;
     98 
     99 			/* find maximal area that has the same bg/fg colors */
    100 			int k=1;
    101 			s[0] = ch;
    102 			while ((k+i)<terminal_width && exposebuffer_fg[ix+k]==my_fg &&
    103 				exposebuffer_bg[ix+k]==my_bg) 
    104 			{
    105 				ch = s[k] = exposebuffer[ix+k];
    106 				if (ch != ' ')
    107 					all_blanks = 0;
    108 				k++;
    109 			}
    110 			s[k] = 0;
    111 			fbui_fill_area (dpy, win, x0,y0,x0+(k*cell_w)-1,y0+cell_h-1, bg);
    112 
    113 			if (!all_blanks)
    114 				fbui_draw_string (dpy, win, NULL, x0,y0, s, fg);
    115 			i += k;
    116 		}
    117 	}
    118 }
    119 
    120 void resize(int new_width, int new_height)
    121 {
    122 	int i, j;
    123 
    124 	if (new_width==terminal_width && new_height==terminal_height)
    125 		return;
    126 
    127 	unsigned long size = new_width * new_height;
    128 	wchar_t *newbuffer = (wchar_t*) malloc(sizeof(wchar_t) * size);
    129 	char *newbuffer_fg = malloc(size);
    130 	char *newbuffer_bg = malloc(size);
    131 
    132 	if (!newbuffer || !newbuffer_fg || !newbuffer_bg)
    133 		FATAL ("out of memory in resize()");
    134 
    135 	for (j=0;j<size;j++) {
    136 		newbuffer[j] = ' ';
    137 		newbuffer_fg[j] = default_fgcolor;
    138 		newbuffer_bg[j] = default_bgcolor;
    139 	}
    140 
    141 	for (j=0; j<terminal_height; j++) {
    142 		for (i=0; i<terminal_width; i++) {
    143 			if (i < new_width && j < new_height) {
    144 				int ix =i + j*terminal_width;
    145 				wchar_t ch = exposebuffer[ix];
    146 				char fg = exposebuffer_fg [ix];
    147 				char bg = exposebuffer_bg [ix];
    148 
    149 				int ix2 = i + j*new_width;
    150 				newbuffer[ix2] = ch;
    151 				newbuffer_fg[ix2] = fg;
    152 				newbuffer_fg[ix2] = bg;
    153 			}
    154 		}
    155 	}
    156 
    157 	free (exposebuffer);
    158 	free (exposebuffer_fg);
    159 	free (exposebuffer_bg);
    160 
    161 	exposebuffer = newbuffer;
    162 	exposebuffer_fg = newbuffer_fg;
    163 	exposebuffer_bg = newbuffer_bg;
    164 
    165 	terminal_width = new_width;
    166 	terminal_height = new_height;
    167 
    168 #if 0
    169 	redraw();
    170 
    171 	fbui_draw_line (dpy,win,0,0,100,100,RGB_YELLOW);
    172 	fbui_flush (dpy,win);
    173 #endif
    174 }
    175 
    176 void scroll_exposebuf_up(int num, int top, int bottom)
    177 {
    178 	int i,j,k;
    179 	int y;
    180 	for (j=top+num; j<=bottom; j+=num) {
    181 		k=0;
    182 		while (k < num) {
    183 			y = j+k;
    184 			if (y < terminal_height) 
    185 			for (i=0; i<terminal_width; i++) {
    186 				int ix = i+terminal_width*y;
    187 				exposebuffer [ix-terminal_width] = exposebuffer[ix];
    188 				exposebuffer_fg [ix-terminal_width] = exposebuffer_fg [ix];
    189 				exposebuffer_bg [ix-terminal_width] = exposebuffer_bg [ix];
    190 			}
    191 			k++;
    192 		}
    193 	}
    194 	k=0;
    195 	while (k < num) {
    196 		y = bottom-k-1;
    197 		if (y < terminal_height) 
    198 		for (i=0; i<terminal_width; i++) {
    199 			int ix = i+terminal_width*y;
    200 			exposebuffer [ix] = ' ';
    201 			exposebuffer_fg [ix] = default_bgcolor;
    202 			exposebuffer_bg [ix] = default_bgcolor;
    203 		}
    204 		k++;
    205 	}
    206 }
    207 
    208 
    209 void scroll_exposebuf_down()
    210 {
    211 	int i,j;
    212 	for (j=terminal_height-2; j>=0; j--) {
    213 		for (i=0; i<terminal_width; i++) {
    214 			int ix = i+terminal_width*j;
    215 			exposebuffer [ix+terminal_width] = exposebuffer[ix];
    216 			exposebuffer_fg [ix+terminal_width] = exposebuffer_fg [ix];
    217 			exposebuffer_bg [ix+terminal_width] = exposebuffer_bg [ix];
    218 		}
    219 	}
    220 	for (i=0; i<terminal_width; i++) {
    221 		exposebuffer [i] = ' ';
    222 		exposebuffer_fg [i] = default_bgcolor;
    223 		exposebuffer_bg [i] = default_bgcolor;
    224 	}
    225 }
    226 
    227 
    228 
    229 void MoveCursor (int, int);
    230 
    231 
    232 wchar_t AsciiGetAltChar (wchar_t charcode)
    233 {
    234 		switch (charcode)
    235 		{
    236 			case '+': charcode = '>'; break; /* vt100 '+': arrow pointing right */
    237 			case ',': charcode = '<'; break; /* vt100 ',': arrow pointing left */
    238 			case '-': charcode = 0136; break; /* vt100 '-': arrow pointing up */
    239 			case '.': charcode = 'v'; break; /* vt100 '.': arrow pointing down */
    240 			case '0': charcode = '#'; break; /* vt100 '0': solid square block */
    241 			case '`': charcode = '+'; break; /* vt100 '`': diamond */
    242 			case 'a': charcode = ':'; break; /* vt100 'a': checker board (stipple) */
    243 			case 'f': charcode = '\\'; break; /* vt100 'f': degree */
    244 			case 'g': charcode = '#'; break; /* vt100 'g': plus/minus */
    245 			case 'h': charcode = '#'; break; /* vt100 'h': board os squares */
    246 			case 'j': charcode = '+'; break; /* vt100 'j': rightdown corner */
    247 			case 'k': charcode = '+'; break; /* vt100 'k': upright corner */
    248 			case 'l': charcode = '+'; break; /* vt100 'l': upleft corner */
    249 			case 'm': charcode = '+'; break; /* vt100 'm': downleft corner */
    250 			case 'n': charcode = '+'; break; /* vt100 'n': crossover */
    251 			case 'o': charcode = '~'; break; /* vt100 'o': scan line 1 */
    252 			case 'p': charcode = '-'; break; /* vt100 'p': scan line 3 */
    253 			case 'q': charcode = '-'; break; /* vt100 'q': horizontal line */
    254 			case 'r': charcode = '-'; break; /* vt100 'r': scan line 7 */
    255 			case 's': charcode = '_'; break; /* vt100 's': scan line 9 */
    256 			case 't': charcode = '+'; break; /* vt100 't': tee pointing right */
    257 			case 'u': charcode = '+'; break; /* vt100 'u': tee pointing left */
    258 			case 'v': charcode = '+'; break; /* vt100 'v': tee pointing up */
    259 			case 'w': charcode = '+'; break; /* vt100 'w': tee pointing down */
    260 			case 'x': charcode = '|'; break; /* vt100 'x': vertical line */
    261 			case 'y': charcode = '<'; break; /* vt100 'y': less or equal */
    262 			case 'z': charcode = '>'; break; /* vt100 'z': more or equal */
    263 			case '{': charcode = '*'; break; /* vt100 '{': Pi */
    264 			case '|': charcode = '!'; break; /* vt100 '|': not equal */
    265 			case '}': charcode = 'f'; break; /* vt100 '}': UK pound */
    266 			case '~': charcode = 'o'; break; /* vt100 '~': bullet */
    267 		}
    268 		return charcode;
    269 }
    270 
    271 
    272 void
    273 fbtermBlinkCursor (int force)
    274 {
    275 	int x,y;
    276 	x = cell_w*(int)(cursor_x/cell_w);
    277 	y = cell_h*(int)(cursor_y/cell_h);
    278 
    279 	if (force != CURSOR_HIDE)
    280 	{
    281 		fbui_fill_area (dpy, win,
    282 			x, y, x + cell_w - 1, y + cell_h - 1, color[fgcolor]);
    283 	}
    284 	else
    285 	{
    286 		fbui_fill_area (dpy, win,
    287 			x, y, x + cell_w - 1, y + cell_h - 1, color[bgcolor]);
    288 	}
    289 	fbui_flush (dpy, win);
    290 }
    291 
    292 void
    293 fbtermDeleteChars (unsigned int nb_chars)
    294 {
    295 	int x, y;
    296 	
    297 	x = cell_w * (int)(cursor_x / cell_w);
    298 	y = cell_h * (int)(cursor_y / cell_h);
    299 	/* Copy the characters between current+nb_chars and EOL to current position */
    300 	/*ggiFlush (vis);*/
    301 
    302 	fbui_flush (dpy, win);
    303 	fbui_copy_area (dpy, win, x+(nb_chars*cell_w), y, x, y,
    304 		vis_w-x-(nb_chars*cell_w), cell_h);
    305 }
    306 
    307 void
    308 fbtermInsertChars (unsigned int nb_chars)
    309 {
    310 	int x, y;
    311 	
    312 	x = cell_w * (int)(cursor_x / cell_w);
    313 	y = cell_h * (int)(cursor_y / cell_h);
    314 
    315 	fbui_copy_area (dpy, win, x, y, x+(nb_chars*cell_w), y, vis_w-x, cell_h);
    316 	fbui_fill_area (dpy, win, x, y, nb_chars*cell_w, cell_h, color[default_bgcolor]);
    317 }
    318 
    319 void
    320 fbtermEraseNChars (unsigned int nb_chars)
    321 {
    322 	int x, y;
    323 	
    324 	x = cell_w * (int)(cursor_x / cell_w);
    325 	y = cell_h * (int)(cursor_y / cell_h);
    326 
    327 	fbui_fill_area (dpy, win, x, y, nb_chars*cell_w, cell_h, color[default_bgcolor]);
    328 }
    329 
    330 void
    331 fbtermEraseToEOL ()
    332 {
    333 	int x, y;
    334 	
    335 	x = cell_w * (int)(cursor_x / cell_w);
    336 	y = cell_h * (int)(cursor_y / cell_h);
    337 
    338 	fbui_fill_area (dpy, win, x, y, vis_w-x, cell_h, color[default_bgcolor]);
    339 }
    340 
    341 void
    342 fbtermEraseFromBOL ()
    343 {
    344 	int x, y;
    345 	
    346 	x = cell_w * (int)(cursor_x / cell_w + 1);
    347 	y = cell_h * (int)(cursor_y / cell_h);
    348 
    349 	fbui_fill_area (dpy, win, 0, y, x, cell_h, color[default_bgcolor]);
    350 }
    351 
    352 void
    353 fbtermEraseLine ()
    354 {
    355 	int y;
    356 	
    357 	y = cell_h * (int)(cursor_y / cell_h);
    358 
    359 	fbui_fill_area (dpy, win, 0, y, vis_w, cell_h, color[default_bgcolor]);
    360 }
    361 
    362 void
    363 fbtermEraseToEOD ()
    364 {
    365 	int y;
    366 	
    367 	fbtermEraseToEOL ();
    368 	y = cell_h * (int)(cursor_y / cell_h + 1);
    369 
    370 	fbui_fill_area (dpy, win, 0, y, vis_w, vis_h-y, color[default_bgcolor]);
    371 }
    372 
    373 void
    374 fbtermEraseFromBOD ()
    375 {
    376 	int y;
    377 	
    378 	fbtermEraseFromBOL ();
    379 	y = cell_h * (int)(cursor_y / cell_h);
    380 
    381 	fbui_fill_area (dpy, win, 0, 0, vis_w, y, color[default_bgcolor]);
    382 }
    383 
    384 void
    385 fbtermEraseDisplay ()
    386 {
    387 	int i;
    388 
    389 	fbui_fill_area (dpy, win, 0,0, 
    390 		terminal_width*cell_w-1,
    391 		terminal_height*cell_h-1,
    392 		color [default_bgcolor]);
    393 	fbui_flush (dpy, win);
    394 
    395 	int c = default_bgcolor;
    396 	int size = terminal_width * terminal_height;
    397 	for (i=0; i<size; i++) {
    398 		exposebuffer[i] = ' ';
    399 		exposebuffer_fg[i] = c;
    400 		exposebuffer_bg[i] = c;
    401 	}
    402 }
    403 
    404 void
    405 fbtermScrollUp (unsigned int nb_lines)
    406 {
    407 	debug (DEBUG_DETAIL, "Scrolling %d lines in region %d-%d", nb_lines, region_top, region_bottom);
    408 
    409 #if 0
    410 	/* too slow due to video memory read speed */
    411 
    412 	fbui_copy_area (dpy, win, 0, (region_top+nb_lines)*cell_h, 
    413 		0, region_top*cell_h,
    414 		vis_w, (region_bottom-region_top-nb_lines)*cell_h);
    415 	int y = (region_bottom - nb_lines)*cell_h;
    416 	fbui_fill_area (dpy, win, 0, y, vis_w, vis_h, color[default_bgcolor]);
    417 #endif
    418 
    419 	scroll_exposebuf_up (nb_lines, region_top, region_bottom);
    420 	redraw();
    421 }
    422 
    423 void
    424 fbtermScrollDown (unsigned int nb_lines)
    425 {
    426 	debug (DEBUG_DETAIL, "Scrolling %d lines in region %d-%d", nb_lines, region_top, region_bottom);
    427 #if 0
    428 	/* too slow due to video memory read speed */
    429 
    430 	fbui_copy_area (dpy, win, 0, region_top*cell_h, 
    431 		0, (region_top+nb_lines)*cell_h,
    432 		vis_w, (region_bottom-region_top-nb_lines)*cell_h);
    433 	int y = region_top*cell_h;
    434 	fbui_fill_area (dpy, win, 0, 0, vis_w, y-1, color[default_bgcolor]);
    435 #endif
    436 
    437 	scroll_exposebuf_down();
    438 	redraw();
    439 }
    440 
    441 void
    442 fbtermNextPos ()
    443 {
    444 	MoveCursor (cursor_x + cell_w, cursor_y);
    445 }
    446 
    447 
    448 int
    449 FBUIWriteChar (wchar_t charcode)
    450 {
    451 	int cur_bgcolor, cur_fgcolor, dummy_color;
    452 	
    453 	if (linewrap_pending)
    454 	{
    455 		MoveCursor (cursor_x0, cursor_y + cell_h);
    456 	}
    457 	cur_bgcolor = bgcolor;
    458 	cur_fgcolor = fgcolor;
    459 	if (altcharset_mode)
    460 	{
    461 		charcode = AsciiGetAltChar (charcode);
    462 	}
    463 	if (reverse_mode)
    464 	{
    465 		dummy_color = cur_bgcolor;
    466 		cur_bgcolor = cur_fgcolor;
    467 		cur_fgcolor = dummy_color;
    468 	}
    469 	if (invisible_mode)
    470 	{
    471 		cur_fgcolor = cur_bgcolor;
    472 	}
    473 	if (bold_mode)
    474 	{
    475 		cur_fgcolor += 8;
    476 	}
    477 
    478 	fbui_fill_area (dpy, win, cursor_x, cursor_y, cursor_x + cell_w - 1, cursor_y + cell_h - 1, color[cur_bgcolor]);
    479 	char s[2] = { (char)charcode, 0 };
    480 
    481 	fbui_draw_string (dpy, win, NULL, cursor_x, cursor_y, s, color[cur_fgcolor]);
    482 
    483 	/* Save to buffer for later expose */
    484 	int i = cursor_x / cell_w;
    485 	int j = cursor_y / cell_h;
    486 	int ix = i + j * terminal_width;
    487 	int size = terminal_width * terminal_height;
    488 	if (ix < size) {
    489 		exposebuffer [ix] = charcode;
    490 		exposebuffer_fg [ix] = cur_fgcolor;
    491 		exposebuffer_bg [ix] = cur_bgcolor;
    492 	}
    493 
    494 	if (underline_mode) {
    495 		fbui_draw_hline (dpy, win, cursor_x, cursor_x+cell_w-1, cursor_y+cell_h-1,
    496 			color[cur_fgcolor]);
    497 		fbui_flush (dpy, win);
    498 	} 
    499 
    500 	return 0;
    501 }
    502 
    503 
    504 
    505 void
    506 HandleFBUIEvents (unsigned char *shellinput, size_t *shellinput_size)
    507 {
    508 	struct timeval tv;
    509 	int i;
    510 
    511 	tv.tv_sec = 0;
    512 	tv.tv_usec = 0;
    513 
    514 	Event ev;
    515 	unsigned char event_num;
    516 
    517 	usleep (3000);
    518 	if (fbui_poll_event (dpy, &ev, FBUI_EVENTMASK_ALL))
    519 		return;
    520 
    521 	event_num = ev.type;
    522 
    523 	if (ev.win != win) {
    524 printf ("event id = %d, window id %d\n", ev.id, win->id);
    525 		FATAL ("event not for fbterm window");
    526 	}
    527 
    528 	if (event_num == FBUI_EVENT_EXPOSE) {
    529 		redraw();
    530 	}
    531 	else if (event_num == FBUI_EVENT_MOVE_RESIZE) {
    532 		vis_w = ev.width;
    533 		vis_h = ev.height;
    534 		int new_width = vis_w / cell_w;
    535 		int new_height = vis_h / cell_h;
    536 		resize (new_width, new_height);
    537 
    538 		// update the OS
    539 		ws.ws_col = terminal_width;
    540 		ws.ws_row = terminal_height;
    541 		ws.ws_xpixel = vis_w;
    542 		ws.ws_ypixel = vis_h;
    543         	if (ioctl (master_fd, TIOCSWINSZ, &ws) < 0) {
    544 			WARNING("unable to set tty window size");
    545 		}
    546 	}
    547 	else if (event_num == FBUI_EVENT_ENTER) {
    548 		printf ("fbterm got Enter\n");
    549 		return;
    550 	}
    551 	else if (event_num == FBUI_EVENT_LEAVE) {
    552 		printf ("fbterm got Leave\n");
    553 		return;
    554 	}
    555 	else if (event_num == FBUI_EVENT_ACCEL) {
    556 		// Not using accelerators
    557 		return;
    558 	}
    559 	else if (event_num == FBUI_EVENT_KEY) {
    560 		short ch = fbui_convert_key (dpy, ev.key);
    561 		if (!ch) 
    562 			return;
    563 
    564 		if (SHELLINPUT_SIZE - *shellinput_size >= (MB_CUR_MAX>3?MB_CUR_MAX:3))
    565 		switch (ch) {
    566 		case '\n':
    567 			shellinput[*shellinput_size] = '\n';
    568 			(*shellinput_size)++;
    569 			break;
    570 
    571 		case 8:
    572 		case 9:
    573 		case FBUI_DEL:
    574 			shellinput[*shellinput_size] = 0x08;
    575 			(*shellinput_size)++;
    576 			break;
    577 
    578 		default:
    579 			fbtermPutShellChar (shellinput, shellinput_size, ch);
    580 			break;
    581 
    582 		case FBUI_F1:
    583 			memcpy (shellinput+*shellinput_size, "\033OP", 3);
    584 			(*shellinput_size) += 3;
    585 			break;
    586 		case FBUI_F2:
    587 			memcpy (shellinput+*shellinput_size, "\033OQ", 3);
    588 			(*shellinput_size) += 3;
    589 			break;
    590 		case FBUI_F3:
    591 			memcpy (shellinput+*shellinput_size, "\033OR", 3);
    592 			(*shellinput_size) += 3;
    593 			break;
    594 		case FBUI_F4:
    595 			memcpy (shellinput+*shellinput_size, "\033OS", 3);
    596 			(*shellinput_size) += 3;
    597 			break;
    598 		case FBUI_F5:
    599 			memcpy (shellinput+*shellinput_size, "\033Ot", 3);
    600 			(*shellinput_size) += 3;
    601 			break;
    602 		case FBUI_F6:
    603 			memcpy (shellinput+*shellinput_size, "\033Ou", 3);
    604 			(*shellinput_size) += 3;
    605 			break;
    606 		case FBUI_F7:
    607 			memcpy (shellinput+*shellinput_size, "\033Ov", 3);
    608 			(*shellinput_size) += 3;
    609 			break;
    610 		case FBUI_F8:
    611 			memcpy (shellinput+*shellinput_size, "\033Ol", 3);
    612 			(*shellinput_size) += 3;
    613 			break;
    614 		case FBUI_F9:
    615 			memcpy (shellinput+*shellinput_size, "\033Ow", 3);
    616 			(*shellinput_size) += 3;
    617 			break;
    618 		case FBUI_F10:
    619 			memcpy (shellinput+*shellinput_size, "\033Ox", 3);
    620 			(*shellinput_size) += 3;
    621 			break;
    622 
    623 		case FBUI_UP:
    624 			memcpy (shellinput+*shellinput_size, "\033OA", 3);
    625 			(*shellinput_size) += 3;
    626 			break;
    627 		case FBUI_DOWN:
    628 			memcpy (shellinput+*shellinput_size, "\033OB", 3);
    629 			(*shellinput_size) += 3;
    630 			break;
    631 		case FBUI_LEFT:
    632 			memcpy (shellinput+*shellinput_size, "\033OD", 3);
    633 			(*shellinput_size) += 3;
    634 			break;
    635 		case FBUI_RIGHT:
    636 			memcpy (shellinput+*shellinput_size, "\033OC", 3);
    637 			(*shellinput_size) += 3;
    638 			break;
    639 		case FBUI_PGUP:
    640 			memcpy (shellinput+*shellinput_size, "\033Os", 3);
    641 			(*shellinput_size) += 3;
    642 			break;
    643 		case FBUI_PGDN:
    644 			memcpy (shellinput+*shellinput_size, "\033On", 3);
    645 			(*shellinput_size) += 3;
    646 			break;
    647 		case FBUI_INS:
    648 			memcpy (shellinput+*shellinput_size, "\033[L", 3);
    649 			(*shellinput_size) += 3;
    650 			break;
    651 		case FBUI_HOME:
    652 			memcpy (shellinput+*shellinput_size, "\033Oq", 3);
    653 			(*shellinput_size) += 3;
    654 			break;
    655 		case FBUI_END:
    656 			memcpy (shellinput+*shellinput_size, "\033Op", 3);
    657 			(*shellinput_size) += 3;
    658 			break;
    659 		}
    660 	}
    661 }
    662 
    663 
    664 void FBUIMapColors ()
    665 {
    666 	int i;
    667 }
    668 
    669 int
    670 FBUIInit_graphicsmode (int vc,short cols,short rows,short xrel,short yrel)
    671 {
    672 	int err;
    673 	char dummy[64];
    674 	long size = rows * cols;
    675 
    676 	terminal_width = cols;
    677 	terminal_height = rows;
    678 
    679 printf ("cols=%d rows=%d\n",cols,rows);
    680 
    681 	exposebuffer=(wchar_t*) malloc(size * sizeof(wchar_t));
    682 	exposebuffer_fg=(char*) malloc(size);
    683 	exposebuffer_bg=(char*) malloc(size);
    684 
    685         font = font_new ();
    686         if (!pcf_read (font, "courR14.pcf")) {
    687                 font_free (font);
    688 		FATAL ("cannot load font");
    689         }
    690 
    691 	cell_h = font->ascent + font->descent;
    692 
    693 	if (!cell_h) {
    694 		fbui_window_close (dpy, win);
    695 		printf ("oops, invalid font data\n");
    696 		exit(1);
    697 	}
    698 	
    699 	cell_w = font->widths [' ' - font->first_char];
    700 
    701 	if (!cell_w) {
    702 		cell_w = font->widths ['W' - font->first_char];
    703 		if (!cell_w) {
    704 			fbui_window_close (dpy, win);
    705 			printf ("oops, invalid font data\n");
    706 			exit(1);
    707 		}
    708 	}
    709 
    710 	int default_w = cell_w * cols;
    711 	int default_h = cell_h * rows;
    712 printf ("fbterm: trying for window size %d,%d to provide %dx%d text\n", default_w, default_h, rows,cols);
    713 
    714 	default_fgcolor = 7;
    715 	default_bgcolor = 0;
    716 	fgcolor = default_fgcolor;
    717 	bgcolor = default_bgcolor;
    718 
    719 	int argc=1;
    720 	char *argv[1] = {"foo"};
    721 
    722 	if (xrel > 999) {
    723 		xrel = 2;
    724 		yrel = 15;
    725 	}
    726 
    727         short win_w, win_h;
    728 
    729 	long fg;
    730 	long bg = color[default_bgcolor];
    731 
    732 	dpy = fbui_display_open ();
    733         if (!dpy)
    734                 FATAL ("cannot open display");
    735 	
    736         win = fbui_window_open (dpy, default_w, default_h, 
    737 		&win_w, &win_h,
    738 		9999,9999,
    739 		0, +20, 
    740 		&fg, &bg, 
    741 		"fbterm", "", 
    742 		FBUI_PROGTYPE_APP, 
    743 		false,false, 
    744 		vc,
    745 		true, 
    746 		false,
    747 		false, argc,argv);
    748         if (!win)
    749                 FATAL ("cannot create window");
    750 
    751 	vis_w = win_w;
    752 	vis_h = win_h;
    753 
    754 	if (win_w < 1 || win_h < 1) {
    755 		printf ("Don't have window dimensions yet.\n");
    756 	} else {
    757 		printf ("Window dimensions %d,%d (%dx%d) text\n", 
    758 			win_w,win_h,win_w/cell_w,win_h/cell_h);
    759 	}
    760 
    761 	cursor_x0 = 0;
    762 	cursor_y0 = 0;
    763 
    764 	if (fbui_set_font (dpy, win, font))
    765 		FATAL ("cannot set font");
    766 	
    767 	return 0;
    768 }
    769 
    770 
    771 #ifdef DEBUG
    772 void ShowGrid (void)
    773 {
    774 	int x, y;
    775 	
    776 	ggiSetGCForeground (vis, color[2]);
    777 	for (x = 0; x < vis_w; x += cell_w)
    778 	{
    779 		ggiDrawVLine (vis, x, 0, vis_h);
    780 	}
    781 	for (y = 0; y < vis_h; y += cell_h)
    782 	{
    783 		ggiDrawHLine (vis, 0, y, vis_w);
    784 	}
    785 }
    786 #endif /* DEBUG */
    787 
    788 void FBUIExit (void)
    789 {
    790 	fbui_window_close (dpy, win);
    791 	fbui_display_close (dpy);
    792 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.