1 #include <wmcliphist.h>
2 #include <time.h>
3
4 /* when true, clipboard will be automatically taken up by wmcliphist */
5 gint auto_take_up = 0;
6
7 /* number of items to keep (may be overriden from command line) */
8 gint num_items_to_keep = 10;
9
10 /* number if items kept */
11 gint num_items = 0;
12
13 /* current number of locked items */
14 gint locked_count;
15
16 /* list of clipboard history items */
17 GList *history_items = NULL;
18
19 /* selected item */
20 HISTORY_ITEM *selected = NULL;
21
22
23
24 /*
25 * dumps history list to stderr
26 * for debugging purposes only
27 */
28 void
dump_history_list_fn(char * header)29 dump_history_list_fn(char *header)
30 {
31 gint indent = 1, i;
32 GList *node = history_items;
33 HISTORY_ITEM *data;
34 gchar *converted;
35
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;
46 }
47 fprintf(stderr, "==========\n");
48 }
49
50 void
my_get_selection_text(GtkClipboard * clipboard,const gchar * text,gpointer data)51 my_get_selection_text(GtkClipboard *clipboard, const gchar *text, gpointer
52 data)
53 {
54 /* previous clipboard content */
55 static gchar *old_content = "";
56 static gint has_old_content = 0;
57 gchar *content;
58 gchar *converted;
59
60 begin_func("my_get_selection_text");
61
62 if (text == NULL) {
63 return_void();
64 }
65
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);
71
72 converted = from_utf8(content);
73 /* fprintf(stderr, ">>> %s\n", converted); */
74 g_free(converted);
75
76 if (has_old_content > 0)
77 g_free(old_content);
78 old_content = content;
79 has_old_content = 1;
80
81 /* process item */
82 process_item(content, 0, TRUE);
83 }
84
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;
92 }
93
94 return_void();
95 }
96
97
98 /*
99 * get clipboard content - partialy inspired by Downloader for X
100 */
101 gboolean
my_get_xselection(GtkWidget * window,GdkEvent * event)102 my_get_xselection(GtkWidget *window, GdkEvent *event)
103 {
104 GdkAtom atom;
105 gint format;
106 size_t length;
107 gchar *content = NULL;
108
109 /* previous clipboard content */
110 static size_t old_content_len = 0;
111 static gchar *old_content = "";
112
113
114 begin_func("my_get_xselection");
115
116 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
117 my_get_selection_text, NULL);
118
119 return_val(TRUE);
120
121 length = (size_t) gdk_selection_property_get(window->window,
122 (guchar **) &content, &atom, &format);
123
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;
134
135 /* process item */
136 /* process_item(content, length, 0, TRUE); */
137 } else {
138 /* no new data */
139 g_free(content);
140 }
141
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;
149 }
150
151 }
152
153 return_val(TRUE);
154 }
155
156
157 /*
158 * clipboard conversion - inspired by Downloader for X too :)
159 */
160 gboolean
time_conv_select()161 time_conv_select()
162 {
163 begin_func("time_conv_select");
164
165 gtk_selection_convert(main_window,
166 GDK_SELECTION_PRIMARY,
167 GDK_TARGET_STRING,
168 GDK_CURRENT_TIME);
169 return_val(TRUE);
170 }
171
172
173 /*
174 * handles request for selection from other apps
175 */
176 gboolean
selection_handle(GtkWidget * widget,GtkSelectionData * selection_data,guint info,guint time_stamp,gpointer data)177 selection_handle(GtkWidget *widget,
178 GtkSelectionData *selection_data,
179 guint info,
180 guint time_stamp,
181 gpointer data)
182 {
183 static gchar *converted = NULL;
184
185 begin_func("selection_handle");
186
187 if (converted != NULL) {
188 g_free(converted);
189 }
190
191 if (selected) {
192 converted = from_utf8(selected->content);
193 gtk_selection_data_set(selection_data,
194 GDK_SELECTION_TYPE_STRING,
195 8,
196 (guchar *) converted,
197 strlen(converted));
198 } else {
199 gtk_selection_data_set(selection_data,
200 GDK_SELECTION_TYPE_STRING,
201 8,
202 (guchar *)"",
203 0);
204 }
205
206 return_val(TRUE);
207 }
208