Week 11 6/2/02 1 X11 Window System 6/2/02 2 Benefits of X n network transparency n clients on one host can use the display of another n is network independent n needs a reliable stream connection n device independence n X clients will work with any X server n is policy-free n provides “ mechanism without policy” 6/2/02 3 1
X is policy-free n the server provides minimal but complete functionality n the user interface is provided by the client n Application Interface: n the application is allocated a window n sends draw request to the server n gets events related to its own windows n Management Interface: n the server does not manage window placements n the manager client does this n changing window manager changes the management interface 6/2/02 4 Drawbacks n is HUGE n is hard to approach n is quiet COMPLICATED 6/2/02 5 Xserver X11 protocol device driver client client screen client keyboard mouse display 6/2/02 6 2
The Server n Runs on a host where the hardware is n Manages Input Devices n pointers: Mouse/tablet n Keyboard n Manages Output Devices n Display Frame Buffer n Process X Protocol requests from clients n send events to clients 6/2/02 7 a Client n runs on any host that has connectivity to the server n is the Application, including the user interface n Menus n Interaction techniques n Drawing n Handling input n interprets the X Protocol n sending requests n receiving events 6/2/02 8 Software Layers Application Widgets Xt Intrinsics Xlib X Protocol 6/2/02 9 3
Software Layers … X Protocol: the actual byte stream that goes 1. between the client & server Xlib: a C library interface to the X Protocol 2. Xt Intrinsics: a C library for using & 3. constructing widgets Widgets: a collection of user interface 4. abstractions widget: a reusable, configurable piece of code. • a collection of widgets is also called a toolkit • 6/2/02 10 Event-Driven Programming n X event n is a data structure sent by the server that describes something that just happened that may be of interest to the application. 6/2/02 11 Terminology screen x y child height window parent window width 6/2/02 12 4
a Window n is a rectangular area of the screen n may contain one or more child windows n a parent window clips all child windows n appears when it’s mapped n disappears when it’s unmapped 6/2/02 13 Window … parent: windows in an application form a tree. 1. a window lies within the coordinate space of its parent • all portions of a window that are beyond its parent’s boundary • are clipped window ID: an integer that identifies the window. 2. position: the location of the upper left corner within the 3. parent’s coordinate system. width, height and border width: measured in pixels 4. coordinate system: each window has its own system. 5. attributes: background/foreground color, border color, 6. cursor … 6/2/02 14 Event n is the means by which the server informs the client of user input n when the user does something - moves the mouse, presses a key, … - the server translates these actions into events, and sends them to the appropriate client(s) n events are only sent to clients which have specifically asked to be informed of that type of event n clients can force events to be sent to other clients n are typically reported relative to a window 6/2/02 15 5
Widget n a user interface abstraction n implements some part of a user interface n menu, scrollbar, text entry, … n each widget has a window n where it displays its information n where it reacts to user actions 6/2/02 16 shell widget text widget composite widget main application window or widget label widget command button widget pop-up shell widget 6/2/02 17 Widgets … n Shell widgets: n is used as the parent of all other application widgets n includes functionality that allows to interact with the window manager n is invisible n popup dialog box: n is a widget that appears temporarily on the screen, until the user provides some input 6/2/02 18 6
Resources n is a named piece of data, usually part of a widget n the user or application can set value of a widget n mainly used to control widgets n the resource manager/resource database n part of the xlib which is responsible to manage/read/provide resource information. 6/2/02 19 Callbacks n an application procedure associated with a widget n it’s called when something happens in a widget n button pressed, scrollbar moved n main communication mechanism between widget and application 6/2/02 20 Widget Classes n a widget set defines classes of widgets n all widgets belong to a widget class n all widgets in a class have: n the same basic appearance n and behave in the same way n are like objects and classes in oop 6/2/02 21 7
Widget Sets/Toolkits n a toolkit is a set of widgets that work together n there are many n Athena n Motif n DCE n KDE n GNOME n … 6/2/02 22 #include <X11/Xlib.h> int main() { Display *dsp = XOpenDisplay(NULL); if(!dsp) { return 1; } int screenNumber = DefaultScreen(dsp); unsigned long white = WhitePixel(dsp,screenNumber); unsigned long black = BlackPixel(dsp,screenNumber); Window win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 50, 50, 200, 200, 0, black, white); XMapWindow(dsp, win); long eventMask = StructureNotifyMask; XSelectInput( dsp, win, eventMask ); XEvent evt; do { XNextEvent(dsp, &evt); } while(evt.type != MapNotify); GC gc = XCreateGC( dsp, win, 0, NULL ); XSetForeground( dsp, gc, black ); XDrawLine(dsp, win, gc, 10, 10,190,190); XDrawLine(dsp, win, gc, 10,190,190, 10); eventMask = ButtonPressMask|ButtonReleaseMask; XSelectInput(dsp, win, eventMask); do { XNextEvent( dsp, &evt ); } while(evt.type != ButtonRelease); XDestroyWindow(dsp, win); XCloseDisplay(dsp); return 0; } 6/2/02 23 defines structures, macros, function prototypes #include <X11/Xlib.h> host:server.screen int main() { Display *dsp = XOpenDisplay(NULL); if(!dsp) { return 1; } int screenNumber = DefaultScreen(dsp); unsigned long white = WhitePixel(dsp, screenNumber); unsigned long black = BlackPixel(dsp, screenNumber); Window win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), x,y coordinates 50, 50, 200, 200, width, height 0, black, width and color of the border white); color of the backgound XMapWindow(dsp, win); makes the window visible 6/2/02 24 8
long eventMask = StructureNotifyMask; XSelectInput(dsp, win, eventMask ); wait till the window XEvent evt; gets mapped do { XNextEvent(dsp, &evt); } while(evt.type != MapNotify); flushes all output buffers 6/2/02 25 • Graphics Context: • information about appearance • color, linestyle, … GC gc = XCreateGC(dsp, win, 0, NULL); XSetForeground(dsp, gc, black); XDrawLine(dsp, win, gc, 10, 10, 190,190); XDrawLine(dsp, win, gc, 10,190, 190, 10); 6/2/02 26 eventMask = ButtonPressMask|ButtonReleaseMask; XSelectInput(dsp, win, eventMask); do { XNextEvent(dsp, &evt); } while(evt.type != ButtonRelease); XDestroyWindow(dsp, win); XCloseDisplay(dsp); return 0; } 6/2/02 27 9
#include <X11/Xlib.h> int main() { Display *dsp = XOpenDisplay(NULL); if(!dsp) { return 1; } int screenNumber = DefaultScreen(dsp); unsigned long white = WhitePixel(dsp,screenNumber); unsigned long black = BlackPixel(dsp,screenNumber); Window win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 50, 50, 200, 200, 0, black, white); XMapWindow(dsp, win); long eventMask = StructureNotifyMask; XSelectInput( dsp, win, eventMask ); XEvent evt; do { XNextEvent(dsp, &evt); } while(evt.type != MapNotify); GC gc = XCreateGC( dsp, win, 0, NULL ); XSetForeground( dsp, gc, black ); XDrawLine(dsp, win, gc, 10, 10,190,190); XDrawLine(dsp, win, gc, 10,190,190, 10); eventMask = ButtonPressMask|ButtonReleaseMask; XSelectInput(dsp, win, eventMask); do { XNextEvent( dsp, &evt ); } while(evt.type != ButtonRelease); XDestroyWindow(dsp, win); XCloseDisplay(dsp); return 0; } 6/2/02 28 tcl/tk wm title . "two lines" canvas .c -width 200 -height 200 pack .c .c create line 10 10 190 190 .c create line 10 190 190 10 bind .c <Button> exit 6/2/02 29 10
Recommend
More recommend