diff --git a/makefile b/makefile index a57dde7..44bd73b 100644 --- a/makefile +++ b/makefile @@ -13,19 +13,12 @@ INCS_3=`pkg-config --cflags ${DEPS_3}` LIBS_3=`pkg-config --libs ${DEPS_3}` # Dependencies for WebkitGTK6/GTK4 -SRC_4=rosenrot4.c +SRC_4=rosenrot4_beta.c DEPS_4='webkitgtk-6.0' INCS_4=`pkg-config --cflags ${DEPS_4}` `pkg-config --cflags gtk4` LIBS_4=`pkg-config --libs ${DEPS_4}` `pkg-config --libs gtk4` # DEPRECATION_FLAGS=-DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -# Dependencies starting from scratchpad -SRC_4_greenfield=rosenrot4_greenfield_minimal.c -DEPS_4_greenfield='webkitgtk-6.0' -INCS_4_greenfield=`pkg-config --cflags ${DEPS_4_greenfield}` `pkg-config --cflags gtk4` -LIBS_4_greenfield=`pkg-config --libs ${DEPS_4_greenfield}` `pkg-config --libs gtk4` -# DEPRECATION_FLAGS=-DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED - # User config CONFIG=config.h @@ -56,10 +49,6 @@ build4: $(SRC_4) $(PLUGINS) $(CONFIG) constants user_cache $(CC) $(STD) $(WARNINGS) $(DEPRECATION_FLAGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS_4) $(PLUGINS) $(SRC_4) -o rosenrot $(LIBS_4) $(ADBLOCK) @echo -build4_greenfield: $(SRC_4_greenfield) $(PLUGINS) $(CONFIG) constants user_cache - $(CC) $(STD) $(WARNINGS) $(DEPRECATION_FLAGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS_4_greenfield) $(PLUGINS) $(SRC_4_greenfield) -o rosenrot $(LIBS_4_greenfield) $(ADBLOCK) - @echo - diagnose: G_ENABLE_DIAGNOSTIC=1 ./rosenrot diff --git a/rosenrot4_beta.c b/rosenrot4_beta.c index a0c74d9..fe4c56d 100644 --- a/rosenrot4_beta.c +++ b/rosenrot4_beta.c @@ -6,6 +6,15 @@ #include "plugins/plugins.h" #include +#define NOTNULL(x) \ + do { \ + if (x == NULL) { \ + printf("\nNull found"); \ + printf("@ %s (%d): ", __FILE__, __LINE__); \ + exit(0); \ + } \ + } while (0) + static GtkNotebook* notebook; static GtkWindow* window; typedef enum { _SEARCH, @@ -17,15 +26,12 @@ static struct { GtkEntryBuffer* line_text; Bar_entry_mode entry_mode; } bar; +static int num_tabs = 0; +static int custom_style_enabled = 1; -#define NOTNULL(x) \ - do { \ - if (x == NULL) { \ - printf("\nNull found"); \ - printf("@ %s (%d): ", __FILE__, __LINE__); \ - exit(0); \ - } \ - } while (0) +/* Forward declarations */ +void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode); +void notebook_create_new_tab(GtkNotebook* notebook, const char* uri); /* Utils */ @@ -36,6 +42,49 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook) return view; } +/* Load content */ + +void load_uri(WebKitWebView* view, const char* uri) +{ + + NOTNULL(view); + if (strlen(uri) == 0) { + webkit_web_view_load_uri(view, ""); + toggle_bar(notebook, _SEARCH); + } else if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) { + webkit_web_view_load_uri(view, uri); + } else if (strstr(uri, ".com") || strstr(uri, ".org")) { + char tmp[strlen("https://") + strlen(uri) + 1]; + snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); + webkit_web_view_load_uri(view, tmp); + } else { + // Check for shortcuts + int l = SHORTCUT_N + strlen(uri) + 1; + char uri_expanded[l]; + str_init(uri_expanded, l); + int check = shortcut_expand(uri, uri_expanded); + if (check == 2) { + webkit_web_view_load_uri(view, uri_expanded); + } else { + // Feed into search engine. + char tmp[strlen(uri) + strlen(SEARCH)]; + snprintf(tmp, sizeof(tmp), SEARCH, uri); + webkit_web_view_load_uri(view, tmp); + } + } +} + +void set_custom_style(WebKitWebView* view) +{ + NOTNULL(view); + if (custom_style_enabled) { + char* style_js = malloc(STYLE_N + 1); + read_style_js(style_js); + webkit_web_view_evaluate_javascript(view, style_js, -1, NULL, "rosenrot-style-plugin", NULL, NULL, NULL); + free(style_js); + } +} + /* Handle shortcuts */ int handle_shortcut(func id) { @@ -58,26 +107,7 @@ int handle_shortcut(func id) /* Listen to keypresses */ -static gboolean handle_signal_keypress(GtkWidget* w, - guint keyval, - guint keycode, - GdkModifierType state, - GtkEventControllerKey* event_controller_keypress) -{ - - fprintf(stdout, "New keypress!\n"); - if (1) { - printf("Keypress state: %d\n", state); - printf("Keypress value: %d\n", keyval); - } - for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++){ - if ((state & shortcut[i].mod || shortcut[i].mod == 0x0) && keyval == shortcut[i].key) { - return handle_shortcut(shortcut[i].id); - } - } - return 0; -} -static int handle_signal_keypress2(void *self, int keyval, int keycode, +static int handle_signal_keypress(void *self, int keyval, int keycode, GdkModifierType state, void *controller) { // (void) self, (void) keycode, (void) controller; @@ -104,13 +134,39 @@ WebKitWebView* create_new_webview() WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, NULL); GtkEventController *event_controller = gtk_event_controller_key_new(); - g_signal_connect(event_controller, "key-pressed", G_CALLBACK(handle_signal_keypress2), NULL); + g_signal_connect(event_controller, "key-pressed", G_CALLBACK(handle_signal_keypress), NULL); gtk_widget_add_controller(GTK_WIDGET(view), event_controller); NOTNULL(view); return view; } -void notebook_create_first_tab(GtkNotebook* notebook, const char* uri) +GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, + WebKitNavigationAction* navigation_action, + GtkNotebook* notebook) +{ + NOTNULL(self); + NOTNULL(notebook); + if (num_tabs < MAX_NUM_TABS || num_tabs == 0) { + WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action); + const char* uri = webkit_uri_request_get_uri(uri_request); + printf("Creating new window: %s\n", uri); + notebook_create_new_tab(notebook, uri); + gtk_notebook_set_show_tabs(notebook, true); + } else { + webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); + } + return GTK_WIDGET(self); // NULL; + /* + WebKitGTK documentation recommends returning the new webview. + I imagine that this might allow e.g., to go back in a new tab + or generally to keep track of history. + However, this would require either modifying notebook_create_new_tab + or duplicating its contents, for unclear gain. + */ +} + + +void notebook_create_new_tab(GtkNotebook* notebook, const char* uri) { WebKitWebView* view = create_new_webview(); @@ -124,6 +180,56 @@ void notebook_create_first_tab(GtkNotebook* notebook, const char* uri) gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-"); } +/* Top bar */ +void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode) +{ + NOTNULL(notebook); + NOTNULL(window); + bar.entry_mode = mode; + switch (bar.entry_mode) { + case _SEARCH: { + const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); + gtk_entry_set_placeholder_text(bar.line, "Search"); + gtk_entry_buffer_set_text(bar.line_text, url, strlen(url)); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 1); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + break; + } + case _FIND: { + const char* search_text = webkit_find_controller_get_search_text( + webkit_web_view_get_find_controller(notebook_get_webview(notebook))); + + if (search_text != NULL) + gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text)); + + gtk_entry_set_placeholder_text(bar.line, "Find"); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 1); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + break; + } + case _HIDDEN: + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); + } +} + +// Handle what happens when the user is on the bar and presses enter +void handle_signal_bar_press_enter(void* data) +{ + WebKitWebView* view = notebook_get_webview(notebook); + NOTNULL(notebook); + NOTNULL(view); + if (bar.entry_mode == _SEARCH) + load_uri(view, gtk_entry_buffer_get_text(bar.line_text)); + else if (bar.entry_mode == _FIND) + webkit_find_controller_search( + webkit_web_view_get_find_controller(view), + gtk_entry_buffer_get_text(bar.line_text), + WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, + G_MAXUINT); + + gtk_widget_hide(GTK_WIDGET(bar.widget)); +} + int main(int argc, char** argv) { /* Initialize GTK in general */ @@ -152,7 +258,7 @@ int main(int argc, char** argv) // g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook); GtkEventController *event_controller = gtk_event_controller_key_new(); - g_signal_connect(event_controller, "key-pressed", G_CALLBACK(handle_signal_keypress2), NULL); + g_signal_connect(event_controller, "key-pressed", G_CALLBACK(handle_signal_keypress), NULL); gtk_widget_add_controller(GTK_WIDGET(window), event_controller); // GtkEventController* event_controller_keypress = gtk_event_controller_key_new(); // g_signal_connect_object(event_controller_keypress, "key-pressed", G_CALLBACK(handle_signal_keypress), notebook, G_CONNECT_DEFAULT); @@ -162,7 +268,7 @@ int main(int argc, char** argv) gtk_window_present(window); char* first_uri = argc > 1 ? argv[1] : HOME; - notebook_create_first_tab(notebook, first_uri); + notebook_create_new_tab(notebook, first_uri); /* Show to user */ gtk_widget_set_visible(GTK_WIDGET(window), 1); diff --git a/rosenrot4_greenfield.c b/rosenrot4_greenfield.c index 4b50bbb..e8da29d 100644 --- a/rosenrot4_greenfield.c +++ b/rosenrot4_greenfield.c @@ -30,8 +30,6 @@ static int custom_style_enabled = 1; } while (0) -/* Event controllers */ - /* Forward declarations */ void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode); void notebook_create_new_tab(GtkNotebook* notebook, const char* uri);