actualitzat Director::shutdownSystem()

This commit is contained in:
2025-08-10 22:56:42 +02:00
parent 03db5b4b0a
commit 6011365330

View File

@@ -3,12 +3,14 @@
#include <SDL3/SDL.h> // Para SDL_LogCategory, SDL_LogInfo, SDL_SetLogPriority, SDL_LogPriority, SDL_Quit #include <SDL3/SDL.h> // Para SDL_LogCategory, SDL_LogInfo, SDL_SetLogPriority, SDL_LogPriority, SDL_Quit
#include <sys/stat.h> // Para mkdir, stat, S_IRWXU #include <sys/stat.h> // Para mkdir, stat, S_IRWXU
#include <sys/wait.h>
#include <unistd.h> // Para getuid #include <unistd.h> // Para getuid
#include <cerrno> // Para errno, EEXIST, EACCES, ENAMETOOLONG #include <cerrno> // Para errno, EEXIST, EACCES, ENAMETOOLONG
#include <cstdio> // Para printf, perror #include <cstdio> // Para printf, perror
#include <cstdlib> // Para exit, EXIT_FAILURE, srand, rand, system #include <cstdlib> // Para exit, EXIT_FAILURE, srand, rand, system
#include <ctime> // Para time #include <ctime> // Para time
#include <iostream> // Para std::cerr
#include <memory> // Para make_unique, unique_ptr #include <memory> // Para make_unique, unique_ptr
#include <span> // Para span #include <span> // Para span
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
@@ -40,6 +42,11 @@
#include <pwd.h> // Para getpwuid, passwd #include <pwd.h> // Para getpwuid, passwd
#endif #endif
#ifdef _WIN32
#include <winbase.h>
#include <windows.h>
#endif
// Constructor // Constructor
Director::Director(int argc, std::span<char *> argv) { Director::Director(int argc, std::span<char *> argv) {
#ifdef RECORDING #ifdef RECORDING
@@ -368,21 +375,77 @@ auto Director::run() -> int {
return 0; return 0;
} }
// Apaga el sistema // Apaga el sistema de forma segura
void Director::shutdownSystem(bool should_shutdown) { void Director::shutdownSystem(bool should_shutdown) {
if (should_shutdown) { if (!should_shutdown) {
return;
}
#ifdef _WIN32 #ifdef _WIN32
// Apaga el sistema en Windows // Windows: Usar API nativa de Windows
system("shutdown /s /t 5"); if (!InitiateSystemShutdownA(
NULL, // lpMachineName (NULL = local machine)
"Sistema apagándose...", // lpMessage
5, // dwTimeout (5 segundos)
TRUE, // bForceAppsClosed
FALSE // bRebootAfterShutdown (FALSE = shutdown)
)) {
std::cerr << "Error: No se pudo iniciar el apagado en Windows. Código: "
<< GetLastError() << std::endl;
}
#elif __APPLE__ #elif __APPLE__
// Apaga el sistema en macOS // macOS: Usar execvp con fork
system("sudo shutdown -h +0.1"); pid_t pid = fork();
if (pid == 0) {
// Proceso hijo - ejecutar shutdown
char *args[] = {
const_cast<char *>("shutdown"),
const_cast<char *>("-h"),
const_cast<char *>("+0.1"),
NULL};
execvp("shutdown", args);
// Si execvp falla, salir del proceso hijo
std::cerr << "Error: execvp falló en macOS" << std::endl;
_exit(1);
} else if (pid > 0) {
// Proceso padre - esperar al hijo
int status;
waitpid(pid, &status, 0);
if (WEXITSTATUS(status) != 0) {
std::cerr << "Error: Comando shutdown falló en macOS" << std::endl;
}
} else {
std::cerr << "Error: No se pudo crear proceso fork en macOS" << std::endl;
}
#elif __linux__ #elif __linux__
// Apaga el sistema en Linux // Linux: Usar execvp con fork
system("sleep 5; shutdown -h now"); pid_t pid = fork();
if (pid == 0) {
// Proceso hijo - primero sleep, luego shutdown
sleep(5);
char *args[] = {
const_cast<char *>("shutdown"),
const_cast<char *>("-h"),
const_cast<char *>("now"),
NULL};
execvp("shutdown", args);
// Si execvp falla, salir del proceso hijo
std::cerr << "Error: execvp falló en Linux" << std::endl;
_exit(1);
} else if (pid > 0) {
// Proceso padre - esperar al hijo
int status;
waitpid(pid, &status, 0);
if (WEXITSTATUS(status) != 0) {
std::cerr << "Error: Comando shutdown falló en Linux" << std::endl;
}
} else {
std::cerr << "Error: No se pudo crear proceso fork en Linux" << std::endl;
}
#else #else
// Sistema operativo no compatible
#error "Sistema operativo no soportado" #error "Sistema operativo no soportado"
#endif #endif
}
} }