用Xlib库进行基本图形编程
生活随笔
收集整理的這篇文章主要介紹了
用Xlib库进行基本图形编程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用Xlib庫進(jìn)行基本圖形編程
用Xlib庫進(jìn)行基本圖形編程目錄
1、前言
2、X Window系統(tǒng)的客戶服務(wù)器模式
3、GUI編程-同步化的編程模型
4、基本的Xlib概念
? ? ? ? 1、X Display
? ? ? ? 2、GC-圖形上下文
? ? ? ? 3、對象句柄
? ? ? ? 4、Xlib 結(jié)構(gòu)體的內(nèi)存分配
? ? ? ? 5、事件
5、編譯給予Xlib的程序
6、打開和關(guān)閉一個連接到X服務(wù)器的連接
7、檢查關(guān)于Display的基本信息
8、創(chuàng)建一個簡單的窗口-我們的“hello world”程序
9、在窗口中繪畫
? ? ? ? 1、分配圖形上下文(GC)
? ? ? ? 2、基本繪圖-點(diǎn),線,框,圓...
10、X事件
? ? ? ? 1、使用事件遮罩給事件型別注冊
? ? ? ? 2、接收事件-撰寫事件循環(huán)
? ? ? ? 3、暴露事件
? ? ? ? 4、獲得用戶輸入
? ? ? ? ? ? ? ? 1、鼠標(biāo)按鈕點(diǎn)擊和釋放事件
? ? ? ? ? ? ? ? 2、鼠標(biāo)移動事件
? ? ? ? ? ? ? ? 3、鼠標(biāo)指針進(jìn)入和離開事件
? ? ? ? ? ? ? ? 4、鍵盤焦點(diǎn)
? ? ? ? ? ? ? ? 5、鍵盤按下和釋放事件
? ? ? ? 5、X事件-完整的例子
11、處理文本和字體
? ? ? ? 1、字體結(jié)構(gòu)
? ? ? ? 2、載入字體
? ? ? ? 3、把字體賦給圖形上下文
? ? ? ? 4、在窗口中繪出文本
12、窗口階層
? ? ? ? 1、根,父和子窗口
? ? ? ? 2、事件傳播
13、和窗口管理器交互
? ? ? ? 1、窗口屬性
? ? ? ? 2、設(shè)置窗口名和圖標(biāo)名
? ? ? ? 3、設(shè)置最佳窗口尺寸
? ? ? ? 4、設(shè)置窗口管理器的雜項
? ? ? ? 5、設(shè)置應(yīng)用程序的圖標(biāo)
14、簡單窗口操作
? ? ? ? 1、映射和取消映射窗口
? ? ? ? 2、在屏幕上移動窗口
? ? ? ? 3、縮放窗口
? ? ? ? 4、改變窗口的堆疊次序-提高會放低
? ? ? ? 5、標(biāo)識會取消標(biāo)識窗口
? ? ? ? 6、獲得窗口的信息
15、使用顏色來繪出彩虹
? ? ? ? 1、色表
? ? ? ? 2、分配和釋放色表
? ? ? ? 3、分配和釋放單個顏色
? ? ? ? 4、用顏色繪畫
16、X Bitmap和Pixmap
? ? ? ? 1、什么是X Bitmap?什么又是X Pixmap?
? ? ? ? 2、從文件中載入Bitmap
? ? ? ? 3、在窗口中畫出Bitmap
? ? ? ? 4、創(chuàng)建Pixmap
? ? ? ? 5、在窗口中畫出Pixmap
? ? ? ? 6、釋放Pixmap
17、鼠標(biāo)光標(biāo)的雜事
? ? ? ? 1、創(chuàng)建和釋放鼠標(biāo)光標(biāo)
? ? ? ? 2、設(shè)置窗口的鼠標(biāo)光標(biāo)
Preface
前言
This tutorial is the first in a series of "would-be" tutorials about graphical programming?
in the X window environment. By itself, it is useless. A real X programmer usually uses a?
much higher level of abstraction, such as using Motif (or its free version, lesstiff), GTK,?
QT and similar libraries. However, we need to start somewhere. More than this, knowing how?
things work down below is never a bad idea.
該教程是“可能會有”的關(guān)于在X Window環(huán)境下進(jìn)行圖形化編程的教程的第一個系列。其自身是用處不大
的。一個真正的X程序員一般使用一個更高層次的抽象,比如用Motif(或者它的的免費(fèi)版本,lesstiff),
GTK,QT和類似的庫。然而,我們需要從一個地方開始入手。不僅如此,知道表象之下的事情是如何工作
的決不會是壞的主意。
After reading this tutorial, one would be able to write very simple graphical programs, but?
not programs with a descent user interface. For such programs, one of the previously?
mentioned libraries would be used.?
在讀完本教程后,你可能能夠些簡單的圖形程序,但是那不會是一個有良好用戶界面的程序。對于這樣的
程序,也許就要用到上述的庫中的某一個。
The Client And Server Model Of The X Window System
X Window系統(tǒng)的客戶和服務(wù)器模型
The X window system was developed with one major goal - flexibility. The idea was that the?
way things look is one thing, but the way things work is another matter. Thus, the lower?
levels provide the tools required to draw windows, handle user input, allow drawing graphics?
using colors (or black and white screens), etc. To this point, a decision was made to?
separate the system into two parts. A client that decides what to do, and a server that?
actually draws on the screen and reads user input in order to send it to the client for?
processing.
X window系統(tǒng)開發(fā)之初有一個最大的目標(biāo)-靈活性。想法是這樣的東西看上如如何一回事,東西如何工作
的又是另外一回事。因而,底層提供在畫窗口,處理用戶輸入,允許使用顏色畫圖形(或者黑白屏幕)等動
作中需要的工作。就這點(diǎn)決定了把系統(tǒng)分為兩個部分。客戶決定作什么,而服務(wù)器實(shí)際在屏幕上畫圖并讀
出用戶輸入以發(fā)給客戶進(jìn)行處理。
This model is the complete opposite of what one is used to when dealing with clients and?
servers. In our case, the user seats near the machine controlled by the server, while the?
client might be running on a remote machine. The server controls the screen, mouse and?
keyboard. A client may connect to the server, request that it draws a window (or several?
windows), and ask the server to send it any input the user sends to these windows. Thus,?
several clients may connect to a single X server - one might be running an email software,?
one running a WWW browser, etc. When input it sent by the user to some window, the server?
sends a message to the client controlling this window for processing. The client decides?
what to do with this input, and sends the server requests for drawing in the window.
這個模型正好和人們在客戶和服務(wù)器中所習(xí)慣的行為相反。在我們的例子,用戶坐在由服務(wù)器控制的機(jī)器
旁邊,而客戶可能運(yùn)行于一個遠(yuǎn)程的機(jī)器上。服務(wù)器控制屏幕,鼠標(biāo)和鍵盤??蛻艨赡苓B接到了服務(wù)器,
發(fā)出畫一個(或者多個)窗口的請求,并要求服務(wù)器把任何用戶發(fā)送給這些窗口的輸入給他。因而,幾個客
戶可能連接到了同一個X服務(wù)器-一個可能在運(yùn)行email軟件,一個可能在運(yùn)行WWW瀏覽器,等等。當(dāng)由用
戶發(fā)送輸入給某些窗口時,服務(wù)器向控制這些窗口的客戶發(fā)送消息以供處理。客戶決定對輸入作什么,并
給服務(wù)器發(fā)送請求來在窗口中繪圖。
The whole session is carried out using the X message protocol. This protocol was originally?
carried over the TCP/IP protocol suite, allowing the client to run on any machine connected?
to the same network that the server is. Later on the X servers were extended to allow?
clients running on the local machine more optimized access to the server (note that an X?
protocol message may be several hundreds of KB in size), such as using shared memory, or?
using Unix domain sockets (a method for creating a logical channel on a Unix system between?
two processes).?
整個會話過程是用X消息協(xié)議執(zhí)行的。該協(xié)議最初時由TCP/IP協(xié)議包執(zhí)行的,允許客戶運(yùn)行于任何和服務(wù)
器連接在相同網(wǎng)絡(luò)上的機(jī)器上。后來,X服務(wù)器被擴(kuò)展為允許客戶運(yùn)行在本地機(jī)器上更優(yōu)的訪問服務(wù)器(注
意到X協(xié)議消息可能有幾百KB那么大),比如使用共享內(nèi)存,或者使用Unix域sockets(一個在Unix系統(tǒng)上的
兩個進(jìn)程間創(chuàng)建邏輯通道的方法)。
GUI programming - the Asynchronous Programming Model
GUI編程-同步化的編程模型
Unlike conventional computer programs, that carry some serial nature, a GUI program usually?
uses an asynchronous programming model, also known as "event-driven programming". This means?
that that program mostly sits idle, waiting for events sent by the X server, and then acts?
upon these events. An event may say "The user pressed the 1st button mouse in spot x,y", or?
"the window you control needs to be redrawn". In order for the program to be responsive to?
the user input, as well as to refresh requests, it needs to handle each event in a rather?
short period of time (e.g. less than 200 milliseconds, as a rule of thumb).
不同于包含某種順序化執(zhí)行內(nèi)質(zhì)的傳統(tǒng)的計算機(jī)程序。GUI程序通常使用同步化的編程模型,也被稱為“
事件驅(qū)動編程”。這個意味著程序大部分時候時閑著的,等待由X服務(wù)器發(fā)送的事件,然后根據(jù)這些事件
作出反應(yīng)。事件可能時”用戶在點(diǎn)x,y處按下第一個按鈕“,或者時”你控制的串口需要重畫“。為了程
序能夠響應(yīng)用戶輸入以及刷新請求,它需要在一個相當(dāng)短的時間內(nèi)處理每個事件(比如作為一個大體的規(guī)
則,小于200毫秒)。
This also implies that the program may not perform operations that might take a long time?
while handling an event (such as opening a network connection to some remote server, or?
connecting to a database server, or even performing a long file copy operation). Instead, it?
needs to perform all these operations in an asynchronous manner. This may be done by using?
various asynchronous models to perform the longish operations, or by performing them in a?
different process or thread.
這也意味著程序可能不執(zhí)行在處理過程也許需要很長時間的事件的操作(例如打開一個連接到遠(yuǎn)程服務(wù)器
的網(wǎng)絡(luò)連接,或者連接到數(shù)據(jù)庫服務(wù)器,甚至執(zhí)行一個大文件的拷貝)。而是,它需要同步化的執(zhí)行所有
這些操作。這可能通過使用各種同步模型來執(zhí)行長時間的操作,或者通過用不同的進(jìn)程或者線程來執(zhí)行他
們。
So the way a GUI program looks is something like that:
因而GUI程序看上去大概是這樣:
? ?1. Perform initialization routines.
? ?2. Connect to the X server.
? ?3. Perform X-related initialization.
? ?4. While not finished:
? ?? ?? ?1. Receive the next event from the X server.
? ?? ?? ?2. handle the event, possibly sending various drawing requests to the X server.
? ?? ?? ?3. If the event was a quit message, exit the loop.?
? ?5. Close down the connection to the X server.
? ?6. Perform cleanup operations.?
? ?1、執(zhí)行初始化
? ?2、連接到X服務(wù)器
? ?3、執(zhí)行X相關(guān)的初始化工作
? ?4、在沒有結(jié)束之前:
? ?? ???1、接收來自于X服務(wù)器的下一個事件
? ?? ???2、處理事件,也許向X服務(wù)器發(fā)送多種繪畫請求
? ?? ???3、如果事件是退出消息,跳出循環(huán)
? ?5、關(guān)閉連接到X服務(wù)器的連接
? ?6、執(zhí)行清理操作
Basic Xlib Notions
基本的Xlib概念
In order to eliminate the needs of programs to actually implement the X protocol layer, a?
library called 'Xlib' was created. This library gives a program a very low-level access to?
any X server. Since the protocol is standardized, A client using any implementation of Xlib?
may talk with any X server. This might look trivial these days, but back at the days of?
using character mode terminals and proprietary methods of drawing graphics on screens, this?
looked like a major break-through. In fact, you'll notice the big hype going around?
thin-clients, windows terminal servers, etc. They are implementing today what the X protocol?
enabled in the late 80's. On the other hand, the X universe is playing a catch-up game?
regarding CUA (common user access, a notion made by IBM to refer to the usage of a common?
look and feel for all programs in order to ease the lives of the users). Not having a common?
look and feel was a philosophy of the creators of the X window system. Obviously, it had?
some drawbacks that are evident today.?
為了消除程序事件實(shí)現(xiàn)X協(xié)議層的需求,一個稱為‘Xlib’的庫被創(chuàng)造出來。該庫給程序提供了一個對任
何X服務(wù)器非常底層的訪問。因為協(xié)議是標(biāo)準(zhǔn)化的,客戶使用Xlib的任何一種實(shí)現(xiàn)都可以和和X服務(wù)器通話
。這些可能在今天看來沒什么大用,但回到使用字符模式終端和專有方法在屏幕上繪圖的日子,這是一個
很大的突破。事實(shí)上,你將注意到咱愛瘦客戶,窗口終端服務(wù)器,等等周圍進(jìn)行的各種虛偽的騙局。他們
在今天實(shí)現(xiàn)X協(xié)議在80年代晚期已經(jīng)能夠作的事情。另外一方面,X universe在玩一個關(guān)于CUA(共通用戶
感觀,一個由IBM制造的概念,指的是對所有程序使用共通的觀感以使得用戶能夠更加輕松)的catch-up游
戲。沒有共通的感觀是X window系統(tǒng)創(chuàng)造者的哲學(xué)。明顯,它有許多在今天看來顯然的缺陷。
The X Display
The major notion of using Xlib is the X display. This is a structure representing the?
connection we have open with a given X server. It hides a queue of messages coming from the?
server, and a queue of pending requests that our client intends to send to the server. In?
Xlib, this structure is named 'Display'. When we open a connection to an X server, the?
library returns a pointer to such a structure. Later, we supply this pointer to any Xlib?
function that should send messages to the X server or receive messages from this server.?
X Display
使用Xlib的最大的概念是X display。這是一個代表我們和一個給定X服務(wù)器打開的連接的結(jié)構(gòu)體。它隱藏
了服務(wù)器的消息隊列,客戶將要發(fā)送給服務(wù)器的請求隊列。在Xlib中,這個結(jié)構(gòu)體被命名為‘Display’
。當(dāng)我們打開一個連接到X服務(wù)器的連接的時候,庫返回一個指向這種結(jié)構(gòu)體的指針。然后,我們把這個
指針提供給任何需要發(fā)送消息給X服務(wù)器或者從這個服務(wù)器接收消息的Xlib函數(shù)。
The GC - Graphics Context
When we perform various drawing operations (graphics, text, etc), we may specify various?
options for controlling how the data will be drawn - what foreground and background colors?
to use, how line edges will be connected, what font to use when drawing some text, etc). In?
order to avoid the need to supply zillions of parameters to each drawing function, a?
graphical context structure, of type 'GC' is used. We set the various drawing options in?
this structure, and then pass a pointer to this structure to any drawing routines. This is?
rather handy, as we often needs to perform several drawing requests with the same options.?
Thus, we would initialize a graphical context, set the desired options, and pass this GC?
structure to all drawing functions.?
GC -圖形上下文
當(dāng)我們執(zhí)行各種繪出(圖形,文本,等)操作的時候,我們可能要指定幾個選項以控制數(shù)據(jù)怎么被繪出 -?
前景色和背景色是什么,線的邊緣如何連接,在繪出文本的時候使用何種字體,等。為了避免給每個繪出
函數(shù)提供n多參數(shù),一個類型為‘GC’的圖形上下文結(jié)構(gòu)被啟用。我們在這個結(jié)構(gòu)中設(shè)置各種繪出選項,
并且把指向這個結(jié)構(gòu)的指針傳遞給每個繪出函數(shù)。這個是相當(dāng)方便的,因為我們通常需要用相同選項執(zhí)行
好幾個繪出請求。因而,我們初始化圖形上下文,設(shè)置所需的選項,并把這個GC結(jié)構(gòu)傳遞給所有的繪出函
數(shù)。
Object Handles
When various objects are created for us by the X server - such as windows, drawing areas and?
cursors - the relevant function returns a handle. This is some identifier for the object?
that actually resides in the X server's memory - not in our application's memory. We can?
later manipulate this object by supplying this handle to various Xlib functions. The server?
keeps a mapping between these handles and the actual objects it manages. Xlib provides?
various type definitions for these objects (Window, Cursor, Colormap and so on), which are?
all eventually mapped to simple integers. We should still use these type names when defining?
variables that hold handles - for portability reasons.?
對象句柄
當(dāng)各種對象被創(chuàng)造出來給X服務(wù)器使用 - 例如窗口,繪畫區(qū)域和光標(biāo) - 相關(guān)的函數(shù)返回一個句柄。這
是實(shí)際存在于X服務(wù)器的內(nèi)存中的對象的標(biāo)識符。我們能夠在后面通過把這些句柄提供給各種Xlib函數(shù)來
操縱這些對象。服務(wù)器保存了這些句柄和它們管理的對象之間的映射。Xlib提供各種型別定義給這些對象
(窗口,光標(biāo),色表等等),它們實(shí)際上最終映射為簡單的整數(shù)。我們在定義保存這些句柄的變量的時候仍
然應(yīng)當(dāng)使用這些型別名-為了有更好的可移植性。
Memory Allocation For Xlib Structures
為Xlib結(jié)構(gòu)體分配內(nèi)存
Various structure types are used in Xlib's interface. Some of them are allocated directly by?
the user. Others are allocated using specific Xlib functions. This allows the library to?
initialize properly these structures. This is very handy, since these structures tend to?
contain a lot of variables, making it rather tedious for the poor programmer to initialize.?
Remember - Xlib tries to be as flexible as possible, and this means it is also as complex as?
it can get. Having default values will enable a beginner X programmer to use the library,?
without interfering with the ability of a more experienced programmer to tweak with these?
zillions of options.
在Xlib的接口中使用了各種結(jié)構(gòu)型別。他們中的一些直接由用戶分配內(nèi)存。其他的使用Xlib函數(shù)分配。這
使得庫能夠恰當(dāng)?shù)某跏蓟@些結(jié)構(gòu)。這非常方便,因為這些結(jié)構(gòu)傾向于包含很多變量,使得對于差勁點(diǎn)的
程序員非常難于初始化它們。記住-Xlib嘗試著盡可能的了靈活,而且這意味著他也是盡可能的復(fù)雜。由
缺省值使得初學(xué)X的程序員能夠使用這個庫,而不打擾有經(jīng)驗的程序員在n多選項中作調(diào)整的可能。
As for freeing memory, this is done in one of two ways. In cases where we allocated the?
memory - we free it in the same manner (i.e. use free() to free memory allocated using?
malloc()). In case we used some Xlib function to allocate it, or we used some Xlib query?
method that returns dynamically allocated memory - we will use the XFree() function to free?
this memory block.
對于釋放內(nèi)存,由兩種方法完成。在我們分配內(nèi)存的情況-我們用相同方法釋放它們(也就是使用free()
來釋放由malloc()分配的內(nèi)存)。在我們用某Xlib函數(shù)分配的時候,或者我們使用返回動態(tài)分配的內(nèi)存的
Xlib查詢方法的時候-我們使用XFree()函數(shù)來釋放這些內(nèi)存塊。
Events
事件
A structure of type 'XEvent' is used to pass events received from the X server. Xlib?
supports a large amount of event types. The XEvent structure contains the type of event?
received, as well as the data associated with the event (e.g. position on the screen where?
the event was generated, mouse button associated with the event, region of screen associated?
with a 'redraw' event, etc). The way to read the event's data depends on the event type.?
Thus, an XEvent structure contains a C language union of all possible event types (if you're?
not sure what C unions are, it is time to check your proffered C language manual...). Thus,?
we could have an XExpose event, an XButton event, an XMotion event, etc.?
型別‘XEvent’的結(jié)構(gòu)被用來傳遞從X服務(wù)器接收來的事件。Xlib支持很大數(shù)量的事件型別。XEvent結(jié)構(gòu)
包含接收事件的類型,以及與該事件相關(guān)的數(shù)據(jù)(例如事件產(chǎn)生的屏幕位置,與事件相關(guān)的鼠標(biāo)按鈕,和
‘redraw’事件相關(guān)的屏幕區(qū)域,等)。讀取事件的數(shù)據(jù)的方法和事件類型有關(guān)。因而,XEvent結(jié)構(gòu)包含
一個C語言對于所有可能事件型別的聯(lián)合(如果你不確知C的聯(lián)合是什么,該是查查你的C語言手冊的時候
...)。因而,我們能夠有一個XExpose事件,XButton事件,XMotion事件,等。
Compiling Xlib-Based Programs
編譯基于Xlib的程序
Compiling Xlib-Based programs requires linking them with the Xlib library. This is done?
using a compilation command like this:
編譯基于Xlib的需要把他們和Xlib庫進(jìn)行鏈接。這是通過使用如下的編譯命令行來完成的:
cc prog.c -o prog -lX11
If the compiler complains that it cannot find the X11 library, try adding a '-L' flag, like?
this:
如果編譯器抱怨它找不到X11庫,嘗試加上‘-L’標(biāo)志,像這樣:
cc prog.c -o prog -L/usr/X11/lib -lX11
or perhaps this (for a system with release 6 of X11):
或者也許是這樣(對于用X11的release 6的系統(tǒng)):
cc prog.c -o prog -L/usr/X11R6/lib -lX11
On SunOs 4 systems, the X libraries are placed in /usr/openwin/lib:
在SunOs 4系統(tǒng)上,X庫被放置于/usr/openwin/lib:
cc prog.c -o prog -L/usr/openwin/lib -lX11
and so on...
等等...
Opening And Closing The Connection To An X Server
打開和關(guān)閉連接到X服務(wù)器的連接
An X program first needs to open the connection to the X server. When we do that, we need to?
specify the address of the host running the X server, as well as the display number. The X?
window system can support several displays all connected to the same machine. However,?
usually there is only one such display, which is display number '0'. If we wanted to connect?
to the local display (i.e. the display of the machine on which our client program runs), we?
could specify the display as ":0". To connect to the first display of a machine whose?
address is "simey", we could use the address "simey:0". Here is how the connection is?
opened:
X程序首先需要打開連接到X服務(wù)器的連接。在我們完成這件工作的時候,我們需要指定運(yùn)行X服務(wù)器的機(jī)
器的地址,以及display號碼。X window系統(tǒng)能夠支持全部連接于同一個機(jī)器的好幾個display。然而,通
常只有一個這樣的display,它的display號是‘0’。如果我們想要連接到本地display(也就是我們客戶
程序所運(yùn)行的機(jī)器的display),我們可以指定display為’:0‘。要連接到地址為”simey“的機(jī)器的第一
個display,我們能夠使用地址”simey:0“。這兒是連接是如何被打開的:
#include <X11/Xlib.h>? ?/* defines common Xlib functions and structs. */
.
.
/* this variable will contain the pointer to the Display structure */
/* returned when opening a connection.? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
Display* display;
/* open the connection to the display "simey:0". */
display = XOpenDisplay("simey:0");
if (display == NULL) {
? ? fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
? ? exit (-1);
}
Note that is common for X programs to check if the environment variable 'DISPLAY' is?
defined, and if it is, use its contents as the parameter to the XOpenDisplay() function.
注意,對于X程序來說檢查系統(tǒng)變量‘DISPLAY’是否被定義了是很常見的,而且如果是的話,使用它的內(nèi)
容作為XOpenDisplay()函數(shù)的參數(shù)。
When the program finished its business and needs to close the connection the X server, it?
does something like this:
當(dāng)程序完成了它的使命并且需要關(guān)閉連接到X服務(wù)器的連接的時候,它如下動作:
XCloseDisplay(display);
This would cause all windows created by the program (if any are left) to be automatically?
closed by the server, and any resources stored on the server on behalf of the clients - to?
be freed. Note that this does not cause our client program to terminate - we could use the?
normal exit() function to do that.
這將導(dǎo)致所有由程序創(chuàng)造的窗口(如果還有剩下的話)自動被服務(wù)器關(guān)閉,而且為了客戶的利益任何留在服
務(wù)器上的資源-將被釋放。注意這將不會導(dǎo)致我們的客戶程序終止-我們使用普通的exit()函數(shù)來完成。
Checking Basic Information About A Display
檢查關(guān)于Display的基本信息
Once we opened a connection to an X server, we should check some basic information about it:?
what screens it has, what is the size (width and height) of the screen, how many colors it?
supports (black and white? grey scale? 256 colors? more?), and so on. We will show a code?
snippet that makes few of these checks, with comments explaining each function as it is?
being used. We assume that 'display' is a pointer to a 'Display' structure, as returned by a?
previous call to XOpenDisplay().
一旦我們打開了一個連接到X服務(wù)器的連接,我們應(yīng)當(dāng)檢查有關(guān)它的一些基本信息:他有什么樣的屏幕,
尺寸是多少(寬和高),它支持多少顏色(黑白?灰度?256色?更多?),以及等等。我們將展示一些作一
些這樣檢查的代碼片段,以及在使用中解釋每個函數(shù)的注釋。我們假定‘display’是一個指向‘Display
’的結(jié)構(gòu)的指針,由前面對XOpenDisplay()的調(diào)用返回的。
/* this variable will be used to store the "default" screen of the??*/
/* X server. usually an X server has only one screen, so we're only */
/* interested in that screen.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?*/
int screen_num;
/* these variables will store the size of the screen, in pixels.? ? */
int screen_width;
int screen_height;
/* this variable will be used to store the ID of the root window of our */
/* screen. Each screen always has a root window that covers the whole? ?*/
/* screen, and always exists.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? */
Window root_window;
/* these variables will be used to store the IDs of the black and white */
/* colors of the given screen. More on this will be explained later.? ? */
unsigned long white_pixel;
unsigned long black_pixel;
/* check the number of the default screen for our X server. */
screen_num = DefaultScreen(display);
/* find the width of the default screen of our X server, in pixels. */
screen_width = DisplayWidth(display, screen_num);
/* find the height of the default screen of our X server, in pixels. */
screen_height = DisplayHeight(display, screen_num);
/* find the ID of the root window of the screen. */
root_window = RootWindow(display, screen_num);
/* find the value of a white pixel on this screen. */
white_pixel = WhitePixel(display, screen_num);
/* find the value of a black pixel on this screen. */
black_pixel = BlackPixel(display, screen_num);
There are various other macros to get more information about the screen, that you can find?
in any Xlib reference. There are also function equivalents for some of these macros (e.g.?
XWhitePixel, which does the same as WhitePixel).
有各種其他的宏來得到關(guān)于屏幕的更多信息,你可以從任何Xlib參考書中得到它們。還有和這些宏完成相
同功能的函數(shù)(例如XWhitePixel,它和WhitePixel干一樣的事情)。
Creating A Basic Window - Our "hello world" Program
創(chuàng)建一個簡單窗口 - 我們的“hello world”程序
After we got some basic information about our screen, we can get to creating our first?
window. Xlib supplies several functions for creating new windows, one of which is?
XCreateSimpleWindow(). This function gets quite a few parameters determining the window's?
size, its position, and so on. Here is a complete list of these parameters:
在我們獲得了一些有關(guān)我們的屏幕的基本信息之后,我們可以開始創(chuàng)建我們第一個窗口。Xlib提供數(shù)個函
數(shù)來創(chuàng)建新窗口,其中的一個是XCreateSimpleWindow()。這個函數(shù)或者少量幾個決定窗口的大小和位置
等的參數(shù)。這有一個這些參數(shù)的完整列表:
Display* display
? ? Pointer to the Display structure.
Window parent
? ? The ID of an existing window that should be the parent of the new window.
int x
? ? X Position of the top-left corner of the window (given as number of pixels from the left?
of the screen).
int y
? ? Y Position of the top-left corner of the window (given as number of pixels from the top?
of the screen).
unsigned int width
? ? Width of the new window, in pixels.
unsigned int height
? ? Height of the new window, in pixels.
unsigned int border_width
? ? Width of the window's border, in pixels.
unsigned long border
? ? Color to be used to paint the window's border.
unsigned long background
? ? Color to be used to paint the window's background.
Lets create a simple window, whose width is 1/3 of the screen's width, height is 1/3 of the?
screen's height, background color is white, border color is black, and border width is 2?
pixels. The window will be placed at the top-left corner of the screen.
讓我們創(chuàng)建一個簡單窗口,它的寬是屏幕寬的1/3,高是屏幕高的1/3,背景色是白的,邊框顏色是黑色的
,而且邊框?qū)?象素。窗口將會放置在屏幕的左上角。
/* this variable will store the ID of the newly created window. */
Window win;
/* these variables will store the window's width and height. */
int win_width;
int win_height;
/* these variables will store the window's location. */
int win_x;
int win_y;
/* calculate the window's width and height. */
win_width = DisplayWidth(display, screen_num) / 3;
win_height = DisplayHeight(display, screen_num) / 3;
/* position of the window is top-left corner - 0,0. */
win_x = win_y = 0;
/* create the window, as specified earlier. */
win = XCreateSimpleWindow(display,
? ?? ?? ?? ?? ?? ?? ?? ???RootWindow(display, screen_num),
? ?? ?? ?? ?? ?? ?? ?? ???win_x, win_y,
? ?? ?? ?? ?? ?? ?? ?? ???win_width, win_height,
? ?? ?? ?? ?? ?? ?? ?? ???win_border_width, BlackPixel(display, screen_num),
? ?? ?? ?? ?? ?? ?? ?? ???WhitePixel(display, screen_num));
The fact that we created the window does not mean it will be drawn on screen. By default,?
newly created windows are not mapped on the screen - they are invisible. In order to make?
our window visible, we use the XMapWindow() function, as follows:
我們創(chuàng)造了窗口的事實(shí)并不意味著它會被畫在屏幕上。缺省的,新創(chuàng)建的窗口不會被映射于屏幕之上 -?
它們是不可見的。為了使得我們的窗口可見,我們使用XMapWindow()函數(shù),如下:
XMapWindow(win);
To see all the code we have gathered so far, take a look at the simple-window.c program.?
You'll see two more function not explained so far - XFlush() and XSync(). The XFlush()?
function flushes all pending requests to the X server - much like the fflush() function is?
used to flash standard output. The XSync() function also flushes all pending requests to the?
X server, and then waits until the X server finishes processing these requests. In a normal?
program this will not be necessary (you'll see why when we get to write a normal X program),?
but for now we put it there. Try compiling the program either with or without these function?
calls to see the difference in its behavior.
要看我們至今積累寫出的所有代碼,看看simple-window.c程序。你將看到至今沒有解釋的兩個另外的函
數(shù) - XFlush()和XSync()函數(shù)用來清除仍未發(fā)送給X服務(wù)器的請求 - 很想用來清除標(biāo)準(zhǔn)輸出的fflush()
函數(shù)。XSync()函數(shù)也清除所有仍未發(fā)送給X服務(wù)器的消息,而且等待X服務(wù)器結(jié)束處理所有這些請求。在
一個通常的程序中,這將不會是必要的(你可以看到為什么在我們開始寫一個普通的X程序的時候),但對
于現(xiàn)在我們把它放在那兒。嘗試著有和去掉這些函數(shù)調(diào)用來編譯程序,以觀察它們行為上的不同點(diǎn)。
Drawing In A Window
在窗口中繪圖
Drawing in a window can be done using various graphical functions - drawing pixels, lines,?
circles, rectangles, etc. In order to draw in a window, we first need to define various?
general drawing parameters - what line width to use, which color to draw with, etc. This is?
done using a graphical context (GC).?
在窗口中繪圖能夠通過使用各種圖形函數(shù)來完成 - 畫點(diǎn),線,圓,矩形,等。為了能夠在窗口中繪圖,
我們首先需要定義幾種通用的繪圖參數(shù) - 線寬使用多少的,繪圖的顏色是什么,等。這個是用圖形上下
文(GC)來完成的。
Allocating A Graphics Context (GC)
分配圖形上下文(GC)
As we said, a graphical context defines several attributes to be used with the various?
drawing functions. For this, we define a graphical context. We can use more than one?
graphical context with a single window, in order to draw in multiple styles (different?
colors, different line widths, etc.). Allocating a new GC is done using the XCreateGC()?
function, as follows (in this code fragment, we assume "display" is a pointer to a Display?
structure, and "win" is the ID of a previously created window):
如我所說,圖形上下文給出幾個用于繪圖函數(shù)的屬性。因此,我們定義一個圖形上下文。我們能夠在一個
窗口中使用多余一個的圖形上下文,以達(dá)到用多種風(fēng)格(不同的顏色,線寬,等)繪圖。分配一個新的GC是
通過使用XCreateGC()函數(shù)來完成的,如下(在這個代碼片段中,我們假定“display”是一個只想Display
結(jié)構(gòu)的指針,而起“win”是先前創(chuàng)建的窗口的ID):
/* this variable will contain the handle to the returned graphics context. */
GC gc;
/* these variables are used to specify various attributes for the GC. */
/* initial values for the GC. */
XGCValues values = CapButt | JoinBevel;
/* which values in 'values' to check when creating the GC. */
unsigned long valuemask = GCCapStyle | GCJoinStyle;
/* create a new graphical context. */
gc = XCreateGC(display, win, valuemask, &values);
if (gc < 0) {
? ? fprintf(stderr, "XCreateGC: \n");
}
Note should be taken regarding the roles of "valuemask" and "values". Since a graphics?
context has zillions of attributes, and since often we don't want to define few of them, we?
need to be able to tell the XCreateGC() which attributes we want to set. This is what the?
"valuemask" variable is for. We then use the "values" variable to specify actual values for?
the attributes we defined in the "valuesmask". Thus, for each constant used in "values",?
we'll use the matching constant in "valuesmask". In this case, we defined a graphics context?
with two attributes:
注意“valuesmask”和“values”的角色。因為圖形上下文有n多屬性,并且我們不想定義它們中的一些
,我們需要能夠告訴XCreateGC()哪些屬性是我們想要設(shè)置的。這就是“valuesmask”變量的用處。我們
然后使用“values”變量來指定我們在“valuesmask”中定義的屬性的值。因而,對于每個在“values”
中使用的常量,我們將使用在“valuesmask”中相應(yīng)的常量。在此例中,我們用兩個屬性定義圖形上下文
:
? ?1. When drawing a multiple-part line, the lines should be joined in a 'Bevelian' style.
? ?2. A line's end-point will be drawn straight (as opposed to ending the line in a round?
shape, if its width is more than 1 pixel wide).?
? ?1、當(dāng)在畫多部分的線的時候,線應(yīng)該以‘Bevelian’風(fēng)格連接起來。
? ?2、線的終點(diǎn)將被直的畫出來(與以圓角結(jié)束線相對,如果它的寬度大于一個象素)。
The rest of the attributes of this GC will be set to their default values.
這個GC的剩余屬性將由它們的缺省值設(shè)定。
Once we created a graphics context, we can use it in drawing functions. We can also modify?
its parameters using various functions. Here are a few examples:
一旦我們創(chuàng)建了一個圖形上下文,我們能夠在繪圖函數(shù)中使用它。我們還能夠各種函數(shù)修改它的參數(shù)。這
兒有幾個例子:
/* change the foreground color of this GC to white. */
XSetForeground(display, gc, WhitePixel(display, screen_num));
/* change the background color of this GC to black. */
XSetBackground(display, gc, BlackPixel(display, screen_num));
/* change the fill style of this GC to 'solid'. */
XSetFillStyle(display, gc, FillSolid);
/* change the line drawing attributes of this GC to the given values. */
/* the parameters are: Display structure, GC, line width (in pixels), */
/* line drawing??style, cap (line's end) drawing style, and lines? ???*/
/* join style.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
XSetLineAttributes(display, gc, 2, LineSolid, CapRound, JoinRound);
for complete information on the various attributes available in a graphics context, refer to?
the manual page of XCreateGC(). We will use just a few simple attributes in our tutorial, to?
avoid over-complicating it.
要獲關(guān)于在圖形上下文中有的各種屬性的完整信息,參考XCreateGC()的手冊頁。我們將在我們的教程中
僅僅使用幾個簡單的屬性,以避搞得過度復(fù)雜了。
Drawing Primitives - Point, Line, Box, Circle...
基本繪圖-點(diǎn),線,框,圓...
After we have created a GC, we can draw on a window using this GC, with a set of Xlib?
functions, collectively called "drawing primitives". Without much fuss, lets see how they?
are used. We assume that "gc" is a previously initialized GC, and that 'win' contains the?
handle of a previously created window.
在我們創(chuàng)建了GC之后,我們能夠使用這個GC在窗口上用一套Xlib函數(shù)繪畫了,這些函數(shù)合成為“基本繪圖
函數(shù)”。廢話不多說了,讓我們看看它們是如何使用的吧。我們假定”gc“是先前初始化了的GC,而且‘
win’包含了先前創(chuàng)建的窗口的句柄。
/* draw a pixel at position '5,60' (line 5, column 60) of the given window. */
XDrawPoint(display, win, gc, 5, 5);
/* draw a line between point '20,20' and point '40,100' of the window. */
XDrawLine(display, win, gc, 20, 20, 40, 100);
/* draw an arc whose center is at position 'x,y', its width (if it was a? ???*/
/* full ellipse) is 'w', and height is 'h'. Start the arc at angle 'angle1'??*/
/* (angle 0 is the hour '3' on a clock, and positive numbers go? ?? ?? ?? ???*/
/* counter-clockwise. the angles are in units of 1/64 of a degree (so 360*64 */
/* is 360 degrees).? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? */
int x = 30, y = 40;
int h = 15, w = 45;
int angle1 = 0, angle2 = 2.109;
XDrawArc(display, win, gc, x-(w/2), y-(h/2), w, h, angle1, angle2);
/* now use the XDrawArc() function to draw a circle whose diameter */
/* is 15 pixels, and whose center is at location '50,100'.? ?? ?? ?*/
XDrawArc(display, win, gc, 50-(15/2), 100-(15/2), 15, 15, 0, 360*64);
/* the XDrawLines() function draws a set of consecutive lines, whose? ???*/
/* edges are given in an array of XPoint structures.? ?? ?? ?? ?? ?? ?? ?*/
/* The following block will draw a triangle. We use a block here, since??*/
/* the C language allows defining new variables only in the beginning of */
/* a block.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
??{
? ? /* this array contains the pixels to be used as the line's end-points. */
? ? XPoint points[] = {
? ?? ?{0, 0},
? ?? ?{15, 15},
? ?? ?{0, 15},
? ?? ?{0, 0}
? ? };
? ? /* and this is the number of pixels in the array. The number of drawn */
? ? /* lines will be 'npoints - 1'.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?*/
? ? int npoints = sizeof(points)/sizeof(XPoint);
? ? /* draw a small triangle at the top-left corner of the window. */
? ? /* the triangle is made of a set of consecutive lines, whose? ?*/
? ? /* end-point pixels are specified in the 'points' array.? ?? ? */
? ? XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
??}
/* draw a rectangle whose top-left corner is at '120,150', its width is */
/* 50 pixels, and height is 60 pixels.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? */
XDrawRectangle(display, win, gc, 120, 150, 50, 60);
/* draw a filled rectangle of the same size as above, to the left of the??*/
/* previous rectangle. note that this rectangle is one pixel smaller than */
/* the previous line, since 'XFillRectangle()' assumes it is filling up? ?*/
/* an already drawn rectangle. This may be used to draw a rectangle using */
/* one color, and later to fill it using another color.? ?? ?? ?? ?? ?? ? */
XFillRectangle(display, win, gc, 60, 150, 50, 60);
Hopefully, you got the point by now. We will mention a few more functions that may be used?
in a similar fashion. For example, XFillArc() takes the same parameters as XDrawArc(), but?
draws only the inside of this arc (like XFillRectangle() does to a rectangle drawn using the?
XDrawRectangle() function). There is also an XFillPolygon() function that fills the inside?
of a polygon. It takes almost the same parameters as XDrawLines(). However, if the last?
point in the array has a different location than the first point in the array, the?
XFillPolygon() function automatically adds another "virtual" lines, connecting these two?
points. Another difference between the two functions, is that XFillPolygon() takes an?
additional parameters, shape, that is used to help the X server optimize its operation. You?
can read about it in your manual pages. There are also plural versions for these functions,?
namely XFillArcs() and XFillRectangles().
但愿你跟上了我的進(jìn)度。我們還將提到更多的一些使用上差不多的函數(shù)。例如,XFillArc()和XDrawArc()
帶有相同的參數(shù),但是只畫出弧的內(nèi)部(像XFillRectangle()函數(shù)所作的和用XDrawRectangle()函數(shù)畫出
的矩形一樣)。還有一個填充多邊形內(nèi)部的XFillPolygon()函數(shù)。它和XDrawLines()基本上有相同的參數(shù)
。然而,如果數(shù)組的最后一個點(diǎn)和第一個點(diǎn)處于不同的位置,XFillPolygon()函數(shù)自動添加一條”
virtual“線,連接這兩個點(diǎn)。兩個函數(shù)的另外一個不同點(diǎn)就是XFillPolygon()帶另外一個參數(shù),shape。
它用來幫助X服務(wù)器優(yōu)化它的行為。你能夠在手冊頁上學(xué)到這些。對于這些函數(shù)還有復(fù)數(shù)版本,名字為
XFillArcs()和XFillRectangles()。
The source code for a program doing these drawings is found in the file simple-drawing.c.
完成這些繪畫的程序的源代碼位于文件simple-drawing.c中。
X Events
X 事件
In an Xlib program, everything is driven by events. Event painting on the screen is?
sometimes done as a response to an event - an "expose" event. If part of a program's window?
that was hidden, gets exposed (e.g. the window was raised above other windows), the X server?
will send an "expose" event to let the program know it should repaint that part of the?
window. User input (key presses, mouse movement, etc.) is also received as a set of events.?
在Xlib程序中,所有的事情都是被事件驅(qū)動的。事件繪圖有時是對事件-一個”暴露的“事件-的反應(yīng)。
如果程序窗口被隱藏的一部分重又暴露了(例如窗口從另外一個窗口后面升上來了),X服務(wù)器將發(fā)送一個
”暴露的“事件讓程序知道它應(yīng)當(dāng)重新畫處窗口的這個部分。用戶輸入(按鍵,鼠標(biāo)移動,等)也是作為一
套事件被接收的。
Registering For Event Types Using Event Masks
使用事件遮罩給事件型別注冊
After a program creates a window (or several windows), it should tell the X server what?
types of events it wishes to receive for this window. By default, no events are sent to the?
program. It may register for various mouse (also called "pointer") events, keyboard events,?
expose events and so on. This is done for optimizing the server-to-client connection (i.e.?
why send a program (that might even be running at the other side of the globe) an event it?
is not interested in?).
在程序創(chuàng)建了一個窗口(或者幾個窗口)之后,它應(yīng)當(dāng)告訴X服務(wù)器它想讓這個窗口接收什么型別的事件。
缺省的,沒有事件發(fā)送給程序。它可能注冊各種鼠標(biāo)(也被稱為”指針“)事件,鍵盤事件,暴露事件等等
。這是用于優(yōu)化服務(wù)器和客戶之間的連接(也就是,為什么要發(fā)送給程序(那可能是運(yùn)行于地球的另外一邊
的)它不感興趣的事件的?)。
In Xlib, we use the XSelectInput() function to register for events. This function accepts 3?
parameters - the display structure, an ID of a window, and a mask of the event types it?
wishes to get. The window ID parameter allows us to register for receiving different types?
of events for different windows. Here is how we register for "expose" events for a window?
whose ID is 'win':
在Xlib中,我們使用XSelectInput()函數(shù)來注冊事件。這個函數(shù)接收3個參數(shù) - display結(jié)構(gòu),窗口的ID
,以及它想要收到的事件型別的遮罩。窗口ID這個參數(shù)使得我們能夠為不同的窗口注冊接收不同型別的事
件。這兒是我們?nèi)绾谓oID為‘win’的窗口注冊”暴露”事件的:
XSelectInput(display, win, ExposureMask);
ExposureMask is a constant defined in the "X.h" header file. If we wanted to register to?
several event types, we can logically "or" them, as follows:
ExposureMask 是定義在頭文件“X.h”中的常量。如果我們想要注冊好幾種事件型別,我們用邏輯或進(jìn)行
連接,如下:
XSelectInput(display, win, ExposureMask | ButtonPressMask);
This registers for "expose" events as well as for mouse button presses inside the given?
window. You should note that a mask may represent several event sub-types.
遮注冊了“暴露”事件以及鼠標(biāo)按鈕在給定窗口按下的事件。你應(yīng)當(dāng)注意到一個遮罩有可能代表了好幾種
事件子型別。
Note: A common bug programmers do is adding code to handle new event types in their program,?
while forgetting to add the masks for these events in the call to XSelectInput(). Such a?
programmer then could sit down for hours debugging his program, wondering "why doesn't my?
program notice that i released the button??", only to find that they registered for button?
press events, but not for button release events.?
注意:一個常見的蹩腳程序員所作的是在它們的程序中添加代碼來處理新的事件型別,而忘記了在調(diào)用
XSelectInput()中添加這些事件的遮罩。這樣的程序員然后坐下來花數(shù)個小時調(diào)試它們的程序,奇怪于“
為什么我的程序沒注意到我釋放鼠標(biāo)??”,最后只是發(fā)現(xiàn)它們忘記了只注冊鼠標(biāo)按下事件,而不是鼠標(biāo)
釋放事件。
Receiving Events - Writing The Events Loop
接收事件-撰寫事件循環(huán)
After we have registered for the event types we are interested in, we need to enter a loop?
of receiving events and handling them. There are various ways to write such a loop, but the?
basic loop looks like this:
在我們?yōu)楦信d趣的事件型別注冊了之后,我們需要進(jìn)入接收事件和處理它們的循環(huán)。有好幾種撰寫這樣的
循環(huán)的辦法,但基本的循環(huán)是這樣的:
/* this structure will contain the event's data, once received. */
XEvent an_event;
/* enter an "endless" loop of handling events. */
while (1) {
? ? XNextEvent(display, &an_event);
? ? switch (an_event.type) {
? ?? ?case Expose:
? ?? ???/* handle this event type... */
? ?? ???.
? ?? ???.
? ?? ???break;
? ?? ?default: /* unknown event type - ignore it. */
? ?? ???break;
? ? }
}
The XNextEvent() function fetches the next event coming from the X server. If no event is?
waiting, it blocks until one is received. When it returns, the event's data is placed in the?
XEvent variable given to the function as the second parameter. After that, the "type" field?
of this variable specifies what type of event we got. Expose is the event type that tells us?
there is a part of the window that needs to be redrawn. After we handle this event, we go?
back and wait for the next event to process. Obviously, we will need to give the user some?
way of terminating the program. This is usually done by handling a special "quit" event, as?
we'll soon see.
XNextEvent()函數(shù)取得從X服務(wù)器發(fā)送來的下一個事件。如果沒有事件在等待,它阻塞在那知道接收到了
一個。當(dāng)它返回了,事件的數(shù)據(jù)被放置給函數(shù)的第二個參數(shù)XEvent變量中。之后,變量的“type”域指定
了我們得到的事件的型別。事件的型別是Expose告訴我們窗口的一部分布需要重畫。在我們處理了事件之
后,我們回過頭來繼續(xù)等待下一個要處理的。明顯,我們需要給用戶某種終止程序的途徑。如我們即將看
到的,這通常是通過處理“quit”事件來完成那個的。
Expose Events
暴露事件
The "expose" event is one of the most basic events an application may receive. It will be?
sent to us in one of several cases:
“暴露”事件是程序可能接收的最基本的事件中的一個。它在一下情況中將發(fā)送給我們:
? ? * A window that covered part of our window has moved away, exposing part (or all) of our?
window.
? ? * Our window was raised above other windows.
? ? * Our window mapped for the first time.
? ? * Our window was de-iconified.?
? ? * 覆蓋我們一部分窗口的窗口被移開了,暴露我們窗口的部分(或者全部)。
? ? * 我們的窗口從其他窗口后面升上來了
? ? * 我們的窗口第一次映射
? ? * 我們的窗口被取消標(biāo)識了。
You should note the implicit assumption hidden here - the contents of our window is lost?
when it is being obscured (covered) by other windows. One may wonder why the X server does?
not save this contents. The answer is - to save memory. After all, the number of windows on?
a display at a given time may be very large, and storing the contents of all of them might?
require a lot of memory (for instance, a 256 color bitmap covering 400 pixels by 400 pixels?
takes 160KB of memory to store. Now think about 20 windows, some much larger than this?
size). Actually, there is a way to tell the X server to store the contents of a window in?
special cases, as we will see later.
你應(yīng)當(dāng)注意背后隱藏的假設(shè) - 我們窗口的內(nèi)容在被其他窗口遮蓋時候丟失了。你可能奇怪X服務(wù)器為什
么不保存這些內(nèi)容。答案是 - 為了節(jié)省內(nèi)存。畢竟,窗口在display上的數(shù)量在給定時間是非常巨大的
,而且保存它們的所有內(nèi)容可能需要很多的內(nèi)存(例如,大小為400*400象素的256色位圖占據(jù)160KB的內(nèi)存
存儲。現(xiàn)在想想20個窗口,比這個數(shù)字要大得多)。事實(shí)上,在特殊情況下有告訴X服務(wù)器保存窗口內(nèi)容的
辦法,我們將在后面看到。
When we get an "expose" event, we should take the event's data from the "xexpose" member of?
the XEvent structure (in our code example we refer to it as "an_event.xexpose"). It contains?
several interesting fields:
當(dāng)我們得到一個“暴露”事件的時候,我們應(yīng)當(dāng)從XEvent結(jié)構(gòu)的"xexpose“成員處取出事件的數(shù)據(jù)(在我
們的代碼例子中用”an_event.xexpose“引用它)。它包含幾個有趣的域:
count
? ? Number of other expose events waiting in the server's events queue. This may be useful?
if we got several expose events in a row - we will usually avoid redrawing the window until?
we get the last of them (i.e. until count is 0).?
? ? 在服務(wù)器中的事件隊列等待的其他暴露事件的數(shù)量。這個可能在我們一次接連得到好幾個的時候有用?
- 我們通常將避免重畫知道我們得到它們的最后一個(也就是知道count為0的時候)。
Window window
? ? The ID of the window this expose event was sent for (in case our application registered?
for events on several windows).?
? ? 暴露事件被發(fā)送的窗口的ID(如果我們的程序在幾個窗口中注冊事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the window's?
region that needs to be redrawn.?
? ? 需要重畫的窗口區(qū)域的從窗口左上角開始的x和y坐標(biāo)(象素為單位)。
int width, height
? ? The width and height (in pixels) of the window's region that needs to be redraw.
? ? 需要重畫的窗口區(qū)域的寬和高(象素為單位)。?
In our demo programs, we will tend to ignore the region supplied, and simply re-draw all the?
window. However, this is very inefficient, and we will try to demonstrate some techniques?
for drawing only the relevant section of screen later on.
在我們的演示程序中,我們將傾向于忽略提供的區(qū)域,而僅僅重畫整個屏幕。然而,這是非常沒有效率的
,而且我們將嘗試在后面演示一些僅僅畫出相關(guān)屏幕部分的技術(shù)。
As an example, here is how we will draw a line across our window, whenever we receive?
"expose" events. Assume this 'case' is part of the event loop's switch command.
作為例子,這是我們將如何橫跨我們的窗口畫一條線,每當(dāng)我們接收到”暴露“事件的時候。假設(shè)這個’
case‘是事件循環(huán)switch語句的一部分。
??case Expose:
? ? /* if we have several other expose events waiting, don't redraw. */
? ? /* we will do the redrawing when we receive the last of them.? ? */
? ? if (an_event.xexpose.count > 0)
? ?? ???break;
? ? /* ok, now draw the line... */
? ? XDrawLine(display, win, gc, 0, 100, 400, 100);
? ? break;
Getting User Input
獲得用戶輸入
User input traditionally comes from two sources - the mouse and the keyboard. Various event?
types exist to notify us of user input - a key being pressed on the keyboard, a key being?
released on the keyboard, the mouse moving over our window, the mouse entering (or leaving)?
our window and so on.?
傳統(tǒng)上用戶輸入有兩個來源 - 鼠標(biāo)和鍵盤。存在多種事件型別來通知我們用戶的輸入 - 鍵盤上的按鍵
被按下,在鍵盤上釋放按鍵,鼠標(biāo)移動于我們的窗口之上,鼠標(biāo)進(jìn)入(或者離開)我們的窗口等等。
Mouse Button Click And Release Events
鼠標(biāo)按鈕點(diǎn)擊和釋放事件
The first event type we'll deal with is a mouse button-press (or button release) event in?
our window. In order to register to such an event type, we would add one (or more) of the?
following masks to the event types we specify for the XSelectInput() function:
我們將要處理的第一個事件型別是在我們窗口中鼠標(biāo)按鈕按下(或者按鍵放開)事件。為了注冊這樣的一個
事件,我們將添加一下遮罩中一個(或者更多)來在XSelectInput()函數(shù)中指定事件型別:
ButtonPressMask
? ? Notify us of any button that was pressed in one of our windows.
ButtonReleaseMask
? ? Notify us of any button that was released over one of our windows.
The event types to be checked for in our event-loop switch, are any of the following:
ButtonPress
? ? A button was pressed over one of our windows.
ButtonRelease
? ? A button was released over one of our windows.
The event structure for these event types is accessed as "an_event.xbutton", and contains?
the following interesting fields:
這些事件型別的事件結(jié)構(gòu)是通過"an_event.xbutton“來訪問的,并且包括一下有趣的域:
Window window
? ? The ID of the window this button event was sent for (in case our application registered?
for events on several windows).
? ? 鼠標(biāo)事件發(fā)送給的窗口ID(如果我們的程序在幾個窗口中注冊了事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?
pointer, during the click.
? ? 在點(diǎn)擊時,鼠標(biāo)指針從窗口左上角為原點(diǎn)的x和y坐標(biāo)(象素為單位)。
int button
? ? The number of mouse button that was clicked. May be a value such as Button1, Button2,?
Button3.
? ? 被點(diǎn)擊的鼠標(biāo)按鈕的編號??赡苁窍馚utton1, Button2, Button3這樣的值。
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?
situations by an application (e.g. if the mouse button was clicked two times in a duration?
shorter than a given amount, assume this was a double-click).
? ? 事件發(fā)生的時間(毫秒為單位)??赡苡糜谠诔绦蛑杏嬎恪彪p擊“的情況(例如,如果鼠標(biāo)按鈕在小于
給定時間內(nèi)被點(diǎn)擊兩次,就認(rèn)定這個為雙擊)。
As an example, here is how we will draw a black pixel at the mouse position, whenever we?
receive "button press" events, with the 1st mouse button, and erase that pixel (i.e. draw a?
white pixel) when the 2nd mouse button is pressed. We assume the existence of two GCs,?
gc_draw with foreground color set to black, and gc_erase, with foreground color set to?
white.
作為例子,這兒是我們?nèi)绾蚊慨?dāng)接收到”鼠標(biāo)按下“事件時,當(dāng)按下的是第一個鼠標(biāo)按鈕的時候在鼠標(biāo)點(diǎn)
擊位置畫一個黑點(diǎn)的,而是第二個的時候擦除該點(diǎn)(也就是畫個白點(diǎn))。我們假定存在兩個GC,gc_draw設(shè)
置為前景色為黑,而gc_erase前景色為白。
Assume that the following 'case' is part of the event loop's switch command.
假定一下'case’是事件循環(huán)的swtich語句的一部分。
??case ButtonPress:
? ? /* store the mouse button coordinates in 'int' variables. */
? ? /* also store the ID of the window on which the mouse was */
? ? /* pressed.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? x = an_event.xbutton.x;
? ? y = an_event.xbutton.y;
? ? the_win = an_event.xbutton.window;
? ? /* check which mouse button was pressed, and act accordingly. */
? ? switch (an_event.xbutton.button) {
? ?? ???case Button1:
? ?? ?? ?? ?/* draw a pixel at the mouse position. */
? ?? ?? ?? ?XDrawPoint(display, the_win, gc_draw, x, y);
? ?? ?? ?? ?break;
? ?? ???case Button2:
? ?? ?? ?? ?/* erase a pixel at the mouse position. */
? ?? ?? ?? ?XDrawPoint(display, the_win, gc_erase, x, y);
? ?? ?? ?? ?break;
? ?? ???default: /* probably 3rd button - just ignore this event. */
? ?? ?? ?? ?break;
? ? }
? ? break;
Mouse Movement Events
鼠標(biāo)移動事件
Similar to mouse button press and release events, we also can be notified of various mouse?
movement events. These can be split into two families. One is of mouse pointer movement?
while no buttons are pressed, and the second is a mouse pointer motion while one (or more)?
of the buttons are pressed (this is sometimes called "a mouse drag operation", or just?
"dragging"). The following event masks may be added in the call to XSelectInput() for our?
application to be notified of such events:
與鼠標(biāo)按下和釋放事件類似,我們也可以得到各種鼠標(biāo)移動事件的通知。這些能夠劃分為兩類。一類是按
鈕沒有被按下時鼠標(biāo)指針的移動,而第二類時當(dāng)一個或者多個按鈕被按下的時候鼠標(biāo)指針的移動(這有時
被稱為“鼠標(biāo)托放操作”,或者僅僅“拖放”)。下面的事件遮罩可以加到XSelectInput()的調(diào)用中以讓
我們的應(yīng)用程序得到這些事件的通知。
PointerMotionMask
? ? Events of the pointer moving in one of the windows controlled by our application, while?
no mouse button is held pressed.
指針移動遮罩
? ? 當(dāng)沒有鼠標(biāo)按鈕被按下時,由程序控制的窗口中的一個的指針移動的事件
ButtonMotionMask
? ? Events of the pointer moving while one (or more) of the mouse buttons is held pressed.
按鈕移動遮罩
? ? 當(dāng)鼠標(biāo)的一個(或者更多)的鼠標(biāo)按鈕被按下的時候指針移動的事件。
Button1MotionMask
? ? Same as ButtonMotionMask, but only when the 1st mouse button is held pressed.
按鈕1移動遮罩
? ? 核按鈕移動遮罩一樣,只不過當(dāng)?shù)谝粋€鼠標(biāo)按鈕被按下的時候。
Button2MotionMask, Button3MotionMask, Button4MotionMask, Button5MotionMask
? ? Likewise, for 2nd mouse button, or 3rd, 4th or 5th.
鼠標(biāo)2移動遮罩,鼠標(biāo)3移動遮罩,鼠標(biāo)4移動遮罩,鼠標(biāo)5移動遮罩
? ? 類似的,用于第二個鼠標(biāo)按鈕,或者第三,第四,第五。
The event types to be checked for in our event-loop switch, are any of the following:
在我們的事件循環(huán)swtich語句中要檢查的事件型別,是以下的任何一個:
MotionNotify
? ? The mouse pointer moved in one of the windows for which we requested to be notified of?
such events.
移動通知
? ? 在我們需要得到這個消息通知的窗口中移動的鼠標(biāo)指針。
The event structure for these event types is accessed as "an_event.xbutton", and contains?
the following interesting fields:
這些事件型別的事件結(jié)構(gòu)是以”an_event.xbutton“來訪問的,并且包含一下有趣的域:
Window window
? ? The ID of the window this mouse motion event was sent for (in case our application?
registered for events on several windows).
? ? 鼠標(biāo)移動事件發(fā)送給的窗口的ID(如果我們的應(yīng)用程序給幾個窗口注冊了事件)。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?
pointer, when the event was generated.
? ? 事件發(fā)生的時候,以窗口的左上角為原點(diǎn)鼠標(biāo)指針?biāo)挥诘膞和y坐標(biāo)(象素為單位)。
unsigned int state
? ? A mask of the buttons (or keys) held down during this event - if any. This field is a?
bitwise OR of any of the following:
? ? 按鈕(或者按鍵)在事件發(fā)生時按下的遮罩 - 如果有的話。改域是以下值的位或:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?
being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?
is usually the 'ALT' key or the 'META' key).?
? ? 它們的名字是可以自明的,前五個是指被按下的鼠標(biāo)按鈕,而剩下的指的是被按下的“特殊按鍵”
(Mod1通常是‘ALT’或者‘META’鍵)。
Time time
? ? time (in millisecond) the event took place in.
? ? 事件發(fā)生所處的事件(毫秒為單位)。
As an example, the following code handles a "draw mode" for a painting program, that is, if?
the user moves the mouse while the 1st mouse button is being held down, then we 'draw' on?
the screen. Note that this code has a flow: Since mouse movement may generate many events,?
it might be that we won't get a mouse motion event for each pixel the mouse moved over. Our?
program should be able to cope with such a situation. One way to do that would be to?
remember the last pixel the mouse was dragged over, and draw a line between that position?
and the new mouse pointer position. Assume that the following 'case' is part of the event?
loop's switch command.
作為例子,以下的代碼處理一個繪圖程序的“繪圖模式”,也就是說如果用戶在鼠標(biāo)1鍵按下的時候移動
了,那么我們在屏幕上“繪圖”。注意代碼有一個慣性:因為鼠標(biāo)移動可能產(chǎn)生許多事件,可能我們不會
在每個鼠標(biāo)移到的點(diǎn)都得到鼠標(biāo)移動事件。我們的程序應(yīng)當(dāng)能夠處理這么一個情況。解決的一個辦法可能
是記住鼠標(biāo)托過的上一個點(diǎn),并在和新的鼠標(biāo)指針位置之間畫直線。假定下面的‘case’是事件循環(huán)的
switch語句的一部分。
??case MotionNotify:
? ? /* store the mouse button coordinates in 'int' variables. */
? ? /* also store the ID of the window on which the mouse was */
? ? /* pressed.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? x = an_event.xmotion.x;
? ? y = an_event.xmotion.y;
? ? the_win = an_event.xbutton.window;
? ? /* if the 1st mouse button was held during this event, draw a pixel */
? ? /* at the mouse pointer location.? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???*/
? ? if (an_event.xmotion.state & Button1Mask) {
? ?? ???/* draw a pixel at the mouse position. */
? ?? ???XDrawPoint(display, the_win, gc_draw, x, y);
? ? }
? ? break;
Mouse Pointer Enter And Leave Events
鼠標(biāo)指針進(jìn)入和離開事件
Another type of event that applications might be interested at, is a mouse pointer entering?
a window the program controls, or leaving such a window. Some programs use these events to?
show the user that the application is now in focus. In order to register for such an event?
type, we would add one (or more) of the following masks to the event types we specify for?
the XSelectInput() function:
另一個應(yīng)用程序可能感興趣的事件型別,是鼠標(biāo)指針進(jìn)入或者離開程序控制的窗口。一些程序使用這些事
件來向用戶展示應(yīng)用程序現(xiàn)在在焦點(diǎn)狀態(tài)。為了注冊這么一個事件型別,我們將把下面的一個(或者多個)
遮罩添加到我們給XSelectInput()函數(shù)指定的事件型別中:
EnterWindowMask
? ? Notify us when the mouse pointer enters any of our controlled windows.
? ? 當(dāng)鼠標(biāo)指針進(jìn)入我們控制的任何窗口時通知我們。
LeaveWindowMask
? ? Notify us when the mouse pointer leaves any of our controlled windows.
? ? 當(dāng)鼠標(biāo)指針離開我們控制的任何窗口時通知我們。
The event types to be checked for in our event-loop switch, are any of the following:
要在我們的事件循環(huán)swtich中檢查的事件型別是以下的這些:
EnterNotify
? ? The mouse pointer just entered one of our controlled windows.
? ? 鼠標(biāo)指針剛剛進(jìn)入了我們控制的窗口
LeaveNotify
? ? The mouse pointer just left one of our controlled windows.
? ? 鼠標(biāo)指針剛剛離開了我們控制的窗口
The event structure for these event types is accessed as "an_event.xcrossing", and contains?
the following interesting fields:
這些事件型別的事件結(jié)構(gòu)是通過“an_event.xcrossing"來訪問的,并且包含以下有趣的域:
Window window
? ? The ID of the window this button event was sent for (in case our application registered?
for events on several windows).
? ? 鼠標(biāo)事件發(fā)送給的窗口的ID(如果我們的程序給幾個程序注冊了事件)。
Window subwindow
? ? The ID of the child window from which the mouse entered our window (in an EnterNotify?
event), or into which the mouse pointer has moved (in a LeaveNotify event), or None, if the?
mouse moved from outside our window.
? ? 鼠標(biāo)進(jìn)入到我們的窗口(在EnterNotify事件中),或者鼠標(biāo)指針移出到的子窗口ID(在LeaveNotify事
件中),或者兩者均否,如果鼠標(biāo)從我們的窗口外邊移入。
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?
pointer, when the event was generated.
? ? 事件產(chǎn)生的時候,鼠標(biāo)指針以窗口的左上角為原點(diǎn)的x和y坐標(biāo)(象素為單位)。
int mode
? ? The number of mouse button that was clicked. May be a value such as Button1, Button2,?
Button3.
? ? 鼠標(biāo)指針點(diǎn)擊的編號??赡苁侨鏐utton1, Button2, Button3這樣的值。
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?
situations by an application (e.g. if the mouse button was clicked two times in a duration?
shorter than a given amount, assume this was a double-click).
? ? 事件發(fā)生的時間(毫秒為單位)??赡苡糜谠诔绦蛑杏嬎恪彪p擊“的情況(例如,如果鼠標(biāo)按鈕在小于
給定時間內(nèi)被點(diǎn)擊兩次,就認(rèn)定這個為雙擊)。
unsigned int state
? ? A mask of the buttons (or keys) held down during this event - if any. This field is a?
bitwise OR of any of the following:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?
being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?
is usually the 'ALT' key or the 'META' key).?
Bool focus
? ? Set to True if the window has the keyboard focus, False otherwise.
? ? 如果窗口擁有鍵盤焦點(diǎn),設(shè)置為真。否則反之。
The Keyboard Focus
鍵盤焦點(diǎn)
There may be many windows on a screen, but only a single keyboard attached to them. How does?
the X server then know which window should be sent a given keyboard input? This is done?
using the keyboard focus. Only a single window on the screen may have the keyboard focus at?
a given time. There are Xlib functions that allow a program to set the keyboard focus to a?
given window. The user can usually set the keyboard focus using the window manager (often by?
clicking on the title bar of the desired window). Once our window has the keyboard focus,?
every key press or key release will cause an event to be sent to our program (if it?
registered for these event types...).
屏幕上有許多窗口,但是僅僅有一個鍵盤和他附著。X服務(wù)器如何知道鍵盤輸入是發(fā)送給哪個窗口的呢?
這個是通過鍵盤焦點(diǎn)來完成的。在給定的時間,屏幕上只有一個窗口能夠有鍵盤焦點(diǎn)。存在Xlib函數(shù)來使
得程序給某個窗口設(shè)置鍵盤焦點(diǎn)。用戶通常能夠使用窗口管理器來安排鍵盤焦點(diǎn)(通常是通過點(diǎn)擊所需窗
口的標(biāo)題欄)。一旦我們的窗口擁有了鍵盤焦點(diǎn),每個鍵的按下和放開都將導(dǎo)致事件發(fā)送給我們的程序(如
果它注冊了這些事件類型...)。
Keyboard Press And Release Events
鍵盤按下和釋放事件
If a window controlled by our program currently holds the keyboard focus, it can receive key?
press and key release events. In order to register for such events, any of the following?
masks may be added to the call to XSelectInput():
如果由我們程序控制的窗口當(dāng)前保有鍵盤焦點(diǎn),它能夠接收鍵的按下和釋放事件。為了注冊這些事件,下
面的遮罩要加到XSelectInput()的調(diào)用中去:
KeyPressMask
? ? Notify our program when a key was pressed while any of its controlled windows had the?
keyboard focus.
KeyPressMask
? ? Notify our program when a key was released while any of its controlled windows had the?
keyboard focus.
The event types to be checked for in our event-loop switch, are any of the following:
KeyPress
? ? A key was just pressed on the keyboard while any of our windows had the keyboard focus.
KeyRelease
? ? A key was just released on the keyboard while any of our windows had the keyboard focus.
The event structure for these event types is accessed as "an_event.xkey", and contains the?
following interesting fields:
Window window
? ? The ID of the window this button event was sent for (in case our application registered?
for events on several windows).
unsigned int keycode
? ? The code of the key that was pressed (or released). This is some internal X code, that?
should be translated into a key symbol, as will be explained below.
int x, y
? ? The x and y coordinates (in pixels) from the top-left of the window, of the mouse?
pointer, when the event was generated.
Time time
? ? time (in millisecond) the event took place in. May be used to calculate "double-click"?
situations by an application (e.g. if the mouse button was clicked two times in a duration?
shorter than a given amount, assume this was a double-click).
unsigned int state
? ? A mask of the buttons (or modifier keys) held down during this event - if any. This?
field is a bitwise OR of any of the following:
??# Button1Mask
??# Button2Mask
??# Button3Mask
??# Button4Mask
??# Button5Mask
??# ShiftMask
??# LockMask
??# ControlMask
??# Mod1Mask
??# Mod2Mask
??# Mod3Mask
??# Mod4Mask
??# Mod5Mask
? ? Their names are self explanatory, where the first 5 refer to mouse buttons that are?
being pressed, while the rest refer to various "special keys" that are being pressed (Mod1?
is usually the 'ALT' key or the 'META' key).?
As we mentioned, the key code is rather meaningless on its own, and is affected by the?
specific keyboard device attached to the machine running the X server. To actually use this?
code, we translate it into a key symbol, which is standardized. We may use the?
XKeycodeToKeysym() function to do the translation. This function gets 3 parameters: a?
pointer to the display, the key code to be translated, and an index (we'll supply '0' for?
this parameter). Standard Xlib key codes are found in the include file "X11/keysymdef.h". As?
an example for using the key press events together with the XKeycodeToKeysym function, we'll?
show how to handle key presses of this sort: Pressing '1' will cause painting the pixel?
where the mouse pointer is currently located. Pressing the DEL key will cause to erase that?
pixel (using a 'gc_erase' GC). Pressing any of the letters (a to z, upper case or lower?
case) will cause it to be printed to standard output. Any other key pressed will be ignored.?
Assume that the following 'case' is part of the event loop's switch command.?
如果我們提到過的,按鍵代碼于其本身是相當(dāng)沒有意義的,并且是受到了附著于運(yùn)行X服務(wù)器的機(jī)器的鍵
盤設(shè)備的影響。為了真正使用這些代碼,我們要把他們翻譯為按鍵符號,它們是標(biāo)準(zhǔn)的。我們能用
XKeycodeToKeysym()函數(shù)來完成翻譯工作。這個函數(shù)需要3個參數(shù):只想display的指針,要翻譯的按鍵代
碼,和一個索引(我們將用‘0’來作這個參數(shù))。標(biāo)準(zhǔn)Xl
總結(jié)
以上是生活随笔為你收集整理的用Xlib库进行基本图形编程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开机出现grub rescue无法进入系
- 下一篇: linux中ramdisk的使用