2013-12-19 17:10:14 +00:00
|
|
|
#pragma once
|
|
|
|
#include "types.h"
|
2020-03-28 16:58:01 +00:00
|
|
|
|
2020-04-02 16:48:23 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2020-04-09 09:44:19 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2020-04-04 08:53:19 +00:00
|
|
|
#include <thread>
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-08-25 16:38:36 +00:00
|
|
|
#ifdef __ANDROID__
|
2013-12-19 17:10:14 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#undef PAGE_MASK
|
|
|
|
#define PAGE_MASK (PAGE_SIZE-1)
|
|
|
|
#else
|
2021-07-23 20:34:12 +00:00
|
|
|
#if defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
#define PAGE_SIZE 16384
|
|
|
|
#else
|
2013-12-19 17:10:14 +00:00
|
|
|
#define PAGE_SIZE 4096
|
2021-07-23 20:34:12 +00:00
|
|
|
#endif
|
2013-12-19 17:10:14 +00:00
|
|
|
#define PAGE_MASK (PAGE_SIZE-1)
|
|
|
|
#endif
|
|
|
|
|
2020-04-04 08:53:19 +00:00
|
|
|
class cThread
|
|
|
|
{
|
2013-12-19 17:10:14 +00:00
|
|
|
private:
|
2020-04-04 08:53:19 +00:00
|
|
|
typedef void* ThreadEntryFP(void* param);
|
2019-04-27 12:08:43 +00:00
|
|
|
ThreadEntryFP* entry;
|
2013-12-19 17:10:14 +00:00
|
|
|
void* param;
|
2020-04-04 08:53:19 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::thread thread;
|
2019-04-27 12:08:43 +00:00
|
|
|
|
|
|
|
cThread(ThreadEntryFP* function, void* param)
|
2020-04-04 08:53:19 +00:00
|
|
|
:entry(function), param(param) {}
|
2019-04-27 12:08:43 +00:00
|
|
|
~cThread() { WaitToEnd(); }
|
2013-12-19 17:10:14 +00:00
|
|
|
void Start();
|
|
|
|
void WaitToEnd();
|
|
|
|
};
|
2019-04-27 12:08:43 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
class cResetEvent
|
|
|
|
{
|
|
|
|
private:
|
2020-04-02 16:48:23 +00:00
|
|
|
std::mutex mutx;
|
|
|
|
std::condition_variable cond;
|
2019-04-27 12:08:43 +00:00
|
|
|
bool state;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
public :
|
2019-04-27 12:08:43 +00:00
|
|
|
cResetEvent();
|
2013-12-19 17:10:14 +00:00
|
|
|
~cResetEvent();
|
|
|
|
void Set(); //Set state to signaled
|
|
|
|
void Reset(); //Set state to non signaled
|
2018-10-29 19:03:47 +00:00
|
|
|
bool Wait(u32 msec);//Wait for signal , then reset[if auto]. Returns false if timed out
|
2013-12-19 17:10:14 +00:00
|
|
|
void Wait(); //Wait for signal , then reset[if auto]
|
|
|
|
};
|
|
|
|
|
2020-03-29 17:29:14 +00:00
|
|
|
void set_user_config_dir(const std::string& dir);
|
|
|
|
void set_user_data_dir(const std::string& dir);
|
|
|
|
void add_system_config_dir(const std::string& dir);
|
|
|
|
void add_system_data_dir(const std::string& dir);
|
2015-08-22 19:49:12 +00:00
|
|
|
|
2020-03-29 17:29:14 +00:00
|
|
|
std::string get_writable_config_path(const std::string& filename);
|
|
|
|
std::string get_writable_data_path(const std::string& filename);
|
|
|
|
std::string get_readonly_config_path(const std::string& filename);
|
|
|
|
std::string get_readonly_data_path(const std::string& filename);
|
|
|
|
bool file_exists(const std::string& filename);
|
|
|
|
bool make_directory(const std::string& path);
|
|
|
|
|
2021-03-02 17:45:18 +00:00
|
|
|
// returns a prefix for a game save file, for example: ~/.local/share/flycast/mvsc2.zip
|
2020-03-29 17:29:14 +00:00
|
|
|
std::string get_game_save_prefix();
|
2021-03-02 17:45:18 +00:00
|
|
|
// returns the full path of the game, without the file extension
|
2020-03-29 17:29:14 +00:00
|
|
|
std::string get_game_basename();
|
2021-03-02 17:45:18 +00:00
|
|
|
// returns the game directory
|
2020-03-29 17:29:14 +00:00
|
|
|
std::string get_game_dir();
|
2021-03-02 17:45:18 +00:00
|
|
|
// returns the position of the last path separator, or string::npos if none
|
2020-11-26 15:45:57 +00:00
|
|
|
size_t get_last_slash_pos(const std::string& path);
|
2020-03-29 17:29:14 +00:00
|
|
|
|
|
|
|
bool mem_region_lock(void *start, std::size_t len);
|
|
|
|
bool mem_region_unlock(void *start, std::size_t len);
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-08-30 21:35:10 +00:00
|
|
|
class VArray2 {
|
2019-05-11 20:09:52 +00:00
|
|
|
public:
|
2013-12-19 17:10:14 +00:00
|
|
|
u8* data;
|
2019-05-11 20:09:52 +00:00
|
|
|
unsigned size;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-05-11 20:09:52 +00:00
|
|
|
void Zero() {
|
2020-03-29 17:29:14 +00:00
|
|
|
std::memset(data, 0, size);
|
2019-05-11 20:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INLINE u8& operator [](unsigned i) {
|
2013-12-19 17:10:14 +00:00
|
|
|
#ifdef MEM_BOUND_CHECK
|
2019-05-11 20:09:52 +00:00
|
|
|
if (i >= size)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2019-08-30 21:35:10 +00:00
|
|
|
ERROR_LOG(COMMON, "Error: VArray2 , index out of range (%d > %d)\n", i, size - 1);
|
2013-12-19 17:10:14 +00:00
|
|
|
MEM_DO_BREAK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
};
|
2020-04-09 09:44:19 +00:00
|
|
|
|
|
|
|
static inline void string_tolower(std::string& s)
|
|
|
|
{
|
|
|
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::string get_file_extension(const std::string& s)
|
|
|
|
{
|
|
|
|
size_t dot = s.find_last_of('.');
|
|
|
|
if (dot == std::string::npos)
|
|
|
|
return "";
|
|
|
|
std::string ext = s.substr(dot + 1, s.length() - dot - 1);
|
|
|
|
string_tolower(ext);
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline std::string get_file_basename(const std::string& s)
|
|
|
|
{
|
|
|
|
size_t dot = s.find_last_of('.');
|
|
|
|
if (dot == std::string::npos)
|
|
|
|
return s;
|
|
|
|
return s.substr(0, dot);
|
|
|
|
}
|
2020-12-28 10:31:26 +00:00
|
|
|
|
|
|
|
static inline std::string trim_trailing_ws(const std::string& str,
|
|
|
|
const std::string& whitespace = " ")
|
|
|
|
{
|
|
|
|
const auto strEnd = str.find_last_not_of(whitespace);
|
|
|
|
if (strEnd == std::string::npos)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return str.substr(0, strEnd + 1);
|
|
|
|
}
|