3 #include "backends/imgui_impl_sdl.h"
4 #include "backends/imgui_impl_opengl3.h"
8 #if defined(IMGUI_IMPL_OPENGL_ES2)
9 #include <SDL_opengles2.h>
11 #include <SDL2/SDL_opengl.h>
16 #include <spdlog/spdlog.h>
32 #define MAX_EVENTS_PER_FRAME 10
33 #define MAX_CONSEC_EVENT_HANDLE_FAILURES 3
35 #define DEFAULT_MAIN_CFG_PATH "./app_config.conf"
43 spdlog::critical(
"main.cpp - Failed to load main config. Perhaps path {0} is wrong?", val);
48 spdlog::critical(
"main.cpp - Failed to load main config through default path. Please either provide path"
57 int main(
int argc,
char** argv) {
59 spdlog::critical(
"main.cpp - Failed to parse arguments and prepare basic program settings. Exiting...");
64 spdlog::set_level(spdlog::level::debug);
69 spdlog::error(
"main.cpp - Failed to load optional config file");
83 spdlog::critical(
"Please make sure language options are set correctly in main configuration file. Exiting...");
88 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
89 spdlog::critical(
"main.cpp - Failed to init SDL. Error: %s\n", SDL_GetError());
94 #if defined(IMGUI_IMPL_OPENGL_ES2)
96 const char* glsl_version =
"#version 100";
97 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
98 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
99 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
100 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
101 #elif defined(__APPLE__)
103 const char* glsl_version =
"#version 150";
104 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
105 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
106 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
107 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
110 const char* glsl_version =
"#version 130";
111 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
112 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
113 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
118 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
119 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
120 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
121 SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI);
123 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 480, 640, window_flags);
124 SDL_GLContext gl_context = SDL_GL_CreateContext(window);
125 SDL_GL_MakeCurrent(window, gl_context);
126 SDL_GL_SetSwapInterval(1);
129 IMGUI_CHECKVERSION();
130 ImGui::CreateContext();
131 ImGuiIO& io = ImGui::GetIO(); (void)io;
132 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
134 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
135 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
142 ImGui::StyleColorsLight();
145 ImGuiStyle& style = ImGui::GetStyle();
146 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
147 style.WindowRounding = 0.0f;
148 style.Colors[ImGuiCol_WindowBg].w = 1.0f;
150 style.FrameBorderSize = 1.0f;
153 ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
154 ImGui_ImplOpenGL3_Init(glsl_version);
158 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
159 bool main_window_open =
true;
160 ImGuiWindowFlags main_window_flags = 0;
162 main_window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
163 ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoMouseInputs;
168 std::thread keyboard_thread, rfid_thread;
171 app_wrk->set_scr_mgr(scr_mgr.get());
175 spdlog::error(
"Failed to lod font (this is already logged from load_font_size()");
179 spdlog::error(
"Failed to lod font (this is already logged from load_font_size()");
183 spdlog::error(
"Failed to lod font (this is already logged from load_font_size()");
188 rv = app_wrk->init_hx711_controller();
190 spdlog::critical(
"main.cpp - Failed to start HX711.");
191 if (app_wrk->main_config->init_fail_quit)
197 spdlog::critical(
"main.cpp - SPI keyboard failed to initialize. Unless another keyboard"
198 " is connected, you won't be able to control the application.");
199 if (app_wrk->main_config->init_fail_quit)
return 1;
205 spdlog::critical(
"main.cpp - SPI keyboard failed to initialize. Unless another keyboard"
206 " is connected, you won't be able to control the application.");
207 if (app_wrk->main_config->init_fail_quit)
return 1;
213 spdlog::critical(
"main.cpp - Failed to open DB connection or initialize DB driver.");
233 while (SDL_PollEvent(&event)) {
239 ImGui_ImplSDL2_ProcessEvent(&event);
241 if (event.type == SDL_QUIT)
243 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
244 event.window.windowID == SDL_GetWindowID(window))
249 ImGui_ImplOpenGL3_NewFrame();
250 ImGui_ImplSDL2_NewFrame();
255 bool use_work_area =
true;
257 const ImGuiViewport* viewport = ImGui::GetMainViewport();
258 ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos);
259 ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size);
261 ImGui::Begin(
get_localized_text(
"FRAME_TITLE"), &main_window_open, main_window_flags);
263 ImGui::PushStyleColor(ImGuiCol_NavHighlight, (ImVec4) ImColor(50, 50, 255, 255));
264 ImGui::PushStyleColor(ImGuiCol_FrameBg, (ImVec4) ImColor(230, 230, 230, 255));
265 ImGui::PushStyleColor(ImGuiCol_Border, (ImVec4) ImColor(50, 50, 50, 255));
268 app_wrk->get_scr_mgr()->render_active_screen();
271 while (app_wrk->font_push_cnt > 0) {
273 app_wrk->font_push_cnt--;
276 ImGui::PopStyleColor(3);
283 glViewport(0, 0, (
int)io.DisplaySize.x, (
int)io.DisplaySize.y);
284 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
285 glClear(GL_COLOR_BUFFER_BIT);
286 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
289 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
290 SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
291 SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
292 ImGui::UpdatePlatformWindows();
293 ImGui::RenderPlatformWindowsDefault();
294 SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
297 SDL_GL_SwapWindow(window);
301 ImGui_ImplOpenGL3_Shutdown();
302 ImGui_ImplSDL2_Shutdown();
303 ImGui::DestroyContext();
305 SDL_GL_DeleteContext(gl_context);
306 SDL_DestroyWindow(window);
312 if (keyboard_thread.joinable())
313 keyboard_thread.detach();
314 if (rfid_thread.joinable())
315 rfid_thread.detach();
318 std::this_thread::sleep_for(std::chrono::milliseconds(200));
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
const char * get_localized_text(const char *key)
Get the localized text object.
int load_default_dict()
Loads default language dictionary.
int main(int argc, char **argv)
#define DEFAULT_MAIN_CFG_PATH
int parse_args_and_prepare(int argc, char **argv)
int load_main_config_ini(const char *cfg_path)
This funciton loads main configuration. _ini symobolizes that the expected format is ....
int load_opt_config(const char *cfg_path)
Loads the optional config.
int handle_rfid_event(rfid_event *event)
Function handles RFID event.
int handle_raw_event(kb_event *raw_event)
Offers option for cutom event handling. If not desired only created SDL event.
int handle_sdl_event(const SDL_Event *event)
Handles SDL2 library event.
void start_capturing_events()
Infinite loop that reads SPI keyboard. Runs in it's own thread!
void clean()
Stops event capturing and closes SPI.
int init_from_conf()
Initializes my_spi with config loaded from config file into app_workspace. Starts SPI.
kb_event * poll_event()
Polls event from queue into param event Returns 1 on success because it is inteded to return "true" o...
int init_from_conf()
RFID reader init from loaded configuration.
void clean()
Stops the reading thread.
rfid_event * poll_event()
Polls event from RFID event_queue.
void start_reading_cards()
Loop that is run in its own thread, reading the RFID reader.
Structure of SPI keyboard event, Contains pressed scancode, flags and timestamp of event creation.
Structure used for RFID event. This structure contains read tag and flags of the event.