#include #include #include #include #include #include #include #include #include #include #include #include #include "x11.h" #include "log.h" // TODO: Event logic should be decoupled from Window logic #include "draw.h" struct Window { xcb_screen_t *screen; xcb_connection_t *connection; xcb_xrm_database_t *database; xcb_drawable_t window; xcb_visualtype_t *visual_type; cairo_surface_t *surface; cairo_t *cr; double dpi; uint16_t screen_width; uint16_t screen_height; int x, y; int width, height; GSource *source; }; typedef struct { GSource source; Window *win; xcb_generic_event_t *event; } XcbSource; 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; } 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); return xsource->event != NULL; } static gboolean xcb_source_dispatch(GSource *source, GSourceFunc callback, gpointer data) { XcbSource *xsource = (XcbSource *)source; g_assert_nonnull(xsource->event); g_assert_nonnull(callback); g_assert_nonnull(data); do { switch (xsource->event->response_type & ~0x80) { case 0: { xcb_generic_error_t *error = (xcb_generic_error_t *)xsource->event; // TODO: This code crashes the program, so we should handle the errors log_warning("Xcb event loop error '%s' [type %d]", xcb_event_get_error_label(error->response_type), error->response_type); break; } case XCB_EXPOSE: { xcb_expose_event_t *expose = (xcb_expose_event_t *)xsource->event; log_debug("Processing event 'Expose' [type %d]", XCB_EXPOSE); // Redraw callback(data); break; } case XCB_BUTTON_PRESS: { log_debug("Keypress event"); break; } default: { log_debug("Ignoring event '%s' [type %d]", xcb_event_get_label(xsource->event->response_type), XCB_EVENT_RESPONSE_TYPE(xsource->event)); } } } while ((xsource->event = xcb_poll_for_event(xsource->win->connection)) != NULL); return G_SOURCE_CONTINUE; } static void xcb_source_finalize(GSource *source) { log_debug("Xcb event loop finalized"); } static GSourceFuncs source_fns = { NULL, xcb_source_check, xcb_source_dispatch, xcb_source_finalize, }; static void attach_source(Window *win) { XcbSource *source = (XcbSource *)g_source_new(&source_fns, sizeof(XcbSource)); source->win = win; source->event = NULL; win->source = (GSource *)source; g_source_set_static_name(win->source, "XcbSource"); // 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_attach(win->source, NULL); } Window *window_create(void) { 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 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); xcb_generic_error_t *error; xcb_randr_query_version_reply_t *randr_version = xcb_randr_query_version_reply(win->connection, version_cookie, &error); log_debug("RandR version %d.%d", randr_version->major_version, randr_version->minor_version); g_assert_true(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); g_assert_null(error); 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); win->screen_width = screen_size->width; win->screen_height = screen_size->height; win->width = win->height = 0; win->x = win->y = 0; xcb_randr_select_input(win->connection, win->window, 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); 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"); 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_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); 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; const uint32_t event_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | 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); log_debug("Xcb colormap created (id %u)", colormap); const uint32_t value_list[] = { XCB_NONE, // back pixmap 0x000000, // back pixel 0x000000, // border pixel true, // override redirect event_mask, // event mask colormap // colormap }; win->width = win->height = 1; log_debug("Window temporary size [width=%d, height=%d]", win->width, win->height); 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, XCB_COPY_FROM_PARENT, win->window, win->screen->root, win->x, win->y, win->width, win->height, 0, // border XCB_WINDOW_CLASS_INPUT_OUTPUT, win->screen->root_visual, value_mask, value_list); 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_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, screen_size->width, screen_size->height); g_assert_cmpint(cairo_surface_status(win->surface), ==, CAIRO_STATUS_SUCCESS); win->cr = cairo_create(win->surface); g_assert_cmpint(cairo_status(win->cr), ==, CAIRO_STATUS_SUCCESS); log_debug("Cairo initialized"); attach_source(win); log_debug("Xcb event loop attached"); return win; } cairo_t *window_get_context(Window *win) { return win->cr; } double window_get_scale(Window *win) { return MAX(1, win->dpi/96.); } void window_get_screen_size(Window *win, int *width, int *height) { *width = win->screen_width; *height = win->screen_height; } void window_move(Window *win, int x, int y) { if (win->x == x && win->y == y) return; win->x = x; win->y = y; const uint32_t values[] = { x, y }; xcb_configure_window(win->connection, win->window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values); log_debug("Window updated position [x=%d, y=%d]", win->x, win->y); } void window_resize(Window *win, int width, int height) { if (win->width == width && win->height == height) return; win->width = width; win->height = height; const uint32_t values[] = { width, height }; xcb_configure_window(win->connection, win->window, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, values); log_debug("Window updated size [width=%d, height=%d]", win->width, win->height); } 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); cairo_set_source_surface(win->cr, surface, 0, 0); cairo_paint(win->cr); cairo_show_page(win->cr); xcb_flush(win->connection); } void window_destroy(Window *win) { g_source_destroy(win->source); g_source_unref(win->source); cairo_destroy(win->cr); cairo_surface_destroy(win->surface); xcb_xrm_database_free(win->database); xcb_disconnect(win->connection); g_free(win); } // vim: ts=4 sw=4 et