aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2023-11-15 22:35:41 +0100
committerFederico Angelilli <code@fedang.net>2023-11-15 22:35:41 +0100
commit22086e50382b99de092899a203e520c30f50b618 (patch)
tree90ca79a7e9fedd52b21d458cf91f1fbd8a56bd06
parent7d795eede96e08bf7673a3264648b33693adea21 (diff)
Add `Connection` to separate xcb setup from window logic
-rw-r--r--comet.c12
-rw-r--r--connection.c137
-rw-r--r--connection.h29
-rw-r--r--draw.c7
-rw-r--r--draw.h2
-rw-r--r--window.c (renamed from x11.c)220
-rw-r--r--window.h (renamed from x11.h)14
7 files changed, 244 insertions, 177 deletions
diff --git a/comet.c b/comet.c
index 85bdb27..38c6db8 100644
--- a/comet.c
+++ b/comet.c
@@ -2,9 +2,10 @@
#include <glib.h>
#include <glib-unix.h>
-#include "x11.h"
+#include "window.h"
#include "log.h"
#include "draw.h"
+#include "connection.h"
#define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0))
@@ -26,10 +27,13 @@ int main(int argc, char **argv)
GMainLoop *mainloop = g_main_loop_new(NULL, FALSE);
- Window *win = window_create();
+ Connection *con = connection_create();
- int screen_width, screen_height;
- window_get_screen_size(win, &screen_width, &screen_height);
+ Window *win = window_create(con);
+
+ int screen_width = con->screen_size->width;
+ int screen_height = con->screen_size->height;
+ log_info("w=%d, h=%d", screen_width, screen_height);
int height = EVEN(round(screen_height * 0.021));
int x_padding = EVEN(round(screen_width * 0.005));
diff --git a/connection.c b/connection.c
new file mode 100644
index 0000000..20c1e6d
--- /dev/null
+++ b/connection.c
@@ -0,0 +1,137 @@
+#include <glib.h>
+#include <xcb/xcb.h>
+#include <xcb/xcb_ewmh.h>
+#include <xcb/xcb_xrm.h>
+#include <xcb/xcb_errors.h>
+#include <xcb/randr.h>
+
+#include "log.h"
+#include "connection.h"
+
+struct Connection {
+ xcb_connection_t *connection;
+ xcb_screen_t *screen;
+ xcb_randr_screen_size_t *screen_size;
+ double screen_dpi;
+ xcb_visualtype_t *visual_type;
+ xcb_depth_t *depth;
+ xcb_xrm_database_t *database;
+ xcb_errors_context_t *errors;
+ xcb_ewmh_connection_t ewmh;
+};
+
+static bool query_xrm(Connection *con, const char *res, char **value)
+{
+ if (xcb_xrm_resource_get_string(con->database, res, res, value) >= 0) {
+ log_debug("Xrm query '%s' found '%s'", res, *value);
+ return true;
+ }
+
+ log_debug("Xrm query '%s' not found", res);
+ return false;
+}
+
+static void update_scale(Connection *con)
+{
+ char *dpi_value;
+ if (query_xrm(con, "Xft.dpi", &dpi_value)) {
+ con->screen_dpi = strtod(dpi_value, NULL);
+ g_free(dpi_value);
+ } else {
+ con->screen_dpi = (double)con->screen_size->height * 25.4 / (double)con->screen_size->mheight;
+ log_debug("Fallback dpi value '%.2lf'", con->screen_dpi);
+ }
+}
+
+Connection *connection_create()
+{
+ Connection *con = g_malloc0(sizeof(Connection));
+ g_assert_nonnull(con);
+
+ int preferred_screen = 0;
+ con->connection = xcb_connect(NULL, &preferred_screen);
+ g_assert_true(con->connection != NULL && !xcb_connection_has_error(con->connection));
+ log_debug("Xcb connection established");
+
+ log_debug("Default screen %d", preferred_screen);
+ xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(con->connection));
+
+ while (preferred_screen != 0 && iter.rem) {
+ xcb_screen_next(&iter);
+ preferred_screen--;
+ }
+
+ con->screen = iter.data;
+ xcb_generic_error_t *error;
+
+ xcb_randr_query_version_cookie_t version_cookie = xcb_randr_query_version(con->connection,
+ XCB_RANDR_MAJOR_VERSION,
+ XCB_RANDR_MINOR_VERSION);
+
+ xcb_randr_query_version_reply_t *randr_version = xcb_randr_query_version_reply(con->connection,
+ version_cookie,
+ &error);
+
+ g_assert_null(error);
+ log_debug("RandR loaded [version=%d.%d]", randr_version->major_version, randr_version->minor_version);
+ g_assert_cmpint(randr_version->major_version, >=, 1);
+
+ xcb_randr_get_screen_info_cookie_t cookie = xcb_randr_get_screen_info(con->connection, con->screen->root);
+ xcb_randr_get_screen_info_reply_t *info_reply = xcb_randr_get_screen_info_reply(con->connection, cookie, &error);
+ g_assert_null(error);
+
+ con->screen_size = xcb_randr_get_screen_info_sizes(info_reply);
+ g_assert_nonnull(con->screen_size);
+ log_debug("Screen size [width=%d, height=%d]", con->screen_size->width, con->screen_size->height);
+
+ xcb_randr_select_input(con->connection,
+ con->screen->root,
+ XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
+ XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
+ XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
+
+ con->database = xcb_xrm_database_from_default(con->connection);
+ g_assert_nonnull(con->database);
+
+ g_assert(xcb_errors_context_new(con->connection, &con->errors) == 0);
+ log_debug("Xcb errors loaded");
+
+ xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(con->screen);
+ while (depth_iter.rem) {
+ xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
+
+ while (visual_iter.rem) {
+ if (con->screen->root_visual == visual_iter.data->visual_id) {
+ con->visual_type = visual_iter.data;
+ con->depth = depth_iter.data;
+ goto found_visual;
+ }
+ xcb_visualtype_next(&visual_iter);
+ }
+ xcb_depth_next(&depth_iter);
+ }
+
+found_visual:
+ log_debug("Xcb visual type found [id=%u]", con->visual_type->visual_id);
+
+ xcb_intern_atom_cookie_t *ewmh_cookie = xcb_ewmh_init_atoms(con->connection, &con->ewmh);
+ g_assert_true(xcb_ewmh_init_atoms_replies(&con->ewmh, ewmh_cookie, &error));
+ g_assert_null(error);
+ log_debug("Xcb ewmh initialized");
+
+ update_scale(con);
+
+ xcb_flush(con->connection);
+ log_debug("Xcb set up");
+
+ return con;
+}
+
+void connection_destroy(Connection *con)
+{
+ xcb_ewmh_connection_wipe(&con->ewmh);
+ xcb_errors_context_free(con->errors);
+ xcb_xrm_database_free(con->database);
+ xcb_disconnect(con->connection);
+ g_free(con);
+}
diff --git a/connection.h b/connection.h
new file mode 100644
index 0000000..d028cce
--- /dev/null
+++ b/connection.h
@@ -0,0 +1,29 @@
+#ifndef COMET_CONNECTION_H
+#define COMET_CONNECTION_H
+
+#include <glib.h>
+#include <xcb/xcb.h>
+#include <xcb/xcb_ewmh.h>
+#include <xcb/xcb_xrm.h>
+#include <xcb/xcb_errors.h>
+#include <xcb/randr.h>
+
+
+// TODO: Make this opaque
+typedef struct {
+ xcb_connection_t *connection;
+ xcb_screen_t *screen;
+ xcb_randr_screen_size_t *screen_size;
+ double screen_dpi;
+ xcb_visualtype_t *visual_type;
+ xcb_depth_t *depth;
+ xcb_xrm_database_t *database;
+ xcb_errors_context_t *errors;
+ xcb_ewmh_connection_t ewmh;
+} Connection;
+
+Connection *connection_create();
+
+void connection_destroy(Connection *con);
+
+#endif
diff --git a/draw.c b/draw.c
index afd727e..440231b 100644
--- a/draw.c
+++ b/draw.c
@@ -3,7 +3,7 @@
#include <pango/pango-font.h>
#include <pango/pango-types.h>
-#include "x11.h"
+#include "draw.h"
#include "log.h"
// Make this a state passed to draw
@@ -28,7 +28,8 @@ void draw(Window *win)
int screen_width, screen_height;
window_get_screen_size(win, &screen_width, &screen_height);
- int width = round((screen_width - 2 * draw_x_padding) * scale);
+ int width0 = screen_width - 2 * draw_x_padding;
+ int width = round(width0 * scale);
int height = round(draw_height * scale);
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
@@ -98,7 +99,7 @@ void draw(Window *win)
// TODO: Move these somewhere else
window_move(win, x, y);
- window_resize(win, width, height);
+ window_resize(win, width0, draw_height);
window_paint_surface(win, surface, width, height);
cairo_surface_destroy(surface);
diff --git a/draw.h b/draw.h
index 929fecb..e352ad5 100644
--- a/draw.h
+++ b/draw.h
@@ -1,7 +1,7 @@
#ifndef COMET_DRAW_H
#define COMET_DRAW_H
-#include "x11.h"
+#include "window.h"
void draw_init(const char *font, int height, int x_padding, int y_padding);
diff --git a/x11.c b/window.c
index beb65b8..6bb9f50 100644
--- a/x11.c
+++ b/window.c
@@ -13,24 +13,17 @@
#include <xcb/randr.h>
#include <xcb/shape.h>
-#include "x11.h"
+#include "window.h"
#include "log.h"
// TODO: Event logic should be decoupled from Window logic
#include "draw.h"
struct Window {
- xcb_connection_t *connection;
- xcb_screen_t *screen;
- xcb_randr_screen_size_t *screen_size;
- xcb_xrm_database_t *database;
- xcb_errors_context_t *errors;
+ Connection *con;
xcb_drawable_t window;
- xcb_visualtype_t *visual_type;
- xcb_ewmh_connection_t ewmh;
cairo_surface_t *surface;
cairo_t *cr;
- double dpi;
int x, y;
int width, height;
GSource *source;
@@ -46,7 +39,7 @@ static gboolean xcb_source_check(GSource *source)
{
XcbSource *xsource = (XcbSource *)source;
g_assert_null(xsource->event);
- xsource->event = xcb_poll_for_event(xsource->win->connection);
+ xsource->event = xcb_poll_for_event(xsource->win->con->connection);
return xsource->event != NULL;
}
@@ -65,9 +58,9 @@ static gboolean xcb_source_dispatch(GSource *source, GSourceFunc callback, gpoin
xcb_generic_error_t *error = (xcb_generic_error_t *)xsource->event;
const char *extension;
- const char *name = xcb_errors_get_name_for_error(win->errors, error->error_code, &extension);
- const char *major = xcb_errors_get_name_for_major_code(win->errors, error->major_code);
- const char *minor = xcb_errors_get_name_for_minor_code(win->errors, error->major_code, error->minor_code);
+ const char *name = xcb_errors_get_name_for_error(win->con->errors, error->error_code, &extension);
+ const char *major = xcb_errors_get_name_for_major_code(win->con->errors, error->major_code);
+ const char *minor = xcb_errors_get_name_for_minor_code(win->con->errors, error->major_code, error->minor_code);
// TODO: Handle errors instead of aborting
log_error("Xcb error '%s' [extension=%s, major=%s, minor=%s, resource=%u, sequence=%u]",
@@ -99,7 +92,7 @@ static gboolean xcb_source_dispatch(GSource *source, GSourceFunc callback, gpoin
log_debug("Processing event 'PropertyNotify' [type=%d]", XCB_PROPERTY_NOTIFY);
if (property->atom == XCB_ATOM_RESOURCE_MANAGER) {
- window_update_scale(data);
+ //window_update_scale(data);
// Redraw
callback(data);
@@ -109,7 +102,7 @@ static gboolean xcb_source_dispatch(GSource *source, GSourceFunc callback, gpoin
default: {
const char *extension;
- const char *name = xcb_errors_get_name_for_xcb_event(win->errors, xsource->event, &extension);
+ const char *name = xcb_errors_get_name_for_xcb_event(win->con->errors, xsource->event, &extension);
log_debug("Ignoring event '%s' [extension=%s, type=%d]",
name,
@@ -117,7 +110,7 @@ static gboolean xcb_source_dispatch(GSource *source, GSourceFunc callback, gpoin
xsource->event->response_type & 0x7f);
}
}
- } while ((xsource->event = xcb_poll_for_event(xsource->win->connection)) != NULL);
+ } while ((xsource->event = xcb_poll_for_event(xsource->win->con->connection)) != NULL);
return G_SOURCE_CONTINUE;
}
@@ -146,26 +139,15 @@ static void attach_source(Window *win)
// TODO: Draw should be decoupled from this file
g_source_set_callback(win->source, G_SOURCE_FUNC(draw), win, NULL);
- g_source_add_unix_fd(win->source, xcb_get_file_descriptor(win->connection), G_IO_IN | G_IO_HUP | G_IO_ERR);
+ g_source_add_unix_fd(win->source, xcb_get_file_descriptor(win->con->connection), G_IO_IN | G_IO_HUP | G_IO_ERR);
g_source_attach(win->source, NULL);
}
-static bool query_xrm(Window *win, const char *res, char **value)
-{
- if (xcb_xrm_resource_get_string(win->database,res, res, value) >= 0) {
- log_debug("Xrm query '%s' found '%s'", res, *value);
- return true;
- }
-
- log_debug("Xrm query '%s' not found", res);
- return false;
-}
-
static xcb_atom_t intern_atom(Window *win, const char *atom)
{
xcb_generic_error_t *error;
- xcb_intern_atom_cookie_t cookie = xcb_intern_atom(win->connection, false, strlen(atom), atom);
- xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(win->connection, cookie, &error);
+ xcb_intern_atom_cookie_t cookie = xcb_intern_atom(win->con->connection, false, strlen(atom), atom);
+ xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(win->con->connection, cookie, &error);
g_assert_null(error);
xcb_atom_t id = reply->atom;
@@ -182,7 +164,7 @@ static void wm_set_size(Window *win)
xcb_icccm_size_hints_set_base_size(&hints, win->width, win->height);
xcb_icccm_size_hints_set_position(&hints, false, win->x, win->y);
- xcb_icccm_set_wm_size_hints(win->connection, win->window, XCB_ATOM_WM_NORMAL_HINTS, &hints);
+ xcb_icccm_set_wm_size_hints(win->con->connection, win->window, XCB_ATOM_WM_NORMAL_HINTS, &hints);
log_debug("Xcb icccm updated size hints");
}
@@ -196,19 +178,19 @@ static void wm_set_struts(Window *win)
win->x, end, 0, 0, // top y0, top y1, bottom y0, bottom y1
};
- xcb_change_property(win->connection,
+ xcb_change_property(win->con->connection,
XCB_PROP_MODE_REPLACE,
win->window,
- win->ewmh._NET_WM_STRUT,
+ win->con->ewmh._NET_WM_STRUT,
XCB_ATOM_CARDINAL,
32,
4,
values);
- xcb_change_property(win->connection,
+ xcb_change_property(win->con->connection,
XCB_PROP_MODE_REPLACE,
win->window,
- win->ewmh._NET_WM_STRUT_PARTIAL,
+ win->con->ewmh._NET_WM_STRUT_PARTIAL,
XCB_ATOM_CARDINAL,
32,
12,
@@ -220,110 +202,42 @@ static void wm_set_struts(Window *win)
static void wm_setup(Window *win)
{
const char *title = "comet";
- xcb_icccm_set_wm_name(win->connection, win->window, XCB_ATOM_STRING, 8, strlen(title), title);
+ xcb_icccm_set_wm_name(win->con->connection, win->window, XCB_ATOM_STRING, 8, strlen(title), title);
log_debug("Window updated title [%s]", title);
const char class[] = "comet\0Comet";
- xcb_icccm_set_wm_class(win->connection, win->window, strlen(class), class);
+ xcb_icccm_set_wm_class(win->con->connection, win->window, strlen(class), class);
log_debug("Window updated class [%s]", "comet\\0Comet");
xcb_generic_error_t *error;
- xcb_intern_atom_cookie_t *ewmh_cookie = xcb_ewmh_init_atoms(win->connection, &win->ewmh);
+ xcb_intern_atom_cookie_t *ewmh_cookie = xcb_ewmh_init_atoms(win->con->connection, &win->con->ewmh);
- g_assert_true(xcb_ewmh_init_atoms_replies(&win->ewmh, ewmh_cookie, &error));
+ g_assert_true(xcb_ewmh_init_atoms_replies(&win->con->ewmh, ewmh_cookie, &error));
g_assert_null(error);
log_debug("Xcb ewmh connected");
- xcb_ewmh_set_wm_window_type(&win->ewmh, win->window, 1, &win->ewmh._NET_WM_WINDOW_TYPE_DOCK);
+ xcb_ewmh_set_wm_window_type(&win->con->ewmh, win->window, 1, &win->con->ewmh._NET_WM_WINDOW_TYPE_DOCK);
xcb_atom_t state[] = {
- win->ewmh._NET_WM_STATE_STICKY,
- win->ewmh._NET_WM_STATE_ABOVE
+ win->con->ewmh._NET_WM_STATE_STICKY,
+ win->con->ewmh._NET_WM_STATE_ABOVE
};
- xcb_ewmh_set_wm_state(&win->ewmh, win->window, G_N_ELEMENTS(state), state);
+ xcb_ewmh_set_wm_state(&win->con->ewmh, win->window, G_N_ELEMENTS(state), state);
- xcb_ewmh_set_wm_desktop(&win->ewmh, win->window, 0xFFFFFFFF);
+ xcb_ewmh_set_wm_desktop(&win->con->ewmh, win->window, 0xFFFFFFFF);
- xcb_ewmh_set_wm_pid(&win->ewmh, win->window, getpid());
+ xcb_ewmh_set_wm_pid(&win->con->ewmh, win->window, getpid());
wm_set_size(win);
wm_set_struts(win);
}
-Window *window_create(void)
+Window *window_create(Connection *con)
{
Window *win = g_malloc0(sizeof(Window));
-
- int preferred_screen = 0;
- win->connection = xcb_connect(NULL, &preferred_screen);
- g_assert_true(win->connection != NULL && !xcb_connection_has_error(win->connection));
- log_debug("Xcb connected");
-
- xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(win->connection));
- log_debug("Default screen %d", preferred_screen);
-
- while (preferred_screen != 0 && iter.rem) {
- xcb_screen_next(&iter);
- preferred_screen--;
- }
-
- win->screen = iter.data;
-
- xcb_randr_query_version_cookie_t version_cookie = xcb_randr_query_version(win->connection,
- XCB_RANDR_MAJOR_VERSION,
- XCB_RANDR_MINOR_VERSION);
-
- xcb_generic_error_t *error;
- xcb_randr_query_version_reply_t *randr_version = xcb_randr_query_version_reply(win->connection,
- version_cookie,
- &error);
-
- g_assert_null(error);
- log_debug("RandR loaded [version=%d.%d]", randr_version->major_version, randr_version->minor_version);
- g_assert_cmpint(randr_version->major_version, >=, 1);
-
- xcb_randr_get_screen_info_cookie_t cookie = xcb_randr_get_screen_info(win->connection, win->screen->root);
- xcb_randr_get_screen_info_reply_t *info_reply = xcb_randr_get_screen_info_reply(win->connection, cookie, &error);
- g_assert_null(error);
-
- win->screen_size = xcb_randr_get_screen_info_sizes(info_reply);
- g_assert_nonnull(win->screen_size);
- log_debug("Screen size [width=%d, height=%d]", win->screen_size->width, win->screen_size->height);
-
- win->width = win->height = 0;
- win->x = win->y = 0;
-
- xcb_randr_select_input(win->connection,
- win->screen->root,
- XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
- XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
- XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
-
- win->database = xcb_xrm_database_from_default(win->connection);
- g_assert_nonnull(win->database);
-
- window_update_scale(win);
-
- g_assert(xcb_errors_context_new(win->connection, &win->errors) == 0);
- log_debug("Xcb errors loaded");
-
- xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(win->screen);
- while (depth_iter.rem) {
- xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
-
- while (visual_iter.rem) {
- if (win->screen->root_visual == visual_iter.data->visual_id) {
- win->visual_type = visual_iter.data;
- goto found_visual;
- }
- xcb_visualtype_next(&visual_iter);
- }
- xcb_depth_next(&depth_iter);
- }
-
-found_visual:
- log_debug("Xcb visual type found [id=%u]", win->visual_type->visual_id);
+ g_assert_nonnull(win);
+ win->con = con;
const uint32_t value_mask = XCB_CW_BACK_PIXMAP | XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
| XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
@@ -332,8 +246,8 @@ found_visual:
| XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_FOCUS_CHANGE
| XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_BUTTON_PRESS;
- xcb_colormap_t colormap = xcb_generate_id(win->connection);
- xcb_create_colormap(win->connection, XCB_COLORMAP_ALLOC_NONE, colormap, win->screen->root, win->visual_type->visual_id);
+ xcb_colormap_t colormap = xcb_generate_id(win->con->connection);
+ xcb_create_colormap(win->con->connection, XCB_COLORMAP_ALLOC_NONE, colormap, win->con->screen->root, win->con->visual_type->visual_id);
log_debug("Xcb colormap created [id=%u]", colormap);
const uint32_t value_list[] = {
@@ -351,15 +265,15 @@ found_visual:
win->x = win->y = 0;
log_debug("Window temporary position [x=%d, y=%d]", win->x, win->y);
- win->window = xcb_generate_id(win->connection);
- xcb_create_window(win->connection,
+ win->window = xcb_generate_id(win->con->connection);
+ xcb_create_window(win->con->connection,
XCB_COPY_FROM_PARENT,
- win->window, win->screen->root,
+ win->window, win->con->screen->root,
win->x, win->y,
win->width, win->height,
0, // border
XCB_WINDOW_CLASS_INPUT_OUTPUT,
- win->screen->root_visual,
+ win->con->screen->root_visual,
value_mask,
value_list);
@@ -368,15 +282,15 @@ found_visual:
wm_setup(win);
log_debug("Window wm options completed");
- xcb_map_window(win->connection, win->window);
- xcb_flush(win->connection);
+ xcb_map_window(win->con->connection, win->window);
+ xcb_flush(win->con->connection);
log_debug("Xcb initialized");
- win->surface = cairo_xcb_surface_create(win->connection,
+ win->surface = cairo_xcb_surface_create(win->con->connection,
win->window,
- win->visual_type,
- win->screen_size->width,
- win->screen_size->height);
+ win->con->visual_type,
+ win->con->screen_size->width,
+ win->con->screen_size->height);
g_assert_cmpint(cairo_surface_status(win->surface), ==, CAIRO_STATUS_SUCCESS);
log_debug("Cairo surface created");
@@ -397,29 +311,15 @@ cairo_t *window_get_context(Window *win)
return win->cr;
}
-void window_update_scale(Window *win)
-{
- const char *dpi_res = "Xft.dpi";
- char *dpi_value;
-
- if (query_xrm(win, dpi_res, &dpi_value)) {
- win->dpi = strtod(dpi_value, NULL);
- g_free(dpi_value);
- } else {
- win->dpi = (double)win->screen_size->height * 25.4 / (double)win->screen_size->mheight;
- log_debug("Fallback dpi value '%.2lf'", win->dpi);
- }
-}
-
double window_get_scale(Window *win)
{
- return MAX(1, win->dpi/96.);
+ return MAX(1, win->con->screen_dpi/96.);
}
void window_get_screen_size(Window *win, int *width, int *height)
{
- *width = win->screen_size->width;
- *height = win->screen_size->height;
+ *width = win->con->screen_size->width;
+ *height = win->con->screen_size->height;
}
void window_set_opacity(Window *win, double alpha)
@@ -429,7 +329,7 @@ void window_set_opacity(Window *win, double alpha)
unsigned long opacity = 0xffffffff * alpha;
xcb_atom_t _NET_WM_WINDOW_OPACITY = intern_atom(win, "_NET_WM_WINDOW_OPACITY");
- xcb_change_property(win->connection,
+ xcb_change_property(win->con->connection,
XCB_PROP_MODE_REPLACE,
win->window,
_NET_WM_WINDOW_OPACITY,
@@ -450,7 +350,7 @@ void window_move(Window *win, int x, int y)
win->y = y;
const uint32_t values[] = { x, y };
- xcb_configure_window(win->connection,
+ xcb_configure_window(win->con->connection,
win->window,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
values);
@@ -466,7 +366,7 @@ void window_resize(Window *win, int width, int height)
win->height = height;
const uint32_t values[] = { width, height };
- xcb_configure_window(win->connection,
+ xcb_configure_window(win->con->connection,
win->window,
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
values);
@@ -485,12 +385,12 @@ static void window_paint_corners(Window *win)
// TODO: Check this value
int depth = 1;
- xcb_pixmap_t bitmap = xcb_generate_id(win->connection);
- xcb_create_pixmap(win->connection, depth, bitmap, win->window, win->width, win->height);
+ xcb_pixmap_t bitmap = xcb_generate_id(win->con->connection);
+ xcb_create_pixmap(win->con->connection, depth, bitmap, win->window, win->width, win->height);
log_debug("Xcb pixmap created [id=%u]", bitmap);
- cairo_surface_t *surface = cairo_xcb_surface_create_for_bitmap(win->connection,
- win->screen,
+ cairo_surface_t *surface = cairo_xcb_surface_create_for_bitmap(win->con->connection,
+ win->con->screen,
bitmap,
win->width,
win->height);
@@ -519,17 +419,17 @@ static void window_paint_corners(Window *win)
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
- xcb_shape_mask(win->connection, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING, win->window, 0, 0, bitmap);
- xcb_shape_select_input(win->connection, win->window, XCB_SHAPE_NOTIFY);
+ xcb_shape_mask(win->con->connection, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING, win->window, 0, 0, bitmap);
+ xcb_shape_select_input(win->con->connection, win->window, XCB_SHAPE_NOTIFY);
log_debug("Xcb shape mask configured");
- xcb_free_pixmap(win->connection, bitmap);
+ xcb_free_pixmap(win->con->connection, bitmap);
}
void window_paint_surface(Window *win, cairo_surface_t *surface, int width, int height)
{
cairo_xcb_surface_set_size(win->surface, width, height);
- xcb_clear_area(win->connection, false, win->window, 0, 0, 0, 0);
+ xcb_clear_area(win->con->connection, false, win->window, 0, 0, 0, 0);
cairo_set_source_surface(win->cr, surface, 0, 0);
cairo_paint(win->cr);
@@ -537,8 +437,8 @@ void window_paint_surface(Window *win, cairo_surface_t *surface, int width, int
window_paint_corners(win);
- xcb_circulate_window(win->connection, XCB_CIRCULATE_RAISE_LOWEST, win->window);
- xcb_flush(win->connection);
+ xcb_circulate_window(win->con->connection, XCB_CIRCULATE_RAISE_LOWEST, win->window);
+ xcb_flush(win->con->connection);
}
void window_destroy(Window *win)
@@ -549,10 +449,6 @@ void window_destroy(Window *win)
cairo_destroy(win->cr);
cairo_surface_destroy(win->surface);
- xcb_ewmh_connection_wipe(&win->ewmh);
- xcb_errors_context_free(win->errors);
- xcb_xrm_database_free(win->database);
- xcb_disconnect(win->connection);
g_free(win);
}
diff --git a/x11.h b/window.h
index daf6aff..009966a 100644
--- a/x11.h
+++ b/window.h
@@ -1,22 +1,22 @@
-#ifndef COMET_X11_H
-#define COMET_X11_H
+#ifndef COMET_WINDOW_H
+#define COMET_WINDOW_H
#include <cairo.h>
+#include "connection.h"
+
typedef struct Window Window;
-Window *window_create(void);
+Window *window_create(Connection *con);
cairo_t *window_get_context(Window *win);
-void window_update_scale(Window *win);
-
double window_get_scale(Window *win);
-void window_get_screen_size(Window *win, int *width, int *height);
-
void window_set_opacity(Window *win, double alpha);
+void window_get_screen_size(Window *win, int *width, int *height);
+
void window_move(Window *win, int x, int y);
void window_resize(Window *win, int width, int height);