diff --git a/sysmodules/rosalina/include/sleep.h b/sysmodules/rosalina/include/sleep.h new file mode 100644 index 0000000..521ae02 --- /dev/null +++ b/sysmodules/rosalina/include/sleep.h @@ -0,0 +1,31 @@ +/* +* This file is part of Luma3DS +* Copyright (C) 2016-2018 Aurora Wright, TuxSH +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* Additional Terms 7.b and 7.c of GPLv3 apply to this file: +* * Requiring preservation of specified reasonable legal notices or +* author attributions in that material or in the Appropriate Legal +* Notices displayed by works containing it. +* * Prohibiting misrepresentation of the origin of that material, +* or requiring that modified versions of such material be marked in +* reasonable ways as different from the original version. +*/ + +#pragma once + +void Sleep__Init(void); +void Sleep__HandleNotification(u32 notifId); +bool Sleep__Status(void); diff --git a/sysmodules/rosalina/include/sock_util.h b/sysmodules/rosalina/include/sock_util.h index f66f100..e96d096 100644 --- a/sysmodules/rosalina/include/sock_util.h +++ b/sysmodules/rosalina/include/sock_util.h @@ -89,7 +89,8 @@ typedef struct sock_server Handle shall_terminate_event; } sock_server; -Result server_init(struct sock_server *serv); -void server_bind(struct sock_server *serv, u16 port); -void server_run(struct sock_server *serv); -void server_finalize(struct sock_server *serv); +Result server_init(struct sock_server *serv); +void server_bind(struct sock_server *serv, u16 port); +void server_run(struct sock_server *serv); +void server_finalize(struct sock_server *serv); +bool Wifi__IsConnected(void); \ No newline at end of file diff --git a/sysmodules/rosalina/source/input_redirection.c b/sysmodules/rosalina/source/input_redirection.c index 90b6bfe..e3725c0 100644 --- a/sysmodules/rosalina/source/input_redirection.c +++ b/sysmodules/rosalina/source/input_redirection.c @@ -31,6 +31,8 @@ #include "input_redirection.h" #include "menus.h" #include "memory.h" +#include "sleep.h" +#include "sock_util.h" bool inputRedirectionEnabled = false; Handle inputRedirectionThreadStartedEvent; @@ -96,6 +98,13 @@ void inputRedirectionThreadMain(void) pfd.events = POLLIN; pfd.revents = 0; + if (Sleep__Status()) + { + while (!Wifi__IsConnected() + && inputRedirectionEnabled && !terminationRequest) + svcSleepThread(1000000000ULL); + } + int pollres = socPoll(&pfd, 1, 10); if(pollres > 0 && (pfd.revents & POLLIN)) { diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index c061aa0..5f8f6be 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -32,6 +32,7 @@ #include "errdisp.h" #include "hbloader.h" #include "utils.h" +#include "sleep.h" #include "MyThread.h" #include "menus/process_patches.h" #include "menus/miscellaneous.h" @@ -49,6 +50,7 @@ void __appInit() // this is called after main exits void __appExit() { + acExit(); fsExit(); fsregExit(); srvSysExit(); @@ -117,6 +119,8 @@ int main(void) if(R_FAILED(svcCreateEvent(&terminationRequestEvent, RESET_STICKY))) svcBreak(USERBREAK_ASSERT); + Sleep__Init(); + do { res = svcWaitSynchronization(notificationHandle, -1LL); @@ -135,6 +139,8 @@ int main(void) terminationRequest = true; svcSignalEvent(terminationRequestEvent); } + + Sleep__HandleNotification(notifId); } while(!terminationRequest); diff --git a/sysmodules/rosalina/source/sleep.c b/sysmodules/rosalina/source/sleep.c new file mode 100644 index 0000000..4c6afe3 --- /dev/null +++ b/sysmodules/rosalina/source/sleep.c @@ -0,0 +1,62 @@ +/* +* This file is part of Luma3DS +* Copyright (C) 2016-2018 Aurora Wright, TuxSH +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +* Additional Terms 7.b and 7.c of GPLv3 apply to this file: +* * Requiring preservation of specified reasonable legal notices or +* author attributions in that material or in the Appropriate Legal +* Notices displayed by works containing it. +* * Prohibiting misrepresentation of the origin of that material, +* or requiring that modified versions of such material be marked in +* reasonable ways as different from the original version. +*/ + +#include <3ds.h> + +static bool g_isSleeping = false; +static LightEvent g_onWakeUpEvent; + +void Sleep__Init(void) +{ + srvSubscribe(0x214); ///< Sleep entry + srvSubscribe(0x213); ///< Sleep exit + LightEvent_Init(&g_onWakeUpEvent, RESET_STICKY); +} + +void Sleep__HandleNotification(u32 notifId) +{ + if (notifId == 0x214) ///< Sleep entry + { + LightEvent_Clear(&g_onWakeUpEvent); + g_isSleeping = true; + } + else if (notifId == 0x213) ///< Sleep exit + { + g_isSleeping = false; + LightEvent_Signal(&g_onWakeUpEvent); + } +} + +bool Sleep__Status(void) +{ + if (g_isSleeping) + { + LightEvent_Wait(&g_onWakeUpEvent); + svcSleepThread(1000000000ULL); + return true; + } + return false; +} diff --git a/sysmodules/rosalina/source/sock_util.c b/sysmodules/rosalina/source/sock_util.c index 8f0daf5..8f0346b 100644 --- a/sysmodules/rosalina/source/sock_util.c +++ b/sysmodules/rosalina/source/sock_util.c @@ -28,10 +28,12 @@ #include <3ds/result.h> #include <3ds/svc.h> #include <3ds/synchronization.h> +#include <3ds/services/ac.h> #include #include "memory.h" #include "minisoc.h" #include "sock_util.h" +#include "sleep.h" extern Handle terminationRequestEvent; extern bool terminationRequest; @@ -187,6 +189,14 @@ void server_run(struct sock_server *serv) for(nfds_t i = 0; i < serv->nfds; i++) fds[i].revents = 0; + + if (Sleep__Status()) + { + while (!Wifi__IsConnected() + && serv->running && !terminationRequest) + svcSleepThread(1000000000ULL); + } + int pollres = socPoll(fds, serv->nfds, 50); for(nfds_t i = 0; pollres > 0 && i < serv->nfds; i++) @@ -292,3 +302,13 @@ void server_finalize(struct sock_server *serv) svcClearEvent(serv->started_event); svcCloseHandle(serv->started_event); } + +bool Wifi__IsConnected(void) +{ + u32 status = 0; + u32 wifistatus = 0; + + acInit(); + return R_SUCCEEDED(ACU_GetWifiStatus(&wifistatus)) && wifistatus > 0 + && R_SUCCEEDED(ACU_GetStatus(&status)) && status != 1; +}