Skip to content

Commit

Permalink
single slicer instance
Browse files Browse the repository at this point in the history
check for other instances during startup
send message with command line arguments if found and terminate
listen for those messages and load objects from paths in messages from them
  • Loading branch information
kocikdav committed Apr 29, 2020
1 parent 9cb5975 commit d828a1e
Show file tree
Hide file tree
Showing 16 changed files with 841 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Boost on Raspberry-Pi does not link to pthreads.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

find_package(DBus REQUIRED)
include_directories(${DBUS_INCLUDE_DIRS})
endif()

if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUXX)
Expand Down
59 changes: 59 additions & 0 deletions cmake/modules/FindDBus.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# - Try to find DBus
# Once done, this will define
#
# DBUS_FOUND - system has DBus
# DBUS_INCLUDE_DIRS - the DBus include directories
# DBUS_LIBRARIES - link these to use DBus
#
# Copyright (C) 2012 Raphael Kubo da Costa <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS
# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(PC_DBUS QUIET dbus-1)

FIND_LIBRARY(DBUS_LIBRARIES
NAMES dbus-1
HINTS ${PC_DBUS_LIBDIR}
${PC_DBUS_LIBRARY_DIRS}
)

FIND_PATH(DBUS_INCLUDE_DIR
NAMES dbus/dbus.h
HINTS ${PC_DBUS_INCLUDEDIR}
${PC_DBUS_INCLUDE_DIRS}
)

GET_FILENAME_COMPONENT(_DBUS_LIBRARY_DIR ${DBUS_LIBRARIES} PATH)
FIND_PATH(DBUS_ARCH_INCLUDE_DIR
NAMES dbus/dbus-arch-deps.h
HINTS ${PC_DBUS_INCLUDEDIR}
${PC_DBUS_INCLUDE_DIRS}
${_DBUS_LIBRARY_DIR}
${DBUS_INCLUDE_DIR}
PATH_SUFFIXES include
)

SET(DBUS_INCLUDE_DIRS ${DBUS_INCLUDE_DIR} ${DBUS_ARCH_INCLUDE_DIR})

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(DBUS REQUIRED_VARS DBUS_INCLUDE_DIRS DBUS_LIBRARIES)
13 changes: 12 additions & 1 deletion src/PrusaSlicer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@
#include "slic3r/GUI/GUI.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/3DScene.hpp"
#include "slic3r/GUI/InstanceCheck.hpp"
#include "slic3r/GUI/AppConfig.hpp"
#endif /* SLIC3R_GUI */

using namespace Slic3r;

int CLI::run(int argc, char **argv)
{

#ifdef __WXGTK__
// On Linux, wxGTK has no support for Wayland, and the app crashes on
// startup if gtk3 is used. This env var has to be set explicitly to
Expand Down Expand Up @@ -524,6 +525,16 @@ int CLI::run(int argc, char **argv)
#ifdef SLIC3R_GUI
// #ifdef USE_WX
GUI::GUI_App *gui = new GUI::GUI_App();

bool gui_single_instance_setting = gui->app_config->get("single_instance") == "1";
if (Slic3r::instance_check(argc, argv, gui_single_instance_setting)) {
//TODO: do we have delete gui and other stuff?
return -1;
}

//gui->app_config = app_config;
//app_config = nullptr;

// gui->autosave = m_config.opt_string("autosave");
GUI::GUI_App::SetInstance(gui);
gui->CallAfter([gui, this, &load_configs] {
Expand Down
3 changes: 2 additions & 1 deletion src/PrusaSlicer_app_msvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <shellapi.h>
#include <wchar.h>



#ifdef SLIC3R_GUI
extern "C"
{
Expand Down Expand Up @@ -216,7 +218,6 @@ int APIENTRY wWinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */,
int wmain(int argc, wchar_t **argv)
{
#endif

std::vector<wchar_t*> argv_extended;
argv_extended.emplace_back(argv[0]);

Expand Down
5 changes: 5 additions & 0 deletions src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ void PrintConfigDef::init_common_params()
{
ConfigOptionDef* def;

def = this->add("single_instance", coBool);
def->label = L("Single Instance");
def->mode = comAdvanced;
def->set_default_value(new ConfigOptionBool(false));

def = this->add("printer_technology", coEnum);
def->label = L("Printer technology");
def->tooltip = L("Printer technology");
Expand Down
8 changes: 8 additions & 0 deletions src/slic3r/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ set(SLIC3R_GUI_SOURCES
GUI/DoubleSlider.hpp
GUI/ObjectDataViewModel.cpp
GUI/ObjectDataViewModel.hpp
GUI/InstanceCheck.cpp
GUI/InstanceCheck.hpp
Utils/Http.cpp
Utils/Http.hpp
Utils/FixModelByWin10.cpp
Expand Down Expand Up @@ -195,6 +197,8 @@ if (APPLE)
GUI/RemovableDriveManagerMM.mm
GUI/RemovableDriveManagerMM.h
GUI/Mouse3DHandlerMac.mm
GUI/InstanceCheckMac.mm
GUI/InstanceCheckMac.h
)
FIND_LIBRARY(DISKARBITRATION_LIBRARY DiskArbitration)

Expand All @@ -206,6 +210,10 @@ encoding_check(libslic3r_gui)

target_link_libraries(libslic3r_gui libslic3r avrdude cereal imgui GLEW::GLEW OpenGL::GL OpenGL::GLU hidapi libcurl ${wxWidgets_LIBRARIES})

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(libslic3r_gui ${DBUS_LIBRARIES})
endif()

if(APPLE)
target_link_libraries(libslic3r_gui ${DISKARBITRATION_LIBRARY})
endif()
Expand Down
3 changes: 3 additions & 0 deletions src/slic3r/GUI/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ void AppConfig::set_defaults()
set("use_retina_opengl", "1");
#endif

if (get("single_instance").empty())
set("single_instance", "0");

if (get("remember_output_path").empty())
set("remember_output_path", "1");

Expand Down
72 changes: 47 additions & 25 deletions src/slic3r/GUI/GUI_App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "UpdateDialogs.hpp"
#include "Mouse3DController.hpp"
#include "RemovableDriveManager.hpp"
#include "InstanceCheck.hpp"

#ifdef __WXMSW__
#include <dbt.h>
Expand Down Expand Up @@ -209,6 +210,17 @@ static void register_win32_device_notification_event()
}
return false;
});

wxWindow::MSWRegisterMessageHandler(WM_COPYDATA, [](wxWindow* win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {

COPYDATASTRUCT* copy_data_structure = { 0 };
copy_data_structure = (COPYDATASTRUCT*)lParam;
if (copy_data_structure->dwData == 1) {
LPCWSTR arguments = (LPCWSTR)copy_data_structure->lpData;
Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(boost::nowide::narrow(arguments));
}
return true;
});
}
#endif // WIN32

Expand Down Expand Up @@ -253,7 +265,11 @@ GUI_App::GUI_App()
, m_imgui(new ImGuiWrapper())
, m_wizard(nullptr)
, m_removable_drive_manager(std::make_unique<RemovableDriveManager>())
{}
, m_other_instance_message_handler(std::make_unique<OtherInstanceMessageHandler>())
{
//app config initializes early becasuse it is used in instance checking in PrusaSlicer.cpp
this->init_app_config();
}

GUI_App::~GUI_App()
{
Expand Down Expand Up @@ -284,6 +300,30 @@ bool GUI_App::init_opengl()
}
#endif // ENABLE_NON_STATIC_CANVAS_MANAGER

void GUI_App::init_app_config()
{
// Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release.
SetAppName(SLIC3R_APP_KEY);
//SetAppName(SLIC3R_APP_KEY "-beta");
SetAppDisplayName(SLIC3R_APP_NAME);

// Set the Slic3r data directory at the Slic3r XS module.
// Unix: ~/ .Slic3r
// Windows : "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
// Mac : "~/Library/Application Support/Slic3r"

if (data_dir().empty())
set_data_dir(wxStandardPaths::Get().GetUserDataDir().ToUTF8().data());

if (!app_config)
app_config = new AppConfig();

// load settings
app_conf_exists = app_config->exists();
if (app_conf_exists) {
app_config->load();
}
}
bool GUI_App::OnInit()
{
try {
Expand All @@ -301,34 +341,14 @@ bool GUI_App::on_init_inner()
wxCHECK_MSG(wxDirExists(resources_dir), false,
wxString::Format("Resources path does not exist or is not a directory: %s", resources_dir));

// Profiles for the alpha are stored into the PrusaSlicer-alpha directory to not mix with the current release.
SetAppName(SLIC3R_APP_KEY);
// SetAppName(SLIC3R_APP_KEY "-beta");
SetAppDisplayName(SLIC3R_APP_NAME);

// Enable this to get the default Win32 COMCTRL32 behavior of static boxes.
// Enable this to get the default Win32 COMCTRL32 behavior of static boxes.
// wxSystemOptions::SetOption("msw.staticbox.optimized-paint", 0);
// Enable this to disable Windows Vista themes for all wxNotebooks. The themes seem to lead to terrible
// performance when working on high resolution multi-display setups.
// Enable this to disable Windows Vista themes for all wxNotebooks. The themes seem to lead to terrible
// performance when working on high resolution multi-display setups.
// wxSystemOptions::SetOption("msw.notebook.themed-background", 0);

// Slic3r::debugf "wxWidgets version %s, Wx version %s\n", wxVERSION_STRING, wxVERSION;

// Set the Slic3r data directory at the Slic3r XS module.
// Unix: ~/ .Slic3r
// Windows : "C:\Users\username\AppData\Roaming\Slic3r" or "C:\Documents and Settings\username\Application Data\Slic3r"
// Mac : "~/Library/Application Support/Slic3r"
if (data_dir().empty())
set_data_dir(wxStandardPaths::Get().GetUserDataDir().ToUTF8().data());

app_config = new AppConfig();

// load settings
app_conf_exists = app_config->exists();
if (app_conf_exists) {
app_config->load();
}


std::string msg = Http::tls_global_init();
wxRichMessageDialog
dlg(nullptr,
Expand Down Expand Up @@ -407,6 +427,8 @@ bool GUI_App::on_init_inner()
if (! plater_)
return;

//m_other_instance_message_handler->report();

if (app_config->dirty() && app_config->get("autosave") == "1")
app_config->save();

Expand Down
5 changes: 4 additions & 1 deletion src/slic3r/GUI/GUI_App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PrintHostJobQueue;

namespace GUI{
class RemovableDriveManager;
class OtherInstanceMessageHandler;
enum FileType
{
FT_STL,
Expand Down Expand Up @@ -108,7 +109,7 @@ class GUI_App : public wxApp
std::unique_ptr<ImGuiWrapper> m_imgui;
std::unique_ptr<PrintHostJobQueue> m_printhost_job_queue;
ConfigWizard* m_wizard; // Managed by wxWindow tree

std::unique_ptr <OtherInstanceMessageHandler> m_other_instance_message_handler;
public:
bool OnInit() override;
bool initialized() const { return m_initialized; }
Expand Down Expand Up @@ -196,6 +197,7 @@ class GUI_App : public wxApp
std::vector<Tab *> tabs_list;

RemovableDriveManager* removable_drive_manager() { return m_removable_drive_manager.get(); }
OtherInstanceMessageHandler* other_instance_message_handler() { return m_other_instance_message_handler.get(); }

ImGuiWrapper* imgui() { return m_imgui.get(); }

Expand All @@ -211,6 +213,7 @@ class GUI_App : public wxApp

private:
bool on_init_inner();
void init_app_config();
void window_pos_save(wxTopLevelWindow* window, const std::string &name);
void window_pos_restore(wxTopLevelWindow* window, const std::string &name, bool default_maximized = false);
void window_pos_sanitize(wxTopLevelWindow* window);
Expand Down
Loading

0 comments on commit d828a1e

Please sign in to comment.