summaryrefslogtreecommitdiff
path: root/tests/lissajoux.c
blob: d512fcde892f29ce6d64613918d330432379fea6 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#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 <xcb/xcb.h>
#include <xcb/shm.h>
#include <xcb/xcb_aux.h>
#include <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;

double tab_cos[3600];
double tab_sin[3600];

xcb_shm_segment_info_t 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 *datap)
{
  int       i, nbr;
  double    a1, a2, p1, p2;
  double    pi, period;
  double    x, y;
  
  if (do_shm)
    { 
      i = xcb_image_shm_get (datap->conn, datap->draw,
			  datap->image, shminfo,
			  0, 0,
			  XCB_ALL_PLANES);
      assert(i);
    }
  else
    {
      datap->image = xcb_image_get (datap->conn, datap->draw,
                                  0, 0, W_W, W_H,
                                  XCB_ALL_PLANES, datap->format);
      assert(datap->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 = 10000;
  for (i = 0 ; i < nbr ; i++)
      {
	x = tab_cos[(int)(a1*i + p1*nbr) % 3600];
	y = tab_sin[(int)(a2*i + p2*nbr) % 3600];
	xcb_image_put_pixel (datap->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 = tab_cos[(int)(a1*i + p1*nbr) % 3600];
	y = tab_sin[(int)(a2*i + p2*nbr) % 3600];
	xcb_image_put_pixel (datap->image,
			  (int)((double)(W_W-5)*(x+1)/2.0),
			  (int)((double)(W_H-5)*(y+1)/2.0), 0);
      }

  if (do_shm)
    xcb_image_shm_put (datap->conn, datap->draw, datap->gc,
		    datap->image, shminfo,
		    0, 0, 0, 0, W_W, W_H, 0);
  else
    {
      xcb_image_put (datap->conn, datap->draw, datap->gc, datap->image,
                   0, 0, 0, 0, W_W, W_H);
      xcb_image_destroy (datap->image);
    }
}

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

  if (t <= 2.0)
    {
      draw_lissajoux (datap);
    }
  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);
      if (do_shm)
        xcb_image_shm_destroy (datap->image);
      xcb_disconnect (datap->conn);
      exit(0);
    }
}

/* Return 0 if shm is not available, 1 otherwise */
void
shm_test (Data *datap)
{
  xcb_shm_query_version_reply_t *rep;

  rep = xcb_shm_query_version_reply (datap->conn,
				 xcb_shm_query_version (datap->conn),
				 NULL);
  if (rep)
    {
      uint8_t format;
      int shmctl_status;
      
      if (rep->shared_pixmaps && 
	  (rep->major_version > 1 || rep->minor_version > 0))
	format = rep->pixmap_format;
      else
	format = 0;
      datap->image = xcb_image_shm_create (datap->conn, datap->depth,
                                        format, NULL, W_W, W_H);
      assert(datap->image);

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

      shminfo.shmseg = xcb_shm_seg_new (datap->conn);
      xcb_shm_attach (datap->conn, shminfo.shmseg,
		    shminfo.shmid, 0);
      shmctl_status = shmctl(shminfo.shmid, IPC_RMID, 0);
      assert(shmctl_status != -1);
      free (rep);
    }

  if (datap->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);
      datap->image = NULL;
    }
}

int
main (int argc, char *argv[])
{
  Data             data;
  xcb_screen_t       *screen;
  xcb_drawable_t      win;
  xcb_drawable_t      rect;
  xcb_gcontext_t      bgcolor;
  uint32_t           mask;
  uint32_t           valgc[2];
  uint32_t           valwin[3];
  xcb_rectangle_t     rect_coord = { 0, 0, W_W, W_H};
  xcb_generic_event_t *e;
  int              try_shm;
  int              screen_num;
  int              i;
  
  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 available)\n");
      exit (0);
    }
  if (argc >= 2)
    try_shm = atoi (argv[1]);
  if (try_shm != 0)
    try_shm = 1;

  data.conn = xcb_connect (0, &screen_num);
  screen = xcb_aux_get_screen(data.conn, screen_num);
  data.depth = xcb_aux_get_depth (data.conn, screen);

  win.window = screen->root;

  data.gc = xcb_gcontext_new (data.conn);
  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  valgc[0] = screen->black_pixel;
  valgc[1] = 0; /* no graphics exposures */
  xcb_create_gc (data.conn, data.gc, win, mask, valgc);

  bgcolor = xcb_gcontext_new (data.conn);
  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  valgc[0] = screen->white_pixel;
  valgc[1] = 0; /* no graphics exposures */
  xcb_create_gc (data.conn, bgcolor, win, mask, valgc);

  data.draw.window = xcb_window_new (data.conn);
  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
  valwin[0] = screen->white_pixel;
  valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
  valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
  xcb_create_window (data.conn, 0,
		   data.draw.window,
		   screen->root,
		   0, 0, W_W, W_H,
		   10,
		   XCB_WINDOW_CLASS_INPUT_OUTPUT,
		   screen->root_visual,
		   mask, valwin);
  xcb_map_window (data.conn, data.draw.window);

  rect.pixmap = xcb_pixmap_new (data.conn);
  xcb_create_pixmap (data.conn, data.depth,
		   rect.pixmap, data.draw,
		   W_W, W_H);
  xcb_poly_fill_rectangle(data.conn, rect, bgcolor, 1, &rect_coord);

  data.format = XCB_IMAGE_FORMAT_Z_PIXMAP;
  xcb_flush (data.conn); 

  if (try_shm)
    shm_test (&data);

  for (i = 0; i < 3600; i++) {
    tab_cos[i] = cos (2.0 * 3.1415926535897 * (double)i / 3600.0);
    tab_sin[i] = sin (2.0 * 3.1415926535897 * (double)i / 3600.0);
  }

  time_start = get_time ();
  t_previous = 0.0;
  while (1)
    {
      e = xcb_poll_for_event(data.conn, NULL);
      if (e)
	{
	  switch (e->response_type)
	    {
	    case XCB_EXPOSE:
	      xcb_copy_area(data.conn, rect, data.draw, bgcolor,
		          0, 0, 0, 0, W_W, W_H);
	      break;
	    }
	  free (e);
        }
      step (&data);
      xcb_flush (data.conn);
      t_previous = t;
    }
  /*NOTREACHED*/
}