#include #include #include "fenX11.h" XImage *ximage; static Display *disp; static int screen; static Window root; static unsigned long wpx, bpx; #define NB_WINDOWS_MAX 15 static Xwin current_windows[NB_WINDOWS_MAX]; static int nb_windows = 0; static end_flag = 0; // Initialisation void initWindows() { unsigned long wpx, bpx; disp=XOpenDisplay(NULL); screen=DefaultScreen(disp); root=DefaultRootWindow(disp); wpx=WhitePixel(disp, screen); bpx=BlackPixel(disp, screen); } // Ouverture et enregistrement d'une fenêtre Xwin openWindow(int px, int py, int image_width, int image_height) { Xwin xwin; char *image_data; xwin = (Xwin)malloc(sizeof(struct Xwin_t)); if (!xwin) return NULL; // Callbacks xwin->expose = (void (*)())NULL; xwin->buttonpress = (void (*)(int,int,int))NULL; xwin->keypress = (void (*)(long))NULL; // Création de la fenêtre xwin->win=XCreateSimpleWindow(disp,root,px,py,image_width,image_height,6,bpx,wpx); // autres masques dispo (X.h) : EnterWindowMask, LeaveWindowMask,PointerMotionMask XSelectInput(disp,xwin->win,ExposureMask|ButtonPressMask|KeyPressMask); XMapWindow(disp,xwin->win); xwin->gcv.foreground=bpx; xwin->gcv.background=wpx; xwin->gcv.line_width=2; xwin->gcv.function=GXcopy; xwin->gcmask=GCForeground|GCBackground|GCLineWidth|GCFunction; xwin->gcontext=XCreateGC(disp,xwin->win,xwin->gcmask,&(xwin->gcv)); xwin->cmap=XDefaultColormap(disp,screen); /* if (!(image_data = (char*) malloc (sizeof(unsigned int) * image_width * image_height))) { perror ("calloc image_data:"); exit (1); } ximage = XCreateImage(disp, DefaultVisual(disp, screen), DisplayPlanes (disp, screen), ZPixmap, 0, image_data, image_width, image_height, 8 * sizeof(unsigned int), 0); ximage->byte_order = LSBFirst; */ current_windows[nb_windows] = xwin; nb_windows++; return xwin; } // Positionnement d'une couleur void setColor(Xwin xwin, unsigned char r, unsigned char v, unsigned char b) { XColor col; col.red=(unsigned short)255*r; col.green=(unsigned short)255*v; col.blue=(unsigned short)255*b; col.flags=DoRed|DoGreen|DoBlue; XAllocColor(disp,xwin->cmap,&col); xwin->gcv.foreground=col.pixel; xwin->gcmask=GCForeground; XChangeGC(disp,xwin->gcontext,xwin->gcmask,&(xwin->gcv)); } // affichage d'un point void drawPoint(Xwin xwin, int x, int y) { XDrawPoint(disp,xwin->win,xwin->gcontext,x,y); } // affichage d'une droite void drawLine(Xwin xwin,int x1, int y1, int x2, int y2) { XDrawLine(disp,xwin->win,xwin->gcontext,x1,y1,x2,y2); } // affichage d'un rectangle void drawRectangle(Xwin xwin, int x1, int y1, int l, int h) { XFillRectangle(disp,xwin->win,xwin->gcontext,x1,y1,l,h); } void drawString(Xwin xwin, int posx, int posy, char *str) { XDrawString(disp, xwin->win,xwin->gcontext, posx, posy, str, strlen(str)); } /* void placer_point(int x, int y, int c) { XPutPixel(ximage, x, y, c); } void dessiner_image(int image_width, int image_height) { XPutImage (disp, win, DefaultGC (disp, screen), ximage, 0, 0, 0, 0, image_width, image_height); } */ void clearWindow(Xwin xwin) { XClearWindow(disp,xwin->win); } void setExpose(Xwin xwin, void (*expose)()) { xwin->expose = expose; } void setButtonPress(Xwin xwin, void (*buttonpress)(int, int, int)) { xwin->buttonpress = buttonpress; } void setKeyPress(Xwin xwin, void (*keypress)(long)) { xwin->keypress = keypress; } void quitMainLoop() { end_flag = 1; } void mainLoop() { int i; Xwin xwin; XEvent ev; while(!end_flag) { XNextEvent(disp,&ev); // recherche de la fenêtre concernée par l'évènement for(i=0; iwin != ev.xany.window; i++); if (i == nb_windows) { continue; } xwin=current_windows[i]; // KeyRelease, ButtonRelease, MotionNotify, EnterNotify, LeaveNotify switch(ev.type) { case Expose: if (xwin->expose) xwin->expose(); break; case ButtonPress: { int x,y; x=ev.xbutton.x; y=ev.xbutton.y; // if (ev.xbutton.button == Button1) if (xwin->buttonpress) xwin->buttonpress(ev.xbutton.button, x,y); break; } case KeyPress: // if (xwin->keypress) xwin->keypress(XLookupKeysym(&(ev.xkey), ev.xkey.state-16)); if (xwin->keypress) xwin->keypress(XKeycodeToKeysym(disp, ev.xkey.keycode,ev.xkey.state)); break; default: printf("Evenement inconnu %d\n", ev.type); } } }