-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
146 lines (129 loc) · 3.11 KB
/
main.c
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
#include <ncurses.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "utils.h"
#include "defines.h"
#include "elements.h"
#include "chatclient.h"
#include "messages.h"
#include "debug.h"
#define ctrl(x) ((x) & 0x1f)
typedef enum
{
STATE_LOGIN,
STATE_MAIN
} State;
LoginWindow loginWindow;
ChatWindow chatWindow;
ChatClient client;
State state = STATE_LOGIN;
void initWindows();
void exitProgram();
void draw();
void handleResize(int sig);
void rcvMsg(Message msg);
int main()
{
signal(SIGWINCH, handleResize);
initscr();
raw();
noecho();
keypad(stdscr, TRUE);
initWindows();
draw();
int ch;
while (state == STATE_LOGIN)
{
ch = getch();
if (ch == '\n' && loginWindow.input_num == 2)
{
state = STATE_MAIN;
}
else
{
handleChLoginWindow(&loginWindow, ch);
}
draw();
}
// ///////////////// TEMP /////////////////
// state = STATE_MAIN;
// strcpy(loginWindow.server_ip, "10.192.211.75");
// strcpy(loginWindow.server_port, "5700");
// strcpy(loginWindow.username, "z4okolatka");
// \\\\\\\\\\\\\\\\\ TEMP \\\\\\\\\\\\\\\\\
ChatClient client = initChatClient(loginWindow.server_ip, atoi(loginWindow.server_port), loginWindow.username);
connectToChat(&client, loginWindow.username, rcvMsg);
strcpy(chatWindow.username, loginWindow.username);
char msg[MESSAGE_BUFFER];
draw();
while (state == STATE_MAIN)
{
ch = getch();
if (ch == ctrl('e'))
{
sendMessage(&client, createMessage("[Left the chat]", loginWindow.username));
disconnect(&client);
exitProgram();
}
else if (ch == '\n' || ch == KEY_ENTER)
{
sendMessage(&client, createMessage(chatWindow.message, loginWindow.username));
clearMessageBox(&chatWindow);
}
else
{
handleChChatWindow(&chatWindow, ch);
}
draw();
}
exitProgram();
}
void initWindows()
{
int console_height, console_width;
getmaxyx(stdscr, console_height, console_width);
loginWindow = createLoginWindow(console_width, console_height);
chatWindow = createChatWindow(console_width, console_height);
}
void draw()
{
clear();
refresh();
if (state == STATE_LOGIN)
drawLoginWindow(&loginWindow);
else if (state == STATE_MAIN)
drawChatWindow(&chatWindow);
}
void handleResize(int sig)
{
endwin();
refresh();
clear();
int console_height, console_width;
getmaxyx(stdscr, console_height, console_width);
if (state == STATE_LOGIN)
{
resizeLoginWindow(&loginWindow, console_width, console_height);
}
else if (state == STATE_MAIN)
{
resizeChatWindow(&chatWindow, console_width, console_height);
}
draw();
}
void rcvMsg(Message msg)
{
receiveMessage(&chatWindow, msg);
draw();
}
void exitProgram()
{
debugLog(VERBOSE, "Exiting program");
if (loginWindow.win)
delwin(loginWindow.win);
endwin();
// closeDebugLog();
exit(0);
}