Skip to content

Commit 5129f00

Browse files
committed
2 parents d895b16 + 82bc51d commit 5129f00

File tree

6 files changed

+74
-50
lines changed

6 files changed

+74
-50
lines changed

Client/include/Client.h

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ extern int send_result;
1313
extern int bytes_received;
1414

1515
extern bool connected;
16+
extern bool kill;
17+
18+
extern string kill_reason;
1619

1720
extern int client_init();
1821

Client/include/GUI.h

+9-11
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,20 @@
3939

4040
enum class screens { Home, Vol, Media, Spotify };
4141

42-
int gui_init();
42+
extern int gui_init();
4343

44-
ImVec4* set_colors();
45-
ImGuiStyle& set_style();
44+
extern ImVec4* set_colors();
45+
extern ImGuiStyle& set_style();
4646

47-
void draw_main(screens current);
47+
extern void draw_main(screens current);
4848

49-
void countdown_reconnect();
50-
51-
void draw_home();
52-
void draw_loading();
53-
void draw_setup();
54-
void draw_disconnected_alert();
49+
extern void draw_home();
50+
extern void draw_killed();
51+
extern void draw_setup();
52+
extern void draw_disconnected_alert();
5553

5654
screens get_default_screen();
5755

5856
#ifdef _DEBUG
5957
void draw_performance();
60-
#endif
58+
#endif

Client/include/Header.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <iostream>
99
#include <thread>
10-
#include <chrono>
1110
#include <string>
1211
#include <vector>
1312

@@ -26,10 +25,8 @@ using std::vector;
2625

2726
using std::to_string;
2827

29-
extern bool failed;
28+
extern bool failed; // TODO: Move to specific headers
3029
extern bool failed_backup;
31-
extern bool has_commsock;
32-
extern bool has_confsock;
3330
extern bool done;
3431
extern bool disconnected_modal;
3532

Client/src/Client.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ struct timeval timeout;
77
bool failed = true;
88
bool connected = false;
99
bool disconnected_modal = false;
10+
bool kill = false;
1011

1112
int sock;
1213
int bytesReceived;
1314
int send_result;
1415

1516
char ip_address[16];
1617

18+
string kill_reason;
19+
1720
json config;
1821

1922
int client_init() {
20-
const string pad_id = rw_UUID();
23+
const string ID = rw_UUID();
2124

2225
sock = socket(AF_INET, SOCK_STREAM, 0);
2326
if (sock == -1) {done = true; return 1;}
@@ -44,7 +47,7 @@ int client_init() {
4447
if (done) return 0;
4548
} while (!connected);
4649
if (!have_ip) rw_ipstore();
47-
// if previous MAYBETODO is implemented, add a check_config here with current connected ip as parameter
50+
// if previous MAYBETODO is implemented, add a check_config here with current connected ip as param
4851

4952
timeout.tv_sec = 0;
5053
if ((setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) ||
@@ -54,8 +57,8 @@ int client_init() {
5457

5558
disconnected_modal = false;
5659
string message;
57-
char* buf = (char*)malloc(sizeof(char) * (1024 * 256));
58-
send_result = send(sock, pad_id.c_str(), pad_id.size() + 1, 0);
60+
char* buf = new char[1024 * 256];
61+
send_result = send(sock, ID.c_str(), ID.length() + 1, 0);
5962
do {
6063
memset(buf, 0, 1024 * 256);
6164
bytesReceived = recv(sock, buf, 1024 * 256, 0);
@@ -64,12 +67,17 @@ int client_init() {
6467
else {
6568
cerr << message << "\n";
6669
if (message.substr(0, 3) == "cfg") write_config(message.substr(3, message.length()));
70+
else if (message.substr(0, 4) == "kill") {
71+
kill = true;
72+
kill_reason = message.substr(4, message.length());
73+
}
6774
}
6875
} while (bytesReceived > 0 || send_result > 0 || !done);
69-
free(buf);
76+
delete[] buf;
7077
close(sock);
7178
connected = false;
72-
disconnected_modal = true;
79+
if (!kill) disconnected_modal = true;
80+
failed = true;
7381
cerr << "------------REBOOTING SOCK------------\n";
7482
if (!done) return client_init();
7583
return 0;

Client/src/Config.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../include/Config.h"
22
#include "../include/Header.h"
3+
#include "../include/Client.h"
34

45
struct passwd* pw = getpwuid(getuid());
56

@@ -55,7 +56,7 @@ bool rw_ipstore() {
5556
ip_reader.close();
5657
if (!read_ip.empty()) {
5758
strncpy(ip_address, read_ip.c_str(), read_ip.length());
58-
failed = false;
59+
if (!kill) failed = false;
5960
return true;
6061
}
6162
}
@@ -100,7 +101,7 @@ void clear_config() {
100101
ofstream writer(nxsh_config);
101102
profiles.clear();
102103
profile profile1;
103-
for (int i = 1; i <= profile1.columns * profile1.rows; i++) {
104+
for (int i = 1; i <= profile1.columns * profile1.rows; ++i) {
104105
button button1;
105106
profile1.buttons.push_back(button1);
106107
}
@@ -128,18 +129,18 @@ void set_properties() {
128129
if (config[config.begin().key()].contains("profiles")) {
129130
int profile_count = 0;
130131
for (auto& profile : config[config.begin().key()]["profiles"]) if (profile.is_object()) profile_count++;
131-
for (int i = 0; i < profile_count; i++) {
132+
for (int i = 0; i < profile_count; ++i) {
132133
profile profile1;
133134
json profile_store = config[config.begin().key()]["profiles"][to_string(i)];
134135
if (profile_store.contains("columns")) profile1.columns = std::stoi(profile_store["columns"].get<string>());
135136
if (profile_store.contains("rows")) profile1.rows = std::stoi(profile_store["rows"].get<string>());
136137
if (profile_store.contains("pages")) {
137138
int page_count = 0;
138139
for (auto& page : profile_store["pages"]) if (page.is_object()) page_count++;
139-
for (int j = 0; j < page_count; j++) {
140+
for (int j = 0; j < page_count; ++j) {
140141
json page_store = profile_store["pages"][to_string(j)];
141142
if (page_store.contains("buttons")) {
142-
for (int k = 0; k < profile1.columns * profile1.rows; k++) {
143+
for (int k = 0; k < profile1.columns * profile1.rows; ++k) {
143144
button button1;
144145
if (page_store["buttons"].contains(to_string(k))) {
145146
json button_store = page_store["buttons"][to_string(k)];

Client/src/GUI.cpp

+41-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "../include/Config.h"
44
#include "../include/Client.h"
55

6+
// int current_profile = 0; // TODO:
7+
68
bool done = false;
79
bool failed_backup;
810
bool run_setup;
@@ -63,8 +65,9 @@ int gui_init() {
6365
ImGui_ImplSDL3_NewFrame();
6466
ImGui::NewFrame();
6567

66-
if (run_setup && !have_ip) draw_setup();
67-
if (!connected && have_ip || disconnected_modal) draw_disconnected_alert();
68+
if (run_setup && !have_ip) draw_setup();
69+
if ((!connected && have_ip) && !kill || disconnected_modal) draw_disconnected_alert();
70+
if (kill && !connected) draw_killed();
6871

6972
im_window_flags |= ImGuiWindowFlags_NoDecoration
7073
| ImGuiWindowFlags_NoDocking
@@ -141,9 +144,9 @@ ImGuiStyle& set_style(/*style parameters*/) {
141144

142145
void draw_main(screens current) { // TODO: Create window in main to display tabs, close button etc.
143146
ImGuiIO& io = ImGui::GetIO();
147+
ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x, io.DisplaySize.y));
148+
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
144149
ImGui::Begin("main", NULL, im_window_flags | ImGuiWindowFlags_NoBringToFrontOnFocus); // Allow performance info to always be on top
145-
ImGui::SetWindowSize(ImVec2(io.DisplaySize.x, io.DisplaySize.y));
146-
ImGui::SetWindowPos(ImVec2(0.0f, 0.0f));
147150
switch(current) {
148151
case screens::Home:
149152
draw_home();
@@ -163,8 +166,8 @@ void draw_button(string label, int index) {
163166
void draw_home() {
164167
ImGuiIO& io = ImGui::GetIO();
165168
if (!profiles.empty() && !reconfiguring) {
166-
for (int i = 1; i <= (profiles[0].columns * profiles[0].rows); i++) {
167-
ImGui::BeginDisabled(profiles[0].buttons[i-1].action == "");
169+
for (int i = 1; i <= (profiles[0].columns * profiles[0].rows); ++i) {
170+
ImGui::BeginDisabled(profiles[0].buttons[i-1].action.empty());
168171
if (profiles[0].buttons[i-1].default_label) draw_button(to_string(i), i);
169172
else if (profiles[0].buttons[i-1].label.empty()) draw_button("##", i);
170173
else draw_button(profiles[0].buttons[i-1].label, i);
@@ -179,12 +182,12 @@ void draw_home() {
179182
if (ImGui::BeginPopupModal("Reconfiguring", NULL, im_window_flags)) {
180183
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
181184
ImGui::SetWindowFontScale(2.0f);
182-
ImGui::Text("Reconfiguring...\nOlease Wait");
185+
ImGui::Text("Reconfiguring...\nPlease Wait");
183186
ImGui::EndPopup();
184187
}
185188
}
186189
ImGui::BeginDisabled();
187-
for (int i = 1; i <= 24; i++) {
190+
for (int i = 1; i <= 24; ++i) {
188191
ImGui::Button(to_string(i).c_str(), ImVec2((io.DisplaySize.x / 6), (io.DisplaySize.y / 4)));
189192
if (i % 6 != 0) ImGui::SameLine();
190193
}
@@ -197,8 +200,7 @@ screens get_default_screen() {
197200
return screens::Home;
198201
}
199202

200-
void draw_setup() {
201-
ImGuiIO& io = ImGui::GetIO();
203+
void set_popup_style() {
202204
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, default_style.ItemSpacing);
203205
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, default_style.ItemInnerSpacing);
204206
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, default_style.FramePadding);
@@ -207,45 +209,60 @@ void draw_setup() {
207209
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, default_style.ChildBorderSize);
208210
ImGui::PushStyleVar(ImGuiStyleVar_PopupBorderSize, default_style.PopupBorderSize);
209211
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, default_style.FrameBorderSize);
212+
}
213+
214+
void draw_killed() {
215+
set_popup_style();
216+
ImGui::OpenPopup("Killed");
217+
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
218+
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
219+
if (ImGui::BeginPopupModal("Killed", NULL, im_window_flags)) {
220+
ImGui::SetWindowFontScale(2.0f);
221+
ImGui::Text("Disconnected.");
222+
ImGui::Text(kill_reason.c_str());
223+
if (ImGui::Button("Reconnect")) {
224+
kill = false;
225+
disconnected_modal = true;
226+
}
227+
ImGui::EndPopup();
228+
}
229+
ImGui::PopStyleVar(8);
230+
}
231+
232+
void draw_setup() {
233+
set_popup_style();
210234
ImGui::OpenPopup("Setup", im_window_flags);
235+
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
236+
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
211237
if (ImGui::BeginPopupModal("Setup", NULL, im_window_flags)) {
212-
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
213238
ImGui::SetWindowFontScale(2.0f);
214239
ImGui::BeginDisabled(failed == failed_backup);
215240
ImGui::Text("Welcome to NexusShell!");
216241
ImGui::Separator();
217242
if (!hide_failed) {
218-
if (failed) ImGui::Text("Connection failed.");
243+
if (failed) ImGui::Text("Connection failed.");
219244
if (connected) run_setup = false;
220245
}
221246
ImGui::Text("Server IP address:");
222-
ImGui::InputText("##", ip_address, IM_ARRAYSIZE(ip_address));
247+
ImGui::InputText("##", ip_address, 16);
223248
if (ImGui::Button("Connect")) {
224249
failed = false;
225250
failed_backup = false;
226251
hide_failed = false;
227252
}
228253
ImGui::EndDisabled();
229-
// TODO: virtual_keyboard();
230254
ImGui::EndPopup();
231255
}
232256
ImGui::PopStyleVar(8);
233257
}
234258
// FIXME: Sluggish font
235259
void draw_disconnected_alert() {
236260
if (connected) disconnected_modal = false;
237-
ImGuiIO& io = ImGui::GetIO();
238-
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, default_style.ItemSpacing);
239-
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, default_style.ItemInnerSpacing);
240-
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, default_style.FramePadding);
241-
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, default_style.WindowPadding);
242-
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, default_style.WindowBorderSize);
243-
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, default_style.ChildBorderSize);
244-
ImGui::PushStyleVar(ImGuiStyleVar_PopupBorderSize, default_style.PopupBorderSize);
245-
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, default_style.FrameBorderSize);
261+
set_popup_style();
246262
ImGui::OpenPopup("Disconnected", im_window_flags);
263+
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
264+
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
247265
if (ImGui::BeginPopupModal("Disconnected", NULL, im_window_flags)) {
248-
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
249266
ImGui::SetWindowFontScale(2.0f);
250267
failed = false;
251268
ImGui::Text("Disconnected.");

0 commit comments

Comments
 (0)