static void cursor_set (xcb_connection_t *c, xcb_screen_t *screen, xcb_window_t window, int cursor_id) { uint32_t values_list[3]; xcb_void_cookie_t cookie_font; xcb_void_cookie_t cookie_gc; xcb_generic_error_t *error; xcb_font_t font; xcb_cursor_t cursor; xcb_gcontext_t gc; uint32_t mask; uint32_t value_list; font = xcb_generate_id (c); cookie_font = xcb_open_font_checked (c, font, strlen ("cursor"), "cursor"); error = xcb_request_check (c, cookie_font); if (error) { fprintf (stderr, "ERROR: can't open font : %d\n", error->error_code); xcb_disconnect (c); exit (-1); } cursor = xcb_generate_id (c); xcb_create_glyph_cursor (c, cursor, font, font, cursor_id, cursor_id + 1, 0, 0, 0, 0, 0, 0); gc = xcb_generate_id (c); mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; values_list[0] = screen->black_pixel; values_list[1] = screen->white_pixel; values_list[2] = font; cookie_gc = xcb_create_gc_checked (c, gc, window, mask, values_list); error = xcb_request_check (c, cookie_gc); if (error) { fprintf (stderr, "ERROR: can't create gc : %d\n", error->error_code); xcb_disconnect (c); exit (-1); } mask = XCB_CW_CURSOR; value_list = cursor; xcb_change_window_attributes (c, window, mask, &value_list); xcb_free_cursor (c, cursor); cookie_font = xcb_close_font_checked (c, font); error = xcb_request_check (c, cookie_font); if (error) { fprintf (stderr, "ERROR: can't close font : %d\n", error->error_code); xcb_disconnect (c); exit (-1); } }
static xcb_gc_t getFontGC(xcb_connection_t *connection, xcb_screen_t *screen, xcb_window_t window, const char *fontName) { xcb_font_t font = xcb_generate_id(connection); xcb_void_cookie_t fontCookie = xcb_open_font_checked(connection, font, strlen(fontName), fontName); testCookie(fontCookie, connection, "can't open font"); xcb_gcontext_t gc = xcb_generate_id(connection); uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; uint32_t value_list[3]; value_list[0] = screen->black_pixel; value_list[1] = screen->white_pixel; value_list[2] = font; xcb_void_cookie_t gcCookie = xcb_create_gc_checked(connection, gc, window, mask, value_list); testCookie(gcCookie, connection, "can't create gc"); fontCookie = xcb_close_font_checked(connection, font); testCookie(fontCookie, connection, "can't close font"); return gc; }
int main(int argc, char **argv) { uint32_t width = test_width - 2 * INSET_X; uint32_t height = test_height - 2 * INSET_Y; int snum; xcb_void_cookie_t check_cookie; xcb_window_t w; xcb_gcontext_t gc; xcb_pixmap_t pix; xcb_connection_t *c = xcb_connect(0, &snum); xcb_screen_t *s = xcb_aux_get_screen(c, snum); xcb_alloc_named_color_cookie_t bg_cookie = xcb_alloc_named_color(c, s->default_colormap, strlen("white"), "white"); xcb_alloc_named_color_cookie_t fg_cookie = xcb_alloc_named_color(c, s->default_colormap, strlen("black"), "black"); xcb_alloc_named_color_reply_t *bg_reply = xcb_alloc_named_color_reply(c, bg_cookie, 0); xcb_alloc_named_color_reply_t *fg_reply = xcb_alloc_named_color_reply(c, fg_cookie, 0); uint32_t fg, bg; xcb_image_t *image, *native_image, *subimage; uint32_t mask = 0; xcb_params_gc_t gcv; assert(bg_reply && fg_reply); bg = bg_reply->pixel; fg = fg_reply->pixel; free(bg_reply); free(fg_reply); w = make_window(c, s, bg, fg, width, height); gc = xcb_generate_id(c); check_cookie = xcb_create_gc_checked(c, gc, w, 0, 0); assert(!xcb_request_check(c, check_cookie)); image = xcb_image_create_from_bitmap_data((uint8_t *)test_bits, test_width, test_height); native_image = xcb_image_native(c, image, 1); assert(native_image); if (native_image != image) xcb_image_destroy(image); subimage = xcb_image_subimage(native_image, INSET_X, INSET_Y, width, height, 0, 0, 0); assert(subimage); xcb_image_destroy(native_image); subimage->format = XCB_IMAGE_FORMAT_XY_BITMAP; pix = xcb_generate_id(c); xcb_create_pixmap(c, s->root_depth, pix, w, subimage->width, subimage->height); gc = xcb_generate_id(c); XCB_AUX_ADD_PARAM(&mask, &gcv, foreground, fg); XCB_AUX_ADD_PARAM(&mask, &gcv, background, bg); xcb_aux_create_gc(c, gc, pix, mask, &gcv); xcb_image_put(c, pix, gc, subimage, 0, 0, 0); process_events(c, gc, w, pix, width, height); xcb_disconnect(c); return 1; }
static xcb_gc_t gc_font_get (xcb_connection_t *c, xcb_screen_t *screen, xcb_window_t window, const char *font_name) { uint32_t value_list[3]; xcb_void_cookie_t cookie_font; xcb_void_cookie_t cookie_gc; xcb_generic_error_t *error; xcb_font_t font; xcb_gcontext_t gc; uint32_t mask; font = xcb_generate_id (c); cookie_font = xcb_open_font_checked (c, font, strlen (font_name), font_name); error = xcb_request_check (c, cookie_font); if (error) { fprintf (stderr, "ERROR: can't open font : %d\n", error->error_code); xcb_disconnect (c); return -1; } gc = xcb_generate_id (c); mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; value_list[0] = screen->black_pixel; value_list[1] = screen->white_pixel; value_list[2] = font; cookie_gc = xcb_create_gc_checked (c, gc, window, mask, value_list); error = xcb_request_check (c, cookie_gc); if (error) { fprintf (stderr, "ERROR: can't create gc : %d\n", error->error_code); xcb_disconnect (c); exit (-1); } cookie_font = xcb_close_font_checked (c, font); error = xcb_request_check (c, cookie_font); if (error) { fprintf (stderr, "ERROR: can't close font : %d\n", error->error_code); xcb_disconnect (c); exit (-1); } return gc; }
static int init_bar() { uint32_t vals[4]; xcb_void_cookie_t cookie; xcb_generic_error_t *err; /* create status bar window at the bottom */ bar_.w = nil_.scr->width_in_pixels; bar_.h = nil_.font.ascent + nil_.font.descent + 2; bar_.x = 0; bar_.y = nil_.scr->height_in_pixels - bar_.h; bar_.win = xcb_generate_id(nil_.con); vals[0] = XCB_BACK_PIXMAP_PARENT_RELATIVE; vals[1] = 1; /* override_redirect */ vals[2] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_EXPOSURE; vals[3] = nil_.cursor[CURSOR_NORMAL]; cookie = xcb_create_window_checked(nil_.con, nil_.scr->root_depth, bar_.win, nil_.scr->root, bar_.x, bar_.y, bar_.w, bar_.h, 0, XCB_COPY_FROM_PARENT, nil_.scr->root_visual, XCB_CW_BACK_PIXMAP | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_CURSOR, vals); err = xcb_request_check(nil_.con, cookie); if (err) { NIL_ERR("create window %d", err->error_code); return -1; } vals[0] = XCB_STACK_MODE_ABOVE; xcb_configure_window(nil_.con, bar_.win, XCB_CONFIG_WINDOW_STACK_MODE, &vals[0]); cookie = xcb_map_window_checked(nil_.con, bar_.win); err = xcb_request_check(nil_.con, cookie); if (err) { NIL_ERR("map window %d", err->error_code); return -1; } /* graphic context */ bar_.gc = xcb_generate_id(nil_.con); vals[0] = nil_.color.bar_fg; vals[1] = nil_.color.bar_bg; vals[2] = nil_.font.id; cookie = xcb_create_gc_checked(nil_.con, bar_.gc, bar_.win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT, vals); err = xcb_request_check(nil_.con, cookie); if (err) { NIL_ERR("map window %d", err->error_code); return -1; } return 0; }
/* * Initialize the surface to represent the given drawable. * */ void draw_util_surface_init(xcb_connection_t *conn, surface_t *surface, xcb_drawable_t drawable, xcb_visualtype_t *visual, int width, int height) { surface->id = drawable; surface->visual_type = ((visual == NULL) ? visual_type : visual); surface->width = width; surface->height = height; surface->gc = xcb_generate_id(conn); xcb_void_cookie_t gc_cookie = xcb_create_gc_checked(conn, surface->gc, surface->id, 0, NULL); xcb_generic_error_t *error = xcb_request_check(conn, gc_cookie); if (error != NULL) { ELOG("Could not create graphical context. Error code: %d. Please report this bug.\n", error->error_code); } surface->surface = cairo_xcb_surface_create(conn, surface->id, surface->visual_type, width, height); surface->cr = cairo_create(surface->surface); }
X11BufferSurface::X11BufferSurface(X11WindowContext& wc) : windowContext_(&wc) { gc_ = xcb_generate_id(&xConnection()); std::uint32_t value[] = {0, 0}; auto c = xcb_create_gc_checked(&xConnection(), gc_, wc.xWindow(), XCB_GC_FOREGROUND, value); windowContext().errorCategory().checkThrow(c, "ny::X11BufferSurface: create_gc"); //query the format //this is needed because the xserver may need a different bpp for an image //with the depth of the window. //For 24-bit depth images the xserver often required 32 bpp. auto setup = xcb_get_setup(&xConnection()); auto fmtit = xcb_setup_pixmap_formats(setup); auto fmtend = fmtit + xcb_setup_pixmap_formats_length(setup); xcb_format_t* fmt {}; while(fmtit != fmtend) { if(fmtit->depth == windowContext().visualDepth()) { fmt = fmtit; break; } ++fmtit; } if(!fmt) throw std::runtime_error("ny::X11BufferSurface: couldn't query depth format bpp"); format_ = visualToFormat(*windowContext().xVisualType(), fmt->bits_per_pixel); if(format_ == ImageDataFormat::none) throw std::runtime_error("ny::X11BufferSurface: couldn't parse visual format"); //check if the server has shm suport //it is also implemented without shm but the performance might be worse auto cookie = xcb_shm_query_version(&xConnection()); auto reply = xcb_shm_query_version_reply(&xConnection(), cookie, nullptr); shm_ = reply; if(reply) free(reply); if(!shm_) warning("ny::X11BufferSurface: shm server does not support shm extension"); }
static void setCursor(xcb_connection_t *connection, xcb_screen_t *screen, xcb_window_t window, int cursorId) { xcb_font_t font = xcb_generate_id(connection); xcb_void_cookie_t fontCookie = xcb_open_font_checked(connection, font, strlen("cursor"), "cursor"); testCookie(fontCookie, connection, "can't open font"); xcb_cursor_t cursor = xcb_generate_id(connection); xcb_create_glyph_cursor(connection, cursor, font, font, cursorId, cursorId + 1, 0,0,0,0,0,0); xcb_gcontext_t gc = xcb_generate_id(connection); uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; uint32_t values_list[3]; values_list[0] = screen->black_pixel; values_list[1] = screen->white_pixel; values_list[2] = font; xcb_void_cookie_t gcCookie = xcb_create_gc_checked(connection, gc, window, mask, values_list); testCookie(gcCookie, connection, "can't create gc"); mask = XCB_CW_CURSOR; uint32_t value_list = cursor; xcb_change_window_attributes(connection, window, mask, &value_list); xcb_free_cursor(connection, cursor); fontCookie = xcb_close_font_checked(connection, font); testCookie(fontCookie, connection, "can't close font"); }
/** \param connection connection to a X sever \param screen screen, where the window will be created \param parent parent window (0 for screen->root) \param windowed means no fullscreen */ scr_window(xcb_connection_t *connection, xcb_screen_t *screen, xcb_window_t parent, bool windowed) : con(connection), scr(screen), client_win(0) { uint32_t mask; uint32_t values[3]; xcb_void_cookie_t cookie; xcb_generic_error_t *error = NULL; if(!parent) parent = scr->root; // use parent window size when not in windowed mode if(!windowed) { xcb_get_geometry_cookie_t geo_cookie = xcb_get_geometry(con, parent); xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply(con, geo_cookie, &error); if(error) { std::cerr << "Could not get parent window geometry." << std::endl; exit(1); } width = reply->width; height = reply->height; free(reply); } else { // use some defaults in windowed mode width = 640; height = 480; } if(windowed) { // create a black maybe override-redirected window // and register for expose and resize events. mask = XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK; values[0] = scr->black_pixel; values[1] = !windowed; // only if in fullscreen mode, otherwise normal window values[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; win = xcb_generate_id(con); cookie = xcb_create_window_checked( con, XCB_COPY_FROM_PARENT, win, parent, 0, 0, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, mask, values ); error = xcb_request_check(con, cookie); if(error) { std::cerr << "Could not create window." << std::endl; exit(1); } // map the window on the screen xcb_map_window(con, win); xcb_flush(con); } else { // directly use the parent window win = parent; // cahnge window attributes like above mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; values[0] = scr->black_pixel; values[1] = XCB_EVENT_MASK_EXPOSURE; xcb_void_cookie_t cookie = xcb_change_window_attributes(con, win, mask, values); xcb_generic_error_t *error = xcb_request_check(con, cookie); if(error) { std::cerr << "Could not configure window." << std::endl; exit(1); } } // open a font. "fixed" should hopefully be available everywhere font = xcb_generate_id(con); std::string font_name = "fixed"; cookie = xcb_open_font_checked(con, font, font_name.size(), font_name.c_str()); error = xcb_request_check(con, cookie); if(error) { std::cerr << "Could not open font " << font_name << "." << std::endl; exit(1); } // allocate white text graphics context with above font txt_gc = xcb_generate_id(con); mask = XCB_GC_FOREGROUND | XCB_GC_FONT; values[0] = scr->white_pixel; values[1] = font; cookie = xcb_create_gc_checked(con, txt_gc, win, mask, values); error = xcb_request_check(con, cookie); if(error) { std::cerr << "Could not create graphics context." << std::endl; exit(1); } }
void draw(xcb_connection_t *connection, xcb_screen_t *screen, xcb_window_t window) { if (NULL != testcase) { DRAW_FUNC pf = (DRAW_FUNC)dlsym(0, testcase); if (NULL != pf) { fprintf(stderr, "call %s \n", testcase); pf(); return; } } //get_version(); xcb_font_t font = xcb_generate_id(connection); xcb_void_cookie_t cookie_font = xcb_open_font_checked(connection, font, strlen(font_name_pattern), font_name_pattern); xcb_generic_error_t *error = xcb_request_check(connection, cookie_font); if (error) { fprintf(stderr, "ERROR: can't open font :%d\n", error->error_code); xcb_disconnect(connection); exit(0); } xcb_gcontext_t gc = xcb_generate_id(connection); uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT; uint32_t value_list[3]; value_list[0] = screen->black_pixel; value_list[1] = screen->white_pixel; value_list[2] = font; xcb_void_cookie_t cookie_gc = xcb_create_gc_checked(connection, gc, window, mask, value_list); error = xcb_request_check(connection, cookie_gc); if (error) { fprintf(stderr, "ERROR: can't create gc: %d\n", error->error_code); xcb_disconnect(connection); exit(0); } cookie_font = xcb_close_font_checked(connection, font); error = xcb_request_check(connection, cookie_font); if (error) { fprintf(stderr, "ERROR: can't close font: %d\n", error->error_code); xcb_disconnect(connection); exit(0); } glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); glColor3f(1.0f, 0.0f, 0.0f); glRasterPos2f(0.0f, 0.0f); glPushAttrib(GL_LIST_BIT); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); static int isFirstCall = 1; static GLuint lists; if (isFirstCall) { lists = glGenLists(128); } #if 1 //for (i = 0; i < sizeof(letters) glNewList(lists + 'A', GL_COMPILE); glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[0]); glEndList(); GLubyte *str = (GLubyte*)"A"; #else glXUseXFont(font, 0, 128, lists); GLubyte *str = (GLubyte*)"Hello, world."; #endif glListBase(lists); glCallLists(strlen(str), GL_UNSIGNED_BYTE, str); glPopAttrib(); glFlush(); /* static int isFirstCall = 1; static GLuint lists; //load font //gen list if (isFirstCall) { lists = glGenLists(128); } glXUseXFont(font, 0, 128, lists); glListBase(lists); char *str = "Hello, world"; for (; *str!='\0';++str) { glCallList(lists + *str); } glFlush(); //glDeleteLists(lists, 256); //use font //draw text //unuse font? //delete list //unload font*/ cookie_gc = xcb_free_gc(connection, gc); error = xcb_request_check(connection, cookie_gc); if (error) { fprintf(stderr, "ERROR: can't free gc: %d\n", error->error_code); xcb_disconnect(connection); exit(0); } }