wmcliphist: Add version 2.0 to repository.
[dockapps.git] / wmcliphist / clipboard.c
blobb39b915f3709e809a25a377b1212427ab94adff5
1 #include <wmcliphist.h>
2 #include <time.h>
4 /* when true, clipboard will be automatically taken up by wmcliphist */
5 gint auto_take_up = 0;
7 /* number of items to keep (may be overriden from command line) */
8 gint num_items_to_keep = 10;
10 /* number if items kept */
11 gint num_items = 0;
13 /* current number of locked items */
14 gint locked_count;
16 /* list of clipboard history items */
17 GList *history_items = NULL;
19 /* selected item */
20 HISTORY_ITEM *selected = NULL;
25 * dumps history list to stderr
26 * for debugging purposes only
28 void
29 dump_history_list_fn(char *header)
31 gint indent = 1, i;
32 GList *node = history_items;
33 HISTORY_ITEM *data;
34 gchar *converted;
36 fprintf(stderr, "%s\n", header);
37 while (node) {
38 data = (HISTORY_ITEM *)node->data;
39 for (i = 0; i < indent; i++)
40 putc('-', stderr);
41 converted = from_utf8(data->content);
42 fprintf(stderr, " %s\n", converted);
43 g_free(converted);
44 indent++;
45 node = node->next;
47 fprintf(stderr, "==========\n");
50 void
51 my_get_selection_text(GtkClipboard *clipboard, const gchar *text, gpointer
52 data)
54 /* previous clipboard content */
55 static gchar *old_content = "";
56 static gint has_old_content = 0;
57 gchar *content;
58 gchar *converted;
60 begin_func("my_get_selection_text");
62 if (text == NULL) {
63 return_void();
66 if (g_utf8_collate(text, old_content) != 0 &&
67 !GTK_CHECK_MENU_ITEM(menu_app_clip_ignore)->active) {
68 /* new data in clipboard */
69 /* store new content for future comparation */
70 content = g_strdup(text);
72 converted = from_utf8(content);
73 /* fprintf(stderr, ">>> %s\n", converted); */
74 g_free(converted);
76 if (has_old_content > 0)
77 g_free(old_content);
78 old_content = content;
79 has_old_content = 1;
81 /* process item */
82 process_item(content, 0, TRUE);
85 /* when clipboard is locked, set selection owener to myself */
86 if (GTK_CHECK_MENU_ITEM(menu_app_clip_ignore)->active ||
87 GTK_CHECK_MENU_ITEM(menu_app_clip_lock)->active) {
88 if (gtk_selection_owner_set(dock_app,
89 GDK_SELECTION_PRIMARY,
90 GDK_CURRENT_TIME) == 0)
91 selected = NULL;
94 return_void();
99 * get clipboard content - partialy inspired by Downloader for X
101 gboolean
102 my_get_xselection(GtkWidget *window, GdkEvent *event)
104 GdkAtom atom;
105 gint format;
106 size_t length;
107 gchar *content = NULL;
109 /* previous clipboard content */
110 static size_t old_content_len = 0;
111 static gchar *old_content = "";
114 begin_func("my_get_xselection");
116 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
117 my_get_selection_text, NULL);
119 return_val(TRUE);
121 length = (size_t) gdk_selection_property_get(window->window,
122 (guchar **) &content, &atom, &format);
124 if (length > 0) {
125 if ((length != old_content_len ||
126 memcmp(content, old_content, length) != 0) &&
127 !GTK_CHECK_MENU_ITEM(menu_app_clip_ignore)->active) {
128 /* new data in clipboard */
129 /* store new content for future comparation */
130 if (old_content_len > 0)
131 g_free(old_content);
132 old_content = content;
133 old_content_len = length;
135 /* process item */
136 /* process_item(content, length, 0, TRUE); */
137 } else {
138 /* no new data */
139 g_free(content);
142 /* when clipboard is locked, set selection owener to myself */
143 if (GTK_CHECK_MENU_ITEM(menu_app_clip_ignore)->active ||
144 GTK_CHECK_MENU_ITEM(menu_app_clip_lock)->active) {
145 if (gtk_selection_owner_set(dock_app,
146 GDK_SELECTION_PRIMARY,
147 GDK_CURRENT_TIME) == 0)
148 selected = NULL;
153 return_val(TRUE);
158 * clipboard conversion - inspired by Downloader for X too :)
160 gboolean
161 time_conv_select()
163 begin_func("time_conv_select");
165 gtk_selection_convert(main_window,
166 GDK_SELECTION_PRIMARY,
167 GDK_TARGET_STRING,
168 GDK_CURRENT_TIME);
169 return_val(TRUE);
174 * handles request for selection from other apps
176 gboolean
177 selection_handle(GtkWidget *widget,
178 GtkSelectionData *selection_data,
179 guint info,
180 guint time_stamp,
181 gpointer data)
183 static gchar *converted = NULL;
185 begin_func("selection_handle");
187 if (converted != NULL) {
188 g_free(converted);
191 if (selected) {
192 converted = from_utf8(selected->content);
193 gtk_selection_data_set(selection_data,
194 GDK_SELECTION_TYPE_STRING,
196 (guchar *) converted,
197 strlen(converted));
198 } else {
199 gtk_selection_data_set(selection_data,
200 GDK_SELECTION_TYPE_STRING,
202 (guchar *)"",
206 return_val(TRUE);