2020-04-29 08:50:28 +00:00
|
|
|
#include "GUI_App.hpp"
|
|
|
|
#include "InstanceCheck.hpp"
|
2020-05-26 15:42:57 +00:00
|
|
|
#include "Plater.hpp"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "MainFrame.hpp"
|
|
|
|
#endif
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#include "libslic3r/Utils.hpp"
|
|
|
|
#include "libslic3r/Config.hpp"
|
|
|
|
|
2020-04-29 08:50:28 +00:00
|
|
|
#include "boost/nowide/convert.hpp"
|
|
|
|
#include <boost/log/trivial.hpp>
|
2020-10-17 23:42:19 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2020-04-29 08:50:28 +00:00
|
|
|
#include <iostream>
|
2020-05-14 10:15:02 +00:00
|
|
|
#include <unordered_map>
|
2020-04-29 08:50:28 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2020-10-12 15:06:03 +00:00
|
|
|
#include <optional>
|
2020-12-10 07:40:08 +00:00
|
|
|
#include <cstdint>
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <strsafe.h>
|
|
|
|
#endif //WIN32
|
|
|
|
|
2020-04-29 08:50:28 +00:00
|
|
|
#if __linux__
|
|
|
|
#include <dbus/dbus.h> /* Pull in all of D-Bus headers. */
|
|
|
|
#endif //__linux__
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace instance_check_internal
|
|
|
|
{
|
|
|
|
struct CommandLineAnalysis
|
|
|
|
{
|
2020-10-12 15:06:03 +00:00
|
|
|
std::optional<bool> should_send;
|
|
|
|
std::string cl_string;
|
2020-04-29 08:50:28 +00:00
|
|
|
};
|
2020-05-14 10:15:02 +00:00
|
|
|
static CommandLineAnalysis process_command_line(int argc, char** argv)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
2020-10-14 11:02:03 +00:00
|
|
|
CommandLineAnalysis ret;
|
|
|
|
//if (argc < 2)
|
|
|
|
// return ret;
|
2020-10-12 15:06:03 +00:00
|
|
|
std::vector<std::string> arguments { argv[0] };
|
2021-01-29 06:39:27 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
2020-05-14 10:15:02 +00:00
|
|
|
const std::string token = argv[i];
|
2020-10-12 15:06:03 +00:00
|
|
|
// Processing of boolean command line arguments shall match DynamicConfig::read_cli().
|
|
|
|
if (token == "--single-instance")
|
2020-04-29 08:50:28 +00:00
|
|
|
ret.should_send = true;
|
2020-10-12 15:06:03 +00:00
|
|
|
else if (token == "--no-single-instance")
|
|
|
|
ret.should_send = false;
|
|
|
|
else
|
|
|
|
arguments.emplace_back(token);
|
2020-05-14 10:15:02 +00:00
|
|
|
}
|
2020-10-12 15:06:03 +00:00
|
|
|
ret.cl_string = escape_strings_cstyle(arguments);
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "single instance: " <<
|
2020-10-13 11:50:57 +00:00
|
|
|
(ret.should_send.has_value() ? (*ret.should_send ? "true" : "false") : "undefined") <<
|
2020-10-12 15:06:03 +00:00
|
|
|
". other params: " << ret.cl_string;
|
2020-04-29 08:50:28 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-04-29 08:50:28 +00:00
|
|
|
|
|
|
|
static HWND l_prusa_slicer_hwnd;
|
|
|
|
static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam)
|
|
|
|
{
|
|
|
|
//checks for other instances of prusaslicer, if found brings it to front and return false to stop enumeration and quit this instance
|
|
|
|
//search is done by classname(wxWindowNR is wxwidgets thing, so probably not unique) and name in window upper panel
|
|
|
|
//other option would be do a mutex and check for its existence
|
2020-05-14 10:15:02 +00:00
|
|
|
//BOOST_LOG_TRIVIAL(error) << "ewp: version: " << l_version_wstring;
|
|
|
|
TCHAR wndText[1000];
|
|
|
|
TCHAR className[1000];
|
2020-09-24 15:17:35 +00:00
|
|
|
int err;
|
|
|
|
err = GetClassName(hwnd, className, 1000);
|
|
|
|
if (err == 0)
|
|
|
|
return true;
|
|
|
|
err = GetWindowText(hwnd, wndText, 1000);
|
|
|
|
if (err == 0)
|
|
|
|
return true;
|
2020-04-29 08:50:28 +00:00
|
|
|
std::wstring classNameString(className);
|
|
|
|
std::wstring wndTextString(wndText);
|
2020-11-25 11:28:10 +00:00
|
|
|
if (wndTextString.find(L"PrusaSlicer") != std::wstring::npos && classNameString == L"wxWindowNR") {
|
2020-05-14 10:15:02 +00:00
|
|
|
//check if other instances has same instance hash
|
|
|
|
//if not it is not same version(binary) as this version
|
2020-12-10 07:40:08 +00:00
|
|
|
HANDLE handle = GetProp(hwnd, L"Instance_Hash_Minor");
|
|
|
|
uint64_t other_instance_hash = PtrToUint(handle);
|
|
|
|
uint64_t other_instance_hash_major;
|
|
|
|
uint64_t my_instance_hash = GUI::wxGetApp().get_instance_hash_int();
|
2020-05-14 10:15:02 +00:00
|
|
|
handle = GetProp(hwnd, L"Instance_Hash_Major");
|
|
|
|
other_instance_hash_major = PtrToUint(handle);
|
|
|
|
other_instance_hash_major = other_instance_hash_major << 32;
|
|
|
|
other_instance_hash += other_instance_hash_major;
|
|
|
|
if(my_instance_hash == other_instance_hash)
|
|
|
|
{
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "win enum - found correct instance";
|
|
|
|
l_prusa_slicer_hwnd = hwnd;
|
|
|
|
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
|
|
|
|
SetForegroundWindow(hwnd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "win enum - found wrong instance";
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-14 10:15:02 +00:00
|
|
|
static bool send_message(const std::string& message, const std::string &version)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
2020-05-14 10:15:02 +00:00
|
|
|
if (!EnumWindows(EnumWindowsProc, 0)) {
|
|
|
|
std::wstring wstr = boost::nowide::widen(message);
|
2020-09-24 15:17:35 +00:00
|
|
|
std::unique_ptr<LPWSTR> command_line_args = std::make_unique<LPWSTR>(const_cast<LPWSTR>(wstr.c_str()));
|
|
|
|
/*LPWSTR command_line_args = new wchar_t[wstr.size() + 1];
|
2020-05-14 10:15:02 +00:00
|
|
|
copy(wstr.begin(), wstr.end(), command_line_args);
|
2020-09-24 15:17:35 +00:00
|
|
|
command_line_args[wstr.size()] = 0;*/
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
//Create a COPYDATASTRUCT to send the information
|
|
|
|
//cbData represents the size of the information we want to send.
|
|
|
|
//lpData represents the information we want to send.
|
|
|
|
//dwData is an ID defined by us(this is a type of ID different than WM_COPYDATA).
|
|
|
|
COPYDATASTRUCT data_to_send = { 0 };
|
|
|
|
data_to_send.dwData = 1;
|
2020-09-24 15:17:35 +00:00
|
|
|
data_to_send.cbData = sizeof(TCHAR) * (wcslen(*command_line_args.get()) + 1);
|
|
|
|
data_to_send.lpData = *command_line_args.get();
|
2020-05-14 10:15:02 +00:00
|
|
|
SendMessage(l_prusa_slicer_hwnd, WM_COPYDATA, 0, (LPARAM)&data_to_send);
|
|
|
|
return true;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
2020-05-14 10:15:02 +00:00
|
|
|
return false;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#else
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 15:20:02 +00:00
|
|
|
static bool get_lock(const std::string& name, const std::string& path)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
2020-05-14 15:20:02 +00:00
|
|
|
std::string dest_dir = path + name;
|
|
|
|
BOOST_LOG_TRIVIAL(debug) <<"full lock path: "<< dest_dir;
|
2020-05-14 10:15:02 +00:00
|
|
|
struct flock fl;
|
|
|
|
int fdlock;
|
2020-04-29 08:50:28 +00:00
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 1;
|
2020-10-17 23:42:19 +00:00
|
|
|
|
|
|
|
if (! boost::filesystem::is_directory(path)) {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "get_lock(): datadir does not exist yet, creating...";
|
|
|
|
if (! boost::filesystem::create_directories(path))
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "get_lock(): unable to create datadir !!!";
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
if ((fdlock = open(dest_dir.c_str(), O_WRONLY | O_CREAT, 0666)) == -1)
|
2020-05-14 15:20:02 +00:00
|
|
|
return true;
|
2020-04-29 08:50:28 +00:00
|
|
|
|
|
|
|
if (fcntl(fdlock, F_SETLK, &fl) == -1)
|
2020-05-14 15:20:02 +00:00
|
|
|
return true;
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 15:20:02 +00:00
|
|
|
return false;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#endif //WIN32
|
|
|
|
#if defined(__APPLE__)
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
static bool send_message(const std::string &message_text, const std::string &version)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
2020-05-14 10:15:02 +00:00
|
|
|
//std::string v(version);
|
|
|
|
//std::replace(v.begin(), v.end(), '.', '-');
|
|
|
|
//if (!instance_check_internal::get_lock(v))
|
|
|
|
{
|
|
|
|
send_message_mac(message_text, version);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#elif defined(__linux__)
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
static bool send_message(const std::string &message_text, const std::string &version)
|
|
|
|
{
|
|
|
|
/*std::string v(version);
|
|
|
|
std::replace(v.begin(), v.end(), '.', '-');
|
|
|
|
if (!instance_check_internal::get_lock(v))*/
|
|
|
|
/*auto checker = new wxSingleInstanceChecker;
|
|
|
|
if ( !checker->IsAnotherRunning() ) */
|
|
|
|
{
|
|
|
|
DBusMessage* msg;
|
2021-01-29 06:39:27 +00:00
|
|
|
// DBusMessageIter args;
|
2020-05-14 10:15:02 +00:00
|
|
|
DBusConnection* conn;
|
|
|
|
DBusError err;
|
|
|
|
dbus_uint32_t serial = 0;
|
|
|
|
const char* sigval = message_text.c_str();
|
|
|
|
//std::string interface_name = "com.prusa3d.prusaslicer.InstanceCheck";
|
|
|
|
std::string interface_name = "com.prusa3d.prusaslicer.InstanceCheck.Object" + version;
|
2020-09-25 13:13:01 +00:00
|
|
|
std::string method_name = "AnotherInstance";
|
2020-05-14 10:15:02 +00:00
|
|
|
//std::string object_name = "/com/prusa3d/prusaslicer/InstanceCheck";
|
|
|
|
std::string object_name = "/com/prusa3d/prusaslicer/InstanceCheck/Object" + version;
|
|
|
|
|
|
|
|
|
|
|
|
// initialise the error value
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
// connect to bus, and check for errors (use SESSION bus everywhere!)
|
|
|
|
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
|
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Connection Error. Message to another instance wont be send.";
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Connection Error: " << err.message;
|
|
|
|
dbus_error_free(&err);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NULL == conn) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Connection is NULL. Message to another instance wont be send.";
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
//some sources do request interface ownership before constructing msg but i think its wrong.
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
//create new method call message
|
|
|
|
msg = dbus_message_new_method_call(interface_name.c_str(), object_name.c_str(), interface_name.c_str(), method_name.c_str());
|
|
|
|
if (NULL == msg) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Message is NULL. Message to another instance wont be send.";
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-25 13:13:01 +00:00
|
|
|
//the AnotherInstance method is not sending reply.
|
2020-05-14 10:15:02 +00:00
|
|
|
dbus_message_set_no_reply(msg, TRUE);
|
|
|
|
|
|
|
|
//append arguments to message
|
|
|
|
if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &sigval, DBUS_TYPE_INVALID)) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Ran out of memory while constructing args for DBus message. Message to another instance wont be send.";
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
// send the message and flush the connection
|
|
|
|
if (!dbus_connection_send(conn, msg, &serial)) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Ran out of memory while sending DBus message.";
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
dbus_connection_flush(conn);
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
BOOST_LOG_TRIVIAL(trace) << "DBus message sent.";
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
// free the message and close the connection
|
2020-10-15 13:22:34 +00:00
|
|
|
dbus_message_unref(msg);
|
2020-05-14 10:15:02 +00:00
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return true;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
2020-05-14 10:15:02 +00:00
|
|
|
return false;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
2020-05-14 10:15:02 +00:00
|
|
|
|
|
|
|
#endif //__APPLE__/__linux__
|
2020-04-29 08:50:28 +00:00
|
|
|
} //namespace instance_check_internal
|
|
|
|
|
|
|
|
bool instance_check(int argc, char** argv, bool app_config_single_instance)
|
2020-10-15 13:22:34 +00:00
|
|
|
{
|
2021-01-11 10:14:51 +00:00
|
|
|
std::size_t hashed_path;
|
2020-10-15 13:22:34 +00:00
|
|
|
#ifdef _WIN32
|
2021-01-11 10:14:51 +00:00
|
|
|
hashed_path = std::hash<std::string>{}(boost::filesystem::system_complete(argv[0]).string());
|
2020-10-15 13:22:34 +00:00
|
|
|
#else
|
2021-01-11 10:14:51 +00:00
|
|
|
boost::system::error_code ec;
|
2021-01-11 12:31:30 +00:00
|
|
|
#ifdef __linux__
|
2021-01-11 10:14:51 +00:00
|
|
|
// If executed by an AppImage, start the AppImage, not the main process.
|
|
|
|
// see https://docs.appimage.org/packaging-guide/environment-variables.html#id2
|
2021-01-11 10:41:22 +00:00
|
|
|
const char *appimage_env = std::getenv("APPIMAGE");
|
|
|
|
bool appimage_env_valid = false;
|
|
|
|
if (appimage_env) {
|
|
|
|
try {
|
|
|
|
auto appimage_path = boost::filesystem::canonical(boost::filesystem::path(appimage_env));
|
|
|
|
if (boost::filesystem::exists(appimage_path)) {
|
|
|
|
hashed_path = std::hash<std::string>{}(appimage_path.string());
|
|
|
|
appimage_env_valid = true;
|
|
|
|
}
|
|
|
|
} catch (std::exception &) {
|
|
|
|
}
|
|
|
|
if (! appimage_env_valid)
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "APPIMAGE environment variable was set, but it does not point to a valid file: " << appimage_env;
|
|
|
|
}
|
|
|
|
if (! appimage_env_valid)
|
2021-01-11 12:31:30 +00:00
|
|
|
#endif // __linux__
|
2021-01-11 10:14:51 +00:00
|
|
|
hashed_path = std::hash<std::string>{}(boost::filesystem::canonical(boost::filesystem::system_complete(argv[0]), ec).string());
|
2020-11-21 10:43:48 +00:00
|
|
|
if (ec.value() > 0) { // canonical was not able to find the executable (can happen with appimage on some systems. Does it fail on Fuse file systems?)
|
|
|
|
ec.clear();
|
|
|
|
// Compose path with boost canonical of folder and filename
|
|
|
|
hashed_path = std::hash<std::string>{}(boost::filesystem::canonical(boost::filesystem::system_complete(argv[0]).parent_path(), ec).string() + "/" + boost::filesystem::system_complete(argv[0]).filename().string());
|
|
|
|
if (ec.value() > 0) {
|
|
|
|
// Still not valid, process without canonical
|
|
|
|
hashed_path = std::hash<std::string>{}(boost::filesystem::system_complete(argv[0]).string());
|
2020-11-17 13:44:18 +00:00
|
|
|
}
|
2020-11-21 10:43:48 +00:00
|
|
|
}
|
2021-01-11 10:14:51 +00:00
|
|
|
#endif // _WIN32
|
2020-10-15 13:22:34 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
std::string lock_name = std::to_string(hashed_path);
|
|
|
|
GUI::wxGetApp().set_instance_hash(hashed_path);
|
|
|
|
BOOST_LOG_TRIVIAL(debug) <<"full path: "<< lock_name;
|
2020-04-29 08:50:28 +00:00
|
|
|
instance_check_internal::CommandLineAnalysis cla = instance_check_internal::process_command_line(argc, argv);
|
2020-10-12 15:06:03 +00:00
|
|
|
if (! cla.should_send.has_value())
|
|
|
|
cla.should_send = app_config_single_instance;
|
2020-05-14 15:20:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
GUI::wxGetApp().init_single_instance_checker(lock_name + ".lock", data_dir() + "/cache/");
|
2020-10-12 15:06:03 +00:00
|
|
|
if (cla.should_send.value() && GUI::wxGetApp().single_instance_checker()->IsAnotherRunning()) {
|
2020-05-14 15:20:02 +00:00
|
|
|
#else // mac & linx
|
2020-10-14 16:17:31 +00:00
|
|
|
// get_lock() creates the lockfile therefore *cla.should_send is checked after
|
2020-10-15 05:45:36 +00:00
|
|
|
if (instance_check_internal::get_lock(lock_name + ".lock", data_dir() + "/cache/") && *cla.should_send) {
|
2020-05-14 15:20:02 +00:00
|
|
|
#endif
|
2020-05-14 10:15:02 +00:00
|
|
|
instance_check_internal::send_message(cla.cl_string, lock_name);
|
2020-04-29 08:50:28 +00:00
|
|
|
BOOST_LOG_TRIVIAL(info) << "instance check: Another instance found. This instance will terminate.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(info) << "instance check: Another instance not found or single-instance not set.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 06:46:30 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
bool unlock_lockfile(const std::string& name, const std::string& path)
|
|
|
|
{
|
|
|
|
std::string dest_dir = path + name;
|
|
|
|
//BOOST_LOG_TRIVIAL(debug) << "full lock path: " << dest_dir;
|
|
|
|
struct flock fl;
|
|
|
|
int fdlock;
|
|
|
|
fl.l_type = F_UNLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 1;
|
|
|
|
if ((fdlock = open(dest_dir.c_str(), O_WRONLY | O_CREAT, 0666)) == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (fcntl(fdlock, F_SETLK, &fl) == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif //__APPLE__
|
2020-04-29 08:50:28 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
wxDEFINE_EVENT(EVT_LOAD_MODEL_OTHER_INSTANCE, LoadFromOtherInstanceEvent);
|
|
|
|
wxDEFINE_EVENT(EVT_INSTANCE_GO_TO_FRONT, InstanceGoToFrontEvent);
|
|
|
|
|
|
|
|
void OtherInstanceMessageHandler::init(wxEvtHandler* callback_evt_handler)
|
|
|
|
{
|
|
|
|
assert(!m_initialized);
|
|
|
|
assert(m_callback_evt_handler == nullptr);
|
|
|
|
if (m_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_initialized = true;
|
|
|
|
m_callback_evt_handler = callback_evt_handler;
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2020-05-14 10:15:02 +00:00
|
|
|
this->register_for_messages(wxGetApp().get_instance_hash_string());
|
2020-04-29 08:50:28 +00:00
|
|
|
#endif //__APPLE__
|
|
|
|
|
|
|
|
#ifdef BACKGROUND_MESSAGE_LISTENER
|
|
|
|
m_thread = boost::thread((boost::bind(&OtherInstanceMessageHandler::listen, this)));
|
|
|
|
#endif //BACKGROUND_MESSAGE_LISTENER
|
|
|
|
}
|
2020-05-14 10:15:02 +00:00
|
|
|
void OtherInstanceMessageHandler::shutdown(MainFrame* main_frame)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "message handler shutdown().";
|
|
|
|
assert(m_initialized);
|
|
|
|
if (m_initialized) {
|
2020-05-14 10:15:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
HWND hwnd = main_frame->GetHandle();
|
|
|
|
RemoveProp(hwnd, L"Instance_Hash_Minor");
|
|
|
|
RemoveProp(hwnd, L"Instance_Hash_Major");
|
|
|
|
#endif //_WIN32
|
2020-04-29 08:50:28 +00:00
|
|
|
#if __APPLE__
|
|
|
|
//delete macos implementation
|
|
|
|
this->unregister_for_messages();
|
|
|
|
#endif //__APPLE__
|
|
|
|
#ifdef BACKGROUND_MESSAGE_LISTENER
|
|
|
|
if (m_thread.joinable()) {
|
|
|
|
// Stop the worker thread, if running.
|
|
|
|
{
|
|
|
|
// Notify the worker thread to cancel wait on detection polling.
|
|
|
|
std::lock_guard<std::mutex> lck(m_thread_stop_mutex);
|
|
|
|
m_stop = true;
|
|
|
|
}
|
|
|
|
m_thread_stop_condition.notify_all();
|
|
|
|
// Wait for the worker thread to stop.
|
|
|
|
m_thread.join();
|
|
|
|
m_stop = false;
|
|
|
|
}
|
|
|
|
#endif //BACKGROUND_MESSAGE_LISTENER
|
2020-05-18 06:28:53 +00:00
|
|
|
m_callback_evt_handler = nullptr;
|
|
|
|
m_initialized = false;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
void OtherInstanceMessageHandler::init_windows_properties(MainFrame* main_frame, size_t instance_hash)
|
|
|
|
{
|
|
|
|
size_t minor_hash = instance_hash & 0xFFFFFFFF;
|
|
|
|
size_t major_hash = (instance_hash & 0xFFFFFFFF00000000) >> 32;
|
|
|
|
HWND hwnd = main_frame->GetHandle();
|
|
|
|
HANDLE handle_minor = UIntToPtr(minor_hash);
|
|
|
|
HANDLE handle_major = UIntToPtr(major_hash);
|
|
|
|
SetProp(hwnd, L"Instance_Hash_Minor", handle_minor);
|
|
|
|
SetProp(hwnd, L"Instance_Hash_Major", handle_major);
|
|
|
|
//BOOST_LOG_TRIVIAL(debug) << "window properties initialized " << instance_hash << " (" << minor_hash << " & "<< major_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
void OtherInstanceMessageHandler::print_window_info(HWND hwnd)
|
|
|
|
{
|
|
|
|
std::wstring instance_hash = boost::nowide::widen(wxGetApp().get_instance_hash_string());
|
|
|
|
TCHAR wndText[1000];
|
|
|
|
TCHAR className[1000];
|
|
|
|
GetClassName(hwnd, className, 1000);
|
|
|
|
GetWindowText(hwnd, wndText, 1000);
|
|
|
|
std::wstring classNameString(className);
|
|
|
|
std::wstring wndTextString(wndText);
|
|
|
|
HANDLE handle = GetProp(hwnd, L"Instance_Hash_Minor");
|
|
|
|
size_t result = PtrToUint(handle);
|
|
|
|
handle = GetProp(hwnd, L"Instance_Hash_Major");
|
|
|
|
size_t r2 = PtrToUint(handle);
|
|
|
|
r2 = (r2 << 32);
|
|
|
|
result += r2;
|
|
|
|
BOOST_LOG_TRIVIAL(info) << "window info: " << result;
|
|
|
|
}
|
|
|
|
#endif //0
|
|
|
|
#endif //WIN32
|
2020-04-29 08:50:28 +00:00
|
|
|
namespace MessageHandlerInternal
|
|
|
|
{
|
|
|
|
// returns ::path to possible model or empty ::path if input string is not existing path
|
2020-09-24 15:17:35 +00:00
|
|
|
static boost::filesystem::path get_path(const std::string& possible_path)
|
2020-04-29 08:50:28 +00:00
|
|
|
{
|
2020-05-14 10:15:02 +00:00
|
|
|
BOOST_LOG_TRIVIAL(debug) << "message part:" << possible_path;
|
2020-04-29 08:50:28 +00:00
|
|
|
|
|
|
|
if (possible_path.empty() || possible_path.size() < 3) {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "empty";
|
|
|
|
return boost::filesystem::path();
|
|
|
|
}
|
|
|
|
if (boost::filesystem::exists(possible_path)) {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "is path";
|
|
|
|
return boost::filesystem::path(possible_path);
|
|
|
|
} else if (possible_path[0] == '\"') {
|
|
|
|
if(boost::filesystem::exists(possible_path.substr(1, possible_path.size() - 2))) {
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "is path in quotes";
|
|
|
|
return boost::filesystem::path(possible_path.substr(1, possible_path.size() - 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "is NOT path";
|
|
|
|
return boost::filesystem::path();
|
|
|
|
}
|
|
|
|
} //namespace MessageHandlerInternal
|
|
|
|
|
2020-10-12 15:06:03 +00:00
|
|
|
void OtherInstanceMessageHandler::handle_message(const std::string& message)
|
|
|
|
{
|
2020-04-29 08:50:28 +00:00
|
|
|
BOOST_LOG_TRIVIAL(info) << "message from other instance: " << message;
|
|
|
|
|
2020-10-12 15:06:03 +00:00
|
|
|
std::vector<std::string> args;
|
|
|
|
bool parsed = unescape_strings_cstyle(message, args);
|
|
|
|
assert(parsed);
|
|
|
|
if (! parsed) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "message from other instance is incorrectly formatted: " << message;
|
|
|
|
return;
|
2020-04-29 08:50:28 +00:00
|
|
|
}
|
2020-10-12 15:06:03 +00:00
|
|
|
|
|
|
|
std::vector<boost::filesystem::path> paths;
|
|
|
|
// Skip the first argument, it is the path to the slicer executable.
|
|
|
|
auto it = args.begin();
|
|
|
|
for (++ it; it != args.end(); ++ it) {
|
|
|
|
boost::filesystem::path p = MessageHandlerInternal::get_path(*it);
|
|
|
|
if (! p.string().empty())
|
2020-04-29 08:50:28 +00:00
|
|
|
paths.emplace_back(p);
|
|
|
|
}
|
2020-10-12 15:06:03 +00:00
|
|
|
if (! paths.empty()) {
|
2020-04-29 08:50:28 +00:00
|
|
|
//wxEvtHandler* evt_handler = wxGetApp().plater(); //assert here?
|
|
|
|
//if (evt_handler) {
|
|
|
|
wxPostEvent(m_callback_evt_handler, LoadFromOtherInstanceEvent(GUI::EVT_LOAD_MODEL_OTHER_INSTANCE, std::vector<boost::filesystem::path>(std::move(paths))));
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BACKGROUND_MESSAGE_LISTENER
|
|
|
|
|
|
|
|
namespace MessageHandlerDBusInternal
|
|
|
|
{
|
|
|
|
//reply to introspect makes our DBus object visible for other programs like D-Feet
|
|
|
|
static void respond_to_introspect(DBusConnection *connection, DBusMessage *request)
|
|
|
|
{
|
|
|
|
DBusMessage *reply;
|
|
|
|
const char *introspection_data =
|
|
|
|
" <!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
|
|
|
|
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">"
|
|
|
|
" <!-- dbus-sharp 0.8.1 -->"
|
|
|
|
" <node>"
|
|
|
|
" <interface name=\"org.freedesktop.DBus.Introspectable\">"
|
|
|
|
" <method name=\"Introspect\">"
|
|
|
|
" <arg name=\"data\" direction=\"out\" type=\"s\" />"
|
|
|
|
" </method>"
|
|
|
|
" </interface>"
|
|
|
|
" <interface name=\"com.prusa3d.prusaslicer.InstanceCheck\">"
|
2020-09-25 13:13:01 +00:00
|
|
|
" <method name=\"AnotherInstance\">"
|
2020-04-29 08:50:28 +00:00
|
|
|
" <arg name=\"data\" direction=\"in\" type=\"s\" />"
|
|
|
|
" </method>"
|
|
|
|
" </interface>"
|
|
|
|
" </node>";
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(request);
|
|
|
|
dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_data, DBUS_TYPE_INVALID);
|
|
|
|
dbus_connection_send(connection, reply, NULL);
|
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
//method AnotherInstance receives message from another PrusaSlicer instance
|
|
|
|
static void handle_method_another_instance(DBusConnection *connection, DBusMessage *request)
|
|
|
|
{
|
|
|
|
DBusError err;
|
2020-09-24 15:17:35 +00:00
|
|
|
char* text = nullptr;
|
2020-04-29 08:50:28 +00:00
|
|
|
wxEvtHandler* evt_handler;
|
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
dbus_message_get_args(request, &err, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
|
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
BOOST_LOG_TRIVIAL(trace) << "Dbus method AnotherInstance received with wrong arguments.";
|
|
|
|
dbus_error_free(&err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wxGetApp().other_instance_message_handler()->handle_message(text);
|
|
|
|
|
|
|
|
evt_handler = wxGetApp().plater();
|
|
|
|
if (evt_handler) {
|
|
|
|
wxPostEvent(evt_handler, InstanceGoToFrontEvent(EVT_INSTANCE_GO_TO_FRONT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//every dbus message received comes here
|
|
|
|
static DBusHandlerResult handle_dbus_object_message(DBusConnection *connection, DBusMessage *message, void *user_data)
|
|
|
|
{
|
|
|
|
const char* interface_name = dbus_message_get_interface(message);
|
|
|
|
const char* member_name = dbus_message_get_member(message);
|
2020-05-14 10:15:02 +00:00
|
|
|
std::string our_interface = "com.prusa3d.prusaslicer.InstanceCheck.Object" + wxGetApp().get_instance_hash_string();
|
2020-04-29 08:50:28 +00:00
|
|
|
BOOST_LOG_TRIVIAL(trace) << "DBus message received: interface: " << interface_name << ", member: " << member_name;
|
|
|
|
if (0 == strcmp("org.freedesktop.DBus.Introspectable", interface_name) && 0 == strcmp("Introspect", member_name)) {
|
|
|
|
respond_to_introspect(connection, message);
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
2020-09-25 13:13:01 +00:00
|
|
|
} else if (0 == strcmp(our_interface.c_str(), interface_name) && 0 == strcmp("AnotherInstance", member_name)) {
|
2020-04-29 08:50:28 +00:00
|
|
|
handle_method_another_instance(connection, message);
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
}
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
}
|
|
|
|
} //namespace MessageHandlerDBusInternal
|
|
|
|
|
|
|
|
void OtherInstanceMessageHandler::listen()
|
|
|
|
{
|
|
|
|
DBusConnection* conn;
|
|
|
|
DBusError err;
|
|
|
|
int name_req_val;
|
|
|
|
DBusObjectPathVTable vtable;
|
2020-05-14 10:15:02 +00:00
|
|
|
std::string instance_hash = wxGetApp().get_instance_hash_string();
|
|
|
|
std::string interface_name = "com.prusa3d.prusaslicer.InstanceCheck.Object" + instance_hash;
|
|
|
|
std::string object_name = "/com/prusa3d/prusaslicer/InstanceCheck/Object" + instance_hash;
|
2020-04-29 08:50:28 +00:00
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
//BOOST_LOG_TRIVIAL(debug) << "init dbus listen " << interface_name << " " << object_name;
|
2020-04-29 08:50:28 +00:00
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
// connect to the bus and check for errors (use SESSION bus everywhere!)
|
|
|
|
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
|
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Connection Error: "<< err.message;
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Dbus Messages listening terminating.";
|
|
|
|
dbus_error_free(&err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NULL == conn) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Connection is NULL. Dbus Messages listening terminating.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// request our name on the bus and check for errors
|
|
|
|
name_req_val = dbus_bus_request_name(conn, interface_name.c_str(), DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
|
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Request name Error: "<< err.message;
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Dbus Messages listening terminating.";
|
|
|
|
dbus_error_free(&err);
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != name_req_val) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Not primary owner of DBus name - probably another PrusaSlicer instance is running.";
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Dbus Messages listening terminating.";
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set callbacks. Unregister function should not be nessary.
|
|
|
|
vtable.message_function = MessageHandlerDBusInternal::handle_dbus_object_message;
|
|
|
|
vtable.unregister_function = NULL;
|
|
|
|
|
|
|
|
// register new object - this is our access to DBus
|
|
|
|
dbus_connection_try_register_object_path(conn, object_name.c_str(), &vtable, NULL, &err);
|
|
|
|
if ( dbus_error_is_set(&err) ) {
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "DBus Register object Error: "<< err.message;
|
|
|
|
BOOST_LOG_TRIVIAL(error) << "Dbus Messages listening terminating.";
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
dbus_error_free(&err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:15:02 +00:00
|
|
|
BOOST_LOG_TRIVIAL(trace) << "Dbus object "<< object_name <<" registered. Starting listening for messages.";
|
2020-04-29 08:50:28 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Wait for 1 second
|
|
|
|
// Cancellable.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lck(m_thread_stop_mutex);
|
|
|
|
m_thread_stop_condition.wait_for(lck, std::chrono::seconds(1), [this] { return m_stop; });
|
|
|
|
}
|
|
|
|
if (m_stop)
|
|
|
|
// Stop the worker thread.
|
|
|
|
|
|
|
|
break;
|
|
|
|
//dispatch should do all the work with incoming messages
|
|
|
|
//second parameter is blocking time that funciton waits for new messages
|
|
|
|
//that is handled here with our own event loop above
|
|
|
|
dbus_connection_read_write_dispatch(conn, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
}
|
|
|
|
#endif //BACKGROUND_MESSAGE_LISTENER
|
|
|
|
} // namespace GUI
|
|
|
|
} // namespace Slic3r
|