Top Banner
X Windows Antonio Barletta "Only the code you never write has no bugs" (Byte Magazines - April 1998)
68
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: X Windows

X Windows

Antonio Barletta

"Only the code you never write has no bugs"

(Byte Magazines - April 1998)

Page 2: X Windows

X is a distributed, intelligent, device independent, operating

system independent, windowing system.

Page 3: X Windows

History of X ancestoro Early 1970s (Xerox PARC)?DYNABOOK (early 1970s)?STAR (late 1970s)

o First Commercial Exploitation (1980s)?Apple: LISA, Mack series

o Late 1980s many GUI approach?SUN-UNIX (Sun View)?DEC (Dec Windows)?MICROSOFT (Windows on PC)

Page 4: X Windows

Version of Xo X was developed originally by MIT's

Project Athena and Digital Equipment Corporation?X1 (1984)?X10.4 (1986)?X11.1 (september 1987)?X11.2 (march 1988)?X11.3 (february 1989)?X11.4 (january 1990)?X11.6 (august 1997)

X Consortium

MIT

Page 5: X Windows

X Overview (for impatients)

X Server

X lib

X toolkit Intrinsics

Motif Widget

X Applications

IP

TCP

X lib

Page 6: X Windows

X Windows System

o platform independento network based (any kind of network)o 2 high level interface toolkit:?Motif (OSF) - DEC, IBM, HP?Open Look/Open Windows - AT&T, SUN

(dead)

Page 7: X Windows

Common Desktop EnvironmentCDE

o CDE is an environment ?user tools?network tools?deskset applications? and development tools

o it is recognized as a standard by major open systems vendors

Page 8: X Windows

Motif

o Motif 1.0o Motif 1.1 (no drag & drop)o Motif 1.2 (1993) ?is based on Release5 Xlib + Xt

o Motif 2.0 (late 1994) ?X11R6

Page 9: X Windows

UNIX OS Other OS

Macintosh

X

Motif

X windows Unix System

es:Linux

X windows Display/Server

Operating Systems

Solar Net PC-X1.1, PC X-ware 3.0,

Reflection Suite for Windows, eXceed 5 for

Win, ecc

MacX, Planet X, XsoftWare, Xten,

Xodus

PC

Page 10: X Windows

X windows concepts

o Windowing System for Bitmapped Graphics Displays?DISPLAY = KEYBOARD + MOUSE + one or

more SCREENo Server/Client Modelo Network Oriented Windowing Systemo Extensibleo Event driveno Wide variety of languages and operating systemo Security

Page 11: X Windows

X windows Architecture

Page 12: X Windows

Server

o allow access to the display by multiple clients X protocol requests (demux)

o interprets network messages from clientso passes user input to the clients by sending

network messages (replies, error & events)o does two-dimensional drawing, freeing up

the client from processor intensive graphics.

"It is the program that controls each display and outs as an intermediate between users programs ( called clients or applications )"

Page 13: X Windows

Server

o maintain complex data structureso allows distributed processingo keeps track of resources (such as windows,

cursors, fonts and graphics contexts) that are shared between clients

Page 14: X Windows

Server (Unix)

o although might be placed in the kernel in general is a process

o the number of the system calls are minimized to improve performance

o it listens to port(TCP) = 6000+N, where N that goes from 0..n, indicates the number of the screens

o to improve stability...?never trusts on client data: it checks all !!?never waits for a client response

Page 15: X Windows

Client

o many different client connected to a single server

o may run on the same machine of the server

o all communication by X protocol

Page 16: X Windows

X-Windows Manager

o a client written with Xlibo have special responsibilitieso mediates competing demands for physical

resources of a display?screen space?colormap

Page 17: X Windows

X-Window Managero allows the user to move or resize windowso start new applicationso control the stacking of win on the screen

WINDOW LAYOUT POLICY. Set of rules that specify allowable sizes and positions of windows and icons:> TILED: no window are allowed to overlap

> REAL-ESTATE-DRIVEN: keyboard output is automatically assigned to whatever windows

>LISTENER: they listen to a click to type

Page 18: X Windows

Extensible

o a defined mechanism for incorporating extensions

o requires:?additional piece of software on the server?additional library at the level of Xlib?the C programming library (es: -lXext)

Page 19: X Windows

Event Driven

o multiple graphic applications may run simultaneously

o applications are modelesso 2 categories:?user input?window system side effects

o it is server job to distribute events to various window on the screen

Page 20: X Windows

Operating System & Languages

o X windows system is not intended for a specific operating system

o there is not a specific language specification

o C mainly, C++, Pascal, etc

Page 21: X Windows

Securityo simple authorization mechanism (from X11R4 ):?MIT-MAGIC-COOKIE?long randomly generated binary passwd that the

client library must know to contact the server?change each time a server is reset?client have a magic cookie for each server in

~/.Xauthorityo in other system (Win3.11,WinNT,etc) no

security

Page 22: X Windows

Security (example)

In a Linux system:xwd -root -display anywin.X.server:0.0 -out snarfed.xwd(catch the information on Xserver)xwud -in snarfed.xwd(show the snarfed screen image)

Page 23: X Windows

X windows Software Architecture

Page 24: X Windows

Xlib

o Xlib is a set of library routines used to implement the basic X calls

o many toolkit layer use Xlib to interface to the protocol written in C or C++?Xt intrinsic (MIT)?Andrew, InterView, Xview, ecc

Page 25: X Windows

Overview of XlibColor Routines to change the way colors drawn by an application are interpreted on

the screenCursors Routines to change the shape and colors of the image that tracks the pointer

around the screen.DataManagement

Several mechanism to associate data winth windows or numbers

DisplayConnection

Routines to connect and disconnect an application with disply, possiblyacross the network.

Display andServer Spec.

Macros and equivalent functions are provided that provide information abouta particular server implementation and tha connected display hardware.

Drawing Routines to draw dots, lines, rectangles, polygons, arcs and analougous set tofill the last

Errors Routines to set the functions called when errors occur.

Events Routines to get input from the user, fron other applications and from theserver. In X these are collectively called events.

Extensions To find put what extensions are available on a particular server.

Fonts To list available fonts.

Graphics Context To set the way drawing requests are interpreted

host Access To control access to a server from outer machines connected in a network

InterclientCommunications

Routines enabling any client to make available information for any otherclients to read.

Keyboard, Pointer Functions to modify the way input is handled.

Windows Life to create and destroy a window.

WindowManagement

To allow manipolation of windows around screen, changing their size,visibility, position, ecc

Page 26: X Windows

How Xlib works

o X Protocolo Bufferingo Resourceso Properties and Atomso Windows Manager

Page 27: X Windows

X Protocol concepts

o over a single bi-directional stream of 8 -bit bytes, bytes in sequence and not duplicated

o operate asynchronously: higher network efficiency

o synchronous operating speed is limited by the round trip delay (5÷50 ms)

o multiples of 4 bytes to simplify implementation on 16-32 bit architectures

Page 28: X Windows

X Protocol

o requesto replayo eventso errors

Page 29: X Windows

o Request : it is generated by Xlib and sent to the server ; a protocol request can carry a wide variety of information ; exceptions are routines that only affect data structures to Xlib and do not affect the server;

o Replay : it is sent from the server to Xlib in response to certain request;

o Event : it is sent from the server to Xlib and contains information about a device action or about a side effect of a previous request;

o Error : it tells the client that a previous request was invalid.

Page 30: X Windows

X/Request

unused

Major OpcodeData

Length FieldData Field

(Minor Opcode)

8 bits 8 bits 16 bits 8 bits 0 ... n

Header Additional Data

Page 31: X Windows

o Major Opcode: it defines the nature of requests [128...255 reserved]

o Length Field: it must equal the minimum length required to contain the request (including the header) expressed in units of 4 bytes

o Data Field: multiple request, contains the minor opcode

m Every request on a given connection is implicitly assigned a sequence number, that is used in Replies, Errors and Events

Page 32: X Windows

Encoding of types (examples)o BYTE: 8-bit valueo INT8/16/32: 8/16/32 bit signed integero CARD8/16/32: 8/16/32 bit unsigned integero GCONTEXT: CARD32o BITMASK: CARD32o WINDOW , DRAWABLE : CARD32o CURSOR: CARD32o ATOM: CARD32o POINT: INT16 + INT16o etc .....

Page 33: X Windows

Clear Area (Request example)# number of

bytesValue Description

1 61 opcode1 BOOL exposures2 4 request length4 WINDOW window2 INT16 x2 INT16 y2 CARD16 width2 CARD16 height

Page 34: X Windows

0070 .. .. 3D 00 04 00 15 00 30 00 04 000080 D0 01 70 03 14 00 .. .. .. .. .. ..

opcode

exposures request length

width

window

XY

height

61

false 4 x (4 bytes)

00300015

464880

204

Page 35: X Windows

Copy Area (Request example)# number of

bytes Value Description

1 62 opcode1 unused2 7 request length4 DRAWABLE src-drawable4 DRAWABLE dst-drawable4 GCONTEXT gc2 INT16 src-x2 INT16 dst-y2 INT16 dst-x2 INT16 src-y2 CARD16 width2 CARD16 height

Page 36: X Windows

0050 .. .. .. .. .. .. .. .. .. .. 3E 00 07 00 15 000060 30 00 15 00 30 00 16 00 30 00 04 00 18 00 04 000070 04 00 70 03 CC 01 .. ..

opcode62

unused

460

request length7

src-drawable

00300015dst-drawable

00300015

dst-Y

4width

880

height

gc

00300016

src-X

4src-Y

24

dst-X

4

Page 37: X Windows

X/Replay

DataLength Field

32 bits

32 bytes .... n bytes

o Every replay contains the least-significant 16 bits of the sequence number of the corresponding request.

o Unused bytes are not guaranteed to be zero

Page 38: X Windows

X/Erroro 32 bytes longo 8-bit error code [64 ..127 for extensions]o major & minor opcode of the failed

requesto and also this values:?ID (for ColorMap, Cursor, Font, Window)?failing Atom for Atoms?failing value for value errors

m Unused bytes are not guaranteed to be zero

Page 39: X Windows

X/Event

o 32 bytes longo 8-bit type code [64...127 for ext.]o every core event also contains the least-

significant 16 bits of the sequence number of the last request issued by client

m Unused bytes are not guaranteed to be zero

Page 40: X Windows

Buffering

o reducing overhead, improve network performanceo most Xlib calls do not require immediate responseo Xlib sends the buffer full of requests when ...?application calls an Xlib routine to wait for an event, but

no matching event is currently available on Xlib's queue?an immediate replay?flush the request buffer manually

Xlib saves up requests instead of sending them to the server immediately, so that the client program can continue running instead of waiting to gain access to the network after every Xlib call.

Page 41: X Windows

Server queue

eventsXlib queue

request

Server queueevents

Server queueevents

server 1server 2

Xlib

Page 42: X Windows

Example of X-Windows packetADDR HEX0000 08 00 2B 39 02 21 AA 00 04 00 92 7D 08 00 45 000010 01 04 8F 02 00 00 3C 06 E2 38 82 C0 02 10 82 C00020 05 29 0F A5 17 70 38 1E 09 B9 00 01 48 71 50 180030 18 00 98 00 00 00 46 00 05 00 15 00 30 00 1A 000040 30 00 46 00 D0 01 0B 00 14 00 38 00 04 00 16 000050 30 00 00 00 01 00 01 00 00 00 3E 00 07 00 15 000060 30 00 15 00 30 00 16 00 30 00 04 00 18 00 04 000070 04 00 70 03 CC 01 3D 00 04 00 15 00 30 00 04 000080 D0 01 70 03 14 00 68 00 01 00 4C 4A 17 00 15 000090 30 00 27 00 30 00 04 00 DF 01 4E 65 77 20 6D 6100A0 69 6C 20 6F 6E 20 6F 6E 64 65 20 50 4F 4D 38 3800B0 42 20 66 72 6F 6D 20 49 4E 25 22 56 41 4E 4E 4F00C0 5A 5A 49 40 4E 49 53 2E 47 41 52 52 2E 49 54 2200D0 20 20 22 44 61 6E 69 65 6C 65 20 56 61 6E 6E 6F00E0 7A 7A 69 22 30 00 3E 00 07 00 15 00 30 00 15 0000F0 30 00 16 00 30 00 04 00 18 00 04 00 04 00 70 030100 CC 01 3D 00 04 00 15 00 30 00 04 00 D0 01 70 030110 14 00

Request 1

Page 43: X Windows

DLC: Destination = Station DEC 390221DLC: Source = Station DECnet 00927DDLC: Ethertype = 0800 (IP)IP: Version = 4, header length = 20 bytesIP: Type of service = 00, Total length= 260 bytesIP: Identification = 36610, Flags = 0xIP: Fragment Offset = 0 bytesIP: Time to live = 60 seconds/hops,Protocol = 6 (TCP)IP: header checksum = E238 (correct)IP: Source Address = [130.192.2.16]IP: Destination Address = [130.192.5.41]TCP: Source port = 4005TCP: Destination port = 6000 (X Windows)TCP: Sequence number = 941492665TCP: Acknowledge number = 84081TCP: Data Offset = 20 bytes, Flags = 18TCP: Window = 6144, Checksum = 9800 (correct)

Page 44: X Windows

XWIN: Request opcode = 70 (Poly Fill Rectangle)XWIN: Drawable = 00300015, Graphics context = 0030001AXWIN: X = 70, Y = 464, Width = 11, Heigth= 20XWIN: Request opcode = 56 (Change GC)XWIN: Graphics context = 00300016XWIN: Value mask = 00010000XWIN: Graphics exposures = 1 (True)XWIN: Request opcode = 62 (Copy Area)XWIN: Source Drawable = 00300016XWIN: Destination drawable = 00300015XWIN: Graphics context = 00300015XWIN: Source X = 4, Y = 24XWIN: Destination X = 4, Y = 4XWIN: Width = 880, Heigth = 460XWIN: Request opcode = 61 (Clear Area)XWIN: Exposure = 0 (False)XWIN: Window = 00300015XWIN: X = 4, Y = 464, Width = 880, Height = 20

Page 45: X Windows

XWIN: Request opcode = 104 (Bell)XWIN: Percent = 0XWIN: Request opcode = 76 (Image Text8)XWIN: Drawable = 00300015, Graphics context = 00300027XWIN: X = 4, Y = 479XWIN: String = New mail on node POL88B from IN...XWIN: Request opcode = 62 (Copy Area)XWIN: Source drawable = 00300015XWIN: Graphics context = 00300016XWIN: Source X = 4, Y = 24XWIN: Destination X = 4, Y 0 4XWIN: Widdth = 880, Height = 460XWIN: request opcode = 61 (clear Area)XWIN: Exposures = 0 (False )XWIN: Window = 00300015XWIN: X = 4, Y = 464, Width = 880, Height = 20

Page 46: X Windows

Resourceso to reduce network traffic, the server

maintains complex abstractions such as windows or fonts for which the client have allocate an integer ID number for each one as a nickname: that is a resources

o the ID is used as argument for the Xlib routines

o client must query the server information about resources

Page 47: X Windows

Properties and Atomso a way to allow clients to communicate each

othero a property is a packet of information

associated with a windowo properties have a string name and a

numerical identifier called atomso an atom is an ID that uniquely identify a

particular propertyo both an atom and a window are needed to

uniquely identified a property

Page 48: X Windows

Basic steps to create a window application

o Connecting to the Server o Checking the Environment o Creating the Window o Displaying the Window o Cleaning up

Page 49: X Windows

Connecting to the Server

o initial connection to the server we use the function XOpenDisplay()

o normally pass this function a NULL parameter to indicate that we wish to use the display specified in the DISPLAY environment variable

o if all goes well it should return a pointer to a Display data structure

o once you have obtained a successful connection you will need to determine which screen you are connected to: DefaultScreen() function.

Page 50: X Windows

Display *display;display = XOpenDisplay ( (char *) NULL);int screen;screen = DefaultScreen(display);

Page 51: X Windows

Checking the Environment o programs must be device independento ask the X server what the characteristics of

the screen areo a set of macros are used:?DisplayWidth (display,screen)?DisplayHeight (display,screen)?DefaultDepth (display,screen)?DisplayWidthMM(display, screen)?DisplayHeightMM(display, screen)

o use with extreme care !!?some video cards don’t know what video they are

connected to

Page 52: X Windows

Creating the Windowo 2 window creation functions:?XCreateSimpleWindow()?XCreateWindow()

o function call will return the window identifier? first parameter is the target display, as obtained from

XOpenDisplay()? second parameter is the parent's id? x and y parameters give the desired pixel location of the top left

corner of this new window (window manager has the option of arbitrarily changing)

?width and height are the size of the window in pixels? depth is the number of bit planes available? class will take the form of a constant, either InputOutput, InputOnly

or CopyFromParent? visual describes an abstract model for graphics hardware capabilities

Page 53: X Windows

#include <X11/Xlib.h>Display *display;Window parent_window;int x, y;unsigned int width, height;unsigned int border_width;int depth;unsigned int class;Visual *visual;unsigned long valuemask;XSetWindowAttributes *attributes;

window = XCreateWindow (display, parent_window, x, y, width,height, border_width, depth, class,visual, valuemask, attributes);

Page 54: X Windows

Displaying the Window

o XMapWindow (display, window)o XMapRaised (display, window)

Page 55: X Windows

Cleaning up

o XDestroyWindow (display, window)o XCloseDisplay (display)

Page 56: X Windows

Xt Intrinsic

o Most common way to write applications:?quite powerful?cooperate well with other X applications?user-interface conventions?C language?object oriented layer

Page 57: X Windows

X architecture again

Page 58: X Windows

Xt concepts

o ready-to-use user interfaceo self contained program units called widgetso widget: it is a reusable, configurable piece of code

that operates independentlyo widget set is a collection of widget that provide

commonly used user-interface (look & feel)o Xtoolkit, Motif, Open Look

Page 59: X Windows

X Windows Programming Bases (Example Unix)

o header files for Xo important X environment variables?DISPLAY=“ ip.name.server : 0.0 ”

o handling inputo compiling your code

Page 60: X Windows

void init_x() {unsigned long black,white;dis=XOpenDisplay((char *)NULL);screen=DefaultScreen(dis);black=BlackPixel(dis,screen);white=WhitePixel(dis, screen);win=XCreateSimpleWindow(

dis,DefaultRootWindow(dis),0,0, 200, 300, 5, white,black);

XSetStandardProperties(dis,win,"Mywindow","HI!",None,NULL,0,NULL);XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);gc=XCreateGC(dis, win, 0,0); XSetBackground(dis,gc,white);

XSetForeground(dis,gc,black);XClearWindow(dis, win);

XMapRaised(dis, win);};

void init_x() {unsigned long black,white;dis=XOpenDisplay((char *)NULL);screen=DefaultScreen(dis);black=BlackPixel(dis,screen);white=WhitePixel(dis, screen);win=XCreateSimpleWindow(

dis,DefaultRootWindow(dis),0,0, 200, 300, 5, white,black);

XSetStandardProperties(dis,win,"Mywindow","HI!",None,NULL,0,NULL);XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);gc=XCreateGC(dis, win, 0,0); XSetBackground(dis,gc,white);

XSetForeground(dis,gc,black);XClearWindow(dis, win);

XMapRaised(dis, win);};

void close_x() {XFreeGC(dis, gc);XDestroyWindow(dis,win);XCloseDisplay(dis); exit(1); }

void close_x() {XFreeGC(dis, gc);XDestroyWindow(dis,win);XCloseDisplay(dis); exit(1); }

#include <X11/Xlib.h>

#include <X11/Xutil.h>

#include <X11/Xos.h>

#include <X11/Xlib.h>

#include <X11/Xutil.h>

#include <X11/Xos.h>

Page 61: X Windows

Handling input

o setting up the input masks o creating an instance of the XEvent o checking for events o handling events.

Page 62: X Windows

XEvent event;KeySym key;char text[255];while(1) {

XNextEvent(dis, &event);if (event.type==Expose && event.xexpose.count==0) {

redraw();}

if (event.type==KeyPress &&XLookupString(&event.xkey,text,255,&key,0)==1) {

if (text[0]=='q') {close_x();}

printf("You pressed the %c key!\n",text[0]);}

if (event.type==ButtonPress) {printf("You pressed a button at (%i,%i)\n",

event.xbutton.x,event.xbutton.y);}

} /* end while */....

XEvent event;KeySym key;char text[255];while(1) {

XNextEvent(dis, &event);if (event.type==Expose && event.xexpose.count==0) {

redraw();}

if (event.type==KeyPress &&XLookupString(&event.xkey,text,255,&key,0)==1) {

if (text[0]=='q') {close_x();}

printf("You pressed the %c key!\n",text[0]);}

if (event.type==ButtonPress) {printf("You pressed a button at (%i,%i)\n",

event.xbutton.x,event.xbutton.y);}

} /* end while */....

Page 63: X Windows

XNextEvent(dis, &event);

event.type ?

KeyPress ButtonPress Expose

....

Page 64: X Windows

Compiling the code

o gcc hi.c -o hi -lX11 o gcc hi.c -o hi -I /usr/include/X11 -L

/usr/X11/lib -lX11 o gcc hi.c -o hi -I /sowhere/else -L

/who/knows/where -lX11

Page 65: X Windows

The Future: Broadwayo Broadway, was the code name for a ground-breaking

initiative evolving the X Window System for creating and accessing a lot of information which in our world today is stored in the form of applications

o interactive applications on the World Wide Web: the resulting technology is the latest release of X X11R6.3 (Open Group).

o any application linked to the Web using X11R6.3 can be located, accessed and executed with the same tools used for accessing static HTML documents today - Web browsers.

Page 66: X Windows

o requires absolutely no changes to application programs

o a central concept in X11R6.3 is the ability to remotely invoke applications

o there are 4 X11R6.3 key components:1) Remote Execution 2) Web Browser Plug-in3) Security4) LBX (Low Bandwidth X), high performance Internet

distributed graphics protocolo combined technology: audio, web, X, video

Page 67: X Windows

Internet Optimized X Protocol

o LBX (Low-Bandwidth X) → X.fasto target → WAN & serial lineo protocol optimization?data compression?caching & short circuiting

Page 68: X Windows

References

o "The definitive Guides to the X Window System" vol. 0, 1, 4 (O'Reilly & Ass.)

o "A brief intro to X11 Programming" http://www.cba.uga.edu/~bhammond/XIntro/

o http://dslep01.gsi.de/~karig/Online/X_intro.html