[vlc-devel] [PATCH 1/2] libvlccore: add a couple of functions to power off the system
Ludovic Fauvet
etix at videolan.org
Tue Apr 16 20:19:29 CEST 2013
Only Windows is fully supported ATM since it requires a bit more work on
other platforms.
---
include/vlc_os.h | 29 +++++++++++++++
src/Makefile.am | 7 ++++
src/libvlccore.sym | 2 ++
src/posix/os.c | 40 +++++++++++++++++++++
src/win32/os.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 180 insertions(+)
create mode 100644 include/vlc_os.h
create mode 100644 src/posix/os.c
create mode 100644 src/win32/os.c
diff --git a/include/vlc_os.h b/include/vlc_os.h
new file mode 100644
index 0000000..7081ec5
--- /dev/null
+++ b/include/vlc_os.h
@@ -0,0 +1,29 @@
+/*****************************************************************************
+ * vlc_os.h: Operating System helpers
+ *****************************************************************************
+ * Copyright (C) 2013 VLC authors and VideoLAN
+ *
+ * Authors: Ludovic Fauvet <etix at videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef VLC_OS_H
+#define VLC_OS_H 1
+
+VLC_API bool vlc_can_power_off( void ) VLC_USED;
+VLC_API bool vlc_power_off( void );
+
+#endif
diff --git a/src/Makefile.am b/src/Makefile.am
index af4c10b..adfbf3e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -253,6 +253,7 @@ SOURCES_libvlc_darwin = \
darwin/specific.c \
posix/rand.c \
darwin/netconf.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_android = \
@@ -266,6 +267,7 @@ SOURCES_libvlc_android = \
posix/linux_specific.c \
posix/specific.c \
posix/rand.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_linux = \
@@ -279,6 +281,7 @@ SOURCES_libvlc_linux = \
posix/linux_specific.c \
posix/specific.c \
posix/rand.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_win32 = \
@@ -290,6 +293,7 @@ SOURCES_libvlc_win32 = \
win32/specific.c \
win32/winsock.c \
win32/rand.c \
+ win32/os.c \
$(NULL)
SOURCES_libvlc_symbian = \
@@ -297,6 +301,7 @@ SOURCES_libvlc_symbian = \
symbian/dirs.c \
win32/plugin.c \
posix/rand.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_os2 = \
@@ -307,6 +312,7 @@ SOURCES_libvlc_os2 = \
os2/thread.c \
os2/specific.c \
os2/rand.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_other = \
@@ -318,6 +324,7 @@ SOURCES_libvlc_other = \
posix/plugin.c \
posix/specific.c \
posix/rand.c \
+ posix/os.c \
$(NULL)
SOURCES_libvlc_common = \
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index ddae89b..77f9569 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -468,6 +468,7 @@ vlc_b64_decode_binary
vlc_b64_decode_binary_to_buffer
vlc_b64_encode
vlc_b64_encode_binary
+vlc_can_power_off
vlc_cancel
vlc_clone
VLC_CompileBy
@@ -541,6 +542,7 @@ vlc_object_hold
vlc_object_release
vlc_object_get_name
vlc_object_alive
+vlc_power_off
vlc_rand_bytes
vlc_drand48
vlc_lrand48
diff --git a/src/posix/os.c b/src/posix/os.c
new file mode 100644
index 0000000..8072c20
--- /dev/null
+++ b/src/posix/os.c
@@ -0,0 +1,40 @@
+/*****************************************************************************
+ * os.c: Operating System helpers
+ *****************************************************************************
+ * Copyright (C) 2013 VLC authors and VideoLAN
+ *
+ * Authors: Ludovic Fauvet <etix at videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_os.h>
+
+bool vlc_can_power_off()
+{
+ // STUB
+ return false;
+}
+
+bool vlc_power_off()
+{
+ // STUB
+ return false;
+}
diff --git a/src/win32/os.c b/src/win32/os.c
new file mode 100644
index 0000000..71c187c
--- /dev/null
+++ b/src/win32/os.c
@@ -0,0 +1,102 @@
+/*****************************************************************************
+ * os.c: Operating System helpers
+ *****************************************************************************
+ * Copyright (C) 2013 VLC authors and VideoLAN
+ *
+ * Authors: Ludovic Fauvet <etix at videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_os.h>
+#include <windows.h>
+#include <reason.h>
+
+static void adjust_poweroff_privileges( HANDLE *hToken, bool enable )
+{
+ TOKEN_PRIVILEGES tkp;
+
+ LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME,
+ &tkp.Privileges[0].Luid );
+
+ tkp.PrivilegeCount = 1;
+ tkp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED;
+
+ AdjustTokenPrivileges( *hToken, FALSE, &tkp, 0,
+ (PTOKEN_PRIVILEGES)NULL, 0 );
+}
+
+static HANDLE acquire_poweroff_privileges()
+{
+ HANDLE hToken;
+
+ if( !OpenProcessToken(GetCurrentProcess(),
+ TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
+ return NULL;
+
+ adjust_poweroff_privileges( &hToken, true );
+
+ if( GetLastError() != ERROR_SUCCESS )
+ {
+ CloseHandle( hToken );
+ return NULL;
+ }
+
+ return hToken;
+}
+
+bool vlc_can_power_off()
+{
+ HANDLE hToken;
+
+ // Try to acquire the shutdown privileges
+ if( ( hToken = acquire_poweroff_privileges() ) != NULL )
+ {
+ // Remove the aquired privilege since we do not
+ // need it immediately.
+ adjust_poweroff_privileges( &hToken, false );
+ CloseHandle( hToken );
+ return true;
+ }
+
+ return false;
+}
+
+bool vlc_power_off()
+{
+ HANDLE hToken;
+
+ // Acquire the necessary privileges to power off
+ if( ( hToken = acquire_poweroff_privileges() ) == NULL )
+ return false;
+
+ CloseHandle( hToken );
+
+ //TODO Support suspend / hibernation
+
+ if( !ExitWindowsEx( EWX_SHUTDOWN | EWX_FORCEIFHUNG,
+ SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
+ SHTDN_REASON_MINOR_UPGRADE |
+ SHTDN_REASON_FLAG_PLANNED ) )
+ return false;
+
+ // Shutdown is successful
+ return true;
+}
--
1.7.10.4
More information about the vlc-devel
mailing list