|
| 1 | +/* |
| 2 | + * Copyright 2021 elementary, Inc. <https://elementary.io> |
| 3 | + * Copyright 2021 Corentin Noël <[email protected]> |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +public enum Gala.AppState { |
| 8 | + STOPPED, |
| 9 | + STARTING, |
| 10 | + RUNNING |
| 11 | +} |
| 12 | + |
| 13 | +public class Gala.App : GLib.Object { |
| 14 | + public string id { |
| 15 | + get { |
| 16 | + if (app_info != null) { |
| 17 | + return app_info.get_id (); |
| 18 | + } else { |
| 19 | + return window_id_string; |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public GLib.DesktopAppInfo? app_info { get; construct; } |
| 25 | + |
| 26 | + public GLib.Icon icon { |
| 27 | + get { |
| 28 | + if (app_info != null) { |
| 29 | + return app_info.get_icon (); |
| 30 | + } |
| 31 | + |
| 32 | + if (fallback_icon == null) { |
| 33 | + fallback_icon = new GLib.ThemedIcon ("application-x-executable"); |
| 34 | + } |
| 35 | + |
| 36 | + return fallback_icon; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public string name { |
| 41 | + get { |
| 42 | + if (app_info != null) { |
| 43 | + return app_info.get_name (); |
| 44 | + } else { |
| 45 | + unowned string? name = null; |
| 46 | + var window = get_backing_window (); |
| 47 | + if (window != null) { |
| 48 | + name = window.get_wm_class (); |
| 49 | + } |
| 50 | + |
| 51 | + return name ?? C_("program", "Unknown"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public string? description { |
| 57 | + get { |
| 58 | + if (app_info != null) { |
| 59 | + return app_info.get_description (); |
| 60 | + } |
| 61 | + |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public Gala.AppState state { get; private set; default = AppState.STOPPED; } |
| 67 | + |
| 68 | + private GLib.SList<Meta.Window> windows = new GLib.SList<Meta.Window> (); |
| 69 | + private uint interesting_windows = 0; |
| 70 | + private string? window_id_string = null; |
| 71 | + private GLib.Icon? fallback_icon = null; |
| 72 | + private int started_on_workspace; |
| 73 | + |
| 74 | + public static unowned App? new_from_startup_sequence (Meta.StartupSequence sequence) { |
| 75 | + unowned string? app_id = sequence.get_application_id (); |
| 76 | + if (app_id == null) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + var basename = GLib.Path.get_basename (app_id); |
| 81 | + unowned var appsys = Gala.AppSystem.get_default (); |
| 82 | + return appsys.lookup_app (basename); |
| 83 | + } |
| 84 | + |
| 85 | + public App (GLib.DesktopAppInfo info) { |
| 86 | + Object (app_info: info); |
| 87 | + } |
| 88 | + |
| 89 | + public App.for_window (Meta.Window window) { |
| 90 | + window_id_string = "window:%u".printf (window.get_stable_sequence ()); |
| 91 | + add_window (window); |
| 92 | + } |
| 93 | + |
| 94 | + public unowned GLib.SList<Meta.Window> get_windows () { |
| 95 | + return windows; |
| 96 | + } |
| 97 | + |
| 98 | + public void add_window (Meta.Window window) { |
| 99 | + if (windows.find (window) != null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + windows.prepend (window); |
| 104 | + if (!window.is_skip_taskbar ()) { |
| 105 | + interesting_windows++; |
| 106 | + } |
| 107 | + |
| 108 | + sync_running_state (); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + public void remove_window (Meta.Window window) { |
| 113 | + if (windows.find (window) == null) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + if (!window.is_skip_taskbar ()) { |
| 118 | + interesting_windows--; |
| 119 | + } |
| 120 | + |
| 121 | + windows.remove (window); |
| 122 | + sync_running_state (); |
| 123 | + } |
| 124 | + |
| 125 | + private void sync_running_state () { |
| 126 | + if (state != Gala.AppState.STARTING) { |
| 127 | + unowned var app_sys = Gala.AppSystem.get_default (); |
| 128 | + if (interesting_windows == 0) { |
| 129 | + state = Gala.AppState.STOPPED; |
| 130 | + app_sys.notify_app_state_changed (this); |
| 131 | + } else { |
| 132 | + state = Gala.AppState.RUNNING; |
| 133 | + app_sys.notify_app_state_changed (this); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public void handle_startup_sequence (Meta.StartupSequence sequence) { |
| 139 | + bool starting = !sequence.get_completed (); |
| 140 | + |
| 141 | + if (starting && state == AppState.STOPPED) { |
| 142 | + state = AppState.STARTING; |
| 143 | + } |
| 144 | + |
| 145 | + if (starting) { |
| 146 | + started_on_workspace = sequence.workspace; |
| 147 | + } else if (interesting_windows > 0) { |
| 148 | + state = AppState.RUNNING; |
| 149 | + } else { |
| 150 | + state = AppState.STOPPED; |
| 151 | + } |
| 152 | + |
| 153 | + unowned var app_sys = Gala.AppSystem.get_default (); |
| 154 | + app_sys.notify_app_state_changed (this); |
| 155 | + } |
| 156 | + |
| 157 | + private Meta.Window? get_backing_window () requires (app_info == null) { |
| 158 | + return windows.data; |
| 159 | + } |
| 160 | + |
| 161 | + public GLib.SList<Posix.pid_t?> get_pids () { |
| 162 | + var results = new GLib.SList<Posix.pid_t?> (); |
| 163 | + foreach (unowned var window in windows) { |
| 164 | + var pid = window.get_pid (); |
| 165 | + if (pid < 1) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + /* Note in the (by far) common case, app will only have one pid, so |
| 170 | + * we'll hit the first element, so don't worry about O(N^2) here. |
| 171 | + */ |
| 172 | + if (results.find (pid) == null) { |
| 173 | + results.prepend (pid); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return results; |
| 178 | + } |
| 179 | +} |
0 commit comments