Compare commits

...

16 Commits

5 changed files with 269 additions and 283 deletions

View File

@ -63,14 +63,15 @@ You can see some screenshots in the [screenshots](./screenshots) folder.
### Cool things ### Cool things
I just found out that you can inspect a GTK application with the GTK explorer if you set a certain command-line variable. Try this with `make inspect`. - I just found out that you can inspect a GTK application with the GTK explorer if you set a certain command-line variable. Try this with `make inspect`.
- Static variables keep their value between invocations.
### Known bugs/gotchas. ### Known bugs/gotchas.
General: General:
- [ ] By default the searchbar is pretty gigantic. I've made this so because I'm a bit myopic, but also work with my laptop in a laptop stand. Anyways, if you are a more normal person you can change this in the style.css. - By default the searchbar is pretty gigantic. I've made this so because I'm a bit myopic, but also work with my laptop in a laptop stand. Anyways, if you are a more normal person you can change this in the style.css.
- [ ] The style.css usage isn't updated until installation. This is because by default rose uses the theme located in /usr/share/themes/rose/style.css, and that file isn't updated until make install. - The style.css usage isn't updated until installation. This is because by default rose uses the theme located in /usr/share/themes/rose/style.css, and that file isn't updated until make install.
About my own system: About my own system:

View File

@ -16,6 +16,23 @@
#define SEARCH "https://lite.duckduckgo.com/html/?q=%s" // "https://search.nunosempere.com/search?q=%s" #define SEARCH "https://lite.duckduckgo.com/html/?q=%s" // "https://search.nunosempere.com/search?q=%s"
#define HOME ROSE_HOMEPAGE ? "file:///home/loki/Documents/core/software/fresh/C/rose-browser/rosenrot/user-scripts/ubuntu-20.04/rose-images/rose-homepage.png" : "https://search.nunosempere.com/" #define HOME ROSE_HOMEPAGE ? "file:///home/loki/Documents/core/software/fresh/C/rose-browser/rosenrot/user-scripts/ubuntu-20.04/rose-images/rose-homepage.png" : "https://search.nunosempere.com/"
// Plugins
#define LIBRE_REDIRECT_ENABLED true
#define READABILITY_ENABLED true
#define CUSTOM_STYLE_ENABLED true
#define CUSTOM_USER_AGENT false
/*
To disable plugins:
1. set their corresponding variable to false
2. you could also look into this file at commit afe93518a for an approach using stand-in code.
3. recompile
To remove plugins completely;
1. Remove the corresponding code in this file by looking for the variables above.
2. Remove PLUGIN and $(PLUGIN) from the makefiel
3. Recompile
*/
// Webkit settings // Webkit settings
// See: https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html // See: https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html
#define WEBKIT_DEFAULT_SETTINGS \ #define WEBKIT_DEFAULT_SETTINGS \
@ -33,7 +50,7 @@
"service-worker-registrations-directory", CACHE_DIR "service-worker-registrations-directory", CACHE_DIR
// GTK // GTK
#define GTK "gtk-application-prefer-dark-theme", false, "gtk-enable-animations", false #define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", false, "gtk-enable-animations", false
#define KEY(x) GDK_KEY_##x #define KEY(x) GDK_KEY_##x
// Shortcuts // Shortcuts
@ -67,7 +84,7 @@ static struct {
unsigned mod; unsigned mod;
unsigned key; unsigned key;
func id; func id;
} keys[] = { } shortcut[] = {
{ CTRL, KEY(h), goback }, { CTRL, KEY(h), goback },
{ CTRL, KEY(j), goforward }, { CTRL, KEY(j), goforward },
{ CTRL, KEY(r), refresh }, { CTRL, KEY(r), refresh },

View File

@ -7,9 +7,10 @@
## C compiler ## C compiler
CC=gcc # gcc: more options. Also I don't know whether tcc has error messages/debug options. CC=gcc # gcc: more options. Also I don't know whether tcc has error messages/debug options.
## CC=tcc # tcc: much faster compilation
## other alternatives; clang, zig cc
WARNINGS=-Wall WARNINGS=-Wall
OPTIMIZED=-O3 #-Ofast OPTIMIZED=-O3 #-Ofast
## CC=tcc # tcc: much faster
## Main file ## Main file
SRC=rose.c SRC=rose.c
@ -30,11 +31,10 @@ LIBRE_REDIRECT=./plugins/libre_redirect/libre_redirect.c ./plugins/libre_redirec
CUSTOM_STYLES=./plugins/style/style.c CUSTOM_STYLES=./plugins/style/style.c
READABILITY=./plugins/readability/readability.c READABILITY=./plugins/readability/readability.c
SHORTCUTS=./plugins/shortcuts/shortcuts.c SHORTCUTS=./plugins/shortcuts/shortcuts.c
STAND_IN=./plugins/stand_in/stand_in.c # gives function definitions for the above, which do nothing STAND_IN=./plugins/stand_in/stand_in.c # gives function definitions for the above, which do nothing
PLUGS=$(LIBRE_REDIRECT) $(READABILITY) $(CUSTOM_STYLES) $(SHORTCUTS) PLUGINS=$(LIBRE_REDIRECT) $(READABILITY) $(CUSTOM_STYLES) $(SHORTCUTS)
# PLUGS=$(STAND_IN) # PLUGINS=$(STAND_IN)
# Note that if you want some plugins but not others, # Note that if you want some plugins but not others,
# You should edit the stand_in.c file # You should edit the stand_in.c file
@ -56,7 +56,7 @@ CURRENT_CACHE_DIR=/home/$(USER)/.cache/rose
DEFAULT_DIR=/home/loki/Documents/core/software/fresh/C/rose-browser/rosenrot DEFAULT_DIR=/home/loki/Documents/core/software/fresh/C/rose-browser/rosenrot
CURRENT_DIR=`pwd` CURRENT_DIR=`pwd`
build: $(SRC) $(PLUGS) $(CONFIG) build: $(SRC) $(PLUGINS) $(CONFIG)
# Recompute constants # Recompute constants
cd plugins/readability/ && sh recompute_READABILITY_N.sh cd plugins/readability/ && sh recompute_READABILITY_N.sh
cd plugins/style && sh recompute_STYLE_N.sh cd plugins/style && sh recompute_STYLE_N.sh
@ -70,10 +70,10 @@ build: $(SRC) $(PLUGS) $(CONFIG)
sed -i "s|$(DEFAULT_DIR)|$(CURRENT_DIR)|g" {} + sed -i "s|$(DEFAULT_DIR)|$(CURRENT_DIR)|g" {} +
# Compile rosenrot # Compile rosenrot
GIO_MODULE_DIR=/usr/lib/x86_64-linux-gnu/gio/modules/ GIO_MODULE_DIR=/usr/lib/x86_64-linux-gnu/gio/modules/
$(CC) $(WARNINGS) $(OPTIMIZED) $(DEBUG) $(INCS) $(PLUGS) $(SRC) -o rose $(LIBS) $(ADBLOCK) $(CC) $(WARNINGS) $(OPTIMIZED) $(DEBUG) $(INCS) $(PLUGINS) $(SRC) -o rose $(LIBS) $(ADBLOCK)
lint: lint:
clang-tidy $(SRC) $(PLUGS) -- -Wall -O3 `pkg-config --cflags 'webkit2gtk-4.0'` -o rose `pkg-config --libs 'webkit2gtk-4.0'` clang-tidy $(SRC) $(PLUGINS) -- -Wall -O3 `pkg-config --cflags 'webkit2gtk-4.0'` -o rose `pkg-config --libs 'webkit2gtk-4.0'`
inspect: build inspect: build
GTK_DEBUG=interactive ./rose GTK_DEBUG=interactive ./rose
@ -96,5 +96,5 @@ clean:
rm rose rm rose
rm $(CACHE_DIR) rm $(CACHE_DIR)
format: $(SRC) $(PLUGS) format: $(SRC) $(PLUGINS)
$(FORMATTER) $(SRC) $(PLUGS) $(rose.h) $(FORMATTER) $(SRC) $(PLUGINS) $(rose.h)

BIN
rose

Binary file not shown.

506
rose.c
View File

@ -1,42 +1,148 @@
#include <stdlib.h>
#include <string.h>
#include <webkit2/webkit2.h>
#include "config.h" #include "config.h"
#include "plugins/libre_redirect/libre_redirect.h" #include "plugins/libre_redirect/libre_redirect.h"
#include "plugins/readability/readability.h" #include "plugins/readability/readability.h"
#include "plugins/shortcuts/shortcuts.h" #include "plugins/shortcuts/shortcuts.h"
#include "plugins/style/style.h" #include "plugins/style/style.h"
#include <stdlib.h>
#include <string.h>
#include <webkit2/webkit2.h>
/* Global declarations */ /* Global declarations */
static GtkNotebook* notebook; static GtkNotebook* notebook;
static GtkWindow* window; static GtkWindow* window;
static struct {
GtkHeaderBar* widget;
GtkEntry* line;
GtkEntryBuffer* line_text;
enum { _SEARCH,
_FIND,
_HIDDEN } entry_mode;
} bar; // top bar.
static int num_tabs = 0;
// Search, find and url bar /* Utils */
static GtkHeaderBar* bar; WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
static GtkEntry* bar_line; // widget {
static GtkEntryBuffer* bar_line_text; return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(
static int bar_entry_mode; notebook, gtk_notebook_get_current_page(notebook)));
enum { _SEARCH, _FIND, _HIDDEN }; }
/* Plugins */ /* Load content*/
// #include "plugins/stand_in/stand_in.h" void load_uri(WebKitWebView* view, const char* uri)
int LIBRE_REDIRECT_ENABLED = true; {
int READABILITY_ENABLED = true; 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:")) {
int CUSTOM_STYLE_ENABLED = true; webkit_web_view_load_uri(view, uri);
int CUSTOM_USER_AGENT = false; } else {
int NUM_TABS = 0; // Check for shortcuts
// to enable plugins, int l = SHORTCUT_N + strlen(uri) + 1;
// 1. Enable them: char uri_expanded[l];
// - uncomment their #include statement str_init(uri_expanded, l);
// - set their variable to true int check = shortcut_expand(uri, uri_expanded);
// - in build.sh, uncomment: REQS= #./plugins/*/*.c if (check == 2) {
// 2. Remove stand_in code; webkit_web_view_load_uri(view, uri_expanded);
// - Add an #include "plugins/stand_in/stand_in.h" line, and modify } else {
// stand_in.c so as to include stand-in functions for the excluded plugin // Feed into search engine.
// - In the make file, include the stand_in.c file and exclude the char tmp[strlen(uri) + strlen(SEARCH)];
// relevant plugin snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
}
}
}
WebKitWebView* webview_new() /* Deal with new load or changed load */
void redirect_if_annoying(WebKitWebView* view, const char* uri)
{
if (LIBRE_REDIRECT_ENABLED) {
int l = LIBRE_N + strlen(uri) + 1;
char uri_filtered[l];
str_init(uri_filtered, l);
int check = libre_redirect(uri, uri_filtered);
if (check == 2) {
webkit_web_view_load_uri(view, uri_filtered);
}
}
}
void set_custom_style(WebKitWebView* view)
{
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(view, style_js,
NULL, NULL, NULL);
free(style_js);
}
}
void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
GtkNotebook* notebook)
{
switch (load_event) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/
case WEBKIT_LOAD_STARTED:
set_custom_style(self);
redirect_if_annoying(self, webkit_web_view_get_uri(self));
break;
case WEBKIT_LOAD_REDIRECTED:
redirect_if_annoying(self, webkit_web_view_get_uri(self));
break;
case WEBKIT_LOAD_COMMITTED:
redirect_if_annoying(self, webkit_web_view_get_uri(self));
set_custom_style(self);
break;
case WEBKIT_LOAD_FINISHED: {
/* Add gtk tab title */
const char* webpage_title = webkit_web_view_get_title(self);
const int max_length = 25;
char tab_title[max_length + 1];
if (webpage_title != NULL) {
for (int i = 0; i < (max_length); i++) {
tab_title[i] = webpage_title[i];
if (webpage_title[i] == '\0') {
break;
}
}
tab_title[max_length] = '\0';
}
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
webpage_title == NULL ? "" : tab_title);
// gtk_widget_hide(GTK_WIDGET(bar));
}
}
}
/* Create new tabs */
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri);
// handle_signal_create_new_tab is bound to a signal inside notebook_create_new_tab
// and itself calls notebook_create_new_tab
// therefore we need to do a forward declaration for one of them.
GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
WebKitNavigationAction* navigation_action,
GtkNotebook* 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);
return NULL;
} else {
webkit_web_view_run_javascript(self,
"alert('Too many tabs, not opening a new one')", NULL, NULL, NULL);
return 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.
*/
}
WebKitWebView* create_new_webview()
{ {
char* style; char* style;
WebKitSettings* settings; WebKitSettings* settings;
@ -75,233 +181,119 @@ WebKitWebView* webview_new()
web_context, "user-content-manager", contentmanager, web_context, "user-content-manager", contentmanager,
NULL); NULL);
} }
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{ {
return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page( if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) {
notebook, gtk_notebook_get_current_page(notebook)));
}
void load_uri(WebKitWebView* view, const char* uri)
{
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 {
// 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 redirect_if_annoying(WebKitWebView* view, const char* uri)
{
int l = LIBRE_N + strlen(uri) + 1;
char uri_filtered[l];
str_init(uri_filtered, l);
int check = libre_redirect(uri, uri_filtered);
if (check == 2) {
webkit_web_view_load_uri(view, uri_filtered);
}
}
void load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
GtkNotebook* notebook)
{
switch (load_event) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/
case WEBKIT_LOAD_STARTED:
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
break;
case WEBKIT_LOAD_REDIRECTED:
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
break;
case WEBKIT_LOAD_COMMITTED:
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
break;
case WEBKIT_LOAD_FINISHED: {
/* Add gtk tab title */
const char* webpage_title = webkit_web_view_get_title(self);
const int max_length = 25;
char tab_title[max_length + 1];
if (webpage_title != NULL) {
for (int i = 0; i < (max_length); i++) {
tab_title[i] = webpage_title[i];
if (webpage_title[i] == '\0') {
break;
}
}
tab_title[max_length] = '\0';
}
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
webpage_title == NULL ? "" : tab_title);
// gtk_widget_hide(GTK_WIDGET(bar));
}
}
}
void notebook_append(GtkNotebook* notebook, const char* uri);
/* notebook_append calls handle_create, but handle_create also calls
* notebook_append. Therefore we need to declare notebook_append, so that
* handle_create_new_tab knows its type.
*/
GtkWidget* handle_create_new_tab(WebKitWebView* self,
WebKitNavigationAction* navigation_action,
GtkNotebook* 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_append(notebook, uri);
gtk_notebook_set_show_tabs(notebook, true);
return NULL;
} else {
webkit_web_view_run_javascript(notebook_get_webview(notebook),
"alert('Too many tabs, not opening a new one')", NULL, NULL, NULL);
return 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_append
* or duplicating its contents, for unclear gain.
*/
}
void notebook_append(GtkNotebook* notebook, const char* uri)
{
if (NUM_TABS < MAX_NUM_TABS || NUM_TABS == 0) {
GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window)); GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window));
GdkVisual* rgba_visual = gdk_screen_get_rgba_visual(screen); GdkVisual* rgba_visual = gdk_screen_get_rgba_visual(screen);
GdkRGBA rgba; GdkRGBA rgba;
gdk_rgba_parse(&rgba, BG_COLOR); gdk_rgba_parse(&rgba, BG_COLOR);
WebKitWebView* view = webview_new(); WebKitWebView* view = create_new_webview();
gtk_widget_set_visual(GTK_WIDGET(window), rgba_visual); gtk_widget_set_visual(GTK_WIDGET(window), rgba_visual);
g_signal_connect(view, "load_changed", G_CALLBACK(load_changed), notebook); g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook);
g_signal_connect(view, "create", G_CALLBACK(handle_create_new_tab), notebook); g_signal_connect(view, "create", G_CALLBACK(handle_signal_create_new_tab), notebook);
int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL); int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL);
gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true); gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true);
gtk_widget_show_all(GTK_WIDGET(window)); gtk_widget_show_all(GTK_WIDGET(window));
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar.widget));
webkit_web_view_set_background_color(view, &rgba); webkit_web_view_set_background_color(view, &rgba);
load_uri(view, (uri) ? uri : HOME); load_uri(view, (uri) ? uri : HOME);
if (CUSTOM_STYLE_ENABLED) { set_custom_style(view);
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
gtk_notebook_set_current_page(notebook, n); gtk_notebook_set_current_page(notebook, n);
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-"); gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-");
webkit_web_view_set_zoom_level(view, ZOOM); webkit_web_view_set_zoom_level(view, ZOOM);
NUM_TABS += 1; num_tabs += 1;
} else { } else {
webkit_web_view_run_javascript(notebook_get_webview(notebook), webkit_web_view_run_javascript(notebook_get_webview(notebook),
"alert('Too many tabs, not opening a new one')", NULL, NULL, NULL); "alert('Too many tabs, not opening a new one')", NULL, NULL, NULL);
} }
} }
/* Top bar */
void show_bar(GtkNotebook* notebook) void show_bar(GtkNotebook* notebook)
{ {
if (bar_entry_mode == _SEARCH) { if (bar.entry_mode == _SEARCH) {
const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
gtk_entry_set_placeholder_text(bar_line, "Search"); gtk_entry_set_placeholder_text(bar.line, "Search");
gtk_entry_buffer_set_text(bar_line_text, url, strlen(url)); gtk_entry_buffer_set_text(bar.line_text, url, strlen(url));
gtk_widget_show(GTK_WIDGET(bar)); gtk_widget_show(GTK_WIDGET(bar.widget));
gtk_window_set_focus(window, GTK_WIDGET(bar_line)); gtk_window_set_focus(window, GTK_WIDGET(bar.line));
} else if (bar_entry_mode == _HIDDEN) { } else if (bar.entry_mode == _HIDDEN) {
gtk_widget_hide(GTK_WIDGET(bar)); gtk_widget_hide(GTK_WIDGET(bar.widget));
} else { } else {
const char* search_text = webkit_find_controller_get_search_text( const char* search_text = webkit_find_controller_get_search_text(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
if (search_text != NULL) if (search_text != NULL)
gtk_entry_buffer_set_text(bar_line_text, search_text, strlen(search_text)); gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text));
gtk_entry_set_placeholder_text(bar_line, "Find"); gtk_entry_set_placeholder_text(bar.line, "Find");
gtk_widget_show(GTK_WIDGET(bar)); gtk_widget_show(GTK_WIDGET(bar.widget));
gtk_window_set_focus(window, GTK_WIDGET(bar_line)); gtk_window_set_focus(window, GTK_WIDGET(bar.line));
} }
} }
// Handle what happens when the user is on the bar and presses enter
void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
{
if (bar.entry_mode == _SEARCH)
load_uri(notebook_get_webview(notebook),
gtk_entry_buffer_get_text(bar.line_text));
else if (bar.entry_mode == _FIND)
webkit_find_controller_search(
webkit_web_view_get_find_controller(notebook_get_webview(notebook)),
gtk_entry_buffer_get_text(bar.line_text),
WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND,
G_MAXUINT);
int handle_key(func id, GtkNotebook* notebook) gtk_widget_hide(GTK_WIDGET(bar.widget));
}
/* Handle shortcuts */
// Act when a particular shortcut is detected
int handle_shortcut(func id, GtkNotebook* notebook)
{ {
static double zoom = ZOOM; static double zoom = ZOOM;
static bool is_fullscreen = 0; static bool is_fullscreen = 0;
WebKitWebView* view = notebook_get_webview(notebook);
switch (id) { switch (id) {
case goback: case goback:
webkit_web_view_go_back(notebook_get_webview(notebook)); webkit_web_view_go_back(view);
break; break;
case goforward: case goforward:
webkit_web_view_go_forward(notebook_get_webview(notebook)); webkit_web_view_go_forward(view);
break; break;
case refresh: case refresh:
webkit_web_view_reload(notebook_get_webview(notebook)); webkit_web_view_reload(view);
break; break;
case refresh_force: case refresh_force:
webkit_web_view_reload_bypass_cache(notebook_get_webview(notebook)); webkit_web_view_reload_bypass_cache(view);
break; break;
case back_to_home: case back_to_home:
load_uri(notebook_get_webview(notebook), HOME); load_uri(view, HOME);
break; break;
case zoomin: case zoomin:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), webkit_web_view_set_zoom_level(view,
(zoom += ZOOM_VAL)); (zoom += ZOOM_VAL));
break; break;
case zoomout: case zoomout:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), webkit_web_view_set_zoom_level(view,
(zoom -= ZOOM_VAL)); (zoom -= ZOOM_VAL));
break; break;
case zoom_reset: case zoom_reset:
webkit_web_view_set_zoom_level(notebook_get_webview(notebook), webkit_web_view_set_zoom_level(view,
(zoom = ZOOM)); (zoom = ZOOM));
break; break;
@ -322,7 +314,7 @@ int handle_key(func id, GtkNotebook* notebook)
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
NUM_TABS -= 1; num_tabs -= 1;
switch (gtk_notebook_get_n_pages(notebook)) { switch (gtk_notebook_get_n_pages(notebook)) {
case 0: case 0:
@ -345,34 +337,34 @@ int handle_key(func id, GtkNotebook* notebook)
break; break;
case show_searchbar: case show_searchbar:
bar_entry_mode = _SEARCH; bar.entry_mode = _SEARCH;
show_bar(notebook); show_bar(notebook);
break; break;
case show_finder: case show_finder:
bar_entry_mode = _FIND; bar.entry_mode = _FIND;
show_bar(notebook); show_bar(notebook);
break; break;
case finder_next: case finder_next:
webkit_find_controller_search_next( webkit_find_controller_search_next(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(view));
break; break;
case finder_prev: case finder_prev:
webkit_find_controller_search_previous( webkit_find_controller_search_previous(
webkit_web_view_get_find_controller(notebook_get_webview(notebook))); webkit_web_view_get_find_controller(view));
break; break;
case new_tab: case new_tab:
notebook_append(notebook, NULL); notebook_create_new_tab(notebook, NULL);
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
bar_entry_mode = _SEARCH; bar.entry_mode = _SEARCH;
show_bar(notebook); show_bar(notebook);
break; break;
case hide_bar: case hide_bar:
bar_entry_mode = _HIDDEN; bar.entry_mode = _HIDDEN;
show_bar(notebook); show_bar(notebook);
break; break;
@ -380,7 +372,7 @@ int handle_key(func id, GtkNotebook* notebook)
if (READABILITY_ENABLED) { if (READABILITY_ENABLED) {
char* readability_js = malloc(READABILITY_N + 1); char* readability_js = malloc(READABILITY_N + 1);
read_readability_js(readability_js); read_readability_js(readability_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), webkit_web_view_run_javascript(view,
readability_js, NULL, NULL, NULL); readability_js, NULL, NULL, NULL);
free(readability_js); free(readability_js);
} }
@ -390,97 +382,73 @@ int handle_key(func id, GtkNotebook* notebook)
return 1; return 1;
} }
// Listen to key presses and call shortcuts if needed.
int keypress(void* self, GdkEvent* e, GtkNotebook* notebook) int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
{ {
(void)self; (void)self;
for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++)
if ((e->key.state == keys[i].mod || keys[i].mod == 0x0) && e->key.keyval == keys[i].key) if ((event->key.state == shortcut[i].mod || shortcut[i].mod == 0x0) && event->key.keyval == shortcut[i].key)
return handle_key(keys[i].id, notebook); return handle_shortcut(shortcut[i].id, notebook);
/* /*
printf("Event type: %d\n", e->type); If I wanted to bind button presses, like the extra button in the mouse,
printf("Keyval: %d\n", e->key.keyval); I would have to bind the button-press-event signal instead.
// Note: if I wanted to bind button presses, like the extra button in the mouse, Some links in case I go down that road:
// I would have to bind the button-press-event signal instead. - <https://docs.gtk.org/gtk3/signal.Widget.button-press-event.html>
// Some links in case I go down that road: <https://docs.gtk.org/gtk3/signal.Widget.button-press-event.html> - <https://docs.gtk.org/gdk3/union.Event.html>
// https://docs.gtk.org/gdk3/union.Event.html - https://docs.gtk.org/gdk3/struct.EventButton.html
// https://docs.gtk.org/gdk3/struct.EventButton.html
*/ */
return 0; return 0;
} }
void search_activate(GtkEntry* self, GtkNotebook* notebook)
{
if (bar_entry_mode == _SEARCH)
load_uri(notebook_get_webview(notebook),
gtk_entry_buffer_get_text(bar_line_text));
else if (bar_entry_mode == _FIND)
webkit_find_controller_search(
webkit_web_view_get_find_controller(notebook_get_webview(notebook)),
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));
}
void window_init(GtkNotebook* notebook)
{
GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css",
NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(css), 800);
gtk_entry_buffer_new("", 0);
gtk_entry_set_alignment(bar_line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar_line), BAR_SIZE, -1);
gtk_header_bar_set_custom_title(bar, GTK_WIDGET(bar_line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar));
g_signal_connect(bar_line, "activate", G_CALLBACK(search_activate), notebook);
g_signal_connect(window, "key-press-event", G_CALLBACK(keypress), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
}
void notebook_init(GtkNotebook* notebook, const char* uri)
{
gtk_notebook_set_show_border(notebook, false);
gtk_notebook_set_show_tabs(notebook, false);
notebook_append(notebook, uri);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// <https://docs.gtk.org/gtk3/func.init.html> /* Initialize GTK in general */
gtk_init(NULL, NULL); gtk_init(NULL, NULL); // <https://docs.gtk.org/gtk3/func.init.html>
g_object_set(gtk_settings_get_default(), GTK_SETTINGS_CONFIG_H, NULL); // <https://docs.gtk.org/gobject/method.Object.set.html>
GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css", NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800);
// Define GTK entities. These are declared globally /* Initialize GTK objects. These are declared as static globals */
window = GTK_WINDOW(gtk_window_new(0)); // Notebook
bar = GTK_HEADER_BAR(gtk_header_bar_new());
bar_line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
bar_line = GTK_ENTRY(gtk_entry_new_with_buffer(bar_line_text));
gtk_window_set_default_size(window, WIDTH, HEIGHT);
notebook = GTK_NOTEBOOK(gtk_notebook_new()); notebook = GTK_NOTEBOOK(gtk_notebook_new());
window_init(notebook); gtk_notebook_set_show_tabs(notebook, false);
gtk_notebook_set_show_border(notebook, false);
// Initialize with first uri // Window
char* first_uri = argc > 1 ? argv[1] : HOME; window = GTK_WINDOW(gtk_window_new(0));
notebook_init(notebook, first_uri); gtk_window_set_default_size(window, WIDTH, HEIGHT);
g_object_set(gtk_settings_get_default(), GTK, NULL); g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
// More GTK stuff
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook));
gtk_widget_show_all(GTK_WIDGET(window));
gtk_widget_hide(GTK_WIDGET(bar));
// Deal with more uris, if this is necessary. // Bar
bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text));
gtk_entry_set_alignment(bar.line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
bar.widget = GTK_HEADER_BAR(gtk_header_bar_new());
gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget));
/* Load first tab */
char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri);
/* Show to user */
gtk_widget_show_all(GTK_WIDGET(window));
gtk_widget_hide(GTK_WIDGET(bar.widget));
/* Deal with more tabs */
if (argc > 2) { if (argc > 2) {
gtk_notebook_set_show_tabs(notebook, true); gtk_notebook_set_show_tabs(notebook, true);
for (int i = 2; i < argc; i++) { for (int i = 2; i < argc; i++) {
notebook_append(notebook, argv[i]); notebook_create_new_tab(notebook, argv[i]);
} }
} }
gtk_main(); gtk_main();
// this point is never reached, since gtk_main(); never exits.
} }