build: add module to handle querying platform details
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#include "../../print.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int z__fx_stream_is_tty(FILE *fp)
|
||||
{
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(fp));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
return status == TRUE ? 1 : 0;
|
||||
}
|
||||
|
||||
int z__fx_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h)
|
||||
{
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(fp));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
if (status == FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (w) {
|
||||
*w = csbi.srWindow.Right - csbi.srWindow.Left;
|
||||
}
|
||||
|
||||
if (h) {
|
||||
*h = csbi.srWindow.Bottom - csbi.srWindow.Top;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int z__fx_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
|
||||
{
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(in));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
if (status == FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x) {
|
||||
*x = csbi.dwCursorPosition.X;
|
||||
}
|
||||
|
||||
if (y) {
|
||||
*y = csbi.dwCursorPosition.Y;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define APPLY_FLAG(mod, attrib, x, y) \
|
||||
if ((mod) & (x)) { \
|
||||
(attrib) |= (y); \
|
||||
}
|
||||
|
||||
#define APPLY_FLAG_X(mod, attrib, x, y) \
|
||||
if ((mod) & (x)) { \
|
||||
(attrib) |= (y); \
|
||||
} else { \
|
||||
(attrib) &= ~(y); \
|
||||
}
|
||||
|
||||
int z__fx_stream_set_modifier(FILE *fp, unsigned int mod)
|
||||
{
|
||||
WORD attrib = 0;
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
|
||||
HANDLE console = (HANDLE)(INT_PTR)_get_osfhandle(fileno(fp));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
if (status == FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
attrib = csbi.wAttributes;
|
||||
|
||||
if (mod & Z__FX_STREAM_MOD_RESET) {
|
||||
attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_FX;
|
||||
SetConsoleTextAttribute(console, attrib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Z__FX_STREAM_MOD_GET_FG_COLOUR(mod) != 0) {
|
||||
if (mod & Z__FX_STREAM_MOD_BLACK) {
|
||||
attrib
|
||||
&= ~(FOREGROUND_RED | FOREGROUND_GREEN
|
||||
| FOREGROUND_FX);
|
||||
} else {
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_RED, FOREGROUND_RED);
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_GREEN,
|
||||
FOREGROUND_GREEN);
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_FX, FOREGROUND_FX);
|
||||
}
|
||||
}
|
||||
|
||||
if (Z__FX_STREAM_MOD_GET_BG_COLOUR(mod) != 0) {
|
||||
if (mod & Z__FX_STREAM_MOD_BG_BLACK) {
|
||||
attrib
|
||||
&= ~(BACKGROUND_RED | BACKGROUND_GREEN
|
||||
| BACKGROUND_FX);
|
||||
} else {
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_BG_RED,
|
||||
BACKGROUND_RED);
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_BG_GREEN,
|
||||
BACKGROUND_GREEN);
|
||||
APPLY_FLAG_X(
|
||||
mod, attrib, Z__FX_STREAM_MOD_BG_FX,
|
||||
BACKGROUND_FX);
|
||||
}
|
||||
}
|
||||
|
||||
APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_ULINE, COMMON_LVB_UNDERSCORE);
|
||||
APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_INVERT, COMMON_LVB_REVERSE_VIDEO);
|
||||
APPLY_FLAG(
|
||||
mod, attrib, Z__FX_STREAM_MOD_BRIGHT | Z__FX_STREAM_MOD_BOLD,
|
||||
FOREGROUND_INTENSITY);
|
||||
APPLY_FLAG(mod, attrib, Z__FX_STREAM_MOD_BG_BRIGHT, BACKGROUND_INTENSITY);
|
||||
|
||||
SetConsoleTextAttribute(console, attrib);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user