From 841fd80df76370060c75c40bc273b9527295a6b4 Mon Sep 17 00:00:00 2001 From: stsdc <6031763+stsdc@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:09:52 +0100 Subject: [PATCH] Workaround started automatically --- com.github.stsdc.monitor.json | 3 ++ ...=> com.github.stsdc.monitor-workaround.py} | 4 ++ flatpak/workaround/meson.build | 4 ++ meson.build | 6 +++ meson_options.txt | 3 +- src/Managers/ProcessProvider.vala | 50 ++++++++----------- src/Managers/ProcessUtils.vala | 49 ++++-------------- 7 files changed, 49 insertions(+), 70 deletions(-) rename flatpak/workaround/{process_monitor.py => com.github.stsdc.monitor-workaround.py} (97%) mode change 100644 => 100755 create mode 100644 flatpak/workaround/meson.build diff --git a/com.github.stsdc.monitor.json b/com.github.stsdc.monitor.json index 1195471a..78dd2144 100644 --- a/com.github.stsdc.monitor.json +++ b/com.github.stsdc.monitor.json @@ -332,6 +332,9 @@ { "name": "com.github.stsdc.monitor", "buildsystem": "meson", + "config-opts": [ + "-Dflatpak-workaround=enabled" + ], "sources": [ { "type": "dir", diff --git a/flatpak/workaround/process_monitor.py b/flatpak/workaround/com.github.stsdc.monitor-workaround.py old mode 100644 new mode 100755 similarity index 97% rename from flatpak/workaround/process_monitor.py rename to flatpak/workaround/com.github.stsdc.monitor-workaround.py index a8feafdb..fd1b6adf --- a/flatpak/workaround/process_monitor.py +++ b/flatpak/workaround/com.github.stsdc.monitor-workaround.py @@ -1,3 +1,7 @@ +#!/usr/bin/python + +"""The workaround script should run only in Flatpak environment.""" + import os import dbus diff --git a/flatpak/workaround/meson.build b/flatpak/workaround/meson.build new file mode 100644 index 00000000..375f837d --- /dev/null +++ b/flatpak/workaround/meson.build @@ -0,0 +1,4 @@ +install_data( + join_paths('com.github.stsdc.monitor-workaround.py'), + install_dir: join_paths(datadir, 'workaround') +) diff --git a/meson.build b/meson.build index 2c47b0f3..3dc1863b 100644 --- a/meson.build +++ b/meson.build @@ -74,4 +74,10 @@ subdir('src') meson.add_install_script('meson/post_install.py') subdir('po') + + +if get_option('flatpak-workaround').enabled() + subdir('flatpak/workaround') +endif + # subdir('tests') \ No newline at end of file diff --git a/meson_options.txt b/meson_options.txt index d0fedb3b..c19f93f2 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1 +1,2 @@ -option('indicator-wingpanel', type : 'feature', value : 'disabled', description : 'Enables the Indicator for Wingpanel.') \ No newline at end of file +option('indicator-wingpanel', type : 'feature', value : 'disabled', description : 'Enables the Indicator for Wingpanel.') +option('flatpak-workaround', type : 'feature', value : 'disabled', description : 'Enables the Flatpak workaround.') \ No newline at end of file diff --git a/src/Managers/ProcessProvider.vala b/src/Managers/ProcessProvider.vala index 7ee14daf..b714659a 100644 --- a/src/Managers/ProcessProvider.vala +++ b/src/Managers/ProcessProvider.vala @@ -1,10 +1,12 @@ namespace Monitor { - public class ProcessProvider { + public class ProcessProvider : GLib.Object{ private static GLib.Once instance; public static unowned ProcessProvider get_default () { return instance.once (() => { return new ProcessProvider (); }); } + private bool is_flatpak; + public Gee.HashMap pids_cmdline = new Gee.HashMap (); public Gee.HashMap pids_stat = new Gee.HashMap (); public Gee.HashMap pids_statm = new Gee.HashMap (); @@ -14,14 +16,28 @@ namespace Monitor { DBusWorkaroundClient dbus_workaround_client; - public ProcessProvider () { - if (ProcessUtils.is_flatpak_env ()) { + construct { + this.is_flatpak = ProcessUtils.is_flatpak_env (); + if (this.is_flatpak) { + this.spawn_workaround (); dbus_workaround_client = DBusWorkaroundClient.get_default (); } } + private bool spawn_workaround () { + try { + debug ("Spawning workaround..."); + string app_path = ProcessUtils.get_flatpak_app_path (); + string command = @"flatpak-spawn --host env LANG=C $app_path/share/workaround/com.github.stsdc.monitor-workaround.py"; + return GLib.Process.spawn_command_line_async (command); + } catch (SpawnError e) { + warning ("Spawning workaround error: %s\n", e.message); + return false; + } + } + public int[] get_pids () { - if (ProcessUtils.is_flatpak_env ()) { + if (this.is_flatpak) { int[] pids; pids_cmdline.clear (); pids_stat.clear (); @@ -54,31 +70,5 @@ namespace Monitor { return pids; } - - private bool process_line (IOChannel channel, IOCondition condition, GLib.List _pids) { - if (condition == IOCondition.HUP) { - // debug ("%s: The fd has been closed.\n", stream_name); - return false; - } - - try { - string line; - channel.read_line (out line, null, null); - if (line[0].isdigit ()) { - print ("%d\n", int.parse (line)); - // pids.add (line.strip ()); - - } - } catch (IOChannelError e) { - warning ("IOChannelError: %s\n", e.message); - return false; - } catch (ConvertError e) { - warning ("ConvertError: %s\n", e.message); - return false; - } - - return true; - } - } } diff --git a/src/Managers/ProcessUtils.vala b/src/Managers/ProcessUtils.vala index ae0788bb..43e57679 100644 --- a/src/Managers/ProcessUtils.vala +++ b/src/Managers/ProcessUtils.vala @@ -88,48 +88,9 @@ public class Monitor.ProcessUtils { out status ); - - // GLib.Process.spawn_async_with_pipes - // ("/", - // spawn_args, - // Environ.get (), - // SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, - // null, - // out child_pid, - // out standard_input, - // out standard_output, - // out standard_error - // ); - // debug ("Status: %d", status); } catch (SpawnError e) { error (e.message); } - // IOChannel output = new IOChannel.unix_new (standard_output); - // output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => { - // if (condition == IOCondition.HUP) { - // print ("%s: The fd has been closed.\n", "stream_name"); - // return false; - // } - - // try { - // string line; - // channel.read_line (out line, null, null); - // print ("%s: %s", "stream_name", line); - // } catch (IOChannelError e) { - // print ("%s: IOChannelError: %s\n", "stream_name", e.message); - // return false; - // } catch (ConvertError e) { - // print ("%s: ConvertError: %s\n", "stream_name", e.message); - // return false; - // } - - // return true; - // }); - - // ChildWatch.add (child_pid, (pid, status) => { - //// Triggered when the child indicated by child_pid exits - // GLib.Process.close_pid (pid); - // }); string ? stdout_no_debug = ""; @@ -167,4 +128,14 @@ public class Monitor.ProcessUtils { return false; } + public static string get_flatpak_app_path () { + string ? flatpak_info_content = ProcessUtils.read_file ("/.flatpak-info"); + foreach (var line in flatpak_info_content.split ("\n")) { + if (line.contains ("app-path")) { + return line.replace ("app-path=", ""); + } + } + return ""; + } + }