diff options
Diffstat (limited to 'xlib-tutorial')
| -rw-r--r-- | xlib-tutorial/2nd-program-anatomy.html | 282 | ||||
| -rw-r--r-- | xlib-tutorial/Xlib.pdf | bin | 357941 -> 0 bytes | |||
| -rw-r--r-- | xlib-tutorial/index.html | 116 | ||||
| -rw-r--r-- | xlib-tutorial/prog-1.cc | 29 | ||||
| -rw-r--r-- | xlib-tutorial/prog-2.cc | 74 | ||||
| -rw-r--r-- | xlib-tutorial/server.html | 89 |
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 | |||
| 9 | The program starts with the legal stuff: | ||
| 10 | |||
| 11 | <PRE><CODE> | ||
| 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 | </PRE></CODE> | ||
| 18 | |||
| 19 | Then the serious thing. First we open a connection to the server. | ||
| 20 | |||
| 21 | <PRE><CODE> | ||
| 22 | Display *dpy = XOpenDisplay(NIL); | ||
| 23 | assert(dpy); | ||
| 24 | </PRE></CODE> | ||
| 25 | |||
| 26 | If it fails (and it may), <B><A HREF="/gui/x/xlib/display/opening.html">XOpenDisplay()</A></B> will return NIL. | ||
| 27 | |||
| 28 | <P> | ||
| 29 | |||
| 30 | We gonna create a window, but we need to get the window's background | ||
| 31 | color first. X uses a quite complex color model in order to accommodate | ||
| 32 | to every conceivable piece of hardware. Each color is encoded by an integer, | ||
| 33 | but the integer for a given color may change from a machine to another | ||
| 34 | one, and even on the same machine, from an execution of the program to | ||
| 35 | the next. The only "colors" that X guarantees to exist are black and | ||
| 36 | white. We can get them using the | ||
| 37 | <B><A HREF="/gui/x/xlib/display/display-macros.html#BlackPixel">BlackPixel()</A></B> | ||
| 38 | and | ||
| 39 | <B><A HREF="/gui/x/xlib/display/display-macros.html#WhitePixel">WhitePixel()</A></B> | ||
| 40 | macros. | ||
| 41 | |||
| 42 | <PRE><CODE> | ||
| 43 | int blackColor = BlackPixel(dpy, DefaultScreen(dpy)); | ||
| 44 | int whiteColor = WhitePixel(dpy, DefaultScreen(dpy)); | ||
| 45 | </PRE></CODE> | ||
| 46 | |||
| 47 | As we yet can see, most of the Xlib calls take the "display" (the | ||
| 48 | value returned by | ||
| 49 | <B><A HREF="/gui/x/xlib/display/opening.html">XOpenDisplay()</A></B>) | ||
| 50 | as their first parameter. <A HREF="server.html">You want to know why ?</A> | ||
| 51 | |||
| 52 | <P> | ||
| 53 | |||
| 54 | There is still someting magic, (the | ||
| 55 | <B><A HREF="/gui/x/xlib/display/display-macros.html#DefaultScreen">DefaultScreen()</A></B> | ||
| 56 | stuff), but we gonna keep it for a <A HREF="screen-and-root-window.html">later | ||
| 57 | explanation</A>. We now can | ||
| 58 | create 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 | |||
| 67 | Unlike what appears in the <A HREF="./">dialog</A>, we use the | ||
| 68 | function | ||
| 69 | <B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateSimpleWindow()</A></B> | ||
| 70 | instead 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> | ||
| 73 | is 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 | ||
| 76 | we gonna stick to it for now. There are still a bunch of parameters to | ||
| 77 | explain: | ||
| 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 | ||
| 84 | seem magic until now. This is the "parent window" of the window we are | ||
| 85 | creating. The window we create appears inside its parent, and is | ||
| 86 | bounded by it (the window is said to be "clipped" by its | ||
| 87 | parent). Those who guess from the name "Default" that they may be | ||
| 88 | other root windows guess right. More on this <A | ||
| 89 | HREF="screen-and-root-window.html">later</A>. For now, the default | ||
| 90 | root 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 | ||
| 92 | our window. | ||
| 93 | |||
| 94 | <P><LI> <CODE>0, 0</CODE> These are the coordinates of the upper left | ||
| 95 | corner of the window (the origin of the coordinates in X is at the | ||
| 96 | upper left, contrary to most mathematical textbooks). The dimensions, | ||
| 97 | like every dimensions in X, are in pixels (X does not support | ||
| 98 | user-defined scales, unlike some other graphical systems like | ||
| 99 | <A HREF="/web-directory/science-and-technology/computer/graphics/">OpenGL</A>). | ||
| 100 | |||
| 101 | <P> | ||
| 102 | |||
| 103 | Contrary to what may seem, there is very little chance for the window | ||
| 104 | to appear, at 0,0. The reason is that the | ||
| 105 | <A HREF="window-manager.html">window manager</A> will put the window | ||
| 106 | at its policy-defined position. | ||
| 107 | |||
| 108 | <P><LI> <CODE>200, 100</CODE>: these are the width and height of the | ||
| 109 | window, in pixels. | ||
| 110 | |||
| 111 | <P><LI> <CODE>0</CODE>: this is the width of the window's border. This | ||
| 112 | has nothing to do with the border appended by the | ||
| 113 | <A HREF="window-manager.html">window manager</A>, so this is most | ||
| 114 | often set to zero. | ||
| 115 | |||
| 116 | <P><LI> <CODE>blackColor, blackColor</CODE>: these are the colors of the | ||
| 117 | window's border (NOT the | ||
| 118 | <A HREF="window-manager.html">window manager</A>'s border), and the | ||
| 119 | window's background, respectively. | ||
| 120 | <B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateSimpleWindow()</A></B> | ||
| 121 | clears the window when created, | ||
| 122 | <B><A HREF="/gui/x/xlib/window/XCreateWindow.html">XCreateWindow()</A></B> | ||
| 123 | does 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 | |||
| 133 | As we're starting to know, X is based upon a | ||
| 134 | <A HREF="server.html">client-server</A> architecture. The X server | ||
| 135 | sends events to the client (the program we're writing), to keep it | ||
| 136 | informed of the modifications in the server. There are many of them | ||
| 137 | (each time a window is created, moved, masked, unmasked, and many | ||
| 138 | other things), so a client must tell the server the events it is | ||
| 139 | interested in. With this <B>XSelectInput()</B> stuff, we tell the | ||
| 140 | server 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 | ||
| 142 | way to be informed for example of only mapping modification, and not | ||
| 143 | creations, so we've to take everything. In this particular application | ||
| 144 | we're interesting in "mapping" events (<I>grosso modo</I>, the window | ||
| 145 | appears 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 | |||
| 153 | And (once again) this is a <A HREF="server.html">client-server</A> | ||
| 154 | system. The map request is asynchronous, meaning that the time this | ||
| 155 | instruction is executed doesn't tell us when the window is actually | ||
| 156 | mapped. 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> | ||
| 158 | event (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 | |||
| 166 | Yet another magic stuff. But mastering them is the reason of the | ||
| 167 | existence of this tutorial... | ||
| 168 | |||
| 169 | <P> | ||
| 170 | |||
| 171 | For several reasons, the graphical model of X is stateless, meaning | ||
| 172 | that the server doesn't remember (among other things) attributes such | ||
| 173 | as the drawing color, the thickness of the lines and so on. Thus, | ||
| 174 | we've to give <EM>all these parameters</EM> to the server on each | ||
| 175 | drawing request. To avoid passing two dozens of parameters, many of | ||
| 176 | them unchanged from one request to the next, X uses an object called | ||
| 177 | the <B>Graphics Context</B>, or <B>GC</B> for short. We store in the | ||
| 178 | graphics context all the needed parameters. Here, we want the color | ||
| 179 | used 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 | |||
| 187 | There are many other parameters used to draw a line, but all of them | ||
| 188 | have reasonable default values. | ||
| 189 | |||
| 190 | <P> | ||
| 191 | |||
| 192 | That's okay for now. Everything is set up, and we wait for the window | ||
| 193 | mapping. | ||
| 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 | |||
| 206 | We 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 | ||
| 213 | as <B>ConfigureNotify</B>, telling the window has changed in position, and/or | ||
| 214 | size). | ||
| 215 | <LI> Some events can be received, even if we don't have asked for | ||
| 216 | them, they are called "non-maskable". <B>GraphicsExpose</B> is such an | ||
| 217 | event. | ||
| 218 | </UL> | ||
| 219 | |||
| 220 | The non-maskable events are sent only in response to some program | ||
| 221 | requests (such as copying an area), so they aren't likely to happen in | ||
| 222 | our context. | ||
| 223 | |||
| 224 | <P> | ||
| 225 | |||
| 226 | The | ||
| 227 | <B><A HREF="/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html">XNextEvent()</A></B> | ||
| 228 | procedure is blocking, so if there are no event to read, the program | ||
| 229 | just wait inside the | ||
| 230 | <B><A HREF="/gui/x/xlib/event-handling/manipulating-event-queue/XNextEvent.html">XNextEvent()</A></B>. | ||
| 231 | |||
| 232 | <P> | ||
| 233 | |||
| 234 | When we have exited the loop, we have good confidence that the window | ||
| 235 | appears on the screen. Actually, this may not be the case since, for | ||
| 236 | example, the user may have iconified it using the | ||
| 237 | <A HREF="window-manager.html">window manager</A>, but for now, we assume the window | ||
| 238 | actually 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 | |||
| 246 | The line is between points (10, 60) and (180, 20). The (0,0) is at the | ||
| 247 | upper 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 | ||
| 249 | know, X has a <A HREF="server.html">client-server</A> | ||
| 250 | architecture. Thus the request stays in the client, unless we tell it | ||
| 251 | to go to the server. This is done by <B><A | ||
| 252 | HREF="/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 | |||
| 260 | Clever readers may have noticed that we didn't use | ||
| 261 | <B><A HREF="/gui/x/xlib/event-handling/XFlush.html">XFlush()</A></B> | ||
| 262 | before, and it didn't prevent all the requests such as | ||
| 263 | <B><A HREF="/gui/x/xlib/window/XMapWindow.html">XMapWindow()</A></B> | ||
| 264 | to 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> | ||
| 266 | performs an implicit | ||
| 267 | <B><A HREF="/gui/x/xlib/event-handling/XFlush.html">XFlush()</A></B> | ||
| 268 | before trying to read some events. We have our line now, we just wait | ||
| 269 | for 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 | |||
| 277 | That's all for now. In <!A HREF="more-interaction.html"><B>next | ||
| 278 | lesson</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 | |||
| 10 | I haven't found anything very satisfying on the Web as an Xlib | ||
| 11 | tutorial. Many of them are too much Motif-oriented for my | ||
| 12 | taste. Furthermore, I answer questions about X programming almost | ||
| 13 | daily, so I've started to put together some small coursewares. | ||
| 14 | |||
| 15 | <H4>Important note:</H4> the example programs are written in C++, but | ||
| 16 | this is mainly for the ability to declare variables anywhere. | ||
| 17 | |||
| 18 | <HR> | ||
| 19 | |||
| 20 | Let's begin with a short story: the eternal story of the newbie at | ||
| 21 | Xlib 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), | ||
| 27 | with XOpenDisplay, then create a window with XCreateWindow, then draw | ||
| 28 | a line with XDrawLine. Then, the program sleeps for ten seconds so I | ||
| 29 | can see the result. Sounds easy." | ||
| 30 | </I> | ||
| 31 | |||
| 32 | <P> | ||
| 33 | |||
| 34 | The poor newbie writes the program. And nothing happens. He then | ||
| 35 | calls 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 | ||
| 43 | poor newbie<I>, " if you | ||
| 44 | don't." | ||
| 45 | </I> | ||
| 46 | |||
| 47 | <P> | ||
| 48 | |||
| 49 | The poor newbie changes the program. And nothing happens. He then | ||
| 50 | calls 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 | |||
| 63 | The poor newbie changes the program. The window appears with nothing | ||
| 64 | in it (<A HREF="prog-1.cc">like this</A>). The poor newbie then calls | ||
| 65 | his 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 | |||
| 80 | The poor newbie changes the program. Things are getting more and more | ||
| 81 | complex. Not as easy as it first seemed. A loop gets the events until a | ||
| 82 | MapNotify. The window appears with nothing | ||
| 83 | in 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 | |||
| 95 | The poor newbie fixes the program. And the miracle happens ! A line in the | ||
| 96 | window. 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 | |||
| 101 | Now 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 | |||
| 108 | Now, if you want to learn more and get a deeper understanding of the | ||
| 109 | program, 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 | |||
| 18 | main() | ||
| 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 | |||
| 20 | main() | ||
| 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 | |||
| 9 | Everybody says that X has a "client-server" architecture. So this must | ||
| 10 | be true, but what does it mean ? | ||
| 11 | |||
| 12 | <P> | ||
| 13 | |||
| 14 | Well, basically a client-server architecture is conceptually a simple | ||
| 15 | thing, but the consequences may be a bit subtle, especially the way it | ||
| 16 | is implemented in the Xlib. | ||
| 17 | |||
| 18 | <H2>What is a client-server architecture ?</H2> | ||
| 19 | |||
| 20 | A client-server architecture is a general mechanism for handling a | ||
| 21 | shared resource that several programs may want to access | ||
| 22 | simultaneously. In the case of X, the shared resources are the drawing | ||
| 23 | area and the input channel. If every process was allowed to write on | ||
| 24 | it at its will, several processes may want to draw at the same place, | ||
| 25 | resulting in an unpredictable chaos. Thus, only one process is allowed | ||
| 26 | to get access to the drawing area: the X server. The processes wanting | ||
| 27 | to 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 | ||
| 29 | performs the requests for its clients, and sends them back replies. It | ||
| 30 | may also send messages without explicit client's requests to keep them | ||
| 31 | informed of what is going on. These messages sent by the server on its | ||
| 32 | own behalf are called "events". | ||
| 33 | |||
| 34 | <H2>Advantages of the client-server architecture</H2> | ||
| 35 | |||
| 36 | The client-server architecture has several advantages, many of them | ||
| 37 | resulting from the ability to run the server and the clients on | ||
| 38 | separate 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 | |||
| 61 | As we already mentioned, the server and a client communicates over a | ||
| 62 | communication channel. This channel is composed of two layers: the | ||
| 63 | low-level one, which is responsible for carrying bytes in a reliable | ||
| 64 | way (that is with no loss nor duplication). This link may be among | ||
| 65 | others a named pipe in the Unix environment, a DECNet link and of | ||
| 66 | course a TCP/IP connection. | ||
| 67 | |||
| 68 | <P> | ||
| 69 | |||
| 70 | The upper layer use the byte-transport channel to implement a | ||
| 71 | higher-level protocol: the X protocol. This protocol says how to tell | ||
| 72 | the server to request window creation, graphics drawing, and so on, | ||
| 73 | and how the server answers and sends events. The protocol itself is | ||
| 74 | separated 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> | ||
