Skip to content

Commit 91d0b6c

Browse files
authored
Merge pull request #551 from jrassa/scripting
implement scripting support
2 parents d8ae0ea + 72edb79 commit 91d0b6c

9 files changed

Lines changed: 82 additions & 8 deletions

File tree

es-app/src/FileData.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Log.h"
1111
#include "MameNames.h"
1212
#include "platform.h"
13+
#include "Scripting.h"
1314
#include "SystemData.h"
1415
#include "VolumeControl.h"
1516
#include "Window.h"
@@ -282,6 +283,8 @@ void FileData::launchGame(Window* window)
282283
command = Utils::String::replace(command, "%BASENAME%", basename);
283284
command = Utils::String::replace(command, "%ROM_RAW%", rom_raw);
284285

286+
Scripting::fireEvent("game-start", rom, basename);
287+
285288
LOG(LogInfo) << " " << command;
286289
int exitCode = runSystemCommand(command);
287290

@@ -290,6 +293,8 @@ void FileData::launchGame(Window* window)
290293
LOG(LogWarning) << "...launch terminated with nonzero exit code " << exitCode << "!";
291294
}
292295

296+
Scripting::fireEvent("game-end");
297+
293298
window->init();
294299
VolumeControl::getInstance()->init();
295300
window->normalizeNextUpdate();

es-app/src/guis/GuiMenu.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "views/ViewController.h"
1414
#include "CollectionSystemManager.h"
1515
#include "EmulationStation.h"
16+
#include "Scripting.h"
1617
#include "SystemData.h"
1718
#include "VolumeControl.h"
1819
#include <SDL_events.h>
@@ -296,13 +297,15 @@ void GuiMenu::openUISettings()
296297
s->addSaveFunc([window, theme_set]
297298
{
298299
bool needReload = false;
299-
if(Settings::getInstance()->getString("ThemeSet") != theme_set->getSelected())
300+
std::string oldTheme = Settings::getInstance()->getString("ThemeSet");
301+
if(oldTheme != theme_set->getSelected())
300302
needReload = true;
301303

302304
Settings::getInstance()->setString("ThemeSet", theme_set->getSelected());
303305

304306
if(needReload)
305307
{
308+
Scripting::fireEvent("theme-changed", theme_set->getSelected(), oldTheme);
306309
CollectionSystemManager::get()->updateSystemsList();
307310
ViewController::get()->goToStart();
308311
ViewController::get()->reloadAll(); // TODO - replace this with some sort of signal-based implementation
@@ -472,6 +475,7 @@ void GuiMenu::openQuitMenu()
472475
row.makeAcceptInputHandler([window] {
473476
window->pushGui(new GuiMsgBox(window, "REALLY RESTART?", "YES",
474477
[] {
478+
Scripting::fireEvent("quit");
475479
if(quitES("/tmp/es-restart") != 0)
476480
LOG(LogWarning) << "Restart terminated with non-zero result!";
477481
}, "NO", nullptr));
@@ -487,9 +491,8 @@ void GuiMenu::openQuitMenu()
487491
row.makeAcceptInputHandler([window] {
488492
window->pushGui(new GuiMsgBox(window, "REALLY QUIT?", "YES",
489493
[] {
490-
SDL_Event ev;
491-
ev.type = SDL_QUIT;
492-
SDL_PushEvent(&ev);
494+
Scripting::fireEvent("quit");
495+
quitES("");
493496
}, "NO", nullptr));
494497
});
495498
row.addElement(std::make_shared<TextComponent>(window, "QUIT EMULATIONSTATION", Font::get(FONT_SIZE_MEDIUM), 0x777777FF), true);
@@ -500,6 +503,8 @@ void GuiMenu::openQuitMenu()
500503
row.makeAcceptInputHandler([window] {
501504
window->pushGui(new GuiMsgBox(window, "REALLY RESTART?", "YES",
502505
[] {
506+
Scripting::fireEvent("quit", "reboot");
507+
Scripting::fireEvent("reboot");
503508
if (quitES("/tmp/es-sysrestart") != 0)
504509
LOG(LogWarning) << "Restart terminated with non-zero result!";
505510
}, "NO", nullptr));
@@ -511,6 +516,8 @@ void GuiMenu::openQuitMenu()
511516
row.makeAcceptInputHandler([window] {
512517
window->pushGui(new GuiMsgBox(window, "REALLY SHUTDOWN?", "YES",
513518
[] {
519+
Scripting::fireEvent("quit", "shutdown");
520+
Scripting::fireEvent("shutdown");
514521
if (quitES("/tmp/es-shutdown") != 0)
515522
LOG(LogWarning) << "Shutdown terminated with non-zero result!";
516523
}, "NO", nullptr));

es-core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ set(CORE_SOURCES
9292
${CMAKE_CURRENT_SOURCE_DIR}/src/PowerSaver.cpp
9393
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_draw_gl.cpp
9494
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_init_sdlgl.cpp
95+
${CMAKE_CURRENT_SOURCE_DIR}/src/Scripting.cpp
9596
${CMAKE_CURRENT_SOURCE_DIR}/src/Settings.cpp
9697
${CMAKE_CURRENT_SOURCE_DIR}/src/Sound.cpp
9798
${CMAKE_CURRENT_SOURCE_DIR}/src/ThemeData.cpp

es-core/src/InputManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "CECInput.h"
55
#include "Log.h"
66
#include "platform.h"
7+
#include "Scripting.h"
78
#include "Window.h"
89
#include <pugixml/src/pugixml.hpp>
910
#include <SDL.h>
@@ -382,6 +383,9 @@ void InputManager::writeDeviceConfig(InputConfig* config)
382383

383384
config->writeToXML(root);
384385
doc.save_file(path.c_str());
386+
387+
Scripting::fireEvent("config-changed");
388+
Scripting::fireEvent("controls-changed");
385389

386390
// execute any onFinish commands and re-load the config for changes
387391
doOnFinish();

es-core/src/Scripting.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Scripting.h"
2+
#include "Log.h"
3+
#include "platform.h"
4+
#include "utils/FileSystemUtil.h"
5+
6+
namespace Scripting
7+
{
8+
void fireEvent(const std::string& eventName, const std::string& arg1, const std::string& arg2)
9+
{
10+
LOG(LogDebug) << "fireEvent: " << eventName << " " << arg1 << " " << arg2;
11+
12+
std::list<std::string> scriptDirList;
13+
std::string test;
14+
15+
// check in exepath
16+
test = Utils::FileSystem::getExePath() + "/scripts/" + eventName;
17+
if(Utils::FileSystem::exists(test))
18+
scriptDirList.push_back(test);
19+
20+
// check in homepath
21+
test = Utils::FileSystem::getHomePath() + "/.emulationstation/scripts/" + eventName;
22+
if(Utils::FileSystem::exists(test))
23+
scriptDirList.push_back(test);
24+
25+
for(std::list<std::string>::const_iterator dirIt = scriptDirList.cbegin(); dirIt != scriptDirList.cend(); ++dirIt) {
26+
std::list<std::string> scripts = Utils::FileSystem::getDirContent(*dirIt);
27+
for (std::list<std::string>::const_iterator it = scripts.cbegin(); it != scripts.cend(); ++it) {
28+
// append folder to path
29+
std::string script = *it + " \"" + arg1 + "\" \"" + arg2 + "\"";
30+
LOG(LogDebug) << " executing: " << script;
31+
runSystemCommand(script);
32+
}
33+
}
34+
}
35+
36+
} // Scripting::

es-core/src/Scripting.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#ifndef ES_CORE_SCRIPTING_H
3+
#define ES_CORE_SCRIPTING_H
4+
5+
#include <string>
6+
7+
namespace Scripting
8+
{
9+
void fireEvent(const std::string& eventName, const std::string& arg1="", const std::string& arg2="");
10+
} // Scripting::
11+
12+
#endif //ES_CORE_SCRIPTING_H

es-core/src/Settings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "utils/FileSystemUtil.h"
44
#include "Log.h"
5+
#include "Scripting.h"
56
#include "platform.h"
67
#include <pugixml/src/pugixml.hpp>
78
#include <algorithm>
@@ -193,6 +194,9 @@ void Settings::saveFile()
193194
}
194195

195196
doc.save_file(path.c_str());
197+
198+
Scripting::fireEvent("config-changed");
199+
Scripting::fireEvent("settings-changed");
196200
}
197201

198202
void Settings::loadFile()

es-core/src/Window.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "InputManager.h"
88
#include "Log.h"
99
#include "Renderer.h"
10+
#include "Scripting.h"
1011
#include <algorithm>
1112
#include <iomanip>
1213

@@ -277,8 +278,10 @@ void Window::render()
277278
if (!isProcessing() && mAllowSleep && (!mScreenSaver || mScreenSaver->allowSleep()))
278279
{
279280
// go to sleep
280-
mSleeping = true;
281-
onSleep();
281+
if (mSleeping == false) {
282+
mSleeping = true;
283+
onSleep();
284+
}
282285
}
283286
}
284287
}
@@ -401,11 +404,12 @@ void Window::setHelpPrompts(const std::vector<HelpPrompt>& prompts, const HelpSt
401404

402405
void Window::onSleep()
403406
{
407+
Scripting::fireEvent("sleep");
404408
}
405409

406410
void Window::onWake()
407411
{
408-
412+
Scripting::fireEvent("wake");
409413
}
410414

411415
bool Window::isProcessing()

es-core/src/platform.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ int runSystemCommand(const std::string& cmd_utf8)
4242

4343
int quitES(const std::string& filename)
4444
{
45-
touch(filename);
45+
if (!filename.empty())
46+
touch(filename);
4647
SDL_Event* quit = new SDL_Event();
4748
quit->type = SDL_QUIT;
4849
SDL_PushEvent(quit);

0 commit comments

Comments
 (0)