Xlib

維基百科,自由的百科全書
跳至導覽 跳至搜尋
Xlib
開發者X.Org基金會
首次發布大約1985年
目前版本
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    原始碼庫
    • {{URL|example.com|可选的显示文本}}
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    程式語言C
    引擎
      Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
      類型
      許可協定
        Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
        網站www.x.org, 文件: www.x.org/releases/current/doc/libX11/libX11/libX11.html

        Xlib是一種X Window System協定的使用者端,以C語言撰寫。其功能是與X server溝通。這樣的功能可以讓程式人員撰寫程式時,毋須了解其協定的細節。但甚少應用程式會直接使用Xlib;通常是透過其他的函式庫來呼叫Xlib用以提供部件工具箱(widget toolkits):

        File:Xlib and XCB in the X Window System graphics stack.svg
        Xlib及其應用

        Xlib發表於1985年,目前使用在許多的Unix-like作業系統上。

        目前XCB有可能取代Xlib.

        資料型別[編輯]

        Xlib主要的資料型別是Display[1]結構。

        範例[編輯]

        下面是一個XLib的範列,產生一個視窗。

        /*
          Simple Xlib application drawing a box in a window.
          gcc input.c -o output -lX11
        */
        
        #include <X11/Xlib.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <stdbool.h>
        
        int main(void)
        {
          Display *d;
          Window w;
          XEvent e;
          char *msg = "Hello, World!";
          int s;
        
          bool done = false;
        
          /* open connection with the server */
          d = XOpenDisplay(NULL);
          if (d == NULL) {
            fprintf(stderr, "Cannot open display\n");
            exit(1);
          }
        
          s = DefaultScreen(d);
        
          /* create window */
          w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 640, 480, 0,
        			  BlackPixel(d, s), WhitePixel(d, s));
        
          /* register interest in the delete window message */
          Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
          XSetWMProtocols(d, w, &wmDeleteMessage, 1);
           
          /* select kind of events we are interested in */
          XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);
        
          /* map (show) the window */
          XMapWindow(d, w);
        
          /* event loop */
          while (!done) {
            XNextEvent(d, &e);
            /* draw or redraw the window */
            if (e.type == Expose) {
              XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
              XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
            }
        
            /* exit on key press */
            switch(e.type){
              
            case KeyPress:
              XDestroyWindow(d, w);
              break;
        
            case DestroyNotify:
              done = true;
              break;
        
            case ClientMessage:
              if (e.xclient.data.l[0] == wmDeleteMessage){
        	done = true;
              }
              break;      
            }
          }
        
          /* close connection to server */
          XCloseDisplay(d);
        
          return 0;
        }
        

        注釋[編輯]

        1. ^ Display Structure on freedesktop CVS. Tip search for: typedef struct _XDisplay Display. [2009-07-09]. (原始內容存檔於2008-01-31). 

        外部連結[編輯]