2 * (c) 2001 Michal Krause <michal@krause.cz>
5 #include "wmcliphist.h"
7 #define WMCLIPHIST_VERSION "2.1"
15 begin_func("print_help");
17 fprintf(stderr
, "Usage: wmcliphist [options]\n\n"
18 "wmcliphist is small dock applet for Window Maker which "
19 "keeps X clipboard history\n\n");
20 fprintf(stderr
, "-h show this help\n"
22 "-n <num> set number of items to keep (default 10)\n"
23 "-c color set color for locked items (default is red)\n"
24 "-s <size> choose wmcliphist icon size:\n"
26 " 16 = tiny 16x16 px icon\n"
27 " 30 = icon suitable for 32px dock/slit\n"
28 " 40 = icon suitable for 48px dock/slit\n"
29 " 60 = icon suitable for 64px dock/slit (default)\n");
30 fprintf(stderr
, "-i <num> choose wmcliphist icon antialiasing:\n"
31 " 0 = for mid tones background (default)\n"
32 " 1 = for dark background\n"
33 " 2 = for light background\n");
34 fprintf(stderr
, "-d dumps clipboard history to stdout"
35 " in pseudo XML format (no escaping etc.)\n");
36 fprintf(stderr
, "-b <type> choose clipboard to manage\n"
37 " PRIMARY = select copies, middle click pastes (default)\n"
38 " SECONDARY = not used\n"
39 " CLIPBOARD = Ctrl+C copies, Ctrl+V pastes\n\n");
45 wmcliphist_exit(gint code
)
47 begin_func("wmcliphist_exit");
57 main(int argc
, char **argv
)
65 gboolean dump_only
= FALSE
;
70 debug_init_nothreads();
74 /* load configuration */
75 if ((res
= rcconfig_get(rcconfig_get_name("rc"))) != 0) {
76 fprintf(stderr
, "~/.wmcliphistrc parse error (%d)\n", res
);
80 /* parse command line */
81 while ((arg
= argv
[i
])) {
83 if (*(arg
+ 1) == 'h')
85 else if (*(arg
+ 1) == 'n') {
88 fprintf(stderr
, "Missing value of -n\n");
91 num_items_to_keep
= atol(argv
[i
]);
92 } else if (*(arg
+ 1) == 'c') {
95 fprintf(stderr
, "Missing value of -c\n");
98 memset(locked_color_str
, 0, 32);
99 strncpy(locked_color_str
, argv
[i
], 31);
100 } else if (*(arg
+ 1) == 'i') {
103 fprintf(stderr
, "Missing value of -i\n");
106 icon_number
= atoi(argv
[i
]);
107 if (icon_number
< 0 || icon_number
> 2) {
108 fprintf(stderr
, "Invalid value of -i\n");
111 } else if (*(arg
+ 1) == 's') {
114 fprintf(stderr
, "Missing value of -s\n");
117 icon_size
= atoi(argv
[i
]);
118 if (icon_size
!= 60 && icon_size
!= 40
122 fprintf(stderr
, "Invalid value of -s\n");
125 if (icon_size
== 0) {
128 } else if (*(arg
+ 1) == 'd') {
130 } else if (*(arg
+ 1) == 'v') {
131 printf("wmcliphist "WMCLIPHIST_VERSION
"\n");
133 } else if (*(arg
+ 1) == 'b') {
136 fprintf(stderr
, "Missing value of -b\n");
139 memset(clipboard_str
, 0, 32);
140 strncpy(clipboard_str
, argv
[i
], 31);
142 fprintf(stderr
, "Invalid option -%c\n", *(arg
+ 1));
146 fprintf(stderr
, "Invalid option %s\n", arg
);
152 signal(SIGCHLD
, SIG_IGN
);
155 gtk_init(&argc
, &argv
);
157 /* create main window */
158 main_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
160 /* set the window title */
161 gtk_window_set_title(GTK_WINDOW(main_window
), "wmcliphist");
163 /* creat dock icon */
164 dock_app
= main_window
;
166 /* add event box to main window*/
167 event
= gtk_event_box_new();
168 gtk_container_add(GTK_CONTAINER(dock_app
), event
);
170 /* set the default window size */
171 gtk_window_set_default_size(GTK_WINDOW(dock_app
), icon_size
, icon_size
);
174 /* create icon_mask */
175 if (icon_size
== 60) {
177 if (icon_number
== 0) {
178 icon_file
= "ico_60x60_gray.png";
179 } else if (icon_number
== 1) {
180 icon_file
= "ico_60x60_black.png";
182 icon_file
= "ico_60x60_white.png";
184 } else if (icon_size
== 40) {
187 if (icon_number
== 0) {
188 icon_file
= "ico_40x40_gray.png";
189 } else if (icon_number
== 1) {
190 icon_file
= "ico_40x40_black.png";
192 icon_file
= "ico_40x40_white.png";
194 } else if (icon_size
== 30) {
197 if (icon_number
== 0) {
198 icon_file
= "ico_30x30_gray.png";
199 } else if (icon_number
== 1) {
200 icon_file
= "ico_30x30_black.png";
202 icon_file
= "ico_30x30_white.png";
207 icon_file
= "ico_16x16.png";
210 icon_file
= g_strconcat(DATADIR
"/", icon_file
, NULL
);
211 pixmap
= gtk_image_new_from_file(icon_file
);
212 gtk_widget_show(pixmap
);
213 gtk_container_add(GTK_CONTAINER(event
), pixmap
);
216 /* use only mouse events */
217 gtk_widget_add_events(dock_app
, GDK_BUTTON_PRESS_MASK
);
219 gtk_widget_show(dock_app
);
220 gtk_widget_show_all(main_window
);
222 /* transparenter Hintergrund für pixmap*/
224 cairo_region_t
*region
;
225 cairo_surface_t
*surface
;
227 surface
= cairo_image_surface_create_from_png(icon_file
);
228 region
= gdk_cairo_region_create_from_surface(surface
);
229 gdk_window_shape_combine_region(gtk_widget_get_window(dock_app
),
234 /* set the window as a dockapp*/
235 display
= gdk_display_get_default();
237 mywmhints
.icon_window
= GDK_WINDOW_XID(gtk_widget_get_window(main_window
));
238 mywmhints
.window_group
= GDK_WINDOW_XID(gtk_widget_get_window(main_window
));
239 mywmhints
.icon_x
= 0;
240 mywmhints
.icon_y
= 0;
241 mywmhints
.flags
= IconWindowHint
| StateHint
;
242 mywmhints
.initial_state
= WithdrawnState
;
244 XSetWMHints(GDK_DISPLAY_XDISPLAY(display
), GDK_WINDOW_XID(gtk_widget_get_window(main_window
)), &mywmhints
);
246 /* create clipboard history menu */
247 menu_hist
= gtk_menu_new();
248 menu_title
= gtk_menu_item_new();
249 gtk_menu_shell_append(GTK_MENU_SHELL(menu_hist
), menu_title
);
250 gtk_widget_show(menu_title
);
251 gtk_widget_show(menu_hist
);
254 /* create application menu */
255 menu_app
= gtk_menu_new();
257 menu_app_clip_lock
= gtk_check_menu_item_new_with_label("Clipboard lock");
258 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_clip_lock
);
260 menu_app_clip_ignore
= gtk_check_menu_item_new_with_label("Clipboard ignore");
261 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_clip_ignore
);
263 menu_app_save
= gtk_menu_item_new_with_label("Save history");
264 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_save
);
265 g_signal_connect(G_OBJECT(menu_app_save
),
267 G_CALLBACK(menu_app_item_click
),
270 menu_app_exit
= gtk_menu_item_new_with_label("Exit");
271 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_exit
);
272 g_signal_connect(G_OBJECT(menu_app_exit
),
274 G_CALLBACK(menu_app_item_click
),
277 gtk_widget_show_all(menu_app
);
279 list_node
= action_list
;
281 ACTION
*action
= list_node
->data
;
283 if (action
->action
== ACT_SUBMENU
&&
284 strcmp(action
->command
, "-") != 0) {
285 action
->menu_item
= gtk_menu_item_new_with_label(
287 gtk_menu_shell_append(GTK_MENU_SHELL(menu_hist
),
289 action
->submenu
= gtk_menu_new();
290 gtk_menu_item_set_submenu(
291 GTK_MENU_ITEM(action
->menu_item
),
293 menu_title
= gtk_menu_item_new();
294 gtk_menu_shell_append(GTK_MENU_SHELL(action
->submenu
), menu_title
);
295 gtk_widget_show(menu_title
);
296 gtk_widget_show(action
->menu_item
);
297 gtk_widget_show(action
->submenu
);
300 if (action
->action
== ACT_SUBMENU
&&
301 strcmp(action
->command
, "-") == 0) {
302 printf("'%s'\n", action
->command
);
303 action
->submenu
= menu_hist
;
305 list_node
= list_node
->next
;
308 GtkWidget
*separator
;
310 separator
= gtk_menu_item_new();
311 gtk_widget_show(separator
);
312 gtk_menu_shell_insert(GTK_MENU_SHELL(menu_hist
), separator
, 1);
316 /* prepare colors and styles */
317 if (gdk_rgba_parse(&locked_color
, locked_color_str
) == FALSE
) {
320 sprintf(msg_str
, "Invalid color string: '%s'.\n"
321 "Falling back to default (red).",
323 show_message(msg_str
, "Warning", "OK", NULL
, NULL
);
324 strcpy(locked_color_str
, DEF_LOCKED_COLOR
);
325 gdk_rgba_parse(&locked_color
, locked_color_str
);
330 if (strcmp(clipboard_str
, "PRIMARY") == 0) {
331 clipboard
= GDK_SELECTION_PRIMARY
;
332 } else if (strcmp(clipboard_str
, "SECONDARY") == 0) {
333 clipboard
= GDK_SELECTION_SECONDARY
;
334 } else if (strcmp(clipboard_str
, "CLIPBOARD") == 0) {
335 clipboard
= GDK_SELECTION_CLIPBOARD
;
339 sprintf(msg_str
, "Invalid clipboard string: '%s'.\n"
340 "Falling back to default ("
344 show_message(msg_str
, "Warning", "OK", NULL
, NULL
);
345 clipboard
= DEF_CLIPBOARD
;
348 /* load previously saved history */
349 if (history_load(dump_only
) != 0) {
350 if (errno
== E_TOO_MUCH
) {
351 if (show_message("Number of items to keep (-n switch or "
352 "keep directive in ~/.wmcliphistrc)\n"
353 "is lower than actual number of items "
354 "in history file.\nSome items from "
355 "history will be lost. May I continue?",
356 "Warning", "Yes", "No", NULL
) == 1) {
360 } else if (errno
!= E_OPEN
) {
362 fprintf(stderr
, "cannot load history (%d)\n", errno
);
372 /* run clipboard monitor */
373 g_signal_connect(G_OBJECT(main_window
),
374 "selection_received",
375 G_CALLBACK(my_get_xselection
),
377 g_timeout_add(250, time_conv_select
, NULL
);
380 /* run autosave timer */
381 if (autosave_period
> 0)
382 g_timeout_add(autosave_period
* 1000, history_autosave
, NULL
);
385 /* setup everything for supplying selection to other apps */
386 gtk_selection_add_target(dock_app
,
388 GDK_SELECTION_TYPE_STRING
,
391 g_signal_connect(G_OBJECT(dock_app
),
393 G_CALLBACK(selection_handle
),
396 /* connect signal for menu popup */
397 g_signal_connect(G_OBJECT(dock_app
),
399 G_CALLBACK(button_press
),
402 g_signal_connect(G_OBJECT(dock_app
),
404 G_CALLBACK(wmcliphist_exit
),