Skip to content

Commit 33ae2be

Browse files
committed
misc: some coding stuff
1 parent a640ae2 commit 33ae2be

3 files changed

Lines changed: 52 additions & 60 deletions

File tree

include/util.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "fmt/base.h"
1414
#include "fmt/color.h"
1515
#include "spdlog/spdlog.h"
16+
#include "version.h"
1617

1718
namespace fs = std::filesystem;
1819
enum class SavingOp;
@@ -144,6 +145,18 @@ extern int g_sock;
144145
extern char g_sock_path[100];
145146
extern int g_scr_w, g_scr_h;
146147

148+
static inline const std::string version_infos = fmt::format(
149+
"oshot v{} built from branch '{}' at {} commit '{}' ({}).\n"
150+
"Date: {}\n"
151+
"Tag: {}\n",
152+
VERSION,
153+
GIT_BRANCH,
154+
GIT_DIRTY,
155+
GIT_COMMIT_HASH,
156+
GIT_COMMIT_MESSAGE,
157+
GIT_COMMIT_DATE,
158+
GIT_TAG);
159+
147160
#ifdef __linux__
148161
std::vector<uint8_t> ximage_to_rgba(XImage* image, int width, int height);
149162
#endif

src/main.cpp

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939
using namespace Tray;
4040

41+
// Avoid dragging glfw headers
42+
struct GLFWwindow;
43+
4144
// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and
4245
// compatibility with old VS compilers. To link with VS2010-era libraries, VS2015+ requires linking with
4346
// legacy_stdio_definitions.lib, which we do using this pragma. Your own project should not be affected, as you are
@@ -74,19 +77,8 @@ std::error_code ec;
7477
// Print the version and some other infos, then exit successfully
7578
static void version()
7679
{
77-
fmt::print(
78-
"oshot v{} built from branch '{}' at {} commit '{}' ({}).\n"
79-
"Date: {}\n"
80-
"Tag: {}\n",
81-
VERSION,
82-
GIT_BRANCH,
83-
GIT_DIRTY,
84-
GIT_COMMIT_HASH,
85-
GIT_COMMIT_MESSAGE,
86-
GIT_COMMIT_DATE,
87-
GIT_TAG);
88-
89-
// if only everyone would not return error when querying the program version :(
80+
fmt::print(FMT_COMPILE("{}"), version_infos);
81+
fmt::print("\n");
9082
std::exit(EXIT_SUCCESS);
9183
}
9284

@@ -98,22 +90,6 @@ static void help(bool invalid_opt = false)
9890
std::exit(invalid_opt);
9991
}
10092

101-
#ifndef _WIN32
102-
static bool recv_all(int fd, void* dst, size_t n)
103-
{
104-
uint8_t* p = static_cast<uint8_t*>(dst);
105-
while (n)
106-
{
107-
const ssize_t r = ::recv(fd, p, n, 0);
108-
if (r <= 0)
109-
return false;
110-
p += static_cast<size_t>(r);
111-
n -= static_cast<size_t>(r);
112-
}
113-
return true;
114-
}
115-
#endif
116-
11793
// clang-format off
11894
// parseargs() but only for parsing the user config path trough args
11995
// and so we can directly construct Config
@@ -218,11 +194,6 @@ static bool do_capture = false;
218194
static capture_result_t pending_image;
219195
static bool do_copy_image = false;
220196

221-
struct GLFWwindow;
222-
223-
// Avoid dragging glfw headers
224-
void extern_glfwTerminate();
225-
226197
void exit_handler(int _)
227198
{
228199
quit.store(true);
@@ -365,12 +336,20 @@ int main(int argc, char* argv[])
365336
#endif
366337

367338
#ifdef __linux__
339+
// AppRun prepends bundled libs to LD_LIBRARY_PATH so the AppImage is self-contained.
340+
// Child processes (zenity, grim, etc.) inherit it and resolve against the bundled
341+
// (older) libs instead of the host's, causing symbol version mismatches.
342+
// AppRun saves the original value here, restore it so that spawned system binaries
343+
// use the host libs.
368344
const char* orig = std::getenv("APPIMAGE_ORIG_LD_LIBRARY_PATH");
369345
if (orig)
370346
setenv("LD_LIBRARY_PATH", orig, 1);
371347
else
372-
unsetenv("LD_LIBRARY_PATH");
348+
unsetenv("LD_LIBRARY_PATH"); // not running from AppImage, clear any stale value
373349

350+
// Xlib is not thread-safe by default. We call it from both the render thread
351+
// and the IPC/clipboard thread; this enables Xlib's internal locking.
352+
// Must be called before glfwInit(), which opens a Display* immediately.
374353
XInitThreads();
375354
#endif
376355

@@ -455,6 +434,19 @@ int main(int argc, char* argv[])
455434
continue;
456435
}
457436

437+
auto recv_all = [](int fd, void* dst, size_t n) {
438+
uint8_t* p = static_cast<uint8_t*>(dst);
439+
while (n)
440+
{
441+
const ssize_t r = ::recv(fd, p, n, 0);
442+
if (r <= 0)
443+
return false;
444+
p += static_cast<size_t>(r);
445+
n -= static_cast<size_t>(r);
446+
}
447+
return true;
448+
};
449+
458450
char type = 0;
459451
uint32_t len = 0;
460452

src/screenshot_tool.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ void ScreenshotTool::DrawMenuItems()
10571057
ImGui::Text("%s", text_display.data());
10581058
ImGui::Spacing();
10591059

1060-
text_display = centered_text("Screenshot tool to extract text on the fly");
1060+
text_display = centered_text("Screenshot tool for extracting text on the fly");
10611061
ImGui::Text("%s", text_display.data());
10621062
ImGui::Spacing();
10631063

@@ -1067,22 +1067,10 @@ void ScreenshotTool::DrawMenuItems()
10671067
{
10681068
ImGui::BeginChild("##scrollable_region", ImVec2(0, 100), false, ImGuiWindowFlags_HorizontalScrollbar);
10691069

1070-
static std::string infos = fmt::format(
1071-
"oshot v{} built from branch '{}' at {} commit '{}' ({}).\n"
1072-
"Date: {}\n"
1073-
"Tag: {}\n",
1074-
VERSION,
1075-
GIT_BRANCH,
1076-
GIT_DIRTY,
1077-
GIT_COMMIT_HASH,
1078-
GIT_COMMIT_MESSAGE,
1079-
GIT_COMMIT_DATE,
1080-
GIT_TAG);
1081-
1082-
ImGui::Text("%s", infos.c_str());
1070+
ImGui::Text("%s", version_infos.c_str());
10831071

10841072
if (ImGui::Button("Copy text"))
1085-
g_clipboard->CopyText(infos);
1073+
g_clipboard->CopyText(version_infos);
10861074
ImGui::EndChild();
10871075
ImGui::TreePop();
10881076
}
@@ -1134,7 +1122,6 @@ void ScreenshotTool::DrawOcrTools()
11341122

11351123
const bool invalid_path = HasError(InvalidPath);
11361124
const bool invalid_model = HasError(InvalidModel);
1137-
const bool has_errors = invalid_path || invalid_model;
11381125

11391126
// --- Path input ---
11401127
push_error_style(invalid_path);
@@ -1178,7 +1165,7 @@ void ScreenshotTool::DrawOcrTools()
11781165
}
11791166

11801167
// --- Extract button + result details ---
1181-
if (!has_errors)
1168+
if (!invalid_path && !invalid_model)
11821169
{
11831170
if (ImGui::Button("Extract Text"))
11841171
{
@@ -1205,21 +1192,21 @@ void ScreenshotTool::DrawOcrTools()
12051192
HelpMarker("If results seem off, try Edit > Optimize OCR for...");
12061193
}
12071194

1208-
const auto& results = m_inputs.ocr_results;
1209-
if (results.confidence > 0 && ImGui::TreeNode("Details"))
1195+
if (m_inputs.ocr_results.confidence > 0 && ImGui::TreeNode("Details"))
12101196
{
1211-
ImGui::Text("Confidence:");
1212-
ImGui::SameLine();
1213-
ImGui::TextColored(get_confidence_color(results.confidence), "%d%%", results.confidence);
1197+
ImGui::BulletText("Confidence:");
12141198
ImGui::SameLine();
1215-
ImGui::Text(" PSM: %s", results.psm_str.c_str());
1199+
ImGui::TextColored(
1200+
get_confidence_color(m_inputs.ocr_results.confidence), "%d%%", m_inputs.ocr_results.confidence);
1201+
1202+
ImGui::BulletText("PSM: %s", m_inputs.ocr_results.psm_str.c_str());
12161203
ImGui::TreePop();
12171204
}
12181205
}
12191206

12201207
ImGui::InputTextMultiline("##source",
12211208
&m_inputs.ocr_results.data,
1222-
ImVec2(-1, ImGui::GetTextLineHeight() * 10),
1209+
ImVec2(-1, ImGui::GetTextLineHeight() * 8),
12231210
g_config->File.allow_out_edit ? 0 : ImGuiInputTextFlags_ReadOnly);
12241211

12251212
if (!m_inputs.ocr_results.data.empty())
@@ -1265,7 +1252,7 @@ void ScreenshotTool::DrawBarDecodeTools()
12651252

12661253
ImGui::InputTextMultiline("##barcode",
12671254
&m_inputs.barcode_text,
1268-
ImVec2(-1, ImGui::GetTextLineHeight() * 10),
1255+
ImVec2(-1, ImGui::GetTextLineHeight() * 8),
12691256
g_config->File.allow_out_edit ? 0 : ImGuiInputTextFlags_ReadOnly);
12701257

12711258
if (!m_inputs.barcode_text.empty())

0 commit comments

Comments
 (0)