summaryrefslogtreecommitdiff
path: root/jet2.cpp
blob: 1a088dda21848ba3713a87829597714d31b364b2 (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
#include <stdio.h>
#include <string.h>
#include <curses.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "ipc.cpp"

#define NORMAL_MODE 0
#define INSERT_MODE 1
#define PORT 6969
#define pos(x, y) (x) + (y) * window_width

int main(int argc, char *argv[]) {
	initscr();
	cbreak();
	noecho();
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);

	int window_height, window_width;
	getmaxyx(stdscr, window_height, window_width);

	char *view = new char[window_width * window_height];
	for (int i = 0; i < window_width * window_height; view[i++] = 0);

	int s = socket(AF_INET, SOCK_STREAM, 0);
	sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)};
	connect(s, (sockaddr *) &addr, sizeof(sockaddr_in));

	int mode = NORMAL_MODE;

	int quit = 0;
	while (!quit) {
		clear();

		uint8_t msg[5];
		msg[0] = OP_SHOW;
		encode2(window_width, msg, 1);
		encode2(window_height, msg, 3);
		write(s, msg, 5);
		read(s, view, window_width * window_height);
		for (int i = 0; i < window_width * window_height; i++) {
			printw("%c", view[i]);
		}

		uint8_t mov[2];
		uint8_t del[1];
		uint8_t ins[2];
		int input = getch();
		if (mode == NORMAL_MODE) {
			switch (input) {
				case '':
					quit = 1;
					break;
				case 'i':
					mode = INSERT_MODE;
					break;
				case 'h':
					mov[0] = OP_MOVE1;
					mov[1] = -1;
					write(s, mov, 2);
					break;
				case 'l':
					mov[0] = OP_MOVE1;
					mov[1] = 1;
					write(s, mov, 2);
					break;
			}
		} else {
			switch (input) {
				case '':
					mode = NORMAL_MODE;
					break;
				case KEY_BACKSPACE:
					del[0] = OP_DELETE;
					write(s, del, 1);
					break;
				default:
					ins[0] = OP_INSERT;
					ins[1] = input;
					write(s, ins, 2);
			}
		}
	}

	endwin();
	return 0;
}