Home | History | Annotate | Download | only in gtk_clipboard_dump
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <gtk/gtk.h>
      6 #include <stdio.h>
      7 #include <string.h>
      8 #include <string>
      9 
     10 namespace {
     11 
     12 void PrintClipboardContents(GtkClipboard* clip) {
     13   GdkAtom* targets;
     14   int num_targets = 0;
     15 
     16   // This call is bugged, the cache it checks is often stale; see
     17   // <http://bugzilla.gnome.org/show_bug.cgi?id=557315>.
     18   // gtk_clipboard_wait_for_targets(clip, &targets, &num_targets);
     19 
     20   GtkSelectionData* target_data =
     21       gtk_clipboard_wait_for_contents(clip,
     22                                       gdk_atom_intern("TARGETS", false));
     23   if (!target_data) {
     24     printf("failed to get the contents!\n");
     25     return;
     26   }
     27 
     28   gtk_selection_data_get_targets(target_data, &targets, &num_targets);
     29 
     30   printf("%d available targets:\n---------------\n", num_targets);
     31 
     32   for (int i = 0; i < num_targets; i++) {
     33     gchar* target_name_cstr = gdk_atom_name(targets[i]);
     34     std::string target_name(target_name_cstr);
     35     g_free(target_name_cstr);
     36     printf("  [format: %s", target_name.c_str());
     37     GtkSelectionData* data = gtk_clipboard_wait_for_contents(clip, targets[i]);
     38     if (!data) {
     39       printf("]: NULL\n\n");
     40       continue;
     41     }
     42 
     43     printf(" / length: %d / bits %d]: ", data->length, data->format);
     44 
     45     if (strstr(target_name.c_str(), "image")) {
     46       printf("(image omitted)\n\n");
     47     } else if (strstr(target_name.c_str(), "TIMESTAMP")) {
     48       // TODO(estade): Print the time stamp in human readable format.
     49       printf("(time omitted)\n\n");
     50     } else {
     51       for (int j = 0; j < data->length; j++) {
     52         // Output data one byte at a time. Currently wide strings look
     53         // pretty weird.
     54         printf("%c", (data->data[j] == 0 ? '_' : data->data[j]));
     55       }
     56       printf("\n\n");
     57     }
     58     gtk_selection_data_free(data);
     59   }
     60 
     61   if (num_targets <= 0) {
     62     printf("No targets advertised. Text is: ");
     63     gchar* text = gtk_clipboard_wait_for_text(clip);
     64     printf("%s\n", text ? text : "NULL");
     65     g_free(text);
     66   }
     67 
     68   g_free(targets);
     69   gtk_selection_data_free(target_data);
     70 }
     71 
     72 }
     73 
     74 /* Small program to dump the contents of GTK's clipboards to the terminal.
     75  * Feel free to add to it or improve formatting or whatnot.
     76  */
     77 int main(int argc, char* argv[]) {
     78   gtk_init(&argc, &argv);
     79 
     80   printf("Desktop clipboard\n");
     81   PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
     82 
     83   printf("X clipboard\n");
     84   PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_PRIMARY));
     85 }
     86