XCB

實作X11顯示伺服器協定客戶端的函式庫

XCB(X C Binding)是一套以 C語言撰寫,並用於綁定(Binding) X Window System之上。XCB是一套免費的軟體,目標在於取代 Xlib。這個計劃開始於2001年,作者是Bart Massey.

XCB
開發者Jamey Sharp、Josh Triplett、Bart Massey
目前版本1.14(2020年2月22日,​4年前​(2020-02-22 [1]
原始碼庫 編輯維基數據鏈結
作業系統POSIX
類型X Window核心協定開發函式庫
授權條款MIT授權條款
網站xcb.freedesktop.org

理念

XCB 主要目標是:

範例

/* Simple XCB application drawing a box in a window */
 
#include <xcb/xcb.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  xcb_connection_t    *c;
  xcb_screen_t        *s;
  xcb_window_t         w;
  xcb_gcontext_t       g;
  xcb_generic_event_t *e;
  uint32_t             mask;
  uint32_t             values[2];
  int                  done = 0;
  xcb_rectangle_t      r = { 20, 20, 60, 60 };
 
                        /* open connection with the server */
  c = xcb_connect(NULL,NULL);
  if (xcb_connection_has_error(c)) {
    printf("Cannot open display\n");
    exit(1);
  }
                        /* get the first screen */
  s = xcb_setup_roots_iterator( xcb_get_setup(c) ).data;

                       /* create black graphics context */
  g = xcb_generate_id(c);
  w = s->root;
  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  values[0] = s->black_pixel;
  values[1] = 0;
  xcb_create_gc(c, g, w, mask, values);
 
                       /* create window */
  w = xcb_generate_id(c);
  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
  values[0] = s->white_pixel;
  values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
  xcb_create_window(c, s->root_depth, w, s->root,
                    10, 10, 100, 100, 1,
                    XCB_WINDOW_CLASS_INPUT_OUTPUT, s->root_visual,
                    mask, values);
 
                        /* map (show) the window */
  xcb_map_window(c, w);
   
  xcb_flush(c);
 
                        /* event loop */
  while (!done && (e = xcb_wait_for_event(c))) {
    switch (e->response_type & ~0x80) {
    case XCB_EXPOSE:    /* draw or redraw the window */
      xcb_poly_fill_rectangle(c, w, g,  1, &r);
      xcb_flush(c);
      break;
    case XCB_KEY_PRESS:  /* exit on key press */
      done = 1;
      break;
    }
    free(e);
  }
                        /* close connection to server */
  xcb_disconnect(c);

  return 0;
}

注釋

  1. ^ [ANNOUNCE] libxcb 1.14. xcb (郵寄清單). 2020-02-22 [2020-02-22]. (原始內容存檔於2021-02-26). 

外部連結