Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Frame from Resource folder to support native MacOS port. #56

Merged
merged 1 commit into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/frontends/common2/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ include(GNUInstallDirs)

set(SOURCE_FILES
commonframe.cpp
gnuframe.cpp
fileregistry.cpp
ptreeregistry.cpp
programoptions.cpp
Expand All @@ -12,6 +13,7 @@ set(SOURCE_FILES

set(HEADER_FILES
commonframe.h
gnuframe.h
fileregistry.h
ptreeregistry.h
programoptions.h
Expand Down
85 changes: 1 addition & 84 deletions source/frontends/common2/commonframe.cpp
@@ -1,101 +1,23 @@
#include "StdAfx.h"
#include "frontends/common2/commonframe.h"
#include "frontends/common2/utils.h"
#include "frontends/common2/fileregistry.h"
#include "linux/resources.h"
#include "linux/context.h"

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>

#ifdef __APPLE__
#include "mach-o/dyld.h"
#endif

#include "Log.h"
#include "Core.h"
#include "config.h"

namespace
{

bool dirExists(const std::string & folder)
{
struct stat stdbuf;

if (stat(folder.c_str(), &stdbuf) == 0 && S_ISDIR(stdbuf.st_mode))
{
return true;
}
else
{
return false;
}
}

std::string getResourcePath(const std::string & target)
{
std::vector<std::string> paths;

char self[1024] = {0};

#ifdef __APPLE__
uint32_t size = sizeof(self);
const int ch = _NSGetExecutablePath(self, &size);
#else
const int ch = readlink("/proc/self/exe", self, sizeof(self));
#endif

if (ch != -1)
{
const char * path = dirname(self);

// case 1: run from the build folder
paths.emplace_back(std::string(path) + '/'+ ROOT_PATH);
// case 2: run from the installation folder
paths.emplace_back(std::string(path) + '/'+ SHARE_PATH);
}

// case 3: use the source folder
paths.emplace_back(CMAKE_SOURCE_DIR);

for (const std::string & path : paths)
{
char * real = realpath(path.c_str(), nullptr);
if (real)
{
const std::string resourcePath = std::string(real) + target;
free(real);
if (dirExists(resourcePath))
{
return resourcePath;
}
}
}

throw std::runtime_error("Cannot found the resource path: " + target);
}

}

namespace common2
{

CommonFrame::CommonFrame()
: myResourcePath(getResourcePath("/resource/"))
{
// should this go down to LinuxFrame (maybe Initialisation?)
g_sProgramDir = getResourcePath("/bin/");
}

BYTE* CommonFrame::GetResource(WORD id, LPCSTR lpType, DWORD expectedSize)
{
myResource.clear();

const std::string & filename = getResourceName(id);
const std::string path = myResourcePath + filename;
const std::string path = getResourcePath(filename);

const int fd = open(path.c_str(), O_RDONLY);

Expand Down Expand Up @@ -133,9 +55,4 @@ namespace common2
return resource;
}

std::string CommonFrame::Video_GetScreenShotFolder()
{
return GetHomeDir() + "/Pictures/";
}

}
7 changes: 2 additions & 5 deletions source/frontends/common2/commonframe.h
Expand Up @@ -10,16 +10,13 @@ namespace common2
class CommonFrame : public LinuxFrame
{
public:
CommonFrame();

BYTE* GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) override;

std::string Video_GetScreenShotFolder() override;

protected:
virtual std::string getResourcePath(const std::string & filename) = 0;

static std::string getBitmapFilename(const std::string & resource);

const std::string myResourcePath;
std::vector<BYTE> myResource;
};

Expand Down
99 changes: 99 additions & 0 deletions source/frontends/common2/gnuframe.cpp
@@ -0,0 +1,99 @@
#include "StdAfx.h"
#include "frontends/common2/gnuframe.h"
#include "frontends/common2/fileregistry.h"

#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>

#ifdef __APPLE__
#include "mach-o/dyld.h"
#endif

#include "Core.h"
#include "config.h"

namespace
{

bool dirExists(const std::string & folder)
{
struct stat stdbuf;

if (stat(folder.c_str(), &stdbuf) == 0 && S_ISDIR(stdbuf.st_mode))
{
return true;
}
else
{
return false;
}
}

std::string getResourceFolder(const std::string & target)
{
std::vector<std::string> paths;

char self[1024] = {0};

#ifdef __APPLE__
uint32_t size = sizeof(self);
const int ch = _NSGetExecutablePath(self, &size);
#else
const int ch = readlink("/proc/self/exe", self, sizeof(self));
#endif

if (ch != -1)
{
const char * path = dirname(self);

// case 1: run from the build folder
paths.emplace_back(std::string(path) + '/'+ ROOT_PATH);
// case 2: run from the installation folder
paths.emplace_back(std::string(path) + '/'+ SHARE_PATH);
}

// case 3: use the source folder
paths.emplace_back(CMAKE_SOURCE_DIR);

for (const std::string & path : paths)
{
char * real = realpath(path.c_str(), nullptr);
if (real)
{
const std::string resourcePath = std::string(real) + target;
free(real);
if (dirExists(resourcePath))
{
return resourcePath;
}
}
}

throw std::runtime_error("Cannot found the resource path: " + target);
}

}

namespace common2
{

GNUFrame::GNUFrame()
: myHomeDir(GetHomeDir())
, myResourceFolder(getResourceFolder("/resource/"))
{
// should this go down to LinuxFrame (maybe Initialisation?)
g_sProgramDir = getResourceFolder("/bin/");
}

std::string GNUFrame::getResourcePath(const std::string & filename)
{
return myResourceFolder + filename;
}

std::string GNUFrame::Video_GetScreenShotFolder()
{
return myHomeDir + "/Pictures/";
}

}
22 changes: 22 additions & 0 deletions source/frontends/common2/gnuframe.h
@@ -0,0 +1,22 @@
#pragma once

#include "frontends/common2/commonframe.h"
#include <string>

namespace common2
{

class GNUFrame : public virtual CommonFrame
{
public:
GNUFrame();

std::string Video_GetScreenShotFolder() override;
std::string getResourcePath(const std::string & filename) override;

private:
const std::string myHomeDir;
const std::string myResourceFolder;
};

}
2 changes: 1 addition & 1 deletion source/frontends/libretro/retroframe.cpp
Expand Up @@ -139,7 +139,7 @@ namespace ra2
void RetroFrame::GetBitmap(LPCSTR lpBitmapName, LONG cb, LPVOID lpvBits)
{
const std::string filename = getBitmapFilename(lpBitmapName);
const std::string path = myResourcePath + filename;
const std::string path = getResourcePath(filename);

std::vector<char> buffer;
readFileToBuffer(path, buffer);
Expand Down
3 changes: 2 additions & 1 deletion source/frontends/libretro/retroframe.h
@@ -1,14 +1,15 @@
#pragma once

#include "frontends/common2/commonframe.h"
#include "frontends/common2/gnuframe.h"

#include <memory>
#include <vector>

namespace ra2
{

class RetroFrame : public common2::CommonFrame
class RetroFrame : public virtual common2::CommonFrame, public common2::GNUFrame
{
public:
RetroFrame();
Expand Down
3 changes: 2 additions & 1 deletion source/frontends/ncurses/nframe.h
@@ -1,6 +1,7 @@
#pragma once

#include "frontends/common2/commonframe.h"
#include "frontends/common2/gnuframe.h"

#include <memory>
#include <string>
Expand All @@ -13,7 +14,7 @@ namespace na2
class EvDevPaddle;
struct NCurses;

class NFrame : public common2::CommonFrame
class NFrame : public virtual common2::CommonFrame, public common2::GNUFrame
{
public:
NFrame(const std::shared_ptr<EvDevPaddle> & paddle);
Expand Down
3 changes: 2 additions & 1 deletion source/frontends/sdl/imgui/sdlimguiframe.h
Expand Up @@ -3,6 +3,7 @@
#include "frontends/sdl/sdlframe.h"
#include "frontends/sdl/imgui/sdlsettings.h"
#include "frontends/sdl/imgui/glselector.h"
#include "frontends/common2/gnuframe.h"

namespace common2
{
Expand All @@ -12,7 +13,7 @@ namespace common2
namespace sa2
{

class SDLImGuiFrame : public SDLFrame
class SDLImGuiFrame : public SDLFrame, public common2::GNUFrame
{
public:
SDLImGuiFrame(const common2::EmulatorOptions & options);
Expand Down
3 changes: 2 additions & 1 deletion source/frontends/sdl/renderer/sdlrendererframe.h
@@ -1,6 +1,7 @@
#pragma once

#include "frontends/sdl/sdlframe.h"
#include "frontends/common2/gnuframe.h"

namespace common2
{
Expand All @@ -10,7 +11,7 @@ namespace common2
namespace sa2
{

class SDLRendererFrame : public SDLFrame
class SDLRendererFrame : public SDLFrame, public common2::GNUFrame
{
public:
SDLRendererFrame(const common2::EmulatorOptions & options);
Expand Down
4 changes: 2 additions & 2 deletions source/frontends/sdl/sdlframe.cpp
Expand Up @@ -178,7 +178,7 @@ namespace sa2

void SDLFrame::SetApplicationIcon()
{
const std::string path = myResourcePath + "APPLEWIN.ICO";
const std::string path = getResourcePath("APPLEWIN.ICO");
std::shared_ptr<SDL_Surface> icon(IMG_Load(path.c_str()), SDL_FreeSurface);
if (icon)
{
Expand All @@ -194,7 +194,7 @@ namespace sa2
void SDLFrame::GetBitmap(LPCSTR lpBitmapName, LONG cb, LPVOID lpvBits)
{
const std::string filename = getBitmapFilename(lpBitmapName);
const std::string path = myResourcePath + filename;
const std::string path = getResourcePath(filename);

std::shared_ptr<SDL_Surface> surface(SDL_LoadBMP(path.c_str()), SDL_FreeSurface);
if (surface)
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/sdl/sdlframe.h
Expand Up @@ -14,7 +14,7 @@ namespace common2
namespace sa2
{

class SDLFrame : public common2::CommonFrame
class SDLFrame : public virtual common2::CommonFrame
{
public:
SDLFrame(const common2::EmulatorOptions & options);
Expand Down