reworded tart and now it works with utf8 and is enabled by defalt
This commit is contained in:
@@ -5,8 +5,6 @@ set( CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
|
||||
set(LIB_SOURCES
|
||||
term.c
|
||||
term.h
|
||||
tart.c
|
||||
)
|
||||
|
||||
|
||||
410
source/tart.c
410
source/tart.c
@@ -1,255 +1,255 @@
|
||||
#include "../includes/tart.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "term.h"
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
|
||||
struct tart_cell tart_test() {
|
||||
#ifdef TART_RGB_COLORS
|
||||
return (struct tart_cell){{0,0,0}, {0,0,0}, 0, '&',0};
|
||||
|
||||
#define BUFFER_AT_POS(W) (W->windowSize.x * W->buffer.curser.y) + W->buffer.curser.x
|
||||
#define BUFFER_LINE_SIZE (w->windowSize.x) * TART_CELL_SIZE + sizeof(tart_cell)
|
||||
#define BUFFER_SIZE (w->windowSize.x * w->windowSize.y)
|
||||
#define BUFFER_BUFFER_SIZE BUFFER_LINE_SIZE * w->windowSize.y
|
||||
|
||||
tart_vec2 GetTerminalSize(tart_window* w) {
|
||||
struct winsize win;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
|
||||
return (tart_vec2){win.ws_col,win.ws_row};
|
||||
};
|
||||
|
||||
int tart_create_buffer(tart_window* w) {
|
||||
w->buffer.buffer = (tart_cell**)malloc(w->windowSize.y * sizeof(tart_cell*));
|
||||
if(w->buffer.buffer == NULL) {
|
||||
return TART_NG;
|
||||
}
|
||||
for(int i = 0; i < w->windowSize.y; i++) {
|
||||
|
||||
int x = BUFFER_LINE_SIZE;
|
||||
w->buffer.buffer[i] = (tart_cell*)malloc(x);
|
||||
bzero(w->buffer.buffer[i], (BUFFER_LINE_SIZE));
|
||||
}
|
||||
tart_cell ce;
|
||||
bzero(ce, TART_CELL_SIZE);
|
||||
snprintf(ce,TART_CELL_SIZE,
|
||||
#ifdef TART_UTF8
|
||||
"\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m",
|
||||
#else
|
||||
return (struct tart_cell){196,105,0,'&'};
|
||||
"\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m",
|
||||
#endif
|
||||
}
|
||||
struct tart_window tart_create_window() {
|
||||
struct tart_window window;
|
||||
window.buffer_count = 0;
|
||||
for(int i = 0; i < 0xFF; i++) {
|
||||
window.buffers[i] = tart_create_buffer(0, (struct tart_vec2){0,0},
|
||||
(struct tart_vec2){0,0});
|
||||
}
|
||||
window.size = term_current_size();
|
||||
w->pallet[0].forground.r,
|
||||
w->pallet[0].forground.g,
|
||||
w->pallet[0].forground.b,
|
||||
#ifdef TART_UTF8
|
||||
L' '
|
||||
#else
|
||||
' '
|
||||
#endif
|
||||
);
|
||||
for(int i = 0; i < w->windowSize.y; i++ ) {
|
||||
for(int j = 0; j < w->windowSize.x + 1; j++) {
|
||||
memcpy(w->buffer.buffer[i][j], ce, sizeof(tart_cell));
|
||||
}
|
||||
//t_snprintf(w->buffer.buffer[i][w->windowSize.x],
|
||||
// TART_CELL_SIZE,
|
||||
// TC("\e[1B\e[%dC"),
|
||||
// w->windowSize.x);
|
||||
}
|
||||
|
||||
int dataSize = (window.size.x * window.size.y * sizeof(char) *
|
||||
TART_CELL_DATA_SIZE) + 100;
|
||||
|
||||
w->buffer.curser = (tart_vec2){0,0};
|
||||
|
||||
window.data = malloc(dataSize);
|
||||
for(int g = 0; g < dataSize; g++) {
|
||||
window.data[g] = '\0';
|
||||
}
|
||||
window.data_count = dataSize;
|
||||
printf("\033[?1049h");
|
||||
return window;
|
||||
//bzero(w->buffer.buffer[w->windowSize.y], (BUFFER_LINE_SIZE));
|
||||
|
||||
//t_snprintf(w->buffer.buffer[w->windowSize.y][w->windowSize.x],
|
||||
// TART_CELL_SIZE,
|
||||
// TC("\e[n")
|
||||
// );
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
void tart_destroy_window(struct tart_window* winodw) {
|
||||
printf("\033[?1049l");
|
||||
void tart_free_buffer(tart_window* w) {
|
||||
free(w->buffer.buffer);
|
||||
}
|
||||
|
||||
tart_byte tart_restore_window(struct tart_window* window) {
|
||||
for (int i = 0; i < window->data_count; i++) {
|
||||
window->data[i] = '\0';
|
||||
}
|
||||
int tart_create_window(tart_window* w, tart_vec2 ws, const char* title) {
|
||||
w->windowSize.x = GetTerminalSize(w).x - 1;
|
||||
w->windowSize.y = GetTerminalSize(w).y;
|
||||
w->terminalSize = GetTerminalSize(w);
|
||||
w->homePosition = (tart_vec2){
|
||||
(w->terminalSize.x / 2) - (w->windowSize.x / 2),
|
||||
(w->terminalSize.x / 2) - (w->windowSize.x / 2)
|
||||
};
|
||||
|
||||
tart_create_buffer(w);
|
||||
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
struct tart_buffer tart_create_buffer(tart_id id, struct tart_vec2 size,
|
||||
struct tart_vec2 position) {
|
||||
unsigned int cell_count = size.x * size.y;
|
||||
|
||||
struct tart_cell* cells = (struct tart_cell*)malloc(cell_count * sizeof(struct tart_cell));
|
||||
|
||||
for (int i = 0;i < cell_count;i++) {
|
||||
cells[i] = NULL_CELL;
|
||||
}
|
||||
|
||||
unsigned int data_count = (size.x*size.y) * TART_CELL_DATA_SIZE;
|
||||
struct tart_buffer buf = {cell_count,0,id,size,position,cells}; // -NOTE- dose not set the layer
|
||||
buf.cell_count = cell_count;
|
||||
return buf;
|
||||
}
|
||||
#ifdef TART_RGB_COLORS
|
||||
struct tart_cell tart_create_cell(char display, tart_byte style, struct tart_rgb foreground, struct tart_rgb background) {
|
||||
struct tart_cell b = {foreground, background, style, display,0};
|
||||
return b;
|
||||
}
|
||||
int flush_buffer(tart_window* w) {
|
||||
tart_cell ce;
|
||||
bzero(ce, TART_CELL_SIZE);
|
||||
snprintf(ce,TART_CELL_SIZE,
|
||||
#ifdef TART_UTF8
|
||||
"\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m",
|
||||
#else
|
||||
|
||||
struct tart_cell tart_create_cell(char display, tart_byte style,
|
||||
tart_byte foreground, tart_byte background) {
|
||||
return (struct tart_cell){foreground,background,style,display};
|
||||
}
|
||||
"\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m",
|
||||
#endif
|
||||
tart_byte tart_add_buffer(struct tart_window* window,
|
||||
struct tart_buffer buffer) {
|
||||
if(window->buffer_count <= 0xFF) {
|
||||
window->buffers[window->buffer_count] = buffer;
|
||||
window->buffer_count++;
|
||||
return window->buffer_count;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
tart_byte tart_set_buffer(struct tart_window* window, struct tart_buffer buffer, tart_byte layer) {
|
||||
if(layer <= 0xFF) {
|
||||
window->buffers[layer] = buffer;
|
||||
return layer;
|
||||
w->pallet[0].background.r,
|
||||
w->pallet[0].background.g,
|
||||
w->pallet[0].background.b,
|
||||
w->pallet[0].forground.r,
|
||||
w->pallet[0].forground.g,
|
||||
w->pallet[0].forground.b,
|
||||
#ifdef TART_UTF8
|
||||
L' '
|
||||
#else
|
||||
' '
|
||||
#endif
|
||||
);
|
||||
for(int i = 0; i < w->windowSize.y; i++) {
|
||||
for(int j = 0; j < w->windowSize.x ; j++) {
|
||||
bzero(w->buffer.buffer[i][j], sizeof(tart_cell));
|
||||
memcpy(w->buffer.buffer[i][j], ce, sizeof(tart_cell));
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct tart_buffer* tart_get_buffer(struct tart_window* window, tart_byte layer) {
|
||||
return &window->buffers[layer];
|
||||
void flush_palets(tart_window* w) {
|
||||
for(int i = 0; i < TART_PALET_SIZE; i++) {
|
||||
w->pallet[i].background.r = 0;
|
||||
w->pallet[i].background.g = 0;
|
||||
w->pallet[i].background.b = 0;
|
||||
w->pallet[i].forground.r = 254;
|
||||
w->pallet[i].forground.g = 254;
|
||||
w->pallet[i].forground.b = 254;
|
||||
}
|
||||
}
|
||||
|
||||
struct tart_cell* tart_get_cell(struct tart_buffer* buffer, int idx) {
|
||||
return &buffer->cells[idx];
|
||||
int tart_init(tart_window* w) {
|
||||
int y = 0;
|
||||
int x = 0;
|
||||
w->buffer.jump = (tart_vec2){0,0};
|
||||
flush_buffer(w);
|
||||
flush_palets(w);
|
||||
|
||||
//t_snprintf(ce,TART_CELL_SIZE, TC("\e[%d;%dh"),
|
||||
// (w->terminalSize.x/2)-(w->windowSize.x/2),
|
||||
// (w->terminalSize.y/2)-(w->windowSize.y/2));
|
||||
|
||||
//write(STDOUT_FILENO, ce, TART_CELL_SIZE);
|
||||
|
||||
write(STDOUT_FILENO, "\e[?1049h", 8);
|
||||
write(STDOUT_FILENO, "\e[H", 3);
|
||||
write(STDOUT_FILENO, "\e[s", 3);
|
||||
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
struct tart_cell tart_set_cell(struct tart_buffer* buffer, struct tart_cell cell,int idx) {
|
||||
struct tart_cell c = buffer->cells[idx];
|
||||
buffer->cells[idx] = cell;
|
||||
return c;
|
||||
void tart_disable_cusor() {
|
||||
write(STDOUT_FILENO, "\e[?25l", 8);
|
||||
}
|
||||
void tart_enable_cusor() {
|
||||
write(STDOUT_FILENO, "\e[?25h", 8);
|
||||
}
|
||||
|
||||
tart_byte tart_draw_window(struct tart_window * window, char* rend_buffer) {
|
||||
int offset = 0;
|
||||
int i = 0;
|
||||
int bufferWidth = 0;
|
||||
for (int b = 0;b < 0xFF; b++) {
|
||||
if(window->buffers[b].cell_count == 0)
|
||||
continue;
|
||||
//char bufferSetPre[16];
|
||||
//int bufferSetSize = sprintf(bufferSetPre, "\033[%d;%dH", window->buffers[b].position.y, window->buffers[b].position.x);
|
||||
//for(int preIdx = 0; preIdx < bufferSetSize; preIdx++) {
|
||||
// //window->data[(y*window->buffers[b].size.x) + offset] = movePre[preIdx];
|
||||
// window->data[offset] = bufferSetPre[preIdx];
|
||||
// offset++;
|
||||
//}
|
||||
// offset++;
|
||||
//bufferWidth = window->buffers[b].size.x;
|
||||
for (int y = 0; y < window->buffers[b].current_idx; y++) {
|
||||
// add data to window c buffer.
|
||||
struct tart_cell cell = window->buffers[b].cells[y];
|
||||
// puts cell drawing data in pre
|
||||
|
||||
|
||||
char pre[TART_CELL_DATA_SIZE+20];
|
||||
int size = sprintf(pre, "\033[%d;%dH\033[0m\033[%d;%d;%dm%c\033[0;0m",
|
||||
cell.screen_pos.y+1,cell.screen_pos.x+1,
|
||||
(int)cell.style, cell.foreground,
|
||||
cell.background, cell.display) -1;
|
||||
for(int preIdx = 0; preIdx < size; preIdx++) {
|
||||
window->data[offset] = pre[preIdx];
|
||||
offset++;
|
||||
}
|
||||
#ifdef TART_RGB_COLORS
|
||||
char pre[TART_CELL_DATA_SIZE+4];
|
||||
int size = sprintf(pre, "\033[0m\033[38;2;%d;%d;%dm\033[0m\033[48;2;%d;%d;%dm%\033[%dmc\033[0;0m",
|
||||
// TODO add rgb
|
||||
(int)cell.style, cell.foreground,
|
||||
cell.background, cell.display) -1;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
offset++;
|
||||
|
||||
|
||||
// add cursor move command.
|
||||
i++;
|
||||
int tart_close(tart_window* w) {
|
||||
write(STDOUT_FILENO, "\e[?1049l", 8);
|
||||
//for(int i = 0; i < w->windowSize.y; i++) {
|
||||
// free(w->buffer.buffer[i]);
|
||||
//}
|
||||
//free(w->buffer.buffer);
|
||||
return TART_OK;
|
||||
}
|
||||
int tart_draw(tart_window* w) {
|
||||
for(int i = 0; i < w->windowSize.y; i++) {
|
||||
for(int j = 0; j < w->windowSize.x + 1; j++) {
|
||||
write(STDOUT_FILENO,
|
||||
w->buffer.buffer[i][j],
|
||||
TART_CELL_SIZE
|
||||
);
|
||||
}
|
||||
}
|
||||
fwrite(window->data, sizeof(char), window->data_count, stdout);
|
||||
flush_buffer(w);
|
||||
write(STDOUT_FILENO, "\e[u", strlen("\e[u"));
|
||||
return TART_OK;
|
||||
};
|
||||
}
|
||||
|
||||
tart_byte tart_add_cells_to_buffer(struct tart_buffer* buffer, struct tart_cell* cells) {
|
||||
for(int i = 0; i < buffer->cell_count; i++) {
|
||||
buffer->cells[i] = cells[i];
|
||||
}
|
||||
int tart_jump(tart_window* w, tart_vec2 pos) {
|
||||
w->buffer.jump = pos;
|
||||
w->buffer.curser = pos;
|
||||
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
int tart_move_cursor(tart_window* w, tart_vec2 pos) {
|
||||
|
||||
struct tart_cstring tart_cstring(char *string, long size, struct tart_cell type){
|
||||
struct tart_cstring str = {0,size};
|
||||
str.data = malloc(size * sizeof(struct tart_cell));
|
||||
for(int i = 0; i < size; i++) {
|
||||
str.data[i] = type;
|
||||
str.data[i].display = string[i];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
tart_byte tart_cstring_free(struct tart_cstring* string) {
|
||||
free(string->data);
|
||||
string->size = 0;
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
struct tart_cstring tart_cstring_append(struct tart_cstring* lhs, struct tart_cstring* rhs) {
|
||||
struct tart_cstring ret = {0,0};
|
||||
ret.size = lhs->size + rhs->size;
|
||||
ret.data = malloc(ret.size * sizeof(struct tart_cell));
|
||||
for(int i = 0; i < lhs->size; i++) {
|
||||
ret.data[i] = lhs->data[i];
|
||||
}
|
||||
for(int i = 0; i < rhs->size; i++) {
|
||||
ret.data[lhs->size + i] = rhs->data[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct tart_csprite tart_csprite(struct tart_cell* cells, struct tart_vec2* positions, long size) {
|
||||
|
||||
struct tart_csprite sprite = {0,0,0,size};
|
||||
sprite.data = cells;
|
||||
sprite.position = positions;
|
||||
for(int i = 0; i < size; i++) {
|
||||
|
||||
if(positions[i].x > sprite.bounds.x) {
|
||||
sprite.bounds.x = positions[i].x;
|
||||
}
|
||||
|
||||
if(positions[i].y > sprite.bounds.y) {
|
||||
sprite.bounds.y = positions[i].y;
|
||||
if(pos.x >= w->windowSize.x) {
|
||||
for(int i = pos.x; i >= w->windowSize.x; i /= w->windowSize.x) {
|
||||
w->buffer.curser.x = i;
|
||||
w->buffer.curser.y++;
|
||||
}
|
||||
} else {
|
||||
w->buffer.curser.x = pos.x;
|
||||
}
|
||||
sprite.size = size;
|
||||
return sprite;
|
||||
};
|
||||
|
||||
tart_byte tart_csprite_free(struct tart_csprite* sprite) {
|
||||
sprite->bounds.x = 0;
|
||||
sprite->bounds.y = 0;
|
||||
sprite->size = 0;
|
||||
free(sprite->data);
|
||||
free(sprite->position);
|
||||
if(pos.y >= w->windowSize.y - 1) {
|
||||
for(int i = pos.y; i >= w->windowSize.y - 1; i /= w->windowSize.y) {
|
||||
w->buffer.curser.y = i;
|
||||
}
|
||||
}else {
|
||||
w->buffer.curser.y = pos.y;
|
||||
}
|
||||
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
tart_byte tart_draw_cell_position(struct tart_buffer * buffer, struct tart_cell cell, struct tart_vec2 position) {
|
||||
if(buffer->size.x > position.x && buffer->size.y > position.y) {
|
||||
cell.screen_pos = position;
|
||||
buffer->cells[buffer->current_idx] = cell;
|
||||
buffer->current_idx++;
|
||||
int tart_insert_cell(tart_window* w, tart_byte p, t_char c) {
|
||||
if(c == TC('\n')) {
|
||||
tart_move_cursor(w, (tart_vec2){w->buffer.jump.x, w->buffer.curser.y + 1});
|
||||
return TART_OK;
|
||||
}
|
||||
bzero(w->buffer.buffer[w->buffer.curser.y][w->buffer.curser.x], TART_CELL_SIZE);
|
||||
tart_cell ce;
|
||||
bzero(ce, TART_CELL_SIZE);
|
||||
snprintf(ce,TART_CELL_SIZE,
|
||||
#ifdef TART_UTF8
|
||||
"\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m",
|
||||
#else
|
||||
"\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m",
|
||||
#endif
|
||||
w->pallet[p].background.r,
|
||||
w->pallet[p].background.g,
|
||||
w->pallet[p].background.b,
|
||||
w->pallet[p].forground.r,
|
||||
w->pallet[p].forground.g,
|
||||
w->pallet[p].forground.b,
|
||||
c
|
||||
);
|
||||
|
||||
memcpy(w->buffer.buffer[w->buffer.curser.y][w->buffer.curser.x], ce, TART_CELL_SIZE);
|
||||
tart_move_cursor(w, (tart_vec2){w->buffer.curser.x + 1, w->buffer.curser.y});
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
tart_byte tart_draw_cstring_position(struct tart_buffer *buffer, struct tart_cstring string, struct tart_vec2 position) {
|
||||
struct tart_vec2 pos = position;
|
||||
for(int i = 0; i < string.size; i++) {
|
||||
tart_draw_cell_position(buffer, string.data[i], pos);
|
||||
pos.x++;
|
||||
int tart_printf(tart_window* w,tart_byte p, const t_char* c, ...) {
|
||||
va_list args;
|
||||
va_start(args, c);
|
||||
t_char tmp[1024];
|
||||
t_vsnprintf(tmp,1024, c, args);
|
||||
for(int i = 0; i < t_strlen(tmp); i++) {
|
||||
tart_insert_cell(w, p, tmp[i]);
|
||||
}
|
||||
va_end(args);
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
tart_byte tart_draw_csprite_position(struct tart_buffer * buffer, struct tart_csprite sprite, struct tart_vec2 basePosition) {
|
||||
for(int i = 0; i < sprite.size; i++) {
|
||||
struct tart_vec2 position = {sprite.position[i].x + basePosition.x, sprite.position[i].y + basePosition.y};
|
||||
tart_draw_cell_position(buffer, sprite.data[i], position);
|
||||
}
|
||||
int tart_box(tart_window* w, tart_byte bp, tart_byte fp, const t_char* c,
|
||||
tart_vec2 fpos,
|
||||
tart_vec2 spos) {
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
tart_byte tart_restore_buffer(struct tart_buffer *buffer) {
|
||||
for (int i = 0; i < buffer->cell_count; i++) {
|
||||
buffer->cells[i] = NULL_CELL;
|
||||
buffer->current_idx = 0;
|
||||
}
|
||||
int tart_line(tart_window* w, tart_byte p, const t_char* c, tart_vec2 fpos,
|
||||
tart_vec2 spos) {
|
||||
return TART_OK;
|
||||
}
|
||||
|
||||
208
source/term.c
208
source/term.c
@@ -1,208 +0,0 @@
|
||||
#include "term.h"
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned char haveread = 0;
|
||||
unsigned char __state__ = 0;
|
||||
char __term_input_buffer__[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
|
||||
void set_char_push_back(char t) {
|
||||
char tmp1;
|
||||
char hold1;
|
||||
char hold2;
|
||||
__state__ = __state__ + 1;
|
||||
hold1 = __term_input_buffer__[0];
|
||||
for(int i = 0; i < 8 - 1; i++) {
|
||||
hold2 = __term_input_buffer__[i+1];
|
||||
__term_input_buffer__[i+1] = hold1;
|
||||
hold1 = hold2;
|
||||
|
||||
}
|
||||
__term_input_buffer__[0] = t;
|
||||
}
|
||||
|
||||
// if windows is defined.
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#include <Windows.h>
|
||||
#include <conio.h>
|
||||
|
||||
|
||||
|
||||
|
||||
struct tart_vec2 term_current_size() {
|
||||
struct tart_vec2 ret;
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
||||
unsigned int rows = (csbi.srWindow.Right - csbi.srWindow.Left + 1);
|
||||
unsigned int cols = (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
|
||||
|
||||
unsigned short max_short = 0XFFFF;
|
||||
if(rows < max_short && cols < max_short) {
|
||||
|
||||
ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols};
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
HANDLE __term_input_thread;
|
||||
int __term_input_thread_stop;
|
||||
|
||||
DWORD WINAPI input_threaded_func(void* data) {
|
||||
while(__term_input_thread_stop == 1) {
|
||||
if(_kbhit())
|
||||
set_char_push_back(_getch());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int term_threaded_input_init() {
|
||||
__term_input_thread_stop = 1;
|
||||
__term_input_thread = CreateThread(NULL,0,input_threaded_func,NULL, 0, NULL);
|
||||
return 0;
|
||||
}
|
||||
int term_threaded_input_stop() {
|
||||
__term_input_thread_stop = 0;
|
||||
WaitForSingleObject(__term_input_thread, INFINITE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <threads.h>
|
||||
#include <termios.h>
|
||||
struct tart_vec2 term_current_size() {
|
||||
struct tart_vec2 ret;
|
||||
|
||||
struct winsize w;
|
||||
|
||||
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
|
||||
unsigned int rows = w.ws_col;
|
||||
unsigned int cols = w.ws_row;
|
||||
|
||||
unsigned short max_short = 0XFFFF;
|
||||
if(rows < max_short && cols < max_short) {
|
||||
|
||||
ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols};
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct termios old, current;
|
||||
|
||||
void init_termios(int args) {
|
||||
tcgetattr(0, &old);
|
||||
|
||||
current = old;
|
||||
|
||||
current.c_lflag &= ~ICANON;
|
||||
|
||||
if(args == 0x01) {
|
||||
//current.c_lflag ^ ECHO;
|
||||
} else {
|
||||
//current.c_lflag &= ~ECHO;
|
||||
current.c_lflag ^= ECHO;
|
||||
//current.c_lflag |= ECHO;
|
||||
}
|
||||
|
||||
tcsetattr(0,0, ¤t);
|
||||
}
|
||||
|
||||
void reset_termios(void) {
|
||||
|
||||
tcsetattr(0, TCSANOW, &old);
|
||||
}
|
||||
|
||||
char term_getch() {
|
||||
char tmp;
|
||||
init_termios(0x00);
|
||||
tmp = getchar();
|
||||
reset_termios();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char term_getche() {
|
||||
char tmp;
|
||||
init_termios(0x01);
|
||||
tmp = getchar();
|
||||
reset_termios();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
pthread_t input_thread;
|
||||
|
||||
|
||||
void* input_threaded_func(void* vargp) {
|
||||
int myid = getpid();
|
||||
while(1) {
|
||||
set_char_push_back(term_getch());
|
||||
usleep(1000);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int term_threaded_input_stop() {
|
||||
pthread_cancel(input_thread);
|
||||
reset_termios();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int term_threaded_input_init() {
|
||||
|
||||
pthread_create(&input_thread, NULL, input_threaded_func, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char term_key_pressed() {
|
||||
if( __state__ > 0 ) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void term_handled_key() {
|
||||
if( __state__ > 0 ) {
|
||||
__state__ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void term_end_of_key() {
|
||||
if(__state__ > 0 && haveread > 200) {
|
||||
//__state__ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void term_start_read() {
|
||||
haveread += 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
char term_tinputi(int idx){
|
||||
if(idx < 8) {
|
||||
return __term_input_buffer__[idx];
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
|
||||
char* term_tinputb() {
|
||||
return __term_input_buffer__;
|
||||
}
|
||||
|
||||
char term_tinput() {
|
||||
return __term_input_buffer__[0];
|
||||
}
|
||||
void term_disable_cursor() {
|
||||
printf("\033[?25l", stdout);
|
||||
}
|
||||
|
||||
void term_enable_cursor() {
|
||||
printf("\033[?25h", stdout);
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
// #========================================================================#
|
||||
// | PREACHERDHM:TERM |
|
||||
// | |
|
||||
// | term renders all of the data from TART. Term takes a tart_window and |
|
||||
// | renders all of the buffers and cells inside the that window object. |
|
||||
// | There is a draw function and a draw buffer function. |
|
||||
// | |
|
||||
// | Term is just a way to output the data from tart. |
|
||||
// #========================================================================#
|
||||
#ifndef TERM_H
|
||||
#define TERM_H
|
||||
|
||||
#define NOREAD 0
|
||||
|
||||
#include "tart.h"
|
||||
|
||||
struct tart_vec2 term_current_size();
|
||||
|
||||
char term_getch();
|
||||
char term_getche();
|
||||
|
||||
|
||||
// tinput is threaded input
|
||||
int term_threaded_input_init();
|
||||
int term_threaded_input_stop();
|
||||
char term_tinputi(int idx);
|
||||
char* term_tinputb();
|
||||
char term_tinput();
|
||||
unsigned char term_key_pressed();
|
||||
void term_handled_key();
|
||||
void term_end_of_key();
|
||||
void term_start_read();
|
||||
|
||||
void term_disable_cursor();
|
||||
void term_enable_cursor();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user