Skip to content

Commit

Permalink
[core,Q_encoding] Add option to suspend computer when encoding is fin…
Browse files Browse the repository at this point in the history
…ished, Windows-only
  • Loading branch information
eumagga0x2a committed Jun 8, 2023
1 parent b200d87 commit 60c92af
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 77 deletions.
63 changes: 20 additions & 43 deletions avidemux/qt4/ADM_userInterfaces/ADM_dialog/Q_encoding.cpp
Expand Up @@ -60,7 +60,6 @@ void DIA_encodingQt4::priorityChanged(int priorityLevel)
{
ui->comboBoxPriority->disconnect(SIGNAL(currentIndexChanged(int)));
ui->comboBoxPriority->setCurrentIndex(2);
connect(ui->checkBoxShutdown, SIGNAL(currentIndexChanged(int)), this, SLOT(priorityChanged(int)));

GUI_Error_HIG(QT_TRANSLATE_NOOP("qencoding","Privileges Required"), QT_TRANSLATE_NOOP("qencoding","Root privileges are required to perform this operation."));

Expand All @@ -75,37 +74,7 @@ void DIA_encodingQt4::priorityChanged(int priorityLevel)

void DIA_encodingQt4::shutdownChanged(int state)
{
#ifndef _WIN32
if (getuid() != 0)
{
ui->checkBoxShutdown->disconnect(SIGNAL(stateChanged(int)));
ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));

GUI_Error_HIG(QT_TRANSLATE_NOOP("qencoding","Privileges Required"), QT_TRANSLATE_NOOP("qencoding","Root privileges are required to perform this operation."));
}
#else
if(state)
{
ui->checkBoxKeepOpen->disconnect(SIGNAL(stateChanged(int)));
ui->checkBoxKeepOpen->setCheckState(Qt::Unchecked);
stayOpen=false;
connect(ui->checkBoxKeepOpen, SIGNAL(stateChanged(int)), this, SLOT(keepOpenChanged(int)));
}
#endif
}

void DIA_encodingQt4::keepOpenChanged(int state)
{
stayOpen=!!state;
#ifdef _WIN32
if(state)
{
ui->checkBoxShutdown->disconnect(SIGNAL(stateChanged(int)));
ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
}
#endif
stayOpen = (state == 1);
}

void DIA_encodingQt4::deleteStatsChanged(int state)
Expand Down Expand Up @@ -139,15 +108,18 @@ DIA_encodingQt4::DIA_encodingQt4(uint64_t duration) : DIA_encodingBase(duration)
ui->comboBoxPriority->setVisible(false);
ui->labelPrio->setVisible(false);
}
ui->checkBoxShutdown->setVisible(false);
// remove suspend and shutdown options
while(ui->comboBoxFinished->count() > 2)
{
ui->comboBoxFinished->removeItem(2);
}
#endif

if(!prefs->get(DEFAULT_DELETE_FIRST_PASS_LOG_FILES,&deleteStats))
deleteStats = false;
ui->checkBoxDeleteStats->setChecked(deleteStats);

connect(ui->checkBoxShutdown, SIGNAL(stateChanged(int)), this, SLOT(shutdownChanged(int)));
connect(ui->checkBoxKeepOpen, SIGNAL(stateChanged(int)), this, SLOT(keepOpenChanged(int)));
connect(ui->comboBoxFinished, SIGNAL(currentIndexChanged(int)), this, SLOT(shutdownChanged(int)));
connect(ui->checkBoxDeleteStats, SIGNAL(stateChanged(int)), this, SLOT(deleteStatsChanged(int)));
connect(ui->pushButton1, SIGNAL(pressed()), this, SLOT(useTrayButtonPressed()));
connect(ui->pushButton2, SIGNAL(pressed()), this, SLOT(pauseButtonPressed()));
Expand Down Expand Up @@ -199,7 +171,7 @@ DIA_encodingQt4::~DIA_encodingQt4( )
{
ADM_info("Destroying encoding qt4\n");
UI_getTaskBarProgress()->disable();
bool shutdownRequested = (ui->checkBoxShutdown->checkState() == Qt::Checked);
int ix = ui->comboBoxFinished->currentIndex();
if(tray)
{
UI_deiconify();
Expand All @@ -212,12 +184,13 @@ DIA_encodingQt4::~DIA_encodingQt4( )
delete ui;
ui=NULL;
}
if(shutdownRequested)
if(ix > 1)
{
prefs->save(); // won't get another chance
ADM_info("Requesting shutdown...\n");
if(!ADM_shutdown())
ADM_warning("Shutdown request failed\n");
bool suspend = (ix == 2);
ADM_info("Requesting %s...\n", suspend ? "suspend" : "shutdown");
if(!ADM_shutdown(suspend))
ADM_warning("%s request failed\n", suspend ? "Suspend" : "Shutdown");
}
}
/**
Expand Down Expand Up @@ -481,7 +454,8 @@ bool DIA_encodingQt4::isAlive( void )
stopRequest=false;
return true;
}
ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
// encoding aborted
ui->comboBoxFinished->setCurrentIndex(0);
return false;
}

Expand Down Expand Up @@ -519,8 +493,11 @@ void DIA_encodingQt4::keepOpen(void)
delete tray;
tray=NULL;
}
ui->checkBoxShutdown->setCheckState(Qt::Unchecked);
ui->checkBoxShutdown->setEnabled(false);
// remove suspend and shutdown options, if present
while(ui->comboBoxFinished->count() > 2)
{
ui->comboBoxFinished->removeItem(2);
}
ui->pushButton1->setEnabled(false);
ui->pushButton2->setEnabled(false);
ui->comboBoxPriority->setEnabled(false);
Expand Down
1 change: 0 additions & 1 deletion avidemux/qt4/ADM_userInterfaces/ADM_dialog/Q_encoding.h
Expand Up @@ -73,7 +73,6 @@ class DIA_encodingQt4 : public QDialog,public DIA_encodingBase
void pauseButtonPressed(void);
void priorityChanged(int priorityLevel);
void shutdownChanged(int state);
void keepOpenChanged(int state);
void deleteStatsChanged(int state);
void closeEvent(QCloseEvent *event);
void reject(void);
Expand Down
55 changes: 29 additions & 26 deletions avidemux/qt4/ADM_userInterfaces/ADM_dialog/encoding.ui
Expand Up @@ -87,12 +87,39 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout1">
<item>
<widget class="QCheckBox" name="checkBoxShutdown">
<widget class="QLabel" name="labelFinished">
<property name="text">
<string>Shut down computer when finished</string>
<string>When finished:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxFinished">
<property name="currentIndex">
<number>0</number>
</property>
<item>
<property name="text">
<string>Close dialog</string>
</property>
</item>
<item>
<property name="text">
<string>Keep dialog open</string>
</property>
</item>
<item>
<property name="text">
<string>Suspend computer</string>
</property>
</item>
<item>
<property name="text">
<string>Shut down computer</string>
</property>
</item>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
Expand Down Expand Up @@ -147,30 +174,6 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout2">
<item>
<widget class="QCheckBox" name="checkBoxKeepOpen">
<property name="text">
<string>Keep dialog open when finished</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>48</width>
<height>17</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down
2 changes: 1 addition & 1 deletion avidemux_core/ADM_core/include/ADM_misc.h
Expand Up @@ -79,7 +79,7 @@ ADM_CORE6_EXPORT const std::string &ADM_getTimeDateAsString(const char *format=
#include <sys/resource.h>
#endif

ADM_CORE6_EXPORT bool ADM_shutdown(void);
ADM_CORE6_EXPORT bool ADM_shutdown(bool suspend = false);

#ifdef ADM_BIG_ENDIAN
#define R64 ADM_swap64
Expand Down
2 changes: 1 addition & 1 deletion avidemux_core/ADM_core/include/ADM_win32.h
Expand Up @@ -6,7 +6,7 @@
#include "ADM_inttype.h"

ADM_CORE6_EXPORT uint8_t win32_netInit(void);
int shutdown_win32(void);
int shutdown_win32(bool suspend = false);
ADM_CORE6_EXPORT bool getWindowsVersion(char* version);
ADM_CORE6_EXPORT void redirectStdoutToFile(const char *filename);

Expand Down
7 changes: 4 additions & 3 deletions avidemux_core/ADM_core/src/ADM_misc.cpp
Expand Up @@ -300,12 +300,13 @@ extern char *ADM_slashToBackSlash(const char *in)
* \fn ADM_shutdown
* @return
*/
bool ADM_shutdown(void)
bool ADM_shutdown(bool suspend)
{
#ifdef _WIN32
return (shutdown_win32() == 0);
return (shutdown_win32(suspend) == 0);
#else
return (system("shutdown -P 0") == 0);
UNUSED_ARG(suspend);
return (system("shutdown -P 0") == 0);
#endif
}

Expand Down
9 changes: 8 additions & 1 deletion avidemux_core/ADM_core/src/ADM_win32.cpp
Expand Up @@ -5,6 +5,7 @@
#include <io.h>
#include <string>
#include <winnls.h>
#include <powrprof.h>

#include "ADM_default.h"
#include "ADM_win32.h"
Expand Down Expand Up @@ -138,7 +139,7 @@ int setpriority(int which, int who, int value)
return 0;
}

int shutdown_win32(void)
int shutdown_win32(bool suspend)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
Expand All @@ -163,6 +164,12 @@ int shutdown_win32(void)
return -1;
}

if (suspend)
{
if (!SetSuspendState(FALSE, FALSE, FALSE))
return -1;
return 0;
}
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, SHTDN_REASON_FLAG_PLANNED))
{
Expand Down
2 changes: 1 addition & 1 deletion avidemux_core/ADM_core/src/CMakeLists.txt
Expand Up @@ -40,7 +40,7 @@ ADM_ADD_SHARED_LIBRARY(ADM_core6 ${ADM_core_SRCS} ${asmFiles})
include_directories("${PTHREAD_INCLUDE_DIR}")

IF (WIN32)
target_link_libraries(ADM_core6 imagehlp ws2_32)
target_link_libraries(ADM_core6 imagehlp ws2_32 powrprof)
ELSEIF (APPLE)
target_link_libraries(ADM_core6 "-framework Carbon")
elseif (UNIX)
Expand Down

0 comments on commit 60c92af

Please sign in to comment.