]> git.armaanb.net Git - chorizo.git/blob - zea.c
Fix minor memleak
[chorizo.git] / zea.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <string.h>
7
8 #include <gtk/gtk.h>
9 #include <gdk/gdkx.h>
10 #include <gdk/gdkkeysyms.h>
11 #include <gio/gio.h>
12 #include <webkit/webkit.h>
13
14
15 #define DOWNLOAD_DIR "/tmp/tmp"
16 #define ZEA_LANGUAGE "en-US"
17
18
19 static void zea_adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
20                         WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
21 static void zea_destroy_client(GtkWidget *, gpointer);
22 static gboolean zea_do_download(WebKitWebView *, WebKitDownload *, gpointer);
23 static gboolean zea_download_request(WebKitWebView *, WebKitWebFrame *,
24                                      WebKitNetworkRequest *, gchar *,
25                                      WebKitWebPolicyDecision *, gpointer);
26 static Window zea_launch_tabbed(void);
27 static void zea_load_adblock(void);
28 static void zea_load_status_changed(GObject *obj, GParamSpec *pspec,
29                                     gpointer data);
30 static gboolean zea_location_key(GtkWidget *, GdkEvent *, gpointer);
31 static void zea_new_client(const gchar *uri);
32 static gboolean zea_new_client_request(WebKitWebView *, WebKitWebFrame *,
33                                        WebKitNetworkRequest *,
34                                        WebKitWebNavigationAction *,
35                                        WebKitWebPolicyDecision *, gpointer);
36 static gboolean zea_remote_msg(GIOChannel *, GIOCondition, gpointer);
37 static void zea_search(gpointer, gint);
38 static void zea_setup_cooperation(void);
39 static void zea_scroll(GtkAdjustment *, gint, gdouble);
40 static void zea_title_changed(GObject *, GParamSpec *, gpointer);
41 static void zea_uri_changed(GObject *, GParamSpec *, gpointer);
42 static void zea_usage(void);
43 static void zea_web_view_hover(WebKitWebView *, gchar *, gchar *, gpointer);
44 static gboolean zea_web_view_key(GtkWidget *, GdkEvent *, gpointer);
45
46
47 static Window embed = 0;
48 static gint clients = 0;
49 static gdouble global_zoom = 1.0;
50 static gchar *search_text = NULL;
51 static gchar *first_uri = NULL;
52 static gboolean show_all_requests = FALSE;
53 static GSList *adblock_patterns = NULL;
54 static gboolean cooperative_instances = TRUE;
55 static int cooperative_pipe_fp = 0;
56 static gboolean alone = TRUE;
57 static gboolean launch_tabbed = TRUE;
58 static gboolean language_set = FALSE;
59
60
61 struct Client
62 {
63         GtkWidget *win;
64         GtkWidget *vbox;
65         GtkWidget *location;
66         GtkWidget *status;
67         GtkWidget *scroll;
68         GtkWidget *web_view;
69 };
70
71
72 void
73 zea_adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
74             WebKitWebResource *resource, WebKitNetworkRequest *request,
75             WebKitNetworkResponse *response, gpointer data)
76 {
77         GSList *it = adblock_patterns;
78         const gchar *uri;
79
80         (void)web_view;
81         (void)frame;
82         (void)resource;
83         (void)response;
84         (void)data;
85
86         uri = webkit_network_request_get_uri(request);
87         if (show_all_requests)
88                 fprintf(stderr, "-> %s\n", uri);
89
90         while (it)
91         {
92                 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
93                 {
94                         webkit_network_request_set_uri(request, "about:blank");
95                         if (show_all_requests)
96                                 fprintf(stderr, "\tBLOCKED!\n");
97                         return;
98                 }
99                 it = g_slist_next(it);
100         }
101 }
102
103 void
104 zea_destroy_client(GtkWidget *obj, gpointer data)
105 {
106         struct Client *c = (struct Client *)data;
107
108         (void)obj;
109
110         webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
111         gtk_widget_destroy(c->web_view);
112         gtk_widget_destroy(c->scroll);
113         gtk_widget_destroy(c->status);
114         gtk_widget_destroy(c->location);
115         gtk_widget_destroy(c->vbox);
116         gtk_widget_destroy(c->win);
117         free(c);
118
119         clients--;
120         if (clients == 0)
121                 gtk_main_quit();
122 }
123
124 gboolean
125 zea_do_download(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
126 {
127         const gchar *uri;
128         char id[16] = "";
129         gint ret;
130
131         (void)web_view;
132         (void)data;
133
134         uri = webkit_download_get_uri(download);
135         if (fork() == 0)
136         {
137                 chdir(DOWNLOAD_DIR);
138                 if (embed == 0)
139                         ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
140                 else
141                 {
142                         if (snprintf(id, 16, "%ld", embed) >= 16)
143                         {
144                                 fprintf(stderr, "zea: id for xterm embed truncated!\n");
145                                 exit(EXIT_FAILURE);
146                         }
147                         ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
148                                      uri, NULL);
149                 }
150
151                 if (ret == -1)
152                 {
153                         fprintf(stderr, "zea: exec'ing xterm for download");
154                         perror(" failed");
155                         exit(EXIT_FAILURE);
156                 }
157         }
158
159         return FALSE;
160 }
161
162 gboolean
163 zea_download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
164                      WebKitNetworkRequest *request, gchar *mime_type,
165                      WebKitWebPolicyDecision *policy_decision,
166                      gpointer data)
167 {
168         (void)frame;
169         (void)request;
170         (void)data;
171
172         if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
173         {
174                 webkit_web_policy_decision_download(policy_decision);
175                 return TRUE;
176         }
177         return FALSE;
178 }
179
180 gboolean
181 zea_location_key(GtkWidget *widget, GdkEvent *event, gpointer data)
182 {
183         struct Client *c = (struct Client *)data;
184         const gchar *t;
185
186         (void)widget;
187
188         if (event->type == GDK_KEY_PRESS)
189         {
190                 if (((GdkEventKey *)event)->keyval == GDK_KEY_Return)
191                 {
192                         gtk_widget_grab_focus(c->web_view);
193                         t = gtk_entry_get_text(GTK_ENTRY(c->location));
194                         if (t != NULL && t[0] == '/')
195                         {
196                                 if (search_text != NULL)
197                                         g_free(search_text);
198                                 search_text = g_strdup(t + 1);  /* XXX whacky */
199                                 zea_search(c, 1);
200                         }
201                         else
202                                 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), t);
203                         return TRUE;
204                 }
205         }
206
207         return FALSE;
208 }
209
210 void
211 zea_new_client(const gchar *uri)
212 {
213         if (cooperative_instances && !alone)
214         {
215                 write(cooperative_pipe_fp, uri, strlen(uri));
216                 write(cooperative_pipe_fp, "\n", 1);
217                 return;
218         }
219
220         struct Client *c = malloc(sizeof(struct Client));
221         if (!c)
222         {
223                 fprintf(stderr, "zea: fatal: malloc failed\n");
224                 exit(EXIT_FAILURE);
225         }
226
227         c->win = NULL;
228         if (embed != 0)
229         {
230                 c->win = gtk_plug_new(embed);
231                 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
232                 {
233                         fprintf(stderr, "zea: Can't plug-in to XID %ld.\n", embed);
234                         gtk_widget_destroy(c->win);
235                         c->win = NULL;
236                         embed = 0;
237                 }
238         }
239
240         if (c->win == NULL)
241                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
242
243         /* When using Gtk2, zea only shows a white area when run in
244          * suckless' tabbed. It appears we need to set a default window size
245          * for this to work. This is not needed when using Gtk3. */
246         gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
247
248         g_signal_connect(G_OBJECT(c->win), "destroy",
249                          G_CALLBACK(zea_destroy_client), c);
250         gtk_window_set_title(GTK_WINDOW(c->win), "zea");
251
252         c->web_view = webkit_web_view_new();
253         webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
254         webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
255         g_signal_connect(G_OBJECT(c->web_view), "notify::title",
256                          G_CALLBACK(zea_title_changed), c);
257         g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
258                          G_CALLBACK(zea_uri_changed), c);
259         g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
260                          G_CALLBACK(zea_load_status_changed), c);
261         g_signal_connect(G_OBJECT(c->web_view),
262                          "new-window-policy-decision-requested",
263                          G_CALLBACK(zea_new_client_request), NULL);
264         g_signal_connect(G_OBJECT(c->web_view),
265                          "mime-type-policy-decision-requested",
266                          G_CALLBACK(zea_download_request), NULL);
267         g_signal_connect(G_OBJECT(c->web_view), "download-requested",
268                          G_CALLBACK(zea_do_download), NULL);
269         g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
270                          G_CALLBACK(zea_web_view_key), c);
271         g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
272                          G_CALLBACK(zea_web_view_hover), c);
273         g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
274                          G_CALLBACK(zea_adblock), NULL);
275
276         if (!language_set)
277         {
278                 g_object_set(webkit_get_default_session(), "accept-language",
279                              ZEA_LANGUAGE, NULL);
280                 language_set = TRUE;
281         }
282
283         c->scroll = gtk_scrolled_window_new(NULL, NULL);
284
285         gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
286
287         c->location = gtk_entry_new();
288         g_signal_connect(G_OBJECT(c->location), "key-press-event",
289                          G_CALLBACK(zea_location_key), c);
290
291         c->status = gtk_statusbar_new();
292         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
293
294         c->vbox = gtk_vbox_new(FALSE, 2);
295         gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
296         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
297         gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
298
299         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
300
301         gtk_widget_grab_focus(c->web_view);
302         gtk_widget_show_all(c->win);
303
304         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), uri);
305
306         clients++;
307 }
308
309 gboolean
310 zea_new_client_request(WebKitWebView *web_view, WebKitWebFrame *frame,
311                        WebKitNetworkRequest *request,
312                        WebKitWebNavigationAction *navigation_action,
313                        WebKitWebPolicyDecision *policy_decision,
314                        gpointer user_data)
315 {
316         (void)web_view;
317         (void)frame;
318         (void)navigation_action;
319         (void)user_data;
320
321         webkit_web_policy_decision_ignore(policy_decision);
322         zea_new_client(webkit_network_request_get_uri(request));
323
324         return TRUE;
325 }
326
327 gboolean
328 zea_remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
329 {
330         gchar *uri = NULL;
331
332         (void)condition;
333         (void)data;
334
335         g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
336         if (uri)
337         {
338                 g_strstrip(uri);
339                 zea_new_client(uri);
340                 g_free(uri);
341         }
342         return TRUE;
343 }
344
345 void
346 zea_search(gpointer data, gint direction)
347 {
348         struct Client *c = (struct Client *)data;
349
350         if (search_text == NULL)
351                 return;
352
353         webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
354                                     FALSE, direction == 1, TRUE);
355 }
356
357 void
358 zea_setup_cooperation(void)
359 {
360         GIOChannel *towatch;
361         gchar *fifopath;
362
363         fifopath = g_build_filename(g_get_user_runtime_dir(), "zea.fifo", NULL);
364
365         if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
366                 mkfifo(fifopath, 0600);
367
368         cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
369         if (!cooperative_pipe_fp)
370         {
371                 fprintf(stderr, "zea: Can't open FIFO at all.\n");
372         }
373         else
374         {
375                 if (write(cooperative_pipe_fp, "", 0) == -1)
376                 {
377                         /* Could not do an empty write to the FIFO which means there's
378                          * no one listening. */
379                         close(cooperative_pipe_fp);
380                         towatch = g_io_channel_new_file(fifopath, "r+", NULL);
381                         g_io_add_watch(towatch, G_IO_IN, (GIOFunc)zea_remote_msg, NULL);
382                 }
383                 else
384                         alone = FALSE;
385         }
386
387         g_free(fifopath);
388 }
389
390 void
391 zea_scroll(GtkAdjustment *a, gint step_type, gdouble factor)
392 {
393         gdouble new, lower, upper, step;
394         lower = gtk_adjustment_get_lower(a);
395         upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
396         if (step_type == 0)
397                 step = gtk_adjustment_get_step_increment(a);
398         else
399                 step = gtk_adjustment_get_page_increment(a);
400         new = gtk_adjustment_get_value(a) + factor * step;
401         new = new < lower ? lower : new;
402         new = new > upper ? upper : new;
403         gtk_adjustment_set_value(a, new);
404 }
405
406 void
407 zea_title_changed(GObject *obj, GParamSpec *pspec, gpointer data)
408 {
409         const gchar *t;
410         struct Client *c = (struct Client *)data;
411
412         (void)obj;
413         (void)pspec;
414
415         t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
416         gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? "zea" : t));
417 }
418
419 void
420 zea_uri_changed(GObject *obj, GParamSpec *pspec, gpointer data)
421 {
422         const gchar *t;
423         struct Client *c = (struct Client *)data;
424
425         (void)obj;
426         (void)pspec;
427
428         t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
429         gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? "zea" : t));
430 }
431
432 void
433 zea_usage(void)
434 {
435         fprintf(stderr, "Usage: zea [OPTION]... <URI>...\n");
436         exit(EXIT_FAILURE);
437 }
438
439 Window
440 zea_launch_tabbed(void)
441 {
442         gint tabbed_stdout;
443         GIOChannel *tabbed_stdout_channel;
444         GError *err = NULL;
445         gchar *output = NULL;
446         char *argv[] = { "tabbed", "-c", "-d", NULL };
447         Window plug_into;
448
449         if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
450                                       NULL, NULL, NULL, &tabbed_stdout, NULL,
451                                       &err))
452         {
453                 fprintf(stderr, "zea: Could not launch tabbed: %s\n", err->message);
454                 g_error_free(err);
455                 return 0;
456         }
457
458         tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
459         g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
460         if (output == NULL)
461         {
462                 fprintf(stderr, "zea: Could not read XID from tabbed\n");
463                 return 0;
464         }
465
466         g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
467
468         g_strstrip(output);
469         plug_into = strtol(output, NULL, 16);
470         g_free(output);
471         return plug_into;
472 }
473
474 void
475 zea_load_adblock(void)
476 {
477         GRegex *re = NULL;
478         GError *err = NULL;
479         GIOChannel *channel = NULL;
480         gchar *path = NULL;
481         gchar *buf = NULL;
482
483         path = g_build_filename(g_get_user_config_dir(), "zea", "adblock.black", NULL);
484         channel = g_io_channel_new_file(path, "r", &err);
485         if (channel != NULL)
486         {
487                 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
488                        == G_IO_STATUS_NORMAL)
489                 {
490                         g_strstrip(buf);
491                         re = g_regex_new(buf,
492                                          G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
493                                          G_REGEX_MATCH_PARTIAL, &err);
494                         if (err != NULL)
495                         {
496                                 fprintf(stderr, "zea: Could not compile regex: %s\n", buf);
497                                 g_error_free(err);
498                                 err = NULL;
499                         }
500                         adblock_patterns = g_slist_append(adblock_patterns, re);
501
502                         g_free(buf);
503                 }
504         }
505         g_free(path);
506 }
507
508 void
509 zea_load_status_changed(GObject *obj, GParamSpec *pspec, gpointer data)
510 {
511         struct Client *c = (struct Client *)data;
512
513         (void)obj;
514         (void)pspec;
515
516         if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
517             == WEBKIT_LOAD_FINISHED)
518         {
519                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
520                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
521         }
522         else
523         {
524                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
525                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
526         }
527 }
528
529 void
530 zea_web_view_hover(WebKitWebView *web_view, gchar *title, gchar *uri,
531                    gpointer data)
532 {
533         struct Client *c = (struct Client *)data;
534
535         (void)web_view;
536         (void)title;
537
538         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
539         if (uri != NULL)
540                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
541 }
542
543 gboolean
544 zea_web_view_key(GtkWidget *widget, GdkEvent *event, gpointer data)
545 {
546         struct Client *c = (struct Client *)data;
547
548         (void)widget;
549
550         if (event->type == GDK_KEY_PRESS)
551         {
552                 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
553                 {
554                         if (((GdkEventKey *)event)->keyval == GDK_KEY_o)
555                         {
556                                 gtk_widget_grab_focus(c->location);
557                                 return TRUE;
558                         }
559                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_h)
560                         {
561                                 zea_scroll(gtk_scrolled_window_get_hadjustment(
562                                            GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
563                                 return TRUE;
564                         }
565                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_j)
566                         {
567                                 zea_scroll(gtk_scrolled_window_get_vadjustment(
568                                            GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
569                                 return TRUE;
570                         }
571                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_k)
572                         {
573                                 zea_scroll(gtk_scrolled_window_get_vadjustment(
574                                            GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
575                                 return TRUE;
576                         }
577                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_l)
578                         {
579                                 zea_scroll(gtk_scrolled_window_get_hadjustment(
580                                            GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
581                                 return TRUE;
582                         }
583                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_f)
584                         {
585                                 zea_scroll(gtk_scrolled_window_get_vadjustment(
586                                            GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
587                                 return TRUE;
588                         }
589                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_b)
590                         {
591                                 zea_scroll(gtk_scrolled_window_get_vadjustment(
592                                            GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
593                                 return TRUE;
594                         }
595                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_n)
596                         {
597                                 zea_search(c, 1);
598                                 return TRUE;
599                         }
600                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_p)
601                         {
602                                 zea_search(c, -1);
603                                 return TRUE;
604                         }
605                         else if (((GdkEventKey *)event)->keyval == GDK_KEY_g)
606                         {
607                                 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
608                                                          first_uri);
609                                 return TRUE;
610                         }
611                 }
612                 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
613                 {
614                         webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
615                         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
616                         gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
617                 }
618         }
619
620         return FALSE;
621 }
622
623 int
624 main(int argc, char **argv)
625 {
626         int opt, i;
627
628         gtk_init(&argc, &argv);
629
630         while ((opt = getopt(argc, argv, "z:e:rCT")) != -1)
631         {
632                 switch (opt)
633                 {
634                         case 'z':
635                                 global_zoom = atof(optarg);
636                                 break;
637                         case 'e':
638                                 embed = atol(optarg);
639                                 launch_tabbed = FALSE;
640                                 break;
641                         case 'r':
642                                 show_all_requests = TRUE;
643                                 break;
644                         case 'C':
645                                 cooperative_instances = FALSE;
646                                 break;
647                         case 'T':
648                                 launch_tabbed = FALSE;
649                                 break;
650                         default:
651                                 zea_usage();
652                 }
653         }
654
655         if (optind >= argc)
656                 zea_usage();
657
658         zea_load_adblock();
659         zea_setup_cooperation();
660
661         if (launch_tabbed && !(cooperative_instances && !alone))
662                 embed = zea_launch_tabbed();
663
664         first_uri = g_strdup(argv[optind]);
665         for (i = optind; i < argc; i++)
666                 zea_new_client(argv[i]);
667         if (!cooperative_instances || alone)
668                 gtk_main();
669         exit(EXIT_SUCCESS);
670 }