-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
273 lines (223 loc) · 7.03 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @file main.c
* @author awewsomegamer <[email protected]>
*
* @section LICENSE
*
* Assembled - Column based text editor
* Copyright (C) 2023-2024 awewsomegamer
*
* This file is apart of Assembled.
*
* Assembled is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @section DESCRIPTION
*
* The main program file.
*/
#include <editor/syntax/syntax.h>
#include <editor/buffer/editor.h>
#include <editor/keyboard.h>
#include <editor/config.h>
#include <interface/screens/editor_scr.h>
#include <interface/screens/file_load_scr.h>
#include <interface/theming/themes.h>
#include <interface/interface.h>
#include <interface/screens/start.h>
#include <global.h>
#include <util.h>
#include <includes.h>
/**
* Controls whether the main loop is running (1) or not (0).
* */
bool running = 1;
/**
* Set when new input from the keyboard is present, otherwise 0.
* */
bool update = 0;
/**
* Debug log file pointer.
*
* This file is only used if Assembled is compiled with
* AS_DEBUG_ENABLE defined. This can be acheived through
* adding the `debug` target to the `make` command.
* `make debug`
* */
FILE *__AS_DBG_LOG_FILE__ = NULL;
static int default_column_definition[] = { 0 };
struct AS_GlobalCtx as_ctx = { 0 };
/**
* Initialize ncurses library
*
* Initialize the terminal.
* */
void init_ncurses() {
AS_DEBUG_MSG("Initializing ncurses\n");
setlocale(LC_ALL, "UTF-8");
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
if (has_colors()) {
AS_DEBUG_MSG("Terminal has colors\n");
start_color();
use_default_colors();
register_custom_colors();
AS_DEBUG_MSG("Initialized Assembled colors\n");
}
int x = 0, y = 0;
getmaxyx(stdscr, y, x);
as_ctx.render_ctx.max_x = x;
as_ctx.render_ctx.max_y = y;
if (as_ctx.render_ctx.max_x == 0 || as_ctx.render_ctx.max_y == 0) {
endwin();
printf("Max X: %d, Max Y: %d are unsuitable for operation\n", as_ctx.render_ctx.max_x, as_ctx.render_ctx.max_y);
exit(1);
}
AS_DEBUG_MSG("Initialized ncurses\n");
}
/**
* Update the state of the editor
*
* Checks if the keyboard has any new input and
* updates the current screen.
* */
void editor() {
// Read in current key
int c = getch();
if (c > -1) {
// If a key is present, cleaar the message,
// trigger an update and tell the keyboard to
// handle the key
sprintf(as_ctx.editor_scr_message, "");
update = 1;
key(c);
}
// Udate the screen if it exists and has an update function
if (as_ctx.screen != NULL && as_ctx.screen->update != NULL) {
as_ctx.screen->update(&as_ctx.render_ctx);
}
}
/**
* Render the current screen
*
* Render the current active screen, clearing
* the screen in the process
* */
void interface() {
erase();
// Draw the screen if it exists and has a render function
if (as_ctx.screen != NULL && as_ctx.screen->render != NULL) {
as_ctx.screen->render(&as_ctx.render_ctx);
}
refresh();
}
/**
* Function to be called when SIGINT is received.
* */
void terminate(int signal) {
running = 0;
}
int main(int argc, char **argv) {
// Setup up debug file
AS_DEBUG_CODE(
__AS_DBG_LOG_FILE__ = fopen("debug.log", "w");
if (__AS_DBG_LOG_FILE__ == NULL) {
printf("Failed to open ./debug.log\n");
return 1;
}
)
// Initialize
as_ctx.col_desc_i = -1;
read_config();
init_syntax();
if (as_ctx.col_desc_i == -1) {
// No column was selected to be booted with, try to find a defined one
for (int i = 0; i < AS_MAX_COLUMNS; i++) {
if (as_ctx.col_descs[i].column_positions != NULL) {
as_ctx.col_desc_i = i;
break;
}
}
}
if (as_ctx.col_desc_i == -1) {
// No predefined column was found, define a single column
printf("Failed to find a user defined column, defining a single column\n");
AS_DEBUG_MSG("Failed to find a user defined column, defining a single column\n");
as_ctx.col_desc_i = 0;
as_ctx.col_descs[as_ctx.col_desc_i].column_positions = default_column_definition;
as_ctx.col_descs[as_ctx.col_desc_i].column_count = 1;
as_ctx.col_descs[as_ctx.col_desc_i].delimiter = 0;
}
// Register screens
register_start_screen();
register_editor_screen();
register_file_load_scr();
// Check for file to load
if (argc > 1) {
load_file(argv[1]);
switch_to_screen("editor");
} else {
switch_to_screen("start");
}
// Allow program to terminate properly on Ctrl + C
signal(SIGINT, terminate);
init_ncurses();
// Initialization is complete
// Get to running
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
double now = 0;
double last = spec.tv_nsec / 1000000000.0;
double delta = 0;
double accumulator = 0;
// "Wake-up" cycle
editor();
interface();
while (running) {
bool render = 0;
clock_gettime(CLOCK_REALTIME, &spec);
now = spec.tv_nsec / 1000000000.0;
delta = now - last;
last = now;
accumulator += fabs(delta);
while (accumulator >= TARGET_FPS) {
editor();
if (update == 0) {
// No update, don't need to render
accumulator = 0;
break;
}
accumulator -= TARGET_FPS;
render = 1;
}
// Probably impossible to type a character in an
// interval below ~8 milliseconds
usleep(8000);
if (render || (as_ctx.screen->render_options & SCR_OPT_ALWAYS)) {
interface();
}
update = 0;
}
// Shutdown
// Stop ncurses window
endwin();
AS_DEBUG_MSG("Successfuly exited\n");
// Close debug file
AS_DEBUG_CODE( fclose(__AS_DBG_LOG_FILE__); )
return 0;
}