~kiwec/ke

ref: 4e0a6570b5c10ffd1830ab776619aa0a2f95ddfb ke/src/main.cc -rw-r--r-- 1.0 KiB
4e0a6570 — Wolf Clément Change file style 3 years ago
                                                                                
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
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <termbox.h>
#include <unistd.h>
#include "Buffer.h"
#include "Editor.h"

void seghandle(int sn) {
	tb_shutdown();
	signal(sn, SIG_DFL);
	kill(getpid(), sn);
}

int main(int argc, const char** argv) {
	// Init termbox
	auto err = tb_init();
	switch(err) {
		case TB_EUNSUPPORTED_TERMINAL:
		std::cerr << "Termbox: Unsupported terminal." << std::endl;
		return 1;
		case TB_EFAILED_TO_OPEN_TTY:
		std::cerr << "Termbox: Failed to open tty." << std::endl;
		return 1;
		case TB_EPIPE_TRAP_ERROR:
		std::cerr << "Termbox: Pipe trap error." << std::endl;
		return 1;
	}

	tb_select_input_mode(TB_INPUT_ALT | TB_INPUT_MOUSE);
	tb_select_output_mode(TB_OUTPUT_256);
	signal(SIGSEGV, seghandle);
	atexit(tb_shutdown);

	// Start editor
	Editor editor;
	editor.open(argc == 2 ? argv[1] : "");

	for(;;) {
		editor.draw();

		tb_event ev;
		switch(tb_poll_event(&ev)) {
			case TB_EVENT_KEY:
				Input::handle(&ev, false);
				break;
			case TB_EVENT_MOUSE:
				Input::handle(&ev, true);
				break;
		}
	}
}