1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21// LOVE
22#include "System.h"
23#include "window/Window.h"
24
25// SDL
26#include <SDL_clipboard.h>
27#include <SDL_cpuinfo.h>
28
29namespace love
30{
31namespace system
32{
33namespace sdl
34{
35
36System::System()
37{
38}
39
40const char *System::getName() const
41{
42 return "love.system.sdl";
43}
44
45int System::getProcessorCount() const
46{
47 return SDL_GetCPUCount();
48}
49
50bool System::isWindowOpen() const
51{
52 auto window = Module::getInstance<window::Window>(M_WINDOW);
53 return window != nullptr && window->isOpen();
54}
55
56void System::setClipboardText(const std::string &text) const
57{
58 // SDL requires the video subsystem to be initialized and a window to be
59 // opened in order for clipboard text to work, on at least some platforms.
60 if (!isWindowOpen())
61 throw love::Exception("A window must be created in order for setClipboardText to function properly.");
62
63 SDL_SetClipboardText(text.c_str());
64}
65
66std::string System::getClipboardText() const
67{
68 if (!isWindowOpen())
69 throw love::Exception("A window must be created in order for getClipboardText to function properly.");
70
71 std::string text("");
72
73 char *ctext = SDL_GetClipboardText();
74 if (ctext)
75 {
76 text = std::string(ctext);
77 SDL_free(ctext);
78 }
79
80 return text;
81}
82
83love::system::System::PowerState System::getPowerInfo(int &seconds, int &percent) const
84{
85 SDL_PowerState sdlstate = SDL_GetPowerInfo(&seconds, &percent);
86
87 PowerState state = POWER_UNKNOWN;
88 powerStates.find(sdlstate, state);
89
90 return state;
91}
92
93EnumMap<System::PowerState, SDL_PowerState, System::POWER_MAX_ENUM>::Entry System::powerEntries[] =
94{
95 {System::POWER_UNKNOWN, SDL_POWERSTATE_UNKNOWN},
96 {System::POWER_BATTERY, SDL_POWERSTATE_ON_BATTERY},
97 {System::POWER_NO_BATTERY, SDL_POWERSTATE_NO_BATTERY},
98 {System::POWER_CHARGING, SDL_POWERSTATE_CHARGING},
99 {System::POWER_CHARGED, SDL_POWERSTATE_CHARGED},
100};
101
102EnumMap<System::PowerState, SDL_PowerState, System::POWER_MAX_ENUM> System::powerStates(System::powerEntries, sizeof(System::powerEntries));
103
104} // sdl
105} // system
106} // love
107