-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmain.c
More file actions
445 lines (385 loc) · 15.5 KB
/
main.c
File metadata and controls
445 lines (385 loc) · 15.5 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <SDL2/SDL.h>
#define GLEW_STATIC
#include <GL/glew.h>
#define GL_GLEXT_PROTOTYPES
#include <SDL2/SDL_opengl.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "./editor.h"
#include "./file_browser.h"
#include "./la.h"
#include "./free_glyph.h"
#include "./simple_renderer.h"
#include "./common.h"
#include "./lexer.h"
#include "./sv.h"
// TODO: Save file dialog
// Needed when ded is ran without any file so it does not know where to save.
// TODO: An ability to create a new file
// TODO: Delete a word
// TODO: Delete selection
// TODO: Undo/redo system
void MessageCallback(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam)
{
(void) source;
(void) id;
(void) length;
(void) userParam;
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
type, severity, message);
}
static Free_Glyph_Atlas atlas = {0};
static Simple_Renderer sr = {0};
static Editor editor = {0};
static File_Browser fb = {0};
// TODO: display errors reported via flash_error right in the text editor window somehow
#define flash_error(...) do { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } while(0)
int main(int argc, char **argv)
{
Errno err;
FT_Library library = {0};
FT_Error error = FT_Init_FreeType(&library);
if (error) {
fprintf(stderr, "ERROR: Could not initialize FreeType2 library\n");
return 1;
}
// TODO: users should be able to customize the font
// const char *const font_file_path = "./fonts/VictorMono-Regular.ttf";
const char *const font_file_path = "./fonts/iosevka-regular.ttf";
FT_Face face;
error = FT_New_Face(library, font_file_path, 0, &face);
if (error == FT_Err_Unknown_File_Format) {
fprintf(stderr, "ERROR: `%s` has an unknown format\n", font_file_path);
return 1;
} else if (error) {
fprintf(stderr, "ERROR: Could not load file `%s`\n", font_file_path);
return 1;
}
FT_UInt pixel_size = FREE_GLYPH_FONT_SIZE;
error = FT_Set_Pixel_Sizes(face, 0, pixel_size);
if (error) {
fprintf(stderr, "ERROR: Could not set pixel size to %u\n", pixel_size);
return 1;
}
if (argc > 1) {
const char *file_path = argv[1];
err = editor_load_from_file(&editor, file_path);
if (err != 0) {
fprintf(stderr, "ERROR: Could not read file %s: %s\n", file_path, strerror(err));
return 1;
}
}
const char *dir_path = ".";
err = fb_open_dir(&fb, dir_path);
if (err != 0) {
fprintf(stderr, "ERROR: Could not read directory %s: %s\n", dir_path, strerror(err));
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "ERROR: Could not initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window =
SDL_CreateWindow("ded",
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (window == NULL) {
fprintf(stderr, "ERROR: Could not create SDL window: %s\n", SDL_GetError());
return 1;
}
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
int major;
int minor;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
printf("GL version %d.%d\n", major, minor);
}
if (SDL_GL_CreateContext(window) == NULL) {
fprintf(stderr, "ERROR: Could not create OpenGL context: %s\n", SDL_GetError());
return 1;
}
GLenum glewErr = glewInit();
if (GLEW_OK != glewErr) {
fprintf(stderr, "ERROR: Could not initialize GLEW: %s\n", glewGetErrorString(glewErr));
return 1;
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (GLEW_ARB_debug_output) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
} else {
fprintf(stderr, "WARNING: GLEW_ARB_debug_output is not available");
}
simple_renderer_init(&sr);
free_glyph_atlas_init(&atlas, face);
editor.atlas = &atlas;
editor_retokenize(&editor);
bool quit = false;
bool file_browser = false;
while (!quit) {
const Uint32 start = SDL_GetTicks();
SDL_Event event = {0};
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
quit = true;
}
break;
case SDL_KEYDOWN: {
if (file_browser) {
switch (event.key.keysym.sym) {
case SDLK_F3: {
file_browser = false;
}
break;
case SDLK_UP: {
if (fb.cursor > 0) fb.cursor -= 1;
}
break;
case SDLK_DOWN: {
if (fb.cursor + 1 < fb.files.count) fb.cursor += 1;
}
break;
case SDLK_RETURN: {
const char *file_path = fb_file_path(&fb);
if (file_path) {
File_Type ft;
err = type_of_file(file_path, &ft);
if (err != 0) {
flash_error("Could not determine type of file %s: %s", file_path, strerror(err));
} else {
switch (ft) {
case FT_DIRECTORY: {
err = fb_change_dir(&fb);
if (err != 0) {
flash_error("Could not change directory to %s: %s", file_path, strerror(err));
}
}
break;
case FT_REGULAR: {
// TODO: before opening a new file make sure you don't have unsaved changes
// And if you do, annoy the user about it. (just like all the other editors do)
err = editor_load_from_file(&editor, file_path);
if (err != 0) {
flash_error("Could not open file %s: %s", file_path, strerror(err));
} else {
file_browser = false;
}
}
break;
case FT_OTHER: {
flash_error("%s is neither a regular file nor a directory. We can't open it.", file_path);
}
break;
default:
UNREACHABLE("unknown File_Type");
}
}
}
}
break;
}
} else {
switch (event.key.keysym.sym) {
case SDLK_HOME: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_to_begin(&editor);
} else {
editor_move_to_line_begin(&editor);
}
editor.last_stroke = SDL_GetTicks();
} break;
case SDLK_END: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_to_end(&editor);
} else {
editor_move_to_line_end(&editor);
}
editor.last_stroke = SDL_GetTicks();
} break;
case SDLK_BACKSPACE: {
editor_backspace(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_F2: {
if (editor.file_path.count > 0) {
err = editor_save(&editor);
if (err != 0) {
flash_error("Could not save currently edited file: %s", strerror(err));
}
} else {
// TODO: ask the user for the path to save to in this situation
flash_error("Nowhere to save the text");
}
}
break;
case SDLK_F3: {
file_browser = true;
}
break;
case SDLK_F5: {
simple_renderer_reload_shaders(&sr);
}
break;
case SDLK_RETURN: {
if (editor.searching) {
editor_stop_search(&editor);
} else {
editor_insert_char(&editor, '\n');
editor.last_stroke = SDL_GetTicks();
}
}
break;
case SDLK_DELETE: {
editor_delete(&editor);
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_f: {
if (event.key.keysym.mod & KMOD_CTRL) {
editor_start_search(&editor);
}
}
break;
case SDLK_ESCAPE: {
editor_stop_search(&editor);
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
}
break;
case SDLK_a: {
if (event.key.keysym.mod & KMOD_CTRL) {
editor.selection = true;
editor.select_begin = 0;
editor.cursor = editor.data.count;
}
}
break;
case SDLK_TAB: {
// TODO: indent on Tab instead of just inserting 4 spaces at the cursor
// That is insert the spaces at the beginning of the line. Shift+TAB should
// do unindent, that is remove 4 spaces from the beginning of the line.
// TODO: customizable indentation style
// - tabs/spaces
// - tab width
// - etc.
editor_insert_char(&editor, '\t');
}
break;
case SDLK_c: {
if (event.key.keysym.mod & KMOD_CTRL) {
editor_clipboard_copy(&editor);
}
}
break;
case SDLK_v: {
if (event.key.keysym.mod & KMOD_CTRL) {
editor_clipboard_paste(&editor);
}
}
break;
case SDLK_UP: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_paragraph_up(&editor);
} else {
editor_move_line_up(&editor);
}
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_DOWN: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_paragraph_down(&editor);
} else {
editor_move_line_down(&editor);
}
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_LEFT: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_word_left(&editor);
} else {
editor_move_char_left(&editor);
}
editor.last_stroke = SDL_GetTicks();
}
break;
case SDLK_RIGHT: {
editor_update_selection(&editor, event.key.keysym.mod & KMOD_SHIFT);
if (event.key.keysym.mod & KMOD_CTRL) {
editor_move_word_right(&editor);
} else {
editor_move_char_right(&editor);
}
editor.last_stroke = SDL_GetTicks();
}
break;
}
}
}
break;
case SDL_TEXTINPUT: {
if (file_browser) {
// Nothing for now
// Once we have incremental search in the file browser this may become useful
} else {
const char *text = event.text.text;
size_t text_len = strlen(text);
for (size_t i = 0; i < text_len; ++i) {
editor_insert_char(&editor, text[i]);
}
editor.last_stroke = SDL_GetTicks();
}
}
break;
}
}
{
int w, h;
SDL_GetWindowSize(window, &w, &h);
// TODO(#19): update the viewport and the resolution only on actual window change
glViewport(0, 0, w, h);
}
Vec4f bg = hex_to_vec4f(0x181818FF);
glClearColor(bg.x, bg.y, bg.z, bg.w);
glClear(GL_COLOR_BUFFER_BIT);
if (file_browser) {
fb_render(&fb, window, &atlas, &sr);
} else {
editor_render(window, &atlas, &sr, &editor);
}
SDL_GL_SwapWindow(window);
const Uint32 duration = SDL_GetTicks() - start;
const Uint32 delta_time_ms = 1000 / FPS;
if (duration < delta_time_ms) {
SDL_Delay(delta_time_ms - duration);
}
}
return 0;
}
// TODO: ability to search within file browser
// Very useful when you have a lot of files
// TODO: ability to remove trailing whitespaces