Skip to content

Commit

Permalink
karm-sys: Improved the launch API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 26, 2024
1 parent 4ea63ec commit acd5288
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/apps/hideo-files/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ void reduce(State &s, Action a) {

auto stat = Sys::stat(dest).unwrap();
if (stat.type == Sys::Type::FILE) {
(void)Sys::launch(Mime::Uti::PUBLIC_PREVIEW, dest);
(void)Sys::launch({
.action = Mime::Uti::PUBLIC_PREVIEW,
.objects = {dest},
});
} else {
reduce(s, GoTo{dest});
}
Expand Down
5 changes: 3 additions & 2 deletions src/impls/impl-efi/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <karm-json>
#include <karm-logger/logger.h>
#include <karm-sys/file.h>
#include <karm-sys/launch.h>

#include <karm-sys/_embed.h>

Expand Down Expand Up @@ -427,11 +428,11 @@ Res<> populate(Vec<UserInfo> &) {

// MARK: User interactions -----------------------------------------------------

Res<> launch(Mime::Uti const &, Mime::Url const &) {
Res<> launch(Sys::Intent) {
notImplemented();
}

Async::Task<> launchAsync(Mime::Uti const &, Mime::Url const &) {
Async::Task<> launchAsync(Sys::Intent) {
notImplemented();
}

Expand Down
14 changes: 10 additions & 4 deletions src/impls/impl-posix/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//
#include <karm-io/funcs.h>
#include <karm-logger/logger.h>
#include <karm-sys/launch.h>
#include <karm-sys/proc.h>

#include <karm-sys/_embed.h>
Expand Down Expand Up @@ -184,7 +185,12 @@ Res<Stat> stat(Mime::Url const &url) {

// MARK: User interactions -----------------------------------------------------

Res<> launch([[maybe_unused]] Mime::Uti const &uti, [[maybe_unused]] Mime::Url const &url) {
Res<> launch(Intent intent) {
if (intent.objects.len() != 1)
return Error::invalidInput("expected exactly one object");

auto [url, _] = intent.objects[0];

String str = try$(resolve(url)).str();

int pid = fork();
Expand All @@ -193,7 +199,7 @@ Res<> launch([[maybe_unused]] Mime::Uti const &uti, [[maybe_unused]] Mime::Url c

if (pid == 0) {
#ifdef __ck_sys_darwin__
if (uti == Mime::Uti::PUBLIC_MODIFY) {
if (intent.action == Mime::Uti::PUBLIC_MODIFY) {
execlp("open", "open", "-t", str.buf(), nullptr);
} else {
execlp("open", "open", str.buf(), nullptr);
Expand All @@ -214,8 +220,8 @@ Res<> launch([[maybe_unused]] Mime::Uti const &uti, [[maybe_unused]] Mime::Url c
return Posix::fromStatus(status);
}

Async::Task<> launchAsync(Mime::Uti const &uti, Mime::Url const &url) {
co_return launch(uti, url);
Async::Task<> launchAsync(Intent intent) {
co_return _Embed::launch(intent);
}

// MARK: Sockets ---------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/impls/impl-skift/sys.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <handover/hook.h>
#include <hjert-api/api.h>
#include <karm-logger/logger.h>
#include <karm-sys/launch.h>

#include <karm-sys/_embed.h>

Expand Down Expand Up @@ -59,11 +60,11 @@ Res<Stat> stat(Mime::Url const &) {

// MARK: User interactions -----------------------------------------------------

Res<> launch(Mime::Uti const &, Mime::Url const &) {
Res<> launch(Intent) {
notImplemented();
}

Async::Task<> launchAsync(Mime::Uti const &, Mime::Url const &) {
Async::Task<> launchAsync(Intent) {
notImplemented();
}

Expand Down
1 change: 1 addition & 0 deletions src/libs/karm-mime/defs/uti.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ UTI(PUBLIC_OPEN, "public.open")
UTI(PUBLIC_PREVIEW, "public.preview")
UTI(PUBLIC_MODIFY, "public.modify")
UTI(PUBLIC_SHARE, "public.share")
UTI(PUBLIC_PRINT, "public.print")
UTI(PUBLIC_BMP, "public.bmp")
UTI(PUBLIC_TGA, "public.tga")
UTI(PUBLIC_JPEG, "public.jpeg")
Expand Down
10 changes: 8 additions & 2 deletions src/libs/karm-sys/_embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include "info.h"
#include "types.h"

namespace Karm::Sys {

struct Intent;

} // namespace Karm::Sys

namespace Karm::Sys::_Embed {

// MARK: Fd --------------------------------------------------------------------
Expand Down Expand Up @@ -39,9 +45,9 @@ Res<Stat> stat(Mime::Url const &url);

// MARK: User interactions -----------------------------------------------------

Res<> launch(Mime::Uti const &intent, Mime::Url const &url);
Res<> launch(Intent intent);

Async::Task<> launchAsync(Mime::Uti const &intent, Mime::Url const &url);
Async::Task<> launchAsync(Intent intent);

// MARK: Sockets ---------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions src/libs/karm-sys/launch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace Karm::Sys {

Res<> launch(Mime::Uti const &uti, Mime::Url const &url) {
Res<> launch(Intent intent) {
try$(ensureUnrestricted());
return _Embed::launch(uti, url);
return _Embed::launch(intent);
}

Async::Task<> launchAsync(Mime::Uti const &uti, Mime::Url const &url) {
Async::Task<> launchAsync(Intent intent) {
co_try$(ensureUnrestricted());
co_return co_await _Embed::launchAsync(uti, url);
co_return co_await _Embed::launchAsync(intent);
}

} // namespace Karm::Sys
20 changes: 18 additions & 2 deletions src/libs/karm-sys/launch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@

namespace Karm::Sys {

Res<> launch(Mime::Uti const &itent, Mime::Url const &url);
struct Object {
Mime::Url url;
Opt<Mime::Uti> type = NONE;

Async::Task<> launchAsync(Mime::Uti const &itent, Mime::Url const &url);
Object(Mime::Url url) : url(url) {}

Object(Mime::Url url, Mime::Uti type) : url(url), type(type) {}
};

struct Intent {
Mime::Uti action;
Vec<Object> objects;
Opt<Mime::Url> handler = NONE;
Opt<Mime::Url> callback = NONE;
};

Res<> launch(Intent intent);

Async::Task<> launchAsync(Intent intent);

} // namespace Karm::Sys

0 comments on commit acd5288

Please sign in to comment.