From 0869a63eaf1822825ba4710ef81889ac4a2bce51 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Thu, 5 Mar 2020 15:03:48 +0100 Subject: [PATCH 1/9] Fix of #3769 - wipe into infill --- src/libslic3r/GCode/ToolOrdering.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index db7c58a9c..9bdda3a4c 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -212,10 +212,8 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto if (m_print_config_ptr) { // in this case complete_objects is false (see ToolOrdering constructors) something_nonoverriddable = false; for (const auto& eec : layerm->perimeters.entities) // let's check if there are nonoverriddable entities - if (!layer_tools.wiping_extrusions().is_overriddable_and_mark(dynamic_cast(*eec), *m_print_config_ptr, object, region)) { + if (!layer_tools.wiping_extrusions().is_overriddable_and_mark(dynamic_cast(*eec), *m_print_config_ptr, object, region)) something_nonoverriddable = true; - break; - } } if (something_nonoverriddable) @@ -237,7 +235,7 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto has_infill = true; if (m_print_config_ptr) { - if (!something_nonoverriddable && !layer_tools.wiping_extrusions().is_overriddable_and_mark(*fill, *m_print_config_ptr, object, region)) + if (! layer_tools.wiping_extrusions().is_overriddable_and_mark(*fill, *m_print_config_ptr, object, region)) something_nonoverriddable = true; } } From 85bf78f7e779d838404bef0854bc7c29fd20dbbb Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Wed, 4 Mar 2020 15:18:44 +0100 Subject: [PATCH 2/9] Fix of #3765 - relaxed empty layer detection --- src/libslic3r/GCode.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 55e0c26e4..cfc60ffe3 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -632,8 +632,11 @@ std::vector GCode::collect_layers_to_print(const PrintObjec // Negative support_contact_z is not taken into account, it can result in false positives in cases // where previous layer has object extrusions too (https://github.com/prusa3d/PrusaSlicer/issues/2752) + // Only check this layer in case it has some extrusions. + bool has_extrusions = (layer_to_print.object_layer && layer_to_print.object_layer->has_extrusions()) + || (layer_to_print.support_layer && layer_to_print.support_layer->has_extrusions()); - if (layer_to_print.print_z() > maximal_print_z + 2. * EPSILON) + if (has_extrusions && layer_to_print.print_z() > maximal_print_z + 2. * EPSILON) throw std::runtime_error(_(L("Empty layers detected, the output would not be printable.")) + "\n\n" + _(L("Object name")) + ": " + object.model_object()->name + "\n" + _(L("Print z")) + ": " + std::to_string(layers_to_print.back().print_z()) + "\n\n" + _(L("This is " From b3b800de656db89d0b50ca574b15eea0796383f7 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 6 Mar 2020 15:10:58 +0100 Subject: [PATCH 3/9] Refactoring of RemovableDriveManager: 1) On Windows and Linux, the device enumeration now runs at a background thread, while it ran on the UI thread on idle, which may have been blocking on some rare Windows setups, see GH #3515 #3733 #3746 #3766 2) On OSX, the device enumeration now relies on OS callback, no polling is required. 3) Refactored for cleaner interface. --- src/slic3r/GUI/BackgroundSlicingProcess.cpp | 5 +- src/slic3r/GUI/Event.hpp | 17 +- src/slic3r/GUI/GUI_App.cpp | 6 +- src/slic3r/GUI/GUI_App.hpp | 8 +- src/slic3r/GUI/MainFrame.cpp | 4 + src/slic3r/GUI/Mouse3DController.cpp | 5 +- src/slic3r/GUI/Plater.cpp | 148 ++-- src/slic3r/GUI/Plater.hpp | 1 - src/slic3r/GUI/RemovableDriveManager.cpp | 840 +++++++++----------- src/slic3r/GUI/RemovableDriveManager.hpp | 204 ++--- src/slic3r/GUI/RemovableDriveManagerMM.mm | 155 ++-- 11 files changed, 640 insertions(+), 753 deletions(-) diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index 536d6d634..8921960bb 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -101,10 +101,9 @@ void BackgroundSlicingProcess::process_fff() //FIXME localize the messages // Perform the final post-processing of the export path by applying the print statistics over the file name. std::string export_path = m_fff_print->print_statistics().finalize_output_path(m_export_path); - GUI::RemovableDriveManager::get_instance().update(); - bool with_check = GUI::RemovableDriveManager::get_instance().is_path_on_removable_drive(export_path); + bool with_check = GUI::wxGetApp().removable_drive_manager()->is_path_on_removable_drive(export_path); int copy_ret_val = copy_file(m_temp_output_path, export_path, with_check); - switch (copy_ret_val){ + switch (copy_ret_val) { case SUCCESS: break; // no error case FAIL_COPY_FILE: throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed. Maybe the SD card is write locked?"))); diff --git a/src/slic3r/GUI/Event.hpp b/src/slic3r/GUI/Event.hpp index 429ef99b0..b9816a747 100644 --- a/src/slic3r/GUI/Event.hpp +++ b/src/slic3r/GUI/Event.hpp @@ -40,11 +40,19 @@ template struct ArrayEvent : public wxEvent return new ArrayEvent(GetEventType(), data, GetEventObject()); } }; -template struct ArrayEvent : public wxEvent + +template struct Event : public wxEvent { T data; - ArrayEvent(wxEventType type, T data, wxObject* origin = nullptr) + Event(wxEventType type, const T &data, wxObject* origin = nullptr) + : wxEvent(0, type), data(std::move(data)) + { + m_propagationLevel = wxEVENT_PROPAGATE_MAX; + SetEventObject(origin); + } + + Event(wxEventType type, T&& data, wxObject* origin = nullptr) : wxEvent(0, type), data(std::move(data)) { m_propagationLevel = wxEVENT_PROPAGATE_MAX; @@ -53,13 +61,10 @@ template struct ArrayEvent : public wxEvent virtual wxEvent* Clone() const { - return new ArrayEvent(GetEventType(), data, GetEventObject()); + return new Event(GetEventType(), data, GetEventObject()); } }; -template using Event = ArrayEvent; - - } } diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 7ca8753a1..40d818e93 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -155,6 +155,7 @@ GUI_App::GUI_App() , m_em_unit(10) , m_imgui(new ImGuiWrapper()) , m_wizard(nullptr) + , m_removable_drive_manager(std::make_unique()) {} GUI_App::~GUI_App() @@ -262,7 +263,6 @@ bool GUI_App::on_init_inner() m_printhost_job_queue.reset(new PrintHostJobQueue(mainframe->printhost_queue_dlg())); - RemovableDriveManager::get_instance().init(); Bind(wxEVT_IDLE, [this](wxIdleEvent& event) { @@ -274,10 +274,6 @@ bool GUI_App::on_init_inner() this->obj_manipul()->update_if_dirty(); -#if !__APPLE__ - RemovableDriveManager::get_instance().update(wxGetLocalTime(), true); -#endif - // Preset updating & Configwizard are done after the above initializations, // and after MainFrame is created & shown. // The extra CallAfter() is needed because of Mac, where this is the only way diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index 10b09b1da..d02a60ba9 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -29,9 +29,9 @@ class PresetUpdater; class ModelObject; class PrintHostJobQueue; -namespace GUI -{ +namespace GUI{ +class RemovableDriveManager; enum FileType { FT_STL, @@ -96,6 +96,8 @@ class GUI_App : public wxApp // Best translation language, provided by Windows or OSX, owned by wxWidgets. const wxLanguageInfo *m_language_info_best = nullptr; + std::unique_ptr m_removable_drive_manager; + std::unique_ptr m_imgui; std::unique_ptr m_printhost_job_queue; ConfigWizard* m_wizard; // Managed by wxWindow tree @@ -180,6 +182,8 @@ public: std::vector tabs_list; + RemovableDriveManager* removable_drive_manager() { return m_removable_drive_manager.get(); } + ImGuiWrapper* imgui() { return m_imgui.get(); } PrintHostJobQueue& printhost_job_queue() { return *m_printhost_job_queue.get(); } diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 2bbdaaffc..834ec6ff0 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -25,6 +25,7 @@ #include "wxExtensions.hpp" #include "GUI_ObjectList.hpp" #include "Mouse3DController.hpp" +#include "RemovableDriveManager.hpp" #include "I18N.hpp" #include @@ -124,6 +125,9 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S // Store the device parameter database back to appconfig. m_plater->get_mouse3d_controller().save_config(*wxGetApp().app_config); + // Stop the background thread of the removable drive manager, so that no new updates will be sent to the Plater. + wxGetApp().removable_drive_manager()->shutdown(); + // Save the slic3r.ini.Usually the ini file is saved from "on idle" callback, // but in rare cases it may not have been called yet. wxGetApp().app_config->save(); diff --git a/src/slic3r/GUI/Mouse3DController.cpp b/src/slic3r/GUI/Mouse3DController.cpp index 6781ba9ff..8bc9744f1 100644 --- a/src/slic3r/GUI/Mouse3DController.cpp +++ b/src/slic3r/GUI/Mouse3DController.cpp @@ -451,12 +451,13 @@ void Mouse3DController::shutdown() // Stop the worker thread, if running. { // Notify the worker thread to cancel wait on detection polling. - std::unique_lock lock(m_stop_condition_mutex); + std::lock_guard lock(m_stop_condition_mutex); m_stop = true; - m_stop_condition.notify_all(); } + m_stop_condition.notify_all(); // Wait for the worker thread to stop. m_thread.join(); + m_stop = false; } } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 2177d622c..89087be98 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -1953,6 +1953,11 @@ struct Plater::priv wxString get_project_filename(const wxString& extension = wxEmptyString) const; void set_project_filename(const wxString& filename); + // Caching last value of show_action_buttons parameter for show_action_buttons(), so that a callback which does not know this state will not override it. + mutable bool ready_to_slice = { false }; + // Flag indicating that the G-code export targets a removable device, therefore the show_action_buttons() needs to be called at any case when the background processing finishes. + bool writing_to_removable_device = { false }; + private: bool init_object_menu(); bool init_common_menu(wxMenu* menu, const bool is_part = false); @@ -1978,8 +1983,8 @@ private: * we should call tack_snapshot just ones * instead of calls for each action separately * */ - std::string m_last_fff_printer_profile_name; - std::string m_last_sla_printer_profile_name; + std::string m_last_fff_printer_profile_name; + std::string m_last_sla_printer_profile_name; }; const std::regex Plater::priv::pattern_bundle(".*[.](amf|amf[.]xml|zip[.]amf|3mf|prusa)", std::regex::icase); @@ -2151,11 +2156,17 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) // Connect to a 3DConnextion driver (OSX). mouse3d_controller.init(); + this->q->Bind(EVT_REMOVABLE_DRIVE_EJECTED, [this](RemovableDriveEjectEvent &evt) { + this->show_action_buttons(this->ready_to_slice); + Slic3r::GUI::show_info(this->q, (boost::format(_utf8(L("Unmounting successful. The device %s(%s) can now be safely removed from the computer."))) + % evt.data.name % evt.data.path).str()); + }); + this->q->Bind(EVT_REMOVABLE_DRIVES_CHANGED, [this](RemovableDrivesChangedEvent &) { this->show_action_buttons(this->ready_to_slice); }); + // Start the background thread and register this window as a target for update events. + wxGetApp().removable_drive_manager()->init(this->q); + // Initialize the Undo / Redo stack with a first snapshot. this->take_snapshot(_(L("New Project"))); - - //void Plater::priv::show_action_buttons(const bool is_ready_to_slice) const - RemovableDriveManager::get_instance().set_drive_count_changed_callback(std::bind(&Plater::priv::show_action_buttons, this, std::placeholders::_1)); } Plater::priv::~priv() @@ -3697,14 +3708,9 @@ void Plater::priv::on_process_completed(wxCommandEvent &evt) sidebar->set_btn_label(ActionButtonType::abReslice, "Slice now"); show_action_buttons(true); } - else if (wxGetApp().get_mode() == comSimple) - show_action_buttons(false); - - if(!canceled && RemovableDriveManager::get_instance().get_is_writing()) - { - RemovableDriveManager::get_instance().set_is_writing(false); - show_action_buttons(false); - } + else if (this->writing_to_removable_device || wxGetApp().get_mode() == comSimple) + show_action_buttons(false); + this->writing_to_removable_device = false; } void Plater::priv::on_layer_editing_toggled(bool enable) @@ -4264,32 +4270,36 @@ void Plater::priv::update_object_menu() sidebar->obj_list()->append_menu_items_add_volume(&object_menu); } -void Plater::priv::show_action_buttons(const bool is_ready_to_slice) const +void Plater::priv::show_action_buttons(const bool ready_to_slice) const { - RemovableDriveManager::get_instance().set_plater_ready_to_slice(is_ready_to_slice); + // Cache this value, so that the callbacks from the RemovableDriveManager may repeat that value when calling show_action_buttons(). + this->ready_to_slice = ready_to_slice; + wxWindowUpdateLocker noUpdater(sidebar); const auto prin_host_opt = config->option("print_host"); const bool send_gcode_shown = prin_host_opt != nullptr && !prin_host_opt->value.empty(); - bool disconnect_shown = !RemovableDriveManager::get_instance().is_last_drive_removed(); - bool export_removable_shown = RemovableDriveManager::get_instance().get_drives_count() > 0; // when a background processing is ON, export_btn and/or send_btn are showing if (wxGetApp().app_config->get("background_processing") == "1") { + RemovableDriveManager::RemovableDrivesStatus removable_media_status = wxGetApp().removable_drive_manager()->status(); if (sidebar->show_reslice(false) | sidebar->show_export(true) | sidebar->show_send(send_gcode_shown) | - sidebar->show_export_removable(export_removable_shown) | - sidebar->show_disconnect(disconnect_shown)) + sidebar->show_export_removable(removable_media_status.has_removable_drives) | + sidebar->show_disconnect(removable_media_status.has_eject)) sidebar->Layout(); } else { - if (sidebar->show_reslice(is_ready_to_slice) | - sidebar->show_export(!is_ready_to_slice) | - sidebar->show_send(send_gcode_shown && !is_ready_to_slice) | - sidebar->show_export_removable(export_removable_shown && !is_ready_to_slice) | - sidebar->show_disconnect(disconnect_shown && !is_ready_to_slice)) + RemovableDriveManager::RemovableDrivesStatus removable_media_status; + if (! ready_to_slice) + removable_media_status = wxGetApp().removable_drive_manager()->status(); + if (sidebar->show_reslice(ready_to_slice) | + sidebar->show_export(!ready_to_slice) | + sidebar->show_send(send_gcode_shown && !ready_to_slice) | + sidebar->show_export_removable(!ready_to_slice && removable_media_status.has_removable_drives) | + sidebar->show_disconnect(!ready_to_slice && removable_media_status.has_eject)) sidebar->Layout(); } } @@ -4822,51 +4832,38 @@ void Plater::export_gcode(bool prefer_removable) return; } default_output_file = fs::path(Slic3r::fold_utf8_to_ascii(default_output_file.string())); - auto start_dir = wxGetApp().app_config->get_last_output_dir(default_output_file.parent_path().string()); - bool removable_drives_connected = GUI::RemovableDriveManager::get_instance().update(); - if(prefer_removable) - { - if(removable_drives_connected) - { - auto start_dir_removable = wxGetApp().app_config->get_last_output_dir(default_output_file.parent_path().string(), true); - if (RemovableDriveManager::get_instance().is_path_on_removable_drive(start_dir_removable)) - { - start_dir = start_dir_removable; - }else - { - start_dir = RemovableDriveManager::get_instance().get_drive_path(); - } - } + AppConfig &appconfig = *wxGetApp().app_config; + RemovableDriveManager &removable_drive_manager = *wxGetApp().removable_drive_manager(); + // Get a last save path, either to removable media or to an internal media. + std::string start_dir = appconfig.get_last_output_dir(default_output_file.parent_path().string(), prefer_removable); + if (prefer_removable) { + // Returns a path to a removable media if it exists, prefering start_dir. Update the internal removable drives database. + start_dir = removable_drive_manager.get_removable_drive_path(start_dir); + if (start_dir.empty()) + // Direct user to the last internal media. + start_dir = appconfig.get_last_output_dir(default_output_file.parent_path().string(), false); } - wxFileDialog dlg(this, (printer_technology() == ptFFF) ? _(L("Save G-code file as:")) : _(L("Save SL1 file as:")), - start_dir, - from_path(default_output_file.filename()), - GUI::file_wildcards((printer_technology() == ptFFF) ? FT_GCODE : FT_PNGZIP, default_output_file.extension().string()), - wxFD_SAVE | wxFD_OVERWRITE_PROMPT - ); fs::path output_path; - if (dlg.ShowModal() == wxID_OK) { - fs::path path = into_path(dlg.GetPath()); - wxGetApp().app_config->update_last_output_dir(path.parent_path().string(), RemovableDriveManager::get_instance().is_path_on_removable_drive(path.parent_path().string())); - output_path = std::move(path); + { + wxFileDialog dlg(this, (printer_technology() == ptFFF) ? _(L("Save G-code file as:")) : _(L("Save SL1 file as:")), + start_dir, + from_path(default_output_file.filename()), + GUI::file_wildcards((printer_technology() == ptFFF) ? FT_GCODE : FT_PNGZIP, default_output_file.extension().string()), + wxFD_SAVE | wxFD_OVERWRITE_PROMPT + ); + if (dlg.ShowModal() == wxID_OK) + output_path = into_path(dlg.GetPath()); } - if (! output_path.empty()) - { - std::string path = output_path.string(); - p->export_gcode(std::move(output_path), PrintHostJob()); - RemovableDriveManager::get_instance().update(0, false); - RemovableDriveManager::get_instance().set_last_save_path(path); - RemovableDriveManager::get_instance().verify_last_save_path(); - - if(!RemovableDriveManager::get_instance().is_last_drive_removed()) - { - RemovableDriveManager::get_instance().set_is_writing(true); - RemovableDriveManager::get_instance().erase_callbacks(); - RemovableDriveManager::get_instance().add_remove_callback(std::bind(&Plater::drive_ejected_callback, this)); - } - + if (! output_path.empty()) { + p->export_gcode(output_path, PrintHostJob()); + bool path_on_removable_media = removable_drive_manager.set_and_verify_last_save_path(output_path.string()); + // Storing a path to AppConfig either as path to removable media or a path to internal media. + // is_path_on_removable_drive() is called with the "true" parameter to update its internal database as the user may have shuffled the external drives + // while the dialog was open. + appconfig.update_last_output_dir(output_path.parent_path().string(), path_on_removable_media); + p->writing_to_removable_device = path_on_removable_media; } } @@ -5188,28 +5185,11 @@ void Plater::send_gcode() } } +// Called when the Eject button is pressed. void Plater::eject_drive() { - RemovableDriveManager::get_instance().update(0, true); - RemovableDriveManager::get_instance().erase_callbacks(); - RemovableDriveManager::get_instance().add_remove_callback(std::bind(&Plater::drive_ejected_callback, this)); - RemovableDriveManager::get_instance().eject_drive(RemovableDriveManager::get_instance().get_last_save_path()); - + wxGetApp().removable_drive_manager()->eject_drive(); } -void Plater::drive_ejected_callback() -{ - if (RemovableDriveManager::get_instance().get_did_eject()) - { - RemovableDriveManager::get_instance().set_did_eject(false); - show_info(this, - (boost::format(_utf8(L("Unmounting successful. The device %s(%s) can now be safely removed from the computer."))) - % RemovableDriveManager::get_instance().get_ejected_name() - % RemovableDriveManager::get_instance().get_ejected_path()).str()); - } - p->show_action_buttons(false); -} - - void Plater::take_snapshot(const std::string &snapshot_name) { p->take_snapshot(snapshot_name); } void Plater::take_snapshot(const wxString &snapshot_name) { p->take_snapshot(snapshot_name); } @@ -5592,7 +5572,7 @@ void Plater::suppress_background_process(const bool stop_background_process) void Plater::fix_through_netfabb(const int obj_idx, const int vol_idx/* = -1*/) { p->fix_through_netfabb(obj_idx, vol_idx); } void Plater::update_object_menu() { p->update_object_menu(); } -void Plater::show_action_buttons(const bool is_ready_to_slice) const { p->show_action_buttons(is_ready_to_slice); } +void Plater::show_action_buttons(const bool ready_to_slice) const { p->show_action_buttons(ready_to_slice); } void Plater::copy_selection_to_clipboard() { diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 249e0a043..dbf706988 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -213,7 +213,6 @@ public: void fix_through_netfabb(const int obj_idx, const int vol_idx = -1); void send_gcode(); void eject_drive(); - void drive_ejected_callback(); void take_snapshot(const std::string &snapshot_name); void take_snapshot(const wxString &snapshot_name); diff --git a/src/slic3r/GUI/RemovableDriveManager.cpp b/src/slic3r/GUI/RemovableDriveManager.cpp index aa7a3d6a3..c97a745af 100644 --- a/src/slic3r/GUI/RemovableDriveManager.cpp +++ b/src/slic3r/GUI/RemovableDriveManager.cpp @@ -1,6 +1,8 @@ #include "RemovableDriveManager.hpp" -#include -#include "boost/nowide/convert.hpp" +#include + +#include +#include #if _WIN32 #include @@ -9,11 +11,9 @@ #include #include -GUID WceusbshGUID = { 0x25dbce51, 0x6c8f, 0x4a72, - 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 }; #else -//linux includes +// unix, linux & OSX includes #include #include #include @@ -26,124 +26,171 @@ GUID WceusbshGUID = { 0x25dbce51, 0x6c8f, 0x4a72, namespace Slic3r { namespace GUI { +wxDEFINE_EVENT(EVT_REMOVABLE_DRIVE_EJECTED, RemovableDriveEjectEvent); +wxDEFINE_EVENT(EVT_REMOVABLE_DRIVES_CHANGED, RemovableDrivesChangedEvent); + #if _WIN32 -/* currently not used, left for possible future use -INT_PTR WINAPI WinProcCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); -*/ -void RemovableDriveManager::search_for_drives() +std::vector RemovableDriveManager::search_for_removable_drives() const { - m_current_drives.clear(); //get logical drives flags by letter in alphabetical order - DWORD drives_mask = GetLogicalDrives(); - for (size_t i = 0; i < 26; i++) - { - if(drives_mask & (1 << i)) - { - std::string path (1,(char)('A' + i)); - path+=":"; - UINT drive_type = GetDriveTypeA(path.c_str()); + DWORD drives_mask = ::GetLogicalDrives(); + + // Allocate the buffers before the loop. + std::wstring volume_name; + volume_name.resize(MAX_PATH + 1); + std::wstring file_system_name; + file_system_name.resize(MAX_PATH + 1); + + // Iterate the Windows drives from 'A' to 'Z' + std::vector current_drives; + for (size_t i = 0; i < 26; ++ i) + if (drives_mask & (1 << i)) { + std::string path { char('A' + i), ':' }; + UINT drive_type = ::GetDriveTypeA(path.c_str()); // DRIVE_REMOVABLE on W are sd cards and usb thumbnails (not usb harddrives) - if (drive_type == DRIVE_REMOVABLE) - { + if (drive_type == DRIVE_REMOVABLE) { // get name of drive std::wstring wpath = boost::nowide::widen(path); - std::wstring volume_name; - volume_name.resize(1024); - std::wstring file_system_name; - file_system_name.resize(1024); - LPWSTR lp_volume_name_buffer = new wchar_t; - BOOL error = GetVolumeInformationW(wpath.c_str(), &volume_name[0], sizeof(volume_name), NULL, NULL, NULL, &file_system_name[0], sizeof(file_system_name)); - if(error != 0) - { - volume_name.erase(std::find(volume_name.begin(), volume_name.end(), '\0'), volume_name.end()); - if (file_system_name != L"") - { + BOOL error = ::GetVolumeInformationW(wpath.c_str(), volume_name.data(), sizeof(volume_name), nullptr, nullptr, nullptr, file_system_name.data(), sizeof(file_system_name)); + if (error != 0) { + volume_name.erase(volume_name.begin() + wcslen(volume_name.c_str()), volume_name.end()); + if (! file_system_name.empty()) { ULARGE_INTEGER free_space; - GetDiskFreeSpaceExA(path.c_str(), &free_space, NULL, NULL); - if (free_space.QuadPart > 0) - { + ::GetDiskFreeSpaceExA(path.c_str(), &free_space, nullptr, nullptr); + if (free_space.QuadPart > 0) { path += "\\"; - m_current_drives.push_back(DriveData(boost::nowide::narrow(volume_name), path)); + current_drives.emplace_back(DriveData{ boost::nowide::narrow(volume_name), path }); } } } } } + return current_drives; +} + +// Called from UI therefore it blocks the UI thread. +// It also blocks updates at the worker thread. +// Win32 implementation. +void RemovableDriveManager::eject_drive() +{ + if (m_last_save_path.empty()) + return; + +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + + tbb::mutex::scoped_lock lock(m_drives_mutex); + auto it_drive_data = this->find_last_save_path_drive_data(); + if (it_drive_data != m_current_drives.end()) { + // get handle to device + std::string mpath = "\\\\.\\" + m_last_save_path; + mpath = mpath.substr(0, mpath.size() - 1); + HANDLE handle = CreateFileA(mpath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); + if (handle == INVALID_HANDLE_VALUE) { + std::cerr << "Ejecting " << mpath << " failed " << GetLastError() << " \n"; + return; + } + DWORD deviceControlRetVal(0); + //these 3 commands should eject device safely but they dont, the device does disappear from file explorer but the "device was safely remove" notification doesnt trigger. + //sd cards does trigger WM_DEVICECHANGE messege, usb drives dont + DeviceIoControl(handle, FSCTL_LOCK_VOLUME, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); + DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); + // some implemenatations also calls IOCTL_STORAGE_MEDIA_REMOVAL here but it returns error to me + BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); + if (error == 0) { + CloseHandle(handle); + BOOST_LOG_TRIVIAL(error) << "Ejecting " << mpath << " failed " << deviceControlRetVal << " " << GetLastError() << " \n"; + return; + } + CloseHandle(handle); + m_drive_data_last_eject = *it_drive_data; } } -void RemovableDriveManager::eject_drive(const std::string &path) + +std::string RemovableDriveManager::get_removable_drive_path(const std::string &path) { - if(m_current_drives.empty()) - return; - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + + tbb::mutex::scoped_lock lock(m_drives_mutex); + if (m_current_drives.empty()) + return std::string(); + std::size_t found = path.find_last_of("\\"); + std::string new_path = path.substr(0, found); + int letter = PathGetDriveNumberA(new_path.c_str()); + for (const DriveData &drive_data : m_current_drives) { + char drive = drive_data.path[0]; + if (drive == 'A' + letter) + return path; + } + return m_current_drives.front().path; +} + +std::string RemovableDriveManager::get_removable_drive_from_path(const std::string& path) +{ + tbb::mutex::scoped_lock lock(m_drives_mutex); + std::size_t found = path.find_last_of("\\"); + std::string new_path = path.substr(0, found); + int letter = PathGetDriveNumberA(new_path.c_str()); + for (const DriveData &drive_data : m_current_drives) { + assert(! drive_data.path.empty()); + if (drive_data.path.front() == 'A' + letter) + return drive_data.path; + } + return std::string(); +} + +#if 0 +// currently not used, left for possible future use +INT_PTR WINAPI WinProcCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + // here we need to catch messeges about device removal + // problem is that when ejecting usb (how is it implemented above) there is no messege dispached. Only after physical removal of the device. + //uncomment register_window() in init() to register and comment update() in GUI_App.cpp (only for windows!) to stop recieving periodical updates + + LRESULT lRet = 1; + static HDEVNOTIFY hDeviceNotify; + static constexpr GUID WceusbshGUID = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 }; + + switch (message) { - if ((*it).path == path) + case WM_CREATE: + DEV_BROADCAST_DEVICEINTERFACE NotificationFilter; + + ZeroMemory(&NotificationFilter, sizeof(NotificationFilter)); + NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); + NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; + NotificationFilter.dbcc_classguid = WceusbshGUID; + + hDeviceNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); + break; + + case WM_DEVICECHANGE: + { + // here is the important + if(wParam == DBT_DEVICEREMOVECOMPLETE) { - // get handle to device - std::string mpath = "\\\\.\\" + path; - mpath = mpath.substr(0, mpath.size() - 1); - HANDLE handle = CreateFileA(mpath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); - if (handle == INVALID_HANDLE_VALUE) - { - std::cerr << "Ejecting " << mpath << " failed " << GetLastError() << " \n"; - return; - } - DWORD deviceControlRetVal(0); - //these 3 commands should eject device safely but they dont, the device does disappear from file explorer but the "device was safely remove" notification doesnt trigger. - //sd cards does trigger WM_DEVICECHANGE messege, usb drives dont - - DeviceIoControl(handle, FSCTL_LOCK_VOLUME, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); - DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); - // some implemenatations also calls IOCTL_STORAGE_MEDIA_REMOVAL here but it returns error to me - BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr); - if (error == 0) - { - CloseHandle(handle); - std::cerr << "Ejecting " << mpath << " failed " << deviceControlRetVal << " " << GetLastError() << " \n"; - return; - } - CloseHandle(handle); - m_did_eject = true; - m_current_drives.erase(it); - m_ejected_path = m_last_save_path; - m_ejected_name = m_last_save_name; - break; + RemovableDriveManager::get_instance().update(0, true); } } -} -bool RemovableDriveManager::is_path_on_removable_drive(const std::string &path) -{ - if (m_current_drives.empty()) - return false; - std::size_t found = path.find_last_of("\\"); - std::string new_path = path.substr(0, found); - int letter = PathGetDriveNumberA(new_path.c_str()); - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - char drive = (*it).path[0]; - if (drive == ('A' + letter)) - return true; + break; + + default: + // Send all other messages on to the default windows handler. + lRet = DefWindowProc(hWnd, message, wParam, lParam); + break; } - return false; -} -std::string RemovableDriveManager::get_drive_from_path(const std::string& path) -{ - std::size_t found = path.find_last_of("\\"); - std::string new_path = path.substr(0, found); - int letter = PathGetDriveNumberA(new_path.c_str()); - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - char drive = (*it).path[0]; - if (drive == ('A' + letter)) - return (*it).path; - } - return ""; + return lRet; + } + void RemovableDriveManager::register_window() { //creates new unvisible window that is recieving callbacks from system // structure to register - /* currently not used, left for possible future use + // currently not used, left for possible future use WNDCLASSEX wndClass; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; @@ -179,432 +226,291 @@ void RemovableDriveManager::register_window() } //ShowWindow(hWnd, SW_SHOWNORMAL); UpdateWindow(hWnd); - */ } -/* currently not used, left for possible future use -INT_PTR WINAPI WinProcCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +#endif + +#else + +namespace search_for_drives_internal { - // here we need to catch messeges about device removal - // problem is that when ejecting usb (how is it implemented above) there is no messege dispached. Only after physical removal of the device. - //uncomment register_window() in init() to register and comment update() in GUI_App.cpp (only for windows!) to stop recieving periodical updates - - LRESULT lRet = 1; - static HDEVNOTIFY hDeviceNotify; - - switch (message) + static bool compare_filesystem_id(const std::string &path_a, const std::string &path_b) { - case WM_CREATE: - DEV_BROADCAST_DEVICEINTERFACE NotificationFilter; + struct stat buf; + stat(path_a.c_str() ,&buf); + dev_t id_a = buf.st_dev; + stat(path_b.c_str() ,&buf); + dev_t id_b = buf.st_dev; + return id_a == id_b; + } - ZeroMemory(&NotificationFilter, sizeof(NotificationFilter)); - NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); - NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; - NotificationFilter.dbcc_classguid = WceusbshGUID; - - hDeviceNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); - break; - - case WM_DEVICECHANGE: + void inspect_file(const std::string &path, const std::string &parent_path, std::vector &out) { - // here is the important - if(wParam == DBT_DEVICEREMOVECOMPLETE) - { -- RemovableDriveManager::get_instance().update(0, true); + //confirms if the file is removable drive and adds it to vector + + //if not same file system - could be removable drive + if (! compare_filesystem_id(path, parent_path)) { + //free space + boost::filesystem::space_info si = boost::filesystem::space(path); + if (si.available != 0) { + //user id + struct stat buf; + stat(path.c_str(), &buf); + uid_t uid = buf.st_uid; + std::string username(std::getenv("USER")); + struct passwd *pw = getpwuid(uid); + if (pw != 0 && pw->pw_name == username) + out.emplace_back(DriveData{ boost::filesystem::basename(boost::filesystem::path(path)), path }); + } } } - break; - - default: - // Send all other messages on to the default windows handler. - lRet = DefWindowProc(hWnd, message, wParam, lParam); - break; - } - return lRet; - -} -*/ -#else -void RemovableDriveManager::search_for_drives() -{ - - m_current_drives.clear(); - -#if __APPLE__ - // if on macos obj-c class will enumerate - if(m_rdmmm) - { - m_rdmmm->list_devices(); - } -#else + static void search_path(const std::string &path, const std::string &parent_path, std::vector &out) + { + glob_t globbuf; + globbuf.gl_offs = 2; + int error = glob(path.c_str(), GLOB_TILDE, NULL, &globbuf); + if (error == 0) { + for (size_t i = 0; i < globbuf.gl_pathc; ++ i) + inspect_file(globbuf.gl_pathv[i], parent_path, out); + } else { + //if error - path probably doesnt exists so function just exits + //std::cout<<"glob error "<< error<< "\n"; + } + globfree(&globbuf); + } +} + +std::vector RemovableDriveManager::search_for_removable_drives() const +{ + std::vector current_drives; + +#if __APPLE__ + + this->list_devices(current_drives); + +#else //search /media/* folder - search_path("/media/*", "/media"); + search_for_drives_internal::search_path("/media/*", "/media", current_drives); //search_path("/Volumes/*", "/Volumes"); std::string path(std::getenv("USER")); std::string pp(path); - { - //search /media/USERNAME/* folder - pp = "/media/"+pp; - path = "/media/" + path + "/*"; - search_path(path, pp); + //search /media/USERNAME/* folder + pp = "/media/"+pp; + path = "/media/" + path + "/*"; + search_for_drives_internal::search_path(path, pp, current_drives); - //search /run/media/USERNAME/* folder - path = "/run" + path; - pp = "/run"+pp; - search_path(path, pp); - - } + //search /run/media/USERNAME/* folder + path = "/run" + path; + pp = "/run"+pp; + search_for_drives_internal::search_path(path, pp, current_drives); #endif -} -void RemovableDriveManager::search_path(const std::string &path,const std::string &parent_path) -{ - glob_t globbuf; - globbuf.gl_offs = 2; - int error = glob(path.c_str(), GLOB_TILDE, NULL, &globbuf); - if(error == 0) - { - for(size_t i = 0; i < globbuf.gl_pathc; i++) - { - inspect_file(globbuf.gl_pathv[i], parent_path); - } - }else - { - //if error - path probably doesnt exists so function just exits - //std::cout<<"glob error "<< error<< "\n"; - } - - globfree(&globbuf); -} -void RemovableDriveManager::inspect_file(const std::string &path, const std::string &parent_path) -{ - //confirms if the file is removable drive and adds it to vector - //if not same file system - could be removable drive - if(!compare_filesystem_id(path, parent_path)) - { - //free space - boost::filesystem::space_info si = boost::filesystem::space(path); - if(si.available != 0) - { - //user id - struct stat buf; - stat(path.c_str(), &buf); - uid_t uid = buf.st_uid; - std::string username(std::getenv("USER")); - struct passwd *pw = getpwuid(uid); - if (pw != 0 && pw->pw_name == username) - m_current_drives.push_back(DriveData(boost::filesystem::basename(boost::filesystem::path(path)), path)); - } - - } + return current_drives; } -bool RemovableDriveManager::compare_filesystem_id(const std::string &path_a, const std::string &path_b) + +// Called from UI therefore it blocks the UI thread. +// It also blocks updates at the worker thread. +// Unix & OSX implementation. +void RemovableDriveManager::eject_drive() { - struct stat buf; - stat(path_a.c_str() ,&buf); - dev_t id_a = buf.st_dev; - stat(path_b.c_str() ,&buf); - dev_t id_b = buf.st_dev; - return id_a == id_b; -} -void RemovableDriveManager::eject_drive(const std::string &path) -{ - if (m_current_drives.empty()) + if (m_last_save_path.empty()) return; - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - if((*it).path == path) - { - - std::string correct_path(path); - for (size_t i = 0; i < correct_path.size(); ++i) - { - if(correct_path[i]==' ') - { - correct_path = correct_path.insert(i,1,'\\'); - i++; - } - } - //std::cout<<"Ejecting "<<(*it).name<<" from "<< correct_path<<"\n"; -// there is no usable command in c++ so terminal command is used instead -// but neither triggers "succesful safe removal messege" - std::string command = ""; -#if __APPLE__ - //m_rdmmm->eject_device(path); - command = "diskutil unmount "; -#else - command = "umount "; -#endif - command += correct_path; - int err = system(command.c_str()); - if(err) - { - std::cerr<<"Ejecting failed\n"; - return; - } +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS - m_did_eject = true; - m_current_drives.erase(it); - m_ejected_path = m_last_save_path; - m_ejected_name = m_last_save_name; - break; + tbb::mutex::scoped_lock lock(m_drives_mutex); + auto it_drive_data = this->find_last_save_path_drive_data(); + if (it_drive_data != m_current_drives.end()) { + std::string correct_path(m_last_save_path); + for (size_t i = 0; i < correct_path.size(); ++i) + if (correct_path[i]==' ') { + correct_path = correct_path.insert(i,1,'\\'); + ++ i; + } + //std::cout<<"Ejecting "<<(*it).name<<" from "<< correct_path<<"\n"; + // there is no usable command in c++ so terminal command is used instead + // but neither triggers "succesful safe removal messege" + std::string command = +#if __APPLE__ + //this->eject_device(m_last_save_path); + "diskutil unmount "; +#else + "umount "; +#endif + command += correct_path; + int err = system(command.c_str()); + if (err) { + BOOST_LOG_TRIVIAL(error) << "Ejecting " << m_last_save_path << " failed"; + return; } + m_drive_data_last_eject = *it_drive_data; } +} -} -bool RemovableDriveManager::is_path_on_removable_drive(const std::string &path) +std::string RemovableDriveManager::get_removable_drive_path(const std::string &path) { - if (m_current_drives.empty()) - return false; +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + std::size_t found = path.find_last_of("/"); std::string new_path = found == path.size() - 1 ? path.substr(0, found) : path; - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - if(compare_filesystem_id(new_path, (*it).path)) - return true; - } - return false; + + tbb::mutex::scoped_lock lock(m_drives_mutex); + for (const DriveData &data : m_current_drives) + if (search_for_drives_internal::compare_filesystem_id(new_path, data.path)) + return path; + return m_current_drives.empty() ? std::string() : m_current_drives.front().path; } -std::string RemovableDriveManager::get_drive_from_path(const std::string& path) + +std::string RemovableDriveManager::get_removable_drive_from_path(const std::string& path) { std::size_t found = path.find_last_of("/"); std::string new_path = found == path.size() - 1 ? path.substr(0, found) : path; - // trim the filename found = new_path.find_last_of("/"); new_path = new_path.substr(0, found); - //check if same filesystem - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - if (compare_filesystem_id(new_path, (*it).path)) - return (*it).path; - } - return ""; + // check if same filesystem + tbb::mutex::scoped_lock lock(m_drives_mutex); + for (const DriveData &drive_data : m_current_drives) + if (search_for_drives_internal::compare_filesystem_id(new_path, drive_data.path)) + return drive_data.path; + return std::string(); } #endif -RemovableDriveManager::RemovableDriveManager(): - m_drives_count(0), - m_last_update(0), - m_last_save_path(""), - m_last_save_name(""), - m_last_save_path_verified(false), - m_is_writing(false), - m_did_eject(false), - m_plater_ready_to_slice(true), - m_ejected_path(""), - m_ejected_name("") -#if __APPLE__ - , m_rdmmm(new RDMMMWrapper()) -#endif -{} -RemovableDriveManager::~RemovableDriveManager() +void RemovableDriveManager::init(wxEvtHandler *callback_evt_handler) { -#if __APPLE__ - delete m_rdmmm; -#endif -} -void RemovableDriveManager::init() -{ - //add_callback([](void) { RemovableDriveManager::get_instance().print(); }); + assert(! m_initialized); + assert(m_callback_evt_handler == nullptr); + + if (m_initialized) + return; + + m_initialized = true; + m_callback_evt_handler = callback_evt_handler; + #if _WIN32 - //register_window(); + //this->register_window_msw(); #elif __APPLE__ - m_rdmmm->register_window(); + this->register_window_osx(); #endif - update(0, true); -} -bool RemovableDriveManager::update(const long time,const bool check) -{ - if(time != 0) //time = 0 is forced update - { - long diff = m_last_update - time; - if(diff <= -2) - { - m_last_update = time; - }else - { - return false; // return value shouldnt matter if update didnt run - } - } - search_for_drives(); - if (m_drives_count != m_current_drives.size()) - { - if (check) - { - check_and_notify(); - } - m_drives_count = m_current_drives.size(); - } - return !m_current_drives.empty(); + +#ifdef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#else // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + // Don't call update() manually, as the UI triggered APIs call this->update() anyways. + m_thread = boost::thread((boost::bind(&RemovableDriveManager::thread_proc, this))); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS } -bool RemovableDriveManager::is_drive_mounted(const std::string &path) const +void RemovableDriveManager::shutdown() { - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - if ((*it).path == path) +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + if (m_thread.joinable()) { + // Stop the worker thread, if running. { - return true; + // Notify the worker thread to cancel wait on detection polling. + std::lock_guard 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 // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + +#if _WIN32 + //this->unregister_window_msw(); +#elif __APPLE__ + this->unregister_window_osx(); +#endif + + m_initialized = false; +} + +bool RemovableDriveManager::set_and_verify_last_save_path(const std::string &path) +{ +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + + m_last_save_path = this->get_removable_drive_from_path(path); + return ! m_last_save_path.empty(); +} + +RemovableDriveManager::RemovableDrivesStatus RemovableDriveManager::status() +{ +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + this->update(); +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + + RemovableDriveManager::RemovableDrivesStatus out; + { + tbb::mutex::scoped_lock lock(m_drives_mutex); + out.has_eject = this->find_last_save_path_drive_data() != m_current_drives.end(); + out.has_removable_drives = ! m_current_drives.empty(); + } + if (! out.has_eject) + m_last_save_path.clear(); + return out; +} + +// Update is called from thread_proc() and from most of the public methods on demand. +void RemovableDriveManager::update() +{ + std::vector current_drives = this->search_for_removable_drives(); + + // Post update events. + tbb::mutex::scoped_lock lock(m_drives_mutex); + std::sort(current_drives.begin(), current_drives.end()); + if (current_drives != m_current_drives) { + if (! m_drive_data_last_eject.empty() && std::find(current_drives.begin(), current_drives.end(), m_drive_data_last_eject) == current_drives.end()) { + assert(m_callback_evt_handler); + if (m_callback_evt_handler) + wxPostEvent(m_callback_evt_handler, RemovableDriveEjectEvent(EVT_REMOVABLE_DRIVE_EJECTED, std::move(m_drive_data_last_eject))); + m_drive_data_last_eject.clear(); + } else { + assert(m_callback_evt_handler); + if (m_callback_evt_handler) + wxPostEvent(m_callback_evt_handler, RemovableDrivesChangedEvent(EVT_REMOVABLE_DRIVES_CHANGED)); } } - return false; + m_current_drives = std::move(current_drives); } -std::string RemovableDriveManager::get_drive_path() + +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS +void RemovableDriveManager::thread_proc() { - if (m_current_drives.size() == 0) - { - reset_last_save_path(); - return ""; - } - if (m_last_save_path_verified) - return m_last_save_path; - return m_current_drives.back().path; -} -std::string RemovableDriveManager::get_last_save_path() const -{ - if (!m_last_save_path_verified) - return ""; - return m_last_save_path; -} -std::string RemovableDriveManager::get_last_save_name() const -{ - return m_last_save_name; -} -std::vector RemovableDriveManager::get_all_drives() const -{ - return m_current_drives; -} -void RemovableDriveManager::check_and_notify() -{ - if(m_drive_count_changed_callback) - { - m_drive_count_changed_callback(m_plater_ready_to_slice); - } - if(m_callbacks.size() != 0 && m_drives_count > m_current_drives.size() && !is_drive_mounted(m_last_save_path)) - { - for (auto it = m_callbacks.begin(); it != m_callbacks.end(); ++it) + for (;;) { + // Wait for 2 seconds before running the disk enumeration. + // Cancellable. { - (*it)(); + std::unique_lock lck(m_thread_stop_mutex); + m_thread_stop_condition.wait_for(lck, std::chrono::seconds(2), [this]{ return m_stop; }); } + if (m_stop) + // Stop the worker thread. + break; + // Update m_current drives and send out update events. + this->update(); } } -void RemovableDriveManager::add_remove_callback(std::function callback) +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + +std::vector::const_iterator RemovableDriveManager::find_last_save_path_drive_data() const { - m_callbacks.push_back(callback); + return Slic3r::binary_find_by_predicate(m_current_drives.begin(), m_current_drives.end(), + [this](const DriveData &data){ return data.path < m_last_save_path; }, + [this](const DriveData &data){ return data.path == m_last_save_path; }); } -void RemovableDriveManager::erase_callbacks() -{ - m_callbacks.clear(); -} -void RemovableDriveManager::set_drive_count_changed_callback(std::function callback) -{ - m_drive_count_changed_callback = callback; -} -void RemovableDriveManager::set_plater_ready_to_slice(bool b) -{ - m_plater_ready_to_slice = b; -} -void RemovableDriveManager::set_last_save_path(const std::string& path) -{ - if(m_last_save_path_verified)// if old path is on drive - { - if(get_drive_from_path(path) != "") //and new is too, rewrite the path - { - m_last_save_path_verified = false; - m_last_save_path = path; - }//else do nothing - }else - { - m_last_save_path = path; - } -} -void RemovableDriveManager::verify_last_save_path() -{ - std::string last_drive = get_drive_from_path(m_last_save_path); - if (last_drive != "") - { - m_last_save_path_verified = true; - m_last_save_path = last_drive; - m_last_save_name = get_drive_name(last_drive); - }else - { - reset_last_save_path(); - } -} -std::string RemovableDriveManager::get_drive_name(const std::string& path) const -{ - if (m_current_drives.size() == 0) - return ""; - for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it) - { - if ((*it).path == path) - { - return (*it).name; - } - } - return ""; -} -bool RemovableDriveManager::is_last_drive_removed() -{ - if(!m_last_save_path_verified) - { - return true; - } - bool r = !is_drive_mounted(m_last_save_path); - if (r) - { - reset_last_save_path(); - } - return r; -} -bool RemovableDriveManager::is_last_drive_removed_with_update(const long time) -{ - update(time, false); - return is_last_drive_removed(); -} -void RemovableDriveManager::reset_last_save_path() -{ - m_last_save_path_verified = false; - m_last_save_path = ""; - m_last_save_name = ""; -} -void RemovableDriveManager::set_is_writing(const bool b) -{ - m_is_writing = b; - if (b) - { - m_did_eject = false; - } -} -bool RemovableDriveManager::get_is_writing() const -{ - return m_is_writing; -} -bool RemovableDriveManager::get_did_eject() const -{ - return m_did_eject; -} -void RemovableDriveManager::set_did_eject(const bool b) -{ - m_did_eject = b; -} -size_t RemovableDriveManager::get_drives_count() const -{ - return m_current_drives.size(); -} -std::string RemovableDriveManager::get_ejected_path() const -{ - return m_ejected_path; -} -std::string RemovableDriveManager::get_ejected_name() const -{ - return m_ejected_name; -} -}}//namespace Slicer::Gui + +}} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/RemovableDriveManager.hpp b/src/slic3r/GUI/RemovableDriveManager.hpp index 032eef682..3b808d9d7 100644 --- a/src/slic3r/GUI/RemovableDriveManager.hpp +++ b/src/slic3r/GUI/RemovableDriveManager.hpp @@ -3,119 +3,129 @@ #include #include -#include + +#include +#include +#include + +// Custom wxWidget events +#include "Event.hpp" namespace Slic3r { namespace GUI { -#if __APPLE__ -class RDMMMWrapper; -#endif - + struct DriveData { std::string name; std::string path; - DriveData(std::string n, std::string p):name(n),path(p){} + + void clear() { + name.clear(); + path.clear(); + } + bool empty() const { + return path.empty(); + } }; + +inline bool operator< (const DriveData &lhs, const DriveData &rhs) { return lhs.path < rhs.path; } +inline bool operator> (const DriveData &lhs, const DriveData &rhs) { return lhs.path > rhs.path; } +inline bool operator==(const DriveData &lhs, const DriveData &rhs) { return lhs.path == rhs.path; } + +using RemovableDriveEjectEvent = Event; +wxDECLARE_EVENT(EVT_REMOVABLE_DRIVE_EJECTED, RemovableDriveEjectEvent); + +using RemovableDrivesChangedEvent = SimpleEvent; +wxDECLARE_EVENT(EVT_REMOVABLE_DRIVES_CHANGED, RemovableDrivesChangedEvent); + +#if __APPLE__ + // Callbacks on device plug / unplug work reliably on OSX. + #define REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS +#endif // __APPLE__ + class RemovableDriveManager { -#if __APPLE__ -friend class RDMMMWrapper; -#endif public: - static RemovableDriveManager& get_instance() - { - static RemovableDriveManager instance; - return instance; - } + RemovableDriveManager() = default; RemovableDriveManager(RemovableDriveManager const&) = delete; void operator=(RemovableDriveManager const&) = delete; - ~RemovableDriveManager(); - //call only once. on apple register for unmnount callbacks. on windows register for device notification is prepared but not called (eject usb drive on widnows doesnt trigger the callback, sdc ard does), also enumerates devices for first time so init shoud be called on linux too. - void init(); - //update() searches for removable devices, returns false if empty. /time = 0 is forced update, time expects wxGetLocalTime() - bool update(const long time = 0,const bool check = false); - bool is_drive_mounted(const std::string &path) const; - void eject_drive(const std::string &path); - //returns path to last drive which was used, if none was used, returns device that was enumerated last - std::string get_last_save_path() const; - std::string get_last_save_name() const; - //returns path to last drive which was used, if none was used, returns empty string - std::string get_drive_path(); - std::vector get_all_drives() const; - bool is_path_on_removable_drive(const std::string &path); - // callback will notify only if device with last save path was removed - void add_remove_callback(std::function callback); - // erases all remove callbacks added by add_remove_callback() - void erase_callbacks(); - //drive_count_changed callback is called on every added or removed device - void set_drive_count_changed_callback(std::function callback); - //thi serves to set correct value for drive_count_changed callback - void set_plater_ready_to_slice(bool b); - // marks one of the eveices in vector as last used - void set_last_save_path(const std::string &path); - void verify_last_save_path(); - bool is_last_drive_removed(); - // param as update() - bool is_last_drive_removed_with_update(const long time = 0); - void set_is_writing(const bool b); - bool get_is_writing() const; - bool get_did_eject() const; - void set_did_eject(const bool b); - std::string get_drive_name(const std::string& path) const; - size_t get_drives_count() const; - std::string get_ejected_path() const; - std::string get_ejected_name() const; -private: - RemovableDriveManager(); - void search_for_drives(); - //triggers callbacks if last used drive was removed - void check_and_notify(); - //returns drive path (same as path in DriveData) if exists otherwise empty string "" - std::string get_drive_from_path(const std::string& path); - void reset_last_save_path(); + ~RemovableDriveManager() { assert(! m_initialized); } + + // Start the background thread and register this window as a target for update events. + // Register for OSX notifications. + void init(wxEvtHandler *callback_evt_handler); + // Stop the background thread of the removable drive manager, so that no new updates will be sent out. + // Deregister OSX notifications. + void shutdown(); + + // Returns path to a removable media if it exists, prefering the input path. + std::string get_removable_drive_path(const std::string &path); + bool is_path_on_removable_drive(const std::string &path) { return this->get_removable_drive_path(path) == path; } + + // Verify whether the path provided is on removable media. If so, save the path for further eject and return true, otherwise return false. + bool set_and_verify_last_save_path(const std::string &path); + // Eject drive of a file set by set_and_verify_last_save_path(). + void eject_drive(); + + struct RemovableDrivesStatus { + bool has_removable_drives { false }; + bool has_eject { false }; + }; + RemovableDrivesStatus status(); + + // Enumerates current drives and sends out wxWidget events on change or eject. + // Called by each public method, by the background thread and from RemovableDriveManagerMM::on_device_unmount OSX notification handler. + // Not to be called manually. + // Public to be accessible from RemovableDriveManagerMM::on_device_unmount OSX notification handler. + // It would be better to make this method private and friend to RemovableDriveManagerMM, but RemovableDriveManagerMM is an ObjectiveC class. + void update(); + +private: + bool m_initialized { false }; + wxEvtHandler* m_callback_evt_handler { nullptr }; + +#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + // Worker thread, worker thread synchronization and callbacks to the UI thread. + void thread_proc(); + boost::thread m_thread; + std::condition_variable m_thread_stop_condition; + mutable std::mutex m_thread_stop_mutex; + bool m_stop { false }; +#endif // REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS + + // Called from update() to enumerate removable drives. + std::vector search_for_removable_drives() const; + + // m_current_drives is guarded by m_drives_mutex + // sorted ascending by path + std::vector m_current_drives; + // When user requested an eject, the drive to be forcefuly ejected is stored here, so the next update will + // recognize that the eject was finished with success and an eject event is sent out. + // guarded with m_drives_mutex + DriveData m_drive_data_last_eject; + mutable tbb::mutex m_drives_mutex; + + // Returns drive path (same as path in DriveData) if exists otherwise empty string. + std::string get_removable_drive_from_path(const std::string& path); + // Returns iterator to a drive in m_current_drives with path equal to m_last_save_path or end(). + std::vector::const_iterator find_last_save_path_drive_data() const; + // Set with set_and_verify_last_save_path() to a removable drive path to be ejected. + std::string m_last_save_path; - std::vector m_current_drives; - std::vector> m_callbacks; - std::function m_drive_count_changed_callback; - size_t m_drives_count; - long m_last_update; - std::string m_last_save_path; - bool m_last_save_path_verified; - std::string m_last_save_name; - bool m_is_writing;//on device - bool m_did_eject; - bool m_plater_ready_to_slice; - std::string m_ejected_path; - std::string m_ejected_name; #if _WIN32 //registers for notifications by creating invisible window - void register_window(); -#else -#if __APPLE__ - RDMMMWrapper * m_rdmmm; - #endif - void search_path(const std::string &path, const std::string &parent_path); - bool compare_filesystem_id(const std::string &path_a, const std::string &path_b); - void inspect_file(const std::string &path, const std::string &parent_path); -#endif -}; -// apple wrapper for RemovableDriveManagerMM which searches for drives and/or ejects them -#if __APPLE__ -class RDMMMWrapper -{ -public: - RDMMMWrapper(); - ~RDMMMWrapper(); - void register_window(); - void list_devices(); + //void register_window_msw(); +#elif __APPLE__ + void register_window_osx(); + void unregister_window_osx(); + void list_devices(std::vector &out) const; + // not used as of now void eject_device(const std::string &path); - void log(const std::string &msg); -protected: - void *m_imp; - //friend void RemovableDriveManager::inspect_file(const std::string &path, const std::string &parent_path); + // Opaque pointer to RemovableDriveManagerMM + void *m_impl_osx; +#endif }; -#endif -}} -#endif +}} + +#endif // slic3r_GUI_RemovableDriveManager_hpp_ diff --git a/src/slic3r/GUI/RemovableDriveManagerMM.mm b/src/slic3r/GUI/RemovableDriveManagerMM.mm index 01d38b185..c2f55bb79 100644 --- a/src/slic3r/GUI/RemovableDriveManagerMM.mm +++ b/src/slic3r/GUI/RemovableDriveManagerMM.mm @@ -1,5 +1,6 @@ #import "RemovableDriveManager.hpp" #import "RemovableDriveManagerMM.h" +#import "GUI_App.hpp" #import #import @@ -10,22 +11,23 @@ -(instancetype) init { self = [super init]; - if(self) - { - } + //if(self){} return self; } + -(void) on_device_unmount: (NSNotification*) notification { - NSLog(@"on device change"); - Slic3r::GUI::RemovableDriveManager::get_instance().update(0,true); + //NSLog(@"on device change"); + Slic3r::GUI::wxGetApp().removable_drive_manager()->update(); } + -(void) add_unmount_observer { - NSLog(@"add unmount observer"); + //NSLog(@"add unmount observer"); [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidUnmountNotification object:nil]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidMountNotification object:nil]; } + -(NSArray*) list_dev { // DEPRICATED: @@ -40,118 +42,99 @@ DADiskRef disk; DASessionRef session; CFDictionaryRef descDict; - session = DASessionCreate(NULL); - if (session == NULL) { + session = DASessionCreate(nullptr); + if (session == nullptr) err = EINVAL; - } if (err == 0) { - disk = DADiskCreateFromVolumePath(NULL,session,(CFURLRef)volURL); - if (session == NULL) { + disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)volURL); + if (session == nullptr) err = EINVAL; - } } if (err == 0) { descDict = DADiskCopyDescription(disk); - if (descDict == NULL) { + if (descDict == nullptr) err = EINVAL; - } } if (err == 0) { CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey); BOOL ejectable = [mediaEjectableKey boolValue]; CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey); CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey); - if (mediaEjectableKey != NULL) - { + if (mediaEjectableKey != nullptr) { BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader"))); //!CFEqual(deviceModelKey, CFSTR("Disk Image")); - // - if (op) { + if (op) [result addObject:volURL.path]; - } } } - if (descDict != NULL) { + if (descDict != nullptr) CFRelease(descDict); - } - - } return result; } + +//this eject drive is not used now -(void)eject_drive:(NSString *)path { DADiskRef disk; DASessionRef session; NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; int err = 0; - session = DASessionCreate(NULL); - if (session == NULL) { + session = DASessionCreate(nullptr); + if (session == nullptr) err = EINVAL; - } - if (err == 0) { - disk = DADiskCreateFromVolumePath(NULL,session,(CFURLRef)url); - } + if (err == 0) + disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url); if( err == 0) - { - DADiskUnmount(disk, kDADiskUnmountOptionDefault, - NULL, NULL); - } - if (disk != NULL) { + DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr); + if (disk != nullptr) CFRelease(disk); - } - if (session != NULL) { + if (session != nullptr) CFRelease(session); - } } -namespace Slic3r { -namespace GUI { -RDMMMWrapper::RDMMMWrapper():m_imp(nullptr){ - m_imp = [[RemovableDriveManagerMM alloc] init]; -} -RDMMMWrapper::~RDMMMWrapper() -{ - if(m_imp) - { - [m_imp release]; - } -} -void RDMMMWrapper::register_window() -{ - if(m_imp) - { - [m_imp add_unmount_observer]; - } -} -void RDMMMWrapper::list_devices() -{ - if(m_imp) - { - NSArray* devices = [m_imp list_dev]; - for (NSString* volumePath in devices) - { - NSLog(@"%@", volumePath); - Slic3r::GUI::RemovableDriveManager::get_instance().inspect_file(std::string([volumePath UTF8String]), "/Volumes"); - } - } -} -void RDMMMWrapper::log(const std::string &msg) -{ - NSLog(@"%s", msg.c_str()); -} -void RDMMMWrapper::eject_device(const std::string &path) -{ - if(m_imp) - { - NSString * pth = [NSString stringWithCString:path.c_str() - encoding:[NSString defaultCStringEncoding]]; - [m_imp eject_drive:pth]; - } -} -}}//namespace Slicer::GUI - -/* - -*/ @end + +namespace Slic3r { +namespace GUI { + +void RemovableDriveManager::register_window_osx() +{ + assert(m_impl_osx == nullptr); + m_impl_osx = [[RemovableDriveManagerMM alloc] init]; + if (m_impl_osx) + [m_impl_osx add_unmount_observer]; +} + +void RemovableDriveManager::unregister_window_osx() +{ + if (m_impl_osx) + [m_impl_osx release]; +} + +namespace search_for_drives_internal +{ + void inspect_file(const std::string &path, const std::string &parent_path, std::vector &out); +} + +void RemovableDriveManager::list_devices(std::vector &out) const +{ + assert(m_impl_osx != nullptr); + if (m_impl_osx) { + NSArray* devices = [m_impl_osx list_dev]; + for (NSString* volumePath in devices) + search_for_drives_internal::inspect_file(std::string([volumePath UTF8String]), "/Volumes", out); + } +} + +// not used as of now +void RemovableDriveManager::eject_device(const std::string &path) +{ + assert(m_impl_osx != nullptr); + if (m_impl_osx) { + NSString * pth = [NSString stringWithCString:path.c_str() encoding:[NSString defaultCStringEncoding]]; + [m_impl_osx eject_drive:pth]; + } +} + +}}//namespace Slicer::GUI From 3baea9b45847e00f3e202116703cf77aa38ed1c8 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 6 Mar 2020 14:44:06 +0100 Subject: [PATCH 4/9] Fixed uninitialized "Connected" flag of the Mouse3DController after refactoring. --- src/slic3r/GUI/Mouse3DController.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slic3r/GUI/Mouse3DController.hpp b/src/slic3r/GUI/Mouse3DController.hpp index 5d6b5ba8c..e686e12cc 100644 --- a/src/slic3r/GUI/Mouse3DController.hpp +++ b/src/slic3r/GUI/Mouse3DController.hpp @@ -139,7 +139,7 @@ class Mouse3DController std::map m_params_by_device; mutable State m_state; - std::atomic m_connected; + std::atomic m_connected { false }; std::string m_device_str; #if ! __APPLE__ From b6aeffb618b2f09be1b655492934407302bdcb4e Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 6 Mar 2020 15:16:46 +0100 Subject: [PATCH 5/9] Link libcurl and wxWidgets to libslic3r_gui Also enhance and remove duplicate CURL search in cmake --- CMakeLists.txt | 15 ++++++++++++--- src/CMakeLists.txt | 28 +--------------------------- src/slic3r/CMakeLists.txt | 2 +- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c69052c8..8d1d93c5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -328,13 +328,21 @@ find_package(TBB REQUIRED) # add_definitions(-DTBB_USE_CAPTURED_EXCEPTION=0) find_package(CURL REQUIRED) -include_directories(${CURL_INCLUDE_DIRS}) + +add_library(libcurl INTERFACE) +target_link_libraries(libcurl INTERFACE CURL::libcurl) + +if (NOT WIN32) + # Required by libcurl + find_package(ZLIB REQUIRED) + target_link_libraries(libcurl INTERFACE ZLIB::ZLIB) +endif() if (SLIC3R_STATIC) if (NOT APPLE) # libcurl is always linked dynamically to the system libcurl on OSX. # On other systems, libcurl is linked statically if SLIC3R_STATIC is set. - add_definitions(-DCURL_STATICLIB) + target_compile_definitions(libcurl INTERFACE CURL_STATICLIB) endif() if (CMAKE_SYSTEM_NAME STREQUAL "Linux") # As of now, our build system produces a statically linked libcurl, @@ -342,7 +350,8 @@ if (SLIC3R_STATIC) find_package(OpenSSL REQUIRED) message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}") message("OpenSSL libraries: ${OPENSSL_LIBRARIES}") - include_directories(${OPENSSL_INCLUDE_DIR}) + target_include_directories(libcurl INTERFACE ${OPENSSL_INCLUDE_DIR}) + target_link_libraries(libcurl INTERFACE ${OPENSSL_LIBRARIES}) endif() endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 11996af1a..b0eab9bcc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -104,33 +104,7 @@ endif () # Add the Slic3r GUI library, libcurl, OpenGL and GLU libraries. if (SLIC3R_GUI) # target_link_libraries(PrusaSlicer ws2_32 uxtheme setupapi libslic3r_gui ${wxWidgets_LIBRARIES}) -target_link_libraries(PrusaSlicer libslic3r_gui ${wxWidgets_LIBRARIES}) - - # Configure libcurl and its dependencies OpenSSL & zlib - find_package(CURL REQUIRED) - if (NOT WIN32) - # Required by libcurl - find_package(ZLIB REQUIRED) - endif() - target_include_directories(PrusaSlicer PRIVATE ${CURL_INCLUDE_DIRS}) - target_link_libraries(PrusaSlicer ${CURL_LIBRARIES} ${ZLIB_LIBRARIES}) - if (SLIC3R_STATIC) - if (NOT APPLE) - # libcurl is always linked dynamically to the system libcurl on OSX. - # On other systems, libcurl is linked statically if SLIC3R_STATIC is set. - target_compile_definitions(PrusaSlicer PRIVATE CURL_STATICLIB) - endif() - if (CMAKE_SYSTEM_NAME STREQUAL "Linux") - # As of now, our build system produces a statically linked libcurl, - # which links the OpenSSL library dynamically. - find_package(OpenSSL REQUIRED) - message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}") - message("OpenSSL libraries: ${OPENSSL_LIBRARIES}") - target_include_directories(PrusaSlicer PRIVATE ${OPENSSL_INCLUDE_DIR}) - target_link_libraries(PrusaSlicer ${OPENSSL_LIBRARIES}) - endif() - endif() - +target_link_libraries(PrusaSlicer libslic3r_gui) if (MSVC) # Generate debug symbols even in release mode. target_link_options(PrusaSlicer PUBLIC "$<$:/DEBUG>") diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index c8589903e..5e0c34da0 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -191,7 +191,7 @@ add_library(libslic3r_gui STATIC ${SLIC3R_GUI_SOURCES}) encoding_check(libslic3r_gui) -target_link_libraries(libslic3r_gui libslic3r avrdude cereal imgui GLEW::GLEW OpenGL::GL OpenGL::GLU hidapi) +target_link_libraries(libslic3r_gui libslic3r avrdude cereal imgui GLEW::GLEW OpenGL::GL OpenGL::GLU hidapi libcurl ${wxWidgets_LIBRARIES}) if(APPLE) target_link_libraries(libslic3r_gui ${DISKARBITRATION_LIBRARY}) From f4f236e82e9c7b680d3c583ddac403869e83d52c Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 6 Mar 2020 15:18:14 +0100 Subject: [PATCH 6/9] Added test for libcurl https handling disabled on build server. Linux docker image does not expose port 80 --- tests/CMakeLists.txt | 2 +- tests/libslic3r/CMakeLists.txt | 1 + .../test_timeutils.cpp} | 23 +++++++++---------- .../{timeutils => slic3rutils}/CMakeLists.txt | 7 +++--- tests/slic3rutils/slic3rutils_tests_main.cpp | 22 ++++++++++++++++++ 5 files changed, 38 insertions(+), 17 deletions(-) rename tests/{timeutils/timeutils_tests_main.cpp => libslic3r/test_timeutils.cpp} (89%) rename tests/{timeutils => slic3rutils}/CMakeLists.txt (55%) create mode 100644 tests/slic3rutils/slic3rutils_tests_main.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 61fe97277..b91f75be4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,7 +29,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) add_subdirectory(libnest2d) add_subdirectory(libslic3r) -add_subdirectory(timeutils) +add_subdirectory(slic3rutils) add_subdirectory(fff_print) add_subdirectory(sla_print) add_subdirectory(cpp17 EXCLUDE_FROM_ALL) # does not have to be built all the time diff --git a/tests/libslic3r/CMakeLists.txt b/tests/libslic3r/CMakeLists.txt index d8dac3c10..2353414f9 100644 --- a/tests/libslic3r/CMakeLists.txt +++ b/tests/libslic3r/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${_TEST_NAME}_tests test_stl.cpp test_meshsimplify.cpp test_meshboolean.cpp + test_timeutils.cpp ) if (TARGET OpenVDB::openvdb) diff --git a/tests/timeutils/timeutils_tests_main.cpp b/tests/libslic3r/test_timeutils.cpp similarity index 89% rename from tests/timeutils/timeutils_tests_main.cpp rename to tests/libslic3r/test_timeutils.cpp index 9989f9871..6630d7055 100644 --- a/tests/timeutils/timeutils_tests_main.cpp +++ b/tests/libslic3r/test_timeutils.cpp @@ -1,4 +1,4 @@ -#include +#include #include "libslic3r/Time.hpp" @@ -6,45 +6,44 @@ #include #include -namespace { +using namespace Slic3r; -void test_time_fmt(Slic3r::Utils::TimeFormat fmt) { +static void test_time_fmt(Slic3r::Utils::TimeFormat fmt) { using namespace Slic3r::Utils; time_t t = get_current_time_utc(); - + std::string tstr = time2str(t, TimeZone::local, fmt); time_t parsedtime = str2time(tstr, TimeZone::local, fmt); REQUIRE(t == parsedtime); - + tstr = time2str(t, TimeZone::utc, fmt); parsedtime = str2time(tstr, TimeZone::utc, fmt); REQUIRE(t == parsedtime); - + parsedtime = str2time("not valid string", TimeZone::local, fmt); REQUIRE(parsedtime == time_t(-1)); - + parsedtime = str2time("not valid string", TimeZone::utc, fmt); REQUIRE(parsedtime == time_t(-1)); } -} TEST_CASE("ISO8601Z", "[Timeutils]") { test_time_fmt(Slic3r::Utils::TimeFormat::iso8601Z); - + std::string mydate = "20190710T085000Z"; time_t t = Slic3r::Utils::parse_iso_utc_timestamp(mydate); std::string date = Slic3r::Utils::iso_utc_timestamp(t); - + REQUIRE(date == mydate); } TEST_CASE("Slic3r_UTC_Time_Format", "[Timeutils]") { using namespace Slic3r::Utils; test_time_fmt(TimeFormat::gcode); - + std::string mydate = "2019-07-10 at 08:50:00 UTC"; time_t t = Slic3r::Utils::str2time(mydate, TimeZone::utc, TimeFormat::gcode); std::string date = Slic3r::Utils::utc_timestamp(t); - + REQUIRE(date == mydate); } diff --git a/tests/timeutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt similarity index 55% rename from tests/timeutils/CMakeLists.txt rename to tests/slic3rutils/CMakeLists.txt index 6ece9f4d1..f03d8d69a 100644 --- a/tests/timeutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -1,11 +1,10 @@ get_filename_component(_TEST_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) add_executable(${_TEST_NAME}_tests ${_TEST_NAME}_tests_main.cpp - ${PROJECT_SOURCE_DIR}/src/libslic3r/Time.cpp - ${PROJECT_SOURCE_DIR}/src/libslic3r/Time.hpp ) -target_link_libraries(${_TEST_NAME}_tests test_common) + +target_link_libraries(${_TEST_NAME}_tests test_common libslic3r_gui) set_property(TARGET ${_TEST_NAME}_tests PROPERTY FOLDER "tests") # catch_discover_tests(${_TEST_NAME}_tests TEST_PREFIX "${_TEST_NAME}: ") -add_test(${_TEST_NAME}_tests ${_TEST_NAME}_tests ${CATCH_EXTRA_ARGS}) +add_test(${_TEST_NAME}_tests ${_TEST_NAME}_tests "${CATCH_EXTRA_ARGS} exclude:[NotWorking]") diff --git a/tests/slic3rutils/slic3rutils_tests_main.cpp b/tests/slic3rutils/slic3rutils_tests_main.cpp new file mode 100644 index 000000000..b82114976 --- /dev/null +++ b/tests/slic3rutils/slic3rutils_tests_main.cpp @@ -0,0 +1,22 @@ +#include + +#include "slic3r/Utils/Http.hpp" + +TEST_CASE("Http", "[Http][NotWorking]") { + + Slic3r::Http g = Slic3r::Http::get("https://github.com/"); + + unsigned status = 0; + g.on_error([&status](std::string, std::string, unsigned http_status) { + status = http_status; + }); + + g.on_complete([&status](std::string /* body */, unsigned http_status){ + status = http_status; + }); + + g.perform_sync(); + + REQUIRE(status == 200); +} + From e39e9b05ae2058db5ba1cdba186a40abf7a8f779 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Fri, 6 Mar 2020 15:33:30 +0100 Subject: [PATCH 7/9] keyboard and file menu shortcuts for save to sd card and eject --- src/slic3r/GUI/KBShortcutsDialog.cpp | 2 ++ src/slic3r/GUI/MainFrame.cpp | 28 ++++++++++++++++++++++++++++ src/slic3r/GUI/MainFrame.hpp | 2 ++ src/slic3r/GUI/Plater.cpp | 4 ++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/slic3r/GUI/KBShortcutsDialog.cpp b/src/slic3r/GUI/KBShortcutsDialog.cpp index b6e55a7c9..b595f1275 100644 --- a/src/slic3r/GUI/KBShortcutsDialog.cpp +++ b/src/slic3r/GUI/KBShortcutsDialog.cpp @@ -122,6 +122,8 @@ void KBShortcutsDialog::fill_shortcuts() { ctrl + "G", L("Export G-code") }, { ctrl + "Shift+" + "G", L("Send G-code") }, { ctrl + "E", L("Export config") }, + { ctrl + "U", L("Export to SD card / Flash drive") }, + { ctrl + "T", L("Eject SD card / Flash drive") }, // Edit { ctrl + "A", L("Select all objects") }, { "Esc", L("Deselect all") }, diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 834ec6ff0..65f73b776 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -327,6 +327,27 @@ bool MainFrame::can_send_gcode() const return print_host_opt != nullptr && !print_host_opt->value.empty(); } +bool MainFrame::can_export_gcode_sd() const +{ + if (m_plater == nullptr) + return false; + + if (m_plater->model().objects.empty()) + return false; + + if (m_plater->is_export_gcode_scheduled()) + return false; + + // TODO:: add other filters + + return wxGetApp().removable_drive_manager()->status().has_removable_drives; +} + +bool MainFrame::can_eject() const +{ + return wxGetApp().removable_drive_manager()->status().has_eject; +} + bool MainFrame::can_slice() const { bool bg_proc = wxGetApp().app_config->get("background_processing") == "1"; @@ -497,6 +518,9 @@ void MainFrame::init_menubar() [this](wxCommandEvent&) { if (m_plater) m_plater->send_gcode(); }, "export_gcode", nullptr, [this](){return can_send_gcode(); }, this); m_changeable_menu_items.push_back(item_send_gcode); + append_menu_item(export_menu, wxID_ANY, _(L("Export G-code to SD card / Flash drive")) + dots + "\tCtrl+U", _(L("Export current plate as G-code to SD card / Flash drive")), + [this](wxCommandEvent&) { if (m_plater) m_plater->export_gcode(true); }, "export_to_sd", nullptr, + [this]() {return can_export_gcode_sd(); }, this); export_menu->AppendSeparator(); append_menu_item(export_menu, wxID_ANY, _(L("Export plate as &STL")) + dots, _(L("Export current plate as STL")), [this](wxCommandEvent&) { if (m_plater) m_plater->export_stl(); }, "export_plater", nullptr, @@ -520,6 +544,10 @@ void MainFrame::init_menubar() [this]() {return true; }, this); append_submenu(fileMenu, export_menu, wxID_ANY, _(L("&Export")), ""); + append_menu_item(fileMenu, wxID_ANY, _(L("Ejec&t SD card / Flash drive")) + dots + "\tCtrl+T", _(L("Eject SD card / Flash drive after the G-code was exported to it.")), + [this](wxCommandEvent&) { if (m_plater) m_plater->eject_drive(); }, "eject_sd", nullptr, + [this]() {return can_eject(); }, this); + fileMenu->AppendSeparator(); #if 0 diff --git a/src/slic3r/GUI/MainFrame.hpp b/src/slic3r/GUI/MainFrame.hpp index a6d0749ab..20e18fc53 100644 --- a/src/slic3r/GUI/MainFrame.hpp +++ b/src/slic3r/GUI/MainFrame.hpp @@ -70,6 +70,8 @@ class MainFrame : public DPIFrame bool can_export_supports() const; bool can_export_gcode() const; bool can_send_gcode() const; + bool can_export_gcode_sd() const; + bool can_eject() const; bool can_slice() const; bool can_change_view() const; bool can_select() const; diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 89087be98..71106b826 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -876,8 +876,8 @@ Sidebar::Sidebar(Plater *parent) }; init_scalable_btn(&p->btn_send_gcode , "export_gcode", _(L("Send to printer")) + "\tCtrl+Shift+G"); - init_scalable_btn(&p->btn_remove_device, "eject_sd" , _(L("Remove device"))); - init_scalable_btn(&p->btn_export_gcode_removable, "export_to_sd", _(L("Export to SD card / Flash drive"))); + init_scalable_btn(&p->btn_remove_device, "eject_sd" , _(L("Remove device")) + "\tCtrl+T"); + init_scalable_btn(&p->btn_export_gcode_removable, "export_to_sd", _(L("Export to SD card / Flash drive")) + "\tCtrl+U"); // regular buttons "Slice now" and "Export G-code" From 9453c644dd24bf1b7cd8e3a0d497368ebbe6bda6 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 6 Mar 2020 14:09:31 +0100 Subject: [PATCH 8/9] Fixed encoding in error messages sent from the backend --- src/slic3r/GUI/BackgroundSlicingProcess.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index 8921960bb..44b5a8bb8 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -235,7 +235,7 @@ void BackgroundSlicingProcess::thread_proc() // Only post the canceled event, if canceled by user. // Don't post the canceled event, if canceled from Print::apply(). wxCommandEvent evt(m_event_finished_id); - evt.SetString(error); + evt.SetString(GUI::from_u8(error)); evt.SetInt(m_print->canceled() ? -1 : (error.empty() ? 1 : 0)); wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, evt.Clone()); } From e22cc58397404706e57d3bc6c7d17ad544afbdb8 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 6 Mar 2020 14:23:40 +0100 Subject: [PATCH 9/9] Copyright year incremented (AboutDialog, manifests) --- src/platform/msw/PrusaSlicer.rc.in | 2 +- src/platform/osx/Info.plist.in | 2 +- src/slic3r/GUI/AboutDialog.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform/msw/PrusaSlicer.rc.in b/src/platform/msw/PrusaSlicer.rc.in index 2d4186a5d..fb75305c8 100644 --- a/src/platform/msw/PrusaSlicer.rc.in +++ b/src/platform/msw/PrusaSlicer.rc.in @@ -12,7 +12,7 @@ PRODUCTVERSION @SLIC3R_RC_VERSION@ VALUE "ProductName", "@SLIC3R_APP_NAME@" VALUE "ProductVersion", "@SLIC3R_BUILD_ID@" VALUE "InternalName", "@SLIC3R_APP_NAME@" - VALUE "LegalCopyright", "Copyright \251 2016-2019 Prusa Research, \251 2011-2018 Alessandro Ranelucci" + VALUE "LegalCopyright", "Copyright \251 2016-2020 Prusa Research, \251 2011-2018 Alessandro Ranelucci" VALUE "OriginalFilename", "prusa-slicer.exe" } } diff --git a/src/platform/osx/Info.plist.in b/src/platform/osx/Info.plist.in index f4e298180..fc4b6a875 100644 --- a/src/platform/osx/Info.plist.in +++ b/src/platform/osx/Info.plist.in @@ -5,7 +5,7 @@ CFBundleExecutable @SLIC3R_APP_KEY@ CFBundleGetInfoString - @SLIC3R_APP_NAME@ Copyright (C) 2011-2019 Alessandro Ranellucci, (C) 2016-2019 Prusa Reseach + @SLIC3R_APP_NAME@ Copyright (C) 2011-2019 Alessandro Ranellucci, (C) 2016-2020 Prusa Reseach CFBundleIconFile PrusaSlicer.icns CFBundleName diff --git a/src/slic3r/GUI/AboutDialog.cpp b/src/slic3r/GUI/AboutDialog.cpp index bd95630a0..98b04a63d 100644 --- a/src/slic3r/GUI/AboutDialog.cpp +++ b/src/slic3r/GUI/AboutDialog.cpp @@ -266,7 +266,7 @@ AboutDialog::AboutDialog() "" "" "" - "%4% © 2016-2019 Prusa Research.
" + "%4% © 2016-2020 Prusa Research.
" "%5% © 2011-2018 Alessandro Ranellucci.
" "Slic3r %6% " "%7%."