예제 #1
0
파일: xcb.c 프로젝트: freexploit/i3wm
/*
 * Returns the ID of the font matching the given pattern and stores the height
 * of the font (in pixels) in *font_height. die()s if no font matches.
 *
 */
int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
        xcb_void_cookie_t font_cookie;
        xcb_list_fonts_with_info_cookie_t info_cookie;

        /* Send all our requests first */
        int result;
        result = xcb_generate_id(conn);
        font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
        info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);

        xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
        if (error != NULL) {
                fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
                exit(1);
        }

        /* Get information (height/name) for this font */
        xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
        if (reply == NULL)
                errx(1, "Could not load font \"%s\"\n", pattern);

        *font_height = reply->font_ascent + reply->font_descent;

        return result;
}
예제 #2
0
파일: nilwm.c 프로젝트: nqv/nilwm
static
int init_font() {
    xcb_void_cookie_t cookie;
    xcb_generic_error_t *err;
    xcb_list_fonts_with_info_cookie_t info_cookie;
    xcb_list_fonts_with_info_reply_t *info;

    /* open font */
    nil_.font.id = xcb_generate_id(nil_.con);
    cookie = xcb_open_font_checked(nil_.con, nil_.font.id,
        strlen(cfg_.font_name), cfg_.font_name);
    err = xcb_request_check(nil_.con, cookie);
    if (err) {
        NIL_ERR("open font: %d", err->error_code);
        return -1;
    }
    info_cookie = xcb_list_fonts_with_info(nil_.con, 1,
        strlen(cfg_.font_name), cfg_.font_name);
    info = xcb_list_fonts_with_info_reply(nil_.con, info_cookie, 0);
    if (!info) {
        NIL_ERR("load font: %s", cfg_.font_name);
        return -1;
    }
    NIL_LOG("font ascent=%d, descent=%d", info->font_ascent, info->font_descent);
    nil_.font.ascent = info->font_ascent;
    nil_.font.descent = info->font_descent;
    free(info);
    return 0;
}
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;
}
예제 #4
0
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);
  }
}
예제 #5
0
xcb_gc_t get_font_gc(xcb_connection_t *dpy, xcb_window_t win, const char *font_name)
{
	xcb_void_cookie_t ck;
	xcb_font_t font = xcb_generate_id(dpy);
	ck = xcb_open_font_checked(dpy, font, strlen(font_name), font_name);
	check_request(dpy, ck, "Can't open font");
	xcb_gcontext_t gc = xcb_generate_id(dpy);
	uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
	uint32_t values[] = {0xffcccccc, 0xff111111, font};
	xcb_create_gc(dpy, gc, win, mask, values);
	xcb_close_font(dpy, font);
	return gc;
}
예제 #6
0
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 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");
}
예제 #8
0
  /** \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);
    }
  }
예제 #9
0
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);
    }
}