Now emulating steam apps

This commit is contained in:
Paul 2018-03-07 20:58:59 +01:00
parent 12ac4d5876
commit ed7957e653
8 changed files with 133 additions and 11381 deletions

View File

@ -0,0 +1,88 @@
#include "GameEmulator.h"
void
handle_sigterm(int signum) {
SteamAPI_Shutdown();
exit(EXIT_SUCCESS);
}
// Terminate the zombie process
void
handle_sigchld(int signum) {
pid_t pid;
GameEmulator* emulator = GameEmulator::get_instance();
while (true) {
pid = waitpid(WAIT_ANY, NULL, WNOHANG);
if (pid == 0)
return;
else if (pid == -1)
return;
else {
std::cerr << "Steam game terminated." << std::endl;
emulator->m_son_pid = -1;
}
}
}
GameEmulator*
GameEmulator::get_instance() {
static GameEmulator me;
return &me;
}
bool
GameEmulator::init_app(const std::string& app_id) {
if(m_son_pid > 0) {
std::cerr << "Warning: trying to initialize a Steam App while one is already running." << std::endl;
return false;
}
pid_t pid;
if((pid = fork()) == 0) {
//Son's process
setenv("SteamAppId", app_id.c_str(), 1);
if( !SteamAPI_Init() ) {
// TODO Pipe to parent a fail
exit(EXIT_FAILURE);
}
signal(SIGTERM, handle_sigterm);
// TODO Pipe all the shit to parent
for(;;) {
sleep(10);
std::cerr << "I'm a Steam game lol" << std::endl;
}
}
else if (pid == -1) {
std::cerr << "An error occurred while forking. Exitting." << std::endl;
exit(EXIT_FAILURE);
}
else {
//Main process
signal(SIGCHLD, handle_sigchld);
m_son_pid = pid;
}
//Only a successful parent will reach this
return true;
}
bool
GameEmulator::kill_running_app() {
if(m_son_pid > 0) {
kill(m_son_pid, SIGTERM);
// We will set the pid back to -1 when son's death is confirmed
return true;
}
else {
std::cerr << "Warning: trying to kill the Steam Game while it's not running." << std::endl;
return true;
}
return true;
}

31
SAM.Picker/GameEmulator.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <string>
#include <csignal>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "../steam/steam_api.h"
/**
* This class will play the part of being the emulated app
* Todo comment more
*/
class GameEmulator {
public:
static GameEmulator* get_instance();
bool init_app(const std::string& app_id);
bool kill_running_app();
GameEmulator(GameEmulator const&) = delete;
void operator=(GameEmulator const&) = delete;
private:
pid_t m_son_pid;
friend void handle_sigchld(int);
GameEmulator() : m_son_pid(-1) {};
~GameEmulator() {};
};

View File

@ -223,5 +223,7 @@ void
MainPickerWindow::switch_to_games_page() {
gtk_widget_set_visible(GTK_WIDGET(m_back_button), FALSE);
gtk_stack_set_visible_child(GTK_STACK(m_main_stack), GTK_WIDGET(m_game_list_view));
//TODO Clear achievments list
}
// => switch_to_games_page

View File

@ -2,9 +2,7 @@
#define MAX_PATH 1000
MySteam::MySteam()
:
m_child_pid(-1) {
MySteam::MySteam() {
}
// => Constructor
@ -26,33 +24,11 @@ MySteam::get_instance() {
bool
MySteam::launch_game(std::string appID) {
// Print an error if a game is already launched, maybe allow multiple games at the same time in the future?
if(m_child_pid > 0) {
std::cout << "Sorry, a game is already running. Please try again later." << std::endl;
return false;
}
GameEmulator* emulator = GameEmulator::get_instance();
//TODO if
emulator->init_app(appID);
//TODO Change this for release as the cwd will be different
if(!file_exists("./bin/samgame")) {
std::cout << "samgame doesn't exist. Aborting launch." << std::endl;
return false;
}
m_child_pid = create_process();
switch(m_child_pid) {
case -1:
std::cout << "An error occurred while creating the cild process." << std::endl;
perror("fork");
break;
case 0:
execl("./bin/samgame", "samgame", appID.c_str(), (char*) NULL);
break;
default:
return true;
break;
}
return false;
}
// => launch_game
@ -63,16 +39,8 @@ MySteam::launch_game(std::string appID) {
*/
bool
MySteam::quit_game() {
if(m_child_pid > 0) {
kill(m_child_pid, SIGTERM);
}
else {
std::cout << "Cannot quit game, no game launched." << std::endl;
return false;
}
m_child_pid = -1;
return true;
GameEmulator* emulator = GameEmulator::get_instance();
return emulator->kill_running_app();
}
// => quit_game

View File

@ -9,6 +9,7 @@
#include <dirent.h>
#include "Game.h"
#include "SteamAppDAO.h"
#include "GameEmulator.h"
#include "../common/functions.h"
/**
@ -82,6 +83,5 @@ public:
private:
MySteam();
pid_t m_child_pid;
std::vector<Game_t> m_all_subscribed_apps;
};

View File

@ -53,6 +53,7 @@ extern "C"
if( app_id != "0" ) {
g_main_gui->switch_to_stats_page();
g_steam->launch_game(app_id);
std::cerr << "Loading stats and achievements for appid " << app_id << std::endl;
} else {
std::cerr << "An error occurred figuring out which app to launch.. You can report this to the developer." << std::endl;
@ -63,9 +64,8 @@ extern "C"
void
on_back_button_clicked() {
// g_steam->quit_game();
g_steam->quit_game();
g_main_gui->switch_to_games_page();
std::cerr << "Back to the games list" << std::endl;
}
// => on_back_button_clicked
}

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <gmodule.h>
//#include <X11/Xlib.h>
#include "MySteam.h"
#include "MainPickerWindow.h"
#include "globals.h"
@ -30,6 +31,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
//XInitThreads();
gtk_init(&argc, &argv);
g_cache_folder = concat(getenv("HOME"), "/.SamRewritten");

File diff suppressed because it is too large Load Diff