Exemplo n.º 1
0
char*
meta_text_property_to_utf8 (Display             *xdisplay,
                            const XTextProperty *prop)
{
  GdkDisplay *display;
  char **list;
  int count;
  char *retval;

  list = NULL;

  display = gdk_x11_lookup_xdisplay (xdisplay);
  count = gdk_text_property_to_utf8_list_for_display (display,
                                                      gdk_x11_xatom_to_atom_for_display (display, prop->encoding),
                                                      prop->format,
                                                      prop->value,
                                                      prop->nitems,
                                                      &list);

  if (count == 0)
    retval = NULL;
  else
    {
      retval = list[0];
      list[0] = g_strdup (""); /* something to free */
    }

  g_strfreev (list);

  return retval;
}
Exemplo n.º 2
0
static VALUE
rg_m_text_property_to_utf8_list(int argc, VALUE *argv, G_GNUC_UNUSED VALUE self)
{
    gint num, i;
    gchar** list;
    VALUE ret = Qnil;

    if (argc == 3) {
        VALUE encoding, format, text;
        rb_scan_args(argc, argv, "30", &encoding, &format, &text);
        StringValue(text);

        num = gdk_text_property_to_utf8_list(RVAL2ATOM(encoding),
                                             NUM2INT(format),
                                             (const guchar*)RVAL2CSTR(text),
                                             RSTRING_LEN(text), &list);
    } else {
#if GTK_CHECK_VERSION(2,2,0)
        VALUE display, encoding, format, text;
        rb_scan_args(argc, argv, "40", &display, &encoding, &format, &text);
        StringValue(text);

        num = gdk_text_property_to_utf8_list_for_display(GDK_DISPLAY_OBJECT(RVAL2GOBJ(display)),
                RVAL2ATOM(encoding),
                NUM2INT(format),
                (const guchar*)RVAL2CSTR(text),
                RSTRING_LEN(text),
                &list);
#else
        VALUE encoding, format, text;
        rb_scan_args(argc, argv, "30", &encoding, &format, &text);
        StringValue(text);

        rb_warn("Gdk::Property.text_property_to_utf8_list: Not supported arguments in GTK+-2.0.x.");
        num = gdk_text_property_to_utf8_list(RVAL2ATOM(encoding),
                                             NUM2INT(format),
                                             (const guchar*)RVAL2CSTR(text),
                                             RSTRING_LEN(text), &list);
#endif
    }

    ret = rb_ary_new2(num);
    for (i =0; i < num; i++) {
        rb_ary_push(ret, CSTR2RVAL(list[i]));
    }
    g_strfreev(list);
    return ret;
}
Exemplo n.º 3
0
/**
 * gtk_selection_data_get_text:
 * @selection_data: a #GtkSelectionData
 * 
 * Gets the contents of the selection data as a UTF-8 string.
 * 
 * Returns: (type utf8) (nullable) (transfer full): if the selection data contained a
 *   recognized text type and it could be converted to UTF-8, a newly
 *   allocated string containing the converted text, otherwise %NULL.
 *   If the result is non-%NULL it must be freed with g_free().
 **/
guchar *
gtk_selection_data_get_text (const GtkSelectionData *selection_data)
{
  guchar *result = NULL;

  g_return_val_if_fail (selection_data != NULL, NULL);

  init_atoms ();
  
  if (selection_data->length >= 0 &&
      (selection_data->type == g_intern_static_string ("STRING") ||
       selection_data->type == ctext_atom ||
       selection_data->type == utf8_atom))
    {
      gchar **list;
      gint i;
      gint count = gdk_text_property_to_utf8_list_for_display (selection_data->display,
      							       selection_data->type,
						   	       selection_data->format, 
						               selection_data->data,
						               selection_data->length,
						               &list);
      if (count > 0)
	result = (guchar *) list[0];

      for (i = 1; i < count; i++)
	g_free (list[i]);
      g_free (list);
    }
  else if (selection_data->length >= 0 &&
	   (selection_data->type == text_plain_atom ||
	    selection_data->type == text_plain_utf8_atom ||
	    selection_data->type == text_plain_locale_atom))
    {
      result = selection_get_text_plain (selection_data);
    }

  return result;
}
Exemplo n.º 4
0
/* Code to find the active window in order to collect stats for social
 * application browsing.
 */
static void
get_active_application_properties(HippoIdleMonitor *monitor,
                                  char            **wm_class,
                                  char            **title)
{
    Display *xdisplay = GDK_DISPLAY_XDISPLAY(monitor->display);
    int n_screens = gdk_display_get_n_screens(monitor->display);
    Atom net_active_window_x = gdk_x11_get_xatom_by_name_for_display(monitor->display,
                                                                     "_NET_ACTIVE_WINDOW");
    GdkAtom net_active_window_gdk = gdk_atom_intern("_NET_ACTIVE_WINDOW", FALSE);
    Window active_window = None;
    int i;

    Atom type;
    int format;
    unsigned long n_items;
    unsigned long bytes_after;
    guchar *data;
        
    if (wm_class)
        *wm_class = NULL;
    if (title)
        *title = NULL;

    /* Find the currently focused window by looking at the _NET_ACTIVE_WINDOW property
     * on all the screens of the display.
     */
    for (i = 0; i < n_screens; i++) {
        GdkScreen *screen = gdk_display_get_screen(monitor->display, i);
        GdkWindow *root = gdk_screen_get_root_window(screen);

        if (!gdk_x11_screen_supports_net_wm_hint (screen, net_active_window_gdk))
            continue;

        XGetWindowProperty (xdisplay, GDK_DRAWABLE_XID(root),
                            net_active_window_x,
                            0, 1, False, XA_WINDOW,
                            &type, &format, &n_items, &bytes_after, &data);
        if (type == XA_WINDOW) {
            active_window = *(Window *)data;
            XFree(data);
            break;
        }
    }

    /* Now that we have the active window, figure out the application name and WM class
     */
    gdk_error_trap_push();
        
    if (active_window && wm_class) {
        if (XGetWindowProperty (xdisplay, active_window,
                                XA_WM_CLASS,
                                0, G_MAXLONG, False, XA_STRING,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type == XA_STRING)
        {
            if (format == 8) {
                char **list;
                int count;
                
                count = gdk_text_property_to_utf8_list_for_display(monitor->display, GDK_TARGET_STRING,
                                                                   8, data, n_items, &list);

                if (count > 1)
                    *wm_class = g_strdup(list[1]);

                if (list)
                    g_strfreev(list);
            }
            
            XFree(data);
        }
    }

    if (active_window && title) {
        Atom utf8_string = gdk_x11_get_xatom_by_name_for_display(monitor->display, "UTF8_STRING");
        
        if (XGetWindowProperty (xdisplay, active_window,
                                gdk_x11_get_xatom_by_name_for_display(monitor->display, "_NET_WM_NAME"),
                                0, G_MAXLONG, False, utf8_string,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type == utf8_string)
        {
            if (format == 8 && g_utf8_validate((char *)data, -1, NULL)) {
                *title = g_strdup((char *)data);
            }
            
            XFree(data);
        }
    }

    if (active_window && title && *title == NULL) {
        if (XGetWindowProperty (xdisplay, active_window,
                                XA_WM_NAME,
                                0, G_MAXLONG, False, AnyPropertyType,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type != None)
        {
            if (format == 8) {
                char **list;
                int count;
                
                count = gdk_text_property_to_utf8_list_for_display(monitor->display,
                                                                   gdk_x11_xatom_to_atom_for_display(monitor->display, type),
                                                                   8, data, n_items, &list);

                if (count > 0)
                    *title = g_strdup(list[0]);
                
                if (list)
                    g_strfreev(list);
            }
            
            XFree(data);
        }
    }
        
    gdk_error_trap_pop();
}
Exemplo n.º 5
0
/* Code to find the active window in order to collect stats for social
 * application browsing.
 */
static void
get_active_application_properties(HippoIdleMonitor *monitor,
                                  char            **wm_class,
                                  char            **title)
{
    Display *xdisplay = GDK_DISPLAY_XDISPLAY(monitor->display);
    int n_screens = gdk_display_get_n_screens(monitor->display);
    Atom net_active_window_x = gdk_x11_get_xatom_by_name_for_display(monitor->display,
                                                                     "_NET_ACTIVE_WINDOW");
    GdkAtom net_active_window_gdk = gdk_atom_intern("_NET_ACTIVE_WINDOW", FALSE);
    Window active_window = None;
    int i;

    Atom type;
    int format;
    unsigned long n_items;
    unsigned long bytes_after;
    guchar *data;
    gboolean is_desktop = FALSE;
        
    if (wm_class)
        *wm_class = NULL;
    if (title)
        *title = NULL;

    /* Find the currently focused window by looking at the _NET_ACTIVE_WINDOW property
     * on all the screens of the display.
     */
    for (i = 0; i < n_screens; i++) {
        GdkScreen *screen = gdk_display_get_screen(monitor->display, i);
        GdkWindow *root = gdk_screen_get_root_window(screen);

        if (!gdk_x11_screen_supports_net_wm_hint (screen, net_active_window_gdk))
            continue;

        XGetWindowProperty (xdisplay, GDK_DRAWABLE_XID(root),
                            net_active_window_x,
                            0, 1, False, XA_WINDOW,
                            &type, &format, &n_items, &bytes_after, &data);
        if (type == XA_WINDOW) {
            active_window = *(Window *)data;
            XFree(data);
            break;
        }
    }

    /* Now that we have the active window, figure out the application name and WM class
     */
    gdk_error_trap_push();
        
    if (active_window && wm_class) {
        if (XGetWindowProperty (xdisplay, active_window,
                                XA_WM_CLASS,
                                0, G_MAXLONG, False, XA_STRING,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type == XA_STRING)
        {
            if (format == 8) {
                char **list;
                int count;
                
                count = gdk_text_property_to_utf8_list_for_display(monitor->display, GDK_TARGET_STRING,
                                                                   8, data, n_items, &list);

                if (count > 1) {
		    /* This is a check for Nautilus, which sets the instance to this
		     * value for the desktop window; we do this rather than check for
		     * the more general _NET_WM_WINDOW_TYPE_DESKTOP to avoid having
		     * to do another XGetProperty on every iteration. We generally
		     * don't want to count the desktop being focused as application
		     * usage because it frequently can be a false-positive of an
		     * empty workspace.
		     */
		    if (strcmp(list[0], "desktop_window") == 0)
			is_desktop = TRUE;
		    else
			*wm_class = g_strdup(list[1]);
		}

                if (list)
                    g_strfreev(list);
            }
            
            XFree(data);
        }
    }

    if (is_desktop)
	active_window = None;

    if (active_window && title) {
        Atom utf8_string = gdk_x11_get_xatom_by_name_for_display(monitor->display, "UTF8_STRING");
        
        if (XGetWindowProperty (xdisplay, active_window,
                                gdk_x11_get_xatom_by_name_for_display(monitor->display, "_NET_WM_NAME"),
                                0, G_MAXLONG, False, utf8_string,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type == utf8_string)
        {
            if (format == 8 && g_utf8_validate((char *)data, -1, NULL)) {
                *title = g_strdup((char *)data);
            }
            
            XFree(data);
        }
    }

    if (active_window && title && *title == NULL) {
        if (XGetWindowProperty (xdisplay, active_window,
                                XA_WM_NAME,
                                0, G_MAXLONG, False, AnyPropertyType,
                                &type, &format, &n_items, &bytes_after, &data) == Success &&
            type != None)
        {
            if (format == 8) {
                char **list;
                int count;
                
                count = gdk_text_property_to_utf8_list_for_display(monitor->display,
                                                                   gdk_x11_xatom_to_atom_for_display(monitor->display, type),
                                                                   8, data, n_items, &list);

                if (count > 0)
                    *title = g_strdup(list[0]);
                
                if (list)
                    g_strfreev(list);
            }
            
            XFree(data);
        }
    }
        
    gdk_error_trap_pop();
}