From e2b203ba8d1c35f80e13cf0e676ea2c7919f50ea Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 4 Dec 2015 21:25:45 +0100 Subject: [PATCH] Disable screensaver while projecting (untested on Windows) --- lib/Slic3r/GUI/Projector.pm | 15 +++++++++++++-- xs/Build.PL | 6 +++++- xs/MANIFEST | 3 +++ xs/src/libslic3r/GUI/GUI.cpp | 37 ++++++++++++++++++++++++++++++++++++ xs/src/libslic3r/GUI/GUI.hpp | 11 +++++++++++ xs/xsp/GUI.xsp | 15 +++++++++++++++ 6 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 xs/src/libslic3r/GUI/GUI.cpp create mode 100644 xs/src/libslic3r/GUI/GUI.hpp create mode 100644 xs/xsp/GUI.xsp diff --git a/lib/Slic3r/GUI/Projector.pm b/lib/Slic3r/GUI/Projector.pm index d16b8658b..51c2b7f61 100644 --- a/lib/Slic3r/GUI/Projector.pm +++ b/lib/Slic3r/GUI/Projector.pm @@ -595,6 +595,8 @@ sub current_layer_height { sub start_print { my ($self) = @_; + Slic3r::GUI::disable_screensaver(); + { $self->sender(Slic3r::GCode::Sender->new); my $res = $self->sender->connect( @@ -632,15 +634,25 @@ sub stop_print { my ($self) = @_; $self->is_printing(0); + Slic3r::GUI::enable_screensaver(); $self->timer->Stop; $self->_timer_cb(undef); $self->screen->project_layers(undef); +} + +sub print_completed { + my ($self) = @_; # send custom end G-code if ($self->sender) { $self->sender->send($_, 1) for grep !/^;/, split /\n/, $self->config->end_gcode; $self->sender->disconnect; } + + $self->on_print_completed->() + if $self->is_printing && $self->on_print_completed; + + $self->stop_print; } sub is_projecting { @@ -667,8 +679,7 @@ sub project_next_layer { $self->_layer_num($self->_layer_num + 1); Slic3r::debugf "projecting layer %d\n", $self->_layer_num; if ($self->_layer_num >= $self->layer_count) { - $self->on_print_completed->() - if $self->is_printing && $self->on_print_completed; + $self->print_completed; return; } diff --git a/xs/Build.PL b/xs/Build.PL index 08bb259d1..d0704a409 100644 --- a/xs/Build.PL +++ b/xs/Build.PL @@ -12,6 +12,10 @@ use Module::Build::WithXSpp; # NOGDI : prevents inclusion of wingdi.h which defines functions Polygon() and Polyline() in global namespace # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339 my @cflags = qw(-D_GLIBCXX_USE_C99 -DHAS_BOOL -DNOGDI -DSLIC3RXS -DBOOST_ASIO_DISABLE_KQUEUE); +my @ldflags = (); +if ($^O eq 'darwin') { + push @ldflags, qw(-framework IOKit -framework CoreFoundation); +} my @INC = qw(); my @LIBS = qw(); @@ -117,7 +121,7 @@ my $build = Module::Build::WithXSpp->new( Module::Build::WithXSpp 0.13 )}, extra_compiler_flags => [ @INC, @cflags ], - extra_linker_flags => \@LIBS, + extra_linker_flags => [ @LIBS, @ldflags ], # Provides extra C typemaps that are auto-merged extra_typemap_modules => { diff --git a/xs/MANIFEST b/xs/MANIFEST index d9b715cba..bc42a7e57 100644 --- a/xs/MANIFEST +++ b/xs/MANIFEST @@ -38,6 +38,8 @@ src/libslic3r/GCodeWriter.cpp src/libslic3r/GCodeWriter.hpp src/libslic3r/Geometry.cpp src/libslic3r/Geometry.hpp +src/libslic3r/GUI/GUI.cpp +src/libslic3r/GUI/GUI.hpp src/libslic3r/GUI/3DScene.cpp src/libslic3r/GUI/3DScene.hpp src/libslic3r/Layer.cpp @@ -132,6 +134,7 @@ xsp/GCodeSender.xsp xsp/GCode.xsp xsp/GCodeWriter.xsp xsp/Geometry.xsp +xsp/GUI.xsp xsp/GUI_3DScene.xsp xsp/Layer.xsp xsp/Line.xsp diff --git a/xs/src/libslic3r/GUI/GUI.cpp b/xs/src/libslic3r/GUI/GUI.cpp new file mode 100644 index 000000000..3dec75308 --- /dev/null +++ b/xs/src/libslic3r/GUI/GUI.cpp @@ -0,0 +1,37 @@ +#include "GUI.hpp" + +#if __APPLE__ +#import +#elif _WIN32 +#include +#pragma comment(lib, "user32.lib") +#endif + +namespace Slic3r { namespace GUI { + +IOPMAssertionID assertionID; + +void +disable_screensaver() +{ + #if __APPLE__ + CFStringRef reasonForActivity = CFSTR("Slic3r"); + IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, + kIOPMAssertionLevelOn, reasonForActivity, &assertionID); + // ignore result: success == kIOReturnSuccess + #elif _WIN32 + SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); + #endif +} + +void +enable_screensaver() +{ + #if __APPLE__ + IOReturn success = IOPMAssertionRelease(assertionID); + #elif _WIN32 + SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); + #endif +} + +} } diff --git a/xs/src/libslic3r/GUI/GUI.hpp b/xs/src/libslic3r/GUI/GUI.hpp new file mode 100644 index 000000000..3ce72fd61 --- /dev/null +++ b/xs/src/libslic3r/GUI/GUI.hpp @@ -0,0 +1,11 @@ +#ifndef slic3r_GUI_hpp_ +#define slic3r_GUI_hpp_ + +namespace Slic3r { namespace GUI { + +void disable_screensaver(); +void enable_screensaver(); + +} } + +#endif diff --git a/xs/xsp/GUI.xsp b/xs/xsp/GUI.xsp new file mode 100644 index 000000000..165a68b79 --- /dev/null +++ b/xs/xsp/GUI.xsp @@ -0,0 +1,15 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "libslic3r/GUI/GUI.hpp" +%} + + +%package{Slic3r::GUI}; + +void disable_screensaver() + %code{% Slic3r::GUI::disable_screensaver(); %}; + +void enable_screensaver() + %code{% Slic3r::GUI::enable_screensaver(); %};