-
Notifications
You must be signed in to change notification settings - Fork 1
/
dix.c
249 lines (198 loc) · 6.05 KB
/
dix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Dix, library for creating console UI for Windows
* Copyright (C) 2017 P4t
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <wchar.h>
#include "dix.h"
typedef struct {
HWND console;
HANDLE buffer;
view_t *child;
UINT width, height;
/*Data to restore*/
HANDLE old_buffer;
WCHAR title[250];
} screen_t;
screen_t screen = {};
dix_status_t init_dix() {
CONSOLE_SCREEN_BUFFER_INFO info;
UINT width, height;
if((screen.console = GetConsoleWindow()) == NULL ||
(screen.old_buffer = GetStdHandle(STD_OUTPUT_HANDLE)) == NULL) {
return RIP;
}
GetConsoleScreenBufferInfo(screen.old_buffer, &info);
width = (UINT) (info.srWindow.Right - info.srWindow.Left + 1);
height = (UINT) (info.srWindow.Bottom - info.srWindow.Top + 1);
screen.buffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(screen.buffer);
SetStdHandle(STD_OUTPUT_HANDLE, screen.buffer);
set_screen_size(width, height);
GetConsoleTitleW(screen.title, 250);
return OK;
}
void deinit_dix() {
SetConsoleActiveScreenBuffer(screen.old_buffer);
SetConsoleTitleW(screen.title);
SetStdHandle(STD_OUTPUT_HANDLE, screen.old_buffer);
CloseHandle(screen.buffer);
CloseHandle(screen.old_buffer);
}
/*
* So, the thing is
* width and height of buffer cannot be less than the width and height of the console window
*/
dix_status_t set_screen_size(UINT width, UINT height) {
CONSOLE_SCREEN_BUFFER_INFO info;
COORD size, max;
SMALL_RECT rekt = {.Left = 0, .Top = 0};
if(width < 2 ||
height < 2) {
return RIP;
}
max = GetLargestConsoleWindowSize(screen.buffer);
size.X = min(width, max.X);
rekt.Right = (SHORT) (size.X - 1);
screen.width = (UINT) size.X;
GetConsoleScreenBufferInfo(screen.buffer, &info);
size.Y = info.dwSize.Y;
rekt.Bottom = (SHORT) (info.dwSize.Y - 1);
if(screen.width > info.dwSize.X) { //if buffer will be larger
SetConsoleScreenBufferSize(screen.buffer, size);
SetConsoleWindowInfo(screen.buffer, TRUE, &rekt);
}
else if(screen.width != info.dwSize.X) { //and if smaller
SetConsoleWindowInfo(screen.buffer, TRUE, &rekt);
SetConsoleScreenBufferSize(screen.buffer, size);
}
size.Y = min(height, max.Y);
rekt.Bottom = (SHORT) (size.Y - 1);
screen.height = (UINT) size.Y;
if(screen.height > info.dwSize.Y) {
SetConsoleScreenBufferSize(screen.buffer, size);
SetConsoleWindowInfo(screen.buffer, TRUE, &rekt);
}
else if(screen.height != info.dwSize.Y) {
SetConsoleWindowInfo(screen.buffer, TRUE, &rekt);
SetConsoleScreenBufferSize(screen.buffer, size);
}
return OK;
}
void set_cursor_position(UINT x, UINT y) {
COORD pos = {.X = (SHORT) x, .Y = (SHORT) y};
SetConsoleCursorPosition(screen.buffer, pos);
}
void set_screen_title(WCHAR *title) {
SetConsoleTitleW(title);
}
view_t *attach_to_screen(view_t *view) {
view_t *previous;
previous = screen.child;
screen.child = view;
if(view != NULL && view != SCREEN) {
view->parent = SCREEN;
}
return previous;
}
void view_set_size(view_t *view, UINT width, UINT height) {
view->width = width;
view->height = height;
}
void view_set_pos(view_t *view, UINT x, UINT y) {
view->x = x;
view->y = y;
}
void view_set_pos_relative(view_t *view, int x, int y) {
view->x += x;
view->y += y;
}
view_t *create_view(UINT x, UINT y, UINT width, UINT height) {
view_t *view = malloc(sizeof *view);
view->x = x;
view->y = y;
view->width = width;
view->height = height;
view->data = NULL;
view->view_render = NULL;
view->view_destroy = NULL;
return view;
}
void *view_get_data(view_t *view) {
return view->data;
}
void view_set_data(view_t *view, void *data) {
view->data = data; //datdaatdatadata
}
void view_set_render_function(view_t *view, view_render_func view_render) {
view->view_render = view_render;
}
void view_set_destroy_function(view_t *view, view_destroy_func view_destroy) {
view->view_destroy = view_destroy;
}
void destroy_view(view_t *view) {
if(view->view_destroy != NULL) {
view->view_destroy(view->data);
}
free(view);
}
void render_view(view_t *view, render_buf_t *out) {
if(view->view_render != NULL &&
view->width != 0 &&
view->height != 0) {
view->view_render(view, out);
}
}
void render(view_t *view) {
render_buf_t out;
view_t *win; //amazing naming
COORD cursor;
SHORT x, y, width, height;
SHORT row;
DWORD DONTCARE;
//set_screen_size(screen.width, screen.height); //just to be sure I guess
//no
if(view == NULL || view == SCREEN || view->parent == SCREEN) {
view = screen.child;
if(view == NULL) {
return;
}
out.width = screen.width;
out.height = screen.height;
out.buff = malloc(screen.width * screen.height * sizeof *out.buff);
wmemset(out.buff, L' ', screen.width * screen.height);
}
else {
out.width = view->parent->width;
out.height = view->parent->height;
out.buff = malloc(view->parent->width * view->parent->height * sizeof *out.buff);
wmemset(out.buff, L' ', view->parent->width * view->parent->height);
}
render_view(view, &out);
for(win = view->parent, x = 0, y = 0; win != SCREEN; win = view->parent) {
x += win->x;
y += win->y;
}
width = (SHORT) min(out.width, screen.width - x);
height = (SHORT) min(out.height, screen.height - y);
for(row = 0; row < height; row++) {
cursor.X = x;
cursor.Y = y + row;
WriteConsoleOutputCharacterW(screen.buffer, out.buff + row * out.width, (DWORD) width, cursor, &DONTCARE);
}
free(out.buff);
}