9 #include <xcb/xcb_aux.h>
10 #include <xcb/xcb_image.h>
11 #define XCB_ALL_PLANES ~0
13 /* Needed for xcb_set_wm_protocols() */
14 #include <xcb/xcb_icccm.h>
21 /* Parameters of the fractal */
23 /* double cr = -0.7927; */
24 /* double ci = 0.1609; */
26 /* double cr = 0.32; */
27 /* double ci = 0.043; */
29 /* double cr = -1.1380; */
30 /* double ci = -0.2403; */
32 /* double cr = -0.0986; */
33 /* double ci = -0.65186; */
35 /* double cr = -0.1225; */
36 /* double ci = 0.7449; */
40 double origin_x = -1.8;
41 double origin_y = -1.2;
45 /* Numbers of colors in the palette */
49 get_atom (xcb_connection_t *connection, const char *atomName)
53 xcb_atom_t atom = XCB_NONE;
54 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection,
55 xcb_intern_atom(connection, 0, strlen(atomName), atomName), NULL);
65 palette_julia (Data *datap)
67 xcb_alloc_color_reply_t *rep;
70 datap->palette = (uint32_t *)malloc (sizeof (uint32_t) * cmax);
72 for (i = 0 ; i < cmax ; i++)
75 rep = xcb_alloc_color_reply (datap->conn,
76 xcb_alloc_color (datap->conn,
81 rep = xcb_alloc_color_reply (datap->conn,
82 xcb_alloc_color (datap->conn,
84 65535, (i-127)<<9, 0),
87 rep = xcb_alloc_color_reply (datap->conn,
88 xcb_alloc_color (datap->conn,
90 65535, 65535, (i-255)<<10),
94 datap->palette[i] = 0;
96 datap->palette[i] = rep->pixel;
103 draw_julia (Data *datap)
109 datap->image = xcb_image_get (datap->conn, datap->draw,
111 XCB_ALL_PLANES, datap->format);
113 assert(datap->image);
115 for (i = 0 ; i < datap->image->width ; i++)
116 for (j = 0 ; j < datap->image->height ; j++)
118 zr = origin_x + width * (double)i / (double)datap->image->width;
119 zi = origin_y + height * (double)j / (double)datap->image->height;
121 while ((zr*zr + zi*zi < 4.0) &&
125 zr = zr*zr - zi*zi + cr;
129 xcb_image_put_pixel (datap->image,
134 xcb_image_put (datap->conn, datap->draw, datap->gc, datap->image,
139 main (int argc, char *argv[])
142 xcb_screen_t *screen;
145 xcb_gcontext_t bgcolor;
149 xcb_rectangle_t rect_coord = { 0, 0, W_W, W_H};
152 data.conn = xcb_connect (0, &screen_num);
153 screen = xcb_aux_get_screen (data.conn, screen_num);
154 data.depth = xcb_aux_get_depth (data.conn, screen);
158 data.gc = xcb_generate_id (data.conn);
159 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
160 valgc[0] = screen->black_pixel;
161 valgc[1] = 0; /* no graphics exposures */
162 xcb_create_gc (data.conn, data.gc, win, mask, valgc);
164 bgcolor = xcb_generate_id (data.conn);
165 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
166 valgc[0] = screen->white_pixel;
167 valgc[1] = 0; /* no graphics exposures */
168 xcb_create_gc (data.conn, bgcolor, win, mask, valgc);
170 data.draw = xcb_generate_id (data.conn);
171 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
172 valwin[0] = screen->white_pixel;
173 valwin[1] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_EXPOSURE;
174 valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
175 xcb_create_window (data.conn, 0,
180 XCB_WINDOW_CLASS_INPUT_OUTPUT,
183 xcb_map_window (data.conn, data.draw);
185 rect = xcb_generate_id (data.conn);
186 xcb_create_pixmap (data.conn, data.depth,
189 xcb_poly_fill_rectangle(data.conn, rect, bgcolor, 1, &rect_coord);
191 xcb_map_window (data.conn, data.draw);
193 data.format = XCB_IMAGE_FORMAT_Z_PIXMAP;
195 data.cmap = xcb_generate_id (data.conn);
196 xcb_create_colormap (data.conn,
197 XCB_COLORMAP_ALLOC_NONE,
200 screen->root_visual);
202 palette_julia (&data);
204 xcb_atom_t deleteWindowAtom = get_atom(data.conn, "WM_DELETE_WINDOW");
205 /* Listen to X client messages in order to be able to pickup
206 the "delete window" message that is generated for example
207 when someone clicks the top-right X button within the window
208 manager decoration (or when user hits ALT-F4). */
209 xcb_set_wm_protocols (data.conn, data.draw, 1, &deleteWindowAtom);
211 xcb_flush (data.conn);
213 bool finished = false;
216 xcb_generic_event_t *e;
217 if (e = xcb_wait_for_event(data.conn))
219 switch (e->response_type & 0x7f)
223 xcb_copy_area(data.conn, rect, data.draw, bgcolor,
224 0, 0, 0, 0, W_W, W_H);
226 xcb_flush (data.conn);
229 case XCB_CLIENT_MESSAGE:
231 if (((xcb_client_message_event_t *)e)->data.data32[0] == deleteWindowAtom)
237 case XCB_BUTTON_PRESS:
250 xcb_image_destroy (data.image);
251 xcb_disconnect (data.conn);