Skip to content

Commit

Permalink
meta: Update from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 22, 2024
1 parent f1b806d commit ec22ccb
Show file tree
Hide file tree
Showing 66 changed files with 444 additions and 294 deletions.
3 changes: 1 addition & 2 deletions src/impls/impl-darwin/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
#include <impl-posix/fd.h>
#include <impl-posix/utils.h>
#include <karm-base/map.h>
#include <karm-sys/_embed.h>
#include <karm-sys/async.h>
#include <karm-sys/time.h>

#include <karm-sys/_embed.h>

namespace Karm::Sys::_Embed {

struct DarwinSched :
Expand Down
68 changes: 68 additions & 0 deletions src/impls/impl-posix/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <karm-app/_embed.h>
#include <karm-app/prefs.h>
#include <karm-json/parse.h>
#include <karm-logger/logger.h>
#include <karm-mime/url.h>
#include <karm-pkg/_embed.h>
#include <karm-sys/dir.h>
#include <karm-sys/file.h>

namespace Karm::App::_Ember {

struct XdgConfigPrefs : public Prefs {
Mime::Url _url;

XdgConfigPrefs(Mime::Url url)
: _url{std::move(url)} {}

Json::Value _load() {
auto data = Sys::readAllUtf8(_url).unwrapOr("{}"s);
Io::SScan s{data};
return Json::parse(s).unwrapOr(Json::Object{});
}

Res<> _save(Json::Value object) {
auto file = try$(Sys::File::create(_url));
Io::TextEncoder<> enc{file};
Io::Emit e{enc};
return Json::stringify(e, object);
}

Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue) {
auto object = _load();
auto value = object.get(key);
co_return Ok(value == NONE ? defaultValue : value);
}

Async::Task<> saveAsync(String key, Json::Value value) {
auto object = _load();
object.set(key, value);
co_return _save(object);
}
};

static Opt<XdgConfigPrefs> _globalPrefs;

static Res<Mime::Url> _resolveConfigDir() {
auto *xdgConfigHome = getenv("XDG_CONFIG_HOME");
if (xdgConfigHome)
return Ok(Mime::Url::parse(xdgConfigHome) / try$(Pkg::_Embed::currentBundle()));

auto *home = getenv("HOME");
if (home)
return Ok(Mime::Url::parse(home) / ".config" / try$(Pkg::_Embed::currentBundle()));

return Error::notFound("could not find XDG_CONFIG_HOME nor HOME");
}

Prefs &globalPrefs() {
if (not _globalPrefs) {
auto url = _resolveConfigDir().unwrap("could not resolve preferences directory");
Sys::Dir::openOrCreate(url).unwrap("could not create preferences directory");
logInfo("preferences stored at {}", url);
_globalPrefs = XdgConfigPrefs{url / "configs.json"};
}
return *_globalPrefs;
}

} // namespace Karm::App::_Ember
5 changes: 2 additions & 3 deletions src/impls/impl-posix/base.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <execinfo.h>
#include <karm-base/_embed.h>
#include <karm-base/backtrace.h>
#include <karm-io/aton.h>
#include <stdio.h>
#include <stdlib.h>

#include <karm-base/_embed.h>

namespace Karm::_Embed {

// MARK: Backtrace -------------------------------------------------------------
Expand All @@ -21,7 +20,7 @@ Backtrace forceCaptureBacktrace() {
char **symbols = backtrace_symbols(buffer, count);

for (int i = 0; i < count; i++) {
char const *symbol = symbols[i];
char *symbol = symbols[i];
bt._frames.pushBack({
String(symbol),
String("unknown"),
Expand Down
4 changes: 2 additions & 2 deletions src/impls/impl-posix/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
void __panicHandler(Karm::PanicKind kind, char const *msg) {
fprintf(stderr, "%s: %s\n", kind == Karm::PanicKind::PANIC ? "panic" : "debug", msg);

// NOTE: We are calling backinto the framework here, it might cause another
// NOTE: We hare calling backinto the framework here, it might cause another
// panic, this is why we are keeping track of nested panics
static isize _panicDepth = 0;
static isize _panicDepth = 1;
_panicDepth++;
if (_panicDepth == 1) {
auto bt = Karm::Backtrace::capture();
Expand Down
3 changes: 1 addition & 2 deletions src/impls/impl-posix/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <karm-sys/chan.h>

#include <karm-logger/_embed.h>
#include <karm-sys/chan.h>

namespace Karm::Logger::_Embed {

Expand Down
3 changes: 1 addition & 2 deletions src/impls/impl-posix/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
//
#include <karm-io/funcs.h>
#include <karm-logger/logger.h>
#include <karm-sys/_embed.h>
#include <karm-sys/launch.h>
#include <karm-sys/proc.h>

#include <karm-sys/_embed.h>

#include "fd.h"
#include "utils.h"

Expand Down
7 changes: 4 additions & 3 deletions src/impls/impl-sdl/ui.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <SDL.h>
#include <karm-image/loader.h>
#include <karm-pkg/bundle.h>
#include <karm-ui/drag.h>

#include <karm-ui/_embed.h>
#include <karm-ui/drag.h>

namespace Karm::Ui::_Embed {

Expand Down Expand Up @@ -602,7 +601,9 @@ static Res<> _setWindowIcon(SDL_Window *window) {
auto *surface = SDL_CreateRGBSurface(0, image.width(), image.height(), 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
if (not surface)
return Error::other(SDL_GetError());
defer$(SDL_FreeSurface(surface));
Defer defer{[&] {
SDL_FreeSurface(surface);
}};

Gfx::MutPixels pixels{
surface->pixels,
Expand Down
3 changes: 1 addition & 2 deletions src/impls/impl-uring/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
#include <impl-posix/utils.h>
#include <karm-base/map.h>
#include <karm-logger/logger.h>
#include <karm-sys/_embed.h>
#include <karm-sys/async.h>
#include <karm-sys/time.h>

#include <karm-sys/_embed.h>

namespace Karm::Sys::_Embed {

struct __kernel_timespec toKernelTimespec(TimeStamp ts) {
Expand Down
16 changes: 16 additions & 0 deletions src/impls/impl-wasm/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


#include <karm-app/_embed.h>
#include <karm-app/prefs.h>

namespace Karm::App::_Ember {

static Opt<MockPrefs> _globalPrefs;

Prefs &globalPrefs() {
if (not _globalPrefs)
_globalPrefs = MockPrefs{};
return *_globalPrefs;
}

} // namespace Karm::App::_Ember
5 changes: 2 additions & 3 deletions src/impls/impl-wasm/externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

#include <karm-base/size.h>

static constexpr usize PAGE_SIZE = kib(64);
#include "macros.h"

#define wasm_import(name) __attribute__((import_module("env"), import_name(#name))) name
#define wasm_export(name) __attribute__((visibility("default"), export_name(#name))) name
static constexpr usize PAGE_SIZE = kib(64);

extern "C" ExternSym __heap_base;
extern "C" ExternSym __heap_end;
Expand Down
3 changes: 1 addition & 2 deletions src/impls/impl-wasm/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <karm-sys/chan.h>

#include <karm-logger/_embed.h>
#include <karm-sys/chan.h>

namespace Karm::Logger::_Embed {

Expand Down
1 change: 0 additions & 1 deletion src/impls/impl-wasm/macro.h

This file was deleted.

4 changes: 4 additions & 0 deletions src/impls/impl-wasm/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define wasm_import(name) __attribute__((import_module("env"), import_name(#name))) name
#define wasm_export(name) __attribute__((visibility("default"), export_name(#name))) name
1 change: 0 additions & 1 deletion src/impls/impl-wasm/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <karm-base/res.h>
#include <karm-base/time.h>
#include <karm-logger/logger.h>

#include <karm-sys/_embed.h>

#include "externs.h"
Expand Down
13 changes: 13 additions & 0 deletions src/libs/karm-app/_embed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

namespace Karm::App {

struct Prefs;

} // namespace Karm::App

namespace Karm::App::_Embed {

Prefs &globalPrefs();

} // namespace Karm::App::_Embed
23 changes: 23 additions & 0 deletions src/libs/karm-app/prefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "prefs.h"

#include "_embed.h"

namespace Karm::App {

Async::Task<Json::Value> MockPrefs::loadAsync(String key, Json::Value defaultValue) {
auto item = _store.access(key);
if (item)
co_return *item;
co_return defaultValue;
}

Async::Task<> MockPrefs::saveAsync(String key, Json::Value value) {
_store.put(key, value);
co_return Ok();
}

Prefs &globalPrefs() {
return _Embed::globalPrefs();
}

} // namespace Karm::App
26 changes: 26 additions & 0 deletions src/libs/karm-app/prefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <karm-base/async.h>
#include <karm-json/values.h>

namespace Karm::App {

struct Prefs {
virtual ~Prefs() = default;

virtual Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue = NONE) = 0;

virtual Async::Task<> saveAsync(String key, Json::Value value) = 0;
};

struct MockPrefs : public Prefs {
Json::Object _store;

Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue) override;

Async::Task<> saveAsync(String key, Json::Value value) override;
};

Prefs &globalPrefs();

} // namespace Karm::App
2 changes: 1 addition & 1 deletion src/libs/karm-av/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ using Audio = _Audio<false>;

using MutAudio = _Audio<true>;

struct AudioStream : Meta::Static {
struct AudioStream : Meta::Pinned {
struct Configs {
AudioChannel channels;
AudioFormat format;
Expand Down
13 changes: 11 additions & 2 deletions src/libs/karm-base/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "list.h"
#include "rc.h"
#include "res.h"
#include "vec.h"

namespace Karm::Async {

Expand Down Expand Up @@ -86,7 +87,7 @@ struct Continuation : public Resumable {
template <Sender S>
struct [[nodiscard]] Awaiter :
public Continuation<typename S::Inner>,
Meta::Static,
Meta::Pinned,
Meta::NoCopy {

using Continuation<typename S::Inner>::value;
Expand Down Expand Up @@ -213,7 +214,7 @@ static inline void detach(S s) {

// MARK: Cancelation -----------------------------------------------------------

struct Cancelation : Meta::Static {
struct Cancelation : Meta::Pinned {
struct Token {
Cancelation *_c = nullptr;

Expand All @@ -224,6 +225,12 @@ struct Cancelation : Meta::Static {
bool canceled() const {
return _c and _c->canceled();
}

Res<> errorIfCanceled() const {
if (canceled())
return Error::interrupted("operation canceled");
return Ok();
}
};

bool _canceled = false;
Expand All @@ -245,6 +252,8 @@ struct Cancelation : Meta::Static {
}
};

using Ct = Cancelation::Token;

// MARK: Promise ---------------------------------------------------------------

template <typename T>
Expand Down
Loading

0 comments on commit ec22ccb

Please sign in to comment.