From ae20c178b98120e2403c80fde4b3e2c7a7df2fea Mon Sep 17 00:00:00 2001 From: Spencer Dixon Date: Mon, 21 Jun 2021 20:20:05 -0400 Subject: [PATCH] LibDesktop: Add spawn() to AppFiles This adds a convenience utility to AppFiles for quickly launching the apps backed by the AppFile. --- Userland/Libraries/LibDesktop/AppFile.cpp | 21 +++++++++++++++++++++ Userland/Libraries/LibDesktop/AppFile.h | 1 + 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibDesktop/AppFile.cpp b/Userland/Libraries/LibDesktop/AppFile.cpp index e5f901284c..d55d60002a 100644 --- a/Userland/Libraries/LibDesktop/AppFile.cpp +++ b/Userland/Libraries/LibDesktop/AppFile.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Linus Groh + * Copyright (c) 2021, Spencer Dixon * * SPDX-License-Identifier: BSD-2-Clause */ @@ -103,4 +104,24 @@ Vector AppFile::launcher_protocols() const return protocols; } +bool AppFile::spawn() const +{ + if (!is_valid()) + return false; + + pid_t child_pid; + const char* argv[] = { executable().characters(), nullptr }; + if ((errno = posix_spawn(&child_pid, executable().characters(), nullptr, nullptr, const_cast(argv), environ))) { + perror("posix_spawn"); + return false; + } else { + if (disown(child_pid) < 0) { + perror("disown"); + return false; + } + } + + return true; +} + } diff --git a/Userland/Libraries/LibDesktop/AppFile.h b/Userland/Libraries/LibDesktop/AppFile.h index 2172601a49..d6ff5d7509 100644 --- a/Userland/Libraries/LibDesktop/AppFile.h +++ b/Userland/Libraries/LibDesktop/AppFile.h @@ -28,6 +28,7 @@ public: String category() const; Vector launcher_file_types() const; Vector launcher_protocols() const; + bool spawn() const; GUI::Icon icon() const { return GUI::FileIconProvider::icon_for_path(executable()); };