Files
degvi/Terminal.c
2013-01-31 07:30:12 +09:00

65 lines
1.2 KiB
C

#include "init.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "Terminal.h"
Terminal Terminal_create(void)
{
Terminal this;
struct termios settings;
struct winsize w;
this = malloc(sizeof(struct Terminal_struct));
if (this == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
if (tcgetattr(fileno(stdin), &this->backupSettings) == -1) {
fprintf(stderr, "tcgetattr failed\n");
exit(EXIT_FAILURE);
}
memcpy(&settings, &this->backupSettings, sizeof(struct termios));
settings.c_lflag &= ~(ECHO | ICANON);
tcsetattr(fileno(stdin), TCSANOW, &settings);
ioctl(fileno(stdout), TIOCGWINSZ, &w);
this->width = w.ws_col;
this->height = w.ws_row;
return this;
}
void Terminal_destroy(Terminal this)
{
if (this == NULL) {
return;
}
tcsetattr(fileno(stdin), TCSANOW, &this->backupSettings);
free(this);
}
int Terminal_width(Terminal this)
{
return this->width;
}
int Terminal_height(Terminal this)
{
return this->height;
}
void Terminal_setCursorPosition(Terminal this, int x, int y)
{
assert(x >= 0 && x < this->width && y >= 0 && y < this->height);
/* VT100 command */
printf("\x1b[%d;%dH", 1 + y, 1 + x);
}