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"); |
47 gtk_selection_data_free(data); | |
43 continue; | 48 continue; |
Evan Stade
2013/05/10 17:55:37
remove L47-48
| |
44 } else if (strstr(gdk_atom_name(targets[i]), "TIMESTAMP")) { | 49 } else if (strstr(target_name.c_str(), "TIMESTAMP")) { |
45 // TODO(estade): Print the time stamp in human readable format. | 50 // TODO(estade): Print the time stamp in human readable format. |
46 printf("(time omitted)\n\n"); | 51 printf("(time omitted)\n\n"); |
52 gtk_selection_data_free(data); | |
47 continue; | 53 continue; |
Evan Stade
2013/05/10 17:55:37
remove L52-53
| |
54 } else { | |
55 for (int j = 0; j < data->length; j++) { | |
56 // Output data one byte at a time. Currently wide strings look | |
57 // pretty weird. | |
58 printf("%c", (data->data[j] == 0 ? '_' : data->data[j])); | |
59 } | |
60 printf("\n\n"); | |
61 gtk_selection_data_free(data); | |
Evan Stade
2013/05/10 17:55:37
pull this out
| |
48 } | 62 } |
49 | |
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 } | 63 } |
57 | 64 |
58 if (num_targets <= 0) { | 65 if (num_targets <= 0) { |
59 printf("No targets advertised. Text is: "); | 66 printf("No targets advertised. Text is: "); |
60 gchar* text = gtk_clipboard_wait_for_text(clip); | 67 gchar* text = gtk_clipboard_wait_for_text(clip); |
61 printf("%s\n", text ? text : "NULL"); | 68 printf("%s\n", text ? text : "NULL"); |
62 g_free(text); | 69 g_free(text); |
63 } | 70 } |
64 | 71 |
65 g_free(targets); | 72 g_free(targets); |
73 gtk_selection_data_free(target_data); | |
66 } | 74 } |
67 | 75 |
68 } | 76 } |
69 | 77 |
70 /* Small program to dump the contents of GTK's clipboards to the terminal. | 78 /* 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. | 79 * Feel free to add to it or improve formatting or whatnot. |
72 */ | 80 */ |
73 int main(int argc, char* argv[]) { | 81 int main(int argc, char* argv[]) { |
74 gtk_init(&argc, &argv); | 82 gtk_init(&argc, &argv); |
75 | 83 |
76 printf("Desktop clipboard\n"); | 84 printf("Desktop clipboard\n"); |
77 PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); | 85 PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); |
78 | 86 |
79 printf("X clipboard\n"); | 87 printf("X clipboard\n"); |
80 PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_PRIMARY)); | 88 PrintClipboardContents(gtk_clipboard_get(GDK_SELECTION_PRIMARY)); |
81 } | 89 } |
OLD | NEW |