1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3 * Copyright (C) David Zeuthen <davidz@redhat.com>
4 * Copyright (C) 2001 Havoc Pennington
5 * Copyright (C) 2005-2007 Vincent Untz
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include <X11/Xatom.h>
37 #include <gtk/gtkicontheme.h>
40 /* for the kill(2) system call and errno - POSIX.1-2001 and later */
41 #include <sys/types.h>
45 #include "gtkmountoperationprivate.h"
47 /* ---------------------------------------------------------------------------------------------------- */
48 /* these functions are based on code from libwnck (LGPLv2) */
50 static gboolean get_window_list (Display *xdisplay,
56 static char* get_utf8_property (Display *xdisplay,
60 static gboolean get_cardinal (Display *xdisplay,
65 static gboolean read_rgb_icon (Display *xdisplay,
75 get_cardinal (Display *xdisplay,
89 gdk_error_trap_push ();
91 result = XGetWindowProperty (xdisplay,
95 False, XA_CARDINAL, &type, &format, &nitems,
96 &bytes_after, (void*)&num);
97 XSync (xdisplay, False);
98 err = gdk_error_trap_pop ();
100 if (err != Success ||
104 if (type != XA_CARDINAL)
118 get_utf8_property (Display *xdisplay,
131 utf8_string = gdk_x11_get_xatom_by_name ("UTF8_STRING");
133 gdk_error_trap_push ();
136 result = XGetWindowProperty (xdisplay,
141 &type, &format, &nitems,
142 &bytes_after, (guchar **)&val);
143 XSync (xdisplay, False);
144 err = gdk_error_trap_pop ();
146 if (err != Success ||
150 if (type != utf8_string ||
159 if (!g_utf8_validate (val, nitems, NULL))
161 g_warning ("Property %s contained invalid UTF-8\n",
162 gdk_x11_get_xatom_name (atom));
167 retval = g_strndup (val, nitems);
175 find_largest_sizes (gulong *data,
188 return FALSE; /* no space for w, h */
193 if (nitems < ((w * h) + 2))
194 return FALSE; /* not enough data */
196 *width = MAX (w, *width);
197 *height = MAX (h, *height);
200 nitems -= (w * h) + 2;
207 find_best_size (gulong *data,
218 int max_width, max_height;
224 if (!find_largest_sizes (data, nitems, &max_width, &max_height))
228 ideal_width = max_width;
229 if (ideal_height < 0)
230 ideal_height = max_height;
244 return FALSE; /* no space for w, h */
249 if (nitems < ((w * h) + 2))
250 break; /* not enough data */
252 if (best_start == NULL)
258 /* work with averages */
259 const int ideal_size = (ideal_width + ideal_height) / 2;
260 int best_size = (best_w + best_h) / 2;
261 int this_size = (w + h) / 2;
263 /* larger than desired is always better than smaller */
264 if (best_size < ideal_size &&
265 this_size >= ideal_size)
267 /* if we have too small, pick anything bigger */
268 else if (best_size < ideal_size &&
269 this_size > best_size)
271 /* if we have too large, pick anything smaller
272 * but still >= the ideal
274 else if (best_size > ideal_size &&
275 this_size >= ideal_size &&
276 this_size < best_size)
282 best_start = data + 2;
288 nitems -= (w * h) + 2;
303 argbdata_to_pixdata (gulong *argb_data,
310 *pixdata = g_new (guchar, len * 4);
313 /* One could speed this up a lot. */
321 rgba = (argb << 8) | (argb >> 24);
325 *p = (rgba >> 16) & 0xff;
327 *p = (rgba >> 8) & 0xff;
337 read_rgb_icon (Display *xdisplay,
354 gdk_error_trap_push ();
357 result = XGetWindowProperty (xdisplay,
359 gdk_x11_get_xatom_by_name ("_NET_WM_ICON"),
361 False, XA_CARDINAL, &type, &format, &nitems,
362 &bytes_after, (void*)&data);
364 XSync (xdisplay, False);
365 err = gdk_error_trap_pop ();
367 if (err != Success ||
371 if (type != XA_CARDINAL)
377 if (!find_best_size (data, nitems,
378 ideal_width, ideal_height,
388 argbdata_to_pixdata (best, w * h, pixdata);
396 free_pixels (guchar *pixels, gpointer data)
402 scaled_from_pixdata (guchar *pixdata,
411 src = gdk_pixbuf_new_from_data (pixdata,
429 tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, size, size);
433 gdk_pixbuf_fill (tmp, 0);
434 gdk_pixbuf_copy_area (src, 0, 0, w, h,
436 (size - w) / 2, (size - h) / 2);
438 g_object_unref (src);
443 if (w != new_w || h != new_h)
445 dest = gdk_pixbuf_scale_simple (src, new_w, new_h, GDK_INTERP_BILINEAR);
447 g_object_unref (G_OBJECT (src));
458 get_window_list (Display *xdisplay,
474 gdk_error_trap_push ();
476 result = XGetWindowProperty (xdisplay,
480 False, XA_WINDOW, &type, &format, &nitems,
481 &bytes_after, (void*)&data);
482 XSync (xdisplay, False);
483 err = gdk_error_trap_pop ();
485 if (err != Success ||
489 if (type != XA_WINDOW)
495 *windows = g_new (Window, nitems);
496 memcpy (*windows, data, sizeof (Window) * nitems);
505 /* ---------------------------------------------------------------------------------------------------- */
507 struct _GtkMountOperationLookupContext
509 /* Hash from pid (gint) -> XID (gint)
511 * Note that XIDs are at most 27 bits - however, also note that sizeof(XID) == 8 on
512 * x86_64 - that's just xlib brokenness. So it's safe to stuff the XID into a pointer.
514 GHashTable *pid_to_window;
518 GtkMountOperationLookupContext *
519 _gtk_mount_operation_lookup_context_get (GdkDisplay *display)
521 GtkMountOperationLookupContext *context;
526 context = g_new0 (GtkMountOperationLookupContext, 1);
528 context->pid_to_window = g_hash_table_new (g_direct_hash, g_direct_equal);
529 context->display = display;
533 get_window_list (GDK_DISPLAY_XDISPLAY (context->display),
535 gdk_x11_get_xatom_by_name_for_display (context->display,
539 for (n = 0; n < mapping_length; n++)
543 if (!get_cardinal (GDK_DISPLAY_XDISPLAY (context->display),
545 gdk_x11_get_xatom_by_name_for_display (context->display,
550 g_hash_table_insert (context->pid_to_window,
551 GINT_TO_POINTER (pid),
552 GINT_TO_POINTER ((gint) mapping[n]));
560 _gtk_mount_operation_lookup_context_free (GtkMountOperationLookupContext *context)
562 g_hash_table_unref (context->pid_to_window);
566 /* ---------------------------------------------------------------------------------------------------- */
571 pid_get_parent (GPid pid)
575 gchar *stat_filename;
576 gchar *stat_contents;
581 stat_contents = NULL;
582 stat_filename = NULL;
584 /* fail if trying to get the parent of the init process (no such thing) */
588 stat_filename = g_strdup_printf ("/proc/%d/status", pid);
589 if (g_file_get_contents (stat_filename,
596 tokens = g_strsplit (stat_contents, "\n", 0);
598 for (n = 0; tokens[n] != NULL; n++)
600 if (g_str_has_prefix (tokens[n], "PPid:"))
605 ppid = strtoll (tokens[n] + sizeof "PPid:" - 1, &endp, 10);
606 if (endp == NULL || *endp != '\0')
608 g_warning ("Error parsing contents of `%s'. Parent pid is malformed.",
621 g_free (stat_contents);
622 g_free (stat_filename);
628 pid_get_env (GPid pid,
640 key_len = strlen (key);
642 env_filename = g_strdup_printf ("/proc/%d/environ", pid);
643 if (g_file_get_contents (env_filename,
650 /* /proc/<pid>/environ in Linux is split at '\0' points, g_strsplit() can't handle that... */
654 if (env[n] == '\0' || n >= env_len)
657 if (g_str_has_prefix (env + n, key) && (*(env + n + key_len) == '='))
659 ret = g_strdup (env + n + key_len + 1);
661 /* skip invalid UTF-8 */
662 if (!g_utf8_validate (ret, -1, (const gchar **) &end))
667 for (; env[n] != '\0' && n < env_len; n++)
673 g_free (env_filename);
679 pid_get_command_line (GPid pid)
681 gchar *cmdline_filename;
682 gchar *cmdline_contents;
687 cmdline_contents = NULL;
689 cmdline_filename = g_strdup_printf ("/proc/%d/cmdline", pid);
690 if (!g_file_get_contents (cmdline_filename,
696 /* /proc/<pid>/cmdline separates args by NUL-bytes - replace with spaces */
697 for (n = 0; n < cmdline_len - 1; n++)
699 if (cmdline_contents[n] == '\0')
700 cmdline_contents[n] = ' ';
703 /* skip invalid UTF-8 */
704 if (!g_utf8_validate (cmdline_contents, -1, (const gchar **) &end))
708 g_free (cmdline_filename);
710 return cmdline_contents;
713 /* ---------------------------------------------------------------------------------------------------- */
716 /* TODO: please implement for your OS - must return valid UTF-8 */
719 pid_get_parent (GPid pid)
725 pid_get_env (GPid pid,
732 pid_get_command_line (GPid pid)
739 /* ---------------------------------------------------------------------------------------------------- */
742 get_name_for_window_with_pid (GtkMountOperationLookupContext *context,
746 Window windowid_window;
751 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
754 gchar *windowid_value;
756 /* check for $WINDOWID (set by terminals) and see if we can get the title that way */
757 windowid_value = pid_get_env (pid, "WINDOWID");
758 if (windowid_value != NULL)
763 windowid_window = (Window) g_ascii_strtoll (windowid_value, &endp, 10);
764 if (endp != NULL || *endp == '\0')
766 window = windowid_window;
768 g_free (windowid_value);
771 /* otherwise, check for parents */
776 pid = pid_get_parent (pid);
780 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
790 ret = get_utf8_property (GDK_DISPLAY_XDISPLAY (context->display),
792 gdk_x11_get_xatom_by_name_for_display (context->display,
795 ret = get_utf8_property (GDK_DISPLAY_XDISPLAY (context->display),
796 window, gdk_x11_get_xatom_by_name_for_display (context->display,
797 "_NET_WM_ICON_NAME"));
803 /* ---------------------------------------------------------------------------------------------------- */
806 get_pixbuf_for_window_with_pid (GtkMountOperationLookupContext *context,
815 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
818 /* otherwise, check for parents */
821 pid = pid_get_parent (pid);
825 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
838 if (read_rgb_icon (GDK_DISPLAY_XDISPLAY (context->display),
840 size_pixels, size_pixels,
845 ret = scaled_from_pixdata (pixdata,
847 size_pixels, size_pixels);
854 /* ---------------------------------------------------------------------------------------------------- */
856 static const gchar *well_known_commands[] =
858 /* translators: this string is a name for the 'less' command */
859 "less", N_("Terminal Pager"),
860 "top", N_("Top Command"),
861 "bash", N_("Bourne Again Shell"),
862 "sh", N_("Bourne Shell"),
863 "zsh", N_("Z Shell"),
868 _gtk_mount_operation_lookup_info (GtkMountOperationLookupContext *context,
872 gchar **out_command_line,
873 GdkPixbuf **out_pixbuf)
875 g_return_val_if_fail (out_name != NULL && *out_name == NULL, FALSE);
876 g_return_val_if_fail (out_command_line != NULL && *out_command_line == NULL, FALSE);
877 g_return_val_if_fail (out_pixbuf != NULL && *out_pixbuf == NULL, FALSE);
879 /* We perform two different lookups for name and icon size.. this is
880 * because we want the name from the window with WINDOWID and this
881 * normally does not give you an icon
883 * (the canonical example is a tab in gnome-terminal - the shell/command running
884 * in the shell will have WINDOWID set - but this window won't have an icon - so
885 * we want to continue up until the gnome-terminal window so we can get that icon)
888 *out_command_line = pid_get_command_line (pid);
890 *out_name = get_name_for_window_with_pid (context, pid);
892 *out_pixbuf = get_pixbuf_for_window_with_pid (context, pid, size_pixels);
894 /* if we didn't manage to find the name via X, fall back to the basename
895 * of the first element of the command line and, for maximum geek-comfort,
896 * map a few well-known commands to proper translated names
898 if (*out_name == NULL && *out_command_line != NULL &&
899 strlen (*out_command_line) > 0 && (*out_command_line)[0] != ' ')
905 /* find the first character after the first argument */
906 s = strchr (*out_command_line, ' ');
908 s = *out_command_line + strlen (*out_command_line);
910 for (p = s; p > *out_command_line; p--)
919 *out_name = g_strndup (p, s - p);
921 for (n = 0; well_known_commands[n] != NULL; n += 2)
923 /* sometimes the command is prefixed with a -, e.g. '-bash' instead
924 * of 'bash' - handle that as well
926 if ((strcmp (well_known_commands[n], *out_name) == 0) ||
927 ((*out_name)[0] == '-' && (strcmp (well_known_commands[n], (*out_name) + 1) == 0)))
930 *out_name = g_strdup (_(well_known_commands[n+1]));
940 _gtk_mount_operation_kill_process (GPid pid,
947 if (kill ((pid_t) pid, SIGTERM) != 0)
951 /* TODO: On EPERM, we could use a setuid helper using polkit (very easy to implement
952 * via pkexec(1)) to allow the user to e.g. authenticate to gain the authorization
953 * to kill the process. But that's not how things currently work.
959 g_io_error_from_errno (errsv),
960 _("Cannot end process with PID %d: %s"),