diff --git a/binding-mri/niko-binding.cpp b/binding-mri/niko-binding.cpp index bc21751..b3f70e4 100644 --- a/binding-mri/niko-binding.cpp +++ b/binding-mri/niko-binding.cpp @@ -1,3 +1,4 @@ +#include "journal-binding.h" #include "binding-util.h" #include "binding-types.h" #include "sharedstate.h" @@ -25,6 +26,36 @@ namespace syswm { #define NIKO_X (320 - 16) #define NIKO_Y ((13 * 16) * 2) +RB_METHOD(nikoPrepare) +{ + // Prime native window info + syswm::SDL_SysWMinfo syswindow; + SDL_VERSION(&syswindow.version); + SDL_GetWindowWMInfo(shState->rtData().window, &syswindow); + +#ifdef LINUX + char path[PATH_MAX]; + std::string journal; + + // Get current path + getcwd(path, sizeof(path)); + + #ifdef OS_OSX + journal = std::string(path) + std::string("/_______.app/Contents/MacOS/_______"); + #else + journal = std::string(path) + std::string("_______"); + #endif + + // Run the binary + pid_t pid = fork(); + if (pid == 0) { + execl(journal.c_str(), journal.c_str(), "niko", (char*)0); + exit(1); + } +#endif + return Qnil; +} + RB_METHOD(nikoStart) { RB_UNUSED_PARAM; @@ -58,47 +89,32 @@ RB_METHOD(nikoStart) SDL_GetWindowPosition(shState->rtData().window, &x, &y); x += NIKO_X; y += NIKO_Y; - char x_str[16]; - char y_str[16]; - sprintf(x_str, "%d", x); - sprintf(y_str, "%d", y); + char message[32]; + sprintf(message, "%d,%d", x, y); - char path[PATH_MAX]; - std::string journal; + SDL_LockMutex(mutex); + message_len = strlen(message); + strcpy((char*)message_buffer, message); + SDL_UnlockMutex(mutex); - // Get current path - getcwd(path, sizeof(path)); - - #ifdef OS_OSX - journal = std::string(path) + std::string("/_______.app/Contents/MacOS/_______"); - #else - journal = std::string(path) + std::string("_______"); - #endif - - // Run the binary - pid_t pid = fork(); - if (pid == 0) { - execl(journal.c_str(), journal.c_str(), x_str, y_str, (char*)0); - exit(1); + // Clean up connection thread + if (thread != NULL && out_pipe != -1) { + SDL_WaitThread(thread, NULL); + thread = NULL; + } + // Attempt to send it over the tubes + if (out_pipe != -1) { + // We have a connection, so send it over + if (write(out_pipe, (char*)message_buffer, message_len) <= 0) { + // In the case of an error, close + close(out_pipe); + out_pipe = -1; + } + } + if (out_pipe == -1) { + // We don't have a pipe open, so spawn the connection thread + thread = SDL_CreateThread(server_thread, "journal", NULL); } - - // ssize_t len = readlink("/proc/self/exe", path, PATH_MAX); - // if (len > 0) { - // // Replace filename - // for (size_t i = len; i > 0; --i) { - // if (path[i-1] == '/') { - // strcpy(path + i, "_______"); - // break; - // } - // } - // // Run the binary - // pid_t pid = fork(); - // if (pid == 0) { - // execl(path, "_______", x_str, y_str, (char*)0); - - // exit(1); - // } - // } #endif return Qnil; } @@ -108,5 +124,6 @@ void nikoBindingInit() VALUE module = rb_define_module("Niko"); //Functions + _rb_define_module_function(module, "get_ready", nikoPrepare); _rb_define_module_function(module, "do_your_thing", nikoStart); }