aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnaud Ferraris <arnaud.ferraris@collabora.com>2021-12-01 20:09:30 +0100
committerScott Murray <scott.murray@konsulko.com>2021-12-20 18:02:20 -0500
commitd388e92df803d0c8a7b0c18762b0900796e348d1 (patch)
tree213ecdfbfc41e677700c47ff492912637fce4039
parentd58aab33e54bea28a33a5ef6b62a5a3e5b6003d1 (diff)
applaunchd: Initial commit
`applaunchd` is a new service, dedicated to starting and monitoring applications. It is part of the Application Framework rework. Bug-AGL: SPEC-4160 Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com> Change-Id: I9543957f9065670e9112556f5cd2b31d0b2ab9d9
-rw-r--r--LICENSE54
-rw-r--r--README.md27
-rw-r--r--data/meson.build57
-rw-r--r--data/org.automotivelinux.AppLaunch.service.in3
-rw-r--r--data/org.automotivelinux.AppLaunch.xml74
-rw-r--r--data/org.freedesktop.Application.xml17
-rw-r--r--meson.build65
-rw-r--r--src/app_info.c171
-rw-r--r--src/app_info.h56
-rw-r--r--src/app_launcher.c423
-rw-r--r--src/app_launcher.h38
-rw-r--r--src/dbus_activation_manager.c258
-rw-r--r--src/dbus_activation_manager.h42
-rw-r--r--src/main.c84
-rw-r--r--src/meson.build37
-rw-r--r--src/process_manager.c219
-rw-r--r--src/process_manager.h38
-rw-r--r--src/utils.c113
-rw-r--r--src/utils.h24
19 files changed, 1800 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..31c692a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,54 @@
+Apache License
+
+Version 2.0, January 2004
+
+http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
+
+"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
+
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..943679c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,27 @@
+AGL Application Launcher service reference implementation
+
+`applaunchd` is a simple service for launching applications from other
+applications. It exposes an interface named 'org.automotivelinux.AppLaunch' on
+on the D-Bus session bus and can be autostarted by using this interface name.
+
+This interface can be used to:
+- retrieve a list of available applications
+- request that a specific application be started by using the 'start' method
+- subcribe to the 'started' and/or 'terminated' signals in order to be
+ notified when an application started successfully or terminated
+
+For more details about the D-Bus interface, please refer to the file
+`data/org.automotivelinux.AppLaunch.xml`.
+
+Applications can be started either through D-Bus activation (using their D-Bus
+name) or by specifying a command line to be executed, and are monitored until
+they exit. Please note `applaunchd` allows only one instance of a given
+application.
+
+AGL repo for source code:
+https://gerrit.automotivelinux.org/gerrit/#/admin/projects/src/applaunchd
+
+You can also clone the source repository by running the following command:
+```
+$ git clone https://gerrit.automotivelinux.org/gerrit/src/applaunchd
+```
diff --git a/data/meson.build b/data/meson.build
new file mode 100644
index 0000000..34aefa4
--- /dev/null
+++ b/data/meson.build
@@ -0,0 +1,57 @@
+#
+# Copyright (C) 2021 Collabora Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+gnome = import('gnome')
+
+# DBus server interface
+dbus_prefix = 'org.automotivelinux.'
+dbus_namespace = 'applaunchd'
+
+generated_dbus_sources = []
+
+dbus_interfaces = [ 'org.automotivelinux.AppLaunch.xml' ]
+
+generated_dbus_sources += gnome.gdbus_codegen('applaunch-dbus',
+ sources : dbus_interfaces,
+ object_manager : false,
+ interface_prefix : 'org.automotivelinux.',
+ install_header : false,
+ namespace : 'applaunchd')
+
+dbus_header_dir = meson.current_build_dir()
+dbus_inc = include_directories('.')
+install_data(dbus_interfaces, install_dir: ifacedir)
+
+# Application DBus interface
+generated_dbus_sources += gnome.gdbus_codegen('fdo-dbus',
+ sources : [ 'org.freedesktop.Application.xml' ],
+ object_manager : false,
+ interface_prefix : 'org.freedesktop.',
+ install_header : false,
+ namespace : 'fdo')
+
+# systemd service file
+service_data = configuration_data()
+service_data.set('bindir', bindir)
+service_file = 'org.automotivelinux.AppLaunch.service'
+
+configure_file(
+ input : service_file + '.in',
+ output : service_file,
+ configuration : service_data,
+ install : true,
+ install_dir: servicedir,
+)
diff --git a/data/org.automotivelinux.AppLaunch.service.in b/data/org.automotivelinux.AppLaunch.service.in
new file mode 100644
index 0000000..41acad5
--- /dev/null
+++ b/data/org.automotivelinux.AppLaunch.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.automotivelinux.AppLaunch
+Exec=@bindir@/applaunchd
diff --git a/data/org.automotivelinux.AppLaunch.xml b/data/org.automotivelinux.AppLaunch.xml
new file mode 100644
index 0000000..52aa20b
--- /dev/null
+++ b/data/org.automotivelinux.AppLaunch.xml
@@ -0,0 +1,74 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
+
+<!--
+ AppLaunch Interface Specification
+
+ Copyright (C) 2021 Collabora Ltd
+-->
+<node name="/org/automotivelinux/AppLaunch" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
+
+ <!--
+ org.automotivelinux.AppLaunch:
+ @short_description: The AppLaunch interface.
+
+ The AppLaunch interface allows requesting application startup and
+ being notified of application startup and termination events.
+ -->
+ <interface name="org.automotivelinux.AppLaunch">
+
+ <!--
+ start:
+ @appid: Application ID, should match:
+ - the xdg-shell app-id for graphical applications started
+ through a command line
+ - the D-Bus name owned by the application for D-Bus activated
+ apps
+
+ Start the application with the corresponding application ID.
+ -->
+ <method name="start">
+ <arg name="appid" type="s" direction="in"/>
+ </method>
+
+ <!--
+ listApplications:
+ @graphical: Whether the should should be limited to graphical
+ applications.
+ @applist: The application list as an array of variants. Each array item
+ is an application entry, represented as a structure of 3 strings:
+ - application ID
+ - application name
+ - absolute icon path
+
+ Retrieve the list of applications which can be started using this interface.
+ -->
+ <method name="listApplications">
+ <arg name="graphical" type="b" direction="in"/>
+ <arg name="applist" type="av" direction="out"/>
+ </method>
+
+ <!--
+ started:
+ @appid: Application ID
+
+ Emitted when an application started successfully, which happens when:
+ - the D-Bus name has been taken for D-Bus activated applications
+ - the process was created for other applications
+ -->
+ <signal name="started">
+ <arg name="appid" type="s"/>
+ </signal>
+
+ <!--
+ terminated:
+ @appid: Application ID
+
+ Emitted when an application terminated, which happens when:
+ - the D-Bus name has been released for D-Bus activated applications
+ - the process exited for other applications
+ -->
+ <signal name="terminated">
+ <arg name="appid" type="s"/>
+ </signal>
+ </interface>
+</node>
diff --git a/data/org.freedesktop.Application.xml b/data/org.freedesktop.Application.xml
new file mode 100644
index 0000000..dc03d70
--- /dev/null
+++ b/data/org.freedesktop.Application.xml
@@ -0,0 +1,17 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
+<node>
+ <interface name='org.freedesktop.Application'>
+ <method name='Activate'>
+ <arg type='a{sv}' name='platform_data' direction='in'/>
+ </method>
+ <method name='Open'>
+ <arg type='as' name='uris' direction='in'/>
+ <arg type='as' name='platform_data' direction='in'/>
+ </method>
+ <method name='ActivateAction'>
+ <arg type='s' name='action_name' direction='in'/>
+ <arg type='av' name='parameter' direction='in'/>
+ <arg type='as' name='platform_data' direction='in'/>
+ </method>
+ </interface>
+</node>
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..8e820e5
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,65 @@
+#
+# Copyright (C) 2021 Collabora Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+project (
+ 'applaunchd',
+ 'c',
+ version : '0.1.0',
+ license : 'Apache-2.0',
+ meson_version : '>= 0.46.0',
+ default_options :
+ [
+ 'warning_level=1',
+ 'buildtype=debugoptimized',
+ 'c_std=gnu11'
+ ],
+)
+
+cc = meson.get_compiler('c')
+
+app_name = meson.project_name()
+
+prefix = get_option('prefix')
+datadir = get_option('datadir')
+sysconfdir = get_option('sysconfdir')
+bindir = join_paths(prefix, get_option('bindir'))
+ifacedir = join_paths(prefix, join_paths(datadir, 'dbus-1', 'interfaces'))
+servicedir = join_paths(prefix, join_paths(datadir, 'dbus-1', 'services'))
+
+if datadir.startswith('/')
+ full_datadir = datadir
+else
+ full_datadir = join_paths(prefix, datadir)
+endif
+
+if sysconfdir.startswith('/')
+ full_sysconfdir = sysconfdir
+else
+ full_sysconfdir = join_paths(prefix, sysconfdir)
+endif
+
+config_data = configuration_data()
+config_data.set_quoted('APP_DATA_NAME', app_name)
+config_data.set_quoted('DATADIR', full_datadir)
+config_data.set_quoted('SYSCONFDIR', full_sysconfdir)
+
+config_h = configure_file (
+ output: 'config.h',
+ configuration: config_data
+)
+
+subdir('data')
+subdir('src')
diff --git a/src/app_info.c b/src/app_info.c
new file mode 100644
index 0000000..03e1f02
--- /dev/null
+++ b/src/app_info.c
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gio/gio.h>
+
+#include "app_info.h"
+#include "dbus_activation_manager.h"
+
+struct _AppInfo {
+ GObject parent_instance;
+
+ gchar *app_id;
+ gchar *name;
+ gchar *icon_path;
+ gchar *command;
+ gboolean dbus_activated;
+ gboolean graphical;
+
+ AppStatus status;
+
+ /*
+ * `runtime_data` is an opaque pointer depending on the app startup method.
+ * It is set in by ProcessManager or DBusActivationManager.
+ */
+ gpointer runtime_data;
+};
+
+G_DEFINE_TYPE(AppInfo, app_info, G_TYPE_OBJECT);
+
+/*
+ * Initialization & cleanup functions
+ */
+
+static void app_info_dispose(GObject *object)
+{
+ AppInfo *self = APPLAUNCHD_APP_INFO(object);
+
+ g_clear_pointer(&self->app_id, g_free);
+ g_clear_pointer(&self->name, g_free);
+ g_clear_pointer(&self->icon_path, g_free);
+ g_clear_pointer(&self->app_id, g_free);
+
+ if (self->dbus_activated) {
+ g_clear_pointer(&self->runtime_data,
+ dbus_activation_manager_free_runtime_data);
+ } else {
+ g_clear_pointer(&self->runtime_data, g_free);
+ }
+
+ G_OBJECT_CLASS(app_info_parent_class)->dispose(object);
+}
+
+static void app_info_finalize(GObject *object)
+{
+ G_OBJECT_CLASS(app_info_parent_class)->finalize(object);
+}
+
+static void app_info_class_init(AppInfoClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+
+ object_class->dispose = app_info_dispose;
+ object_class->finalize = app_info_finalize;
+}
+
+static void app_info_init(AppInfo *self)
+{
+}
+
+/*
+ * Public functions
+ */
+
+AppInfo *app_info_new(const gchar *app_id, const gchar *name,
+ const gchar *icon_path, const gchar *command,
+ gboolean dbus_activated, gboolean graphical)
+{
+ AppInfo *self = g_object_new(APPLAUNCHD_TYPE_APP_INFO, NULL);
+
+ self->app_id = g_strdup(app_id);
+ self->name = g_strdup(name);
+ self->icon_path = g_strdup(icon_path);
+ self->command = g_strdup(command);
+ self->dbus_activated = dbus_activated;
+ self->graphical = graphical;
+
+ return self;
+}
+
+const gchar *app_info_get_app_id(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), NULL);
+
+ return self->app_id;
+}
+
+const gchar *app_info_get_name(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), NULL);
+
+ return self->name;
+}
+
+const gchar *app_info_get_icon_path(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), NULL);
+
+ return self->icon_path;
+}
+
+const gchar *app_info_get_command(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), NULL);
+
+ return self->command;
+}
+
+gboolean app_info_get_dbus_activated(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), FALSE);
+
+ return self->dbus_activated;
+}
+
+gboolean app_info_get_graphical(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), FALSE);
+
+ return self->graphical;
+}
+
+AppStatus app_info_get_status(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), APP_STATUS_INACTIVE);
+
+ return self->status;
+}
+
+gpointer app_info_get_runtime_data(AppInfo *self)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(self), NULL);
+
+ return self->runtime_data;
+}
+
+void app_info_set_runtime_data(AppInfo *self, gpointer runtime_data)
+{
+ g_return_if_fail(APPLAUNCHD_IS_APP_INFO(self));
+
+ self->runtime_data = runtime_data;
+}
+
+void app_info_set_status(AppInfo *self, AppStatus status)
+{
+ g_return_if_fail(APPLAUNCHD_IS_APP_INFO(self));
+
+ self->status = status;
+}
diff --git a/src/app_info.h b/src/app_info.h
new file mode 100644
index 0000000..dd312c4
--- /dev/null
+++ b/src/app_info.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef APPINFO_H
+#define APPINFO_H
+
+#include <glib-object.h>
+
+typedef enum {
+ APP_STATUS_INACTIVE,
+ APP_STATUS_STARTING,
+ APP_STATUS_RUNNING
+} AppStatus;
+
+G_BEGIN_DECLS
+
+#define APPLAUNCHD_TYPE_APP_INFO app_info_get_type()
+
+G_DECLARE_FINAL_TYPE(AppInfo, app_info, APPLAUNCHD,
+ APP_INFO, GObject);
+
+AppInfo *app_info_new(const gchar *app_id, const gchar *name,
+ const gchar *icon_path, const gchar *command,
+ gboolean dbus_activated, gboolean graphical);
+
+/* Accessors for read-only members */
+const gchar *app_info_get_app_id(AppInfo *self);
+const gchar *app_info_get_name(AppInfo *self);
+const gchar *app_info_get_icon_path(AppInfo *self);
+const gchar *app_info_get_command(AppInfo *self);
+gboolean app_info_get_dbus_activated(AppInfo *self);
+gboolean app_info_get_graphical(AppInfo *self);
+
+/* Accessors for read-write members */
+AppStatus app_info_get_status(AppInfo *self);
+void app_info_set_status(AppInfo *self, AppStatus status);
+
+gpointer app_info_get_runtime_data(AppInfo *self);
+void app_info_set_runtime_data(AppInfo *self, gpointer runtime_data);
+
+G_END_DECLS
+
+#endif
diff --git a/src/app_launcher.c b/src/app_launcher.c
new file mode 100644
index 0000000..9bb95a4
--- /dev/null
+++ b/src/app_launcher.c
@@ -0,0 +1,423 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gio/gdesktopappinfo.h>
+
+#include "app_info.h"
+#include "app_launcher.h"
+#include "dbus_activation_manager.h"
+#include "process_manager.h"
+#include "utils.h"
+
+typedef struct _AppLauncher {
+ applaunchdAppLaunchSkeleton parent;
+
+ DBusActivationManager *dbus_manager;
+ ProcessManager *process_manager;
+
+ GList *apps_list;
+} AppLauncher;
+
+static void app_launcher_iface_init(applaunchdAppLaunchIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE(AppLauncher, app_launcher,
+ APPLAUNCHD_TYPE_APP_LAUNCH_SKELETON,
+ G_IMPLEMENT_INTERFACE(APPLAUNCHD_TYPE_APP_LAUNCH,
+ app_launcher_iface_init));
+
+/*
+ * Internal functions
+ */
+
+/*
+ * This function is executed during the object initialization. It goes through
+ * all available applications on the system and creates a static list
+ * containing all the relevant info (ID, name, command, icon...) for further
+ * processing.
+ */
+static void app_launcher_update_applications_list(AppLauncher *self)
+{
+ g_autoptr(GList) app_list = g_app_info_get_all();
+ g_auto(GStrv) dirlist = g_strsplit(getenv("XDG_DATA_DIRS"), ":", -1);
+ guint len = g_list_length(app_list);
+
+ for (guint i = 0; i < len; i++) {
+ GAppInfo *appinfo = g_list_nth_data(app_list, i);
+ const gchar *desktop_id = g_app_info_get_id(appinfo);
+ GIcon *icon = g_app_info_get_icon(appinfo);
+ g_autoptr(GDesktopAppInfo) desktop_info = g_desktop_app_info_new(desktop_id);
+ g_autofree const gchar *app_id = NULL;
+ g_autofree const gchar *icon_path = NULL;
+ AppInfo *app_info = NULL;
+ gboolean dbus_activated, graphical;
+
+ if (!desktop_info) {
+ g_warning("Unable to find .desktop file for application '%s'", desktop_id);
+ continue;
+ }
+
+ /* Check the application should be part of the apps list */
+ if (!g_app_info_should_show(appinfo)) {
+ g_debug("Application '%s' shouldn't be shown, skipping...", desktop_id);
+ continue;
+ }
+
+ if (g_desktop_app_info_get_is_hidden(desktop_info)) {
+ g_debug("Application '%s' is hidden, skipping...", desktop_id);
+ continue;
+ }
+ if (g_desktop_app_info_get_nodisplay(desktop_info)) {
+ g_debug("Application '%s' has NoDisplay set, skipping...", desktop_id);
+ continue;
+ }
+
+ /*
+ * The application ID is usually the .desktop file name. However, a common practice
+ * is that .desktop files are named after the executable name, in which case the
+ * "StartupWMClass" property indicates the wayland app-id.
+ */
+ app_id = g_strdup(g_desktop_app_info_get_startup_wm_class(desktop_info));
+ if (!app_id) {
+ app_id = g_strdup(desktop_id);
+ gchar *extension = g_strrstr(app_id, ".desktop");
+ if (extension)
+ *extension = 0;
+ }
+
+ /*
+ * An application can be D-Bus activated if one of those conditions are met:
+ * - its .desktop file contains a "DBusActivatable=true" line
+ * - it provides a corresponding D-Bus service file
+ */
+ if (g_desktop_app_info_get_boolean(desktop_info,
+ G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE)) {
+ dbus_activated = TRUE;
+ } else {
+ const gchar *desktop_filename = g_desktop_app_info_get_filename(desktop_info);
+ g_autofree gchar *service_file = g_strconcat(app_id, ".service", NULL);
+
+ /* Default to non-DBus-activatable */
+ dbus_activated = FALSE;
+
+ for (GStrv xdg_data_dir = dirlist; *xdg_data_dir != NULL ; xdg_data_dir++) {
+ g_autofree gchar *service_path = NULL;
+
+ /* Search only in the XDG_DATA_DIR where the .desktop file is located */
+ if (!g_str_has_prefix(desktop_filename, *xdg_data_dir))
+ continue;
+
+ service_path = g_build_filename(*xdg_data_dir, "dbus-1", "services",
+ service_file, NULL);
+ if (g_file_test(service_path, G_FILE_TEST_EXISTS)) {
+ dbus_activated = TRUE;
+ break;
+ }
+ }
+ }
+
+ /* Applications with "Terminal=True" are not graphical apps */
+ graphical = !g_desktop_app_info_get_boolean(desktop_info,
+ G_KEY_FILE_DESKTOP_KEY_TERMINAL);
+
+ /*
+ * GAppInfo retrieves the icon data but doesn't provide a way to retrieve
+ * the corresponding file name, so we have to look it up by ourselves.
+ */
+ if (icon)
+ icon_path = applaunchd_utils_get_icon(dirlist, g_icon_to_string(icon));
+
+ app_info = app_info_new(app_id, g_app_info_get_name(appinfo),
+ icon_path ? icon_path : "",
+ dbus_activated ? "" : g_app_info_get_commandline(appinfo),
+ dbus_activated, graphical);
+
+ g_debug("Adding application '%s'", app_id);
+
+ self->apps_list = g_list_append(self->apps_list, app_info);
+ }
+}
+
+/*
+ * Construct the application list to be sent over D-Bus. It has format "av", meaning
+ * the list itself is an array, each item being a variant consisting of 3 strings:
+ * - app-id
+ * - app name
+ * - icon path
+ */
+static GVariant *app_launcher_get_list_variant(AppLauncher *self, gboolean graphical)
+{
+ GVariantBuilder builder;
+ guint len = g_list_length(self->apps_list);
+
+ /* Init array variant for storing the applications list */
+ g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+
+ for (guint i = 0; i < len; i++) {
+ GVariantBuilder app_builder;
+ AppInfo *app_info = g_list_nth_data(self->apps_list, i);
+
+ if (graphical && !app_info_get_graphical(app_info))
+ continue;
+
+ g_variant_builder_init (&app_builder, G_VARIANT_TYPE("(sss)"));
+
+ /* Create application entry */
+ g_variant_builder_add(&app_builder, "s", app_info_get_app_id(app_info));
+ g_variant_builder_add(&app_builder, "s", app_info_get_name(app_info));
+ g_variant_builder_add(&app_builder, "s", app_info_get_icon_path(app_info));
+
+ /* Add entry to apps list */
+ g_variant_builder_add(&builder, "v", g_variant_builder_end(&app_builder));
+ }
+
+ return g_variant_builder_end(&builder);
+}
+
+/*
+ * Starts the requested application using either the D-Bus activation manager
+ * or the process manager.
+ */
+static gboolean app_launcher_start_app(AppLauncher *self, AppInfo *app_info)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(self), FALSE);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(app_info), FALSE);
+
+ AppStatus app_status = app_info_get_status(app_info);
+ const gchar *app_id = app_info_get_app_id(app_info);
+
+ switch (app_status) {
+ case APP_STATUS_STARTING:
+ g_debug("Application '%s' is already starting", app_id);
+ return TRUE;
+ case APP_STATUS_RUNNING:
+ g_debug("Application '%s' is already running", app_id);
+ /*
+ * The application may be running in the background, activate it
+ * and notify subscribers it should be activated/brought to the
+ * foreground
+ */
+ if (app_info_get_dbus_activated(app_info))
+ dbus_activation_manager_activate_app(self->dbus_manager, app_info);
+ return TRUE;
+ case APP_STATUS_INACTIVE:
+ if (app_info_get_dbus_activated(app_info))
+ dbus_activation_manager_start_app(self->dbus_manager, app_info);
+ else
+ process_manager_start_app(self->process_manager, app_info);
+ return TRUE;
+ default:
+ g_critical("Unknown status %d for application '%s'", app_status, app_id);
+ break;
+ }
+
+ return FALSE;
+}
+
+/*
+ * Internal callbacks
+ */
+
+/*
+ * Handler for the "start" D-Bus method.
+ */
+static gboolean app_launcher_handle_start(applaunchdAppLaunch *object,
+ GDBusMethodInvocation *invocation,
+ const gchar *app_id)
+{
+ AppInfo *app;
+ AppLauncher *self = APPLAUNCHD_APP_LAUNCHER(object);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(self), FALSE);
+
+ /* Seach the apps list for the given app-id */
+ app = app_launcher_get_app_info(self, app_id);
+ if (!app) {
+ g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
+ G_DBUS_ERROR_INVALID_ARGS,
+ "Unknown application '%s'",
+ app_id);
+ return FALSE;
+ }
+
+ app_launcher_start_app(self, app);
+ applaunchd_app_launch_complete_start(object, invocation);
+
+ return TRUE;
+}
+
+/*
+ * Handler for the "listApplications" D-Bus method.
+ */
+static gboolean app_launcher_handle_list_applications(applaunchdAppLaunch *object,
+ GDBusMethodInvocation *invocation,
+ gboolean graphical)
+{
+ GVariant *result = NULL;
+ AppLauncher *self = APPLAUNCHD_APP_LAUNCHER(object);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(self), FALSE);
+
+ /* Retrieve the applications list in the right format for sending over D-Bus */
+ result = app_launcher_get_list_variant(self, graphical);
+ applaunchd_app_launch_complete_list_applications(object, invocation, result);
+
+ return TRUE;
+}
+
+/*
+ * Callback for the "started" signal emitted by both the
+ * process manager and D-Bus activation manager. Forwards
+ * the signal to other applications through D-Bus.
+ */
+static void app_launcher_started_cb(AppLauncher *self,
+ const gchar *app_id,
+ gpointer caller)
+{
+ applaunchdAppLaunch *iface = APPLAUNCHD_APP_LAUNCH(self);
+ g_return_if_fail(APPLAUNCHD_IS_APP_LAUNCH(iface));
+
+ g_debug("Application '%s' started", app_id);
+ /*
+ * Emit the "started" D-Bus signal so subscribers get notified
+ * the application with ID "app_id" started and should be
+ * activated
+ */
+ applaunchd_app_launch_emit_started(iface, app_id);
+}
+
+/*
+ * Callback for the "terminated" signal emitted by both the
+ * process manager and D-Bus activation manager. Forwards
+ * the signal to other applications through D-Bus.
+ */
+static void app_launcher_terminated_cb(AppLauncher *self,
+ const gchar *app_id,
+ gpointer caller)
+{
+ applaunchdAppLaunch *iface = APPLAUNCHD_APP_LAUNCH(self);
+ g_return_if_fail(APPLAUNCHD_IS_APP_LAUNCH(iface));
+
+ g_debug("Application '%s' terminated", app_id);
+ /*
+ * Emit the "terminated" D-Bus signal so subscribers get
+ * notified the application with ID "app_id" terminated
+ */
+ applaunchd_app_launch_emit_terminated(iface, app_id);
+}
+
+/*
+ * Initialization & cleanup functions
+ */
+
+static void app_launcher_dispose(GObject *object)
+{
+ AppLauncher *self = APPLAUNCHD_APP_LAUNCHER(object);
+
+ if (self->apps_list)
+ g_list_free_full(g_steal_pointer(&self->apps_list), g_object_unref);
+
+ g_clear_object(&self->dbus_manager);
+ g_clear_object(&self->process_manager);
+
+ G_OBJECT_CLASS(app_launcher_parent_class)->dispose(object);
+}
+
+static void app_launcher_finalize(GObject *object)
+{
+ G_OBJECT_CLASS(app_launcher_parent_class)->finalize(object);
+}
+
+static void app_launcher_class_init(AppLauncherClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+
+ object_class->dispose = app_launcher_dispose;
+}
+
+static void app_launcher_iface_init(applaunchdAppLaunchIface *iface)
+{
+ iface->handle_start = app_launcher_handle_start;
+ iface->handle_list_applications = app_launcher_handle_list_applications;
+}
+
+static void app_launcher_init (AppLauncher *self)
+{
+ /*
+ * Create the process manager and connect to its signals
+ * so we get notified on app startup/termination
+ */
+ self->process_manager = g_object_new(APPLAUNCHD_TYPE_PROCESS_MANAGER,
+ NULL);
+ g_signal_connect_swapped(self->process_manager, "started",
+ G_CALLBACK(app_launcher_started_cb), self);
+ g_signal_connect_swapped(self->process_manager, "terminated",
+ G_CALLBACK(app_launcher_terminated_cb), self);
+
+ /*
+ * Create the D-Bus activation manager and connect to its signals
+ * so we get notified on app startup/termination
+ */
+ self->dbus_manager = g_object_new(APPLAUNCHD_TYPE_DBUS_ACTIVATION_MANAGER,
+ NULL);
+ g_signal_connect_swapped(self->dbus_manager, "started",
+ G_CALLBACK(app_launcher_started_cb), self);
+ g_signal_connect_swapped(self->dbus_manager, "terminated",
+ G_CALLBACK(app_launcher_terminated_cb), self);
+
+ /* Initialize the applications list */
+ app_launcher_update_applications_list(self);
+}
+
+/*
+ * Public functions
+ */
+
+AppLauncher *app_launcher_get_default(void)
+{
+ static AppLauncher *launcher;
+
+ /*
+ * AppLauncher is a singleton, only create the object if it doesn't
+ * exist already.
+ */
+ if (launcher == NULL) {
+ g_debug("Initializing app launcher service...");
+ launcher = g_object_new(APPLAUNCHD_TYPE_APP_LAUNCHER, NULL);
+ g_object_add_weak_pointer(G_OBJECT(launcher), (gpointer *)&launcher);
+ }
+
+ return launcher;
+}
+
+/*
+ * Search the applications list for an app which matches the provided app-id
+ * and return the corresponding AppInfo object.
+ */
+AppInfo *app_launcher_get_app_info(AppLauncher *self, const gchar *app_id)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(self), NULL);
+
+ guint len = g_list_length(self->apps_list);
+
+ for (guint i = 0; i < len; i++) {
+ AppInfo *app_info = g_list_nth_data(self->apps_list, i);
+
+ if (g_strcmp0(app_info_get_app_id(app_info), app_id) == 0)
+ return app_info;
+ }
+
+ g_warning("Unable to find application with ID '%s'", app_id);
+
+ return NULL;
+}
diff --git a/src/app_launcher.h b/src/app_launcher.h
new file mode 100644
index 0000000..789f091
--- /dev/null
+++ b/src/app_launcher.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef APPLAUNCHER_H
+#define APPLAUNCHER_H
+
+#include <glib-object.h>
+
+#include "applaunch-dbus.h"
+#include "app_info.h"
+
+G_BEGIN_DECLS
+
+#define APPLAUNCHD_TYPE_APP_LAUNCHER app_launcher_get_type()
+
+G_DECLARE_FINAL_TYPE(AppLauncher, app_launcher, APPLAUNCHD, APP_LAUNCHER,
+ applaunchdAppLaunchSkeleton);
+
+AppLauncher *app_launcher_get_default(void);
+
+AppInfo *app_launcher_get_app_info(AppLauncher *self, const gchar *app_id);
+
+G_END_DECLS
+
+#endif
diff --git a/src/dbus_activation_manager.c b/src/dbus_activation_manager.c
new file mode 100644
index 0000000..3c31ee6
--- /dev/null
+++ b/src/dbus_activation_manager.c
@@ -0,0 +1,258 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gio/gio.h>
+
+#include "app_launcher.h"
+#include "dbus_activation_manager.h"
+#include "fdo-dbus.h"
+
+struct _DBusActivationManager {
+ GObject parent_instance;
+};
+
+G_DEFINE_TYPE(DBusActivationManager, dbus_activation_manager, G_TYPE_OBJECT);
+
+enum {
+ STARTED,
+ TERMINATED,
+ N_SIGNALS
+};
+static guint signals[N_SIGNALS];
+
+/*
+ * Application info structure, used for storing relevant data
+ * in the `starting_apps` and `running_apps` lists
+ */
+struct dbus_runtime_data {
+ guint watcher;
+ fdoApplication *fdo_proxy;
+};
+
+/*
+ * Initialization & cleanup functions
+ */
+
+static void dbus_activation_manager_dispose(GObject *object)
+{
+ G_OBJECT_CLASS(dbus_activation_manager_parent_class)->dispose(object);
+}
+
+static void dbus_activation_manager_finalize(GObject *object)
+{
+ G_OBJECT_CLASS(dbus_activation_manager_parent_class)->finalize(object);
+}
+
+static void dbus_activation_manager_class_init(DBusActivationManagerClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+
+ object_class->dispose = dbus_activation_manager_dispose;
+ object_class->finalize = dbus_activation_manager_finalize;
+
+ signals[STARTED] = g_signal_new("started", G_TYPE_FROM_CLASS(klass),
+ G_SIGNAL_RUN_LAST, 0 ,
+ NULL, NULL, NULL, G_TYPE_NONE,
+ 1, G_TYPE_STRING);
+
+ signals[TERMINATED] = g_signal_new("terminated", G_TYPE_FROM_CLASS(klass),
+ G_SIGNAL_RUN_LAST, 0,
+ NULL, NULL, NULL, G_TYPE_NONE,
+ 1, G_TYPE_STRING);
+}
+
+static void dbus_activation_manager_init(DBusActivationManager *self)
+{
+}
+
+/*
+ * Internal callbacks
+ */
+
+/*
+ * This function is called when a D-Bus name we're watching just vanished
+ * from the session bus. This indicates the underlying application terminated,
+ * so we must remove it from the list of running apps and notify listeners
+ * of the app termination so they can act accordingly.
+ */
+static void dbus_activation_manager_app_terminated_cb(GDBusConnection *connection,
+ const gchar *name,
+ gpointer data)
+{
+ DBusActivationManager *self = data;
+ AppLauncher *app_launcher = app_launcher_get_default();
+ AppInfo *app_info;
+ struct dbus_runtime_data *runtime_data;
+
+ g_return_if_fail(APPLAUNCHD_IS_DBUS_ACTIVATION_MANAGER(self));
+ g_return_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(app_launcher));
+
+ app_info = app_launcher_get_app_info(app_launcher, name);
+ g_return_if_fail(APPLAUNCHD_IS_APP_INFO(app_info));
+
+ runtime_data = app_info_get_runtime_data(app_info);
+ g_debug("Application '%s' vanished from D-Bus", name);
+
+ app_info_set_status(app_info, APP_STATUS_INACTIVE);
+ app_info_set_runtime_data(app_info, NULL);
+
+ dbus_activation_manager_free_runtime_data(runtime_data);
+
+ g_signal_emit(self, signals[TERMINATED], 0, name);
+}
+
+/*
+ * Function called when the name appeared on D-Bus, meaning the application
+ * successfully started and registered its D-Bus service.
+ */
+static void dbus_activation_manager_app_started_cb(GDBusConnection *connection,
+ const gchar *name,
+ const gchar *name_owner,
+ gpointer data)
+{
+ DBusActivationManager *self = data;
+ AppLauncher *app_launcher = app_launcher_get_default();
+ AppInfo *app_info;
+ struct dbus_runtime_data *runtime_data;
+
+ g_return_if_fail(APPLAUNCHD_IS_DBUS_ACTIVATION_MANAGER(self));
+ g_return_if_fail(APPLAUNCHD_IS_APP_LAUNCHER(app_launcher));
+
+ app_info = app_launcher_get_app_info(app_launcher, name);
+ g_return_if_fail(APPLAUNCHD_IS_APP_INFO(app_info));
+
+ runtime_data = app_info_get_runtime_data(app_info);
+
+ g_debug("Application '%s' appeared on D-Bus", name);
+ app_info_set_status(app_info, APP_STATUS_RUNNING);
+ dbus_activation_manager_activate_app(self, app_info);
+}
+
+/*
+ * Public functions
+ */
+
+DBusActivationManager *dbus_activation_manager_new(void)
+{
+ return g_object_new(APPLAUNCHD_TYPE_DBUS_ACTIVATION_MANAGER, NULL);
+}
+
+/*
+ * Start an application using D-Bus activation.
+ */
+gboolean dbus_activation_manager_start_app(DBusActivationManager *self,
+ AppInfo *app_info)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_DBUS_ACTIVATION_MANAGER(self), FALSE);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(app_info), FALSE);
+
+ g_autoptr(GError) error = NULL;
+ const gchar *app_id = app_info_get_app_id(app_info);
+ struct dbus_runtime_data *runtime_data = g_new0(struct dbus_runtime_data, 1);
+
+ if (!runtime_data) {
+ g_critical("Unable to allocate runtime data structure for '%s'",
+ app_id);
+ return FALSE;
+ }
+
+ /*
+ * g_bus_watch_name() requests D-Bus activation (due to
+ * G_BUS_NAME_WATCHER_FLAGS_AUTO_START) and subscribes to name
+ * owner changes so we get notified when the requested application
+ * appears and vanishes from D-Bus.
+ */
+ runtime_data->watcher = g_bus_watch_name(G_BUS_TYPE_SESSION, app_id,
+ G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
+ dbus_activation_manager_app_started_cb,
+ dbus_activation_manager_app_terminated_cb,
+ self, NULL);
+ if (runtime_data->watcher == 0) {
+ g_critical("Unable to request D-Bus activation for '%s'", app_id);
+ dbus_activation_manager_free_runtime_data(runtime_data);
+ return FALSE;
+ }
+
+ /* Update application status */
+ app_info_set_status(app_info, APP_STATUS_STARTING);
+ app_info_set_runtime_data(app_info, runtime_data);
+
+ return TRUE;
+}
+
+/*
+ * Once an application has been started through D-Bus, we must activate it
+ * so it shows its main window, if any.
+ * This function doesn't raise an error as headless applications will likely
+ * not implement the org.freedesktop.Application interface.
+ */
+gboolean dbus_activation_manager_activate_app(DBusActivationManager *self,
+ AppInfo *app)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_DBUS_ACTIVATION_MANAGER(self), FALSE);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(app), FALSE);
+
+ g_autoptr(GError) error = NULL;
+ struct dbus_runtime_data *runtime_data = app_info_get_runtime_data(app);
+ const gchar *app_id = app_info_get_app_id(app);
+
+ g_return_val_if_fail(runtime_data != NULL, FALSE);
+
+ /* Base object for interface "org.example.Iface" is "/org/example/Iface" */
+ g_autofree gchar *path = g_strconcat("/", app_id, NULL);
+ g_strdelimit(path, ".", '/');
+
+ if (!runtime_data->fdo_proxy) {
+ runtime_data->fdo_proxy =
+ fdo_application_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ app_id, path, NULL,
+ &error);
+ }
+
+ if (runtime_data->fdo_proxy) {
+ GVariantDict dict;
+
+ g_variant_dict_init(&dict, NULL);
+
+ fdo_application_call_activate_sync (runtime_data->fdo_proxy,
+ g_variant_dict_end(&dict),
+ NULL, &error);
+ if (error) {
+ g_warning("Error activating application %s: %s", app_id,
+ error->message);
+ }
+ } else if (error) {
+ g_warning("Error creating D-Bus proxy for %s: %s", app_id,
+ error->message);
+ }
+
+ g_signal_emit(self, signals[STARTED], 0, app_id);
+
+ return TRUE;
+}
+
+void dbus_activation_manager_free_runtime_data(gpointer data)
+{
+ struct dbus_runtime_data *runtime_data = data;
+
+ g_return_if_fail(runtime_data != NULL);
+
+ g_clear_object(&runtime_data->fdo_proxy);
+ if (runtime_data->watcher > 0)
+ g_bus_unwatch_name(runtime_data->watcher);
+ g_free(runtime_data);
+}
diff --git a/src/dbus_activation_manager.h b/src/dbus_activation_manager.h
new file mode 100644
index 0000000..e475da4
--- /dev/null
+++ b/src/dbus_activation_manager.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef DBUSACTIVATIONMANAGER_H
+#define DBUSACTIVATIONMANAGER_H
+
+#include <glib-object.h>
+
+#include "app_info.h"
+
+G_BEGIN_DECLS
+
+#define APPLAUNCHD_TYPE_DBUS_ACTIVATION_MANAGER dbus_activation_manager_get_type()
+
+G_DECLARE_FINAL_TYPE(DBusActivationManager, dbus_activation_manager,
+ APPLAUNCHD, DBUS_ACTIVATION_MANAGER, GObject);
+
+DBusActivationManager *dbus_activation_manager_new(void);
+
+gboolean dbus_activation_manager_start_app(DBusActivationManager *self,
+ AppInfo *app_info);
+gboolean dbus_activation_manager_activate_app(DBusActivationManager *self,
+ AppInfo *app_info);
+
+void dbus_activation_manager_free_runtime_data(gpointer data);
+
+G_END_DECLS
+
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..d19038a
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <glib-unix.h>
+
+#include "app_launcher.h"
+#include "applaunch-dbus.h"
+
+#define APPLAUNCH_DBUS_NAME "org.automotivelinux.AppLaunch"
+#define APPLAUNCH_DBUS_PATH "/org/automotivelinux/AppLaunch"
+
+static GMainLoop *main_loop = NULL;
+
+static gboolean quit_cb(gpointer user_data)
+{
+ g_info("Quitting...");
+
+ if (main_loop)
+ g_idle_add(G_SOURCE_FUNC(g_main_loop_quit), main_loop);
+ else
+ exit(0);
+
+ return G_SOURCE_REMOVE;
+}
+
+static void bus_acquired_cb(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ AppLauncher *launcher = user_data;
+
+ g_debug("Bus acquired, starting service...");
+ g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(launcher),
+ connection, APPLAUNCH_DBUS_PATH, NULL);
+}
+
+static void name_acquired_cb(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ g_debug("D-Bus name '%s' was acquired", name);
+}
+
+static void name_lost_cb(GDBusConnection *connection, const gchar *name,
+ gpointer user_data)
+{
+ g_critical("Lost the '%s' service name, quitting...", name);
+ g_main_loop_quit(main_loop);
+}
+
+int main(int argc, char *argv[])
+{
+ g_unix_signal_add(SIGTERM, quit_cb, NULL);
+ g_unix_signal_add(SIGINT, quit_cb, NULL);
+
+ main_loop = g_main_loop_new(NULL, FALSE);
+
+ AppLauncher *launcher = app_launcher_get_default();
+ gint owner_id = g_bus_own_name(G_BUS_TYPE_SESSION, APPLAUNCH_DBUS_NAME,
+ G_BUS_NAME_OWNER_FLAGS_NONE, bus_acquired_cb,
+ name_acquired_cb, name_lost_cb,
+ launcher, NULL);
+
+ g_main_loop_run(main_loop);
+ g_main_loop_unref(main_loop);
+
+ g_object_unref(launcher);
+
+ g_bus_unown_name (owner_id);
+
+ return 0;
+}
diff --git a/src/meson.build b/src/meson.build
new file mode 100644
index 0000000..81d1f0c
--- /dev/null
+++ b/src/meson.build
@@ -0,0 +1,37 @@
+#
+# Copyright (C) 2021 Collabora Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+applaunchd_deps = [
+ dependency('gobject-2.0'),
+ dependency('gio-unix-2.0'),
+]
+
+executable (
+ 'applaunchd',
+ config_h,
+ [
+ generated_dbus_sources,
+ 'main.c',
+ 'app_info.c', 'app_info.h',
+ 'app_launcher.c', 'app_launcher.h',
+ 'dbus_activation_manager.c', 'dbus_activation_manager.h',
+ 'process_manager.c', 'process_manager.h',
+ 'utils.c', 'utils.h',
+ ],
+ dependencies : applaunchd_deps,
+ include_directories : include_directories('..'),
+ install : true
+)
diff --git a/src/process_manager.c b/src/process_manager.c
new file mode 100644
index 0000000..023894a
--- /dev/null
+++ b/src/process_manager.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "app_launcher.h"
+#include "process_manager.h"
+
+struct _ProcessManager {
+ GObject parent_instance;
+
+ GList *process_data;
+};
+
+G_DEFINE_TYPE(ProcessManager, process_manager, G_TYPE_OBJECT);
+
+enum {
+ STARTED,
+ TERMINATED,
+ N_SIGNALS
+};
+static guint signals[N_SIGNALS];
+
+/*
+ * Application info structure, used for storing relevant data
+ * in the `running_apps` list
+ */
+struct process_runtime_data {
+ guint watcher;
+ GPid pid;
+ const gchar *app_id;
+};
+
+/*
+ * Initialization & cleanup functions
+ */
+
+static void process_manager_dispose(GObject *object)
+{
+ ProcessManager *self = APPLAUNCHD_PROCESS_MANAGER(object);
+
+ g_return_if_fail(APPLAUNCHD_IS_PROCESS_MANAGER(self));
+
+ if (self->process_data)
+ g_list_free_full(g_steal_pointer(&self->process_data), g_free);
+
+ G_OBJECT_CLASS(process_manager_parent_class)->dispose(object);
+}
+
+static void process_manager_finalize(GObject *object)
+{
+ G_OBJECT_CLASS(process_manager_parent_class)->finalize(object);
+}
+
+static void process_manager_class_init(ProcessManagerClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+
+ object_class->dispose = process_manager_dispose;
+ object_class->finalize = process_manager_finalize;
+
+ signals[STARTED] = g_signal_new("started", G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST, 0 ,
+ NULL, NULL, NULL, G_TYPE_NONE,
+ 1, G_TYPE_STRING);
+
+ signals[TERMINATED] = g_signal_new("terminated", G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST, 0 ,
+ NULL, NULL, NULL, G_TYPE_NONE,
+ 1, G_TYPE_STRING);
+}
+
+static void process_manager_init(ProcessManager *self)
+{
+}
+
+/*
+ * Internal functions
+ */
+
+static const gchar *get_app_id_for_pid(ProcessManager *self, GPid pid)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_PROCESS_MANAGER(self), NULL);
+
+ AppLauncher *app_launcher = app_launcher_get_default();
+ guint len = g_list_length(self->process_data);
+
+ for (guint i = 0; i < len; i++) {
+ struct process_runtime_data *runtime_data =
+ g_list_nth_data(self->process_data, i);
+
+ if (runtime_data->pid == pid)
+ return runtime_data->app_id;
+ }
+
+ return NULL;
+}
+
+/*
+ * Internal callbacks
+ */
+
+/*
+ * This function is called when a watched process terminated, so we can:
+ * - cleanup this application's data (and reap the process so it
+ * doesn't become a zombie)
+ * - notify listeners that the process terminated
+ */
+static void process_manager_app_terminated_cb(GPid pid,
+ gint wait_status,
+ gpointer data)
+{
+ ProcessManager *self = data;
+ AppLauncher *app_launcher = app_launcher_get_default();
+ struct process_runtime_data *runtime_data;
+ const gchar *app_id;
+ AppInfo *app_info;
+
+ g_return_if_fail(APPLAUNCHD_IS_PROCESS_MANAGER(self));
+
+ app_id = get_app_id_for_pid(self, pid);
+ if (!app_id) {
+ g_warning("Unable to retrieve app id for pid %d", pid);
+ return;
+ }
+
+ app_info = app_launcher_get_app_info(app_launcher, app_id);
+ if (!app_info) {
+ g_warning("Unable to find running app with pid %d", pid);
+ return;
+ }
+
+ if (g_spawn_check_exit_status(wait_status, NULL))
+ g_debug("Application '%s' terminated with exit code %i",
+ app_id, WEXITSTATUS(wait_status));
+ else
+ g_warning("Application '%s' crashed", app_id);
+
+ g_spawn_close_pid(pid);
+
+ runtime_data = app_info_get_runtime_data(app_info);
+ g_source_remove(runtime_data->watcher);
+
+ app_info_set_status(app_info, APP_STATUS_INACTIVE);
+ app_info_set_runtime_data(app_info, NULL);
+
+ self->process_data = g_list_remove(self->process_data, runtime_data);
+ g_free(runtime_data);
+
+ g_signal_emit(self, signals[TERMINATED], 0, app_id);
+}
+
+/*
+ * Public functions
+ */
+
+ProcessManager *process_manager_new(void)
+{
+ return g_object_new(APPLAUNCHD_TYPE_PROCESS_MANAGER, NULL);
+}
+
+/*
+ * Start an application by executing the provided command line.
+ */
+gboolean process_manager_start_app(ProcessManager *self,
+ AppInfo *app_info)
+{
+ g_return_val_if_fail(APPLAUNCHD_IS_PROCESS_MANAGER(self), FALSE);
+ g_return_val_if_fail(APPLAUNCHD_IS_APP_INFO(app_info), FALSE);
+
+ gboolean success;
+ g_autofree GStrv args = NULL;
+ const gchar *app_id = app_info_get_app_id(app_info);
+ const gchar *command = app_info_get_command(app_info);
+ struct process_runtime_data *runtime_data;
+
+ runtime_data = g_new0(struct process_runtime_data, 1);
+ if (!runtime_data) {
+ g_critical("Unable to allocate runtime data structure for '%s'",
+ app_id);
+ return FALSE;
+ }
+
+ runtime_data->app_id = app_id;
+
+ args = g_strsplit(command, " ", -1);
+ success = g_spawn_async(NULL, args, NULL,
+ G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
+ NULL, NULL, &runtime_data->pid, NULL);
+ if (!success) {
+ g_critical("Unable to start application '%s'", app_id);
+ g_free(runtime_data);
+ return FALSE;
+ }
+
+ /*
+ * Add a watcher for the child PID in order to get notified when it dies
+ */
+ runtime_data->watcher = g_child_watch_add(runtime_data->pid,
+ process_manager_app_terminated_cb,
+ self);
+ self->process_data = g_list_append(self->process_data, runtime_data);
+ app_info_set_runtime_data(app_info, runtime_data);
+
+ g_signal_emit(self, signals[STARTED], 0, app_id);
+
+ return TRUE;
+}
diff --git a/src/process_manager.h b/src/process_manager.h
new file mode 100644
index 0000000..1979cba
--- /dev/null
+++ b/src/process_manager.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PROCESSMANAGER_H
+#define PROCESSMANAGER_H
+
+#include <glib-object.h>
+
+#include "app_info.h"
+
+G_BEGIN_DECLS
+
+#define APPLAUNCHD_TYPE_PROCESS_MANAGER process_manager_get_type()
+
+G_DECLARE_FINAL_TYPE(ProcessManager, process_manager,
+ APPLAUNCHD, PROCESS_MANAGER, GObject);
+
+ProcessManager *process_manager_new(void);
+
+gboolean process_manager_start_app(ProcessManager *self,
+ AppInfo *app_info);
+
+G_END_DECLS
+
+#endif
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..a053638
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gio/gio.h>
+
+#include "utils.h"
+
+/* Search by descending quality level */
+static const gchar *icon_sizes[] = {
+ "scalable",
+ "512x512",
+ "256x256",
+ "192x192",
+ "128x128",
+ "96x96",
+ "72x72",
+ "64x64",
+ "48x48",
+ "32x32",
+ "24x24",
+ "16x16",
+ "symbolic",
+ NULL
+};
+
+/*
+ * Recursively search the `base_path` folder for a file whose name
+ * starts with `icon_name`: this way we don't care about the extension
+ * and can also get files with an extended name (for example,
+ * "`icon_name`-symbolic.png" would still be recognized as a valid match)
+ */
+static gchar *find_icon(const gchar *base_path, const gchar *icon_name)
+{
+ g_autoptr(GFile) base_dir = g_file_new_for_path(base_path);
+ g_autoptr(GFileEnumerator) child_list =
+ g_file_enumerate_children(base_dir, "*", G_FILE_QUERY_INFO_NONE,
+ NULL, NULL);
+
+ if (!child_list)
+ return NULL;
+
+ while (TRUE) {
+ GFile *current_file;
+ GFileInfo *current_file_info;
+
+ g_file_enumerator_iterate(child_list, &current_file_info,
+ &current_file, NULL, NULL);
+ if (!current_file_info || !current_file)
+ break;
+
+ if (g_file_info_get_file_type(current_file_info) == G_FILE_TYPE_DIRECTORY) {
+ gchar *icon_path = find_icon(g_file_get_path(current_file), icon_name);
+ if (icon_path)
+ return icon_path;
+ } else if (g_str_has_prefix(g_file_get_basename(current_file), icon_name)) {
+ return g_strdup(g_file_get_path(current_file));
+ }
+ }
+
+ return NULL;
+}
+
+/*
+ * Search through a list of folders for the `icon_name` icon file
+ */
+gchar *applaunchd_utils_get_icon(GStrv dir_list, const gchar *icon_name)
+{
+ gchar *icon_path = NULL;
+
+ for (GStrv base_path = dir_list; *base_path != NULL ; base_path++) {
+ g_autoptr(GFile) base_dir = g_file_new_build_filename(*base_path,
+ "icons", NULL);
+
+ g_autoptr(GFileEnumerator) child_list =
+ g_file_enumerate_children(base_dir, "standard::type=directory",
+ G_FILE_QUERY_INFO_NONE, NULL, NULL);
+
+ if (!child_list)
+ continue;
+
+ while (TRUE) {
+ GFile *theme_dir;
+ g_file_enumerator_iterate(child_list, NULL, &theme_dir,
+ NULL, NULL);
+ if (!theme_dir)
+ break;
+
+ for (gint i = 0; icon_sizes[i]; i++) {
+ g_autofree gchar *theme_path =
+ g_build_filename(g_file_get_path(theme_dir),
+ icon_sizes[i], NULL);
+ icon_path = find_icon(theme_path, icon_name);
+ if (icon_path)
+ return icon_path;
+ }
+ }
+ }
+
+ return NULL;
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..1f31e0d
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2021 Collabora Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <glib.h>
+
+gchar *applaunchd_utils_get_icon(GStrv dir_list, const gchar *icon_name);
+
+#endif