Skip to content

Commit 2d929d5

Browse files
committed
core: remove socket and ipc stuff
1 parent 721cb37 commit 2d929d5

7 files changed

Lines changed: 11 additions & 276 deletions

File tree

include/socket.hpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/clipboard.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "clip/clip.h"
88
#include "screen_capture.hpp"
9-
#include "socket.hpp"
109

1110
#ifdef __linux__
1211
# include <sys/wait.h>

src/main.cpp

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "oshot_png.h"
3232
#include "screen_capture.hpp"
3333
#include "screenshot_tool.hpp"
34-
#include "socket.hpp"
3534
#include "spdlog/sinks/basic_file_sink.h"
3635
#include "spdlog/sinks/stdout_color_sinks.h"
3736
#include "switch_fnv1a.hpp"
@@ -51,12 +50,6 @@ struct GLFWwindow;
5150
# pragma comment(lib, "legacy_stdio_definitions")
5251
#endif
5352

54-
#if (!__has_include("version.h"))
55-
# error "version.h not found, please generate it with ./scripts/generateVersion.sh"
56-
#else
57-
# include "version.h"
58-
#endif
59-
6053
// clang-format off
6154
// https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/
6255
// because "--opt-arg arg" won't work
@@ -357,11 +350,6 @@ int main(int argc, char* argv[])
357350
setenv("LD_LIBRARY_PATH", orig, 1);
358351
else
359352
unsetenv("LD_LIBRARY_PATH"); // not running from AppImage, clear any stale value
360-
361-
// Xlib is not thread-safe by default. We call it from both the render thread
362-
// and the IPC/clipboard thread; this enables Xlib's internal locking.
363-
// Must be called before glfwInit(), which opens a Display* immediately.
364-
XInitThreads();
365353
#endif
366354

367355
atexit(exit_handler_nc);
@@ -403,7 +391,6 @@ int main(int argc, char* argv[])
403391
const std::string& imgui_ini_path = configDir + "/imgui.ini";
404392

405393
g_clipboard = std::make_unique<Clipboard>(get_session_type());
406-
g_sender = std::make_unique<SocketSender>();
407394
g_config = std::make_unique<Config>(configFile, configDir);
408395
if (!parseargs(argc, argv, configFile))
409396
return EXIT_FAILURE;
@@ -443,107 +430,6 @@ int main(int argc, char* argv[])
443430
// AppKit), so capture_worker must not run, because it would call run_main_tool
444431
// from a background thread and crash with NSInternalInconsistencyException.
445432
std::thread worker(capture_worker, imgui_ini_path);
446-
447-
std::thread ipc([&] {
448-
while (!quit.load())
449-
{
450-
const int client = ::accept(g_sock, nullptr, nullptr);
451-
if (client < 0)
452-
{
453-
if (quit.load())
454-
break;
455-
continue;
456-
}
457-
458-
// Set receive timeout
459-
struct timeval tv;
460-
tv.tv_sec = 2;
461-
tv.tv_usec = 0;
462-
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
463-
464-
auto recv_all = [](int fd, void* dst, size_t n) {
465-
uint8_t* p = static_cast<uint8_t*>(dst);
466-
size_t remaining = n;
467-
while (remaining > 0)
468-
{
469-
const ssize_t r = ::recv(fd, p, remaining, 0);
470-
if (r <= 0)
471-
{
472-
if (r == 0)
473-
spdlog::debug("Connection closed by peer");
474-
else
475-
spdlog::debug("recv error: {}", strerror(errno));
476-
return false;
477-
}
478-
p += static_cast<size_t>(r);
479-
remaining -= static_cast<size_t>(r);
480-
}
481-
return true;
482-
};
483-
484-
char type = 0;
485-
uint32_t len = 0;
486-
487-
bool ok = recv_all(client, &type, 1) && recv_all(client, &len, sizeof(len));
488-
489-
if (ok)
490-
spdlog::debug("IPC received type={}, len={}", type, len);
491-
492-
std::vector<uint8_t> payload;
493-
if (ok && len > 0 && len < 100 * 1024 * 1024) // Sanity check: max 100MB
494-
{
495-
payload.resize(len);
496-
ok = recv_all(client, payload.data(), payload.size());
497-
}
498-
499-
close(client);
500-
501-
if (!ok)
502-
continue;
503-
504-
if (type == 'T')
505-
{
506-
std::string text(payload.begin(), payload.end());
507-
g_clipboard->CopyText(text);
508-
}
509-
else if (type == 'I')
510-
{
511-
if (payload.size() < 8)
512-
{
513-
spdlog::debug("IPC image payload too small: {}", payload.size());
514-
continue;
515-
}
516-
517-
int w = 0, h = 0;
518-
std::memcpy(&w, payload.data() + 0, 4);
519-
std::memcpy(&h, payload.data() + 4, 4);
520-
521-
spdlog::debug("IPC received image: {}x{}", w, h);
522-
523-
if (w <= 0 || h <= 0)
524-
continue;
525-
526-
const size_t expected = static_cast<size_t>(w) * h * 4;
527-
if (payload.size() != expected + 8)
528-
{
529-
spdlog::debug("IPC image size mismatch: got {}, expected {}", payload.size(), expected + 8);
530-
continue;
531-
}
532-
533-
// Strip the 8-byte header
534-
payload.erase(payload.begin(), payload.begin() + 8);
535-
capture_result_t cap{ std::move(payload), w, h };
536-
{
537-
std::lock_guard lk(mtx);
538-
pending_image = std::move(cap);
539-
do_copy_image = true;
540-
cv.notify_all();
541-
}
542-
}
543-
}
544-
close(g_sock);
545-
unlink(g_sock_path);
546-
});
547433
#endif
548434

549435
std::vector<TrayMenu*> menu;
@@ -596,8 +482,6 @@ int main(int argc, char* argv[])
596482
// Quitted the tray
597483
#if !OSHOT_TOOL_ON_MAIN_THREAD
598484
worker.join();
599-
if (ipc.joinable())
600-
ipc.join();
601485
#endif
602486

603487
return EXIT_SUCCESS;

src/main_tool_metal.mm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ int run_main_tool(const std::string& imgui_ini_path)
309309
glfwDestroyWindow(window);
310310
glfwTerminate();
311311

312-
g_sender->Close();
313-
314312
return EXIT_SUCCESS;
315313
}
316314

src/main_tool_opengl3.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# include "imgui/imgui_impl_opengl3.h"
66
# include "screen_capture.hpp"
77
# include "screenshot_tool.hpp"
8-
# include "socket.hpp"
98
# include "util.hpp"
109
# define GL_SILENCE_DEPRECATION
1110
# if defined(IMGUI_IMPL_OPENGL_ES2)
@@ -298,10 +297,7 @@ int run_main_tool(const std::string& imgui_ini_path)
298297
window = nullptr;
299298

300299
if (!g_is_systray)
301-
{
302300
glfwTerminate();
303-
g_sender->Close();
304-
}
305301

306302
return EXIT_SUCCESS;
307303
}

src/socket.cpp

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/util.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "fmt/format.h"
1717
#include "screen_capture.hpp"
1818
#include "screenshot_tool.hpp"
19-
#include "socket.hpp"
2019
#include "tinyfiledialogs.h"
2120

2221
#define SVPNG_LINKAGE inline
@@ -61,6 +60,7 @@
6160
# include <fcntl.h>
6261
# include <sys/select.h>
6362
# include <sys/socket.h>
63+
# include <sys/file.h>
6464
# include <unistd.h>
6565
# include <sys/un.h>
6666
#endif
@@ -155,34 +155,23 @@ fs::path get_runtime_dir()
155155

156156
bool acquire_tray_lock()
157157
{
158-
// we are the "client" (not systray)
159-
if (g_sender->Start().ok())
158+
std::string lock_path = (get_runtime_dir() / "oshot.lock").string();
159+
int fd = open(lock_path.c_str(), O_CREAT | O_RDWR, 0600);
160+
if (fd < 0)
160161
return false;
161162

162-
g_sock = socket(AF_UNIX, SOCK_STREAM, 0);
163-
if (g_sock < 0)
164-
return false;
165-
166-
sockaddr_un addr{};
167-
addr.sun_family = AF_UNIX;
168-
169-
// Write 99 bytes at most to `addr.sun_path`, with the path to `oshot.sock`.
170-
strncpy(addr.sun_path, (get_runtime_dir() / "oshot.sock").c_str(), 99);
171-
// ensure null-termination
172-
addr.sun_path[99] = '\0';
173-
174-
strncpy(g_sock_path, addr.sun_path, 100);
163+
struct flock fl{};
164+
fl.l_type = F_WRLCK;
165+
fl.l_whence = SEEK_SET;
175166

176-
unlink(addr.sun_path); // remove stale socket
177-
if (bind(g_sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0)
167+
if (fcntl(fd, F_SETLK, &fl) == -1)
178168
{
179-
close(g_sock);
180-
g_sock = -1;
169+
// Another instance holds the lock
170+
close(fd);
181171
return false;
182172
}
183173

184-
fcntl(g_sock, F_SETFD, FD_CLOEXEC);
185-
listen(g_sock, 1);
174+
// We hold the lock. it's released automatically when the process exits
186175
return true;
187176
}
188177

0 commit comments

Comments
 (0)