Hi guys from xorgand cairomailling list, I sent this mail to both of you because I don't know who can explains to me something. I wrote a small code, it's just a window where you should see the date and the time.
If you hit "space" the window moves, if you hit "q" the wm will exit(ok ok it's a bit fulsome to call that shit a wm..). So now my problem, I need to redraw the clock, that actualy doesn't working yet, since I 'm bit lost with "when" and "how" to redraw, if it's cairoor xlibwho has (or both) to redraw etc. Moreover, there is something I don't understand more, when I hit "space", then the window moves and there is a redraw (you can see it when the minutes change). I added as attachments, main.cand Makefileso you just have to type "make" (using pkg-configfor cairo). I want to notice, I just see if I go to another console or X running and then I back, I see a visual glitch at the background of the window. -Nicoo
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<X11/Xlib.h>
#include<X11/cursorfont.h>
#include<X11/keysym.h>
#include<cairo.h>
#include<cairo-xlib.h>
#define DPYW DisplayWidth(dpy,scr)
#define DPYH DisplayHeight(dpy,scr)
// hit "space" -> move the clock
// hit "q" -> exit the wm
typedef struct clockStruc{
cairo_t *c;
cairo_surface_t *s;
Window w;
}CLOCKSTRUC;
int keyRelease(XEvent *_e, Display *_dpy, Window *_w){
XWindowAttributes wa;
XGetWindowAttributes(_dpy,*_w,&wa);
if(XLookupKeysym(&_e->xkey,0) == XK_q) return 1;
if(XLookupKeysym(&_e->xkey,0) == XK_space) XMoveWindow(_dpy, *_w, wa.x+3,wa.y+1);
return 0;
}
void updateClock(CLOCKSTRUC *_mc ){
time_t curtime;
char buffer[16];
struct tm *loctime;
time(&curtime);
loctime = localtime(&curtime);
cairo_set_source_rgba(_mc->c,1,1,1,1);
cairo_select_font_face(_mc->c,"Sans",CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(_mc->c,10);
cairo_move_to(_mc->c, 0, 10);
strftime(buffer, 16, "%a, %d %b", loctime);
cairo_show_text(_mc->c, buffer);
cairo_set_font_size(_mc->c,24);
cairo_move_to(_mc->c, 0, 34);
strftime(buffer, 16, "%H:%M", loctime);
cairo_show_text(_mc->c, buffer);
}
void cairoClock(CLOCKSTRUC *_mc, Display *_dpy, int _scr, Window _prnt, int _x, int _y, int _sizex, int _sizey){ // attention ne detruit pas la surface à la fermeture !!!!
XSetWindowAttributes setwbtnattr;
unsigned long valuemask;
_mc->w=XCreateSimpleWindow(_dpy,_prnt,_x,_y,_sizex,_sizey,0,0,0);
valuemask = CWBackPixmap; //p94 vol. one
setwbtnattr.background_pixmap = None;
XChangeWindowAttributes(_dpy,_mc->w,valuemask, &setwbtnattr);
XStoreName(_dpy, _mc->w, "clock");
XSelectInput(_dpy, _mc->w, ExposureMask|KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask);
XMapWindow(_dpy, _mc->w);
_mc->s=cairo_xlib_surface_create(_dpy, _mc->w, DefaultVisual(_dpy, 0), _sizex, _sizey); // nicoo : demander avis
_mc->c=cairo_create(_mc->s);
cairo_rectangle(_mc->c,0,0,_sizex,_sizey);
cairo_set_source_rgba(_mc->c, 0.5, 0., 0., 1.); // nicoo : rouge, temporaire....
cairo_fill(_mc->c);
}
int main(int argc, char *argv[]){
Display *dpy;
int x=100, y=200;
Window rootwin, wbg, testwin;
int scr, stop=0;
XEvent e;
Cursor theCursor;
CLOCKSTRUC myClock;
dpy = XOpenDisplay(NULL);
scr=DefaultScreen(dpy);
theCursor = XCreateFontCursor(dpy, XC_left_ptr);
rootwin=RootWindow(dpy, scr);
XSelectInput(dpy, rootwin, ExposureMask|KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionHintMask);
XDefineCursor(dpy,rootwin,theCursor);
// creation of the background
wbg=XCreateSimpleWindow(dpy,rootwin,0,0,DPYW,DPYH,0,BlackPixel(dpy, scr), BlackPixel(dpy, scr));
XSelectInput(dpy, wbg, ExposureMask);
XSetWindowBackground(dpy,wbg,128);
XMapWindow(dpy, wbg);
// creation of the clock
cairoClock(&myClock, dpy, scr, wbg,x,y,200,200);
while(stop==0) {
updateClock(&myClock);
XNextEvent(dpy, &e);
switch(e.type){
case Expose : XFlush(dpy); break;
case KeyRelease : stop = keyRelease(&e,dpy,&(myClock.w)); break;
default : break;
}
}
// DEATHSTRUCTION
cairo_surface_destroy(myClock.s);
cairo_destroy(myClock.c);
XFreeCursor(dpy,theCursor);
XCloseDisplay(dpy);
return 0;
}
Makefile
Description: Binary data
_______________________________________________ [email protected]: X.Org support Archives: http://lists.freedesktop.org/archives/xorg Info: http://lists.x.org/mailman/listinfo/xorg Your subscription address: [email protected]
