rosenrot/window.c

351 lines
8.9 KiB
C
Raw Normal View History

2022-05-10 15:35:37 +00:00
#include "window.h"
#define LENGTH(x) ((int) (sizeof(x) / sizeof(x[0])))
2022-05-10 17:52:56 +00:00
2022-05-10 15:35:37 +00:00
struct _RoseWindow {
GtkApplicationWindow parent_instance;
2022-05-10 21:47:17 +00:00
guint xid;
2022-05-16 13:47:41 +00:00
2022-05-10 15:35:37 +00:00
GtkWindow *window;
2022-05-16 13:47:41 +00:00
GtkApplication *app;
WebKitWebView *webview[9];
WebKitWebInspector *inspector;
2022-05-13 10:30:15 +00:00
WebKitFindOptions *findopts;
2022-05-16 13:47:41 +00:00
2022-05-10 15:35:37 +00:00
GHashTable *action_groups;
GHashTable *action_labels;
};
enum {
PROP_ACTIVE_CHILD
};
static float zoom = 1.0;
2022-05-16 15:10:18 +00:00
static int tabid;
2022-05-10 15:35:37 +00:00
G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW)
2022-05-10 17:52:56 +00:00
static void read_clipboard(GObject *object,
GAsyncResult *res,
gpointer webview)
2022-05-10 17:52:56 +00:00
{
GdkClipboard *clipboard = GDK_CLIPBOARD(object);
2022-05-10 17:52:56 +00:00
webkit_web_view_load_uri(
WEBKIT_WEB_VIEW(webview),
gdk_clipboard_read_text_finish(clipboard, res, NULL)
);
}
2022-05-16 13:47:41 +00:00
static void new_tab(RoseWindow *window, unsigned id)
{
2022-05-16 15:10:18 +00:00
/* window->webview[id] = WEBKIT_WEB_VIEW(rose_webview_new()); */
/* if (!options[HOMEPAGE]) */
/* options[HOMEPAGE] = "https://duckduckgo.com"; */
/* */
/* rose_webview_load_url(WEBKIT_WEB_VIEW(window->webview[id]), */
/* options[HOMEPAGE]); */
/* 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[id])); */
2022-05-16 13:47:41 +00:00
}
static void goto_tab(RoseWindow *window, unsigned id)
{
2022-05-16 15:10:18 +00:00
/* if (window->webview[id]) { */
/* puts("new tab"); */
/* new_tab(window, id); */
/* return; */
/* } */
/* printf("%u -> %u\n", tabid, id); */
int len = LENGTH(window->webview);
while (len--)
printf("%i", WEBKIT_IS_WEB_VIEW(window->webview[len]));
puts("");
/* new_tab(window, id); */
/* gtk_window_set_child( */
/* GTK_WINDOW( */
/* gtk_widget_get_root( */
/* GTK_WIDGET( */
/* window->webview[id] */
/* ) */
/* ) */
/* ), */
/* GTK_WIDGET(window->webview[id]) */
/* ); */
/* */
2022-05-16 13:47:41 +00:00
/* gtk_widget_hide(GTK_WIDGET(window->webview[tabid])); */
2022-05-16 15:10:18 +00:00
/* gtk_widget_show(GTK_WIDGET(window->webview[id])); */
2022-05-16 13:47:41 +00:00
}
2022-05-10 18:28:39 +00:00
static gboolean key_press_callback(RoseWindow *window,
2022-05-10 15:35:37 +00:00
guint keyval,
guint keycode,
2022-05-10 15:35:37 +00:00
GdkModifierType state)
{
(void) keycode;
for (int i = 0; i < LENGTH(keys); i++) {
if (keys[i].modkey == state
&& keys[i].keycod == keyval) {
switch (keys[i].funcid) {
case goback:
2022-05-16 13:47:41 +00:00
webkit_web_view_go_back(window->webview[tabid]);
break;
2022-05-13 10:30:15 +00:00
case goforward:
2022-05-16 13:47:41 +00:00
webkit_web_view_go_forward(window->webview[tabid]);
break;
2022-05-13 10:30:15 +00:00
case copy_url: {
GdkDisplay *dpy = gdk_display_get_default();
gdk_clipboard_set_text(
gdk_display_get_clipboard(dpy),
2022-05-16 13:47:41 +00:00
webkit_web_view_get_uri(window->webview[tabid])
);
} break;
2022-05-13 10:30:15 +00:00
case paste_url: {
GdkDisplay *dpy = gdk_display_get_default();
GdkClipboard *clipboard = gdk_display_get_clipboard(dpy);
2022-05-16 13:47:41 +00:00
gdk_clipboard_read_text_async(clipboard, NULL, read_clipboard, window->webview[tabid]);
} break;
2022-05-13 10:30:15 +00:00
case fullscreen:
2022-05-16 13:47:41 +00:00
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[tabid]))));
else
2022-05-16 13:47:41 +00:00
gtk_window_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview[tabid]))));
break;
2022-05-13 10:30:15 +00:00
case search: {
int id = fork();
if (id == 0) {
if (glob_dpy)
close(ConnectionNumber(glob_dpy));
setsid();
2022-05-13 10:30:15 +00:00
char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose", NULL};
execvp("/bin/sh", argument_list);
perror(" failed");
exit(1);
} else {
wait(&id);
2022-05-13 12:14:19 +00:00
char *uri;
if (strcmp((uri = (char*)getatom(AtomGo)), ""))
2022-05-16 15:10:18 +00:00
webkit_web_view_load_uri(window->webview[tabid], uri);
2022-05-13 10:30:15 +00:00
}
} break;
2022-05-13 10:30:15 +00:00
case find: {
int id = fork();
if (id == 0) {
if (glob_dpy)
close(ConnectionNumber(glob_dpy));
2022-05-13 10:30:15 +00:00
setsid();
char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose\tfind", NULL};
execvp("/bin/sh", argument_list);
perror(" failed");
exit(1);
} else {
wait(&id);
2022-05-16 13:47:41 +00:00
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
2022-05-13 10:30:15 +00:00
webkit_find_controller_search(
finder,
getatom(AtomFind),
WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
WEBKIT_FIND_OPTIONS_WRAP_AROUND, G_MAXUINT);
2022-05-10 21:47:17 +00:00
}
2022-05-13 10:30:15 +00:00
} break;
case findnext: {
2022-05-16 13:47:41 +00:00
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
2022-05-13 10:30:15 +00:00
webkit_find_controller_search_next(finder);
} break;
case findprev: {
2022-05-16 13:47:41 +00:00
WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview[tabid]);
2022-05-13 10:30:15 +00:00
webkit_find_controller_search_previous(finder);
} break;
case zoomin:
zoom += 0.1;
2022-05-16 13:47:41 +00:00
webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
break;
2022-05-13 10:30:15 +00:00
case zoomout:
zoom -= 0.1;
2022-05-16 13:47:41 +00:00
webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
break;
2022-05-13 10:30:15 +00:00
2022-05-13 12:08:29 +00:00
case zoomreset:
zoom = 1;
2022-05-16 13:47:41 +00:00
webkit_web_view_set_zoom_level(window->webview[tabid], zoom);
2022-05-13 12:08:29 +00:00
break;
case inspector:
2022-05-16 13:47:41 +00:00
window->inspector = webkit_web_view_get_inspector(window->webview[tabid]);
if (webkit_web_inspector_is_attached(window->inspector))
webkit_web_inspector_close(window->inspector);
else
webkit_web_inspector_show(window->inspector);
return GDK_EVENT_STOP;
break;
2022-05-13 10:30:15 +00:00
case up:
2022-05-16 13:47:41 +00:00
webkit_web_view_run_javascript(window->webview[tabid], "window.scrollBy(0,-100);", NULL, NULL, NULL);
break;
2022-05-13 10:30:15 +00:00
case down:
2022-05-16 13:47:41 +00:00
webkit_web_view_run_javascript(window->webview[tabid], "window.scrollBy(0,100);", NULL, NULL, NULL);
break;
2022-05-13 12:08:29 +00:00
case reload:
2022-05-16 13:47:41 +00:00
webkit_web_view_reload(window->webview[tabid]);
2022-05-13 12:08:29 +00:00
break;
case reloadforce:
2022-05-16 13:47:41 +00:00
webkit_web_view_reload_bypass_cache(window->webview[tabid]);
2022-05-13 12:08:29 +00:00
break;
2022-05-13 18:37:50 +00:00
case history: {
int id = fork();
if (id == 0) {
setsid();
char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose\thistory", NULL};
execvp("/bin/sh", argument_list);
perror(" failed");
exit(1);
} else {
wait(&id);
char *uri;
if (strcmp((uri = (char*)getatom(AtomGo)), ""))
2022-05-16 15:10:18 +00:00
webkit_web_view_load_uri(window->webview[tabid], uri);
2022-05-13 18:37:50 +00:00
}
} break;
2022-05-14 10:04:26 +00:00
case gotop: {
2022-05-16 13:47:41 +00:00
webkit_web_view_run_javascript(window->webview[tabid], "window.scrollTo(0,0);", NULL, NULL, NULL);
2022-05-14 10:04:26 +00:00
} break;
case gobottom: {
webkit_web_view_run_javascript(
2022-05-16 13:47:41 +00:00
window->webview[tabid],
2022-05-14 10:04:26 +00:00
"window.scrollTo(0, document.body.scrollHeight);",
NULL, NULL, NULL);
} break;
2022-05-16 13:47:41 +00:00
2022-05-16 15:10:18 +00:00
case tabnext:
if (tabid != 8)
goto_tab(window, ++tabid);
break;
2022-05-16 13:47:41 +00:00
2022-05-16 15:10:18 +00:00
case tabprev:
if (tabid != 0)
goto_tab(window, --tabid);
2022-05-16 13:47:41 +00:00
2022-05-16 15:10:18 +00:00
break;
2022-05-10 15:35:37 +00:00
}
}
}
2022-05-10 18:28:39 +00:00
2022-05-13 10:30:15 +00:00
2022-05-10 15:35:37 +00:00
return GDK_EVENT_PROPAGATE;
}
static void rose_window_constructed(GObject *object)
{
G_OBJECT_CLASS(rose_window_parent_class)->constructed(object);
}
static void rose_window_init(RoseWindow *window)
{
2022-05-16 13:47:41 +00:00
(void) window;
2022-05-10 15:35:37 +00:00
}
static void destroy()
{
exit(0);
}
2022-05-11 01:26:23 +00:00
guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url)
2022-05-10 15:35:37 +00:00
{
2022-05-16 15:10:18 +00:00
tabid = 0;
memset(window, 0, sizeof(*window));
2022-05-16 13:47:41 +00:00
window->app = app;
window->window = GTK_WINDOW(gtk_application_window_new(window->app));
2022-05-16 13:47:41 +00:00
/* gtk_application_set_menubar(window->app, FALSE); */
2022-05-16 15:10:18 +00:00
window->webview[0] = WEBKIT_WEB_VIEW(webkit_web_view_new());
2022-05-10 21:47:17 +00:00
2022-05-16 13:47:41 +00:00
g_signal_connect(G_OBJECT(window->window), "destroy",
G_CALLBACK(destroy), NULL);
2022-05-13 12:08:29 +00:00
2022-05-16 13:47:41 +00:00
g_signal_connect(G_OBJECT(window->webview[tabid]), "web-process-terminated",
G_CALLBACK(destroy), NULL);
2022-05-16 15:10:18 +00:00
/* g_signal_connect(G_OBJECT(window->webview[tabid]), "load-changed", */
/* G_CALLBACK(rose_load_changed_callback), window->webview[0]); */
2022-05-13 18:37:50 +00:00
2022-05-16 15:10:18 +00:00
if (url)
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(window->webview[tabid]), url);
2022-05-13 18:37:50 +00:00
if (!(appearance[WIDTH] && appearance[HEIGHT])) {
appearance[WIDTH] = 1280;
appearance[HEIGHT] = 720;
2022-05-13 16:41:48 +00:00
}
2022-05-16 15:10:18 +00:00
puts("");
2022-05-16 13:47:41 +00:00
gtk_window_set_default_size(GTK_WINDOW(window->window), appearance[WIDTH], appearance[HEIGHT]);
2022-05-10 21:47:17 +00:00
2022-05-16 13:47:41 +00:00
gtk_window_set_child(GTK_WINDOW(window->window), GTK_WIDGET(window->webview[tabid]));
2022-05-10 15:35:37 +00:00
GtkEventController *controller;
controller = gtk_event_controller_key_new();
2022-05-10 18:28:39 +00:00
g_signal_connect_swapped(controller, "key-pressed", G_CALLBACK(key_press_callback), window);
2022-05-16 13:47:41 +00:00
gtk_widget_add_controller(GTK_WIDGET(window->window), controller);
2022-05-10 15:35:37 +00:00
2022-05-16 13:47:41 +00:00
gtk_widget_show(GTK_WIDGET(window->window));
2022-05-16 15:10:18 +00:00
gtk_widget_show(GTK_WIDGET(window->webview[0]));
2022-05-10 21:47:17 +00:00
2022-05-16 13:47:41 +00:00
return 12;
2022-05-10 15:35:37 +00:00
}
void rose_window_set_webview(RoseWindow *window, GtkWidget *webview)
{
2022-05-16 15:10:18 +00:00
/* window->webview[tabid] = WEBKIT_WEB_VIEW(webview); */
2022-05-10 15:35:37 +00:00
}
static void rose_window_class_init(RoseWindowClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS(class);
object_class->constructed = rose_window_constructed;
}
RoseWindow* rose_window_new(GtkApplication *app)
{
return g_object_new(
GTK_TYPE_APPLICATION_WINDOW,
"application", GTK_APPLICATION(app),
NULL
);
}