Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrapak0 committed Nov 25, 2023
2 parents d895b16 + 82bc51d commit 5129f00
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 50 deletions.
3 changes: 3 additions & 0 deletions Client/include/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ extern int send_result;
extern int bytes_received;

extern bool connected;
extern bool kill;

extern string kill_reason;

extern int client_init();

Expand Down
20 changes: 9 additions & 11 deletions Client/include/GUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,20 @@

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

int gui_init();
extern int gui_init();

ImVec4* set_colors();
ImGuiStyle& set_style();
extern ImVec4* set_colors();
extern ImGuiStyle& set_style();

void draw_main(screens current);
extern void draw_main(screens current);

void countdown_reconnect();

void draw_home();
void draw_loading();
void draw_setup();
void draw_disconnected_alert();
extern void draw_home();
extern void draw_killed();
extern void draw_setup();
extern void draw_disconnected_alert();

screens get_default_screen();

#ifdef _DEBUG
void draw_performance();
#endif
#endif
5 changes: 1 addition & 4 deletions Client/include/Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <iostream>
#include <thread>
#include <chrono>
#include <string>
#include <vector>

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

using std::to_string;

extern bool failed;
extern bool failed; // TODO: Move to specific headers
extern bool failed_backup;
extern bool has_commsock;
extern bool has_confsock;
extern bool done;
extern bool disconnected_modal;

Expand Down
20 changes: 14 additions & 6 deletions Client/src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ struct timeval timeout;
bool failed = true;
bool connected = false;
bool disconnected_modal = false;
bool kill = false;

int sock;
int bytesReceived;
int send_result;

char ip_address[16];

string kill_reason;

json config;

int client_init() {
const string pad_id = rw_UUID();
const string ID = rw_UUID();

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

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

disconnected_modal = false;
string message;
char* buf = (char*)malloc(sizeof(char) * (1024 * 256));
send_result = send(sock, pad_id.c_str(), pad_id.size() + 1, 0);
char* buf = new char[1024 * 256];
send_result = send(sock, ID.c_str(), ID.length() + 1, 0);
do {
memset(buf, 0, 1024 * 256);
bytesReceived = recv(sock, buf, 1024 * 256, 0);
Expand All @@ -64,12 +67,17 @@ int client_init() {
else {
cerr << message << "\n";
if (message.substr(0, 3) == "cfg") write_config(message.substr(3, message.length()));
else if (message.substr(0, 4) == "kill") {
kill = true;
kill_reason = message.substr(4, message.length());
}
}
} while (bytesReceived > 0 || send_result > 0 || !done);
free(buf);
delete[] buf;
close(sock);
connected = false;
disconnected_modal = true;
if (!kill) disconnected_modal = true;
failed = true;
cerr << "------------REBOOTING SOCK------------\n";
if (!done) return client_init();
return 0;
Expand Down
11 changes: 6 additions & 5 deletions Client/src/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../include/Config.h"
#include "../include/Header.h"
#include "../include/Client.h"

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

Expand Down Expand Up @@ -55,7 +56,7 @@ bool rw_ipstore() {
ip_reader.close();
if (!read_ip.empty()) {
strncpy(ip_address, read_ip.c_str(), read_ip.length());
failed = false;
if (!kill) failed = false;
return true;
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ void clear_config() {
ofstream writer(nxsh_config);
profiles.clear();
profile profile1;
for (int i = 1; i <= profile1.columns * profile1.rows; i++) {
for (int i = 1; i <= profile1.columns * profile1.rows; ++i) {
button button1;
profile1.buttons.push_back(button1);
}
Expand Down Expand Up @@ -128,18 +129,18 @@ void set_properties() {
if (config[config.begin().key()].contains("profiles")) {
int profile_count = 0;
for (auto& profile : config[config.begin().key()]["profiles"]) if (profile.is_object()) profile_count++;
for (int i = 0; i < profile_count; i++) {
for (int i = 0; i < profile_count; ++i) {
profile profile1;
json profile_store = config[config.begin().key()]["profiles"][to_string(i)];
if (profile_store.contains("columns")) profile1.columns = std::stoi(profile_store["columns"].get<string>());
if (profile_store.contains("rows")) profile1.rows = std::stoi(profile_store["rows"].get<string>());
if (profile_store.contains("pages")) {
int page_count = 0;
for (auto& page : profile_store["pages"]) if (page.is_object()) page_count++;
for (int j = 0; j < page_count; j++) {
for (int j = 0; j < page_count; ++j) {
json page_store = profile_store["pages"][to_string(j)];
if (page_store.contains("buttons")) {
for (int k = 0; k < profile1.columns * profile1.rows; k++) {
for (int k = 0; k < profile1.columns * profile1.rows; ++k) {
button button1;
if (page_store["buttons"].contains(to_string(k))) {
json button_store = page_store["buttons"][to_string(k)];
Expand Down
65 changes: 41 additions & 24 deletions Client/src/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../include/Config.h"
#include "../include/Client.h"

// int current_profile = 0; // TODO:

bool done = false;
bool failed_backup;
bool run_setup;
Expand Down Expand Up @@ -63,8 +65,9 @@ int gui_init() {
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();

if (run_setup && !have_ip) draw_setup();
if (!connected && have_ip || disconnected_modal) draw_disconnected_alert();
if (run_setup && !have_ip) draw_setup();
if ((!connected && have_ip) && !kill || disconnected_modal) draw_disconnected_alert();
if (kill && !connected) draw_killed();

im_window_flags |= ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_NoDocking
Expand Down Expand Up @@ -141,9 +144,9 @@ ImGuiStyle& set_style(/*style parameters*/) {

void draw_main(screens current) { // TODO: Create window in main to display tabs, close button etc.
ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x, io.DisplaySize.y));
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::Begin("main", NULL, im_window_flags | ImGuiWindowFlags_NoBringToFrontOnFocus); // Allow performance info to always be on top
ImGui::SetWindowSize(ImVec2(io.DisplaySize.x, io.DisplaySize.y));
ImGui::SetWindowPos(ImVec2(0.0f, 0.0f));
switch(current) {
case screens::Home:
draw_home();
Expand All @@ -163,8 +166,8 @@ void draw_button(string label, int index) {
void draw_home() {
ImGuiIO& io = ImGui::GetIO();
if (!profiles.empty() && !reconfiguring) {
for (int i = 1; i <= (profiles[0].columns * profiles[0].rows); i++) {
ImGui::BeginDisabled(profiles[0].buttons[i-1].action == "");
for (int i = 1; i <= (profiles[0].columns * profiles[0].rows); ++i) {
ImGui::BeginDisabled(profiles[0].buttons[i-1].action.empty());
if (profiles[0].buttons[i-1].default_label) draw_button(to_string(i), i);
else if (profiles[0].buttons[i-1].label.empty()) draw_button("##", i);
else draw_button(profiles[0].buttons[i-1].label, i);
Expand All @@ -179,12 +182,12 @@ void draw_home() {
if (ImGui::BeginPopupModal("Reconfiguring", NULL, im_window_flags)) {
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
ImGui::SetWindowFontScale(2.0f);
ImGui::Text("Reconfiguring...\nOlease Wait");
ImGui::Text("Reconfiguring...\nPlease Wait");
ImGui::EndPopup();
}
}
ImGui::BeginDisabled();
for (int i = 1; i <= 24; i++) {
for (int i = 1; i <= 24; ++i) {
ImGui::Button(to_string(i).c_str(), ImVec2((io.DisplaySize.x / 6), (io.DisplaySize.y / 4)));
if (i % 6 != 0) ImGui::SameLine();
}
Expand All @@ -197,8 +200,7 @@ screens get_default_screen() {
return screens::Home;
}

void draw_setup() {
ImGuiIO& io = ImGui::GetIO();
void set_popup_style() {
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, default_style.ItemSpacing);
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, default_style.ItemInnerSpacing);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, default_style.FramePadding);
Expand All @@ -207,45 +209,60 @@ void draw_setup() {
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, default_style.ChildBorderSize);
ImGui::PushStyleVar(ImGuiStyleVar_PopupBorderSize, default_style.PopupBorderSize);
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, default_style.FrameBorderSize);
}

void draw_killed() {
set_popup_style();
ImGui::OpenPopup("Killed");
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Killed", NULL, im_window_flags)) {
ImGui::SetWindowFontScale(2.0f);
ImGui::Text("Disconnected.");
ImGui::Text(kill_reason.c_str());
if (ImGui::Button("Reconnect")) {
kill = false;
disconnected_modal = true;
}
ImGui::EndPopup();
}
ImGui::PopStyleVar(8);
}

void draw_setup() {
set_popup_style();
ImGui::OpenPopup("Setup", im_window_flags);
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Setup", NULL, im_window_flags)) {
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
ImGui::SetWindowFontScale(2.0f);
ImGui::BeginDisabled(failed == failed_backup);
ImGui::Text("Welcome to NexusShell!");
ImGui::Separator();
if (!hide_failed) {
if (failed) ImGui::Text("Connection failed.");
if (failed) ImGui::Text("Connection failed.");
if (connected) run_setup = false;
}
ImGui::Text("Server IP address:");
ImGui::InputText("##", ip_address, IM_ARRAYSIZE(ip_address));
ImGui::InputText("##", ip_address, 16);
if (ImGui::Button("Connect")) {
failed = false;
failed_backup = false;
hide_failed = false;
}
ImGui::EndDisabled();
// TODO: virtual_keyboard();
ImGui::EndPopup();
}
ImGui::PopStyleVar(8);
}
// FIXME: Sluggish font
void draw_disconnected_alert() {
if (connected) disconnected_modal = false;
ImGuiIO& io = ImGui::GetIO();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, default_style.ItemSpacing);
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, default_style.ItemInnerSpacing);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, default_style.FramePadding);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, default_style.WindowPadding);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, default_style.WindowBorderSize);
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, default_style.ChildBorderSize);
ImGui::PushStyleVar(ImGuiStyleVar_PopupBorderSize, default_style.PopupBorderSize);
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, default_style.FrameBorderSize);
set_popup_style();
ImGui::OpenPopup("Disconnected", im_window_flags);
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Disconnected", NULL, im_window_flags)) {
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f));
ImGui::SetWindowFontScale(2.0f);
failed = false;
ImGui::Text("Disconnected.");
Expand Down

0 comments on commit 5129f00

Please sign in to comment.