wmbiff: `regulo_match` modifies `instructions[i].destination`, so remove `const`.
[dockapps.git] / wmcliphist / wmcliphist.c
blobf86b466a49785c7e065d95f0f851fe2b111df7c0
1 /*
2 * (c) 2001 Michal Krause <michal@krause.cz>
3 */
5 #include "wmcliphist.h"
7 #define WMCLIPHIST_VERSION "2.1"
9 /*
10 * print some help
12 void
13 print_help()
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"
21 "-v print version\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"
25 " 0 = no icon\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");
40 exit(1);
41 return_void();
44 static void
45 wmcliphist_exit(gint code)
47 begin_func("wmcliphist_exit");
48 gtk_main_quit();
49 exit(code);
50 return_void();
54 * main func
56 int
57 main(int argc, char **argv)
59 gint i = 1, res;
60 gchar *arg;
61 gchar *icon_file;
62 GList *list_node;
63 int icon_number = 0;
64 int icon_size = 60;
65 gboolean dump_only = FALSE;
66 XWMHints mywmhints;
67 GdkDisplay *display;
69 #ifdef FNCALL_DEBUG
70 debug_init_nothreads();
71 #endif
72 begin_func("main");
74 /* load configuration */
75 if ((res = rcconfig_get(rcconfig_get_name("rc"))) != 0) {
76 fprintf(stderr, "~/.wmcliphistrc parse error (%d)\n", res);
77 return_val(1);
80 /* parse command line */
81 while ((arg = argv[i])) {
82 if (*arg == '-') {
83 if (*(arg + 1) == 'h')
84 print_help();
85 else if (*(arg + 1) == 'n') {
86 i++;
87 if (!argv[i]) {
88 fprintf(stderr, "Missing value of -n\n");
89 print_help();
91 num_items_to_keep = atol(argv[i]);
92 } else if (*(arg + 1) == 'c') {
93 i++;
94 if (!argv[i]) {
95 fprintf(stderr, "Missing value of -c\n");
96 print_help();
98 memset(locked_color_str, 0, 32);
99 strncpy(locked_color_str, argv[i], 31);
100 } else if (*(arg + 1) == 'i') {
101 i++;
102 if (!argv[i]) {
103 fprintf(stderr, "Missing value of -i\n");
104 print_help();
106 icon_number = atoi(argv[i]);
107 if (icon_number < 0 || icon_number > 2) {
108 fprintf(stderr, "Invalid value of -i\n");
109 print_help();
111 } else if (*(arg + 1) == 's') {
112 i++;
113 if (!argv[i]) {
114 fprintf(stderr, "Missing value of -s\n");
115 print_help();
117 icon_size = atoi(argv[i]);
118 if (icon_size != 60 && icon_size != 40
119 && icon_size != 30
120 && icon_size != 16
121 && icon_size != 0) {
122 fprintf(stderr, "Invalid value of -s\n");
123 print_help();
125 if (icon_size == 0) {
126 icon_size = 1;
128 } else if (*(arg + 1) == 'd') {
129 dump_only = TRUE;
130 } else if (*(arg + 1) == 'v') {
131 printf("wmcliphist "WMCLIPHIST_VERSION"\n");
132 exit(1);
133 } else if (*(arg + 1) == 'b') {
134 i++;
135 if (!argv[i]) {
136 fprintf(stderr, "Missing value of -b\n");
137 print_help();
139 memset(clipboard_str, 0, 32);
140 strncpy(clipboard_str, argv[i], 31);
141 } else {
142 fprintf(stderr, "Invalid option -%c\n", *(arg + 1));
143 print_help();
145 } else {
146 fprintf(stderr, "Invalid option %s\n", arg);
147 print_help();
149 i++;
152 signal(SIGCHLD, SIG_IGN);
154 /* initialize Gtk */
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 if (icon_size) {
174 /* create icon_mask */
175 if (icon_size == 60) {
176 /* 60x60 icon */
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";
181 } else {
182 icon_file = "ico_60x60_white.png";
184 } else if (icon_size == 40) {
185 /* 40x40 icon */
186 /* create icon */
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";
191 } else {
192 icon_file = "ico_40x40_white.png";
194 } else if (icon_size == 30) {
195 /* 30x30 icon */
196 /* create icon */
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";
201 } else {
202 icon_file = "ico_30x30_white.png";
204 } else {
205 /* 16x16 icon */
206 /* create icon */
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*/
223 if (icon_size) {
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),
230 region, 0, 0);
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),
266 "activate",
267 G_CALLBACK(menu_app_item_click),
268 GINT_TO_POINTER(0));
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),
273 "activate",
274 G_CALLBACK(menu_app_item_click),
275 GINT_TO_POINTER(1));
277 gtk_widget_show_all(menu_app);
279 list_node = action_list;
280 while (list_node) {
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(
286 action->command);
287 gtk_menu_shell_append(GTK_MENU_SHELL(menu_hist),
288 action->menu_item);
289 action->submenu = gtk_menu_new();
290 gtk_menu_item_set_submenu(
291 GTK_MENU_ITEM(action->menu_item),
292 action->submenu);
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);
298 submenu_count++;
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;
307 if (submenu_count) {
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) {
318 char msg_str[128];
320 sprintf(msg_str, "Invalid color string: '%s'.\n"
321 "Falling back to default (red).",
322 locked_color_str);
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);
329 /* set clipboard */
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;
336 } else {
337 char msg_str[128];
339 sprintf(msg_str, "Invalid clipboard string: '%s'.\n"
340 "Falling back to default ("
341 DEF_CLIPBOARD_STR
342 ").",
343 clipboard_str);
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) {
357 rcconfig_free();
358 exit(1);
360 } else if (errno != E_OPEN) {
361 rcconfig_free();
362 fprintf(stderr, "cannot load history (%d)\n", errno);
363 exit(1);
366 if (dump_only) {
367 rcconfig_free();
368 exit(1);
372 /* run clipboard monitor */
373 g_signal_connect(G_OBJECT(main_window),
374 "selection_received",
375 G_CALLBACK(my_get_xselection),
376 NULL);
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,
387 clipboard,
388 GDK_SELECTION_TYPE_STRING,
391 g_signal_connect(G_OBJECT(dock_app),
392 "selection_get",
393 G_CALLBACK(selection_handle),
394 NULL);
396 /* connect signal for menu popup */
397 g_signal_connect(G_OBJECT(dock_app),
398 "event",
399 G_CALLBACK(button_press),
400 NULL);
402 g_signal_connect(G_OBJECT(dock_app),
403 "destroy",
404 G_CALLBACK(wmcliphist_exit),
405 NULL);
408 hotkeys_init();
410 gtk_main();
412 return_val(0);