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"
50 /* ---------------------------------------------------------------------------------------------------- */
51 /* these functions are based on code from libwnck (LGPLv2) */
53 static gboolean get_window_list (Display *xdisplay,
59 static char* get_utf8_property (Display *xdisplay,
63 static gboolean get_cardinal (Display *xdisplay,
68 static gboolean read_rgb_icon (Display *xdisplay,
78 get_cardinal (Display *xdisplay,
92 gdk_error_trap_push ();
94 result = XGetWindowProperty (xdisplay,
98 False, XA_CARDINAL, &type, &format, &nitems,
99 &bytes_after, (void*)&num);
100 XSync (xdisplay, False);
101 err = gdk_error_trap_pop ();
103 if (err != Success ||
107 if (type != XA_CARDINAL)
121 get_utf8_property (Display *xdisplay,
134 utf8_string = gdk_x11_get_xatom_by_name ("UTF8_STRING");
136 gdk_error_trap_push ();
139 result = XGetWindowProperty (xdisplay,
144 &type, &format, &nitems,
145 &bytes_after, (guchar **)&val);
146 XSync (xdisplay, False);
147 err = gdk_error_trap_pop ();
149 if (err != Success ||
153 if (type != utf8_string ||
162 if (!g_utf8_validate (val, nitems, NULL))
164 g_warning ("Property %s contained invalid UTF-8\n",
165 gdk_x11_get_xatom_name (atom));
170 retval = g_strndup (val, nitems);
178 find_largest_sizes (gulong *data,
194 return FALSE; /* no space for w, h */
199 if (nitems < ((w * h) + 2))
200 return FALSE; /* not enough data */
202 *width = MAX (w, *width);
203 *height = MAX (h, *height);
206 nitems -= (w * h) + 2;
213 find_best_size (gulong *data,
224 int max_width, max_height;
230 if (!find_largest_sizes (data, nitems, &max_width, &max_height))
234 ideal_width = max_width;
235 if (ideal_height < 0)
236 ideal_height = max_height;
250 return FALSE; /* no space for w, h */
255 if (nitems < ((w * h) + 2))
256 break; /* not enough data */
258 if (best_start == NULL)
264 /* work with averages */
265 const int ideal_size = (ideal_width + ideal_height) / 2;
266 int best_size = (best_w + best_h) / 2;
267 int this_size = (w + h) / 2;
269 /* larger than desired is always better than smaller */
270 if (best_size < ideal_size &&
271 this_size >= ideal_size)
273 /* if we have too small, pick anything bigger */
274 else if (best_size < ideal_size &&
275 this_size > best_size)
277 /* if we have too large, pick anything smaller
278 * but still >= the ideal
280 else if (best_size > ideal_size &&
281 this_size >= ideal_size &&
282 this_size < best_size)
288 best_start = data + 2;
294 nitems -= (w * h) + 2;
309 argbdata_to_pixdata (gulong *argb_data,
316 *pixdata = g_new (guchar, len * 4);
319 /* One could speed this up a lot. */
327 rgba = (argb << 8) | (argb >> 24);
331 *p = (rgba >> 16) & 0xff;
333 *p = (rgba >> 8) & 0xff;
343 read_rgb_icon (Display *xdisplay,
360 gdk_error_trap_push ();
363 result = XGetWindowProperty (xdisplay,
365 gdk_x11_get_xatom_by_name ("_NET_WM_ICON"),
367 False, XA_CARDINAL, &type, &format, &nitems,
368 &bytes_after, (void*)&data);
370 XSync (xdisplay, False);
371 err = gdk_error_trap_pop ();
373 if (err != Success ||
377 if (type != XA_CARDINAL)
383 if (!find_best_size (data, nitems,
384 ideal_width, ideal_height,
394 argbdata_to_pixdata (best, w * h, pixdata);
402 free_pixels (guchar *pixels, gpointer data)
408 scaled_from_pixdata (guchar *pixdata,
417 src = gdk_pixbuf_new_from_data (pixdata,
435 tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, size, size);
439 gdk_pixbuf_fill (tmp, 0);
440 gdk_pixbuf_copy_area (src, 0, 0, w, h,
442 (size - w) / 2, (size - h) / 2);
444 g_object_unref (src);
449 if (w != new_w || h != new_h)
451 dest = gdk_pixbuf_scale_simple (src, new_w, new_h, GDK_INTERP_BILINEAR);
453 g_object_unref (G_OBJECT (src));
464 get_window_list (Display *xdisplay,
480 gdk_error_trap_push ();
482 result = XGetWindowProperty (xdisplay,
486 False, XA_WINDOW, &type, &format, &nitems,
487 &bytes_after, (void*)&data);
488 XSync (xdisplay, False);
489 err = gdk_error_trap_pop ();
491 if (err != Success ||
495 if (type != XA_WINDOW)
501 *windows = g_new (Window, nitems);
502 memcpy (*windows, data, sizeof (Window) * nitems);
511 /* ---------------------------------------------------------------------------------------------------- */
513 struct _GtkMountOperationLookupContext
515 /* Hash from pid (gint) -> XID (gint)
517 * Note that XIDs are at most 27 bits - however, also note that sizeof(XID) == 8 on
518 * x86_64 - that's just xlib brokenness. So it's safe to stuff the XID into a pointer.
520 GHashTable *pid_to_window;
524 GtkMountOperationLookupContext *
525 _gtk_mount_operation_lookup_context_get (GdkDisplay *display)
527 GtkMountOperationLookupContext *context;
532 context = g_new0 (GtkMountOperationLookupContext, 1);
534 context->pid_to_window = g_hash_table_new (g_direct_hash, g_direct_equal);
535 context->display = display;
539 get_window_list (GDK_DISPLAY_XDISPLAY (context->display),
541 gdk_x11_get_xatom_by_name_for_display (context->display,
545 for (n = 0; n < mapping_length; n++)
549 if (!get_cardinal (GDK_DISPLAY_XDISPLAY (context->display),
551 gdk_x11_get_xatom_by_name_for_display (context->display,
556 g_hash_table_insert (context->pid_to_window,
557 GINT_TO_POINTER (pid),
558 GINT_TO_POINTER ((gint) mapping[n]));
566 _gtk_mount_operation_lookup_context_free (GtkMountOperationLookupContext *context)
568 g_hash_table_unref (context->pid_to_window);
572 /* ---------------------------------------------------------------------------------------------------- */
577 pid_get_parent (GPid pid)
581 gchar *stat_filename;
582 gchar *stat_contents;
587 stat_contents = NULL;
588 stat_filename = NULL;
590 /* fail if trying to get the parent of the init process (no such thing) */
594 stat_filename = g_strdup_printf ("/proc/%d/status", pid);
595 if (g_file_get_contents (stat_filename,
602 tokens = g_strsplit (stat_contents, "\n", 0);
604 for (n = 0; tokens[n] != NULL; n++)
606 if (g_str_has_prefix (tokens[n], "PPid:"))
611 ppid = strtoll (tokens[n] + sizeof "PPid:" - 1, &endp, 10);
612 if (endp == NULL || *endp != '\0')
614 g_warning ("Error parsing contents of `%s'. Parent pid is malformed.",
627 g_free (stat_contents);
628 g_free (stat_filename);
634 pid_get_env (GPid pid,
646 key_len = strlen (key);
648 env_filename = g_strdup_printf ("/proc/%d/environ", pid);
649 if (g_file_get_contents (env_filename,
656 /* /proc/<pid>/environ in Linux is split at '\0' points, g_strsplit() can't handle that... */
660 if (env[n] == '\0' || n >= env_len)
663 if (g_str_has_prefix (env + n, key) && (*(env + n + key_len) == '='))
665 ret = g_strdup (env + n + key_len + 1);
667 /* skip invalid UTF-8 */
668 if (!g_utf8_validate (ret, -1, (const gchar **) &end))
673 for (; env[n] != '\0' && n < env_len; n++)
679 g_free (env_filename);
685 pid_get_command_line (GPid pid)
687 gchar *cmdline_filename;
688 gchar *cmdline_contents;
693 cmdline_contents = NULL;
695 cmdline_filename = g_strdup_printf ("/proc/%d/cmdline", pid);
696 if (!g_file_get_contents (cmdline_filename,
702 /* /proc/<pid>/cmdline separates args by NUL-bytes - replace with spaces */
703 for (n = 0; n < cmdline_len - 1; n++)
705 if (cmdline_contents[n] == '\0')
706 cmdline_contents[n] = ' ';
709 /* skip invalid UTF-8 */
710 if (!g_utf8_validate (cmdline_contents, -1, (const gchar **) &end))
714 g_free (cmdline_filename);
716 return cmdline_contents;
719 /* ---------------------------------------------------------------------------------------------------- */
722 /* TODO: please implement for your OS - must return valid UTF-8 */
725 pid_get_parent (GPid pid)
731 pid_get_env (GPid pid,
738 pid_get_command_line (GPid pid)
745 /* ---------------------------------------------------------------------------------------------------- */
748 get_name_for_window_with_pid (GtkMountOperationLookupContext *context,
752 Window windowid_window;
757 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
760 gchar *windowid_value;
762 /* check for $WINDOWID (set by terminals) and see if we can get the title that way */
763 windowid_value = pid_get_env (pid, "WINDOWID");
764 if (windowid_value != NULL)
769 windowid_window = (Window) g_ascii_strtoll (windowid_value, &endp, 10);
770 if (endp != NULL || *endp == '\0')
772 window = windowid_window;
774 g_free (windowid_value);
777 /* otherwise, check for parents */
782 pid = pid_get_parent (pid);
786 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
796 ret = get_utf8_property (GDK_DISPLAY_XDISPLAY (context->display),
798 gdk_x11_get_xatom_by_name_for_display (context->display,
801 ret = get_utf8_property (GDK_DISPLAY_XDISPLAY (context->display),
802 window, gdk_x11_get_xatom_by_name_for_display (context->display,
803 "_NET_WM_ICON_NAME"));
809 /* ---------------------------------------------------------------------------------------------------- */
812 get_pixbuf_for_window_with_pid (GtkMountOperationLookupContext *context,
821 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
824 /* otherwise, check for parents */
827 pid = pid_get_parent (pid);
831 window = GPOINTER_TO_INT (g_hash_table_lookup (context->pid_to_window, GINT_TO_POINTER (pid)));
844 if (read_rgb_icon (GDK_DISPLAY_XDISPLAY (context->display),
846 size_pixels, size_pixels,
851 ret = scaled_from_pixdata (pixdata,
853 size_pixels, size_pixels);
860 /* ---------------------------------------------------------------------------------------------------- */
862 static const gchar *well_known_commands[] =
864 /* translators: this string is a name for the 'less' command */
865 "less", N_("Terminal Pager"),
866 "top", N_("Top Command"),
867 "bash", N_("Bourne Again Shell"),
868 "sh", N_("Bourne Shell"),
869 "zsh", N_("Z Shell"),
874 _gtk_mount_operation_lookup_info (GtkMountOperationLookupContext *context,
878 gchar **out_command_line,
879 GdkPixbuf **out_pixbuf)
881 g_return_val_if_fail (out_name != NULL && *out_name == NULL, FALSE);
882 g_return_val_if_fail (out_command_line != NULL && *out_command_line == NULL, FALSE);
883 g_return_val_if_fail (out_pixbuf != NULL && *out_pixbuf == NULL, FALSE);
885 /* We perform two different lookups for name and icon size.. this is
886 * because we want the name from the window with WINDOWID and this
887 * normally does not give you an icon
889 * (the canonical example is a tab in gnome-terminal - the shell/command running
890 * in the shell will have WINDOWID set - but this window won't have an icon - so
891 * we want to continue up until the gnome-terminal window so we can get that icon)
894 *out_command_line = pid_get_command_line (pid);
896 *out_name = get_name_for_window_with_pid (context, pid);
898 *out_pixbuf = get_pixbuf_for_window_with_pid (context, pid, size_pixels);
900 /* if we didn't manage to find the name via X, fall back to the basename
901 * of the first element of the command line and, for maximum geek-comfort,
902 * map a few well-known commands to proper translated names
904 if (*out_name == NULL && *out_command_line != NULL &&
905 strlen (*out_command_line) > 0 && (*out_command_line)[0] != ' ')
911 /* find the first character after the first argument */
912 s = strchr (*out_command_line, ' ');
914 s = *out_command_line + strlen (*out_command_line);
916 for (p = s; p > *out_command_line; p--)
925 *out_name = g_strndup (p, s - p);
927 for (n = 0; well_known_commands[n] != NULL; n += 2)
929 /* sometimes the command is prefixed with a -, e.g. '-bash' instead
930 * of 'bash' - handle that as well
932 if ((strcmp (well_known_commands[n], *out_name) == 0) ||
933 ((*out_name)[0] == '-' && (strcmp (well_known_commands[n], (*out_name) + 1) == 0)))
936 *out_name = g_strdup (_(well_known_commands[n+1]));
946 _gtk_mount_operation_kill_process (GPid pid,
953 if (kill ((pid_t) pid, SIGTERM) != 0)
957 /* TODO: On EPERM, we could use a setuid helper using polkit (very easy to implement
958 * via pkexec(1)) to allow the user to e.g. authenticate to gain the authorization
959 * to kill the process. But that's not how things currently work.
965 g_io_error_from_errno (errsv),
966 _("Cannot end process with pid %d: %s"),