Compare commits

...

5 Commits

7 changed files with 286 additions and 286 deletions

View File

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

View File

@ -1,58 +1,71 @@
#include <stdbool.h> #include <stdbool.h>
// #include <gdk/gdk.h> // <gdk/gdkenums.h>, <gdk/gdkkeysyms.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
// #include "/usr/include/gtk-4.0/gtk/gtk.h"
// Key user config /* Key user config */
#define WIDTH 1920 // 960 for half-width, 1920 for full width #define HEIGHT 1080
#define HEIGHT_GTK3 990 // GTK4 and GTK3 interpret this differently. In GTK4, this includes the height of the top bar #define FULL_WIDTH 1920
#define HEIGHT_GTK4 1080 // GTK4 and GTK3 interpret this differently. In GTK4, this includes the height of the top bar #define WIDTH FULL_WIDTH
#define BAR_SIZE 960 #define BAR_WIDTH FULL_WIDTH/2
// More user config /* More user config */
#define ZOOM_START_LEVEL 2 #define ZOOM_START_LEVEL 1.8
#define ZOOM_STEPSIZE .1 #define ZOOM_STEPSIZE .1
#define MAX_NUM_TABS 8 // 0/false for unlimited tabs #define MAX_NUM_TABS 8 // 0 or 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 SEARCH "https://search.brave.com/search?q=%s"
#define HOME "https://search.brave.com/search" // "file:///opt/rose/homepage.png", "" #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"
// Plugins /* Plugins */
#define LIBRE_REDIRECT_ENABLED true #define LIBRE_REDIRECT_ENABLED true
#define READABILITY_ENABLED true #define READABILITY_ENABLED true
#define CUSTOM_USER_AGENT false #define CUSTOM_USER_AGENT false
/* /*
To disable plugins: To disable plugins:
1. set their corresponding variable to false 1. set their corresponding variable to false
2. recompile 2. recompile
To remove plugins completely; To remove plugins completely;
1. Remove the corresponding code in rosenrot.c by looking for the variables above. 1. Remove the corresponding code in rosenrot.c by looking for the variables above, as well as custom_style_enabled
2. Remove PLUGIN and $(PLUGIN) from the makefile 2. Remove PLUGIN and $(PLUGIN) from the makefile
3. Recompile 3. Recompile
You could also look into commit afe93518a for an approach using stand-in code. You could also look into commit afe93518a for an approach using stand-in code.
*/ */
// Webkit settings /* Webkit */
// https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html // https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html
#define WEBKIT_DEFAULT_SETTINGS \ #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, \ "enable-smooth-scrolling", false, \
"default-charset", "utf-8" "default-charset", "utf-8"
#define DATA_DIR "/home/nuno/.cache/rosenrot" #define DATA_DIR "/home/nuno/.cache/rosenrot"
#define DATA_MANAGER_OPTS "base-cache-directory", DATA_DIR, "base-data-directory", DATA_DIR #define DATA_MANAGER_OPTS "base-cache-directory", DATA_DIR, "base-data-directory", DATA_DIR
#define NETWORK_SESSION_OPTS DATA_DIR, DATA_DIR #define NETWORK_SESSION_OPTS DATA_DIR, DATA_DIR
// GTK /* 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 // 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 // https://gitlab.gnome.org/GNOME/gtk/-/blob/main/gdk/gdkenums.h
#define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", false, "gtk-enable-animations", false
#define KEY(x) GDK_KEY_##x
#define SFT 1 << 0 #define SFT 1 << 0
#define CTRL 1 << 2 #define CTRL 1 << 2
#define ALT 1 << 3 #define ALT 1 << 3
// Shortcuts /* 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 */
typedef enum { typedef enum {
goback, goback,
goforward, goforward,
@ -80,6 +93,9 @@ typedef enum {
finder_next, finder_next,
finder_prev, finder_prev,
halve_window,
rebig_window,
prettify, prettify,
} func; } func;
@ -103,10 +119,11 @@ static struct {
{ CTRL, KEY(minus), zoomout }, { CTRL, KEY(minus), zoomout },
{ CTRL, KEY(0), zoom_reset }, { CTRL, KEY(0), zoom_reset },
{ CTRL, KEY(KP_Page_Up), prev_tab }, { CTRL | SFT, KEY(KP_Page_Up), prev_tab }, // use SFT just to show one can
{ CTRL, KEY(KP_Page_Down), next_tab }, { CTRL | SFT, 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 | SFT, KEY(Page_Up), prev_tab },
{ CTRL, KEY(Page_Down), next_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(t), new_tab }, { CTRL, KEY(t), new_tab },
{ CTRL, KEY(w), close_tab }, { CTRL, KEY(w), close_tab },
@ -115,28 +132,7 @@ static struct {
{ CTRL, KEY(f), show_finder }, { CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next }, { CTRL, KEY(n), finder_next },
{ CTRL, KEY(N), finder_prev }, { CTRL, KEY(N), finder_prev },
{ CTRL, KEY(Left), halve_window },
{ CTRL, KEY(Right), rebig_window },
{ CTRL, KEY(p), prettify } { 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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define STYLE_N 7877 + 1000 #define STYLE_N 8049 + 1000
void read_style_js(char* string) void read_style_js(char* string)
{ {

View File

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

View File

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

View File

@ -1,12 +1,12 @@
#include <gdk/gdk.h> #include <gdk/gdk.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <webkit2/webkit2.h>
#include "config.h" #include "config.h"
#include <webkit2/webkit2.h>
#include "plugins/plugins.h" #include "plugins/plugins.h"
/* Global declarations */ /* Global variables */
static GtkNotebook* notebook; static GtkNotebook* notebook;
static GtkWindow* window; static GtkWindow* window;
typedef enum { _SEARCH, _FIND, _HIDDEN } Bar_entry_mode; typedef enum { _SEARCH, _FIND, _HIDDEN } Bar_entry_mode;
@ -32,30 +32,40 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
/* Load content*/ /* Load content*/
void load_uri(WebKitWebView* view, const char* uri) void load_uri(WebKitWebView* view, const char* uri)
{ {
if (strlen(uri) == 0) { bool is_empty_uri = (strlen(uri) == 0);
if (is_empty_uri) {
webkit_web_view_load_uri(view, ""); webkit_web_view_load_uri(view, "");
toggle_bar(notebook, _SEARCH); 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:")) { 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); webkit_web_view_load_uri(view, uri);
} else if (strstr(uri, ".com") || strstr(uri, ".org")) { return;
}
bool has_common_domain_extension = (strstr(uri, ".com") || strstr(uri, ".org"));
if (has_common_domain_extension){
char tmp[strlen("https://") + strlen(uri) + 1]; char tmp[strlen("https://") + strlen(uri) + 1];
snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
} else { return;
// Check for shortcuts }
int l = SHORTCUT_N + strlen(uri) + 1;
char uri_expanded[l]; int l = SHORTCUT_N + strlen(uri) + 1;
str_init(uri_expanded, l); char uri_expanded[l];
int check = shortcut_expand(uri, uri_expanded); str_init(uri_expanded, l);
if (check == 2) { int check = shortcut_expand(uri, uri_expanded);
webkit_web_view_load_uri(view, uri_expanded); bool has_shortcut = (check == 2);
} else { if (has_shortcut){
// Feed into search engine. webkit_web_view_load_uri(view, uri_expanded);
char tmp[strlen(uri) + strlen(SEARCH)]; return;
snprintf(tmp, sizeof(tmp), SEARCH, uri); }
webkit_web_view_load_uri(view, tmp);
} 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 */ /* Deal with new load or changed load */
@ -111,7 +121,35 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
} }
} }
/* Create new tabs */ /* 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;
}
GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
WebKitNavigationAction* navigation_action, WebKitNavigationAction* navigation_action,
GtkNotebook* notebook) GtkNotebook* notebook)
@ -125,46 +163,9 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
} else { } else {
webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
} }
return NULL; return ABORT_REQUEST_ON_CURRENT_TAB;
/*
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) void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
{ {
if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) { if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) {
@ -236,8 +237,7 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
gtk_widget_hide(GTK_WIDGET(bar.widget)); gtk_widget_hide(GTK_WIDGET(bar.widget));
} }
/* Handle shortcuts */ /* Shortcuts */
// Act when a particular shortcut is detected
int handle_shortcut(func id, GtkNotebook* notebook) int handle_shortcut(func id, GtkNotebook* notebook)
{ {
static double zoom = ZOOM_START_LEVEL; static double zoom = ZOOM_START_LEVEL;
@ -254,11 +254,8 @@ int handle_shortcut(func id, GtkNotebook* notebook)
break; break;
case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */ case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */
if (custom_style_enabled) custom_style_enabled ^= 1;
custom_style_enabled = 0; // fallthrough
else
custom_style_enabled = 1;
// break; passthrough
case refresh: case refresh:
webkit_web_view_reload(view); webkit_web_view_reload(view);
break; break;
@ -297,18 +294,17 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_notebook_set_current_page(notebook, j); gtk_notebook_set_current_page(notebook, j);
break; break;
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1; num_tabs -= 1;
switch(num_tabs){
switch (gtk_notebook_get_n_pages(notebook)) {
case 0: case 0:
exit(0); exit(0);
break; break;
case 1: case 1:
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
break; // fallthrough
default:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
} }
break; break;
case toggle_fullscreen: case toggle_fullscreen:
if (is_fullscreen) if (is_fullscreen)
@ -341,6 +337,13 @@ int handle_shortcut(func id, GtkNotebook* notebook)
toggle_bar(notebook, _HIDDEN); toggle_bar(notebook, _HIDDEN);
break; 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: { case prettify: {
if (READABILITY_ENABLED) { if (READABILITY_ENABLED) {
char* readability_js = malloc(READABILITY_N + 1); char* readability_js = malloc(READABILITY_N + 1);
@ -380,7 +383,6 @@ int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
- 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
*/ */
// This API is deprecated in GTK4 :(.
return 0; return 0;
} }
@ -391,33 +393,35 @@ 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 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(); GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_path(css, "/opt/rosenrot/style-gtk3.css", NULL); 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); /* might change with GTK4/webkitgtk6.0 */ gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800);
/* Initialize GTK objects. These are declared as static globals at the top of this file */ /* 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
notebook = GTK_NOTEBOOK(gtk_notebook_new()); notebook = GTK_NOTEBOOK(gtk_notebook_new());
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
gtk_notebook_set_show_border(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
bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0)); bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text)); bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text));
gtk_entry_set_alignment(bar.line, 0.48); gtk_entry_set_alignment(bar.line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1); gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_WIDTH, -1);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
bar.widget = GTK_HEADER_BAR(gtk_header_bar_new()); bar.widget = GTK_HEADER_BAR(gtk_header_bar_new());
gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line)); gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget)); 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 */ /* Load first tab */
char* first_uri = argc > 1 ? argv[1] : HOME; char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri); notebook_create_new_tab(notebook, first_uri);
@ -434,5 +438,5 @@ int main(int argc, char** argv)
} }
} }
gtk_main(); /* deprecated in GKT4: https://docs.gtk.org/gtk4/migrating-3to4.html#stop-using-gtk_main-and-related-apis */ gtk_main();
} }

View File

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