Skip to content

Commit

Permalink
Move pipe code out into own source file
Browse files Browse the repository at this point in the history
  • Loading branch information
rollerozxa committed Jun 22, 2024
1 parent 125432b commit 8773e54
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 204 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ elseif(EMSCRIPTEN)
set(BACKEND_SRC main_emscripten.cc)
else()
set(BACKEND_SRC main.cc)
set(SRCS ${SRCS} src/tms/backend/pipe.cc)
endif()

set(SRCS ${SRCS} src/tms/backend/${BACKEND_SRC})
Expand Down
215 changes: 11 additions & 204 deletions src/tms/backend/main.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Unified backend used for most platforms (Linux, Windows...)

#include <errno.h>
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
Expand All @@ -21,23 +21,17 @@
#include "version.hh"

#ifdef TMS_BACKEND_WINDOWS

#include <windows.h>
#include <windowsx.h>

#include <clocale>
#include "shlwapi.h"

#define SDL_WINDOW_FULLSCREEN_DESKTOP (SDL_WINDOW_FULLSCREEN | 0x00001000)

#include "SDL_syswm.h"

#include <windows.h>
#include <windowsx.h>
#include <clocale>
#include "shlwapi.h"
#include "SDL_syswm.h"
#else

#include <pwd.h>

#include <pwd.h>
#endif

#include "pipe.hh"

FILE *_f_out = stdout;

static int _storage_type = 0;
Expand All @@ -53,77 +47,6 @@ static int T_intercept_input(SDL_Event ev);
extern "C" int tbackend_init_surface();
extern "C" const char *tbackend_get_storage_path(void);

#ifdef TMS_BACKEND_WINDOWS
char *_tmp[]={0,0};
static HANDLE pipe_h;
static uint8_t buf[512];
#else
static char *_args[2] = {0,0};
static int pipe_h;
static char buf[1024];
#endif

int _pipe_listener(void *p)
{
#ifdef TMS_BACKEND_WINDOWS
DWORD num_read;

while (ConnectNamedPipe(pipe_h, 0) || GetLastError() == ERROR_PIPE_CONNECTED) {
tms_infof("Client connected, reading...");

if (ReadFile(pipe_h, buf, 511, &num_read, 0)) {
tms_infof("read %u bytes:", num_read);
buf[num_read] = '\0';
tms_infof("%s", buf);

_tmp[1] = (char*)buf;

tproject_set_args(2, _tmp);
} else
tms_infof("error reading from pipe: %d", GetLastError());

FlushFileBuffers(pipe_h);
DisconnectNamedPipe(pipe_h);
}

tms_infof("ConnectNamedPipe returned false, quitting");

CloseHandle(pipe_h);

return T_OK;

#elif defined(TMS_BACKEND_HAIKU)

// Unimplemented

#else

ssize_t sz;

while (1) {
tms_infof("attempting to open /tmp/principia.run O_RDONLY");
while ((pipe_h = open("/tmp/principia.run", O_RDONLY)) == -1) {
if (errno != EINTR)
return 1;
}

while ((sz = read(pipe_h, buf, 1023)) > 0) {
//tms_infof("read %d bytes", sz);

if (sz > 0) {
buf[sz] = '\0';
_args[1] = buf;
tproject_set_args(2, _args);
}
}

close(pipe_h);
}

tms_infof("Pipe listener EXITING");
#endif
}

static void _catch_signal(int signal)
{
tms_errorf("Segmentation fault!");
Expand Down Expand Up @@ -156,11 +79,7 @@ void print_log_header() {
"Version %d, built " __DATE__ " " __TIME__ "\n", PRINCIPIA_VERSION_CODE);
}

#ifdef TMS_BACKEND_WINDOWS
int CALLBACK WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
#else
int main(int argc, char **argv)
#endif
{
SDL_Event ev;
int done = 0;
Expand All @@ -169,116 +88,10 @@ int main(int argc, char **argv)

#ifdef TMS_BACKEND_WINDOWS
setlocale(LC_ALL, "C");

pipe_h = CreateNamedPipe(
L"\\\\.\\pipe\\principia-process",
PIPE_ACCESS_INBOUND,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1,
128,
128,
0,
0
);

if (pipe_h == INVALID_HANDLE_VALUE) {
/* could not create named pipe */
tms_infof("Forwarding arguments through pipe...");

while (1) {
pipe_h = CreateFile(
L"\\\\.\\pipe\\principia-process",
GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0
);

if (pipe_h != INVALID_HANDLE_VALUE)
break;

if (GetLastError() != ERROR_PIPE_BUSY) {
tms_errorf("error opening pipe");
exit(1);
}

tms_infof("Waiting for pipe...");
if (!WaitNamedPipe((LPCWSTR)pipe_h, 3000))
tms_errorf("Failed, waited too long.");
}

DWORD dwMode = PIPE_READMODE_MESSAGE;
SetNamedPipeHandleState(
pipe_h,
&dwMode,
0,0);

int len = strlen(cl);
DWORD written;
if (!(WriteFile(pipe_h, cl, len, &written, 0))) {
tms_errorf("error writing to pipe");
}

tms_infof("done");
CloseHandle(pipe_h);

/* bring the window to the front */
HWND h = FindWindow(NULL, L"Principia");
SetForegroundWindow(h);
exit(0);
} else {
/* we've created the named pipe */
tms_infof("Created named pipe, starting listener thread.");
SDL_CreateThread(_pipe_listener, "_pipe_listener", 0);
}

#elif defined(TMS_BACKEND_HAIKU)

// Unimplemented


#else
int status = mkfifo("/tmp/principia.run", S_IWUSR | S_IRUSR);
int skip_pipe = 0;

if (status == 0) {
tms_infof("Created fifo");
} else {
if (errno != EEXIST) {
tms_errorf("could not create fifo pipe!");
skip_pipe = 1;
}
}

if (!skip_pipe) {
if ((pipe_h = open("/tmp/principia.run", O_WRONLY | O_NONBLOCK)) == -1) {
if (errno != ENXIO) {
skip_pipe = 1;
tms_infof("error: %s", strerror(errno));
}
} else {
if (argc > 1) {
/* open the fifo for writing instead */
tms_infof("sending arg: %s", argv[1]);

write(pipe_h, argv[1], strlen(argv[1]));
} else {
tms_infof("principia already running");
}

close(pipe_h);
exit(0);
}
}

if (!skip_pipe) {
tms_infof("Starting fifo listener thread");
SDL_CreateThread(_pipe_listener, "_pipe_listener", 0);
}
#endif

setup_pipe(argc, argv);

char* exedir = SDL_GetBasePath();
tms_infof("chdirring to %s", exedir);
chdir(exedir);
Expand Down Expand Up @@ -356,12 +169,7 @@ int main(int argc, char **argv)

tms_infof("set initial res to %dx%d", _tms.window_width, _tms.window_height);

#ifdef TMS_BACKEND_WINDOWS
_tmp[1] = cl;
tproject_set_args(2, _tmp);
#else
tproject_set_args(argc, argv);
#endif
tms_init();

#ifdef TMS_BACKEND_WINDOWS
Expand Down Expand Up @@ -597,7 +405,6 @@ tbackend_init_surface()

tms_infof("GL Info: %s/%s/%s", glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_VERSION));
tms_infof("GLSL Version: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
/*tms_debugf("Extensions: %s", glGetString(GL_EXTENSIONS));*/

tms_printf("GL versions supported: ");
if (GLEW_VERSION_4_6) tms_printf("4.6,");
Expand Down
Loading

0 comments on commit 8773e54

Please sign in to comment.