diff --git a/Makefile b/Makefile index af6c197..1d8ab27 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,11 @@ TARGET = libconio.a OBJS = conio.o input.o draw.o KOS_CFLAGS += -Iinclude -DBUILD_LIBCONIO -all: create_kos_link defaultall +defaultall: create_kos_link $(OBJS) subdirs linklib include $(KOS_BASE)/addons/Makefile.prefab # creates the kos link to the headers create_kos_link: - rm -f ../include/conio - ln -s ../libconio/include ../include/conio + rm -f $(KOS_PORTS)/include/conio + ln -s $(CURDIR)/include $(KOS_PORTS)/include/conio diff --git a/conio.c b/conio.c index d2c2395..3ec95e7 100644 --- a/conio.c +++ b/conio.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -16,7 +17,6 @@ #include #include #include -#include #include "conio.h" /* the cursor */ @@ -37,314 +37,315 @@ file_t conio_serial_fd; /* scroll everything up a line */ void conio_scroll(void) { - int i; - - switch (conio_ttymode) { - case CONIO_TTY_PVR: - memmove(conio_virtscr, conio_virtscr[1], (CONIO_NUM_ROWS - 1) * CONIO_NUM_COLS); - for (i = 0; i < CONIO_NUM_COLS; i++) - conio_virtscr[CONIO_NUM_ROWS - 1][i] = ' '; - conio_cursor.row--; - break; - case CONIO_TTY_SERIAL: - scif_write_buffer((const uint8 *)"\x1b[M", 3, 1); - break; - case CONIO_TTY_STDIO: - fs_write(conio_serial_fd, (void *)"\x1b[M", 3); - break; - case CONIO_TTY_DBGIO: - dbgio_write_buffer((const uint8 *)"\x1b[M", 3); - break; - } + int i; + + switch (conio_ttymode) { + case CONIO_TTY_PVR: + memmove(conio_virtscr, conio_virtscr[1], (CONIO_NUM_ROWS - 1) * CONIO_NUM_COLS); + for (i = 0; i < CONIO_NUM_COLS; i++) + conio_virtscr[CONIO_NUM_ROWS - 1][i] = ' '; + conio_cursor.row--; + break; + case CONIO_TTY_SERIAL: + scif_write_buffer((const uint8_t *)"\x1b[M", 3, 1); + break; + case CONIO_TTY_STDIO: + fs_write(conio_serial_fd, (void *)"\x1b[M", 3); + break; + case CONIO_TTY_DBGIO: + dbgio_write_buffer((const uint8_t *)"\x1b[M", 3); + break; + } } /* move the cursor back, don't scroll (we can't) */ void conio_deadvance_cursor(void) { - switch (conio_ttymode) { - case CONIO_TTY_PVR: - conio_cursor.col--; - if (conio_cursor.col < 0) { - if (conio_cursor.row != 0) { - conio_cursor.col = CONIO_NUM_COLS - 1; - conio_cursor.row--; - } else { - conio_cursor.col = 0; - } - } - break; - case CONIO_TTY_SERIAL: - scif_write(8); - scif_write(32); - scif_write(8); - break; - case CONIO_TTY_STDIO: - fs_write(conio_serial_fd, (void *)"\x8\x20\x8", 3); - break; - case CONIO_TTY_DBGIO: - dbgio_write(8); - dbgio_write(32); - dbgio_write(8); - break; - } + switch (conio_ttymode) { + case CONIO_TTY_PVR: + conio_cursor.col--; + if (conio_cursor.col < 0) { + if (conio_cursor.row != 0) { + conio_cursor.col = CONIO_NUM_COLS - 1; + conio_cursor.row--; + } else { + conio_cursor.col = 0; + } + } + break; + case CONIO_TTY_SERIAL: + scif_write(8); + scif_write(32); + scif_write(8); + break; + case CONIO_TTY_STDIO: + fs_write(conio_serial_fd, (void *)"\x8\x20\x8", 3); + break; + case CONIO_TTY_DBGIO: + dbgio_write_buffer((const uint8_t *)"\x8\x20\x8", 3); + break; + } } /* move the cursor ahead, scroll if we need to */ void conio_advance_cursor(void) { - switch (conio_ttymode) { - case CONIO_TTY_PVR: - conio_cursor.col++; - if (conio_cursor.col >= CONIO_NUM_COLS) { - conio_cursor.col = 0; - conio_cursor.row++; - if (conio_cursor.row >= CONIO_NUM_ROWS) - conio_scroll(); - } - break; - case CONIO_TTY_SERIAL: - scif_write_buffer((const uint8 *)"\x1b[1C", 4, 1); - break; - case CONIO_TTY_STDIO: - fs_write(conio_serial_fd, (void *)"\x1b[1C", 4); - break; - case CONIO_TTY_DBGIO: - dbgio_write_buffer((const uint8 *)"\x1b[1C", 4); - break; - } + switch (conio_ttymode) { + case CONIO_TTY_PVR: + conio_cursor.col++; + if (conio_cursor.col >= CONIO_NUM_COLS) { + conio_cursor.col = 0; + conio_cursor.row++; + if (conio_cursor.row >= CONIO_NUM_ROWS) + conio_scroll(); + } + break; + case CONIO_TTY_SERIAL: + scif_write_buffer((const uint8_t *)"\x1b[1C", 4, 1); + break; + case CONIO_TTY_STDIO: + fs_write(conio_serial_fd, (void *)"\x1b[1C", 4); + break; + case CONIO_TTY_DBGIO: + dbgio_write_buffer((const uint8_t *)"\x1b[1C", 4); + break; + } } /* move the cursor */ void conio_gotoxy(int x, int y) { - switch (conio_ttymode) { - case CONIO_TTY_PVR: - conio_cursor.col = x; - conio_cursor.row = y; - break; - case CONIO_TTY_SERIAL: { - char tmp[256]; - sprintf(tmp, "\x1b[%d;%df", x, y); - scif_write_buffer((unsigned char *)tmp, strlen(tmp), 1); - break; - } - case CONIO_TTY_STDIO: { - char tmp[256]; - sprintf(tmp, "\x1b[%d;%df", x, y); - fs_write(conio_serial_fd, tmp, strlen(tmp)); - break; - } - case CONIO_TTY_DBGIO: { - char tmp[256]; - sprintf(tmp, "\x1b[%d;%df", x, y); - dbgio_write_buffer((unsigned char *)tmp, strlen(tmp)); - break; - } - } + switch (conio_ttymode) { + case CONIO_TTY_PVR: + conio_cursor.col = x; + conio_cursor.row = y; + break; + case CONIO_TTY_SERIAL: { + char tmp[256]; + sprintf(tmp, "\x1b[%d;%df", x, y); + scif_write_buffer((unsigned char *)tmp, strlen(tmp), 1); + break; + } + case CONIO_TTY_STDIO: { + char tmp[256]; + sprintf(tmp, "\x1b[%d;%df", x, y); + fs_write(conio_serial_fd, tmp, strlen(tmp)); + break; + } + case CONIO_TTY_DBGIO: { + char tmp[256]; + sprintf(tmp, "\x1b[%d;%df", x, y); + dbgio_write_buffer((unsigned char *)tmp, strlen(tmp)); + break; + } + } } /* blocking call for a character */ int conio_getch(void) { - int key = -1; - uint8 b; + int key = -1; + uint8_t b; - switch (conio_ttymode) { - case CONIO_TTY_PVR: + switch (conio_ttymode) { + case CONIO_TTY_PVR: #ifdef GFX - while ((key = kbd_get_key()) == -1) { thd_pass(); } + while ((key = kbd_get_key()) == -1) { thd_pass(); } #endif - break; - case CONIO_TTY_SERIAL: { - while ((key = scif_read()) == -1) { thd_pass(); } - - if (key == 3) - arch_exit(); - - break; - } - case CONIO_TTY_STDIO: { - int i; - i = fs_read(conio_serial_fd, &b, 1); - if (i <= 0) - return -1; - key = b; - if (key == '\n') - return conio_getch(); - break; - } - case CONIO_TTY_DBGIO: { - while ((key = dbgio_read()) == -1) { - thd_sleep(1000); - } - - if (key == 3) - arch_exit(); - - break; - } - } - - return key; + break; + case CONIO_TTY_SERIAL: { + while ((key = scif_read()) == -1) { thd_pass(); } + + if (key == 3) + exit(EXIT_SUCCESS); + + break; + } + case CONIO_TTY_STDIO: { + int i; + i = fs_read(conio_serial_fd, &b, 1); + if (i <= 0) + return -1; + key = b; + if (key == '\n') + return conio_getch(); + break; + } + case CONIO_TTY_DBGIO: { + while (dbgio_read_buffer(&b, 1) == -1) { + thd_sleep(1000); + } + + key = b; + + if (key == 3) + exit(EXIT_SUCCESS); + + break; + } + } + + return key; } /* Check to see if a key has been pressed */ int conio_check_getch(void) { - int key = -1; - uint8 b; + int key = -1; + uint8_t b; - switch (conio_ttymode) { - case CONIO_TTY_PVR: + switch (conio_ttymode) { + case CONIO_TTY_PVR: #ifdef GFX - key = kbd_get_key(); + key = kbd_get_key(); #endif - break; - case CONIO_TTY_SERIAL: { - key = scif_read(); - - if (key == 3) - arch_exit(); - - break; - } - case CONIO_TTY_STDIO: - if (fs_total(conio_serial_fd) > 0) { - if (fs_read(conio_serial_fd, &b, 1) == 1) - key = b; - } - if (key == '\n') - key = -1; - break; - case CONIO_TTY_DBGIO: { - key = dbgio_read(); - - if (key == 3) - arch_exit(); - - break; - } - } - - return key; + break; + case CONIO_TTY_SERIAL: { + key = scif_read(); + + if (key == 3) + exit(EXIT_SUCCESS); + + break; + } + case CONIO_TTY_STDIO: + if (fs_total(conio_serial_fd) > 0) { + if (fs_read(conio_serial_fd, &b, 1) == 1) + key = b; + } + if (key == '\n') + key = -1; + break; + case CONIO_TTY_DBGIO: { + if(dbgio_read_buffer(&b, 1) == 1) + key = b; + + if (key == 3) + exit(EXIT_SUCCESS); + + break; + } + } + + return key; } /* set current char to ch, w/o advancing the cursor */ void conio_setch(int ch) { - switch (conio_ttymode) { - case CONIO_TTY_PVR: - switch (ch) { - case '\n': - case '\r': - break; - default: - conio_virtscr[conio_cursor.row][conio_cursor.col] = ch; - } - break; - case CONIO_TTY_SERIAL: - case CONIO_TTY_STDIO: - case CONIO_TTY_DBGIO: - conio_deadvance_cursor(); - conio_putch(ch); - break; - } + switch (conio_ttymode) { + case CONIO_TTY_PVR: + switch (ch) { + case '\n': + case '\r': + break; + default: + conio_virtscr[conio_cursor.row][conio_cursor.col] = ch; + } + break; + case CONIO_TTY_SERIAL: + case CONIO_TTY_STDIO: + case CONIO_TTY_DBGIO: + conio_deadvance_cursor(); + conio_putch(ch); + break; + } } /* put a character at the cursor and move the cursor */ void conio_putch(int ch) { - switch (conio_ttymode) { - case CONIO_TTY_PVR: - switch (ch) { - case '\r': - conio_cursor.col = 0; - break; - case '\n': - conio_cursor.row++; - conio_cursor.col = 0; - if (conio_cursor.row >= CONIO_NUM_ROWS) - conio_scroll(); - break; - default: - conio_virtscr[conio_cursor.row][conio_cursor.col] = ch; - conio_advance_cursor(); - } - break; - case CONIO_TTY_SERIAL: - if (ch == '\n') - scif_write('\r'); - else if (ch == '\r') - break; - scif_write(ch); - break; - case CONIO_TTY_STDIO: - if (ch == '\n') - fs_write(conio_serial_fd, "\r", 1); - else if (ch == '\r') - break; - fs_write(conio_serial_fd, &ch, 1); - break; - case CONIO_TTY_DBGIO: - dbgio_write(ch); - break; - } + switch (conio_ttymode) { + case CONIO_TTY_PVR: + switch (ch) { + case '\r': + conio_cursor.col = 0; + break; + case '\n': + conio_cursor.row++; + conio_cursor.col = 0; + if (conio_cursor.row >= CONIO_NUM_ROWS) + conio_scroll(); + break; + default: + conio_virtscr[conio_cursor.row][conio_cursor.col] = ch; + conio_advance_cursor(); + } + break; + case CONIO_TTY_SERIAL: + if (ch == '\n') + scif_write('\r'); + else if (ch == '\r') + break; + scif_write(ch); + break; + case CONIO_TTY_STDIO: + if (ch == '\n') + fs_write(conio_serial_fd, "\r", 1); + else if (ch == '\r') + break; + fs_write(conio_serial_fd, &ch, 1); + break; + case CONIO_TTY_DBGIO: + dbgio_write_buffer((uint8_t *)&ch, 1); + break; + } } /* put a string of characters */ void conio_putstr(char *str) { - while (*str != '\0') { - conio_putch(*str++); - } + while (*str != '\0') { + conio_putch(*str++); + } } /* a printfish function */ int conio_printf(const char *fmt, ...) { - char buff[512]; /* buffer overflow waiting to happen.... I'll add a vsnprintf func later */ - va_list args; - int i; + char buff[512]; /* buffer overflow waiting to happen.... I'll add a vsnprintf func later */ + va_list args; + int i; - va_start(args, fmt); - i = vsprintf(buff, fmt, args); + va_start(args, fmt); + i = vsprintf(buff, fmt, args); - if (conio_ttymode != CONIO_TTY_NONE) - conio_putstr(buff); + if (conio_ttymode != CONIO_TTY_NONE) + conio_putstr(buff); - va_end(args); + va_end(args); - return i; + return i; } /* clear the screen */ void conio_clear(void) { - int row, col; - - switch (conio_ttymode) { - case CONIO_TTY_PVR: - /* fill screen with spaces */ - for (row = 0; row < CONIO_NUM_ROWS; row++) - for (col = 0; col < CONIO_NUM_COLS; col++) - conio_virtscr[row][col] = ' '; - break; - case CONIO_TTY_SERIAL: - scif_write_buffer((uint8 *)"\x1b[2J", 4, 1); - break; - case CONIO_TTY_STDIO: - fs_write(conio_serial_fd, (void *)"\x1b[2J", 4); - break; - case CONIO_TTY_DBGIO: - dbgio_write_buffer((uint8 *)"\x1b[2J", 4); - break; - } + int row, col; + + switch (conio_ttymode) { + case CONIO_TTY_PVR: + /* fill screen with spaces */ + for (row = 0; row < CONIO_NUM_ROWS; row++) + for (col = 0; col < CONIO_NUM_COLS; col++) + conio_virtscr[row][col] = ' '; + break; + case CONIO_TTY_SERIAL: + scif_write_buffer((uint8_t *)"\x1b[2J", 4, 1); + break; + case CONIO_TTY_STDIO: + fs_write(conio_serial_fd, (void *)"\x1b[2J", 4); + break; + case CONIO_TTY_DBGIO: + dbgio_write_buffer((uint8_t *)"\x1b[2J", 4); + break; + } } /* conio freeze (for sub-process taking over TA) */ void conio_freeze(void) { - sem_wait(&ft_mutex); + sem_wait(&ft_mutex); } /* conio thaw */ void conio_thaw(void) { - sem_signal(&ft_mutex); + sem_signal(&ft_mutex); } /* set theme */ void conio_set_theme(int theme) { - assert( theme >= CONIO_THEME_PLAIN && theme <= CONIO_THEME_C64 ); - conio_theme = theme; + assert( theme >= CONIO_THEME_PLAIN && theme <= CONIO_THEME_C64 ); + conio_theme = theme; } /* ptr to old printf */ @@ -356,88 +357,88 @@ static volatile int conio_exit = 0; static void *conio_thread(void *param) { (void)param; - conio_entered = 1; - while (!conio_exit) { - sem_wait(&ft_mutex); - conio_input_frame(); - if (conio_ttymode == CONIO_TTY_PVR) { + conio_entered = 1; + while (!conio_exit) { + sem_wait(&ft_mutex); + conio_input_frame(); + if (conio_ttymode == CONIO_TTY_PVR) { #ifdef GFX - conio_draw_frame(); + conio_draw_frame(); #endif - } else { - if (conio_ttymode == CONIO_TTY_SERIAL) - scif_flush(); - thd_sleep(1000/60); /* Simulate frame delay */ - } - sem_signal(&ft_mutex); - } - conio_exit = -1; - return NULL; + } else { + if (conio_ttymode == CONIO_TTY_SERIAL) + scif_flush(); + thd_sleep(1000/60); /* Simulate frame delay */ + } + sem_signal(&ft_mutex); + } + conio_exit = -1; + return NULL; } /* initialize the console I/O stuffs */ int conio_init(int ttymode, int inputmode) { - conio_ttymode = ttymode; - conio_inputmode = inputmode; + conio_ttymode = ttymode; + conio_inputmode = inputmode; - switch (conio_ttymode) { - case CONIO_TTY_PVR: + switch (conio_ttymode) { + case CONIO_TTY_PVR: #ifdef GFX - conio_draw_init(); + conio_draw_init(); #endif - break; - case CONIO_TTY_STDIO: - break; - case CONIO_TTY_SERIAL: - conio_serial_fd = 1; - break; - case CONIO_TTY_DBGIO: - break; - default: - assert_msg( 0, "Unknown CONIO TTY mode" ); - conio_ttymode = CONIO_TTY_PVR; - break; - } - assert_msg( conio_inputmode == CONIO_INPUT_LINE, "Non-Line input modes not supported yet" ); - - conio_input_init(); - if (conio_ttymode == CONIO_TTY_PVR) { - conio_clear(); - conio_gotoxy(0, 0); - } - - sem_init(&ft_mutex, 1); - - /* create the conio thread */ - conio_exit = 0; - if (!thd_create(1, conio_thread, 0)) - return -1; - - /* Wait for it to actually start */ - while (!conio_entered) - thd_pass(); - - return 0; + break; + case CONIO_TTY_STDIO: + break; + case CONIO_TTY_SERIAL: + conio_serial_fd = 1; + break; + case CONIO_TTY_DBGIO: + break; + default: + assert_msg( 0, "Unknown CONIO TTY mode" ); + conio_ttymode = CONIO_TTY_PVR; + break; + } + assert_msg( conio_inputmode == CONIO_INPUT_LINE, "Non-Line input modes not supported yet" ); + + conio_input_init(); + if (conio_ttymode == CONIO_TTY_PVR) { + conio_clear(); + conio_gotoxy(0, 0); + } + + sem_init(&ft_mutex, 1); + + /* create the conio thread */ + conio_exit = 0; + if (!thd_create(1, conio_thread, 0)) + return -1; + + /* Wait for it to actually start */ + while (!conio_entered) + thd_pass(); + + return 0; } int conio_shutdown(void) { - /* shutup our thread */ - conio_exit = 1; + /* shutup our thread */ + conio_exit = 1; - while (conio_exit != -1) - ; + while (conio_exit != -1) + ; - /* Delete the sempahore */ - sem_destroy(&ft_mutex); + /* Delete the sempahore */ + sem_destroy(&ft_mutex); #ifdef GFX - if (conio_ttymode == CONIO_TTY_PVR) - conio_draw_shutdown(); + if (conio_ttymode == CONIO_TTY_PVR) + conio_draw_shutdown(); #endif - conio_input_shutdown(); + conio_input_shutdown(); - conio_ttymode = CONIO_TTY_NONE; - conio_inputmode = CONIO_INPUT_NONE; + conio_ttymode = CONIO_TTY_NONE; + conio_inputmode = CONIO_INPUT_NONE; - return 0; + return 0; } diff --git a/draw.c b/draw.c index 7d96e4f..53e9962 100644 --- a/draw.c +++ b/draw.c @@ -17,185 +17,185 @@ static pvr_ptr_t font_texture = NULL; /* initialize draw stuff: get our texture of the font, etc */ void conio_draw_init(void) { - uint16 *vram; - int x, y; - - font_texture = pvr_mem_malloc(256*256*2); - vram = (uint16 *)font_texture; - - for (y = 0; y < 8; y++) { - for (x = 0; x < 16; x++) { - bfont_draw(vram, 256, 0, y*16 + x); - vram += 16; - } - vram += 23*256; - } + uint16_t *vram; + int x, y; + + font_texture = pvr_mem_malloc(256*256*2); + vram = (uint16_t *)font_texture; + + for (y = 0; y < 8; y++) { + for (x = 0; x < 16; x++) { + bfont_draw(vram, 256, 0, y*16 + x); + vram += 16; + } + vram += 23*256; + } } void conio_draw_shutdown(void) { - assert( font_texture != NULL ); - pvr_mem_free(font_texture); + assert( font_texture != NULL ); + pvr_mem_free(font_texture); } /* Draw one font character (6x12) */ static void draw_char(float x1, float y1, float z1, float a, float r, float g, float b, int c) { - pvr_vertex_t vert; - int ix, iy; - float u1, v1, u2, v2; - - if (c == ' ') - return; - - // assert( c > ' ' && c < 127 ); - if (!( c > ' ' && c < 127 )) - return; - - ix = (c % 16) * 16; - iy = (c / 16) * 24; - u1 = ix * 1.0f / 256.0f; - v1 = iy * 1.0f / 256.0f; - u2 = (ix+12) * 1.0f / 256.0f; - v2 = (iy+24) * 1.0f / 256.0f; - - vert.flags = PVR_CMD_VERTEX; - vert.x = x1; - vert.y = y1 + CONIO_FONT_HEIGHT; - vert.z = z1; - vert.u = u1; - vert.v = v2; - vert.argb = PVR_PACK_COLOR(a, r, g, b); - vert.oargb = 0; - pvr_prim(&vert, sizeof(vert)); - - vert.x = x1; - vert.y = y1; - vert.u = u1; - vert.v = v1; - pvr_prim(&vert, sizeof(vert)); - - vert.x = x1 + CONIO_FONT_WIDTH; - vert.y = y1 + CONIO_FONT_HEIGHT; - vert.u = u2; - vert.v = v2; - pvr_prim(&vert, sizeof(vert)); - - vert.flags = PVR_CMD_VERTEX_EOL; - vert.x = x1 + CONIO_FONT_WIDTH; - vert.y = y1; - vert.u = u2; - vert.v = v1; - pvr_prim(&vert, sizeof(vert)); + pvr_vertex_t vert; + int ix, iy; + float u1, v1, u2, v2; + + if (c == ' ') + return; + + // assert( c > ' ' && c < 127 ); + if (!( c > ' ' && c < 127 )) + return; + + ix = (c % 16) * 16; + iy = (c / 16) * 24; + u1 = ix * 1.0f / 256.0f; + v1 = iy * 1.0f / 256.0f; + u2 = (ix+12) * 1.0f / 256.0f; + v2 = (iy+24) * 1.0f / 256.0f; + + vert.flags = PVR_CMD_VERTEX; + vert.x = x1; + vert.y = y1 + CONIO_FONT_HEIGHT; + vert.z = z1; + vert.u = u1; + vert.v = v2; + vert.argb = PVR_PACK_COLOR(a, r, g, b); + vert.oargb = 0; + pvr_prim(&vert, sizeof(vert)); + + vert.x = x1; + vert.y = y1; + vert.u = u1; + vert.v = v1; + pvr_prim(&vert, sizeof(vert)); + + vert.x = x1 + CONIO_FONT_WIDTH; + vert.y = y1 + CONIO_FONT_HEIGHT; + vert.u = u2; + vert.v = v2; + pvr_prim(&vert, sizeof(vert)); + + vert.flags = PVR_CMD_VERTEX_EOL; + vert.x = x1 + CONIO_FONT_WIDTH; + vert.y = y1; + vert.u = u2; + vert.v = v1; + pvr_prim(&vert, sizeof(vert)); } /* draw len chars at string */ static void draw_string(float x, float y, float z, float a, float r, float g, - float b, char *str, int len) { - int i; - pvr_poly_cxt_t cxt; - pvr_poly_hdr_t poly; - - pvr_poly_cxt_txr(&cxt, PVR_LIST_TR_POLY, PVR_TXRFMT_ARGB1555 | PVR_TXRFMT_NONTWIDDLED, - 256, 256, font_texture, PVR_FILTER_NONE); - pvr_poly_compile(&poly, &cxt); - pvr_prim(&poly, sizeof(poly)); - - for (i = 0; i < len; i++) { - draw_char(x, y, z, a, r, g, b, str[i]); - x += CONIO_FONT_WIDTH; - } + float b, char *str, int len) { + int i; + pvr_poly_cxt_t cxt; + pvr_poly_hdr_t poly; + + pvr_poly_cxt_txr(&cxt, PVR_LIST_TR_POLY, PVR_TXRFMT_ARGB1555 | PVR_TXRFMT_NONTWIDDLED, + 256, 256, font_texture, PVR_FILTER_NONE); + pvr_poly_compile(&poly, &cxt); + pvr_prim(&poly, sizeof(poly)); + + for (i = 0; i < len; i++) { + draw_char(x, y, z, a, r, g, b, str[i]); + x += CONIO_FONT_WIDTH; + } } /* draw a box (used by cursor and border, etc) (at 1.0f z coord) */ static void draw_box(float x, float y, float w, float h, float a, float r, float g, float b) { - pvr_poly_cxt_t cxt; - pvr_poly_hdr_t poly; - pvr_vertex_t vert; - - pvr_poly_cxt_col(&cxt, PVR_LIST_TR_POLY); - pvr_poly_compile(&poly, &cxt); - pvr_prim(&poly, sizeof(poly)); - - vert.flags = PVR_CMD_VERTEX; - vert.x = x; - vert.y = y + h; - vert.z = 1.0f; - vert.u = vert.v = 0.0f; - vert.argb = PVR_PACK_COLOR(a, r, g, b); - vert.oargb = 0; - pvr_prim(&vert, sizeof(vert)); - - vert.y -= h; - pvr_prim(&vert, sizeof(vert)); - - vert.y += h; - vert.x += w; - pvr_prim(&vert, sizeof(vert)); - - vert.flags = PVR_CMD_VERTEX_EOL; - vert.y -= h; - pvr_prim(&vert, sizeof(vert)); + pvr_poly_cxt_t cxt; + pvr_poly_hdr_t poly; + pvr_vertex_t vert; + + pvr_poly_cxt_col(&cxt, PVR_LIST_TR_POLY); + pvr_poly_compile(&poly, &cxt); + pvr_prim(&poly, sizeof(poly)); + + vert.flags = PVR_CMD_VERTEX; + vert.x = x; + vert.y = y + h; + vert.z = 1.0f; + vert.u = vert.v = 0.0f; + vert.argb = PVR_PACK_COLOR(a, r, g, b); + vert.oargb = 0; + pvr_prim(&vert, sizeof(vert)); + + vert.y -= h; + pvr_prim(&vert, sizeof(vert)); + + vert.y += h; + vert.x += w; + pvr_prim(&vert, sizeof(vert)); + + vert.flags = PVR_CMD_VERTEX_EOL; + vert.y -= h; + pvr_prim(&vert, sizeof(vert)); } /* draw the border around the screen etc */ static void draw_border(float r, float g, float b) { - draw_box(0, 0, CONIO_SCREEN_FIRSTCOL - 1, CONIO_SCREEN_HEIGHT, 1.0f, r, g, b); - draw_box(CONIO_SCREEN_LASTCOL, 0, CONIO_SCREEN_WIDTH - CONIO_SCREEN_LASTCOL, CONIO_SCREEN_HEIGHT, 1.0f, r, g, b); - draw_box(0, 0, CONIO_SCREEN_WIDTH, CONIO_FONT_HEIGHT, 1.0f, r, g, b); - draw_box(0, CONIO_SCREEN_HEIGHT - CONIO_FONT_HEIGHT, CONIO_SCREEN_WIDTH, CONIO_FONT_HEIGHT, 1.0f, r, g, b); + draw_box(0, 0, CONIO_SCREEN_FIRSTCOL - 1, CONIO_SCREEN_HEIGHT, 1.0f, r, g, b); + draw_box(CONIO_SCREEN_LASTCOL, 0, CONIO_SCREEN_WIDTH - CONIO_SCREEN_LASTCOL, CONIO_SCREEN_HEIGHT, 1.0f, r, g, b); + draw_box(0, 0, CONIO_SCREEN_WIDTH, CONIO_FONT_HEIGHT, 1.0f, r, g, b); + draw_box(0, CONIO_SCREEN_HEIGHT - CONIO_FONT_HEIGHT, CONIO_SCREEN_WIDTH, CONIO_FONT_HEIGHT, 1.0f, r, g, b); } static void draw_middle(float r, float g, float b) { - draw_box(CONIO_SCREEN_FIRSTCOL, CONIO_FONT_HEIGHT, CONIO_FONT_WIDTH * CONIO_NUM_COLS, CONIO_FONT_HEIGHT * CONIO_NUM_ROWS, 1.0f, r, g, b); + draw_box(CONIO_SCREEN_FIRSTCOL, CONIO_FONT_HEIGHT, CONIO_FONT_WIDTH * CONIO_NUM_COLS, CONIO_FONT_HEIGHT * CONIO_NUM_ROWS, 1.0f, r, g, b); } /* draw the virtual screen */ static void draw_virtscr(float a, float r, float g, float b) { - int i; + int i; - for (i = 0; i < CONIO_NUM_ROWS; i++) { - draw_string(CONIO_SCREEN_FIRSTCOL, (i + 1) * CONIO_FONT_HEIGHT, 1.0f, a, r, g, b, - conio_virtscr[i], CONIO_NUM_COLS); - } + for (i = 0; i < CONIO_NUM_ROWS; i++) { + draw_string(CONIO_SCREEN_FIRSTCOL, (i + 1) * CONIO_FONT_HEIGHT, 1.0f, a, r, g, b, + conio_virtscr[i], CONIO_NUM_COLS); + } } /* draw the cursor */ static float c_alpha = 1.0f, c_dalpha = -0.1f; static void draw_cursor(float r, float g, float b) { - /* modify our alpha to give a little fading effect */ - c_alpha += c_dalpha; - if (c_alpha >= 1.0f || c_alpha <= 0.0f) - c_dalpha = -c_dalpha; - - if (c_alpha > 0.5f) { - draw_box(conio_cursor.col * CONIO_FONT_WIDTH + CONIO_SCREEN_FIRSTCOL, (conio_cursor.row + 1) * CONIO_FONT_HEIGHT, - CONIO_FONT_WIDTH, CONIO_FONT_HEIGHT, - 1.0f, r, g, b); - } + /* modify our alpha to give a little fading effect */ + c_alpha += c_dalpha; + if (c_alpha >= 1.0f || c_alpha <= 0.0f) + c_dalpha = -c_dalpha; + + if (c_alpha > 0.5f) { + draw_box(conio_cursor.col * CONIO_FONT_WIDTH + CONIO_SCREEN_FIRSTCOL, (conio_cursor.row + 1) * CONIO_FONT_HEIGHT, + CONIO_FONT_WIDTH, CONIO_FONT_HEIGHT, + 1.0f, r, g, b); + } } /* our exported drawing function: does a full redraw of everything */ void conio_draw_frame(void) { - pvr_wait_ready(); - pvr_scene_begin(); - pvr_list_begin(PVR_LIST_TR_POLY); - switch (conio_theme) { - case CONIO_THEME_C64: - draw_border(0.4f, 0.6f, 0.98f); - draw_middle(0.0f, 0.0f, 1.0f); - draw_virtscr(1.0f, 1.0f, 1.0f, 1.0f); - draw_cursor(0.4f, 0.6f, 0.98f); - break; - case CONIO_THEME_MATRIX: - draw_virtscr(1.0f, 0.0f, 1.0f, 0.0f); - draw_cursor(0.0f, 0.7f, 0.0f); - break; - case CONIO_THEME_PLAIN: - default: - draw_virtscr(1.0f, 1.0f, 1.0f, 1.0f); - draw_cursor(1.0f, 1.0f, 1.0f); - break; - } - pvr_list_finish(); - pvr_scene_finish(); + pvr_wait_ready(); + pvr_scene_begin(); + pvr_list_begin(PVR_LIST_TR_POLY); + switch (conio_theme) { + case CONIO_THEME_C64: + draw_border(0.4f, 0.6f, 0.98f); + draw_middle(0.0f, 0.0f, 1.0f); + draw_virtscr(1.0f, 1.0f, 1.0f, 1.0f); + draw_cursor(0.4f, 0.6f, 0.98f); + break; + case CONIO_THEME_MATRIX: + draw_virtscr(1.0f, 0.0f, 1.0f, 0.0f); + draw_cursor(0.0f, 0.7f, 0.0f); + break; + case CONIO_THEME_PLAIN: + default: + draw_virtscr(1.0f, 1.0f, 1.0f, 1.0f); + draw_cursor(1.0f, 1.0f, 1.0f); + break; + } + pvr_list_finish(); + pvr_scene_finish(); } diff --git a/include/conio.h b/include/conio.h index 147c829..4b2e5ad 100644 --- a/include/conio.h +++ b/include/conio.h @@ -2,7 +2,7 @@ conio.h - (c)2002 Dan Potter + (c)2002 Megan Potter Adapted from Kosh, (c)2000 Jordan DeLong */ @@ -14,8 +14,8 @@ __BEGIN_DECLS /* some defines */ -#define CONIO_NUM_ROWS 18 -#define CONIO_NUM_COLS 48 +#define CONIO_NUM_ROWS 18 +#define CONIO_NUM_COLS 48 /* our cursor */ typedef struct { int row, col; } conio_cursor_t; @@ -53,23 +53,23 @@ void conio_set_theme(int theme); - MATRIX is green text on a black background - C64 is cyan text on a blue background, with a cyan border */ -#define CONIO_THEME_PLAIN 0 -#define CONIO_THEME_MATRIX 1 -#define CONIO_THEME_C64 2 +#define CONIO_THEME_PLAIN 0 +#define CONIO_THEME_MATRIX 1 +#define CONIO_THEME_C64 2 /* Init modes: - TTY can be serial, PVR console, or stdio (pty) - Input mode can be NONE, LINE, CHAR The only valid combination for now is TTY_PVR and INPUT_LINE */ -#define CONIO_TTY_NONE 0 -#define CONIO_TTY_PVR 1 -#define CONIO_TTY_SERIAL 2 -#define CONIO_TTY_STDIO 3 -#define CONIO_TTY_DBGIO 4 -#define CONIO_INPUT_NONE 0 -#define CONIO_INPUT_LINE 1 -#define CONIO_INPUT_CHAR 2 +#define CONIO_TTY_NONE 0 +#define CONIO_TTY_PVR 1 +#define CONIO_TTY_SERIAL 2 +#define CONIO_TTY_STDIO 3 +#define CONIO_TTY_DBGIO 4 +#define CONIO_INPUT_NONE 0 +#define CONIO_INPUT_LINE 1 +#define CONIO_INPUT_CHAR 2 extern int conio_ttymode, conio_inputmode, conio_theme; diff --git a/include/draw.h b/include/draw.h index e327d7e..2ba7c9c 100644 --- a/include/draw.h +++ b/include/draw.h @@ -2,7 +2,7 @@ draw.h - (c)2002 Dan Potter + (c)2002 Megan Potter Adapted from Kosh, (c)2000 Jordan DeLong */ @@ -11,12 +11,12 @@ #define __CONIO_DRAW_H /* font size constants */ -#define CONIO_FONT_WIDTH 12 -#define CONIO_FONT_HEIGHT 24 +#define CONIO_FONT_WIDTH 12 +#define CONIO_FONT_HEIGHT 24 /* screen size constants */ -#define CONIO_SCREEN_WIDTH 640 -#define CONIO_SCREEN_HEIGHT 480 +#define CONIO_SCREEN_WIDTH 640 +#define CONIO_SCREEN_HEIGHT 480 /* more constants */ #define CONIO_SCREEN_FIRSTCOL ( (CONIO_SCREEN_WIDTH - CONIO_FONT_WIDTH * CONIO_NUM_COLS) / 2 ) diff --git a/include/input.h b/include/input.h index fdc57d3..726bc64 100644 --- a/include/input.h +++ b/include/input.h @@ -2,7 +2,7 @@ input.h - (c)2002 Dan Potter + (c)2002 Megan Potter Adapted from Kosh, (c)2000 Jordan DeLong */ @@ -11,7 +11,7 @@ #define __CONIO_INPUT_H /* size of the input buffer */ -#define CONIO_INPUT_BUFFSIZE 256 +#define CONIO_INPUT_BUFFSIZE 256 /* functions */ void conio_input_frame(void); diff --git a/input.c b/input.c index 7602105..2860749 100644 --- a/input.c +++ b/input.c @@ -21,15 +21,15 @@ /* the buffer for input */ static struct { - char text[CONIO_INPUT_BUFFSIZE]; - unsigned int pos; /* pos in the buffer, not the screen */ + char text[CONIO_INPUT_BUFFSIZE]; + unsigned int pos; /* pos in the buffer, not the screen */ } input_buffer; /* the state var */ enum { - INPUT_PROMPT, /* print the prompt, etc */ - INPUT_READCOMM, /* read and edit a command line */ - INPUT_COMMAND /* call a command based on input_buffer.text */ + INPUT_PROMPT, /* print the prompt, etc */ + INPUT_READCOMM, /* read and edit a command line */ + INPUT_COMMAND /* call a command based on input_buffer.text */ } input_state = INPUT_PROMPT; /***********************************************************************/ @@ -37,234 +37,234 @@ enum { and return them using a semaphore for flow control. */ typedef struct cb_sem_data_str { - char line[256]; - struct cb_sem_data_str *next; + char line[256]; + struct cb_sem_data_str *next; } cb_sem_data_t; -static volatile cb_sem_data_t *cb_queue; -static semaphore_t cb_sem, cb_mutex; -static volatile int cb_dead; +static volatile cb_sem_data_t *cb_queue; +static semaphore_t cb_sem, cb_mutex; +static volatile int cb_dead; static void input_cb_init(void) { - sem_init(&cb_sem, 0); - sem_init(&cb_mutex, 1); - cb_queue = NULL; - cb_dead = 0; + sem_init(&cb_sem, 0); + sem_init(&cb_mutex, 1); + cb_queue = NULL; + cb_dead = 0; } static void cb_default(const char *str) { - cb_sem_data_t *t; + cb_sem_data_t *t; - t = malloc(sizeof(cb_sem_data_t)); - sem_wait(&cb_mutex); - strncpy(t->line, str, 255); t->line[255] = '\0'; - t->next = (cb_sem_data_t *)cb_queue; - cb_queue = t; - sem_signal(&cb_mutex); + t = malloc(sizeof(cb_sem_data_t)); + sem_wait(&cb_mutex); + strncpy(t->line, str, 255); t->line[255] = '\0'; + t->next = (cb_sem_data_t *)cb_queue; + cb_queue = t; + sem_signal(&cb_mutex); - sem_signal(&cb_sem); + sem_signal(&cb_sem); } static void input_cb_shutdown(void) { - cb_sem_data_t *t, *n; - int i; + cb_sem_data_t *t, *n; + int i; - /* Make sure any clients waiting on getline go through */ - cb_dead = 1; - cb_default(""); + /* Make sure any clients waiting on getline go through */ + cb_dead = 1; + cb_default(""); - /* This is pretty damned hacky but it should do the job */ - for (i=0; i<5; i++) - thd_pass(); + /* This is pretty damned hacky but it should do the job */ + for (i=0; i<5; i++) + thd_pass(); - sem_destroy(&cb_mutex); - sem_destroy(&cb_sem); + sem_destroy(&cb_mutex); + sem_destroy(&cb_sem); - for (t=(cb_sem_data_t *)cb_queue; t; t = n) { - n = t->next; - free(t); - } - cb_queue = NULL; + for (t=(cb_sem_data_t *)cb_queue; t; t = n) { + n = t->next; + free(t); + } + cb_queue = NULL; } int conio_input_getline(int block, char *dst, int dstcnt) { - cb_sem_data_t *t, *l; - - /* assert_msg( block != 0, "non-blocking I/O not supported yet" ); */ - if (!block && cb_queue == NULL) - return -1; - - /* Did we quit already? */ - if (cb_dead) - return -1; - - /* Wait for some input to be ready */ - if (block > 0) { - if (sem_wait_timed(&cb_sem, block) < 0) - return -1; - } else { - if (sem_wait(&cb_sem) < 0) - return -1; - } - - /* Grab the mutex and retrieve the line */ - sem_wait(&cb_mutex); - assert( cb_queue != NULL ); - for (l=NULL, t=(cb_sem_data_t *)cb_queue; t->next; t=t->next) - l=t; - assert( t->next == NULL ); - if (l != NULL) { - assert( l->next == t ); - l->next = NULL; - } else { - cb_queue = NULL; - } - sem_signal(&cb_mutex); - - strncpy(dst, t->line, dstcnt-1); - dst[dstcnt-1] = '\0'; - free(t); - - return 0; + cb_sem_data_t *t, *l; + + /* assert_msg( block != 0, "non-blocking I/O not supported yet" ); */ + if (!block && cb_queue == NULL) + return -1; + + /* Did we quit already? */ + if (cb_dead) + return -1; + + /* Wait for some input to be ready */ + if (block > 0) { + if (sem_wait_timed(&cb_sem, block) < 0) + return -1; + } else { + if (sem_wait(&cb_sem) < 0) + return -1; + } + + /* Grab the mutex and retrieve the line */ + sem_wait(&cb_mutex); + assert( cb_queue != NULL ); + for (l=NULL, t=(cb_sem_data_t *)cb_queue; t->next; t=t->next) + l=t; + assert( t->next == NULL ); + if (l != NULL) { + assert( l->next == t ); + l->next = NULL; + } else { + cb_queue = NULL; + } + sem_signal(&cb_mutex); + + strncpy(dst, t->line, dstcnt-1); + dst[dstcnt-1] = '\0'; + free(t); + + return 0; } /***********************************************************************/ /* Input callbacks */ static conio_input_callback_t input_cb = NULL; void conio_input_callback(conio_input_callback_t cb) { - if (cb == NULL) - input_cb = cb_default; - else - input_cb = cb; + if (cb == NULL) + input_cb = cb_default; + else + input_cb = cb; } /***********************************************************************/ /* add text to the input buff and putch it */ static void input_insertbuff(int ch) { - int len; - - len = strlen(input_buffer.text); - - if (len >= CONIO_INPUT_BUFFSIZE - 1) - return; - - /* our str */ - memmove(&input_buffer.text[input_buffer.pos + 1], &input_buffer.text[input_buffer.pos], - len - input_buffer.pos + 1); - /* the virtscr */ - if (conio_cursor.row * CONIO_NUM_COLS + conio_cursor.col + len - input_buffer.pos + 1 - >= CONIO_NUM_COLS * CONIO_NUM_ROWS) - conio_scroll(); - memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col + 1], - &conio_virtscr[conio_cursor.row][conio_cursor.col], - len - input_buffer.pos + 1); - input_buffer.text[input_buffer.pos] = ch; - input_buffer.pos++; - - if (conio_ttymode != CONIO_TTY_STDIO) - conio_putch(ch); - - return; + int len; + + len = strlen(input_buffer.text); + + if (len >= CONIO_INPUT_BUFFSIZE - 1) + return; + + /* our str */ + memmove(&input_buffer.text[input_buffer.pos + 1], &input_buffer.text[input_buffer.pos], + len - input_buffer.pos + 1); + /* the virtscr */ + if (conio_cursor.row * CONIO_NUM_COLS + conio_cursor.col + len - input_buffer.pos + 1 + >= CONIO_NUM_COLS * CONIO_NUM_ROWS) + conio_scroll(); + memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col + 1], + &conio_virtscr[conio_cursor.row][conio_cursor.col], + len - input_buffer.pos + 1); + input_buffer.text[input_buffer.pos] = ch; + input_buffer.pos++; + + if (conio_ttymode != CONIO_TTY_STDIO) + conio_putch(ch); + + return; } /* remove the char at input_buffer.pos from the buffer, and reflect the changes on the virtscr */ static void input_delchar_buff(void) { - int len; + int len; - len = strlen(input_buffer.text); + len = strlen(input_buffer.text); - if (len < 1) - return; + if (len < 1) + return; - /* our str */ - memmove(&input_buffer.text[input_buffer.pos], &input_buffer.text[input_buffer.pos + 1], - len - input_buffer.pos + 1); - input_buffer.text[len] = '\0'; - /* the virtscr */ - memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col], - &conio_virtscr[conio_cursor.row][conio_cursor.col + 1], - len - input_buffer.pos + 1); + /* our str */ + memmove(&input_buffer.text[input_buffer.pos], &input_buffer.text[input_buffer.pos + 1], + len - input_buffer.pos + 1); + input_buffer.text[len] = '\0'; + /* the virtscr */ + memmove(&conio_virtscr[conio_cursor.row][conio_cursor.col], + &conio_virtscr[conio_cursor.row][conio_cursor.col + 1], + len - input_buffer.pos + 1); } /* print the prompt out, clear input buffer */ static void input_prompt(void) { - input_buffer.pos = 0; - *input_buffer.text = '\0'; - /* conio_putstr("> "); */ - input_state = INPUT_READCOMM; + input_buffer.pos = 0; + *input_buffer.text = '\0'; + /* conio_putstr("> "); */ + input_state = INPUT_READCOMM; } /* reading commands from the prompt */ static void input_readcomm(void) { - int key; - - key = conio_check_getch(); - - switch (key) { - case -1: - return; - case KBD_KEY_DEL << 8: - input_delchar_buff(); - break; - case 8: /* backspace */ - if (input_buffer.pos > 0) { - conio_deadvance_cursor(); - input_buffer.pos--; - input_delchar_buff(); - } - break; - case '\r': - input_state = INPUT_COMMAND; - conio_putch('\n'); - break; - case KBD_KEY_LEFT << 8: /* left */ - if (input_buffer.pos > 0) { - input_buffer.pos--; - conio_deadvance_cursor(); - } - break; - case KBD_KEY_RIGHT << 8: - if (input_buffer.pos < strlen(input_buffer.text)) { - input_buffer.pos++; - conio_advance_cursor(); - } - break; - case KBD_KEY_HOME << 8: - for (; input_buffer.pos > 0; input_buffer.pos--) - conio_deadvance_cursor(); - break; - case KBD_KEY_END << 8: - for (; input_buffer.pos < strlen(input_buffer.text); input_buffer.pos++) - conio_advance_cursor(); - break; - default: - if (isascii(key)) - input_insertbuff(key); - } + int key; + + key = conio_check_getch(); + + switch (key) { + case -1: + return; + case KBD_KEY_DEL << 8: + input_delchar_buff(); + break; + case 8: /* backspace */ + if (input_buffer.pos > 0) { + conio_deadvance_cursor(); + input_buffer.pos--; + input_delchar_buff(); + } + break; + case '\r': + input_state = INPUT_COMMAND; + conio_putch('\n'); + break; + case KBD_KEY_LEFT << 8: /* left */ + if (input_buffer.pos > 0) { + input_buffer.pos--; + conio_deadvance_cursor(); + } + break; + case KBD_KEY_RIGHT << 8: + if (input_buffer.pos < strlen(input_buffer.text)) { + input_buffer.pos++; + conio_advance_cursor(); + } + break; + case KBD_KEY_HOME << 8: + for (; input_buffer.pos > 0; input_buffer.pos--) + conio_deadvance_cursor(); + break; + case KBD_KEY_END << 8: + for (; input_buffer.pos < strlen(input_buffer.text); input_buffer.pos++) + conio_advance_cursor(); + break; + default: + if (isascii(key)) + input_insertbuff(key); + } } /* read command line, try to execute builtin commands, otherwise externals (this coming soon) */ static void input_command(void) { - input_state = INPUT_PROMPT; + input_state = INPUT_PROMPT; - if (input_cb) - input_cb(input_buffer.text); + if (input_cb) + input_cb(input_buffer.text); } void conio_input_init(void) { - input_cb_init(); - input_cb = cb_default; + input_cb_init(); + input_cb = cb_default; } void conio_input_shutdown(void) { - input_cb = NULL; - input_cb_shutdown(); + input_cb = NULL; + input_cb_shutdown(); } /* our exported input function, called once per frame. */ void conio_input_frame(void) { - switch (input_state) { - case INPUT_PROMPT: input_prompt(); break; - case INPUT_READCOMM: input_readcomm(); break; - case INPUT_COMMAND: input_command(); break; - } + switch (input_state) { + case INPUT_PROMPT: input_prompt(); break; + case INPUT_READCOMM: input_readcomm(); break; + case INPUT_COMMAND: input_command(); break; + } }