12 #include <X11/XCB/xcb.h>
13 #include <X11/XCB/shm.h>
14 #include <X11/XCB/xcb_aux.h>
15 #include <X11/XCB/xcb_image.h>
17 #include "lissajoux.h"
28 XCBShmSegmentInfo shminfo;
35 gettimeofday(&timev, NULL);
37 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
41 draw_lissajoux (Data *datap)
44 double a1, a2, p1, p2;
50 i = XCBImageSHMGet (datap->conn, datap->draw,
51 datap->image, shminfo,
58 datap->image = XCBImageGet (datap->conn, datap->draw,
60 AllPlanes, datap->format);
68 p1 = 4.0*t_previous*pi*0.05;
72 for (i = 0 ; i < nbr ; i++)
74 x = cos (a1*i*period/nbr + p1);
75 y = sin (a2*i*period/nbr + p2);
76 XCBImagePutPixel (datap->image,
77 (int)((double)(W_W-5)*(x+1)/2.0),
78 (int)((double)(W_H-5)*(y+1)/2.0), 65535);
84 for (i = 0 ; i < nbr ; i++)
86 x = cos (a1*i*period/nbr + p1);
87 y = sin (a2*i*period/nbr + p2);
88 XCBImagePutPixel (datap->image,
89 (int)((double)(W_W-5)*(x+1)/2.0),
90 (int)((double)(W_H-5)*(y+1)/2.0), 0);
94 XCBImageSHMPut (datap->conn, datap->draw, datap->gc,
95 datap->image, shminfo,
96 0, 0, 0, 0, W_W, W_H, 0);
99 XCBImagePut (datap->conn, datap->draw, datap->gc, datap->image,
100 0, 0, 0, 0, W_W, W_H);
101 XCBImageDestroy (datap->image);
109 t = get_time () - time_start;
113 draw_lissajoux (datap);
117 printf("FRAME COUNT..: %i frames\n", loop_count);
118 printf("TIME.........: %3.3f seconds\n", t);
119 printf("AVERAGE FPS..: %3.3f fps\n", (double)loop_count / t);
120 XCBDisconnect (datap->conn);
125 /* Return 0 if shm is not availaible, 1 otherwise */
127 shm_test (Data *datap)
129 XCBShmQueryVersionRep *rep;
131 rep = XCBShmQueryVersionReply (datap->conn,
132 XCBShmQueryVersion (datap->conn),
139 if (rep->shared_pixmaps &&
140 (rep->major_version > 1 || rep->minor_version > 0))
141 format = rep->pixmap_format;
144 datap->image = XCBImageSHMCreate (datap->conn, datap->depth,
145 format, NULL, W_W, W_H);
146 assert(datap->image);
148 shminfo.shmid = shmget (IPC_PRIVATE,
149 datap->image->bytes_per_line*datap->image->height,
151 assert(shminfo.shmid != -1);
152 shminfo.shmaddr = shmat (shminfo.shmid, 0, 0);
153 assert(shminfo.shmaddr);
154 datap->image->data = shminfo.shmaddr;
156 shminfo.shmseg = XCBShmSEGNew (datap->conn);
157 XCBShmAttach (datap->conn, shminfo.shmseg,
159 shmctl_status = shmctl(shminfo.shmid, IPC_RMID, 0);
160 assert(shmctl_status != -1);
165 printf ("Use of shm.\n");
170 printf ("Can't use shm. Use standard functions.\n");
171 shmdt (shminfo.shmaddr);
172 shmctl (shminfo.shmid, IPC_RMID, 0);
178 main (int argc, char *argv[])
188 XCBRECTANGLE rect_coord = { 0, 0, W_W, W_H};
198 printf ("Usage: lissajoux try_shm\n");
199 printf (" try_shm == 0: shm not used\n");
200 printf (" try_shm != 0: shm is used (if availaible)\n");
204 try_shm = atoi (argv[1]);
208 data.conn = XCBConnect (0, &screen_num);
209 screen = XCBAuxGetScreen(data.conn, screen_num);
210 data.depth = XCBAuxGetDepth (data.conn, screen);
212 win.window = screen->root;
214 data.gc = XCBGCONTEXTNew (data.conn);
215 mask = GCForeground | GCGraphicsExposures;
216 valgc[0] = screen->black_pixel;
217 valgc[1] = 0; /* no graphics exposures */
218 XCBCreateGC (data.conn, data.gc, win, mask, valgc);
220 bgcolor = XCBGCONTEXTNew (data.conn);
221 mask = GCForeground | GCGraphicsExposures;
222 valgc[0] = screen->white_pixel;
223 valgc[1] = 0; /* no graphics exposures */
224 XCBCreateGC (data.conn, bgcolor, win, mask, valgc);
226 data.draw.window = XCBWINDOWNew (data.conn);
227 mask = XCBCWBackPixel | XCBCWEventMask | XCBCWDontPropagate;
228 valwin[0] = screen->white_pixel;
229 valwin[1] = KeyPressMask | ButtonReleaseMask | ExposureMask;
230 valwin[2] = ButtonPressMask;
231 XCBCreateWindow (data.conn, 0,
239 XCBMapWindow (data.conn, data.draw.window);
241 rect.pixmap = XCBPIXMAPNew (data.conn);
242 XCBCreatePixmap (data.conn, data.depth,
243 rect.pixmap, data.draw,
245 XCBPolyFillRectangle(data.conn, rect, bgcolor, 1, &rect_coord);
247 data.format = ZPixmap;
248 XCBSync (data.conn, 0);
253 time_start = get_time ();
257 e = XCBPollForEvent(data.conn, NULL);
260 switch (e->response_type)
263 XCBCopyArea(data.conn, rect, data.draw, bgcolor,
264 0, 0, 0, 0, W_W, W_H);
265 XCBSync (data.conn, 0);
271 XCBFlush (data.conn);