Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include <memory> |
| 6 | |
| 7 | #include <GLFW/glfw3.h> |
| 8 | #include <fmt/format.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 9 | #include <glass/Context.h> |
| 10 | #include <glass/MainMenuBar.h> |
| 11 | #include <glass/Model.h> |
| 12 | #include <glass/Storage.h> |
| 13 | #include <glass/networktables/NetworkTables.h> |
| 14 | #include <glass/networktables/NetworkTablesSettings.h> |
| 15 | #include <glass/other/Log.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 16 | #include <imgui.h> |
| 17 | #include <ntcore_cpp.h> |
| 18 | #include <wpigui.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 19 | #include <wpigui_openurl.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | |
| 21 | namespace gui = wpi::gui; |
| 22 | |
| 23 | const char* GetWPILibVersion(); |
| 24 | |
| 25 | namespace ov { |
| 26 | std::string_view GetResource_ov_16_png(); |
| 27 | std::string_view GetResource_ov_32_png(); |
| 28 | std::string_view GetResource_ov_48_png(); |
| 29 | std::string_view GetResource_ov_64_png(); |
| 30 | std::string_view GetResource_ov_128_png(); |
| 31 | std::string_view GetResource_ov_256_png(); |
| 32 | std::string_view GetResource_ov_512_png(); |
| 33 | } // namespace ov |
| 34 | |
| 35 | static std::unique_ptr<glass::NetworkTablesModel> gModel; |
| 36 | static std::unique_ptr<glass::NetworkTablesSettings> gSettings; |
| 37 | static glass::LogData gLog; |
| 38 | static glass::NetworkTablesFlagsSettings gFlagsSettings; |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 39 | static glass::MainMenuBar gMainMenu; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 40 | static unsigned int gPrevMode = NT_NET_MODE_NONE; |
| 41 | |
| 42 | /** |
| 43 | * Generates the proper title bar title based on current instance state and |
| 44 | * event. |
| 45 | */ |
| 46 | static std::string MakeTitle(NT_Inst inst, nt::Event event) { |
| 47 | auto mode = nt::GetNetworkMode(inst); |
| 48 | if (mode & NT_NET_MODE_SERVER) { |
| 49 | auto numClients = nt::GetConnections(inst).size(); |
| 50 | return fmt::format("OutlineViewer - {} Client{} Connected", numClients, |
| 51 | (numClients == 1 ? "" : "s")); |
| 52 | } else if (mode & NT_NET_MODE_CLIENT3 || mode & NT_NET_MODE_CLIENT4) { |
| 53 | if (event.Is(NT_EVENT_CONNECTED)) { |
| 54 | return fmt::format("OutlineViewer - Connected ({})", |
| 55 | event.GetConnectionInfo()->remote_ip); |
| 56 | } |
| 57 | } |
| 58 | return "OutlineViewer - DISCONNECTED"; |
| 59 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 60 | |
| 61 | static void NtInitialize() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 62 | auto inst = nt::GetDefaultInstance(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 63 | auto poller = nt::CreateListenerPoller(inst); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 64 | nt::AddPolledListener(poller, inst, NT_EVENT_CONNECTION | NT_EVENT_IMMEDIATE); |
| 65 | nt::AddPolledLogger(poller, 0, 100); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | gui::AddEarlyExecute([inst, poller] { |
| 67 | auto win = gui::GetSystemWindow(); |
| 68 | if (!win) { |
| 69 | return; |
| 70 | } |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 71 | bool updateTitle = false; |
| 72 | nt::Event connectionEvent; |
| 73 | if (nt::GetNetworkMode(inst) != gPrevMode) { |
| 74 | gPrevMode = nt::GetNetworkMode(inst); |
| 75 | updateTitle = true; |
| 76 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 77 | for (auto&& event : nt::ReadListenerQueue(poller)) { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 78 | if (event.Is(NT_EVENT_CONNECTION)) { |
| 79 | updateTitle = true; |
| 80 | connectionEvent = event; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 81 | } else if (auto msg = event.GetLogMessage()) { |
| 82 | // handle NetworkTables log messages |
| 83 | const char* level = ""; |
| 84 | if (msg->level >= NT_LOG_CRITICAL) { |
| 85 | level = "CRITICAL: "; |
| 86 | } else if (msg->level >= NT_LOG_ERROR) { |
| 87 | level = "ERROR: "; |
| 88 | } else if (msg->level >= NT_LOG_WARNING) { |
| 89 | level = "WARNING: "; |
| 90 | } |
| 91 | gLog.Append(fmt::format("{}{} ({}:{})\n", level, msg->message, |
| 92 | msg->filename, msg->line)); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 95 | |
| 96 | if (updateTitle) { |
| 97 | glfwSetWindowTitle(win, MakeTitle(inst, connectionEvent).c_str()); |
| 98 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 99 | }); |
| 100 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 101 | // NetworkTables table window |
| 102 | gModel = std::make_unique<glass::NetworkTablesModel>(); |
| 103 | gui::AddEarlyExecute([] { gModel->Update(); }); |
| 104 | |
| 105 | // NetworkTables settings window |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 106 | gSettings = std::make_unique<glass::NetworkTablesSettings>( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 107 | "outlineviewer", |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 108 | glass::GetStorageRoot().GetChild("NetworkTables Settings")); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 109 | gui::AddEarlyExecute([] { gSettings->Update(); }); |
| 110 | } |
| 111 | |
| 112 | static void DisplayGui() { |
| 113 | ImGui::GetStyle().WindowRounding = 0; |
| 114 | |
| 115 | // fill entire OS window with this window |
| 116 | ImGui::SetNextWindowPos(ImVec2(0, 0)); |
| 117 | int width, height; |
| 118 | glfwGetWindowSize(gui::GetSystemWindow(), &width, &height); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 119 | ImGui::SetNextWindowSize( |
| 120 | ImVec2(static_cast<float>(width), static_cast<float>(height))); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 121 | |
| 122 | ImGui::Begin("Entries", nullptr, |
| 123 | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_MenuBar | |
| 124 | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | |
| 125 | ImGuiWindowFlags_NoCollapse); |
| 126 | |
| 127 | gFlagsSettings.Update(); |
| 128 | |
| 129 | // can't create popups from within menu, so use flags |
| 130 | bool settings = false; |
| 131 | bool log = false; |
| 132 | bool about = false; |
| 133 | |
| 134 | // main menu |
| 135 | ImGui::BeginMenuBar(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 136 | gMainMenu.WorkspaceMenu(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 137 | gui::EmitViewMenu(); |
| 138 | if (ImGui::BeginMenu("View")) { |
| 139 | gFlagsSettings.DisplayMenu(); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 140 | glass::DisplayNetworkTablesAddMenu(gModel.get()); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 141 | ImGui::EndMenu(); |
| 142 | } |
| 143 | |
| 144 | if (ImGui::BeginMenu("Options")) { |
| 145 | if (ImGui::MenuItem("Settings")) { |
| 146 | settings = true; |
| 147 | } |
| 148 | ImGui::EndMenu(); |
| 149 | } |
| 150 | |
| 151 | if (ImGui::BeginMenu("Info")) { |
| 152 | if (ImGui::MenuItem("Log")) { |
| 153 | log = true; |
| 154 | } |
| 155 | ImGui::Separator(); |
| 156 | if (ImGui::MenuItem("About")) { |
| 157 | about = true; |
| 158 | } |
| 159 | ImGui::EndMenu(); |
| 160 | } |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 161 | |
| 162 | if (ImGui::BeginMenu("Docs")) { |
| 163 | if (ImGui::MenuItem("Online documentation")) { |
| 164 | wpi::gui::OpenURL( |
| 165 | "https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/" |
| 166 | "outlineviewer/"); |
| 167 | } |
| 168 | ImGui::EndMenu(); |
| 169 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 170 | ImGui::EndMenuBar(); |
| 171 | |
| 172 | // settings popup |
| 173 | if (settings) { |
| 174 | ImGui::OpenPopup("Settings"); |
| 175 | } |
| 176 | if (ImGui::BeginPopupModal("Settings", nullptr, |
| 177 | ImGuiWindowFlags_AlwaysAutoResize)) { |
| 178 | if (gSettings->Display()) { |
| 179 | ImGui::CloseCurrentPopup(); |
| 180 | } |
| 181 | ImGui::SameLine(); |
| 182 | if (ImGui::Button("Cancel")) { |
| 183 | ImGui::CloseCurrentPopup(); |
| 184 | } |
| 185 | ImGui::EndPopup(); |
| 186 | } |
| 187 | |
| 188 | // log popup |
| 189 | if (log) { |
| 190 | ImGui::OpenPopup("Log"); |
| 191 | } |
| 192 | if (ImGui::BeginPopupModal("Log", nullptr, |
| 193 | ImGuiWindowFlags_AlwaysAutoResize)) { |
| 194 | if (ImGui::Button("Close")) { |
| 195 | ImGui::CloseCurrentPopup(); |
| 196 | } |
| 197 | ImGui::BeginChild("Lines", ImVec2(width * 0.75f, height * 0.75f)); |
| 198 | glass::DisplayLog(&gLog, true); |
| 199 | ImGui::EndChild(); |
| 200 | ImGui::EndPopup(); |
| 201 | } |
| 202 | |
| 203 | // about popup |
| 204 | if (about) { |
| 205 | ImGui::OpenPopup("About"); |
| 206 | } |
| 207 | if (ImGui::BeginPopupModal("About", nullptr, |
| 208 | ImGuiWindowFlags_AlwaysAutoResize)) { |
| 209 | ImGui::Text("OutlineViewer"); |
| 210 | ImGui::Separator(); |
| 211 | ImGui::Text("v%s", GetWPILibVersion()); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 212 | ImGui::Separator(); |
| 213 | ImGui::Text("Save location: %s", glass::GetStorageDir().c_str()); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 214 | ImGui::Text("%.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, |
| 215 | ImGui::GetIO().Framerate); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 216 | if (ImGui::Button("Close")) { |
| 217 | ImGui::CloseCurrentPopup(); |
| 218 | } |
| 219 | ImGui::EndPopup(); |
| 220 | } |
| 221 | |
| 222 | // display table view |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 223 | glass::DisplayNetworkTablesInfo(gModel.get()); |
| 224 | ImGui::Separator(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 225 | glass::DisplayNetworkTables(gModel.get(), gFlagsSettings.GetFlags()); |
| 226 | |
| 227 | ImGui::End(); |
| 228 | } |
| 229 | |
| 230 | #ifdef _WIN32 |
| 231 | int __stdcall WinMain(void* hInstance, void* hPrevInstance, char* pCmdLine, |
| 232 | int nCmdShow) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 233 | int argc = __argc; |
| 234 | char** argv = __argv; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 235 | #else |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 236 | int main(int argc, char** argv) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 237 | #endif |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 238 | std::string_view saveDir; |
| 239 | if (argc == 2) { |
| 240 | saveDir = argv[1]; |
| 241 | } |
| 242 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 243 | gui::CreateContext(); |
| 244 | glass::CreateContext(); |
| 245 | |
| 246 | gui::AddIcon(ov::GetResource_ov_16_png()); |
| 247 | gui::AddIcon(ov::GetResource_ov_32_png()); |
| 248 | gui::AddIcon(ov::GetResource_ov_48_png()); |
| 249 | gui::AddIcon(ov::GetResource_ov_64_png()); |
| 250 | gui::AddIcon(ov::GetResource_ov_128_png()); |
| 251 | gui::AddIcon(ov::GetResource_ov_256_png()); |
| 252 | gui::AddIcon(ov::GetResource_ov_512_png()); |
| 253 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 254 | glass::SetStorageName("outlineviewer"); |
| 255 | glass::SetStorageDir(saveDir.empty() ? gui::GetPlatformSaveFileDir() |
| 256 | : saveDir); |
| 257 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 258 | gui::AddInit(NtInitialize); |
| 259 | |
| 260 | gui::AddLateExecute(DisplayGui); |
| 261 | |
| 262 | gui::Initialize("OutlineViewer - DISCONNECTED", 600, 400); |
| 263 | gui::Main(); |
| 264 | |
| 265 | gModel.reset(); |
| 266 | gSettings.reset(); |
| 267 | |
| 268 | glass::DestroyContext(); |
| 269 | gui::DestroyContext(); |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 270 | |
| 271 | return 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 272 | } |