style: Add comments & clean up code

Fixed whitespace, added comments & removed unused functions.
This commit is contained in:
bellrise 2022-05-16 11:57:09 +02:00
parent 4de824d665
commit 7d81e7cd38
6 changed files with 63 additions and 70 deletions

21
rose.c
View File

@ -17,25 +17,25 @@ void setatom(int a, const char *v)
XSync(glob_dpy, False); XSync(glob_dpy, False);
} }
const char* getatom(int a) char* getatom(int a)
{ {
static char buf[BUFSIZ];
Atom adummy; Atom adummy;
int idummy; int idummy;
unsigned long ldummy; unsigned long ldummy;
unsigned char *p = NULL; unsigned char *p = NULL;
char *ret;
XSync(glob_dpy, False); XSync(glob_dpy, False);
XGetWindowProperty(glob_dpy, glob_xid, XGetWindowProperty(glob_dpy, glob_xid,
glob_atoms[a], 0L, BUFSIZ, False, glob_atoms[AtomUTF8], glob_atoms[a], 0L, BUFSIZ, False, glob_atoms[AtomUTF8],
&adummy, &idummy, &ldummy, &ldummy, &p); &adummy, &idummy, &ldummy, &ldummy, &p);
if (p)
strncpy(buf, (char *)p, LENGTH(buf) - 1);
else
buf[0] = '\0';
XFree(p);
return buf; /* We need to strdup() the returned string, because the spec says we _have_
to use XFree(). Although it's probably a regular free() call, we can make
sure by just strdup'ing it into our own buffer. */
ret = strdup((char *) p);
XFree(p);
return ret;
} }
static void setup() static void setup()
@ -69,6 +69,7 @@ int main(int argc, char **argv)
options[HOMEPAGE] = argv[1]; options[HOMEPAGE] = argv[1];
argv++; argc--; argv++; argc--;
} }
setup(); setup();
GtkApplication *app = gtk_application_new("org.gtk.rose", G_APPLICATION_NON_UNIQUE); GtkApplication *app = gtk_application_new("org.gtk.rose", G_APPLICATION_NON_UNIQUE);
g_signal_connect(app, "activate", G_CALLBACK(run), NULL); g_signal_connect(app, "activate", G_CALLBACK(run), NULL);

6
rose.h
View File

@ -23,5 +23,7 @@ enum {
extern Display *glob_dpy; /* declared in rose.c */ extern Display *glob_dpy; /* declared in rose.c */
const char* getatom(int a); /* Set/get X11 window properties. `getatom` retuns an allocated string
void setatom(int a, const char *v); which has to be free'd. */
char *getatom(int aid);
void setatom(int aid, const char *val);

View File

@ -2,10 +2,8 @@
struct _RoseWebView { struct _RoseWebView {
WebKitWebView parent_instance; WebKitWebView parent_instance;
GCancellable *cancellable; GCancellable *cancellable;
RoseWebViewNavigationFlags navigation; RoseWebViewNavigationFlags navigation;
char *address; char *address;
}; };
@ -15,9 +13,10 @@ enum {
PROP_ADDRESS PROP_ADDRESS
}; };
G_DEFINE_TYPE(RoseWebView, rose_webview, WEBKIT_TYPE_WEB_VIEW)
static GParamSpec *obj_properties[LAST_PROP]; static GParamSpec *obj_properties[LAST_PROP];
G_DEFINE_TYPE(RoseWebView, rose_webview, WEBKIT_TYPE_WEB_VIEW)
static void update_navigation_flags(RoseWebView *webview) static void update_navigation_flags(RoseWebView *webview)
{ {
@ -88,11 +87,8 @@ static void rose_webview_class_init(RoseWebViewClass *class)
object_class->constructed = rose_webview_constructed; object_class->constructed = rose_webview_constructed;
obj_properties[PROP_ADDRESS] = obj_properties[PROP_ADDRESS] =
g_param_spec_string ("address", g_param_spec_string("address", "Address", "address",
"Address", "", G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
"address",
"",
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
} }
static void rose_webview_init(RoseWebView *webview) static void rose_webview_init(RoseWebView *webview)
@ -106,20 +102,24 @@ void rose_webview_load_url(WebKitWebView *webview, const char *url)
setatom(AtomUri, url); setatom(AtomUri, url);
} }
void rose_load_changed_callback(WebKitWebView *webview, void rose_load_changed_callback(WebKitWebView *webview, WebKitLoadEvent event)
WebKitLoadEvent event)
{ {
char *hist;
size_t nhist;
FILE *f;
if (event == WEBKIT_LOAD_FINISHED) { if (event == WEBKIT_LOAD_FINISHED) {
const char *uri = webkit_web_view_get_uri(webview); /* Append the current URL to the history file. */
char *cookiefile = calloc(1, sizeof(char) * (strlen(options[CACHE]) + 32) + 1); nhist = strlen(options[CACHE]) + 12;
sprintf(cookiefile, "%s/history", options[CACHE]); hist = malloc(sizeof(char) * nhist);
snprintf(hist, nhist, "%s/history", options[CACHE]);
FILE *cookie = fopen(cookiefile, "a"); f = fopen(hist, "a");
fprintf(f, "%s\n", webkit_web_view_get_uri(webview));
fclose(f);
fprintf(cookie, "%s\n", uri); free(hist);
fclose(cookie); }
free(cookiefile);
}
} }
static void rose_download(const char* uri) static void rose_download(const char* uri)
@ -129,6 +129,8 @@ static void rose_download(const char* uri)
strcpy(url, uri); strcpy(url, uri);
/* TODO: replace this whole cruft with another alternative */
int id = fork(); int id = fork();
if (id == 0) { if (id == 0) {
setsid(); setsid();
@ -142,14 +144,13 @@ static void rose_download(const char* uri)
static void rose_response_reciver(WebKitDownload *download) static void rose_response_reciver(WebKitDownload *download)
{ {
const char *uri = webkit_uri_response_get_uri(webkit_download_get_response(download)); const char *uri = webkit_uri_response_get_uri(webkit_download_get_response(download));
rose_download(uri); rose_download(uri);
} }
static void rose_download_callback(WebKitDownload *download) static void rose_download_callback(WebKitDownload *download)
{ {
g_signal_connect(G_OBJECT(download), "notify::response", g_signal_connect(G_OBJECT(download), "notify::response",
G_CALLBACK(rose_response_reciver), NULL); G_CALLBACK(rose_response_reciver), NULL);
} }
GtkWidget* rose_webview_new() GtkWidget* rose_webview_new()
@ -199,11 +200,11 @@ GtkWidget* rose_webview_new()
webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
g_signal_connect(G_OBJECT(context), "download-started", g_signal_connect(G_OBJECT(context), "download-started",
G_CALLBACK(rose_download_callback), NULL); G_CALLBACK(rose_download_callback), NULL);
return g_object_new( return g_object_new(
WEBKIT_TYPE_WEB_VIEW, WEBKIT_TYPE_WEB_VIEW,
"settings", settings, "settings", settings,
"user-content-manager", contentmanager, "user-content-manager", contentmanager,
"web-context", context, NULL); "web-context", context, NULL);
} }

View File

@ -9,21 +9,18 @@
G_DECLARE_FINAL_TYPE(RoseWebView, rose_webview, ROSE, WEBVIEW, WebKitWebView) G_DECLARE_FINAL_TYPE(RoseWebView, rose_webview, ROSE, WEBVIEW, WebKitWebView)
typedef enum typedef enum {
{
ROSE_WEBVIEW_NAV_BACK = 1 << 0, ROSE_WEBVIEW_NAV_BACK = 1 << 0,
ROSE_WEBVIEW_FORWARD = 1 << 1 ROSE_WEBVIEW_FORWARD = 1 << 1
} RoseWebViewNavigationFlags; } RoseWebViewNavigationFlags;
/* Create a new webview in the form of a Gtk Object. */
GtkWidget* rose_webview_new(); GtkWidget* rose_webview_new();
/* Load the given `url` into the webview. */
void rose_webview_load_url(WebKitWebView *webview, const char *url); void rose_webview_load_url(WebKitWebView *webview, const char *url);
RoseWebViewNavigationFlags rose_webview_get_navigation_flags(RoseWebView *webview); /* Called when the URL changes, adds a new entry to the history
file. */
void rose_webview_go_back(RoseWebView *window);
void rose_webview_go_forward(RoseWebView *window);
void rose_load_changed_callback(WebKitWebView *webview, void rose_load_changed_callback(WebKitWebView *webview,
WebKitLoadEvent event); WebKitLoadEvent event);
const char* rose_webview_get_address(RoseWebView *webview);

View File

@ -21,9 +21,7 @@ static float zoom = 1.0;
G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW) G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW)
static void read_clipboard(GObject *object, static void read_clipboard(GObject *object, GAsyncResult *res, gpointer webview)
GAsyncResult *res,
gpointer webview)
{ {
GdkClipboard *clipboard = GDK_CLIPBOARD(object); GdkClipboard *clipboard = GDK_CLIPBOARD(object);
webkit_web_view_load_uri( webkit_web_view_load_uri(
@ -32,16 +30,13 @@ static void read_clipboard(GObject *object,
); );
} }
static gboolean key_press_callback(RoseWindow *window, static gboolean key_press_callback(RoseWindow *window, guint keyval,
guint keyval, guint keycode, GdkModifierType state)
guint keycode,
GdkModifierType state)
{ {
(void) keycode; (void) keycode;
for (int i = 0; i < LENGTH(keys); i++) { for (int i = 0; i < LENGTH(keys); i++) {
if (keys[i].modkey == state if (keys[i].modkey == state && 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);
@ -191,7 +186,6 @@ static gboolean key_press_callback(RoseWindow *window,
} }
} }
return GDK_EVENT_PROPAGATE; return GDK_EVENT_PROPAGATE;
} }
@ -222,13 +216,13 @@ guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url)
window->webview = WEBKIT_WEB_VIEW(webview); window->webview = WEBKIT_WEB_VIEW(webview);
g_signal_connect(G_OBJECT(w), "destroy", g_signal_connect(G_OBJECT(w), "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), "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), "load-changed",
G_CALLBACK(rose_load_changed_callback), window); G_CALLBACK(rose_load_changed_callback), window);
if (url) { if (url) {
rose_webview_load_url(WEBKIT_WEB_VIEW(webview), url); rose_webview_load_url(WEBKIT_WEB_VIEW(webview), url);
@ -263,15 +257,14 @@ void rose_window_set_webview(RoseWindow *window, GtkWidget *webview)
static void rose_window_class_init(RoseWindowClass *class) static void rose_window_class_init(RoseWindowClass *class)
{ {
GObjectClass *object_class = G_OBJECT_CLASS(class); GObjectClass *object_class = G_OBJECT_CLASS(class);
object_class->constructed = rose_window_constructed; object_class->constructed = rose_window_constructed;
} }
RoseWindow* rose_window_new(GtkApplication *app) RoseWindow* rose_window_new(GtkApplication *app)
{ {
return g_object_new( return g_object_new(
GTK_TYPE_APPLICATION_WINDOW, GTK_TYPE_APPLICATION_WINDOW,
"application", GTK_APPLICATION(app), "application", GTK_APPLICATION(app),
NULL NULL
); );
} }

View File

@ -9,11 +9,10 @@
G_DECLARE_FINAL_TYPE(RoseWindow, rose_window, ROSE, WINDOW, GtkApplicationWindow) G_DECLARE_FINAL_TYPE(RoseWindow, rose_window, ROSE, WINDOW, GtkApplicationWindow)
/* Create a new GObject, which is our window. */
RoseWindow* rose_window_new(); RoseWindow* rose_window_new();
/* Set up all the things and show the window. */
guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url); guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url);
gboolean rose_window_close(RoseWindow *window);
void rose_window_set_webview(RoseWindow *window, GtkWidget *webview); void rose_window_set_webview(RoseWindow *window, GtkWidget *webview);
GActionGroup* rose_window_get_action_group(RoseWindow *window,
const char *prefix);