summaryrefslogtreecommitdiff
path: root/xlib-tutorial
diff options
context:
space:
mode:
Diffstat (limited to 'xlib-tutorial')
-rw-r--r--xlib-tutorial/2nd-program-anatomy.html282
-rw-r--r--xlib-tutorial/Xlib.pdfbin357941 -> 0 bytes
-rw-r--r--xlib-tutorial/index.html116
-rw-r--r--xlib-tutorial/prog-1.cc29
-rw-r--r--xlib-tutorial/prog-2.cc74
-rw-r--r--xlib-tutorial/server.html89
6 files changed, 0 insertions, 590 deletions
diff --git a/xlib-tutorial/2nd-program-anatomy.html b/xlib-tutorial/2nd-program-anatomy.html
deleted file mode 100644
index d156f7a..0000000
--- a/xlib-tutorial/2nd-program-anatomy.html
+++ /dev/null
@@ -1,282 +0,0 @@
1<HTML>
2<HEAD>
3<TITLE>Xlib programming tutorial: anatomy of the most basic Xlib program</TITLE>
4</HEAD>
5
6<BODY>
7<H1 ALIGN=center>Anatomy of the most basic Xlib program</H1>
8
9The program starts with the legal stuff:
10
11<PRE><CODE>
12#include &lt;X11/Xlib.h&gt; // Every Xlib program must include this
13#include &lt;assert.h&gt; // I include this to test return values the lazy way
14#include &lt;unistd.h&gt; // So we got the profile for 10 seconds
15
16#define NIL (0) // A name for the void pointer
17</PRE></CODE>
18
19Then the serious thing. First we open a connection to the server.
20
21<PRE><CODE>
22Display *dpy = XOpenDisplay(NIL);
23assert(dpy);
24</PRE></CODE>
25
26If it fails (and it may), <B><A HREF="/gui/x/xlib/display/opening.html">XOpenDisplay()</A></B> will return NIL.
27
28<P>
29
30We gonna create a window, but we need to get the window's background
31color first. X uses a quite complex color model in order to accommodate
32to every conceivable piece of hardware. Each color is encoded by an integer,
33but the integer for a given color may change from a machine to another
34one, and even on the same machine, from an execution of the program to
35the next. The only "colors" that X guarantees to exist are black and
36white. We can get them using the
37<B><A HREF="/gui/x/xlib/display/display-macros.html#BlackPixel">BlackPixel()</A></B>
38and
39<B><A HREF="/gui/x/xlib/display/display-macros.html#WhitePixel">WhitePixel()</A></B>
40macros.
41
42<PRE><CODE>
43 int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
44 int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
45</PRE></CODE>
46
47As we yet can see, most of the Xlib calls take the "display" (the
48value returned by
49<B><A HREF="/gui/x/xlib/display/opening.html">XOpenDisplay()</A></B>)
50as their first parameter. <A HREF="server.html">You want to know why ?</A>
51
52<P>
53
54There is still someting magic, (the
55<B><A HREF="/gui/x/xlib/display/display-macros.html#DefaultScreen">DefaultScreen()</A></B>
56stuff), but we gonna keep it for a <A HREF="screen-and-root-window.html">later
57explanation</A>. We now can
58create our window:
59
60<PRE><CODE>
61 // Create the window
62
63 Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
64 200, 100, 0, blackColor, blackColor);
65</PRE></CODE>
66
67Unlike what appears in the <A HREF="./">dialog</A>, we use the
68function
69<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateSimpleWindow()</A></B>
70instead of
71<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateWindow()</A></B>.
72<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateSimpleWindow()</A></B>
73is not really simpler than
74<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateWindow()</A></B>
75(it takes only a few less parameters), but it uses less concepts, so
76we gonna stick to it for now. There are still a bunch of parameters to
77explain:
78
79<UL>
80
81<LI> <CODE>dpy</CODE> is the usual display connection (<A HREF="server.html">remember</A>).
82
83<P><LI> <CODE>DefaultRootWindow(dpy)</CODE>: yet another parameter that may
84seem magic until now. This is the "parent window" of the window we are
85creating. The window we create appears inside its parent, and is
86bounded by it (the window is said to be "clipped" by its
87parent). Those who guess from the name "Default" that they may be
88other root windows guess right. More on this <A
89HREF="screen-and-root-window.html">later</A>. For now, the default
90root window makes appear our window on the screen, and will give the
91<A HREF="window-manager.html">window manager</A> a chance to decorate
92our window.
93
94<P><LI> <CODE>0, 0</CODE> These are the coordinates of the upper left
95corner of the window (the origin of the coordinates in X is at the
96upper left, contrary to most mathematical textbooks). The dimensions,
97like every dimensions in X, are in pixels (X does not support
98user-defined scales, unlike some other graphical systems like
99<A HREF="/web-directory/science-and-technology/computer/graphics/">OpenGL</A>).
100
101<P>
102
103Contrary to what may seem, there is very little chance for the window
104to appear, at 0,0. The reason is that the
105<A HREF="window-manager.html">window manager</A> will put the window
106at its policy-defined position.
107
108<P><LI> <CODE>200, 100</CODE>: these are the width and height of the
109window, in pixels.
110
111<P><LI> <CODE>0</CODE>: this is the width of the window's border. This
112has nothing to do with the border appended by the
113<A HREF="window-manager.html">window manager</A>, so this is most
114often set to zero.
115
116<P><LI> <CODE>blackColor, blackColor</CODE>: these are the colors of the
117window's border (NOT the
118<A HREF="window-manager.html">window manager</A>'s border), and the
119window's background, respectively.
120<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateSimpleWindow()</A></B>
121clears the window when created,
122<B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateWindow()</A></B>
123does not.
124
125</UL>
126
127<PRE><CODE>
128 // We want to get MapNotify events
129
130 XSelectInput(dpy, w, StructureNotifyMask);
131</PRE></CODE>
132
133As we're starting to know, X is based upon a
134<A HREF="server.html">client-server</A> architecture. The X server
135sends events to the client (the program we're writing), to keep it
136informed of the modifications in the server. There are many of them
137(each time a window is created, moved, masked, unmasked, and many
138other things), so a client must tell the server the events it is
139interested in. With this <B>XSelectInput()</B> stuff, we tell the
140server we want to be informed of "structural" changes occuring on the
141<TT>w</TT> window. Creation and mapping are such changes. There is no
142way to be informed for example of only mapping modification, and not
143creations, so we've to take everything. In this particular application
144we're interesting in "mapping" events (<I>grosso modo</I>, the window
145appears on the screen).
146
147<PRE><CODE>
148 // "Map" the window (that is, make it appear on the screen)
149
150 XMapWindow(dpy, w);
151</PRE></CODE>
152
153And (once again) this is a <A HREF="server.html">client-server</A>
154system. The map request is asynchronous, meaning that the time this
155instruction is executed doesn't tell us when the window is actually
156mapped. To be sure, we've to wait for the server to send us a
157<B><A HREF="/gui/x/xlib/events/window-state-change/map.html">MapNotify</A></B>
158event (this is why we want to be sensitive to such events).
159
160<PRE><CODE>
161 // Create a "Graphics Context"
162
163 GC gc = XCreateGC(dpy, w, 0, NIL);
164</PRE></CODE>
165
166Yet another magic stuff. But mastering them is the reason of the
167existence of this tutorial...
168
169<P>
170
171For several reasons, the graphical model of X is stateless, meaning
172that the server doesn't remember (among other things) attributes such
173as the drawing color, the thickness of the lines and so on. Thus,
174we've to give <EM>all these parameters</EM> to the server on each
175drawing request. To avoid passing two dozens of parameters, many of
176them unchanged from one request to the next, X uses an object called
177the <B>Graphics Context</B>, or <B>GC</B> for short. We store in the
178graphics context all the needed parameters. Here, we want the color
179used to draw lines, called the foregound color:
180
181<PRE><CODE>
182 // Tell the GC we draw using the white color
183
184 XSetForeground(dpy, gc, whiteColor);
185</PRE></CODE>
186
187There are many other parameters used to draw a line, but all of them
188have reasonable default values.
189
190<P>
191
192That's okay for now. Everything is set up, and we wait for the window
193mapping.
194
195<PRE><CODE>
196 // Wait for the MapNotify event
197
198 for(;;) {
199 XEvent e;
200 XNextEvent(dpy, &e);
201 if (e.type == MapNotify)
202 break;
203 }
204</PRE></CODE>
205
206We loop, taking events as they come and discarding them. When we get a
207<B>MapNotify</B>, we exit the loop. We may get events other than
208<B>MapNotify</B> for two reasons:
209
210<UL>
211<LI> We have selected <B>StructureNotifyMask</B> to get
212<B>MapNotify</B> events, but we could get other events as well (such
213as <B>ConfigureNotify</B>, telling the window has changed in position, and/or
214size).
215<LI> Some events can be received, even if we don't have asked for
216them, they are called "non-maskable". <B>GraphicsExpose</B> is such an
217event.
218</UL>
219
220The non-maskable events are sent only in response to some program
221requests (such as copying an area), so they aren't likely to happen in
222our context.
223
224<P>
225
226The
227<B><A HREF="/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html">XNextEvent()</A></B>
228procedure is blocking, so if there are no event to read, the program
229just wait inside the
230<B><A HREF="/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html">XNextEvent()</A></B>.
231
232<P>
233
234When we have exited the loop, we have good confidence that the window
235appears on the screen. Actually, this may not be the case since, for
236example, the user may have iconified it using the
237<A HREF="window-manager.html">window manager</A>, but for now, we assume the window
238actually appears. We can draw our line:
239
240<PRE><CODE>
241 // Draw the line
242
243 XDrawLine(dpy, w, gc, 10, 60, 180, 20);
244</PRE></CODE>
245
246The line is between points (10, 60) and (180, 20). The (0,0) is at the
247upper left corner of the window, as usual. If the program just
248<TT>sleep</TT>s here, nothing will happen, because, in case you don't
249know, X has a <A HREF="server.html">client-server</A>
250architecture. Thus the request stays in the client, unless we tell it
251to go to the server. This is done by <B><A
252HREF="/gui/x/xlib/event-handling/XFlush.html">XFlush()</A></B>:
253
254<PRE><CODE>
255 // Send the "DrawLine" request to the server
256
257 XFlush(dpy);
258</PRE></CODE>
259
260Clever readers may have noticed that we didn't use
261<B><A HREF="/gui/x/xlib/event-handling/XFlush.html">XFlush()</A></B>
262before, and it didn't prevent all the requests such as
263<B><A HREF="/gui/x/xlib/window/XMapWindow.html">XMapWindow()</A></B>
264to be sent to the server. The answer is that
265<B><A HREF="/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html">XNextEvent()</A></B>
266performs an implicit
267<B><A HREF="/gui/x/xlib/event-handling/XFlush.html">XFlush()</A></B>
268before trying to read some events. We have our line now, we just wait
269for 10 seconds, so we can make people see how beautiful is our work:
270
271<PRE><CODE>
272 // Wait for 10 seconds
273
274 sleep(10);
275</PRE></CODE>
276
277That's all for now. In <!A HREF="more-interaction.html"><B>next
278lesson</B></A>, we will have a (very) little more interaction. [to be continued]
279
280<HR><ADDRESS><A HREF="http://tronche.com/">Christophe Tronche</A>, <A HREF="mailto:ch.tronche@computer.org">ch.tronche@computer.org</A></ADDRESS>
281</BODY>
282</HTML>
diff --git a/xlib-tutorial/Xlib.pdf b/xlib-tutorial/Xlib.pdf
deleted file mode 100644
index cef1647..0000000
--- a/xlib-tutorial/Xlib.pdf
+++ /dev/null
Binary files differ
diff --git a/xlib-tutorial/index.html b/xlib-tutorial/index.html
deleted file mode 100644
index 5e57eaf..0000000
--- a/xlib-tutorial/index.html
+++ /dev/null
@@ -1,116 +0,0 @@
1<HTML>
2<HEAD>
3<TITLE>Xlib programming: a short tutorial</TITLE>
4</HEAD>
5
6<BODY>
7
8<H1>Xlib programming: a short tutorial</H1>
9
10I haven't found anything very satisfying on the Web as an Xlib
11tutorial. Many of them are too much Motif-oriented for my
12taste. Furthermore, I answer questions about X programming almost
13daily, so I've started to put together some small coursewares.
14
15<H4>Important note:</H4> the example programs are written in C++, but
16this is mainly for the ability to declare variables anywhere.
17
18<HR>
19
20Let's begin with a short story: the eternal story of the newbie at
21Xlib writing his/her first program.
22
23<P>
24
25<I>
26"Ok, I've to open a connection to the X server (whatever this means),
27with XOpenDisplay, then create a window with XCreateWindow, then draw
28a line with XDrawLine. Then, the program sleeps for ten seconds so I
29can see the result. Sounds easy."
30</I>
31
32<P>
33
34The poor newbie writes the program. And nothing happens. He then
35calls his wizard friend.
36
37<P>
38
39<I>
40-"Did you perform an XFlush after you've done everything ?<BR>
41- No, why ?<BR>
42- The requests stay in the client," </I>wizard doubletalk, thinks the
43poor newbie<I>, " if you
44don't."
45</I>
46
47<P>
48
49The poor newbie changes the program. And nothing happens. He then
50calls his wizard friend.
51
52<P>
53
54<I>
55-"Did you map your window ?<BR>
56- Did I do what ???<BR>
57- Creating a window doesn't make it appear on the screen. You've to
58 map it with XMapWindow first.
59</I>
60
61<P>
62
63The poor newbie changes the program. The window appears with nothing
64in it (<A HREF="prog-1.cc">like this</A>). The poor newbie then calls
65his wizard friend.
66
67<P>
68
69<I>
70-"Did you wait for a MapNotify before drawing your line ?" </I>(more wizard doubletalk)<I><BR>
71- "No, why ?</BR>
72- X has a stateless drawing model, the content of the window may be lost
73 when the window isn't on the screen." </I>(overflow, why can't these
74 wizard guys speak just like you and me ?)<I> "You've to wait for a MapNotify
75 before drawing."
76</I>
77
78<P>
79
80The poor newbie changes the program. Things are getting more and more
81complex. Not as easy as it first seemed. A loop gets the events until a
82MapNotify. The window appears with nothing
83in it. The poor newbie then calls his wizard friend.
84
85<P>
86
87<I>
88-"I got it, did you select the StructureNotifyMask on your window ?<BR>
89- ???<BR>
90- Just do it, and everything'll be fine.
91</I>
92
93<P>
94
95The poor newbie fixes the program. And the miracle happens ! A line in the
96window. Until now, the program looks like <A HREF="prog-2.cc">this</A>
97(it is actually slighty more complex than the dialog may let you think).
98
99<P>
100
101Now you've learned at least 2 things:
102
103<UL>
104<LI> How to draw a line in a window with X.
105<LI> Why some may need an X tutorial.
106</UL>
107
108Now, if you want to learn more and get a deeper understanding of the
109program, go to <A HREF="2nd-program-anatomy.html">next lesson</A>.
110
111<H4><A HREF="/gui/x/">More about X</A>.</H4>
112
113
114<HR><ADDRESS><A HREF="http://tronche.com/">Christophe Tronche</A>, <A HREF="mailto:ch.tronche@computer.org">ch.tronche@computer.org</A></ADDRESS>
115</BODY>
116</HTML>
diff --git a/xlib-tutorial/prog-1.cc b/xlib-tutorial/prog-1.cc
deleted file mode 100644
index 3ba596b..0000000
--- a/xlib-tutorial/prog-1.cc
+++ /dev/null
@@ -1,29 +0,0 @@
1// Written by Ch. Tronche (http://tronche.lri.fr:8000/)
2// Copyright by the author. This is unmaintained, no-warranty free software.
3// Please use freely. It is appreciated (but by no means mandatory) to
4// acknowledge the author's contribution. Thank you.
5// Started on Thu Jun 26 23:29:03 1997
6
7//
8// Xlib tutorial: 1st program
9// Make a window appear on the screen.
10//
11
12#include <X11/Xlib.h> // Every Xlib program must include this
13#include <assert.h> // I include this to test return values the lazy way
14#include <unistd.h> // So we got the profile for 10 seconds
15
16#define NIL (0) // A name for the void pointer
17
18main()
19{
20 Display *dpy = XOpenDisplay(NIL);
21 assert(dpy);
22 Window w = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0,
23 200, 100, 0,
24 CopyFromParent, CopyFromParent, CopyFromParent,
25 NIL, 0);
26 XMapWindow(dpy, w);
27 XFlush(dpy);
28 sleep(10);
29}
diff --git a/xlib-tutorial/prog-2.cc b/xlib-tutorial/prog-2.cc
deleted file mode 100644
index 51ea0e9..0000000
--- a/xlib-tutorial/prog-2.cc
+++ /dev/null
@@ -1,74 +0,0 @@
1// Written by Ch. Tronche (http://tronche.lri.fr:8000/)
2// Copyright by the author. This is unmaintained, no-warranty free software.
3// Please use freely. It is appreciated (but by no means mandatory) to
4// acknowledge the author's contribution. Thank you.
5// Started on Thu Jun 26 23:29:03 1997
6
7//
8// Xlib tutorial: 2nd program
9// Make a window appear on the screen and draw a line inside.
10// If you don't understand this program, go to
11// http://tronche.lri.fr:8000/gui/x/xlib-tutorial/2nd-program-anatomy.html
12//
13
14#include <X11/Xlib.h> // Every Xlib program must include this
15#include <assert.h> // I include this to test return values the lazy way
16#include <unistd.h> // So we got the profile for 10 seconds
17
18#define NIL (0) // A name for the void pointer
19
20main()
21{
22 // Open the display
23
24 Display *dpy = XOpenDisplay(NIL);
25 assert(dpy);
26
27 // Get some colors
28
29 int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
30 int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
31
32 // Create the window
33
34 Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
35 200, 100, 0, blackColor, blackColor);
36
37 // We want to get MapNotify events
38
39 XSelectInput(dpy, w, StructureNotifyMask);
40
41 // "Map" the window (that is, make it appear on the screen)
42
43 XMapWindow(dpy, w);
44
45 // Create a "Graphics Context"
46
47 GC gc = XCreateGC(dpy, w, 0, NIL);
48
49 // Tell the GC we draw using the white color
50
51 XSetForeground(dpy, gc, whiteColor);
52
53 // Wait for the MapNotify event
54
55 for(;;) {
56 XEvent e;
57 XNextEvent(dpy, &e);
58 if (e.type == MapNotify)
59 break;
60 }
61
62 // Draw the line
63
64 XDrawLine(dpy, w, gc, 10, 60, 180, 20);
65
66 // Send the "DrawLine" request to the server
67
68 XFlush(dpy);
69
70 // Wait for 10 seconds
71
72 sleep(10);
73}
74
diff --git a/xlib-tutorial/server.html b/xlib-tutorial/server.html
deleted file mode 100644
index a408834..0000000
--- a/xlib-tutorial/server.html
+++ /dev/null
@@ -1,89 +0,0 @@
1<HTML>
2<HEAD>
3<TITLE>Xlib programming tutorial: What is all this "client-server" stuff ?</TITLE>
4</HEAD>
5
6<BODY>
7<H1 ALIGN=center>What does all this stuff about "client-server" mean ?</H1>
8
9Everybody says that X has a "client-server" architecture. So this must
10be true, but what does it mean ?
11
12<P>
13
14Well, basically a client-server architecture is conceptually a simple
15thing, but the consequences may be a bit subtle, especially the way it
16is implemented in the Xlib.
17
18<H2>What is a client-server architecture ?</H2>
19
20A client-server architecture is a general mechanism for handling a
21shared resource that several programs may want to access
22simultaneously. In the case of X, the shared resources are the drawing
23area and the input channel. If every process was allowed to write on
24it at its will, several processes may want to draw at the same place,
25resulting in an unpredictable chaos. Thus, only one process is allowed
26to get access to the drawing area: the X server. The processes wanting
27to draw stuff or get inputs send requests to the X servers (they are
28"clients"). They do this over a communication channel. The X server
29performs the requests for its clients, and sends them back replies. It
30may also send messages without explicit client's requests to keep them
31informed of what is going on. These messages sent by the server on its
32own behalf are called "events".
33
34<H2>Advantages of the client-server architecture</H2>
35
36The client-server architecture has several advantages, many of them
37resulting from the ability to run the server and the clients on
38separate machines. Here are some advantages:
39
40<UL>
41
42<LI> A client-server architectured system can be very robust: since
43 the server runs in its own address space, it can protect itself
44 against poorly written clients. Thus, if a client has a bug, it
45 will crash alone, the server and the other clients still running
46 as if nothing has happened.
47
48<LI> The client and the server don't have to run on the same machine,
49 so we have some communication mechanism here.
50
51<LI> The client and the server may run on separate machines, resulting
52 in a better load distribution (possibly).
53
54<LI> The client and the server don't have to run on the same hardware,
55 operating system, etc., giving a better interoperability.
56
57</UL>
58
59<H2>Structure of the X client-server architecture</H2>
60
61As we already mentioned, the server and a client communicates over a
62communication channel. This channel is composed of two layers: the
63low-level one, which is responsible for carrying bytes in a reliable
64way (that is with no loss nor duplication). This link may be among
65others a named pipe in the Unix environment, a DECNet link and of
66course a TCP/IP connection.
67
68<P>
69
70The upper layer use the byte-transport channel to implement a
71higher-level protocol: the X protocol. This protocol says how to tell
72the server to request window creation, graphics drawing, and so on,
73and how the server answers and sends events. The protocol itself is
74separated into different parts:
75
76<UL>
77
78<LI> How to connect and how to break a connection,
79<LI> how to represent the different data types,
80<LI> what are the requests and what they mean and
81<LI> what are the replies and what they mean.
82
83</UL>
84
85[to be continued].
86
87<HR><ADDRESS><A HREF="http://tronche.com/">Christophe Tronche</A>, <A HREF="mailto:ch.tronche@computer.org">ch.tronche@computer.org</A></ADDRESS>
88</BODY>
89</HTML>