| 1 | #include <config.h> |
| 2 | |
| 3 | #include <dirent.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <glibtop/procstate.h> |
| 8 | #include <gdkmm/pixbuf.h> |
| 9 | #include <giomm/error.h> |
| 10 | #include <giomm/file.h> |
| 11 | #include <glibmm/miscutils.h> |
| 12 | |
| 13 | #include <iostream> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "prettytable.h" |
| 17 | #include "defaulttable.h" |
| 18 | #include "procinfo.h" |
| 19 | #include "proctable.h" |
| 20 | #include "util.h" |
| 21 | |
| 22 | const unsigned APP_ICON_SIZE = 16; |
| 23 | |
| 24 | PrettyTable::PrettyTable() |
| 25 | { |
| 26 | // init GIO apps cache |
| 27 | std::vector<std::string> dirs = Glib::get_system_data_dirs (); |
| 28 | |
| 29 | for (std::vector<std::string>::iterator it = dirs.begin (); it != dirs.end (); ++it) |
| 30 | { |
| 31 | std::string path = (*it).append ("/applications"); |
| 32 | Glib::RefPtr<Gio::File> file = Gio::File::create_for_path (path); |
| 33 | Glib::RefPtr<Gio::FileMonitor> monitor = file->monitor_directory (); |
| 34 | |
| 35 | monitor->set_rate_limit (1000); // 1 second |
| 36 | |
| 37 | monitor->signal_changed ().connect (sigc::mem_fun (*this, &PrettyTable::file_monitor_event)); |
| 38 | monitors[path] = monitor; |
| 39 | } |
| 40 | |
| 41 | this->init_gio_app_cache (); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | PrettyTable::~PrettyTable() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | PrettyTable::init_gio_app_cache () |
| 51 | { |
| 52 | this->gio_apps.clear (); |
| 53 | |
| 54 | std::vector<Glib::RefPtr<Gio::AppInfo> > apps = Gio::AppInfo::get_all (); |
| 55 | |
| 56 | for (std::vector<Glib::RefPtr<Gio::AppInfo> >::const_iterator it = apps.begin (); |
| 57 | it != apps.end (); ++it) |
| 58 | { |
| 59 | Glib::RefPtr<Gio::AppInfo> app = *it; |
| 60 | Glib::RefPtr<Gio::DesktopAppInfo> desktop = std::dynamic_pointer_cast<Gio::DesktopAppInfo>(app); |
| 61 | std::string executable = app->get_executable (); |
| 62 | std::string flatpak = desktop->get_string ("X-Flatpak"); |
| 63 | bool visible = app->should_show (); |
| 64 | |
| 65 | if (!flatpak.empty ()) |
| 66 | this->gio_apps[flatpak] = app; |
| 67 | else if (executable != "sh" && |
| 68 | executable != "env" && |
| 69 | visible) |
| 70 | this->gio_apps[executable] = app; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | PrettyTable::file_monitor_event (Glib::RefPtr<Gio::File>, |
| 76 | Glib::RefPtr<Gio::File>, |
| 77 | Gio::FileMonitor::Event) |
| 78 | { |
| 79 | this->init_gio_app_cache (); |
| 80 | } |
| 81 | |
| 82 | Glib::RefPtr<Gdk::Texture> |
| 83 | PrettyTable::get_icon_from_theme (const ProcInfo &info) |
| 84 | { |
| 85 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 86 | Glib::RefPtr<Gdk::Texture> icon; |
| 87 | |
| 88 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 89 | |
| 90 | if (icon_theme->has_icon (info.name)) |
| 91 | { |
| 92 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 93 | |
| 94 | // Because g-s-m still uses GtkCellView which does not support GdkPaintable |
| 95 | // the easiest way to get GdkPixbuf or GdkTexture is to get the icon, get |
| 96 | // the file behind it, make it into a sized GdkPixbuf and turn it into |
| 97 | // GdkTexture. |
| 98 | // Once g-s-m uses GtkColumnView, GdkPaintable could be used instead. |
| 99 | icon_paintable = icon_theme->lookup_icon ( |
| 100 | info.name, |
| 101 | APP_ICON_SIZE, |
| 102 | 1, |
| 103 | Gtk::TextDirection::NONE, |
| 104 | Gtk::IconLookupFlags::PRELOAD); |
| 105 | |
| 106 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (icon_paintable->get_file ()->get_path (), APP_ICON_SIZE, APP_ICON_SIZE)); |
| 107 | } |
| 108 | |
| 109 | return icon; |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | PrettyTable::get_default_icon_name (const string &cmd, |
| 114 | string & name) |
| 115 | { |
| 116 | for (size_t i = 0; i != G_N_ELEMENTS (default_table); ++i) |
| 117 | if (default_table[i].command->match (cmd.c_str ())) |
| 118 | { |
| 119 | name = default_table[i].icon; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | Try to get an icon from the default_table |
| 128 | If it's not in defaults, try to load it. |
| 129 | If there is no default for a command, store NULL in defaults |
| 130 | so we don't have to lookup again. |
| 131 | */ |
| 132 | Glib::RefPtr<Gdk::Texture> |
| 133 | PrettyTable::get_icon_from_default (const ProcInfo &info) |
| 134 | { |
| 135 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 136 | Glib::RefPtr<Gdk::Texture> icon; |
| 137 | string name; |
| 138 | |
| 139 | if (!this->get_default_icon_name (info.name, name)) |
| 140 | return icon; |
| 141 | |
| 142 | IconCache::iterator it (this->defaults.find (name)); |
| 143 | if (it == this->defaults.end ()) |
| 144 | { |
| 145 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 146 | |
| 147 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 148 | |
| 149 | if (!icon_theme->has_icon (name)) |
| 150 | return icon; |
| 151 | |
| 152 | // Because g-s-m still uses GtkCellView which does not support GdkPaintable |
| 153 | // the easiest way to get GdkPixbuf or GdkTexture is to get the icon, get |
| 154 | // the file behind it, make it into a sized GdkPixbuf and turn it into |
| 155 | // GdkTexture. |
| 156 | // Once g-s-m uses GtkColumnView, GdkPaintable could be used instead. |
| 157 | icon_paintable = icon_theme->lookup_icon ( |
| 158 | name, |
| 159 | APP_ICON_SIZE, |
| 160 | 1, |
| 161 | Gtk::TextDirection::NONE, |
| 162 | Gtk::IconLookupFlags::PRELOAD); |
| 163 | |
| 164 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (icon_paintable->get_file ()->get_path (), APP_ICON_SIZE, APP_ICON_SIZE)); |
| 165 | |
| 166 | this->defaults[name] = icon; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | icon = it->second; |
| 171 | } |
| 172 | |
| 173 | return icon; |
| 174 | } |
| 175 | |
| 176 | Glib::RefPtr<Gdk::Texture> |
| 177 | PrettyTable::get_icon_from_gio (const ProcInfo &info) |
| 178 | { |
| 179 | Glib::RefPtr<Gdk::Texture> icon; |
| 180 | Glib::RefPtr<Gio::Icon> gicon; |
| 181 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 182 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 183 | const auto executable = info.name.substr (0, info.name.find (' ')); |
| 184 | |
| 185 | Glib::RefPtr<Gio::AppInfo> app = this->gio_apps[executable]; |
| 186 | |
| 187 | if (!app) |
| 188 | { |
| 189 | std::string flatpak; |
| 190 | size_t last_dash, second_last_dash; |
| 191 | |
| 192 | // Extract flatpak id from cgroup_name |
| 193 | flatpak = info.cgroup_name.c_str (); |
| 194 | flatpak = flatpak.substr (flatpak.find_last_of ('/') + 1); |
| 195 | last_dash = flatpak.find_last_of ('-'); |
| 196 | second_last_dash = flatpak.find_last_of ('-', flatpak.find_last_of ('-') - 1); |
| 197 | flatpak = flatpak.substr (second_last_dash + 1, last_dash - second_last_dash - 1); |
| 198 | |
| 199 | app = this->gio_apps[flatpak]; |
| 200 | |
| 201 | if (!app) |
| 202 | return icon; |
| 203 | } |
| 204 | |
| 205 | gicon = app->get_icon (); |
| 206 | if (!gicon) |
| 207 | return icon; |
| 208 | |
| 209 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 210 | |
| 211 | icon_paintable = icon_theme->lookup_icon ( |
| 212 | gicon, |
| 213 | APP_ICON_SIZE, |
| 214 | 1, |
| 215 | Gtk::TextDirection::NONE, |
| 216 | Gtk::IconLookupFlags::PRELOAD); |
| 217 | |
| 218 | std::string path = icon_paintable->get_file ()->get_path (); |
| 219 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (path, APP_ICON_SIZE, APP_ICON_SIZE)); |
| 220 | |
| 221 | return icon; |
| 222 | } |
| 223 | |
| 224 | Glib::RefPtr<Gdk::Texture> |
| 225 | PrettyTable::get_icon_from_name (const ProcInfo &info) |
| 226 | { |
| 227 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 228 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 229 | Glib::RefPtr<Gdk::Texture> icon; |
| 230 | |
| 231 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 232 | |
| 233 | if (!icon_theme->has_icon (info.name)) |
| 234 | return icon; |
| 235 | |
| 236 | icon_paintable = icon_theme->lookup_icon ( |
| 237 | info.name, |
| 238 | APP_ICON_SIZE, |
| 239 | 1, |
| 240 | Gtk::TextDirection::NONE, |
| 241 | Gtk::IconLookupFlags::PRELOAD); |
| 242 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (icon_paintable->get_file ()->get_path (), APP_ICON_SIZE, APP_ICON_SIZE)); |
| 243 | |
| 244 | return icon; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | Glib::RefPtr<Gdk::Texture> |
| 249 | PrettyTable::get_icon_dummy (const ProcInfo &) |
| 250 | { |
| 251 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 252 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 253 | Glib::RefPtr<Gdk::Texture> icon; |
| 254 | |
| 255 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 256 | icon_paintable = icon_theme->lookup_icon ( |
| 257 | "application-x-executable", |
| 258 | APP_ICON_SIZE, |
| 259 | 1, |
| 260 | Gtk::TextDirection::NONE, |
| 261 | Gtk::IconLookupFlags::PRELOAD); |
| 262 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (icon_paintable->get_file ()->get_path (), APP_ICON_SIZE, APP_ICON_SIZE)); |
| 263 | |
| 264 | return icon; |
| 265 | } |
| 266 | |
| 267 | namespace |
| 268 | { |
| 269 | bool |
| 270 | has_kthreadd () |
| 271 | { |
| 272 | glibtop_proc_state buf; |
| 273 | |
| 274 | glibtop_get_proc_state (&buf, 2); |
| 275 | |
| 276 | return buf.cmd == string ("kthreadd"); |
| 277 | } |
| 278 | |
| 279 | // @pre: has_kthreadd |
| 280 | bool |
| 281 | is_kthread (const ProcInfo &info) |
| 282 | { |
| 283 | return info.pid == 2 or info.ppid == 2; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | Glib::RefPtr<Gdk::Texture> |
| 288 | PrettyTable::get_icon_for_kernel (const ProcInfo &info) |
| 289 | { |
| 290 | Glib::RefPtr<Gdk::Texture> icon; |
| 291 | Glib::RefPtr<Gtk::IconTheme> icon_theme; |
| 292 | Glib::RefPtr<Gtk::IconPaintable> icon_paintable; |
| 293 | |
| 294 | if (!is_kthread (info)) |
| 295 | return icon; |
| 296 | |
| 297 | icon_theme = Gtk::IconTheme::get_for_display (Gdk::Display::get_default ()); |
| 298 | icon_paintable = icon_theme->lookup_icon ( |
| 299 | "application-x-executable", |
| 300 | APP_ICON_SIZE, |
| 301 | 1, |
| 302 | Gtk::TextDirection::NONE, |
| 303 | Gtk::IconLookupFlags::PRELOAD); |
| 304 | icon = Gdk::Texture::create_for_pixbuf (Gdk::Pixbuf::create_from_file (icon_paintable->get_file ()->get_path (), APP_ICON_SIZE, APP_ICON_SIZE)); |
| 305 | |
| 306 | return icon; |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | PrettyTable::set_icon (ProcInfo &info) |
| 311 | { |
| 312 | typedef Glib::RefPtr<Gdk::Texture> (PrettyTable::*Getter) (const ProcInfo &); |
| 313 | |
| 314 | static std::vector<Getter> getters; |
| 315 | |
| 316 | if (getters.empty ()) |
| 317 | { |
| 318 | getters.push_back (&PrettyTable::get_icon_from_gio); |
| 319 | getters.push_back (&PrettyTable::get_icon_from_theme); |
| 320 | getters.push_back (&PrettyTable::get_icon_from_default); |
| 321 | getters.push_back (&PrettyTable::get_icon_from_name); |
| 322 | if (has_kthreadd ()) |
| 323 | { |
| 324 | procman_debug ("kthreadd is running with PID 2"); |
| 325 | getters.push_back (&PrettyTable::get_icon_for_kernel); |
| 326 | } |
| 327 | getters.push_back (&PrettyTable::get_icon_dummy); |
| 328 | } |
| 329 | |
| 330 | Glib::RefPtr<Gdk::Texture> icon; |
| 331 | |
| 332 | for (size_t i = 0; not icon and i < getters.size (); ++i) |
| 333 | { |
| 334 | try { |
| 335 | icon = (this->*getters[i])(info); |
| 336 | } |
| 337 | catch (std::exception&e) { |
| 338 | g_warning ("Failed to load icon for %s(%u) : %s", info.name.c_str (), info.pid, e.what ()); |
| 339 | continue; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | info.set_icon (icon); |
| 344 | } |