X-protokolla on asynkroninen, jolloin kaikkea ei välttämättä piirretä heti kun funktiota kutsutaan. XSync() odottaa kunnes xserver on toteuttanut kaikki operaatiot.
Yhteys palvelimelle avataan funktiolla
Display *XOpenDisplay(char *display_name); Display_name sisältää palvelimen osoitteen ja avattavan ruudun (screen) numeron. Se voi kuitenkin olla NULL, jolloin käytetään DISPLAY-ympäristömuuttujaa.Ikkunan luominen
Ikkuna voidaan luoda XCreateWindow- tai XCreateSimpleWindow-funktiolla.Window XCreateWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, int depth, unsigned int class, Visual *visual, unsigned long valuemask, XSetWindowAttributes *attributes);Esimerkissä käytetään XCreateSimpleWindow-funktiota. Funktiolle kerrotaan siis näyttö, isäntäikkuna, x- sekä y-koordinaatit, leveys, korkeus, reunan väri, sekä reunan ja taustan väri.
Window XCreateSimpleWindow(Display *display, Window parent, int x, int y, unsigned int width, unsigned int height, unsigned int border_width, unsigned long border, unsigned long background);
Isäntäikkunan saa kutsulla
RootWindow(display, DefaultScreen(display)).X-protokolla sisältää mahdollisuuden siihen, että asiakas voi pyytää palvelinta ilmoittamaan tietyistä tapahtumista, kuten näppäimenpainalluksista tai hiiren liikkeistä. Tämä tapahtuu funktiolla XSelectInput. Seuraava koodinpätkä pyytää tiedot window-ikkunalle tapahtuvista hiirenpainalluksista sekä tiedon siitä kun ikkuna täytyy piirtää uudelleen.
XSelectInput(display, window, ButtonPressMask | ExposureMask);Tämän jälkeen mapataan ikkuna näytölle (näkyväksi).
XMapWindow(display, window);Tapahtumien käsittely
Palvelin lähettää asiakkaalle asiakkaan haluamat tapahtumat ja ne siirtyvät asiakkaan tapahtumajonoon.Tapahtumia voi tapahtumajonosta tarkastella seuraavilla funktioilla.
XNextEvent palauttaa ensimmäisen tapahtuman tapahtumajonosta ja poistaa sen jonosta. Jos jono on tyhjä, funktio odottaa tapahtumia.
int XNextEvent(Display *display, XEvent *event_return);
int XPeekEvent(Display *display, XEvent *event_return);
int XWindowEvent(Display *display, Window w, long event_mask, XEvent *event_return);
XPeekEvent palauttaa tapahtuman poistamatta sitä jonosta. XWindowEventillä voi puolestaan etsiä tapahtumia ikkunakohtaisesti.
Kaikki tapahtumat sisältävät tiedon niiden näytöstä, lähdeikkunasta ja lähdeajasta (koska kutsut tapahtuvat asynkronisesti niin niiden toteuttamisessa voi olla viivettä). Lisäksi tapahtumat sisältävät tapahtumakohtaisia tietoja, kuten hiiren liikkuessa hiiren koordinaatit.
Grafiikan piirtäminen
Jotta ikkunaan voisi piirtää, täytyy luoda piirtokonteksti (graphics context). Piirtokontekstille voit tämän jälkeen piirtää erilaisilla funktioilla. Tässä muutama ja lisää löytyy manuaalista:- XDrawLine
- XDrawPoint
- XDrawString
- XFillPolygon
GC gc = XCreateGC(display, window, 0, 0);
const char *hello = "Hello world!";
XDrawString(display, window, gc, 30, 50, hello, std::strlen(hello));
XFreeGC(display, gc);
Esimerkkiohjelma
#include <X11/Xlib.h>
#include <cstring>
#include <iostream>
int main(int argc, char **argv)
{
Display *display = XOpenDisplay(NULL);;
if(display == NULL) {
std::cerr << "Can't connect to the xserver.\n";
return 1;
}
int screen_num = DefaultScreen(display);
Window window = XCreateSimpleWindow(display,
RootWindow(display, screen_num),
200, 200, 200, 200, 10,
BlackPixel(display, screen_num),
WhitePixel(display, screen_num));
XSelectInput(display, window,
ButtonPressMask | ExposureMask);
XMapWindow(display, window);
GC gc = XCreateGC(display, window, 0, 0);
bool running = true;
while(running) {
XEvent event;
XNextEvent(display, &event);
switch(event.type) {
case Expose:
{
const char *hello = "Hello world!";
XDrawString(display, window, gc, 30, 50,
hello, std::strlen(hello));
break;
}
case ButtonPress:
running = false;
default:
break;
}
}
XUnmapWindow(display, window);
XFreeGC(display, gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
g++ -g xhello.cc -o xhello -lX11./xhello

Ilman ikkunamanageria ikkuna voisi näyttää tältä. Ikkunalla ei ole otsikkopalkkia eikä sitä voi liikuttaa. (Voit tietenkin koodata ohjelmaan otsikkopalkin ja napata hiiritapahtumat ja kutsua XMoveWindow-funktiota)
Ohjelman voi sammuttaa napsauttamalla sitä hiirellä.

1 kommentti:
How can i carry away windows xp from my laptop and reinstall windows Me -the laptops native software?
I have recently bought a in use accustomed to laptop that is old. The himself I had bought it from had installed windows xp on it, regular though it from the word go came with windows Me. I want to oust the windows xp because it runs slows on the laptop because it takes up more thought than the windows Me would. Also I wish to transfer windows xp because it is an wrongful copy. So when I tried to hop to it updates on it, windows would not initiate updates because the windows xp is not genuine. [URL=http://ekeeyxe.makesgirlscrazy.com]anderson ruffin abbott[/URL]
----------------------------------------------------------------------
Answers :
It's better to take one's leave of [URL=http://dwzagoj.makesboyscrazy.com/federal-tax-return-blue-ink.html]federal tax return blue ink[/URL] Windows XP and impartial upgrade your laptop. It's much better. [URL=http://gxgoowf.makesgirlscrazy.com/echo-harmonica-tablature.html]echo harmonica tablature[/URL] Besides, Windows XP is way [URL=http://ienfauq.makesboyscrazy.com/fast-food-trays-cambro.html]fast food trays cambro[/URL] heartier then Windows Me. Windows Me is out and divers programs that can run with XP, can't [URL=http://tneqnyw.makesboyscrazy.com/pumpkin-whoopie-pies.html]pumpkin whoopie pies[/URL] path with Me.
------------------------------
all you possess to do is brochure the windows me disk into the cd drive. then reboot your laptop, when the coal-black [URL=http://dwzagoj.makesboyscrazy.com/federal-building-cafeteria-honolulu-hi.html]federal building cafeteria honolulu hi[/URL] sift with all the info comes up and when it asks u to boot from cd [URL=http://kqpzlzz.makesgirlscrazy.com/leiner-health-products.html]leiner health products[/URL] belt any indication when it tells you to then inaugurate from there !!! I RECOMEND SINCE ITS AN ILLEAGLE IMITATION TO WIPE [URL=http://kqpzlzz.makesgirlscrazy.com/leia-metal-bikini.html]leia metal bikini[/URL] ELSEWHERE THE [URL=http://kladeac.makesboyscrazy.com/cdu-eisleben-cdu-kriminell-cdu.html]cdu eisleben cdu kriminell cdu[/URL] ENTIRE MAGISTERIAL PUSH WHEN IT ASKS YOU WHICH STIFF [URL=http://lelrzag.makesgirlscrazy.com/leader-training-program-bctp.html]leader training program bctp[/URL] DRIVE TO INSTALL IT ON. THEN ADD ALL THE UNUSED ARRAY ON THE INSINCERE [URL=http://kladeac.makesboyscrazy.com/celestron-iso-70-image-spotter.html]celestron iso 70 image spotter[/URL] URGENTLY CONSTRAIN ONTO A UP TO DATE ORDER LOCATION, IT WILL LOOK LIKE C:/ Raw or something like that
Lähetä kommentti