diff options
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | callbacks.c | 69 | ||||
-rw-r--r-- | callbacks.h | 6 | ||||
-rw-r--r-- | config.mk | 2 | ||||
-rw-r--r-- | sb.c | 151 | ||||
-rw-r--r-- | sb.h | 17 |
6 files changed, 119 insertions, 132 deletions
@@ -3,7 +3,7 @@ include config.mk -SRC = sb.c defaults.h +SRC = sb.c callbacks.c OBJ = ${SRC:.c=.o} all: options sb @@ -23,11 +23,11 @@ ${OBJ}: config.mk sb: ${OBJ} @echo CC -o $@ - @${CC} -o $@ sb.o ${LDFLAGS} + @${CC} -o $@ sb.o callbacks.o ${LDFLAGS} clean: @echo cleaning - @rm -f sb sb.o sb-${VERSION}.tar.gz + @rm -f sb ${OBJ} sb-${VERSION}.tar.gz dist: clean @echo creating dist tarball diff --git a/callbacks.c b/callbacks.c new file mode 100644 index 0000000..27bdaf1 --- /dev/null +++ b/callbacks.c @@ -0,0 +1,69 @@ +#include <gdk/gdkkeysyms.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <webkit/webkit.h> +#include <glib/gstdio.h> +#include "defaults.h" +#include "callbacks.h" + +#include "sb.h" +/* download callback */ +void cb_download(WebKitWebView *web_view, GObject *d, gpointer user_data) { +const gchar *c = webkit_download_get_uri(WEBKIT_DOWNLOAD(d)); +gchar *command = g_strconcat(DEFAULT_DOWNLOAD, g_get_home_dir(), "/ ", g_strdup(c), NULL); +g_spawn_command_line_async(command, NULL); +g_free(command); +} + + +/* entry callback */ +void cb_entry (GtkWidget* entry, gpointer data) { +struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); +load_uri(g_strdup(gtk_entry_get_text (GTK_ENTRY (w.bar)))); +gtk_widget_grab_focus(GTK_WIDGET(t->view)); + +if(w.hide) { + gtk_widget_hide(w.bar); + } +w.hide = FALSE; + +} + + +/* go forward or backwards, simple enough */ +void cb_go(gboolean b) { +struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); +if (b) { webkit_web_view_go_forward(t->view); } +else { webkit_web_view_go_back(t->view); } +} + +/* when the page load is commited, call this function */ +void cb_commit_load (WebKitWebView* page, WebKitWebFrame* frame, tab *t) { + +const gchar* uri = webkit_web_frame_get_uri(frame); + +if (gtk_notebook_get_current_page(w.notebook) == gtk_notebook_page_num(w.notebook, t->scroll)) { +gtk_entry_set_text (GTK_ENTRY (w.bar), uri); +} +FILE *history = fopen(g_build_filename(g_get_home_dir(), DEFAULT_HISTORY_FILE, NULL), "a+"); +fprintf(history, "%s \n", uri); +fclose(history); + +} + + +/* title change callback */ +void cb_title_changed(WebKitWebView *v, WebKitWebFrame *f, const char *title, tab *t) { +gchar *tabtitle; + +if (gtk_notebook_get_current_page(w.notebook) == gtk_notebook_page_num(w.notebook, t->scroll)) { +gtk_window_set_title(GTK_WINDOW(w.win), g_strconcat(title, NULL)); +} +if(strlen(title) < DEFAULT_TAB_LENGTH ) { tabtitle = g_strdup(title); } + else { tabtitle = g_strndup(title, DEFAULT_TAB_LENGTH); strcat(tabtitle, "..."); } + + +gtk_label_set_label(GTK_LABEL(t->label), tabtitle); +} diff --git a/callbacks.h b/callbacks.h new file mode 100644 index 0000000..bfd3511 --- /dev/null +++ b/callbacks.h @@ -0,0 +1,6 @@ +void cb_entry(GtkWidget* entry, gpointer data); +void cb_go(gboolean b); +void cb_download(WebKitWebView *web_view, GObject *download, gpointer user_data); +void cb_title_changed(WebKitWebView *v, WebKitWebFrame *f, const char *title, tab *t); +void cb_commit_load(WebKitWebView* page, WebKitWebFrame* frame, tab *t); + @@ -18,7 +18,7 @@ LIBS = -L/usr/lib -lc ${GTKLIB} # flags CPPFLAGS = -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} CFLAGS = -std=c99 -O2 -Wall -march=native ${INCS} ${CPPFLAGS} -LDFLAGS = -s ${LIBS} +LDFLAGS = -s -O1 ${LIBS} # Solaris #CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" @@ -10,56 +10,10 @@ #include <stdio.h> #include <webkit/webkit.h> #include <glib/gstdio.h> -#include <defaults.h> - -typedef struct tab { - GtkWidget *scroll; - GtkWidget *label; - gchar *main_title; - gchar *url_entry; - gint load_progress; - guint status_context_id; - WebKitWebView *view; - } tab; - -static struct { - WebKitWebSettings *webkitsettings; - WebKitWebWindowFeatures *webkitwindowfeatures; - SoupSession *session; - SoupCookieJar *jar; - gboolean hide; - GtkWidget *win; - GtkWidget *bar; - GtkWidget *search; - GtkWidget *vbox; - GtkNotebook *notebook; - GtkWidget *status; } w; - -#define get_tab(x, page_idx ) (struct tab*)g_object_get_qdata(G_OBJECT( gtk_notebook_get_nth_page( (GtkNotebook*)w.notebook, page_idx ) ), term_data_id); -static GQuark term_data_id = 0; - -static void cb_entry(GtkWidget* entry, gpointer data); -static void cb_go(gboolean b); -static void cb_download(WebKitWebView *web_view, GObject *download, gpointer user_data); -static void link_hover(WebKitWebView* page, const gchar* title, const gchar* link, gpointer data); -static void cb_title_changed(WebKitWebView *v, WebKitWebFrame *f, const char *title, tab *t); -static void load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, tab *t); -static void load_uri(gchar *uri); -static void tab_new(gboolean b); -static void tab_zoom(gboolean b); -static void tab_close(); -static void tab_and_go(); -static void tab_focus(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data); -static void tab_view_source(); -static void tab_reload(); -static void window_setup(); -static void search(GtkEntry *entry, gboolean b); -static void toggle(); -static void show_search(gboolean b); -static void focus_view(); -WebKitWebView * tab_new_requested(WebKitWebView *v, WebKitWebFrame *f); -static void tab_switch(gboolean b); -gboolean cb_keypress(GtkWidget *widget, GdkEventKey *event); +#include "defaults.h" +#include "callbacks.h" +#include "sb.h" + gchar* tab_get_tab_postition() { gchar *page_info = NULL; @@ -70,12 +24,12 @@ gchar* tab_get_tab_postition() { } -static void search(GtkEntry *entry, gboolean b) { +void search(GtkEntry *entry, gboolean b) { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); webkit_web_view_search_text(t->view, gtk_entry_get_text(GTK_ENTRY(w.search)), FALSE, b, TRUE); } -static void show_search(gboolean b) { +void show_search(gboolean b) { if(b) { gtk_widget_show(w.search); @@ -87,29 +41,15 @@ if(b) { } -/* Called when URL is returned from the entry bar */ -static void cb_entry (GtkWidget* entry, gpointer data) { -struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); -load_uri(g_strdup(gtk_entry_get_text (GTK_ENTRY (w.bar)))); -gtk_widget_grab_focus(GTK_WIDGET(t->view)); - -if(w.hide) { - gtk_widget_hide(w.bar); - } -w.hide = FALSE; - -} - - /* Basic reload function */ -static void tab_reload() { +void tab_reload() { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); webkit_web_view_reload(t->view); } /* close tab, and quit if there are no tabs */ -static void tab_close() { +void tab_close() { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); gtk_notebook_remove_page(w.notebook, gtk_notebook_get_current_page(w.notebook)); g_free(t); @@ -123,54 +63,16 @@ if (gtk_notebook_get_n_pages(w.notebook) == 0) { gtk_main_quit(); } } -/* download with external command */ -/* NEEDS WORK, should use sprintf to insert different values, 1) Location 2) File name */ -static void cb_download(WebKitWebView *web_view, GObject *d, gpointer user_data) { -const gchar *c = webkit_download_get_uri(WEBKIT_DOWNLOAD(d)); -gchar *command = g_strconcat(DEFAULT_DOWNLOAD, g_get_home_dir(), "/ ", g_strdup(c), NULL); -g_spawn_command_line_async(command, NULL); -g_free(command); -} - /* link hovering callback */ -static void link_hover (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) { +void link_hover (WebKitWebView* page, const gchar* title, const gchar* link, gpointer data) { if(link != NULL) { gtk_statusbar_push(GTK_STATUSBAR(w.status), 0, link); } else { gtk_statusbar_push(GTK_STATUSBAR(w.status), 0, ""); } } -/* title change callback */ -static void cb_title_changed(WebKitWebView *v, WebKitWebFrame *f, const char *title, tab *t) { -gchar *tabtitle; - -if (gtk_notebook_get_current_page(w.notebook) == gtk_notebook_page_num(w.notebook, t->scroll)) { -gtk_window_set_title(GTK_WINDOW(w.win), g_strconcat(title, NULL)); -} -if(strlen(title) < DEFAULT_TAB_LENGTH ) { tabtitle = g_strdup(title); } - else { tabtitle = g_strndup(title, DEFAULT_TAB_LENGTH); strcat(tabtitle, "..."); } - - -gtk_label_set_label(GTK_LABEL(t->label), tabtitle); -} - - -/* when the page load is commited, call this function */ -static void load_commit_cb (WebKitWebView* page, WebKitWebFrame* frame, tab *t) { - -const gchar* uri = webkit_web_frame_get_uri(frame); - -if (gtk_notebook_get_current_page(w.notebook) == gtk_notebook_page_num(w.notebook, t->scroll)) { -gtk_entry_set_text (GTK_ENTRY (w.bar), uri); -} -FILE *history = fopen(g_build_filename(g_get_home_dir(), DEFAULT_HISTORY_FILE, NULL), "a+"); -fprintf(history, "%s \n", uri); -fclose(history); - -} - /* an alternative to the regular tab command, combines tabbing and history command into one */ -static void tab_and_go() { +void tab_and_go() { gchar *returned; g_spawn_command_line_sync(g_strconcat("sh -c 'sort ", g_build_filename(g_get_home_dir(), ".sb_history", NULL), " | dmenu -l 15 -xs -c'", NULL), &returned, NULL, NULL, NULL); @@ -179,7 +81,7 @@ else { tab_new(FALSE); load_uri(returned); g_free(returned); } } /* loads the uri, check for the protocol sign */ -static void load_uri(gchar *uri) { +void load_uri(gchar *uri) { gchar *u; //Barrowed from surf, no point creating another method, this seems to work well @@ -192,7 +94,7 @@ g_free(u); /*increase or decrease the zoom of the page */ -static void tab_zoom (gboolean b) { +void tab_zoom (gboolean b) { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); if (b) { webkit_web_view_set_zoom_level(t->view, (webkit_web_view_get_zoom_level(t->view) + DEFAULT_ZOOM_INCREMENT)); } else { webkit_web_view_set_zoom_level(t->view, (webkit_web_view_get_zoom_level(t->view) - DEFAULT_ZOOM_INCREMENT)); } @@ -200,7 +102,7 @@ static void tab_zoom (gboolean b) { /* if the bar isn't visible, show it and set the w.hide flag to TRUE*/ -static void grab_bar( ) { +void grab_bar( ) { if(!gtk_widget_get_visible(w.bar)) { gtk_widget_show(w.bar); w.hide = TRUE; @@ -209,16 +111,8 @@ if(!gtk_widget_get_visible(w.bar)) { } -/* go forward or backwards, simple enough */ -static void cb_go(gboolean b) { -struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); -if (b) { webkit_web_view_go_forward(t->view); } -else { webkit_web_view_go_back(t->view); } -} - - /* toggle visibility */ -static void toggle() { +void toggle() { if(gtk_widget_get_visible(w.bar)) { gtk_widget_hide(w.bar); gtk_widget_hide(w.status); @@ -232,7 +126,7 @@ static void toggle() { } /* rotate tabs forward or backwards */ -static void tab_switch(gboolean b) { +void tab_switch(gboolean b) { gint(current) = gtk_notebook_get_current_page(w.notebook); @@ -258,7 +152,7 @@ WebKitWebView * tab_new_requested(WebKitWebView *v, WebKitWebFrame *f) { /* switch to view source mode - stays in mode until reverted */ -static void tab_view_source() { +void tab_view_source() { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); @@ -275,7 +169,7 @@ static void tab_view_source() { /* create a tab */ -static void tab_new(gboolean b) { +void tab_new(gboolean b) { tab *t; t = g_new0(tab, 1); @@ -308,7 +202,7 @@ webkit_web_view_set_zoom_level(t->view, DEFAULT_ZOOM_LEVEL); /*callbacks*/ g_signal_connect (G_OBJECT (t->view), "title-changed", G_CALLBACK (cb_title_changed), t); -g_signal_connect (G_OBJECT (t->view), "load-committed", G_CALLBACK (load_commit_cb), t); +g_signal_connect (G_OBJECT (t->view), "load-committed", G_CALLBACK (cb_commit_load), t); g_signal_connect (G_OBJECT (t->view), "hovering-over-link", G_CALLBACK (link_hover), t->view); g_signal_connect (G_OBJECT (t->view), "download-requested", G_CALLBACK (cb_download), t->view); g_signal_connect (G_OBJECT (t->view), "create-web-view", G_CALLBACK (tab_new_requested), NULL); @@ -324,7 +218,7 @@ gtk_widget_grab_focus(w.bar); /* call the history command. should we do it ASYNC?*/ -static void history_command() { +void history_command() { gchar *returned, *file; file = g_build_filename(g_get_home_dir(), DEFAULT_HISTORY_FILE, NULL); g_spawn_command_line_sync(g_strconcat("sh -c 'sort ", file, " | dmenu -l 15 -xs -c'", NULL), &returned, NULL, NULL, NULL); @@ -335,7 +229,7 @@ g_free(file); /*focus on tab after switching, aka title, statusbar, view, etc */ -static void tab_focus(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data) { +void tab_focus(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data) { gtk_statusbar_push(GTK_STATUSBAR(w.status), 0, ""); struct tab *t = get_tab(NULL, page_num); @@ -351,14 +245,14 @@ gtk_entry_set_text(GTK_ENTRY(w.bar), url); /* focus on view */ -static void focus_view() { +void focus_view() { struct tab *t = get_tab(NULL, gtk_notebook_get_current_page(w.notebook)); gtk_widget_grab_focus(GTK_WIDGET(t->view)); } /* misc functions to help initialization */ -static void window_setup() { +void window_setup() { term_data_id = g_quark_from_static_string("s"); @@ -446,6 +340,7 @@ int main (int argc, char* argv[]) { gtk_init (&argc, &argv); window_setup(); +GQuark term_data_id = 0; if (argc == 2) { load_uri(argv[1]); } gtk_main(); @@ -0,0 +1,17 @@ +void link_hover(WebKitWebView* page, const gchar* title, const gchar* link, gpointer data); +void load_uri(gchar *uri); +void tab_new(gboolean b); +void tab_zoom(gboolean b); +void tab_close(); +void tab_and_go(); +void tab_focus(GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data); +void tab_view_source(); +void tab_reload(); +void window_setup(); +void search(GtkEntry *entry, gboolean b); +void toggle(); +void show_search(gboolean b); +void focus_view(); +WebKitWebView * tab_new_requested(WebKitWebView *v, WebKitWebFrame *f); +void tab_switch(gboolean b); +gboolean cb_keypress(GtkWidget *widget, GdkEventKey *event); |