X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fcurses_cons.c;h=1447bd57d46eddd9e4a3a50114f808a80ad35ef1;hp=2a96eb58226a10e11334b5bccbfaf6190572968b;hb=e9fc46a873dfb004c61cb1f245420e7ebcfad575;hpb=6d6988cc9c8bac21d96afd800076afe0915cf2cd diff --git a/palacios/src/devices/curses_cons.c b/palacios/src/devices/curses_cons.c index 2a96eb5..1447bd5 100644 --- a/palacios/src/devices/curses_cons.c +++ b/palacios/src/devices/curses_cons.c @@ -21,7 +21,7 @@ /* Interface between virtual video card and console */ #include -#include +#include #include #include #include @@ -31,45 +31,46 @@ #include -#define NUM_ROWS 25 -#define NUM_COLS 80 -#define BYTES_PER_COL 2 -#define BYTES_PER_ROW (NUM_COLS * BYTES_PER_COL) +#ifndef DEBUG_CURSES_CONS +#undef PrintDebug +#define PrintDebug(fmt, args...) +#endif -#define SCREEN_SIZE (BYTES_PER_ROW * NUM_ROWS) +#define BYTES_PER_COL 2 struct cons_state { v3_console_t cons; + int rows; + int cols; + uint8_t * framebuf; struct vm_device * frontend_dev; }; static int screen_update(uint_t x, uint_t y, uint_t length, void *private_data); -static uint_t last_offset; +static int screen_update_all(void * private_data) { + struct vm_device *dev = (struct vm_device *) private_data; + struct cons_state *state = (struct cons_state *)dev->private_data; + uint_t screen_size; + + screen_size = state->cols * state->rows * BYTES_PER_COL; + return screen_update(0, 0, screen_size, private_data); +} static int cursor_update(uint_t x, uint_t y, void *private_data) { struct vm_device *dev = (struct vm_device *) private_data; struct cons_state *state = (struct cons_state *) dev->private_data; uint_t offset; - uint_t last_x, last_y; + + PrintDebug("cursor_update(%d, %d, %p)\n", x, y, private_data); /* avoid out-of-range coordinates */ - if (x >= NUM_COLS) x = NUM_COLS - 1; - if (y >= NUM_ROWS) y = NUM_ROWS - 1; - offset = (x * BYTES_PER_COL) + (y * BYTES_PER_ROW); - - /* unfortunately Palacios sometimes misses some writes, - * but if they are accompanied by a cursor move we may be able to - * detect this - */ - if (offset < last_offset) last_offset = 0; - - if (offset > last_offset) { - last_x = (last_offset % BYTES_PER_ROW) / BYTES_PER_COL; - last_y = last_offset / BYTES_PER_ROW; - screen_update(last_x, last_y, offset - last_offset, private_data); - } + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x >= state->cols) x = state->cols - 1; + if (y >= state->rows) y = state->rows - 1; + offset = (x + y * state->cols) * BYTES_PER_COL; /* adjust cursor */ if (v3_console_set_cursor(state->cons, x, y) < 0) { @@ -89,23 +90,28 @@ static int cursor_update(uint_t x, uint_t y, void *private_data) static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data) { struct vm_device * dev = (struct vm_device *)private_data; struct cons_state * state = (struct cons_state *)dev->private_data; - uint_t offset = (x * BYTES_PER_COL) + (y * BYTES_PER_ROW); - uint8_t fb_buf[length]; + uint_t offset = (x + y * state->cols) * BYTES_PER_COL; int i; uint_t cur_x = x; uint_t cur_y = y; + if (length > (state->rows * state->cols * BYTES_PER_COL)) { + PrintError("Screen update larger than curses framebuffer\n"); + return 0; + } + + PrintDebug("screen_update(%d, %d, %d, %p)\n", x, y, length, private_data); + /* grab frame buffer */ - memset(fb_buf, 0, length); - v3_cons_get_fb(state->frontend_dev, fb_buf, offset, length); + v3_cons_get_fb(state->frontend_dev, state->framebuf, offset, length); /* update the screen */ for (i = 0; i < length; i += 2) { uint_t col_index = i; uint8_t col[2]; - col[0] = fb_buf[col_index]; // Character - col[1] = fb_buf[col_index + 1]; // Attribute + col[0] = state->framebuf[col_index]; // Character + col[1] = state->framebuf[col_index + 1]; // Attribute /* update current character */ if (v3_console_set_char(state->cons, cur_x, cur_y, col[0], col[1]) < 0) { @@ -116,8 +122,8 @@ static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data) // CAUTION: the order of these statements is critical // cur_y depends on the previous value of cur_x - cur_y = cur_y + ((cur_x + 1) / NUM_COLS); - cur_x = (cur_x + 1) % NUM_COLS; + cur_y = cur_y + ((cur_x + 1) / state->cols); + cur_x = (cur_x + 1) % state->cols; } /* done with console update */ @@ -126,9 +132,6 @@ static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data) return -1; } - /* store offset to catch missing notifications */ - last_offset = offset + length; - return 0; } @@ -136,9 +139,11 @@ static int scroll(int rows, void * private_data) { struct vm_device *dev = (struct vm_device *)private_data; struct cons_state *state = (struct cons_state *)dev->private_data; + PrintDebug("scroll(%d, %p)\n", rows, private_data); + if (rows < 0) { /* simply update the screen */ - return screen_update(0, 0, SCREEN_SIZE, private_data); + return screen_update_all(private_data); } if (rows > 0) { @@ -153,13 +158,32 @@ static int scroll(int rows, void * private_data) { PrintError("console update (0x%p) failed\n", state->cons); return -1; } - - last_offset = BYTES_PER_ROW * (NUM_ROWS - 1); } return 0; } +static int set_text_resolution(int cols, int rows, void * private_data) { + struct vm_device *dev = (struct vm_device *)private_data; + struct cons_state *state = (struct cons_state *)dev->private_data; + + PrintDebug("set_text_resolution(%d, %d, %p)\n", cols, rows, private_data); + + /* store resolution for internal use */ + V3_ASSERT(cols >= 1); + V3_ASSERT(rows >= 1); + state->cols = cols; + state->rows = rows; + + /* set notification regarding resolution change */ + if (v3_console_set_text_resolution(state->cons, cols, rows) < 0) { + PrintError("console set_text_resolution (0x%p, %u, %u) failed\n", state->cons, cols, rows); + return -1; + } + + /* update the screen */ + return screen_update_all(private_data); +} static int cons_free(struct cons_state * state) { v3_console_close(state->cons); @@ -174,15 +198,14 @@ static int cons_free(struct cons_state * state) { static int console_event_handler(struct v3_vm_info * vm, struct v3_console_event * evt, void * priv_data) { - screen_update(0, 0, SCREEN_SIZE, priv_data); - - return 0; + return screen_update_all(priv_data); } static struct v3_console_ops cons_ops = { .update_screen = screen_update, .update_cursor = cursor_update, .scroll = scroll, + .set_text_resolution = set_text_resolution, }; static struct v3_device_ops dev_ops = { @@ -208,9 +231,13 @@ static int cons_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) V3_ASSERT(state); state->frontend_dev = frontend; + state->cols = 80; + state->rows = 25; + state->framebuf = V3_Malloc(state->cols * state->rows * BYTES_PER_COL); + /* open tty for screen display */ - state->cons = v3_console_open(vm, NUM_COLS, NUM_ROWS); + state->cons = v3_console_open(vm, state->cols, state->rows); if (!state->cons) { PrintError("Could not open console\n"); @@ -220,7 +247,7 @@ static int cons_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) /* allocate device */ struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state); - + if (dev == NULL) { PrintError("Could not attach device %s\n", dev_id); V3_Free(state);