From be0d50002465ac90b8c9328bcfe54d69efb85016 Mon Sep 17 00:00:00 2001 From: bellrise Date: Mon, 16 May 2022 09:54:39 +0200 Subject: [PATCH 1/4] code: Remove global variables from rose.h Global variables in headers may not work the way you think, so it's safer to make a static local one. --- rose.c | 21 +++++++++++---------- rose.h | 1 - 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rose.c b/rose.c index ed9980a..55765ba 100644 --- a/rose.c +++ b/rose.c @@ -3,14 +3,15 @@ #define MSGBUFSZ 8 #define LENGTH(x) (sizeof(x) / sizeof(x[0])) -guint xid; +static Display *glob_dpy; +static guint glob_xid; void setatom(int a, const char *v) { - XChangeProperty(dpy, xid, + XChangeProperty(glob_dpy, glob_xid, atoms[a], atoms[AtomUTF8], 8, PropModeReplace, (unsigned char *)v, strlen(v) + 1); - XSync(dpy, False); + XSync(glob_dpy, False); } const char* getatom(int a) @@ -21,8 +22,8 @@ const char* getatom(int a) unsigned long ldummy; unsigned char *p = NULL; - XSync(dpy, False); - XGetWindowProperty(dpy, xid, + XSync(glob_dpy, False); + XGetWindowProperty(glob_dpy, glob_xid, atoms[a], 0L, BUFSIZ, False, atoms[AtomUTF8], &adummy, &idummy, &ldummy, &ldummy, &p); if (p) @@ -36,14 +37,14 @@ const char* getatom(int a) static void setup() { - if (!(dpy = XOpenDisplay(NULL))) { + if (!(glob_dpy = XOpenDisplay(NULL))) { puts("Can't open default display"); exit(1); } - atoms[AtomFind] = XInternAtom(dpy, "_ROSE_FIND", False); - atoms[AtomGo] = XInternAtom(dpy, "_ROSE_GO", False); - atoms[AtomUri] = XInternAtom(dpy, "_ROSE_URI", False); + atoms[AtomFind] = XInternAtom(glob_dpy, "_ROSE_FIND", False); + atoms[AtomGo] = XInternAtom(glob_dpy, "_ROSE_GO", False); + atoms[AtomUri] = XInternAtom(glob_dpy, "_ROSE_URI", False); } static void run(GtkApplication *app) @@ -56,7 +57,7 @@ static void run(GtkApplication *app) if (!options[HOMEPAGE]) options[HOMEPAGE] = "https://duckduckgo.com"; - xid = rose_window_show(app, window, options[HOMEPAGE]); + glob_xid = rose_window_show(app, window, options[HOMEPAGE]); } int main(int argc, char **argv) diff --git a/rose.h b/rose.h index 99ea26c..192d3dc 100644 --- a/rose.h +++ b/rose.h @@ -17,6 +17,5 @@ enum { AtomFind, AtomGo, AtomUri, AtomUTF8, AtomLast }; static Atom atoms[AtomLast]; -static Display *dpy; const char* getatom(int a); void setatom(int a, const char *v); From 63f277ae4648e0376bb90da50e35d691f5b00c08 Mon Sep 17 00:00:00 2001 From: bellrise Date: Mon, 16 May 2022 09:55:39 +0200 Subject: [PATCH 2/4] code: Remove G_BEGIN_DECLS & G_END_DECLS We're always compiling for C, so we don't need to C++ compiler cruft. --- webview.h | 2 -- window.h | 4 ---- 2 files changed, 6 deletions(-) diff --git a/webview.h b/webview.h index 983e57a..8a4db61 100644 --- a/webview.h +++ b/webview.h @@ -5,8 +5,6 @@ #include #include -G_BEGIN_DECLS - #define ROSE_TYPE_WEBVIEW rose_webview_get_type() G_DECLARE_FINAL_TYPE(RoseWebView, rose_webview, ROSE, WEBVIEW, WebKitWebView) diff --git a/window.h b/window.h index 33e29f6..0207aeb 100644 --- a/window.h +++ b/window.h @@ -5,8 +5,6 @@ #include #include -G_BEGIN_DECLS - #define ROSE_TYPE_WINDOW rose_window_get_type() G_DECLARE_FINAL_TYPE(RoseWindow, rose_window, ROSE, WINDOW, GtkApplicationWindow) @@ -19,5 +17,3 @@ void rose_window_set_webview(RoseWindow *window, GtkWidget *webview); GActionGroup* rose_window_get_action_group(RoseWindow *window, const char *prefix); - -G_END_DECLS From 58932060cce965cd9083467f5804be9781a26a56 Mon Sep 17 00:00:00 2001 From: bellrise Date: Mon, 16 May 2022 10:46:42 +0200 Subject: [PATCH 3/4] rose+window: Fix warnings & global variables --- config.def.h | 14 +++++++------- keyconf.h | 3 --- rose.c | 15 +++++++++------ rose.h | 20 +++++++++++++------- window.c | 11 +++++++---- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/config.def.h b/config.def.h index 5df0e03..acd6497 100644 --- a/config.def.h +++ b/config.def.h @@ -1,17 +1,17 @@ #include "keyconf.h" -OPTIONS { - [CACHE] = DEFAULT, - [HOMEPAGE] = DEFAULT, +static const char *options[] = { + [CACHE] = DEFAULT, /* DEFAULT = "~/.cache/rose" */ + [HOMEPAGE] = DEFAULT, /* DEFAULT = "https://duckduckgo.com" */ }; -APPERANCE { - [HEIGHT] = DEFAULT, - [WIDTH] = DEFAULT, +static int appearance[] = { + [HEIGHT] = DEFAULT, /* DEFAULT = 720 */ + [WIDTH] = DEFAULT, /* DEFAULT = 1280 */ [DARKMODE] = TRUE }; -KEYBINDS { +static const Key keys[] = { { MODKEY, GDK_KEY_h, goback }, { MODKEY, GDK_KEY_l, goforward }, { MODKEY, GDK_KEY_y, copy_url }, diff --git a/keyconf.h b/keyconf.h index 26a124c..3c25969 100644 --- a/keyconf.h +++ b/keyconf.h @@ -18,9 +18,6 @@ #define DARKMODE 2 #define SCROLLBARS 3 -#define KEYBINDS static inline Key keys[] -#define APPERANCE static inline int appearance[] -#define OPTIONS static inline char *options[] typedef struct { unsigned modkey; diff --git a/rose.c b/rose.c index 55765ba..f0d759e 100644 --- a/rose.c +++ b/rose.c @@ -3,13 +3,16 @@ #define MSGBUFSZ 8 #define LENGTH(x) (sizeof(x) / sizeof(x[0])) -static Display *glob_dpy; +Display *glob_dpy; /* defined in rose.h */ + static guint glob_xid; +static Atom glob_atoms[AtomLast]; + void setatom(int a, const char *v) { XChangeProperty(glob_dpy, glob_xid, - atoms[a], atoms[AtomUTF8], 8, PropModeReplace, + glob_atoms[a], glob_atoms[AtomUTF8], 8, PropModeReplace, (unsigned char *)v, strlen(v) + 1); XSync(glob_dpy, False); } @@ -24,7 +27,7 @@ const char* getatom(int a) XSync(glob_dpy, False); XGetWindowProperty(glob_dpy, glob_xid, - atoms[a], 0L, BUFSIZ, False, atoms[AtomUTF8], + glob_atoms[a], 0L, BUFSIZ, False, glob_atoms[AtomUTF8], &adummy, &idummy, &ldummy, &ldummy, &p); if (p) strncpy(buf, (char *)p, LENGTH(buf) - 1); @@ -42,9 +45,9 @@ static void setup() exit(1); } - atoms[AtomFind] = XInternAtom(glob_dpy, "_ROSE_FIND", False); - atoms[AtomGo] = XInternAtom(glob_dpy, "_ROSE_GO", False); - atoms[AtomUri] = XInternAtom(glob_dpy, "_ROSE_URI", False); + glob_atoms[AtomFind] = XInternAtom(glob_dpy, "_ROSE_FIND", False); + glob_atoms[AtomGo] = XInternAtom(glob_dpy, "_ROSE_GO", False); + glob_atoms[AtomUri] = XInternAtom(glob_dpy, "_ROSE_URI", False); } static void run(GtkApplication *app) diff --git a/rose.h b/rose.h index 192d3dc..8a56410 100644 --- a/rose.h +++ b/rose.h @@ -5,17 +5,23 @@ #include "config.h" #include +#include #include #include -#include -#include -#include #include -#include -#include #include +#include +#include + +enum { + AtomFind, + AtomGo, + AtomUri, + AtomUTF8, + AtomLast +}; + +extern Display *glob_dpy; /* declared in rose.c */ -enum { AtomFind, AtomGo, AtomUri, AtomUTF8, AtomLast }; -static Atom atoms[AtomLast]; const char* getatom(int a); void setatom(int a, const char *v); diff --git a/window.c b/window.c index 364f20e..8d1190a 100644 --- a/window.c +++ b/window.c @@ -38,6 +38,8 @@ static gboolean key_press_callback(RoseWindow *window, guint keycode, GdkModifierType state) { + (void) keycode; + for (int i = 0; i < LENGTH(keys); i++) { if (keys[i].modkey == state && keys[i].keycod == keyval) { @@ -74,8 +76,8 @@ static gboolean key_press_callback(RoseWindow *window, case search: { int id = fork(); if (id == 0) { - if (dpy) - close(ConnectionNumber(dpy)); + if (glob_dpy) + close(ConnectionNumber(glob_dpy)); setsid(); char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose", NULL}; execvp("/bin/sh", argument_list); @@ -91,8 +93,8 @@ static gboolean key_press_callback(RoseWindow *window, case find: { int id = fork(); if (id == 0) { - if (dpy) - close(ConnectionNumber(dpy)); + if (glob_dpy) + close(ConnectionNumber(glob_dpy)); setsid(); char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose\tfind", NULL}; execvp("/bin/sh", argument_list); @@ -205,6 +207,7 @@ static void rose_window_init(RoseWindow *window) static void destroy(RoseWindow *window) { + (void) window; exit(0); } From 1d622de7971b8439866dfb2873d0b33004437431 Mon Sep 17 00:00:00 2001 From: bellrise Date: Mon, 16 May 2022 10:47:04 +0200 Subject: [PATCH 4/4] makefile: Fix PHONY targets Previously, `rose` could not be called if it was already compiled because it wasn't a PHONY target, so the source files had to be changed. --- makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/makefile b/makefile index 33e5f0b..0f0b485 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ # -*- indent-tabs-mode: t -*- -CC ?= cc +CC ?= clang CFLAGS = `pkg-config --cflags gtk4 webkit2gtk-5.0 x11` LIBS = `pkg-config --libs gtk4 webkit2gtk-5.0 x11` OPTIONS = -Dgtk_doc=false -Dintrospection=false \ @@ -26,7 +26,8 @@ rose: strip ./rose debug: - $(CC) -fPIC -o rose *.c $(CFLAGS) $(LIBS) $(OPTIONS) -Wall -Wextra + $(CC) -fPIC -o rose *.c $(CFLAGS) $(LIBS) $(OPTIONS) -Wall -Wextra \ + -Wno-unused-variable config.h: [ -f "$@" ] || cp config.def.h $@ @@ -47,5 +48,5 @@ clean-all: clean flags: echo $(CFLAGS) | sed 's/ /\n/g' > compile_flags.txt -.PHONY: all clean clean-all install uninstall flags config.h -.SILENT: all clean clean-all install uninstall flags config.h +.PHONY: all rose clean clean-all install uninstall flags config.h +.SILENT: all rose clean clean-all install uninstall flags config.h