summaryrefslogtreecommitdiff
path: root/src/client/jetc.cpp
blob: 8a0963d309201f649bed4d8d20c29306cda8387e (plain)
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
#include <stdio.h>
#include <string.h>

#include <common/ipc.cpp>
#include <common/socket.cpp>
#include <client/window.cpp>
#include <client/cursor.cpp>

#define NORMAL_MODE 0
#define INSERT_MODE 1

Socket io;
Window window;
Cursor cursor(window);
Message msg;

void update() {
	msg.encode1(OP_SHOW);
	msg.encode2(window.width);
	msg.encode2(window.height);
	io.send(msg);
	io.recv(window.view, window.width * window.height);
	io.recv(window.line_ends, window.height * sizeof(int));

	window.update();
	cursor.update();
}

void move_left() {
	msg.encode1(OP_MOVE1);
	msg.encode1(-1);
	io.send(msg);
	cursor.move_left();
}

void move_right() {
	msg.encode1(OP_MOVE1);
	msg.encode1(1);
	io.send(msg);
	cursor.move_right();
}

void move_down() {
	cursor.move_down();
}

void move_up() {
	cursor.move_up();
}

void delete_element() {
	msg.encode1(OP_DELETE);
	io.send(msg);
	cursor.move_left();
}

void insert_element(int input) {
	msg.encode1(OP_INSERT);
	msg.encode1(input);
	io.send(msg);
	cursor.move_right();
}

int main(int argc, char *argv[]) {
	io.connect();

	int mode = NORMAL_MODE;
	int quit = 0;
	while (!quit) {
		update();

		int input = window.get_input();
		if (mode == NORMAL_MODE) {
			switch (input) {
				case '':
					quit = 1;
					break;
				case 'i':
					mode = INSERT_MODE;
					break;
				case 'h':
					move_left();
					break;
				case 'j':
					move_down();
					break;
				case 'k':
					move_up();
					break;
				case 'l':
					move_right();
					break;
			}
		} else {
			switch (input) {
				case '':
					mode = NORMAL_MODE;
					break;
				case KEY_BACKSPACE:
					delete_element();
					break;
				default:
					insert_element(input);
			}
		}
	}

	return 0;
}