summaryrefslogtreecommitdiff
path: root/xcb-demo/tests/lissajoux.c
blob: 1bb63500a90142ef848921a32556e540a6f6006c (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <assert.h>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>

#include <sys/ipc.h>
#include <sys/shm.h>

#include <X11/Xlib.h>
#include <X11/XCB/xcb.h>
#include <X11/XCB/shm.h>
#include <X11/XCB/xcb_aux.h>
#include <X11/XCB/xcb_image.h>

#include "lissajoux.h"

#define W_W 100
#define W_H 100

double time_start;
int    loop_count;
double t_previous;
double t;
int    do_shm = 0;

XCBShmSegmentInfo shminfo;

double
get_time(void)
{
  struct timeval timev;
  
  gettimeofday(&timev, NULL);

  return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
}

void
draw_lissajoux (Data data)
{
  int       i, nbr;
  double    a1, a2, p1, p2;
  double    pi, period;
  double    x, y;
  
  if (do_shm)
    { 
      i = XCBImageSHMGet (data.conn, data.draw,
			  data.image, shminfo,
			  0, 0,
			  AllPlanes);
      assert(i);
    }
  else
    {
      data.image = XCBImageGet (data.conn, data.draw,
				0, 0, W_W, W_H,
				AllPlanes, data.format);
      assert(data.image);
    }
  
  pi = 3.1415926535897;
  period = 2.0 * pi;
  a1 = 2.0;
  a2 = 3.0;
  p1 = 4.0*t_previous*pi*0.05;
  p2 = 0.0;

  nbr = 1000;
  for (i = 0 ; i < nbr ; i++)
      {
	x = cos (a1*i*period/nbr + p1);
	y = sin (a2*i*period/nbr + p2);
	XCBImagePutPixel (data.image,
			  (int)((double)(W_W-5)*(x+1)/2.0),
			  (int)((double)(W_H-5)*(y+1)/2.0), 65535);
      }

  p1 = 4.0*t*pi*0.05;
  p2 = 0.0;

  for (i = 0 ; i < nbr ; i++)
      {
	x = cos (a1*i*period/nbr + p1);
	y = sin (a2*i*period/nbr + p2);
	XCBImagePutPixel (data.image,
			  (int)((double)(W_W-5)*(x+1)/2.0),
			  (int)((double)(W_H-5)*(y+1)/2.0), 0);
      }

  if (do_shm)
    XCBImageSHMPut (data.conn, data.draw, data.gc,
		    data.image, shminfo,
		    0, 0, 0, 0, W_W, W_H, 0);
  else
    XCBImagePut (data.conn, data.draw, data.gc, data.image,
		 0, 0, 0, 0, W_W, W_H);
}

void
step (Data data)
{
  loop_count++;
  t = get_time () - time_start;

  if (t <= 20.0)
    {
      draw_lissajoux (data);
    }
  else
    {
      printf("FRAME COUNT..: %i frames\n", loop_count);
      printf("TIME.........: %3.3f seconds\n", t);
      printf("AVERAGE FPS..: %3.3f fps\n", (double)loop_count / t);
      exit(0);
    }
}

/* Return 0 if shm is not availaible, 1 otherwise */
void
shm_test (Data data)
{
  XCBShmQueryVersionRep *rep;

  rep = XCBShmQueryVersionReply (data.conn,
				 XCBShmQueryVersion (data.conn),
				 NULL);
  if (rep)
    {
      CARD8 format;
      
      if (rep->shared_pixmaps && 
	  (rep->major_version > 1 || rep->minor_version > 0))
	format = rep->pixmap_format;
      else
	format = 0;
      data.image = XCBImageSHMCreate (data.conn, data.depth,
				      format, NULL, W_W, W_H);
      assert(data.image);

      shminfo.shmid = shmget (IPC_PRIVATE,
			      data.image->bytes_per_line*data.image->height,
			      IPC_CREAT | 0777);
      assert(shminfo.shmid != -1);
      shminfo.shmaddr = shmat (shminfo.shmid, 0, 0);
      assert(shminfo.shmaddr);
      data.image->data = shminfo.shmaddr;

      shminfo.shmseg = XCBShmSEGNew (data.conn);
      XCBShmAttach (data.conn, shminfo.shmseg,
		    shminfo.shmid, 0);
      assert(shmctl(shminfo.shmid, IPC_RMID, 0) != -1);
    }

  if (data.image)
    {
      printf ("Use of shm.\n");
      do_shm = 1;
    }
  else
    {
      printf ("Can't use shm. Use standard functions.\n");
      shmdt (shminfo.shmaddr);		       
      shmctl (shminfo.shmid, IPC_RMID, 0);
      data.image = NULL;
    }
}

int
main (int argc, char *argv[])
{
  Data             data;
  XCBSCREEN       *screen;
  XCBDRAWABLE      win;
  XCBDRAWABLE      rect;
  XCBGCONTEXT      bgcolor;
  CARD32           mask;
  CARD32           valgc[2];
  CARD32           valwin[3];
  XCBRECTANGLE     rect_coord = { 0, 0, W_W, W_H};
  XCBGenericEvent *e;
  int              try_shm;
  int              screen_num;
  
  try_shm = 0;

  /* Arguments test */
  if (argc < 2)
    {
      printf ("Usage: lissajoux try_shm\n");
      printf ("         try_shm == 0: shm not used\n");
      printf ("         try_shm != 0: shm is used (if availaible)\n");
      exit (0);
    }
  if (argc >= 2)
    try_shm = atoi (argv[1]);
  if (try_shm != 0)
    try_shm = 1;

  data.conn = XCBConnect (0, &screen_num);
  screen = XCBAuxGetScreen(data.conn, screen_num);
  data.depth = XCBAuxGetDepth (data.conn, screen);

  win.window = screen->root;

  data.gc = XCBGCONTEXTNew (data.conn);
  mask = GCForeground | GCGraphicsExposures;
  valgc[0] = screen->black_pixel;
  valgc[1] = 0; /* no graphics exposures */
  XCBCreateGC (data.conn, data.gc, win, mask, valgc);

  bgcolor = XCBGCONTEXTNew (data.conn);
  mask = GCForeground | GCGraphicsExposures;
  valgc[0] = screen->white_pixel;
  valgc[1] = 0; /* no graphics exposures */
  XCBCreateGC (data.conn, bgcolor, win, mask, valgc);

  data.draw.window = XCBWINDOWNew (data.conn);
  mask = XCBCWBackPixel | XCBCWEventMask | XCBCWDontPropagate;
  valwin[0] = screen->white_pixel;
  valwin[1] = KeyPressMask | ButtonReleaseMask | ExposureMask;
  valwin[2] = ButtonPressMask;
  XCBCreateWindow (data.conn, 0,
		   data.draw.window,
		   screen->root,
		   0, 0, W_W, W_H,
		   10,
		   InputOutput,
		   screen->root_visual,
		   mask, valwin);
  XCBMapWindow (data.conn, data.draw.window);

  rect.pixmap = XCBPIXMAPNew (data.conn);
  XCBCreatePixmap (data.conn, data.depth,
		   rect.pixmap, data.draw,
		   W_W, W_H);
  XCBPolyFillRectangle(data.conn, rect, bgcolor, 1, &rect_coord);

  data.format = ZPixmap;
  XCBSync (data.conn, 0); 

  if (try_shm)
    shm_test (data);

  time_start = get_time ();
  t_previous = 0.0;
  while (1)
    {
      e = XCBPollForEvent(data.conn, NULL);
      if (e)
	{
	  switch (e->response_type)
	    {
	    case XCBExpose:
	      XCBCopyArea(data.conn, rect, data.draw, bgcolor,
		          0, 0, 0, 0, W_W, W_H);
	      XCBSync (data.conn, 0);
	      break;
	    }
	  free (e);
        }
      step (data);
      XCBFlush (data.conn);
      t_previous = t;
    }
  /*NOTREACHED*/
}