]> git.armaanb.net Git - chorizo.git/blob - browser.c
Introduce user-supplied JavaScript files
[chorizo.git] / browser.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8
9 #include <gtk/gtk.h>
10 #include <gtk/gtkx.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <gio/gio.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
15
16
17 static void client_destroy(GtkWidget *, gpointer);
18 static gboolean client_destroy_request(WebKitWebView *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21                                          gpointer);
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29                               WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
48                               WebKitHitTestResult *, gpointer);
49 static gboolean quit_if_nothing_active(void);
50 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 static void run_user_scripts(WebKitWebView *);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
54 static Window tabbed_launch(void);
55 static void trust_user_certs(WebKitWebContext *);
56
57
58 struct Client
59 {
60     gchar *external_handler_uri;
61     gchar *hover_uri;
62     gchar *feed_html;
63     GtkWidget *location;
64     GtkWidget *vbox;
65     GtkWidget *web_view;
66     GtkWidget *win;
67 };
68
69 struct DownloadManager
70 {
71     GtkWidget *scroll;
72     GtkWidget *toolbar;
73     GtkWidget *win;
74 } dm;
75
76
77 static const gchar *accepted_language[2] = { NULL, NULL };
78 static gint clients = 0, downloads = 0;
79 static gboolean cooperative_alone = TRUE;
80 static gboolean cooperative_instances = TRUE;
81 static int cooperative_pipe_fp = 0;
82 static gchar *download_dir = "/var/tmp";
83 static gboolean enable_webgl = FALSE;
84 static gboolean enable_console_to_stdout = FALSE;
85 static Window embed = 0;
86 static gchar *fifo_suffix = "main";
87 static gdouble global_zoom = 1.0;
88 static gchar *history_file = NULL;
89 static gchar *home_uri = "about:blank";
90 static gboolean initial_wc_setup_done = FALSE;
91 static GHashTable *keywords = NULL;
92 static gchar *search_text = NULL;
93 static gboolean tabbed_automagic = TRUE;
94 static gchar *user_agent = NULL;
95
96
97 void
98 client_destroy(GtkWidget *widget, gpointer data)
99 {
100     struct Client *c = (struct Client *)data;
101
102     g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
103                                          changed_load_progress, c);
104
105     free(c);
106     clients--;
107
108     quit_if_nothing_active();
109 }
110
111 gboolean
112 client_destroy_request(WebKitWebView *web_view, gpointer data)
113 {
114     struct Client *c = (struct Client *)data;
115
116     gtk_widget_destroy(c->win);
117
118     return TRUE;
119 }
120
121 WebKitWebView *
122 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
123 {
124     struct Client *c;
125     WebKitWebContext *wc;
126     gchar *f;
127
128     if (uri != NULL && cooperative_instances && !cooperative_alone)
129     {
130         f = ensure_uri_scheme(uri);
131         write(cooperative_pipe_fp, f, strlen(f));
132         write(cooperative_pipe_fp, "\n", 1);
133         g_free(f);
134         return NULL;
135     }
136
137     c = calloc(1, sizeof(struct Client));
138     if (!c)
139     {
140         fprintf(stderr, __NAME__": fatal: calloc failed\n");
141         exit(EXIT_FAILURE);
142     }
143
144     if (embed != 0)
145     {
146         c->win = gtk_plug_new(embed);
147         if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
148         {
149             fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
150             gtk_widget_destroy(c->win);
151             c->win = NULL;
152             embed = 0;
153         }
154     }
155
156     if (c->win == NULL)
157         c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158
159     gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
160
161     g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
162     gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
163
164     if (related_wv == NULL)
165         c->web_view = webkit_web_view_new();
166     else
167         c->web_view = webkit_web_view_new_with_related_view(related_wv);
168     wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
169
170     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
171     g_signal_connect(G_OBJECT(c->web_view), "notify::title",
172                      G_CALLBACK(changed_title), c);
173     g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
174                      G_CALLBACK(changed_uri), c);
175     g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
176                      G_CALLBACK(changed_load_progress), c);
177     g_signal_connect(G_OBJECT(c->web_view), "create",
178                      G_CALLBACK(client_new_request), NULL);
179     g_signal_connect(G_OBJECT(c->web_view), "context-menu",
180                      G_CALLBACK(menu_web_view), c);
181     g_signal_connect(G_OBJECT(c->web_view), "close",
182                      G_CALLBACK(client_destroy_request), c);
183     g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
184                      G_CALLBACK(decide_policy), NULL);
185     g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
186                      G_CALLBACK(key_web_view), c);
187     g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
188                      G_CALLBACK(key_web_view), c);
189     g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
190                      G_CALLBACK(key_web_view), c);
191     g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
192                      G_CALLBACK(hover_web_view), c);
193     g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
194                      G_CALLBACK(crashed_web_view), c);
195
196     if (!initial_wc_setup_done)
197     {
198         if (accepted_language[0] != NULL)
199             webkit_web_context_set_preferred_languages(wc, accepted_language);
200
201         g_signal_connect(G_OBJECT(wc), "download-started",
202                          G_CALLBACK(download_handle_start), NULL);
203
204         trust_user_certs(wc);
205
206         initial_wc_setup_done = TRUE;
207     }
208
209     if (user_agent != NULL)
210         g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
211                      "user-agent", user_agent, NULL);
212
213     if (enable_console_to_stdout)
214         webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
215
216     if (enable_webgl)
217         webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
218
219     c->location = gtk_entry_new();
220     g_signal_connect(G_OBJECT(c->location), "key-press-event",
221                      G_CALLBACK(key_location), c);
222     g_signal_connect(G_OBJECT(c->location), "icon-release",
223                      G_CALLBACK(icon_location), c);
224     /* XXX This is a workaround. Setting this to NULL (which is done in
225      * grab_feeds_finished() if no feed has been detected) adds a little
226      * padding left of the text. Not sure why. The point of this call
227      * right here is to have that padding right from the start. This
228      * avoids a graphical artifact. */
229     gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
230                                       GTK_ENTRY_ICON_PRIMARY,
231                                       NULL);
232
233     c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
234     gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
235     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
236
237     gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
238
239     if (show)
240         show_web_view(NULL, c);
241     else
242         g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
243                          G_CALLBACK(show_web_view), c);
244
245     if (uri != NULL)
246     {
247         f = ensure_uri_scheme(uri);
248         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
249         g_free(f);
250     }
251
252     clients++;
253
254     return WEBKIT_WEB_VIEW(c->web_view);
255 }
256
257 WebKitWebView *
258 client_new_request(WebKitWebView *web_view,
259                    WebKitNavigationAction *navigation_action, gpointer data)
260 {
261     return client_new(NULL, web_view, FALSE);
262 }
263
264 void
265 cooperation_setup(void)
266 {
267     GIOChannel *towatch;
268     gchar *fifofilename, *fifopath;
269
270     fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
271     fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
272     g_free(fifofilename);
273
274     if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
275         mkfifo(fifopath, 0600);
276
277     cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
278     if (!cooperative_pipe_fp)
279     {
280         fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
281     }
282     else
283     {
284         if (write(cooperative_pipe_fp, "", 0) == -1)
285         {
286             /* Could not do an empty write to the FIFO which means there's
287              * no one listening. */
288             close(cooperative_pipe_fp);
289             towatch = g_io_channel_new_file(fifopath, "r+", NULL);
290             g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
291         }
292         else
293             cooperative_alone = FALSE;
294     }
295
296     g_free(fifopath);
297 }
298
299 void
300 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
301 {
302     WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
303     WebKitURIResponse *resp;
304     GtkToolItem *tb = GTK_TOOL_ITEM(data);
305     gdouble p, size_mb;
306     const gchar *uri;
307     gchar *t, *filename, *base;
308
309     p = webkit_download_get_estimated_progress(download);
310     p = p > 1 ? 1 : p;
311     p = p < 0 ? 0 : p;
312     p *= 100;
313     resp = webkit_download_get_response(download);
314     size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
315
316     uri = webkit_download_get_destination(download);
317     filename = g_filename_from_uri(uri, NULL, NULL);
318     if (filename == NULL)
319     {
320         /* This really should not happen because WebKit uses that URI to
321          * write to a file... */
322         fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
323         t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
324                             webkit_uri_response_get_uri(resp), p, size_mb);
325     }
326     else
327     {
328         base = g_path_get_basename(filename);
329         t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
330         g_free(filename);
331         g_free(base);
332     }
333     gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
334     g_free(t);
335 }
336
337 void
338 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
339 {
340     struct Client *c = (struct Client *)data;
341     gdouble p;
342     gchar *grab_feeds =
343         "a = document.querySelectorAll('"
344         "    html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
345         "    html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
346         "');"
347         "if (a.length == 0)"
348         "    null;"
349         "else"
350         "{"
351         "    out = '';"
352         "    for (i = 0; i < a.length; i++)"
353         "    {"
354         "        url = encodeURIComponent(a[i].href);"
355         "        if ('title' in a[i] && a[i].title != '')"
356         "            title = encodeURIComponent(a[i].title);"
357         "        else"
358         "            title = url;"
359         "        out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
360         "    }"
361         "    out;"
362         "}";
363
364     p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
365     if (p == 1)
366     {
367         p = 0;
368
369         /* The page has loaded fully. We now run the short JavaScript
370          * snippet above that operates on the DOM. It tries to grab all
371          * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
372          * references. */
373         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
374                                        grab_feeds, NULL,
375                                        grab_feeds_finished, c);
376
377         run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
378     }
379     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
380 }
381
382 void
383 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
384 {
385     const gchar *t, *u;
386     struct Client *c = (struct Client *)data;
387
388     u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
389     t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
390
391     u = u == NULL ? __NAME__ : u;
392     u = u[0] == 0 ? __NAME__ : u;
393
394     t = t == NULL ? u : t;
395     t = t[0] == 0 ? u : t;
396
397     gtk_window_set_title(GTK_WINDOW(c->win), t);
398 }
399
400 void
401 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
402 {
403     const gchar *t;
404     struct Client *c = (struct Client *)data;
405     FILE *fp;
406
407     t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
408
409     /* When a web process crashes, we get a "notify::uri" signal, but we
410      * can no longer read a meaningful URI. It's just an empty string
411      * now. Not updating the location bar in this scenario is important,
412      * because we would override the "WEB PROCESS CRASHED" message. */
413     if (t != NULL && strlen(t) > 0)
414     {
415         gtk_entry_set_text(GTK_ENTRY(c->location), t);
416
417         if (history_file != NULL)
418         {
419             fp = fopen(history_file, "a");
420             if (fp != NULL)
421             {
422                 fprintf(fp, "%s\n", t);
423                 fclose(fp);
424             }
425             else
426                 perror(__NAME__": Error opening history file");
427         }
428     }
429 }
430
431 gboolean
432 crashed_web_view(WebKitWebView *web_view, gpointer data)
433 {
434     gchar *t;
435     struct Client *c = (struct Client *)data;
436
437     t = g_strdup_printf("WEB PROCESS CRASHED: %s",
438                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
439     gtk_entry_set_text(GTK_ENTRY(c->location), t);
440     g_free(t);
441
442     return TRUE;
443 }
444
445 gboolean
446 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
447               WebKitPolicyDecisionType type, gpointer data)
448 {
449     WebKitResponsePolicyDecision *r;
450
451     switch (type)
452     {
453         case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
454             r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
455             if (!webkit_response_policy_decision_is_mime_type_supported(r))
456                 webkit_policy_decision_download(decision);
457             else
458                 webkit_policy_decision_use(decision);
459             break;
460         default:
461             /* Use whatever default there is. */
462             return FALSE;
463     }
464     return TRUE;
465 }
466
467 void
468 download_handle_finished(WebKitDownload *download, gpointer data)
469 {
470     downloads--;
471 }
472
473 void
474 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
475                       gpointer data)
476 {
477     g_signal_connect(G_OBJECT(download), "decide-destination",
478                      G_CALLBACK(download_handle), data);
479 }
480
481 gboolean
482 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
483 {
484     gchar *sug_clean, *path, *path2 = NULL, *uri;
485     GtkToolItem *tb;
486     int suffix = 1;
487     size_t i;
488
489     sug_clean = g_strdup(suggested_filename);
490     for (i = 0; i < strlen(sug_clean); i++)
491         if (sug_clean[i] == G_DIR_SEPARATOR)
492             sug_clean[i] = '_';
493
494     path = g_build_filename(download_dir, sug_clean, NULL);
495     path2 = g_strdup(path);
496     while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
497     {
498         g_free(path2);
499
500         path2 = g_strdup_printf("%s.%d", path, suffix);
501         suffix++;
502     }
503
504     if (suffix == 1000)
505     {
506         fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
507         webkit_download_cancel(download);
508     }
509     else
510     {
511         uri = g_filename_to_uri(path2, NULL, NULL);
512         webkit_download_set_destination(download, uri);
513         g_free(uri);
514
515         tb = gtk_tool_button_new(NULL, NULL);
516         gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
517         gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
518         gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
519         gtk_widget_show_all(dm.win);
520
521         g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
522                          G_CALLBACK(changed_download_progress), tb);
523
524         downloads++;
525         g_signal_connect(G_OBJECT(download), "finished",
526                          G_CALLBACK(download_handle_finished), NULL);
527
528         g_object_ref(download);
529         g_signal_connect(G_OBJECT(tb), "clicked",
530                          G_CALLBACK(downloadmanager_cancel), download);
531     }
532
533     g_free(sug_clean);
534     g_free(path);
535     g_free(path2);
536
537     /* Propagate -- to whom it may concern. */
538     return FALSE;
539 }
540
541 void
542 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
543 {
544     WebKitDownload *download = WEBKIT_DOWNLOAD(data);
545
546     webkit_download_cancel(download);
547     g_object_unref(download);
548
549     gtk_widget_destroy(GTK_WIDGET(tb));
550 }
551
552 gboolean
553 downloadmanager_delete(GtkWidget *obj, gpointer data)
554 {
555     if (!quit_if_nothing_active())
556         gtk_widget_hide(dm.win);
557
558     return TRUE;
559 }
560
561 void
562 downloadmanager_setup(void)
563 {
564     dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
565     gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
566     gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
567     gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
568     g_signal_connect(G_OBJECT(dm.win), "delete-event",
569                      G_CALLBACK(downloadmanager_delete), NULL);
570     g_signal_connect(G_OBJECT(dm.win), "key-press-event",
571                      G_CALLBACK(key_downloadmanager), NULL);
572
573     dm.toolbar = gtk_toolbar_new();
574     gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
575                                    GTK_ORIENTATION_VERTICAL);
576     gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
577     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
578
579     dm.scroll = gtk_scrolled_window_new(NULL, NULL);
580     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
581                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
582     gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
583
584     gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
585 }
586
587 gchar *
588 ensure_uri_scheme(const gchar *t)
589 {
590     gchar *f, *fabs;
591
592     f = g_ascii_strdown(t, -1);
593     if (!g_str_has_prefix(f, "http:") &&
594         !g_str_has_prefix(f, "https:") &&
595         !g_str_has_prefix(f, "file:") &&
596         !g_str_has_prefix(f, "about:") &&
597         !g_str_has_prefix(f, "data:"))
598     {
599         g_free(f);
600         fabs = realpath(t, NULL);
601         if (fabs != NULL)
602         {
603             f = g_strdup_printf("file://%s", fabs);
604             free(fabs);
605         }
606         else
607             f = g_strdup_printf("http://%s", t);
608         return f;
609     }
610     else
611         return g_strdup(t);
612 }
613
614 void
615 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
616 {
617     struct Client *c = (struct Client *)data;
618     gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
619     GPid pid;
620     GError *err = NULL;
621
622     (void)simple;
623     (void)param;
624
625     argv[2] = c->external_handler_uri;
626     if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
627                        &pid, &err))
628     {
629         fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
630                 err->message);
631         g_error_free(err);
632     }
633     else
634         g_spawn_close_pid(pid);
635 }
636
637 void
638 grab_environment_configuration(void)
639 {
640     const gchar *e;
641
642     e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
643     if (e != NULL)
644         accepted_language[0] = g_strdup(e);
645
646     e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
647     if (e != NULL)
648         download_dir = g_strdup(e);
649
650     e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
651     if (e != NULL)
652         enable_console_to_stdout = TRUE;
653
654     e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
655     if (e != NULL)
656         enable_webgl = TRUE;
657
658     e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
659     if (e != NULL)
660         fifo_suffix = g_strdup(e);
661
662     e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
663     if (e != NULL)
664         history_file = g_strdup(e);
665
666     e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
667     if (e != NULL)
668         home_uri = g_strdup(e);
669
670     e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
671     if (e != NULL)
672         user_agent = g_strdup(e);
673
674     e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
675     if (e != NULL)
676         global_zoom = atof(e);
677 }
678
679 void
680 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
681 {
682     struct Client *c = (struct Client *)data;
683     WebKitJavascriptResult *js_result;
684     JSCValue *value;
685     JSCException *exception;
686     GError *err = NULL;
687     gchar *str_value;
688
689     g_free(c->feed_html);
690     c->feed_html = NULL;
691
692     /* This was taken almost verbatim from the example in WebKit's
693      * documentation:
694      *
695      * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
696
697     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
698                                                       result, &err);
699     if (!js_result)
700     {
701         fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
702         g_error_free(err);
703         return;
704     }
705
706     value = webkit_javascript_result_get_js_value(js_result);
707     if (jsc_value_is_string(value))
708     {
709         str_value = jsc_value_to_string(value);
710         exception = jsc_context_get_exception(jsc_value_get_context(value));
711         if (exception != NULL)
712         {
713             fprintf(stderr, __NAME__": Error running javascript: %s\n",
714                     jsc_exception_get_message(exception));
715         }
716         else
717             c->feed_html = str_value;
718
719         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
720                                           GTK_ENTRY_ICON_PRIMARY,
721                                           "application-rss+xml-symbolic");
722         gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
723                                        GTK_ENTRY_ICON_PRIMARY,
724                                        TRUE);
725     }
726     else
727     {
728         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
729                                           GTK_ENTRY_ICON_PRIMARY,
730                                           NULL);
731     }
732
733     webkit_javascript_result_unref(js_result);
734 }
735
736 void
737 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
738                gpointer data)
739 {
740     struct Client *c = (struct Client *)data;
741
742     if (!gtk_widget_is_focus(c->location))
743     {
744         if (webkit_hit_test_result_context_is_link(ht))
745         {
746             gtk_entry_set_text(GTK_ENTRY(c->location),
747                                webkit_hit_test_result_get_link_uri(ht));
748
749             if (c->hover_uri != NULL)
750                 g_free(c->hover_uri);
751             c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
752         }
753         else
754         {
755             gtk_entry_set_text(GTK_ENTRY(c->location),
756                                webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
757
758             if (c->hover_uri != NULL)
759                 g_free(c->hover_uri);
760             c->hover_uri = NULL;
761         }
762     }
763 }
764
765 void
766 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
767               gpointer data)
768 {
769     struct Client *c = (struct Client *)data;
770     gchar *d;
771     gchar *data_template =
772         "data:text/html,"
773         "<!DOCTYPE html>"
774         "<html>"
775         "    <head>"
776         "        <meta charset=\"UTF-8\">"
777         "        <title>Feeds</title>"
778         "    </head>"
779         "    <body>"
780         "        <p>Feeds found on this page:</p>"
781         "        <ul>"
782         "        %s"
783         "        </ul>"
784         "    </body>"
785         "</html>";
786
787     if (c->feed_html != NULL)
788     {
789         /* What we're actually trying to do is show a simple HTML page
790          * that lists all the feeds on the current page. The function
791          * webkit_web_view_load_html() looks like the proper way to do
792          * that. Sad thing is, it doesn't create a history entry, but
793          * instead simply replaces the content of the current page. This
794          * is not what we want.
795          *
796          * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
797          * mechanism to show my custom HTML snippet *and* create a
798          * history entry.
799          *
800          * [0]: https://tools.ietf.org/html/rfc2397
801          * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
802         d = g_strdup_printf(data_template, c->feed_html);
803         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
804         g_free(d);
805     }
806 }
807
808 gboolean
809 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
810 {
811     struct Client *c = (struct Client *)data;
812     WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
813     gchar *f;
814
815     if (event->type == GDK_KEY_PRESS)
816     {
817         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
818         {
819             switch (((GdkEventKey *)event)->keyval)
820             {
821                 case GDK_KEY_q:  /* close window (left hand) */
822                     gtk_widget_destroy(c->win);
823                     return TRUE;
824                 case GDK_KEY_w:  /* home (left hand) */
825                     f = ensure_uri_scheme(home_uri);
826                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
827                     g_free(f);
828                     return TRUE;
829                 case GDK_KEY_e:  /* new tab (left hand) */
830                     f = ensure_uri_scheme(home_uri);
831                     client_new(f, NULL, TRUE);
832                     g_free(f);
833                     return TRUE;
834                 case GDK_KEY_r:  /* reload (left hand) */
835                     webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
836                                                         c->web_view));
837                     return TRUE;
838                 case GDK_KEY_d:  /* download manager (left hand) */
839                     gtk_widget_show_all(dm.win);
840                     return TRUE;
841                 case GDK_KEY_2:  /* search forward (left hand) */
842                 case GDK_KEY_n:  /* search forward (maybe both hands) */
843                     search(c, 1);
844                     return TRUE;
845                 case GDK_KEY_3:  /* search backward (left hand) */
846                     search(c, -1);
847                     return TRUE;
848                 case GDK_KEY_l:  /* location (BOTH hands) */
849                     gtk_widget_grab_focus(c->location);
850                     return TRUE;
851                 case GDK_KEY_k:  /* initiate search (BOTH hands) */
852                     gtk_widget_grab_focus(c->location);
853                     gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
854                     gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
855                     return TRUE;
856                 case GDK_KEY_c:  /* reload trusted certs (left hand) */
857                     trust_user_certs(wc);
858                     return TRUE;
859                 case GDK_KEY_x:  /* launch external handler (left hand) */
860                     if (c->external_handler_uri != NULL)
861                         g_free(c->external_handler_uri);
862                     c->external_handler_uri = g_strdup(
863                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
864                     external_handler_run(NULL, NULL, c);
865                     return TRUE;
866             }
867         }
868         /* navigate backward (left hand) */
869         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
870         {
871             webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
872             return TRUE;
873         }
874         /* navigate forward (left hand) */
875         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
876         {
877             webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
878             return TRUE;
879         }
880     }
881
882     return FALSE;
883 }
884
885 gboolean
886 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
887 {
888     if (event->type == GDK_KEY_PRESS)
889     {
890         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
891         {
892             switch (((GdkEventKey *)event)->keyval)
893             {
894                 case GDK_KEY_d:  /* close window (left hand) */
895                 case GDK_KEY_q:
896                     downloadmanager_delete(dm.win, NULL);
897                     return TRUE;
898             }
899         }
900     }
901
902     return FALSE;
903 }
904
905 gboolean
906 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
907 {
908     struct Client *c = (struct Client *)data;
909     const gchar *t;
910     gchar *f;
911
912     if (key_common(widget, event, data))
913         return TRUE;
914
915     if (event->type == GDK_KEY_PRESS)
916     {
917         switch (((GdkEventKey *)event)->keyval)
918         {
919             case GDK_KEY_KP_Enter:
920             case GDK_KEY_Return:
921                 gtk_widget_grab_focus(c->web_view);
922                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
923                 if (t != NULL && t[0] == ':' && t[1] == '/')
924                 {
925                     if (search_text != NULL)
926                         g_free(search_text);
927                     search_text = g_strdup(t + 2);
928                     search(c, 0);
929                 }
930                 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
931                 {
932                     f = ensure_uri_scheme(t);
933                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
934                     g_free(f);
935                 }
936                 return TRUE;
937             case GDK_KEY_Escape:
938                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
939                 gtk_entry_set_text(GTK_ENTRY(c->location),
940                                    (t == NULL ? __NAME__ : t));
941                 return TRUE;
942         }
943     }
944
945     return FALSE;
946 }
947
948 gboolean
949 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
950 {
951     struct Client *c = (struct Client *)data;
952     gdouble dx, dy;
953     gfloat z;
954
955     if (key_common(widget, event, data))
956         return TRUE;
957
958     if (event->type == GDK_KEY_PRESS)
959     {
960         if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
961         {
962             webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
963             gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
964         }
965     }
966     else if (event->type == GDK_BUTTON_RELEASE)
967     {
968         switch (((GdkEventButton *)event)->button)
969         {
970             case 2:
971                 if (c->hover_uri != NULL)
972                 {
973                     client_new(c->hover_uri, NULL, TRUE);
974                     return TRUE;
975                 }
976                 break;
977             case 8:
978                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
979                 return TRUE;
980             case 9:
981                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
982                 return TRUE;
983         }
984     }
985     else if (event->type == GDK_SCROLL)
986     {
987         if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
988             ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
989         {
990             gdk_event_get_scroll_deltas(event, &dx, &dy);
991             z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
992             z += -dy * 0.1;
993             z = dx != 0 ? global_zoom : z;
994             webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
995             return TRUE;
996         }
997     }
998
999     return FALSE;
1000 }
1001
1002 void
1003 keywords_load(void)
1004 {
1005     GError *err = NULL;
1006     GIOChannel *channel = NULL;
1007     gchar *path = NULL, *buf = NULL;
1008     gchar **tokens = NULL;
1009
1010     keywords = g_hash_table_new(g_str_hash, g_str_equal);
1011
1012     path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1013                             NULL);
1014     channel = g_io_channel_new_file(path, "r", &err);
1015     if (channel != NULL)
1016     {
1017         while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1018                == G_IO_STATUS_NORMAL)
1019         {
1020             g_strstrip(buf);
1021             if (buf[0] != '#')
1022             {
1023                 tokens = g_strsplit(buf, " ", 2);
1024                 if (tokens[0] != NULL && tokens[1] != NULL)
1025                     g_hash_table_insert(keywords, g_strdup(tokens[0]),
1026                                         g_strdup(tokens[1]));
1027                 g_strfreev(tokens);
1028             }
1029             g_free(buf);
1030         }
1031         g_io_channel_shutdown(channel, FALSE, NULL);
1032     }
1033     g_free(path);
1034 }
1035
1036 gboolean
1037 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1038 {
1039     gboolean ret = FALSE;
1040     gchar **tokens = NULL;
1041     gchar *val = NULL, *escaped = NULL, *uri = NULL;
1042
1043     tokens = g_strsplit(t, " ", 2);
1044     if (tokens[0] != NULL && tokens[1] != NULL)
1045     {
1046         val = g_hash_table_lookup(keywords, tokens[0]);
1047         if (val != NULL)
1048         {
1049             escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1050             uri = g_strdup_printf((gchar *)val, escaped);
1051             webkit_web_view_load_uri(web_view, uri);
1052             g_free(uri);
1053             g_free(escaped);
1054             ret = TRUE;
1055         }
1056     }
1057     g_strfreev(tokens);
1058
1059     return ret;
1060 }
1061
1062 gboolean
1063 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1064               WebKitHitTestResult *ht, gpointer data)
1065 {
1066     struct Client *c = (struct Client *)data;
1067     GSimpleAction *action = NULL;
1068     WebKitContextMenuItem *mi = NULL;
1069     const gchar *uri = NULL;
1070
1071     (void)ev;
1072
1073     if (webkit_hit_test_result_context_is_link(ht))
1074         uri = webkit_hit_test_result_get_link_uri(ht);
1075     else if (webkit_hit_test_result_context_is_image(ht))
1076         uri = webkit_hit_test_result_get_image_uri(ht);
1077     else if (webkit_hit_test_result_context_is_media(ht))
1078         uri = webkit_hit_test_result_get_media_uri(ht);
1079
1080     if (uri != NULL)
1081     {
1082         webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1083
1084         if (c->external_handler_uri != NULL)
1085             g_free(c->external_handler_uri);
1086         c->external_handler_uri = g_strdup(uri);
1087         action = g_simple_action_new("external_handler", NULL);
1088         g_signal_connect(G_OBJECT(action), "activate",
1089                          G_CALLBACK(external_handler_run), data);
1090         mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1091                                                        "Open with external handler",
1092                                                        NULL);
1093         webkit_context_menu_append(menu, mi);
1094         g_object_unref(action);
1095     }
1096
1097     /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1098     return FALSE;
1099 }
1100
1101 gboolean
1102 quit_if_nothing_active(void)
1103 {
1104     if (clients == 0)
1105     {
1106         if (downloads == 0)
1107         {
1108             gtk_main_quit();
1109             return TRUE;
1110         }
1111         else
1112             gtk_widget_show_all(dm.win);
1113     }
1114
1115     return FALSE;
1116 }
1117
1118 gboolean
1119 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1120 {
1121     gchar *uri = NULL;
1122
1123     g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1124     if (uri)
1125     {
1126         g_strstrip(uri);
1127         client_new(uri, NULL, TRUE);
1128         g_free(uri);
1129     }
1130     return TRUE;
1131 }
1132
1133 void
1134 run_user_scripts(WebKitWebView *web_view)
1135 {
1136     gchar *base = NULL, *path = NULL, *contents = NULL;
1137     const gchar *entry = NULL;
1138     GDir *scriptdir = NULL;
1139
1140     base = g_build_filename(g_get_user_config_dir(), __NAME__, "scripts", NULL);
1141     scriptdir = g_dir_open(base, 0, NULL);
1142     if (scriptdir != NULL)
1143     {
1144         while ((entry = g_dir_read_name(scriptdir)) != NULL)
1145         {
1146             path = g_build_filename(base, entry, NULL);
1147             if (g_str_has_suffix(path, ".js"))
1148             {
1149                 if (g_file_get_contents(path, &contents, NULL, NULL))
1150                 {
1151                     webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1152                     g_free(contents);
1153                 }
1154             }
1155             g_free(path);
1156         }
1157         g_dir_close(scriptdir);
1158     }
1159
1160     g_free(base);
1161 }
1162
1163 void
1164 search(gpointer data, gint direction)
1165 {
1166     struct Client *c = (struct Client *)data;
1167     WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1168     WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1169
1170     if (search_text == NULL)
1171         return;
1172
1173     switch (direction)
1174     {
1175         case 0:
1176             webkit_find_controller_search(fc, search_text,
1177                                           WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1178                                           WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1179                                           G_MAXUINT);
1180             break;
1181         case 1:
1182             webkit_find_controller_search_next(fc);
1183             break;
1184         case -1:
1185             webkit_find_controller_search_previous(fc);
1186             break;
1187     }
1188 }
1189
1190 void
1191 show_web_view(WebKitWebView *web_view, gpointer data)
1192 {
1193     struct Client *c = (struct Client *)data;
1194
1195     (void)web_view;
1196
1197     gtk_widget_grab_focus(c->web_view);
1198     gtk_widget_show_all(c->win);
1199 }
1200
1201 Window
1202 tabbed_launch(void)
1203 {
1204     gint tabbed_stdout;
1205     GIOChannel *tabbed_stdout_channel;
1206     GError *err = NULL;
1207     gchar *output = NULL;
1208     char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1209     Window plug_into;
1210
1211     if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1212                                   NULL, NULL, NULL, &tabbed_stdout, NULL,
1213                                   &err))
1214     {
1215         fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1216         g_error_free(err);
1217         return 0;
1218     }
1219
1220     tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1221     if (tabbed_stdout_channel == NULL)
1222     {
1223         fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1224         return 0;
1225     }
1226     g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1227     g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1228     if (output == NULL)
1229     {
1230         fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1231         return 0;
1232     }
1233     g_strstrip(output);
1234     plug_into = strtol(output, NULL, 16);
1235     g_free(output);
1236     if (plug_into == 0)
1237         fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1238     return plug_into;
1239 }
1240
1241 void
1242 trust_user_certs(WebKitWebContext *wc)
1243 {
1244     GTlsCertificate *cert;
1245     const gchar *basedir, *file, *absfile;
1246     GDir *dir;
1247
1248     basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1249     dir = g_dir_open(basedir, 0, NULL);
1250     if (dir != NULL)
1251     {
1252         file = g_dir_read_name(dir);
1253         while (file != NULL)
1254         {
1255             absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1256                                        file, NULL);
1257             cert = g_tls_certificate_new_from_file(absfile, NULL);
1258             if (cert == NULL)
1259                 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1260             else
1261                 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1262             file = g_dir_read_name(dir);
1263         }
1264         g_dir_close(dir);
1265     }
1266 }
1267
1268
1269 int
1270 main(int argc, char **argv)
1271 {
1272     gchar *c;
1273     int opt, i;
1274
1275     gtk_init(&argc, &argv);
1276     webkit_web_context_set_process_model(webkit_web_context_get_default(),
1277         WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1278
1279     grab_environment_configuration();
1280
1281     while ((opt = getopt(argc, argv, "e:CT")) != -1)
1282     {
1283         switch (opt)
1284         {
1285             case 'e':
1286                 embed = atol(optarg);
1287                 tabbed_automagic = FALSE;
1288                 break;
1289             case 'C':
1290                 cooperative_instances = FALSE;
1291                 break;
1292             case 'T':
1293                 tabbed_automagic = FALSE;
1294                 break;
1295             default:
1296                 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1297                 exit(EXIT_FAILURE);
1298         }
1299     }
1300
1301     keywords_load();
1302     if (cooperative_instances)
1303         cooperation_setup();
1304     downloadmanager_setup();
1305
1306     if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1307         embed = tabbed_launch();
1308
1309     if (!cooperative_instances || cooperative_alone)
1310     {
1311         c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1312                              NULL);
1313         webkit_web_context_set_web_extensions_directory(
1314             webkit_web_context_get_default(), c
1315         );
1316     }
1317
1318     if (optind >= argc)
1319         client_new(home_uri, NULL, TRUE);
1320     else
1321     {
1322         for (i = optind; i < argc; i++)
1323             client_new(argv[i], NULL, TRUE);
1324     }
1325
1326     if (!cooperative_instances || cooperative_alone)
1327         gtk_main();
1328     exit(EXIT_SUCCESS);
1329 }