Compare commits

..

No commits in common. "c3ac0c0333755d33197b3793d535b499124411c9" and "db9d3e66f2d44fd58b2e6761377a0eac432d164b" have entirely different histories.

7 changed files with 286 additions and 286 deletions

View File

@ -10,7 +10,6 @@
- See whether it even exists at all
- [ ] Finish cleaning up GTK4 version
- [ ] Think about best way of having GTK4 version alongside
- [ ] Shortcut to resize window
# Previously done

View File

@ -1,23 +1,22 @@
#include <stdbool.h>
// #include <gdk/gdk.h> // <gdk/gdkenums.h>, <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
// #include "/usr/include/gtk-4.0/gtk/gtk.h"
/* Key user config */
#define HEIGHT 1080
#define FULL_WIDTH 1920
#define WIDTH FULL_WIDTH
#define BAR_WIDTH FULL_WIDTH/2
// Key user config
#define WIDTH 1920 // 960 for half-width, 1920 for full width
#define HEIGHT_GTK3 990 // GTK4 and GTK3 interpret this differently. In GTK4, this includes the height of the top bar
#define HEIGHT_GTK4 1080 // GTK4 and GTK3 interpret this differently. In GTK4, this includes the height of the top bar
#define BAR_SIZE 960
/* More user config */
#define ZOOM_START_LEVEL 1.8
// More user config
#define ZOOM_START_LEVEL 2
#define ZOOM_STEPSIZE .1
#define MAX_NUM_TABS 8 // 0 or false for unlimited tabs
#define SEARCH "https://search.brave.com/search?q=%s"
#define HOME "https://search.brave.com/search"
// #define SEARCH "https://search.nunosempere.com/search?q=%s"
// #define SEARCH "https://lite.duckduckgo.com/html/?q=%s"
// #define HOME "file:///opt/rosenrot/flower-imgs/rose-homepage.png"
#define MAX_NUM_TABS 8 // 0/false for unlimited tabs
#define SEARCH "https://search.brave.com/search?q=%s" // "https://search.nunosempere.com/search?q=%s", "https://lite.duckduckgo.com/html/?q=%s"
#define HOME "https://search.brave.com/search" // "file:///opt/rose/homepage.png", ""
/* Plugins */
// Plugins
#define LIBRE_REDIRECT_ENABLED true
#define READABILITY_ENABLED true
#define CUSTOM_USER_AGENT false
@ -27,45 +26,33 @@ To disable plugins:
2. recompile
To remove plugins completely;
1. Remove the corresponding code in rosenrot.c by looking for the variables above, as well as custom_style_enabled
1. Remove the corresponding code in rosenrot.c by looking for the variables above.
2. Remove PLUGIN and $(PLUGIN) from the makefile
3. Recompile
You could also look into commit afe93518a for an approach using stand-in code.
*/
/* Webkit */
// Webkit settings
// https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html
#define WEBKIT_DEFAULT_SETTINGS \
"enable-back-forward-navigation-gestures", true, \
"enable-developer-extras", true, \
"enable-back-forward-navigation-gestures", true, "enable-developer-extras", true, \
"enable-smooth-scrolling", false, \
"default-charset", "utf-8"
#define DATA_DIR "/home/nuno/.cache/rosenrot"
#define DATA_MANAGER_OPTS "base-cache-directory", DATA_DIR, "base-data-directory", DATA_DIR
#define NETWORK_SESSION_OPTS DATA_DIR, DATA_DIR
/* GTK */
// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gdk/gdkkeysyms.h
// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gdk/gdkenums.h
// GTK
#define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", false, "gtk-enable-animations", false
// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gdk/gdkkeysyms.h
#define KEY(x) GDK_KEY_##x
// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gdk/gdkenums.h
#define SFT 1 << 0
#define CTRL 1 << 2
#define ALT 1 << 3
/* Misc helpers */
#define ABORT_REQUEST_ON_CURRENT_TAB NULL
#define NULLCHECK(x) \
do { \
if (x == NULL) { \
printf("\nNULL check not passed"); \
printf("@ %s (%d): ", __FILE__, __LINE__); \
exit(0); \
} \
} while (0)
/* Shortcuts */
// Shortcuts
typedef enum {
goback,
goforward,
@ -93,9 +80,6 @@ typedef enum {
finder_next,
finder_prev,
halve_window,
rebig_window,
prettify,
} func;
@ -119,11 +103,10 @@ static struct {
{ CTRL, KEY(minus), zoomout },
{ CTRL, KEY(0), zoom_reset },
{ CTRL | SFT, KEY(KP_Page_Up), prev_tab }, // use SFT just to show one can
{ CTRL | SFT, KEY(KP_Page_Down), next_tab },
{ CTRL | SFT, KEY(Page_Up), prev_tab },
// working hypothesis: Page_UP vs KP_Page_Up might depend on whether the user has a numpad
{ CTRL | SFT, KEY(Page_Down), next_tab },
{ CTRL, KEY(KP_Page_Up), prev_tab },
{ CTRL, KEY(KP_Page_Down), next_tab },
{ CTRL, KEY(Page_Up), prev_tab }, // working hypothesis: Page_UP vs KP_Page_Up might depend on whether the user has a numpad
{ CTRL, KEY(Page_Down), next_tab },
{ CTRL, KEY(t), new_tab },
{ CTRL, KEY(w), close_tab },
@ -132,7 +115,28 @@ static struct {
{ CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next },
{ CTRL, KEY(N), finder_prev },
{ CTRL, KEY(Left), halve_window },
{ CTRL, KEY(Right), rebig_window },
{ CTRL, KEY(p), prettify }
};
/* Old controls: {
{ CTRL, KEY(h), goback },
{ CTRL, KEY(l), goforward },
{ CTRL, KEY(r), refresh },
{ CTRL | SFT, KEY(R), refresh_force },
{ CTRL | SFT, KEY(H), back_to_home },
{ CTRL, KEY(equal), zoomin },
{ CTRL, KEY(minus), zoomout },
{ CTRL, KEY(0), zoom_reset },
{ ALT, KEY(h), prev_tab },
{ CTRL, KEY(k), hide_searchbar },
{ ALT, KEY(l), next_tab },
{ CTRL, KEY(w), close_tab },
{ 0x0, KEY(F11), toggle_fullscreen },
{ CTRL, KEY(e), show_searchbar },
{ CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next },
{ CTRL | SFT, KEY(N), finder_prev },
{ CTRL, KEY(p), prettify }
};
*/

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STYLE_N 8049 + 1000
#define STYLE_N 7877 + 1000
void read_style_js(char* string)
{

View File

@ -1,5 +1,5 @@
#pragma once
#define STYLE_N 8049 + 1000
#define STYLE_N 7877 + 1000
void read_style_js(char* string);

View File

@ -6,8 +6,8 @@
var styles = null;
// console.log(document.domain);
switch (document.domain) {
case "forum.effectivealtruism.org":
styles = `
case "forum.effectivealtruism.org":
styles = `
/*
.Layout-main {
margin-left: 100px;
@ -25,16 +25,16 @@ switch (document.domain) {
}
*/
`;
break;
case "nationstates.net":
styles = `
break;
case "nationstates.net":
styles = `
.adidentifier {
display: none;
}
`;
break;
case "mail.proton.me":
styles = `
break;
case "mail.proton.me":
styles = `
/*
.item-container-row.read, .item-container.read {
background-color: white;
@ -48,24 +48,24 @@ switch (document.domain) {
zoom: 0.625 !important;
*/
`;
break;
case "forum.nunosempere.com":
styles = `
break;
case "forum.nunosempere.com":
styles = `
body {
zoom: 0.625 !important;
}
`;
break;
case "search.brave.com":
styles = `
break;
case "search.brave.com":
styles = `
.download-button,
a[href^="https://brave.com/download/"] {
display: none !important;
}
`;
break;
case "search.nunosempere.com":
styles = `
break;
case "search.nunosempere.com":
styles = `
/*
body {
zoom: 1.8;
@ -76,11 +76,11 @@ switch (document.domain) {
display: none;
}
`;
break;
case "reddit.com":
// fallthrough
case "old.reddit.com":
styles = `
break;
case "reddit.com":
// fallthrough
case "old.reddit.com":
styles = `
/* kill sidebar ads */
.ad-container,
a[href^="https://alb.reddit.com"]
@ -98,10 +98,10 @@ switch (document.domain) {
display: none !important;
}
`;
break;
case "twitter.com":
case "x.com":
styles = `
break;
case "twitter.com":
case "x.com":
styles = `
/* hide promoted tweets */
:has(meta[property="og:site_name"][content="Twitter"])
[data-testid="cellInnerDiv"]:has(svg + [dir="auto"]) {
@ -182,25 +182,25 @@ switch (document.domain) {
background: white !important;
}
`;
break;
default:
console.log(`Domain: ${document.domain}`);
console.log("No custom style");
break;
default:
console.log(`Domain: ${document.domain}`);
console.log("No custom style");
}
if (styles != null) {
var styleSheet = document.createElement("style");
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
console.log("Style changed");
var styleSheet = document.createElement("style");
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
console.log("Style changed");
}
// Extra: Replace default alert with new function
// whose style can be changed!
window.alert = (message) => {
let alertDiv = document.getElementById("customAlert");
if (!alertDiv) {
const html = `
let alertDiv = document.getElementById("customAlert");
if (!alertDiv) {
const html = `
<div id="customAlert" class="custom-alert">
<div class="custom-alert-content">
<p id="alertMessage"></p>
@ -232,68 +232,68 @@ window.alert = (message) => {
}
</style>
`;
document.body.insertAdjacentHTML("beforeend", html);
alertDiv = document.getElementById("customAlert");
document.getElementById("alertOkButton").onclick = () => {
alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert);
};
}
document.body.insertAdjacentHTML("beforeend", html);
alertDiv = document.getElementById("customAlert");
document.getElementById("alertOkButton").onclick = () => {
alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert);
};
}
const dismissAlert = (event) => {
if (
event.key === "Enter" /*&& event.ctrlKey*/ &&
alertDiv.classList.contains("visible")
) {
alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert);
}
};
const dismissAlert = (event) => {
if (
event.key === "Enter" /*&& event.ctrlKey*/ &&
alertDiv.classList.contains("visible")
) {
alertDiv.classList.remove("visible");
document.removeEventListener("keydown", dismissAlert);
}
};
document.addEventListener("keydown", dismissAlert);
document.getElementById("alertMessage").textContent = message;
alertDiv.classList.add("visible");
document.addEventListener("keydown", dismissAlert);
document.getElementById("alertMessage").textContent = message;
alertDiv.classList.add("visible");
};
// Extra: hide video players on twitter
if (document.domain == "twitter.com" || document.domain == "x.com") {
// Function to hide the grandparent of video players
// takes 0.014ms to run, so performance is not the concern here.
// timed with console.time, console.timeEnd
// Function to hide the grandparent of video players
// takes 0.014ms to run, so performance is not the concern here.
// timed with console.time, console.timeEnd
function hideVideoPlayerGrandparent() {
document
.querySelectorAll('[data-testid="videoPlayer"]')
.forEach(function (videoPlayer) {
var grandparentElement =
videoPlayer.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement;
var newTextElement = document.createElement("div");
newTextElement.textContent = " [ twitter video ] ";
newTextElement.style["margin-top"] = "10px";
newTextElement.style["margin-left"] = "10px";
newTextElement.style["margin-bottom"] = "10px";
grandparentElement.replaceWith(newTextElement);
});
}
function hideVideoPlayerGrandparent() {
document
.querySelectorAll('[data-testid="videoPlayer"]')
.forEach(function (videoPlayer) {
var grandparentElement =
videoPlayer.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement;
var newTextElement = document.createElement("div");
newTextElement.textContent = " [ twitter video ] ";
newTextElement.style["margin-top"] = "10px";
newTextElement.style["margin-left"] = "10px";
newTextElement.style["margin-bottom"] = "10px";
grandparentElement.replaceWith(newTextElement);
});
}
// Create a new MutationObserver instance
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
hideVideoPlayerGrandparent(); // Call the function to hide video players
}
});
});
// Create a new MutationObserver instance
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
hideVideoPlayerGrandparent(); // Call the function to hide video players
}
});
});
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Start observing the target node for configured mutations
observer.observe(document.body, config);
// Start observing the target node for configured mutations
observer.observe(document.body, config);
// Call the function initially to hide any video players on initial load
hideVideoPlayerGrandparent();
// Call the function initially to hide any video players on initial load
hideVideoPlayerGrandparent();
}
// document.body.style.visibility = "visible";

View File

@ -1,12 +1,12 @@
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include <webkit2/webkit2.h>
#include "config.h"
#include <webkit2/webkit2.h>
#include "plugins/plugins.h"
/* Global variables */
/* Global declarations */
static GtkNotebook* notebook;
static GtkWindow* window;
typedef enum { _SEARCH, _FIND, _HIDDEN } Bar_entry_mode;
@ -32,40 +32,30 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
/* Load content*/
void load_uri(WebKitWebView* view, const char* uri)
{
bool is_empty_uri = (strlen(uri) == 0);
if (is_empty_uri) {
if (strlen(uri) == 0) {
webkit_web_view_load_uri(view, "");
toggle_bar(notebook, _SEARCH);
return;
}
bool has_direct_uri_prefix = g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:");
if (has_direct_uri_prefix){
} 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);
return;
}
bool has_common_domain_extension = (strstr(uri, ".com") || strstr(uri, ".org"));
if (has_common_domain_extension){
} 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);
return;
} 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);
}
}
int l = SHORTCUT_N + strlen(uri) + 1;
char uri_expanded[l];
str_init(uri_expanded, l);
int check = shortcut_expand(uri, uri_expanded);
bool has_shortcut = (check == 2);
if (has_shortcut){
webkit_web_view_load_uri(view, uri_expanded);
return;
}
char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
}
/* Deal with new load or changed load */
@ -121,35 +111,7 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
}
}
/* New tabs */
WebKitWebView* create_new_webview()
{
char* style;
WebKitSettings* settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL);
if (CUSTOM_USER_AGENT) {
webkit_settings_set_user_agent(
settings,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
"like Gecko) Chrome/120.0.0.0 Safari/537.3");
// https://www.useragents.me
}
WebKitWebContext* web_context = webkit_web_context_new_with_website_data_manager(webkit_website_data_manager_new(DATA_MANAGER_OPTS, NULL));
WebKitUserContentManager* contentmanager = webkit_user_content_manager_new();
WebKitCookieManager* cookiemanager = webkit_web_context_get_cookie_manager(web_context);
webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
if (g_file_get_contents("~/opt/rosenrot/style.css", &style, NULL, NULL)) {
webkit_user_content_manager_add_style_sheet(
contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));
}
WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL);
return view;
}
/* Create new tabs */
GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
WebKitNavigationAction* navigation_action,
GtkNotebook* notebook)
@ -163,9 +125,46 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
} 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 ABORT_REQUEST_ON_CURRENT_TAB;
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;
WebKitSettings* settings;
WebKitWebContext* web_context;
WebKitCookieManager* cookiemanager;
WebKitUserContentManager* contentmanager;
settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL);
if (CUSTOM_USER_AGENT) {
webkit_settings_set_user_agent(
settings,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
"like Gecko) Chrome/120.0.0.0 Safari/537.3");
// https://www.useragents.me
}
web_context = webkit_web_context_new_with_website_data_manager(webkit_website_data_manager_new(DATA_MANAGER_OPTS, NULL));
contentmanager = webkit_user_content_manager_new();
cookiemanager = webkit_web_context_get_cookie_manager(web_context);
webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
if (g_file_get_contents("~/opt/rosenrot/style.css", &style, NULL, NULL)) {
webkit_user_content_manager_add_style_sheet(
contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL));
}
return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL);
}
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
{
if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) {
@ -237,7 +236,8 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
gtk_widget_hide(GTK_WIDGET(bar.widget));
}
/* Shortcuts */
/* Handle shortcuts */
// Act when a particular shortcut is detected
int handle_shortcut(func id, GtkNotebook* notebook)
{
static double zoom = ZOOM_START_LEVEL;
@ -254,8 +254,11 @@ int handle_shortcut(func id, GtkNotebook* notebook)
break;
case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */
custom_style_enabled ^= 1;
// fallthrough
if (custom_style_enabled)
custom_style_enabled = 0;
else
custom_style_enabled = 1;
// break; passthrough
case refresh:
webkit_web_view_reload(view);
break;
@ -294,17 +297,18 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_notebook_set_current_page(notebook, j);
break;
case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1;
switch(num_tabs){
switch (gtk_notebook_get_n_pages(notebook)) {
case 0:
exit(0);
break;
case 1:
gtk_notebook_set_show_tabs(notebook, false);
// fallthrough
default:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
break;
}
break;
case toggle_fullscreen:
if (is_fullscreen)
@ -337,13 +341,6 @@ int handle_shortcut(func id, GtkNotebook* notebook)
toggle_bar(notebook, _HIDDEN);
break;
case halve_window:
gtk_window_resize(window, FULL_WIDTH/2, HEIGHT);
break;
case rebig_window:
gtk_window_resize(window, FULL_WIDTH, HEIGHT);
break;
case prettify: {
if (READABILITY_ENABLED) {
char* readability_js = malloc(READABILITY_N + 1);
@ -383,6 +380,7 @@ int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
- https://docs.gtk.org/gdk3/union.Event.html
- https://docs.gtk.org/gdk3/struct.EventButton.html
*/
// This API is deprecated in GTK4 :(.
return 0;
}
@ -393,35 +391,33 @@ int main(int argc, char** argv)
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, "/opt/rosenrot/style-gtk3.css", NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800); /* might change with GTK4/webkitgtk6.0 */
/* Initialize GTK objects. These are declared as static globals at the top of this file */
// Window
window = GTK_WINDOW(gtk_window_new(0));
gtk_window_set_default_size(window, WIDTH, HEIGHT);
// Notebook
notebook = GTK_NOTEBOOK(gtk_notebook_new());
gtk_notebook_set_show_tabs(notebook, false);
gtk_notebook_set_show_border(notebook, false);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook));
// Window
window = GTK_WINDOW(gtk_window_new(0));
gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK3);
g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); /* deprecated in GTK */
// 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_WIDTH, -1);
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));
// Signals
g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook);
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
/* Load first tab */
char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri);
@ -438,5 +434,5 @@ int main(int argc, char** argv)
}
}
gtk_main();
gtk_main(); /* deprecated in GKT4: https://docs.gtk.org/gtk4/migrating-3to4.html#stop-using-gtk_main-and-related-apis */
}

View File

@ -1,15 +1,26 @@
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include <webkit/webkit.h>
#include "config.h"
#include "plugins/plugins.h"
#include <webkit/webkit.h>
#define NULLCHECK(x) \
do { \
if (x == NULL) { \
printf("\nNULL check not passed"); \
printf("@ %s (%d): ", __FILE__, __LINE__); \
exit(0); \
} \
} while (0)
/* Global variables */
static GtkNotebook* notebook;
static GtkWindow* window;
typedef enum { _SEARCH, _FIND, _HIDDEN } Bar_entry_mode;
typedef enum { _SEARCH,
_FIND,
_HIDDEN } Bar_entry_mode;
static struct {
GtkHeaderBar* widget;
GtkEntry* line;
@ -37,39 +48,32 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook) /* TODO: Think throug
void load_uri(WebKitWebView* view, const char* uri)
{
bool is_empty_uri = (strlen(uri) == 0);
if (is_empty_uri) {
webkit_web_view_load_uri(view, "");
toggle_bar(notebook, _SEARCH);
return;
}
bool has_direct_uri_prefix = g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:");
if (has_direct_uri_prefix){
webkit_web_view_load_uri(view, uri);
return;
}
bool has_direct_uri_prefix = (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:"));
bool has_common_domain_extension = (strstr(uri, ".com") || strstr(uri, ".org"));
if (has_common_domain_extension){
char tmp[strlen("https://") + strlen(uri) + 1];
snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri);
webkit_web_view_load_uri(view, tmp);
return;
}
bool has_shortcut;
int l = SHORTCUT_N + strlen(uri) + 1;
char uri_expanded[l];
str_init(uri_expanded, l);
int check = shortcut_expand(uri, uri_expanded);
bool has_shortcut = (check == 2);
if (has_shortcut){
webkit_web_view_load_uri(view, uri_expanded);
return;
}
has_shortcut = (check == 2);
char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
if (is_empty_uri) {
webkit_web_view_load_uri(view, "");
toggle_bar(notebook, _SEARCH);
} else if (has_direct_uri_prefix){
webkit_web_view_load_uri(view, uri);
} else if (has_common_domain_extension){
char tmp[strlen("https://") + strlen(uri) + 1];
snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri);
webkit_web_view_load_uri(view, tmp);
} else if (has_shortcut){
webkit_web_view_load_uri(view, uri_expanded);
} else {
char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp);
}
}
/* Deal with new load or changed load */
@ -169,8 +173,7 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
} 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 ABORT_REQUEST_ON_CURRENT_TAB;
// Could also return GTK_WIDGET(self), in which case the new uri would also be loaded in the current webview. This could be interesting if I wanted to e.g., open an alternative frontend in a new tab
return NULL; // Could also return GTK_WIDGET(self), in which case the new uri would also be loaded in the current webview.
}
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
@ -248,7 +251,8 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) /* con
gtk_widget_hide(GTK_WIDGET(bar.widget));
}
/* Shortcuts */
/* Handle shortcuts */
int handle_shortcut(func id)
{
static double zoom = ZOOM_START_LEVEL;
@ -266,9 +270,12 @@ int handle_shortcut(func id)
webkit_web_view_go_forward(view);
break;
case toggle_custom_style:
custom_style_enabled ^= 1;
// fallthrough
case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */
if (custom_style_enabled)
custom_style_enabled = 0;
else
custom_style_enabled = 1;
// break; passthrough
case refresh:
webkit_web_view_reload(view);
break;
@ -297,27 +304,28 @@ int handle_shortcut(func id)
// https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement
int n = gtk_notebook_get_n_pages(notebook);
int k = gtk_notebook_get_current_page(notebook);
int o = (n + k - 1) % n;
gtk_notebook_set_current_page(notebook, o);
int l = (n + k - 1) % n;
gtk_notebook_set_current_page(notebook, l);
break;
case next_tab:;
int m = gtk_notebook_get_n_pages(notebook);
int l = gtk_notebook_get_current_page(notebook);
int p = (l + 1) % m;
gtk_notebook_set_current_page(notebook, p);
int i = gtk_notebook_get_current_page(notebook);
int j = (i + 1) % m;
gtk_notebook_set_current_page(notebook, j);
break;
case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1;
switch(num_tabs){
switch (gtk_notebook_get_n_pages(notebook)) {
case 0:
exit(0);
break;
case 1:
gtk_notebook_set_show_tabs(notebook, false);
// fallthrough
default:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
break;
}
break;
case toggle_fullscreen:
if (is_fullscreen)
@ -351,13 +359,6 @@ int handle_shortcut(func id)
toggle_bar(notebook, _HIDDEN);
break;
case halve_window:
gtk_window_set_default_size(window, FULL_WIDTH/2, HEIGHT);
break;
case rebig_window:
gtk_window_set_default_size(window, FULL_WIDTH, HEIGHT);
break;
case prettify: {
if (READABILITY_ENABLED) {
char* readability_js = malloc(READABILITY_N + 1);
@ -395,17 +396,16 @@ static int handle_signal_keypress(void* self, int keyval, int keycode,
int main(int argc, char** argv)
{
// Initialize GTK in general
/* Initialize GTK in general */
gtk_init();
g_object_set(gtk_settings_get_default(), GTK_SETTINGS_CONFIG_H, NULL);
// https://docs.gtk.org/gobject/method.Object.set.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, "/opt/rosenrot/style-gtk4.css");
gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_USER);
// Create the main window
window = GTK_WINDOW(gtk_window_new());
gtk_window_set_default_size(window, WIDTH, HEIGHT);
gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK4);
// Set up notebook
notebook = GTK_NOTEBOOK(gtk_notebook_new());
@ -413,17 +413,17 @@ int main(int argc, char** argv)
gtk_notebook_set_show_border(notebook, false);
gtk_window_set_child(window, GTK_WIDGET(notebook));
// Set up top bar
// 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.5);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_WIDTH, -1);
gtk_entry_set_alignment(bar.line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1);
bar.widget = GTK_HEADER_BAR(gtk_header_bar_new());
gtk_header_bar_set_title_widget(bar.widget, GTK_WIDGET(bar.line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget));
// Setup signals
// Signals
GtkEventController* event_controller = gtk_event_controller_key_new();
g_signal_connect(event_controller, "key-pressed", G_CALLBACK(handle_signal_keypress), NULL);
gtk_widget_add_controller(GTK_WIDGET(window), event_controller);
@ -431,17 +431,17 @@ int main(int argc, char** argv)
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
g_signal_connect(GTK_WIDGET(window), "destroy", G_CALLBACK(exit), notebook);
// Load first tab
// Show the application window
gtk_window_present(window);
char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri);
// Show to user
// The first two commands are redundant with notebook_create_new_tab
gtk_window_present(window);
/* Show to user */
gtk_widget_set_visible(GTK_WIDGET(window), 1);
if (argc != 0) gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0);
// Deal with more tabs, if any
/* Deal with more tabs */
if (argc > 2) {
gtk_notebook_set_show_tabs(notebook, true);
for (int i = 2; i < argc; i++) {
@ -450,6 +450,7 @@ int main(int argc, char** argv)
}
// Enter the main event loop, and wait for user interaction
printf("Entering main loop\n");
while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0 && num_tabs > 0)
g_main_context_iteration(NULL, TRUE);