flycast/core/linux-dist/main.cpp

359 lines
7.5 KiB
C++
Raw Normal View History

2013-12-19 17:10:14 +00:00
#include "types.h"
#if HOST_OS==OS_LINUX
#include "hw/sh4/dyna/blockmanager.h"
#include "log/LogManager.h"
#include "emulator.h"
2020-03-28 16:58:01 +00:00
#include <cstdarg>
#include <csignal>
2013-12-19 17:10:14 +00:00
#include <unistd.h>
2016-03-02 05:48:34 +00:00
#if defined(SUPPORT_DISPMANX)
2019-09-07 12:37:39 +00:00
#include "dispmanx.h"
2016-03-02 05:48:34 +00:00
#endif
#if defined(SUPPORT_X11)
2019-09-07 12:37:39 +00:00
#include "x11.h"
2013-12-19 17:10:14 +00:00
#endif
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
#include "sdl/sdl.h"
#endif
#include <sys/stat.h>
2013-12-19 17:10:14 +00:00
#if defined(USE_EVDEV)
2019-09-07 12:37:39 +00:00
#include "evdev.h"
#endif
#if defined(USE_JOYSTICK)
2020-03-28 16:58:01 +00:00
#include "cfg/cfg.h"
2019-09-07 12:37:39 +00:00
#include "joystick.h"
#endif
#ifdef TARGET_PANDORA
2015-08-17 21:46:28 +00:00
#include <signal.h>
#include <execinfo.h>
2015-08-21 00:52:49 +00:00
#include <sys/soundcard.h>
#endif
#if defined(USE_JOYSTICK)
2015-08-17 21:46:28 +00:00
/* legacy joystick input */
static int joystick_fd = -1; // Joystick file descriptor
#endif
void os_SetupInput()
{
#if defined(USE_EVDEV)
input_evdev_init();
#endif
2015-08-17 21:46:28 +00:00
#if defined(USE_JOYSTICK)
int joystick_device_id = cfgLoadInt("input", "joystick_device_id", JOYSTICK_DEFAULT_DEVICE_ID);
if (joystick_device_id < 0) {
INFO_LOG(INPUT, "Legacy Joystick input disabled by config.");
}
else
{
int joystick_device_length = snprintf(NULL, 0, JOYSTICK_DEVICE_STRING, joystick_device_id);
char* joystick_device = (char*)malloc(joystick_device_length + 1);
sprintf(joystick_device, JOYSTICK_DEVICE_STRING, joystick_device_id);
joystick_fd = input_joystick_init(joystick_device);
free(joystick_device);
}
#endif
2015-08-17 21:46:28 +00:00
#if defined(SUPPORT_X11)
input_x11_init();
#endif
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
input_sdl_init();
#endif
2013-12-19 17:10:14 +00:00
}
void UpdateInputState(u32 port)
{
2015-08-17 21:46:28 +00:00
#if defined(USE_JOYSTICK)
input_joystick_handle(joystick_fd, port);
#endif
2015-08-17 21:46:28 +00:00
#if defined(USE_EVDEV)
input_evdev_handle(port);
2015-08-17 21:46:28 +00:00
#endif
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
input_sdl_handle(port);
#endif
2013-12-19 17:10:14 +00:00
}
void os_DoEvents()
{
2015-08-17 21:46:28 +00:00
#if defined(SUPPORT_X11)
input_x11_handle();
event_x11_handle();
2015-08-17 21:46:28 +00:00
#endif
2013-12-19 17:10:14 +00:00
}
void os_SetWindowText(const char * text)
{
2015-08-17 21:46:28 +00:00
#if defined(SUPPORT_X11)
x11_window_set_text(text);
#endif
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
sdl_window_set_text(text);
#endif
2013-12-19 17:10:14 +00:00
}
void os_CreateWindow()
{
2016-03-02 05:48:34 +00:00
#if defined(SUPPORT_DISPMANX)
dispmanx_window_create();
#endif
2015-08-17 21:46:28 +00:00
#if defined(SUPPORT_X11)
x11_window_create();
#endif
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
sdl_window_create();
#endif
2013-12-19 17:10:14 +00:00
}
void common_linux_setup();
void* rend_thread(void* p);
2013-12-19 17:10:14 +00:00
#ifdef TARGET_PANDORA
2015-08-17 21:46:28 +00:00
void clean_exit(int sig_num)
{
void* array[10];
size_t size;
if (joystick_fd >= 0) { close(joystick_fd); }
for (int port = 0; port < 4 ; port++)
{
if (evdev_controllers[port]->fd >= 0)
2015-08-17 21:46:28 +00:00
{
close(evdev_controllers[port]->fd);
2015-08-17 21:46:28 +00:00
}
}
2020-03-28 16:58:01 +00:00
x11_window_destroy();
2015-08-17 21:46:28 +00:00
// finish cleaning
if (sig_num!=0)
{
write(2, "\nSignal received\n", sizeof("\nSignal received\n"));
size = backtrace(array, 10);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
}
#endif
2020-03-29 17:29:14 +00:00
std::string find_user_config_dir()
{
struct stat info;
std::string home = "";
if(getenv("HOME") != NULL)
{
// Support for the legacy config dir at "$HOME/.reicast"
std::string legacy_home = (std::string)getenv("HOME") + "/.reicast";
if((stat(legacy_home.c_str(), &info) == 0) && (info.st_mode & S_IFDIR))
{
// "$HOME/.reicast" already exists, let's use it!
return legacy_home;
}
/* If $XDG_CONFIG_HOME is not set, we're supposed to use "$HOME/.config" instead.
* Consult the XDG Base Directory Specification for details:
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
*/
home = (std::string)getenv("HOME") + "/.config/reicast";
}
if(getenv("XDG_CONFIG_HOME") != NULL)
{
// If XDG_CONFIG_HOME is set explicitly, we'll use that instead of $HOME/.config
home = (std::string)getenv("XDG_CONFIG_HOME") + "/reicast";
}
if(!home.empty())
{
if((stat(home.c_str(), &info) != 0) || !(info.st_mode & S_IFDIR))
{
// If the directory doesn't exist yet, create it!
mkdir(home.c_str(), 0755);
}
return home;
}
// Unable to detect config dir, use the current folder
return ".";
}
2020-03-29 17:29:14 +00:00
std::string find_user_data_dir()
{
struct stat info;
std::string data = "";
if(getenv("HOME") != NULL)
{
// Support for the legacy config dir at "$HOME/.reicast"
std::string legacy_data = (std::string)getenv("HOME") + "/.reicast";
if((stat(legacy_data.c_str(), &info) == 0) && (info.st_mode & S_IFDIR))
{
// "$HOME/.reicast" already exists, let's use it!
return legacy_data;
}
2018-08-13 11:00:24 +00:00
/* If $XDG_DATA_HOME is not set, we're supposed to use "$HOME/.local/share" instead.
* Consult the XDG Base Directory Specification for details:
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
*/
data = (std::string)getenv("HOME") + "/.local/share/reicast";
}
if(getenv("XDG_DATA_HOME") != NULL)
{
// If XDG_DATA_HOME is set explicitly, we'll use that instead of $HOME/.config
data = (std::string)getenv("XDG_DATA_HOME") + "/reicast";
}
if(!data.empty())
{
if((stat(data.c_str(), &info) != 0) || !(info.st_mode & S_IFDIR))
{
// If the directory doesn't exist yet, create it!
mkdir(data.c_str(), 0755);
}
return data;
}
// Unable to detect config dir, use the current folder
return ".";
}
2020-03-29 17:29:14 +00:00
std::vector<std::string> find_system_config_dirs()
{
2020-03-29 17:29:14 +00:00
std::vector<std::string> dirs;
if (getenv("XDG_CONFIG_DIRS") != NULL)
{
2020-03-29 17:29:14 +00:00
std::string s = (std::string)getenv("XDG_CONFIG_DIRS");
2020-03-29 17:29:14 +00:00
std::string::size_type pos = 0;
std::string::size_type n = s.find(':', pos);
while(n != std::string::npos)
{
dirs.push_back(s.substr(pos, n-pos) + "/reicast");
pos = n + 1;
2019-09-07 12:37:39 +00:00
n = s.find(':', pos);
}
// Separator not found
dirs.push_back(s.substr(pos) + "/reicast");
}
else
{
dirs.push_back("/etc/reicast"); // This isn't part of the XDG spec, but much more common than /etc/xdg/
dirs.push_back("/etc/xdg/reicast");
}
return dirs;
}
2020-03-29 17:29:14 +00:00
std::vector<std::string> find_system_data_dirs()
{
2020-03-29 17:29:14 +00:00
std::vector<std::string> dirs;
if (getenv("XDG_DATA_DIRS") != NULL)
{
2020-03-29 17:29:14 +00:00
std::string s = (std::string)getenv("XDG_DATA_DIRS");
2020-03-29 17:29:14 +00:00
std::string::size_type pos = 0;
std::string::size_type n = s.find(':', pos);
while(n != std::string::npos)
{
dirs.push_back(s.substr(pos, n-pos) + "/reicast");
pos = n + 1;
2019-09-07 12:37:39 +00:00
n = s.find(':', pos);
}
// Separator not found
dirs.push_back(s.substr(pos) + "/reicast");
}
else
{
dirs.push_back("/usr/local/share/reicast");
dirs.push_back("/usr/share/reicast");
}
return dirs;
}
2020-01-31 22:51:12 +00:00
int main(int argc, char* argv[])
2013-12-19 17:10:14 +00:00
{
LogManager::Init();
2015-08-17 21:46:28 +00:00
#ifdef TARGET_PANDORA
signal(SIGSEGV, clean_exit);
signal(SIGKILL, clean_exit);
#endif
/* Set directories */
set_user_config_dir(find_user_config_dir());
set_user_data_dir(find_user_data_dir());
2020-03-29 17:29:14 +00:00
std::vector<std::string> dirs;
dirs = find_system_config_dirs();
2019-09-07 12:37:39 +00:00
for (std::size_t i = 0; i < dirs.size(); i++)
{
add_system_data_dir(dirs[i]);
}
dirs = find_system_data_dirs();
2019-09-07 12:37:39 +00:00
for (std::size_t i = 0; i < dirs.size(); i++)
{
add_system_data_dir(dirs[i]);
}
INFO_LOG(BOOT, "Config dir is: %s", get_writable_config_path("/").c_str());
INFO_LOG(BOOT, "Data dir is: %s", get_writable_data_path("/").c_str());
2015-08-17 21:46:28 +00:00
2015-08-29 18:23:16 +00:00
#if defined(USE_SDL)
2020-07-06 15:29:53 +00:00
// init video now: on rpi3 it installs a sigsegv handler(?)
if (SDL_Init(SDL_INIT_VIDEO) != 0)
2015-08-29 18:23:16 +00:00
{
die("SDL: Initialization failed!");
}
#endif
2015-08-17 21:46:28 +00:00
common_linux_setup();
settings.profile.run_counts=0;
if (reicast_init(argc, argv))
die("Reicast initialization failed\n");
2015-08-17 21:46:28 +00:00
rend_thread(NULL);
2015-08-17 21:46:28 +00:00
#ifdef TARGET_PANDORA
clean_exit(0);
#endif
2018-04-27 10:37:56 +00:00
dc_term();
#if defined(USE_EVDEV)
input_evdev_close();
#endif
#if defined(SUPPORT_X11)
x11_window_destroy();
#endif
2018-04-27 10:37:56 +00:00
2019-10-05 09:50:14 +00:00
#if defined(USE_SDL)
sdl_window_destroy();
#endif
2015-08-17 21:46:28 +00:00
return 0;
}
#endif
2014-03-20 15:13:53 +00:00
int get_mic_data(u8* buffer) { return 0; }
void os_DebugBreak()
{
raise(SIGTRAP);
}