summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-20 01:50:14 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-07-20 01:50:14 -0300
commit8693eef8febde4df90b7fbd1fb9ac813b132fa50 (patch)
tree088258d6333d54a6b90f331d3b723ae824110679 /src/client
parent7620a0b1c080f7b6315e796d29e324bdca98484b (diff)
downloadjet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.tar.gz
jet-8693eef8febde4df90b7fbd1fb9ac813b132fa50.zip
Wrap external functionality into classes
Diffstat (limited to 'src/client')
-rw-r--r--src/client/cursor.cpp13
-rw-r--r--src/client/jetc.cpp59
-rw-r--r--src/client/window.cpp73
3 files changed, 77 insertions, 68 deletions
diff --git a/src/client/cursor.cpp b/src/client/cursor.cpp
deleted file mode 100644
index 4e76151..0000000
--- a/src/client/cursor.cpp
+++ /dev/null
@@ -1,13 +0,0 @@
-struct Cursor {
- int x;
- int y;
- Window *w;
-
- void move_left() {
- if (x > 0) x--;
- }
-
- void move_right() {
- if (x < w->width - 1 && w->pos(x + 1, y) != 0) x++;
- }
-};
diff --git a/src/client/jetc.cpp b/src/client/jetc.cpp
index 8d31d72..2dd6074 100644
--- a/src/client/jetc.cpp
+++ b/src/client/jetc.cpp
@@ -1,57 +1,22 @@
#include <stdio.h>
#include <string.h>
-#include <cursesw.h>
-
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/ip.h>
-#include <arpa/inet.h>
-#include <unistd.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
-#define PORT 6969
int main(int argc, char *argv[]) {
- initscr();
- cbreak();
- noecho();
- intrflush(stdscr, FALSE);
- keypad(stdscr, TRUE);
-
Window window;
- getmaxyx(stdscr, window.height, window.width);
- window.init();
-
- 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;
- Cursor cursor = { 0, 0, &window };
int quit = 0;
while (!quit) {
- clear();
+ window.redraw();
- int8_t msg[5];
- msg[0] = OP_SHOW;
- encode2(window.width, msg, 1);
- encode2(window.height, msg, 3);
- write(s, msg, 5);
- read(s, window.view, window.width * window.height);
- for (int i = 0; i < window.width * window.height; i++) {
- printw("%c", window.view[i]);
- }
- move(cursor.y, cursor.x);
-
- int8_t mov[2];
- int8_t del[1];
- int8_t ins[2];
int input = getch();
if (mode == NORMAL_MODE) {
switch (input) {
@@ -62,16 +27,10 @@ int main(int argc, char *argv[]) {
mode = INSERT_MODE;
break;
case 'h':
- mov[0] = OP_MOVE1;
- mov[1] = -1;
- write(s, mov, 2);
- cursor.move_left();
+ window.move_left();
break;
case 'l':
- mov[0] = OP_MOVE1;
- mov[1] = 1;
- write(s, mov, 2);
- cursor.move_right();
+ window.move_right();
break;
}
} else {
@@ -80,19 +39,13 @@ int main(int argc, char *argv[]) {
mode = NORMAL_MODE;
break;
case KEY_BACKSPACE:
- del[0] = OP_DELETE;
- write(s, del, 1);
+ window.delete_element();
break;
default:
- ins[0] = OP_INSERT;
- ins[1] = input;
- write(s, ins, 2);
+ window.insert_element(input);
}
}
}
- close(s);
-
- endwin();
return 0;
}
diff --git a/src/client/window.cpp b/src/client/window.cpp
index e718e63..9e389fe 100644
--- a/src/client/window.cpp
+++ b/src/client/window.cpp
@@ -1,16 +1,85 @@
-struct Window {
+#include <cursesw.h>
+
+class Window {
+ Socket io;
char *view;
int width;
int height;
+ int cursor_x;
+ int cursor_y;
+
+ public:
+
+ Window() : cursor_x(0), cursor_y(0) {
+ initscr();
+ cbreak();
+ noecho();
+ intrflush(stdscr, FALSE);
+ keypad(stdscr, TRUE);
+
+ getmaxyx(stdscr, height, width);
- void init() {
view = new char[width * height];
for (int i = 0; i < width * height; i++) {
view[i] = 0;
}
+
+ io.connect();
+ }
+
+ ~Window() {
+ endwin();
+ }
+
+ void redraw() {
+ clear();
+
+ int8_t msg[5];
+ msg[0] = OP_SHOW;
+ encode2(width, msg, 1);
+ encode2(height, msg, 3);
+ io.send(msg, 5);
+ io.recv(view, width * height);
+ for (int i = 0; i < width * height; i++) {
+ printw("%c", view[i]);
+ }
+ move(cursor_y, cursor_x);
}
char pos(size_t x, size_t y) {
return view[x + y * width];
}
+
+ void move_left() {
+ int8_t mov[2];
+ if (cursor_x > 0) {
+ mov[0] = OP_MOVE1;
+ mov[1] = -1;
+ io.send(mov, 2);
+ cursor_x--;
+ }
+ }
+
+ void move_right() {
+ int8_t mov[2];
+ if (cursor_x < width - 1 && pos(cursor_x + 1, cursor_y) != 0) {
+ mov[0] = OP_MOVE1;
+ mov[1] = 1;
+ io.send(mov, 2);
+ cursor_x++;
+ }
+ }
+
+ void delete_element() {
+ int8_t del[1];
+ del[0] = OP_DELETE;
+ io.send(del, 1);
+ }
+
+ void insert_element(int input) {
+ int8_t ins[2];
+ ins[0] = OP_INSERT;
+ ins[1] = input;
+ io.send(ins, 2);
+ }
};