initial tabs

This commit is contained in:
fenze 2022-05-16 15:47:41 +02:00
parent 99d16fa506
commit 78a46bd5b5
4 changed files with 142 additions and 50 deletions

24
compile_flags.txt Normal file
View File

@ -0,0 +1,24 @@
-I/usr/include/gtk-4.0
-I/usr/include/pango-1.0
-I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include
-I/usr/include/sysprof-4
-I/usr/include/harfbuzz
-I/usr/include/freetype2
-I/usr/include/libpng16
-I/usr/include/libmount
-I/usr/include/blkid
-I/usr/include/fribidi
-I/usr/include/cairo
-I/usr/include/lzo
-I/usr/include/pixman-1
-I/usr/include/gdk-pixbuf-2.0
-I/usr/include/graphene-1.0
-I/usr/lib/graphene-1.0/include
-I/usr/include/gio-unix-2.0
-I/usr/include/webkitgtk-5.0
-mfpmath=sse
-msse
-msse2
-I/usr/include/libsoup-3.0
-pthread

View File

@ -4,10 +4,10 @@
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <stdbool.h> #include <stdbool.h>
#define CTRLKEY #define ALTKEY GDK_ALT_MASK
#define MODKEY GDK_CONTROL_MASK #define MODKEY GDK_CONTROL_MASK
#define MODSFT 5 #define MODSFT 5
#define NOMODK 0 #define NOMODK 0
#define CACHE 0 #define CACHE 0
#define HOMEPAGE 1 #define HOMEPAGE 1
@ -45,5 +45,7 @@ enum {
reloadforce, reloadforce,
history, history,
gotop, gotop,
gobottom gobottom,
tabnext,
tabprev
}; };

View File

@ -26,7 +26,7 @@ rose:
strip ./rose strip ./rose
debug: debug:
$(CC) -fPIC -o rose *.c $(CFLAGS) $(LIBS) -Wall -Wextra $(CC) -fPIC -o rose *.c $(CFLAGS) $(LIBS) -Wall -Wextra -fsanitize=address -DG_DISABLE_CAST_CHECKS
config.h: config.h:
[ -f "$@" ] || cp config.def.h $@ [ -f "$@" ] || cp config.def.h $@

154
window.c
View File

@ -5,10 +5,14 @@
struct _RoseWindow { struct _RoseWindow {
GtkApplicationWindow parent_instance; GtkApplicationWindow parent_instance;
guint xid; guint xid;
GtkWindow *window; GtkWindow *window;
WebKitWebView *webview; GtkApplication *app;
WebKitWebView *webview[9];
WebKitWebInspector *inspector; WebKitWebInspector *inspector;
WebKitFindOptions *findopts; WebKitFindOptions *findopts;
GHashTable *action_groups; GHashTable *action_groups;
GHashTable *action_labels; GHashTable *action_labels;
}; };
@ -18,6 +22,7 @@ enum {
}; };
static float zoom = 1.0; static float zoom = 1.0;
static unsigned tabid = 0;
G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW) G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW)
@ -32,6 +37,54 @@ static void read_clipboard(GObject *object,
); );
} }
static void new_tab(RoseWindow *window, unsigned id)
{
window->webview[id] = WEBKIT_WEB_VIEW(webkit_web_view_new());
if (!options[HOMEPAGE])
options[HOMEPAGE] = "https://duckduckgo.com";
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(window->webview[id]),
options[HOMEPAGE]);
/* gtk_widget_hide(GTK_WIDGET(window->webview[id])); */
gtk_window_set_child(
GTK_WINDOW(
gtk_widget_get_root(
GTK_WIDGET(
window->webview[tabid++]
)
)
),
GTK_WIDGET(window->webview[id])
);
gtk_widget_show(GTK_WIDGET(window->webview[tabid]));
}
static void goto_tab(RoseWindow *window, unsigned id)
{
if (!window->webview[id]) {
new_tab(window, id);
return;
}
gtk_window_set_child(
GTK_WINDOW(
gtk_widget_get_root(
GTK_WIDGET(
window->webview[tabid]
)
)
),
GTK_WIDGET(window->webview[id])
);
/* gtk_widget_hide(GTK_WIDGET(window->webview[tabid])); */
gtk_widget_show(GTK_WIDGET(window->webview[id]));
}
static gboolean key_press_callback(RoseWindow *window, static gboolean key_press_callback(RoseWindow *window,
guint keyval, guint keyval,
guint keycode, guint keycode,
@ -44,32 +97,32 @@ static gboolean key_press_callback(RoseWindow *window,
&& keys[i].keycod == keyval) { && keys[i].keycod == keyval) {
switch (keys[i].funcid) { switch (keys[i].funcid) {
case goback: case goback:
webkit_web_view_go_back(window->webview); webkit_web_view_go_back(window->webview[tabid]);
break; break;
case goforward: case goforward:
webkit_web_view_go_forward(window->webview); webkit_web_view_go_forward(window->webview[tabid]);
break; break;
case copy_url: { case copy_url: {
GdkDisplay *dpy = gdk_display_get_default(); GdkDisplay *dpy = gdk_display_get_default();
gdk_clipboard_set_text( gdk_clipboard_set_text(
gdk_display_get_clipboard(dpy), gdk_display_get_clipboard(dpy),
webkit_web_view_get_uri(window->webview) webkit_web_view_get_uri(window->webview[tabid])
); );
} break; } break;
case paste_url: { case paste_url: {
GdkDisplay *dpy = gdk_display_get_default(); GdkDisplay *dpy = gdk_display_get_default();
GdkClipboard *clipboard = gdk_display_get_clipboard(dpy); GdkClipboard *clipboard = gdk_display_get_clipboard(dpy);
gdk_clipboard_read_text_async(clipboard, NULL, read_clipboard, window->webview); gdk_clipboard_read_text_async(clipboard, NULL, read_clipboard, window->webview[tabid]);
} break; } break;
case fullscreen: case fullscreen:
if (gtk_window_is_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview))))) if (gtk_window_is_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview[tabid])))))
gtk_window_unfullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview)))); gtk_window_unfullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview[tabid]))));
else else
gtk_window_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview)))); gtk_window_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview[tabid]))));
break; break;
case search: { case search: {
@ -86,7 +139,7 @@ static gboolean key_press_callback(RoseWindow *window,
wait(&id); wait(&id);
char *uri; char *uri;
if (strcmp((uri = (char*)getatom(AtomGo)), "")) if (strcmp((uri = (char*)getatom(AtomGo)), ""))
rose_webview_load_url(window->webview, uri); rose_webview_load_url(window->webview[tabid], uri);
} }
} break; } break;
@ -102,7 +155,7 @@ static gboolean key_press_callback(RoseWindow *window,
exit(1); exit(1);
} else { } else {
wait(&id); wait(&id);
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
webkit_find_controller_search( webkit_find_controller_search(
finder, finder,
getatom(AtomFind), getatom(AtomFind),
@ -112,32 +165,32 @@ static gboolean key_press_callback(RoseWindow *window,
} break; } break;
case findnext: { case findnext: {
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
webkit_find_controller_search_next(finder); webkit_find_controller_search_next(finder);
} break; } break;
case findprev: { case findprev: {
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
webkit_find_controller_search_previous(finder); webkit_find_controller_search_previous(finder);
} break; } break;
case zoomin: case zoomin:
zoom += 0.1; zoom += 0.1;
webkit_web_view_set_zoom_level(window->webview, zoom); webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
break; break;
case zoomout: case zoomout:
zoom -= 0.1; zoom -= 0.1;
webkit_web_view_set_zoom_level(window->webview, zoom); webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
break; break;
case zoomreset: case zoomreset:
zoom = 1; zoom = 1;
webkit_web_view_set_zoom_level(window->webview, zoom); webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
break; break;
case inspector: case inspector:
window->inspector = webkit_web_view_get_inspector(window->webview); window->inspector = webkit_web_view_get_inspector(window->webview[tabid]);
if (webkit_web_inspector_is_attached(window->inspector)) if (webkit_web_inspector_is_attached(window->inspector))
webkit_web_inspector_close(window->inspector); webkit_web_inspector_close(window->inspector);
else else
@ -146,19 +199,19 @@ static gboolean key_press_callback(RoseWindow *window,
break; break;
case up: case up:
webkit_web_view_run_javascript(window->webview, "window.scrollBy(0,-100);", NULL, NULL, NULL); webkit_web_view_run_javascript(window->webview[tabid], "window.scrollBy(0,-100);", NULL, NULL, NULL);
break; break;
case down: case down:
webkit_web_view_run_javascript(window->webview, "window.scrollBy(0,100);", NULL, NULL, NULL); webkit_web_view_run_javascript(window->webview[tabid], "window.scrollBy(0,100);", NULL, NULL, NULL);
break; break;
case reload: case reload:
webkit_web_view_reload(window->webview); webkit_web_view_reload(window->webview[tabid]);
break; break;
case reloadforce: case reloadforce:
webkit_web_view_reload_bypass_cache(window->webview); webkit_web_view_reload_bypass_cache(window->webview[tabid]);
break; break;
case history: { case history: {
@ -173,20 +226,36 @@ static gboolean key_press_callback(RoseWindow *window,
wait(&id); wait(&id);
char *uri; char *uri;
if (strcmp((uri = (char*)getatom(AtomGo)), "")) if (strcmp((uri = (char*)getatom(AtomGo)), ""))
rose_webview_load_url(window->webview, uri); rose_webview_load_url(window->webview[tabid], uri);
} }
} break; } break;
case gotop: { case gotop: {
webkit_web_view_run_javascript(window->webview, "window.scrollTo(0,0);", NULL, NULL, NULL); webkit_web_view_run_javascript(window->webview[tabid], "window.scrollTo(0,0);", NULL, NULL, NULL);
} break; } break;
case gobottom: { case gobottom: {
webkit_web_view_run_javascript( webkit_web_view_run_javascript(
window->webview, window->webview[tabid],
"window.scrollTo(0, document.body.scrollHeight);", "window.scrollTo(0, document.body.scrollHeight);",
NULL, NULL, NULL); NULL, NULL, NULL);
} break; } break;
case tabnext: {
if (tabid + 1 > 9)
return GDK_EVENT_PROPAGATE;
goto_tab(window, tabid + 1);
printf("%i\n", tabid);
} break;
case tabprev: {
if (!tabid)
return GDK_EVENT_PROPAGATE;
goto_tab(window, tabid - 1);
printf("%i\n", tabid);
} break;
} }
} }
} }
@ -202,7 +271,7 @@ static void rose_window_constructed(GObject *object)
static void rose_window_init(RoseWindow *window) static void rose_window_init(RoseWindow *window)
{ {
window->window = GTK_WINDOW(gtk_window_new()); (void) window;
} }
static void destroy() static void destroy()
@ -212,26 +281,23 @@ static void destroy()
guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url) guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url)
{ {
GtkWidget *w = gtk_application_window_new(app); window->app = app;
window->window = GTK_WINDOW(gtk_application_window_new(window->app));
gtk_application_set_menubar(app, FALSE); /* gtk_application_set_menubar(window->app, FALSE); */
GtkWidget *webview = rose_webview_new(); window->webview[tabid] = WEBKIT_WEB_VIEW(rose_webview_new());
gtk_widget_set_has_tooltip(webview, FALSE);
gtk_widget_has_default(webview);
gtk_window_set_destroy_with_parent(GTK_WINDOW(w), TRUE);
window->webview = WEBKIT_WEB_VIEW(webview);
g_signal_connect(G_OBJECT(w), "destroy", g_signal_connect(G_OBJECT(window->window), "destroy",
G_CALLBACK(destroy), NULL); G_CALLBACK(destroy), NULL);
g_signal_connect(G_OBJECT(window->webview), "web-process-terminated", g_signal_connect(G_OBJECT(window->webview[tabid]), "web-process-terminated",
G_CALLBACK(destroy), NULL); G_CALLBACK(destroy), NULL);
g_signal_connect(G_OBJECT(window->webview), "load-changed", g_signal_connect(G_OBJECT(window->webview[tabid]), "load-changed",
G_CALLBACK(rose_load_changed_callback), window); G_CALLBACK(rose_load_changed_callback), window->webview[tabid]);
if (url) { if (url) {
rose_webview_load_url(WEBKIT_WEB_VIEW(webview), url); rose_webview_load_url(WEBKIT_WEB_VIEW(window->webview[tabid]), url);
setatom(AtomUri, url); setatom(AtomUri, url);
} }
@ -240,24 +306,24 @@ guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url)
appearance[HEIGHT] = 720; appearance[HEIGHT] = 720;
} }
gtk_window_set_default_size(GTK_WINDOW(w), appearance[WIDTH], appearance[HEIGHT]); gtk_window_set_default_size(GTK_WINDOW(window->window), appearance[WIDTH], appearance[HEIGHT]);
gtk_window_set_child(GTK_WINDOW(w), GTK_WIDGET(webview)); gtk_window_set_child(GTK_WINDOW(window->window), GTK_WIDGET(window->webview[tabid]));
/* Keyboard shortcuts */
GtkEventController *controller; GtkEventController *controller;
controller = gtk_event_controller_key_new(); controller = gtk_event_controller_key_new();
g_signal_connect_swapped(controller, "key-pressed", G_CALLBACK(key_press_callback), window); g_signal_connect_swapped(controller, "key-pressed", G_CALLBACK(key_press_callback), window);
gtk_widget_add_controller(GTK_WIDGET(w), controller); gtk_widget_add_controller(GTK_WIDGET(window->window), controller);
gtk_widget_show(w); gtk_widget_show(GTK_WIDGET(window->window));
gtk_widget_show(GTK_WIDGET(window->webview[tabid]));
return gdk_x11_surface_get_xid(gtk_native_get_surface(GTK_NATIVE(w))); return 12;
} }
void rose_window_set_webview(RoseWindow *window, GtkWidget *webview) void rose_window_set_webview(RoseWindow *window, GtkWidget *webview)
{ {
window->webview = WEBKIT_WEB_VIEW(webview); window->webview[tabid] = WEBKIT_WEB_VIEW(webview);
} }
static void rose_window_class_init(RoseWindowClass *class) static void rose_window_class_init(RoseWindowClass *class)