10 #include <gdk/gdkkeysyms.h>
12 #include <webkit/webkit.h>
15 static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
16 WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
17 static void adblock_load(void);
18 static void client_destroy(GtkWidget *, gpointer);
19 static gboolean client_destroy_request(WebKitWebView *, gpointer);
20 static WebKitWebView *client_new(const gchar *);
21 static WebKitWebView *client_new_request(WebKitWebView *, WebKitWebFrame *,
23 static void cooperation_setup(void);
24 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
26 static void changed_title(GObject *, GParamSpec *, gpointer);
27 static void changed_uri(GObject *, GParamSpec *, gpointer);
28 static gboolean download_handle(WebKitWebView *, WebKitDownload *, gpointer);
29 static gboolean download_reset_indicator(gpointer);
30 static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
31 WebKitNetworkRequest *, gchar *,
32 WebKitWebPolicyDecision *, gpointer);
33 static void downloadmanager_cancel(GtkToolButton *, gpointer data);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void grab_environment_configuration(void);
37 static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
38 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
41 static void keywords_load(void);
42 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
43 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
44 static void search(gpointer, gint);
45 static Window tabbed_launch(void);
46 static void usage(void);
61 struct DownloadManager
69 static gchar *accepted_language = "en-US";
70 static GSList *adblock_patterns = NULL;
71 static gint clients = 0;
72 static gboolean cooperative_alone = TRUE;
73 static gboolean cooperative_instances = TRUE;
74 static int cooperative_pipe_fp = 0;
75 static gchar *download_dir = "/tmp";
76 static gint downloads_indicated = 0;
77 static Window embed = 0;
78 static gchar *fifo_suffix = "main";
79 static gdouble global_zoom = 1.0;
80 static gchar *home_uri = "about:blank";
81 static GHashTable *keywords = NULL;
82 static gboolean language_is_set = FALSE;
83 static gchar *search_text = NULL;
84 static gboolean show_all_requests = FALSE;
85 static gboolean tabbed_automagic = TRUE;
86 static gchar *user_agent = "Mozilla/5.0 (X11; U; Unix; en-US) "
87 "AppleWebKit/537.15 (KHTML, like Gecko) "
89 "Safari/537.15 "__NAME_CAPITALIZED__"/git";
93 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
94 WebKitWebResource *resource, WebKitNetworkRequest *request,
95 WebKitNetworkResponse *response, gpointer data)
97 GSList *it = adblock_patterns;
100 uri = webkit_network_request_get_uri(request);
101 if (show_all_requests)
102 fprintf(stderr, " -> %s\n", uri);
106 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
108 webkit_network_request_set_uri(request, "about:blank");
109 if (show_all_requests)
110 fprintf(stderr, " BLOCKED!\n");
113 it = g_slist_next(it);
122 GIOChannel *channel = NULL;
123 gchar *path = NULL, *buf = NULL;
125 path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
127 channel = g_io_channel_new_file(path, "r", &err);
130 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
131 == G_IO_STATUS_NORMAL)
136 re = g_regex_new(buf,
137 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
138 G_REGEX_MATCH_PARTIAL, &err);
141 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
146 adblock_patterns = g_slist_append(adblock_patterns, re);
150 g_io_channel_shutdown(channel, FALSE, NULL);
156 client_destroy(GtkWidget *obj, gpointer data)
158 struct Client *c = (struct Client *)data;
160 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
161 changed_load_progress, c);
171 client_destroy_request(WebKitWebView *web_view, gpointer data)
173 struct Client *c = (struct Client *)data;
175 gtk_widget_destroy(c->win);
181 client_new(const gchar *uri)
186 if (uri != NULL && cooperative_instances && !cooperative_alone)
188 write(cooperative_pipe_fp, uri, strlen(uri));
189 write(cooperative_pipe_fp, "\n", 1);
193 c = malloc(sizeof(struct Client));
196 fprintf(stderr, __NAME__": fatal: malloc failed\n");
203 c->win = gtk_plug_new(embed);
204 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
206 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
207 gtk_widget_destroy(c->win);
215 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
216 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
219 /* When using Gtk2, it only shows a white area when run in suckless'
220 * tabbed. It appears we need to set a default window size for this
221 * to work. This is not needed when using Gtk3. */
222 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
224 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
225 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
227 c->web_view = webkit_web_view_new();
229 /* XXX I really do want to enable this option. However, I get
230 * reproducible crashes with it enabled. I've seen bug reports from
231 * 2010 about this... WebKit crashes in libpixman, so maybe it's not
233 * Yeah, well. I'll turn it off for now. */
234 /*webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);*/
236 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
237 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
238 G_CALLBACK(changed_title), c);
239 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
240 G_CALLBACK(changed_uri), c);
241 g_signal_connect(G_OBJECT(c->web_view), "notify::progress",
242 G_CALLBACK(changed_load_progress), c);
243 g_signal_connect(G_OBJECT(c->web_view), "create-web-view",
244 G_CALLBACK(client_new_request), NULL);
245 g_signal_connect(G_OBJECT(c->web_view), "close-web-view",
246 G_CALLBACK(client_destroy_request), c);
247 g_signal_connect(G_OBJECT(c->web_view),
248 "mime-type-policy-decision-requested",
249 G_CALLBACK(download_request), NULL);
250 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
251 G_CALLBACK(download_handle), c);
252 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
253 G_CALLBACK(key_web_view), c);
254 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
255 G_CALLBACK(key_web_view), c);
256 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
257 G_CALLBACK(key_web_view), c);
258 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
259 G_CALLBACK(hover_web_view), c);
260 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
261 G_CALLBACK(adblock), NULL);
263 if (!language_is_set)
265 g_object_set(webkit_get_default_session(), "accept-language",
266 accepted_language, NULL);
267 language_is_set = TRUE;
270 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
271 "user-agent", user_agent, NULL);
273 c->scroll = gtk_scrolled_window_new(NULL, NULL);
275 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
277 c->location = gtk_entry_new();
278 g_signal_connect(G_OBJECT(c->location), "key-press-event",
279 G_CALLBACK(key_location), c);
281 c->progress = gtk_progress_bar_new();
282 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
284 c->status = gtk_progress_bar_new();
285 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
286 gtk_widget_set_size_request(c->status, 20, -1);
288 c->top_box = gtk_hbox_new(FALSE, 0);
289 gtk_box_pack_start(GTK_BOX(c->top_box), c->status, FALSE, FALSE, 2);
290 gtk_box_pack_start(GTK_BOX(c->top_box), c->location, TRUE, TRUE, 0);
291 gtk_box_pack_end(GTK_BOX(c->top_box), c->progress, FALSE, TRUE, 2);
293 c->vbox = gtk_vbox_new(FALSE, 0);
294 gtk_box_pack_start(GTK_BOX(c->vbox), c->top_box, FALSE, FALSE, 2);
295 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
297 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
299 gtk_widget_grab_focus(c->web_view);
300 gtk_widget_show_all(c->win);
304 f = ensure_uri_scheme(uri);
305 if (show_all_requests)
306 fprintf(stderr, "====> %s\n", uri);
307 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
313 return WEBKIT_WEB_VIEW(c->web_view);
317 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame, gpointer data)
319 return client_new(NULL);
323 cooperation_setup(void)
326 gchar *fifofilename, *fifopath;
328 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
329 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
330 g_free(fifofilename);
332 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
333 mkfifo(fifopath, 0600);
335 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
336 if (!cooperative_pipe_fp)
338 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
342 if (write(cooperative_pipe_fp, "", 0) == -1)
344 /* Could not do an empty write to the FIFO which means there's
345 * no one listening. */
346 close(cooperative_pipe_fp);
347 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
348 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
351 cooperative_alone = FALSE;
358 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
360 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
361 GtkToolItem *tb = GTK_TOOL_ITEM(data);
364 gchar *t, *filename, *base;
366 p = webkit_download_get_progress(download) * 100;
367 size_mb = webkit_download_get_total_size(download) / 1e6;
369 uri = webkit_download_get_destination_uri(download);
370 filename = g_filename_from_uri(uri, NULL, NULL);
371 if (filename == NULL)
373 /* This really should not happen because WebKit uses that URI to
374 * write to a file... */
375 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
376 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
377 webkit_download_get_uri(download), p, size_mb);
381 base = g_path_get_basename(filename);
382 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
386 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
391 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
393 struct Client *c = (struct Client *)data;
396 p = webkit_web_view_get_progress(WEBKIT_WEB_VIEW(c->web_view));
397 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), p);
401 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
404 struct Client *c = (struct Client *)data;
406 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
407 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
411 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
414 struct Client *c = (struct Client *)data;
416 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
417 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
421 download_handle(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
423 struct Client *c = (struct Client *)data;
424 gchar *path, *path2 = NULL, *uri;
429 path = g_build_filename(download_dir,
430 webkit_download_get_suggested_filename(download),
432 path2 = g_strdup(path);
433 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
437 path2 = g_strdup_printf("%s.%d", path, suffix);
443 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
448 uri = g_filename_to_uri(path2, NULL, NULL);
449 webkit_download_set_destination_uri(download, uri);
453 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 1);
454 downloads_indicated++;
455 g_timeout_add(500, download_reset_indicator, c);
457 tb = gtk_tool_button_new_from_stock(GTK_STOCK_DELETE);
458 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb),
459 webkit_download_get_suggested_filename(download));
460 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
461 gtk_widget_show_all(dm.toolbar);
463 g_signal_connect(G_OBJECT(download), "notify::progress",
464 G_CALLBACK(changed_download_progress), tb);
466 g_object_ref(download);
467 g_signal_connect(G_OBJECT(tb), "clicked",
468 G_CALLBACK(downloadmanager_cancel), download);
478 download_reset_indicator(gpointer data)
480 struct Client *c = (struct Client *)data;
482 downloads_indicated--;
483 if (downloads_indicated == 0)
484 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
490 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
491 WebKitNetworkRequest *request, gchar *mime_type,
492 WebKitWebPolicyDecision *policy_decision, gpointer data)
494 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
496 webkit_web_policy_decision_download(policy_decision);
503 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
505 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
507 webkit_download_cancel(download);
508 g_object_unref(download);
510 gtk_widget_destroy(GTK_WIDGET(tb));
514 downloadmanager_setup(void)
516 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
517 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
518 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
519 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
520 g_signal_connect(G_OBJECT(dm.win), "delete-event",
521 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
522 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
523 G_CALLBACK(key_downloadmanager), NULL);
525 dm.toolbar = gtk_toolbar_new();
526 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
527 GTK_ORIENTATION_VERTICAL);
528 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
529 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
531 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
532 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
533 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
534 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(dm.scroll),
537 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
541 ensure_uri_scheme(const gchar *t)
545 f = g_ascii_strdown(t, -1);
546 if (!g_str_has_prefix(f, "http:") &&
547 !g_str_has_prefix(f, "https:") &&
548 !g_str_has_prefix(f, "file:") &&
549 !g_str_has_prefix(f, "about:"))
552 f = g_strdup_printf("http://%s", t);
560 grab_environment_configuration(void)
564 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
566 accepted_language = g_strdup(e);
568 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
570 download_dir = g_strdup(e);
572 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
574 fifo_suffix = g_strdup(e);
576 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
578 home_uri = g_strdup(e);
580 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
582 user_agent = g_strdup(e);
584 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
586 global_zoom = atof(e);
590 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri, gpointer data)
592 struct Client *c = (struct Client *)data;
594 if (!gtk_widget_is_focus(c->location))
597 gtk_entry_set_text(GTK_ENTRY(c->location),
598 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
600 gtk_entry_set_text(GTK_ENTRY(c->location), uri);
605 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
607 if (event->type == GDK_KEY_PRESS)
609 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
611 switch (((GdkEventKey *)event)->keyval)
613 case GDK_KEY_d: /* close window (left hand) */
614 gtk_widget_hide(dm.win);
624 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
626 struct Client *c = (struct Client *)data;
630 if (event->type == GDK_KEY_PRESS)
632 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
634 switch (((GdkEventKey *)event)->keyval)
636 case GDK_KEY_q: /* close window (left hand) */
637 gtk_widget_destroy(c->win);
639 case GDK_KEY_d: /* download manager (left hand) */
640 gtk_widget_show_all(dm.win);
642 case GDK_KEY_r: /* reload (left hand) */
643 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
646 case GDK_KEY_k: /* initiate search (BOTH hands) */
647 gtk_entry_set_text(GTK_ENTRY(c->location), "/");
648 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
654 switch (((GdkEventKey *)event)->keyval)
657 gtk_widget_grab_focus(c->web_view);
658 t = gtk_entry_get_text(GTK_ENTRY(c->location));
659 if (t != NULL && t[0] == '/')
661 if (search_text != NULL)
663 search_text = g_strdup(t + 1); /* XXX whacky */
666 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
668 f = ensure_uri_scheme(t);
669 if (show_all_requests)
670 fprintf(stderr, "====> %s\n", f);
671 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
676 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
677 gtk_entry_set_text(GTK_ENTRY(c->location),
678 (t == NULL ? __NAME__ : t));
688 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
690 struct Client *c = (struct Client *)data;
691 WebKitHitTestResultContext ht_context;
692 WebKitHitTestResult *ht_result = NULL;
693 gchar *ht_uri = NULL, *f;
697 if (event->type == GDK_KEY_PRESS)
699 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
701 switch (((GdkEventKey *)event)->keyval)
703 case GDK_KEY_q: /* close window (left hand) */
704 gtk_widget_destroy(c->win);
706 case GDK_KEY_w: /* home (left hand) */
707 f = ensure_uri_scheme(home_uri);
708 if (show_all_requests)
709 fprintf(stderr, "====> %s\n", f);
710 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
713 case GDK_KEY_e: /* new tab (left hand) */
714 f = ensure_uri_scheme(home_uri);
715 if (show_all_requests)
716 fprintf(stderr, "====> %s\n", f);
720 case GDK_KEY_r: /* reload (left hand) */
721 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
724 case GDK_KEY_s: /* toggle source view (left hand) */
725 b = webkit_web_view_get_view_source_mode(WEBKIT_WEB_VIEW(
728 webkit_web_view_set_view_source_mode(WEBKIT_WEB_VIEW(
730 webkit_web_view_reload(WEBKIT_WEB_VIEW(c->web_view));
732 case GDK_KEY_d: /* download manager (left hand) */
733 gtk_widget_show_all(dm.win);
735 case GDK_KEY_2: /* search forward (left hand) */
736 case GDK_KEY_n: /* search forward (maybe both hands) */
739 case GDK_KEY_3: /* search backward (left hand) */
742 case GDK_KEY_l: /* location (BOTH hands) */
743 gtk_widget_grab_focus(c->location);
745 case GDK_KEY_k: /* initiate search (BOTH hands) */
746 gtk_widget_grab_focus(c->location);
747 gtk_entry_set_text(GTK_ENTRY(c->location), "/");
748 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
752 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
754 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
755 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
758 else if (event->type == GDK_BUTTON_PRESS)
760 switch (((GdkEventButton *)event)->button)
763 ht_result = webkit_web_view_get_hit_test_result(
764 WEBKIT_WEB_VIEW(c->web_view),
765 (GdkEventButton *)event);
766 g_object_get(ht_result, "context", &ht_context, NULL);
767 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
769 g_object_get(ht_result, "link-uri", &ht_uri, NULL);
771 g_object_unref(ht_result);
774 g_object_unref(ht_result);
777 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
780 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
784 else if (event->type == GDK_SCROLL)
786 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
787 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
789 switch (((GdkEventScroll *)event)->direction)
792 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
794 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
796 case GDK_SCROLL_DOWN:
797 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
799 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
814 GIOChannel *channel = NULL;
815 gchar *path = NULL, *buf = NULL;
816 gchar **tokens = NULL;
818 keywords = g_hash_table_new(g_str_hash, g_str_equal);
820 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
822 channel = g_io_channel_new_file(path, "r", &err);
825 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
826 == G_IO_STATUS_NORMAL)
831 tokens = g_strsplit(buf, " ", 2);
832 if (tokens[0] != NULL && tokens[1] != NULL)
833 g_hash_table_insert(keywords, g_strdup(tokens[0]),
834 g_strdup(tokens[1]));
839 g_io_channel_shutdown(channel, FALSE, NULL);
845 keywords_try_search(WebKitWebView *web_view, const gchar *t)
847 gboolean ret = FALSE;
848 gchar **tokens = NULL;
849 gchar *val = NULL, *uri = NULL;
851 tokens = g_strsplit(t, " ", 2);
852 if (tokens[0] != NULL && tokens[1] != NULL)
854 val = g_hash_table_lookup(keywords, tokens[0]);
857 uri = g_strdup_printf((gchar *)val, tokens[1]);
858 if (show_all_requests)
859 fprintf(stderr, "====> %s\n", uri);
860 webkit_web_view_load_uri(web_view, uri);
871 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
875 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
886 search(gpointer data, gint direction)
888 struct Client *c = (struct Client *)data;
890 if (search_text == NULL)
893 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
894 FALSE, direction == 1, TRUE);
901 GIOChannel *tabbed_stdout_channel;
903 gchar *output = NULL;
904 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
907 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
908 NULL, NULL, NULL, &tabbed_stdout, NULL,
911 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
916 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
917 if (tabbed_stdout_channel == NULL)
919 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
922 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
923 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
926 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
930 plug_into = strtol(output, NULL, 16);
933 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
940 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
946 main(int argc, char **argv)
950 gtk_init(&argc, &argv);
952 grab_environment_configuration();
954 while ((opt = getopt(argc, argv, "e:rCT")) != -1)
959 embed = atol(optarg);
960 tabbed_automagic = FALSE;
963 show_all_requests = TRUE;
966 cooperative_instances = FALSE;
969 tabbed_automagic = FALSE;
979 downloadmanager_setup();
981 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
982 embed = tabbed_launch();
985 client_new(home_uri);
988 for (i = optind; i < argc; i++)
992 if (!cooperative_instances || cooperative_alone)