Skip to content

Commit

Permalink
working on unix instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Feb 3, 2024
1 parent 914c75b commit effca18
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Created : 02/02/2024
// Updated : 03/02/2024

#ifndef __AK_CORE_OS__
#define __AK_CORE_OS__
#ifndef __AK_CORE_OS_INSTANCE__
#define __AK_CORE_OS_INSTANCE__

#include <Core/PreCompiled.h>

Expand All @@ -13,19 +13,19 @@ namespace Ak
class OSInstance
{
public:
OSInstance() = default;

virtual void Init() = 0;
virtual void Init() {}
virtual void Shutdown() = 0;

virtual std::filesystem::path GetExecutablePath() = 0;
virtual std::filesystem::path GetCurrentWorkingDirectoryPath() = 0;
virtual bool OpenURL([[maybe_unused]] const std::string& url) { return false; }
virtual void Delay(std::uint32_t ms) {}
virtual void Delay(std::uint32_t us) {}

virtual ~OSInstance() = default;
virtual OSInstance& Get() = 0;

private:
protected:
OSInstance() = default;
virtual ~OSInstance() = default;
};
}

Expand Down
2 changes: 2 additions & 0 deletions Akel/Runtime/Includes/Drivers/Unix/PreCompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

#include <Core/CompilationProfile.h>
#include <Drivers/Unix/Unix.h>
#include <ctime>
#include <unistd.h>

#endif
21 changes: 21 additions & 0 deletions Akel/Runtime/Includes/Drivers/Unix/UnixInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,31 @@
#define __AK_DRIVERS_UNIX_INSTANCE__

#include <Drivers/Unix/PreCompiled.h>
#include <Core/OS/OSInstance.h>

namespace Ak
{
class AK_UNIX_API UnixInstance final : public OSInstance
{
public:
void Init(int ac, char** av);
void Shutdown() override;

std::filesystem::path GetExecutablePath() override;
std::filesystem::path GetCurrentWorkingDirectoryPath() override;
bool OpenURL([[maybe_unused]] const std::string& url) override;
void Delay(std::uint32_t us) override;

OSInstance& Get() override;

private:
UnixInstance() = default;
~UnixInstance() override = default;

private:
char** m_av = nullptr;
int m_ac;
};
}

#endif
56 changes: 55 additions & 1 deletion Akel/Runtime/Sources/Drivers/Unix/UnixInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,61 @@

#include <Drivers/Unix/UnixInstance.h>

namespace AK
namespace Ak
{
constexpr const int UNIX_PATH_MAX = 1024;

void UnixInstance::Init(int ac, char** av)
{
m_av = av;
m_ac = ac;
}

void UnixInstance::Shutdown()
{

}

std::filesystem::path UnixInstance::GetExecutablePath()
{
std::string path(UNIX_PATH_MAX, 0);
if(std::filesystem::exists("/proc"))
{
int nchar = readlink("/proc/self/exe", path.data(), UNIX_PATH_MAX);
if(nchar >= 0 && nchar < UNIX_PATH_MAX)
return std::filesystem::path(path);
}
if(m_av == nullptr)
return {};
if(m_av[0] && realpath(m_av[0], path.data()))
return std::filesystem::path(path);
return {};
}

std::filesystem::path UnixInstance::GetCurrentWorkingDirectoryPath()
{

}

bool UnixInstance::OpenURL([[maybe_unused]] const std::string& url)
{

}

void UnixInstance::Delay(std::uint32_t us)
{
struct timespec requested = { static_cast<time_t>(us / 1000000), (static_cast<long>(us) % 1000000) * 1000 };
struct timespec remaining;
while(nanosleep(&requested, &remaining) == -1)
{
requested.tv_sec = remaining.tv_sec;
requested.tv_nsec = remaining.tv_nsec;
}
}

OSInstance& UnixInstance::Get()
{
static UnixInstance instance;
return instance;
}
}

0 comments on commit effca18

Please sign in to comment.