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
);
173 /* set default icon */
174 icon_file
= "ico_60x060_gray.png";
177 /* create icon_mask */
178 if (icon_size
== 60) {
180 if (icon_number
== 0) {
181 icon_file
= "ico_60x60_gray.png";
182 } else if (icon_number
== 1) {
183 icon_file
= "ico_60x60_black.png";
185 icon_file
= "ico_60x60_white.png";
187 } else if (icon_size
== 40) {
190 if (icon_number
== 0) {
191 icon_file
= "ico_40x40_gray.png";
192 } else if (icon_number
== 1) {
193 icon_file
= "ico_40x40_black.png";
195 icon_file
= "ico_40x40_white.png";
197 } else if (icon_size
== 30) {
200 if (icon_number
== 0) {
201 icon_file
= "ico_30x30_gray.png";
202 } else if (icon_number
== 1) {
203 icon_file
= "ico_30x30_black.png";
205 icon_file
= "ico_30x30_white.png";
210 icon_file
= "ico_16x16.png";
213 icon_file
= g_strconcat(DATADIR
"/", icon_file
, NULL
);
214 pixmap
= gtk_image_new_from_file(icon_file
);
215 gtk_widget_show(pixmap
);
216 gtk_container_add(GTK_CONTAINER(event
), pixmap
);
219 /* use only mouse events */
220 gtk_widget_add_events(dock_app
, GDK_BUTTON_PRESS_MASK
);
222 gtk_widget_show(dock_app
);
223 gtk_widget_show_all(main_window
);
225 /* transparenter Hintergrund für pixmap*/
227 cairo_region_t
*region
;
228 cairo_surface_t
*surface
;
230 surface
= cairo_image_surface_create_from_png(icon_file
);
231 region
= gdk_cairo_region_create_from_surface(surface
);
232 gdk_window_shape_combine_region(gtk_widget_get_window(dock_app
),
237 /* set the window as a dockapp*/
238 display
= gdk_display_get_default();
240 mywmhints
.icon_window
= GDK_WINDOW_XID(gtk_widget_get_window(main_window
));
241 mywmhints
.window_group
= GDK_WINDOW_XID(gtk_widget_get_window(main_window
));
242 mywmhints
.icon_x
= 0;
243 mywmhints
.icon_y
= 0;
244 mywmhints
.flags
= IconWindowHint
| StateHint
;
245 mywmhints
.initial_state
= WithdrawnState
;
247 XSetWMHints(GDK_DISPLAY_XDISPLAY(display
), GDK_WINDOW_XID(gtk_widget_get_window(main_window
)), &mywmhints
);
249 /* create clipboard history menu */
250 menu_hist
= gtk_menu_new();
251 menu_title
= gtk_menu_item_new();
252 gtk_menu_shell_append(GTK_MENU_SHELL(menu_hist
), menu_title
);
253 gtk_widget_show(menu_title
);
254 gtk_widget_show(menu_hist
);
257 /* create application menu */
258 menu_app
= gtk_menu_new();
260 menu_app_clip_lock
= gtk_check_menu_item_new_with_label("Clipboard lock");
261 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_clip_lock
);
263 menu_app_clip_ignore
= gtk_check_menu_item_new_with_label("Clipboard ignore");
264 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_clip_ignore
);
266 menu_app_save
= gtk_menu_item_new_with_label("Save history");
267 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_save
);
268 g_signal_connect(G_OBJECT(menu_app_save
),
270 G_CALLBACK(menu_app_item_click
),
273 menu_app_exit
= gtk_menu_item_new_with_label("Exit");
274 gtk_menu_shell_append(GTK_MENU_SHELL(menu_app
), menu_app_exit
);
275 g_signal_connect(G_OBJECT(menu_app_exit
),
277 G_CALLBACK(menu_app_item_click
),
280 gtk_widget_show_all(menu_app
);
282 list_node
= action_list
;
284 ACTION
*action
= list_node
->data
;
286 if (action
->action
== ACT_SUBMENU
&&
287 strcmp(action
->command
, "-") != 0) {
288 action
->menu_item
= gtk_menu_item_new_with_label(
290 gtk_menu_shell_append(GTK_MENU_SHELL(menu_hist
),
292 action
->submenu
= gtk_menu_new();
293 gtk_menu_item_set_submenu(
294 GTK_MENU_ITEM(action
->menu_item
),
296 menu_title
= gtk_menu_item_new();
297 gtk_menu_shell_append(GTK_MENU_SHELL(action
->submenu
), menu_title
);
298 gtk_widget_show(menu_title
);
299 gtk_widget_show(action
->menu_item
);
300 gtk_widget_show(action
->submenu
);
303 if (action
->action
== ACT_SUBMENU
&&
304 strcmp(action
->command
, "-") == 0) {
305 printf("'%s'\n", action
->command
);
306 action
->submenu
= menu_hist
;
308 list_node
= list_node
->next
;
311 GtkWidget
*separator
;
313 separator
= gtk_menu_item_new();
314 gtk_widget_show(separator
);
315 gtk_menu_shell_insert(GTK_MENU_SHELL(menu_hist
), separator
, 1);
319 /* prepare colors and styles */
320 if (gdk_rgba_parse(&locked_color
, locked_color_str
) == FALSE
) {
323 sprintf(msg_str
, "Invalid color string: '%s'.\n"
324 "Falling back to default (red).",
326 show_message(msg_str
, "Warning", "OK", NULL
, NULL
);
327 strcpy(locked_color_str
, DEF_LOCKED_COLOR
);
328 gdk_rgba_parse(&locked_color
, locked_color_str
);
333 if (strcmp(clipboard_str
, "PRIMARY") == 0) {
334 clipboard
= GDK_SELECTION_PRIMARY
;
335 } else if (strcmp(clipboard_str
, "SECONDARY") == 0) {
336 clipboard
= GDK_SELECTION_SECONDARY
;
337 } else if (strcmp(clipboard_str
, "CLIPBOARD") == 0) {
338 clipboard
= GDK_SELECTION_CLIPBOARD
;
342 sprintf(msg_str
, "Invalid clipboard string: '%s'.\n"
343 "Falling back to default ("
347 show_message(msg_str
, "Warning", "OK", NULL
, NULL
);
348 clipboard
= DEF_CLIPBOARD
;
351 /* load previously saved history */
352 if (history_load(dump_only
) != 0) {
353 if (errno
== E_TOO_MUCH
) {
354 if (show_message("Number of items to keep (-n switch or "
355 "keep directive in ~/.wmcliphistrc)\n"
356 "is lower than actual number of items "
357 "in history file.\nSome items from "
358 "history will be lost. May I continue?",
359 "Warning", "Yes", "No", NULL
) == 1) {
363 } else if (errno
!= E_OPEN
) {
365 fprintf(stderr
, "cannot load history (%d)\n", errno
);
375 /* run clipboard monitor */
376 g_signal_connect(G_OBJECT(main_window
),
377 "selection_received",
378 G_CALLBACK(my_get_xselection
),
380 g_timeout_add(250, time_conv_select
, NULL
);
383 /* run autosave timer */
384 if (autosave_period
> 0)
385 g_timeout_add(autosave_period
* 1000, history_autosave
, NULL
);
388 /* setup everything for supplying selection to other apps */
389 gtk_selection_add_target(dock_app
,
391 GDK_SELECTION_TYPE_STRING
,
394 g_signal_connect(G_OBJECT(dock_app
),
396 G_CALLBACK(selection_handle
),
399 /* connect signal for menu popup */
400 g_signal_connect(G_OBJECT(dock_app
),
402 G_CALLBACK(button_press
),
405 g_signal_connect(G_OBJECT(dock_app
),
407 G_CALLBACK(wmcliphist_exit
),