diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-03 20:31:28 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-03 20:31:28 +0100 |
| commit | 58eb86428f90926d25219f4e15b80945b00b1810 (patch) | |
| tree | d2e8976bcd7264d38a0f4d4b9772b50e72eac9b8 /x11.c | |
| parent | 04f73ccab99a28d073dd0629e1b43aebdfab0861 (diff) | |
Calculate window size and position
Diffstat (limited to 'x11.c')
| -rw-r--r-- | x11.c | 81 |
1 files changed, 63 insertions, 18 deletions
@@ -10,6 +10,7 @@ #include <xcb/xcb_icccm.h> #include <xcb/xcb_xrm.h> #include <xcb/randr.h> +#include <xcb/xcb_atom.h> #include "x11.h" #include "log.h" @@ -25,6 +26,13 @@ struct Window { double dpi; }; +static xcb_atom_t intern_atom(Window *win, const char *atom) +{ + // TODO: Error handling + xcb_intern_atom_cookie_t cookie = xcb_intern_atom(win->connection, false, strlen(atom), atom); + return xcb_intern_atom_reply(win->connection, cookie, NULL)->atom; +} + Window *window_create(void) { Window *win = g_malloc0(sizeof(Window)); @@ -34,6 +42,16 @@ Window *window_create(void) assert(win->connection != NULL && !xcb_connection_has_error(win->connection)); log_debug("Xcb established connection"); + 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); @@ -46,6 +64,13 @@ Window *window_create(void) log_debug("RandR version %d.%d", randr_version->major_version, randr_version->minor_version); assert(error == NULL || 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); + assert(error == NULL); + + xcb_randr_screen_size_t *screen_size = xcb_randr_get_screen_info_sizes(info_reply); + log_debug("Screen size [width=%d, height=%d]", screen_size->width, screen_size->height); + xcb_randr_select_input(win->connection, win->window, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE | @@ -58,25 +83,14 @@ Window *window_create(void) char *dpi_value; if (xcb_xrm_resource_get_string(win->database, "Xft.dpi", "Xft.dpi", &dpi_value) >= 0) { log_debug("Xrm query '%s' found '%s'", "Xft.dpi", dpi_value); - win->dpi = strtod(dpi_value, NULL); g_free(dpi_value); } else { log_debug("Xrm query '%s' not found", "Xft.dpi"); - - // TODO: Calculate automatically somehow win->dpi = 96.0; + //win->dpi = (double)screen_size->height * 25.4 / (double)screen_size->mheight; + log_debug("Fallback dpi value '%lf'", win->dpi); } - - 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_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(win->screen); while (depth_iter.rem) { @@ -106,7 +120,7 @@ found_visual: 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); - log_debug("X11 colormap created (id %u)", colormap); + log_debug("Xcb colormap created (id %u)", colormap); const uint32_t value_list[] = { XCB_NONE, // back pixmap @@ -117,24 +131,55 @@ found_visual: colormap // colormap }; - int y = 10, x = 10, width = 1000, height = 100; + int width = screen_size->width * 0.98; + int height = screen_size->height * 0.03; + + width -= width % 2 != 0; + height -= height % 2 != 0; + + log_debug("Window size [width=%d, height=%d]", width, height); + + int x = (screen_size->width - width) / 2.0; + int y = screen_size->height * 0.0125; + + log_debug("Window position [x=%d, y=%d]", x, y); win->window = xcb_generate_id(win->connection); xcb_create_window(win->connection, XCB_COPY_FROM_PARENT, win->window, win->screen->root, - y, x, width, height, 0, + x, y, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, win->screen->root_visual, value_mask, value_list); - log_debug("X11 window created (id %u)", win->window); + log_debug("Xcb window created (id %u)", win->window); xcb_icccm_set_wm_name(win->connection, win->window, XCB_ATOM_STRING, 8, strlen("comet"), "comet"); xcb_map_window(win->connection, win->window); - xcb_flush(win->connection); + xcb_atom_t NET_WM_WINDOW_OPACITY = intern_atom(win, "_NET_WM_WINDOW_OPACITY"); + xcb_atom_t CARDINAL = intern_atom(win, "CARDINAL"); + + // Value between 0 and 1 + double transparency = 0.8; + unsigned long opacity = 0xffffffff * transparency; + + //error = + //xcb_request_check(win->connection, + //xcb_change_property_checked(win->connection, + // XCB_PROP_MODE_REPLACE, + // win->window, + // NET_WM_WINDOW_OPACITY, + // CARDINAL, + // 32, + // 1, + // (char *)&opacity)); + //assert(error == NULL); + //log_debug("Window '%s' set to '%d%%'", "opacity", (int)round(100 - 100 * transparency)); + + xcb_flush(win->connection); log_debug("Xcb initialized"); win->surface = cairo_xcb_surface_create(win->connection, win->window, win->visual_type, width, height); |
