Salut les C,
Je débute avec la Xlib et je voulait créer une animation ou plutôt un chronomètre a titre de code d'exemple expliquant le problème qui est que l'appel a la fonction de gestion d'événement
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
XEvent event ;
XNextEvent(display,&event) ;
est bloquante ce qui est plutôt gênant pour incrémenter le compteur toutes les secondes.
Dans le code suivant il faut cliquer avec la souris sur la fenêtre pour l'incrémenter:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
#include <stdio.h>
#include <string.h>
 
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
 
#include <tgmath.h>
 
#define MAXPX 360
 
#ifndef M_PI
  M_PI=3.1415
#endif  
 
struct pixels {
  float x ;
  float y ;
} ;
 
unsigned int pixel_idx=0 ;
 
 
struct pixels get_coords(int position,int scale,float radius,int center_x, int center_y) {
 
  struct pixels res_px ;
  int offset=-90 ;
  float degrees=360/scale*position+offset ;
  float rad=degrees/180.0*M_PI ;
  float xpos=cos(rad) * radius + center_x ;
  float ypos=sin(rad) * radius + center_y ;
  res_px.x=xpos ;
  res_px.y=ypos ;
  return res_px ;
 
}
 
 
int main() {
 
 Display *display ;
 Window root_win, win ;
 GC clock_pointer_gc,clock_circle_gc ;
 XGCValues clock_circle_values, clock_pointer_values ;
 int screen_num,width=450,height=450 ;
 unsigned long background, clock_circle ;
 
 display=XOpenDisplay(NULL) ;           //set root display
 
 root_win=RootWindow(display,0) ;      // set root window
 screen_num=DefaultScreen(display) ;   // get the root screen number
 
 /* start of settings for color getting */ 
 Colormap cmap ;
 XColor xc, xc1 ;
 cmap=DefaultColormap(display,screen_num) ;
 
 XAllocNamedColor(display,cmap,"Red",&xc,&xc1) ; // getting the Red color
 background=xc.pixel ; //color for background
 
 XAllocNamedColor(display,cmap,"White",&xc,&xc1) ; // getting the white color
 clock_circle=xc.pixel ; // color for clock circle
 
 /* settings for the clock pointer */
 clock_pointer_values.foreground=BlackPixel(display,screen_num) ;
 clock_pointer_values.line_width=10 ;
 clock_pointer_values.line_style=LineSolid ;
 clock_pointer_values.cap_style=CapRound ;    /* CapNotLast, CapButt,CapRound, CapProjecting */
 
 /* settings for the clock circle */
 clock_circle_values.foreground=clock_circle ;
 clock_circle_values.line_width=1 ;
 clock_circle_values.line_style=LineSolid ;
 clock_circle_values.cap_style=CapRound ;     /* CapNotLast, CapButt,CapRound, CapProjecting */
 clock_circle_values.join_style= JoinRound ;  /* JoinMiter, JoinRound, JoinBevel */
 
 
 win=XCreateSimpleWindow(display,root_win,
			 0,0,              //x,y
			 width,height,    //width height
			 0,0,        // borderwidth && bordercolor
			 background      // background color
			) ; 
 
 
 clock_pointer_gc=XCreateGC(display,win,GCForeground | GCLineWidth | GCLineStyle| GCCapStyle | GCJoinStyle,&clock_circle_values) ;
 clock_circle_gc=XCreateGC(display,win,GCForeground| GCLineWidth | GCLineStyle| GCCapStyle ,&clock_pointer_values) ;
 XSelectInput(display,win,ButtonPressMask  | KeyPressMask | ExposureMask | StructureNotifyMask ) ;
 
 XMapWindow(display,win) ; // start the display
 
 struct pixels tmp_px1 ;
 struct pixels tmp_px2 ;
 float center_x=450/2 ;
 float center_y=450/2 ;
 XPoint clock_circle_px[360] ;
 int clock_pointer_pos=0 ;
 for (;;) {
 
 
   XEvent event ;
   XNextEvent(display,&event) ;
   switch(event.type) {
     case Expose :
       ;
     case ConfigureNotify :
       if (event.xconfigure.width != 450 || event.xconfigure.height != 450) {
	 width=event.xconfigure.width   ;
	 height=event.xconfigure.height ;
	 XClearWindow(display,event.xany.window) ;
	 printf("size changed to width: %i ancd height: %i\n",width,height) ;
       }
       break ;
 
     case ButtonPress :  
 
 
         XClearWindow(display,event.xany.window) ;  
 
         int c ;
	 for ( c=0 ; c < 360 ; c++) {
	   tmp_px1=get_coords(c,360,200,450/2,450/2) ;
	   clock_circle_px[c].x=tmp_px1.x ;
	   clock_circle_px[c].y=tmp_px1.y ;
	 }
 
	 XFillPolygon(display, win, clock_pointer_gc, clock_circle_px, 360, 0, 0) ;
 
	 tmp_px2=get_coords(clock_pointer_pos,60,150,450/2,450/2) ;
 
 
	 clock_pointer_pos++ ;
	 XDrawLine(display, win, clock_circle_gc,center_x,center_y,tmp_px2.x,tmp_px2.y) ;
 
 
	 break ;
     case KeyPress : 
         printf("keycode %i == %c\n",(int) event.xkey.keycode,event.xkey.keycode) ;
         XCloseDisplay(display) ;
         return 0 ;
 
       }
  }
}
Et implémenter la boucle principale de cette façon n'est pas une solution car l'aiguille apparaît par flash et aléatoirement sur le cadran au lieu de chaque seconde:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
 for (;; ) {
   XClearWindow(display,event.xany.window) ;  
 
  int c ;
  for ( c=0 ; c < 360 ; c++) {
    tmp_px1=get_coords(c,360,200,450/2,450/2) ;
    clock_circle_px[c].x=tmp_px1.x ;
    clock_circle_px[c].y=tmp_px1.y ;
  }
 
  XFillPolygon(display, win, clock_pointer_gc, clock_circle_px, 360, 0, 0) ;
 
  tmp_px2=get_coords(clock_pointer_pos,60,150,450/2,450/2) ;
 
 
  clock_pointer_pos++ ;
  XDrawLine(display, win, clock_circle_gc,center_x,center_y,tmp_px2.x,tmp_px2.y) ;
  sleep(1)  ;     
}
Si vous savez comment contrôler la boucle principale de manière a pouvoir créer une animation c.a.d pourvoir la faire tourner (sans appuyer sur la souris pour afficher l'image suivante) et en pouvant gérer le timing ça serai sympa de poster.

Merci pour vos réponses éclairées.

PS: j'ai chercher dans la doc sans succès mais celle-ci est trop vaste (j'ai wget la doc depuis internet)

PS1: Est-il vrai que Xlib est a la base de XWindow et de GTK ?