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