8 #include <xcb/xcb_aux.h>
9 #include <xcb/xcb_image.h>
10 #define XCB_ALL_PLANES ~0
12 /* Needed for xcb_set_wm_protocols() */
13 #include <xcb/xcb_icccm.h>
20 /* Parameters of the fractal */
22 /* double cr = -0.7927; */
23 /* double ci = 0.1609; */
25 /* double cr = 0.32; */
26 /* double ci = 0.043; */
28 /* double cr = -1.1380; */
29 /* double ci = -0.2403; */
31 /* double cr = -0.0986; */
32 /* double ci = -0.65186; */
34 /* double cr = -0.1225; */
35 /* double ci = 0.7449; */
39 double origin_x = -1.8;
40 double origin_y = -1.2;
44 /* Numbers of colors in the palette */
48 get_atom (xcb_connection_t *connection, const char *atomName)
52 xcb_atom_t atom = XCB_NONE;
53 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection,
54 xcb_intern_atom(connection, 0, strlen(atomName), atomName), NULL);
64 palette_julia (Data *datap)
66 xcb_alloc_color_reply_t *rep;
69 datap->palette = (uint32_t *)malloc (sizeof (uint32_t) * cmax);
71 for (i = 0 ; i < cmax ; i++)
74 rep = xcb_alloc_color_reply (datap->conn,
75 xcb_alloc_color (datap->conn,
80 rep = xcb_alloc_color_reply (datap->conn,
81 xcb_alloc_color (datap->conn,
83 65535, (i-127)<<9, 0),
86 rep = xcb_alloc_color_reply (datap->conn,
87 xcb_alloc_color (datap->conn,
89 65535, 65535, (i-255)<<10),
93 datap->palette[i] = 0;
95 datap->palette[i] = rep->pixel;
102 draw_julia (Data *datap)
108 datap->image = xcb_image_get (datap->conn, datap->draw,
110 XCB_ALL_PLANES, datap->format);
112 for (i = 0 ; i < datap->image->width ; i++)
113 for (j = 0 ; j < datap->image->height ; j++)
115 zr = origin_x + width * (double)i / (double)datap->image->width;
116 zi = origin_y + height * (double)j / (double)datap->image->height;
118 while ((zr*zr + zi*zi < 4.0) &&
122 zr = zr*zr - zi*zi + cr;
126 xcb_image_put_pixel (datap->image,
131 xcb_image_put (datap->conn, datap->draw, datap->gc, datap->image,
136 main (int argc, char *argv[])
139 xcb_screen_t *screen;
142 xcb_gcontext_t bgcolor;
146 xcb_rectangle_t rect_coord = { 0, 0, W_W, W_H};
149 data.conn = xcb_connect (0, &screen_num);
150 screen = xcb_aux_get_screen (data.conn, screen_num);
151 data.depth = xcb_aux_get_depth (data.conn, screen);
155 data.gc = xcb_generate_id (data.conn);
156 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
157 valgc[0] = screen->black_pixel;
158 valgc[1] = 0; /* no graphics exposures */
159 xcb_create_gc (data.conn, data.gc, win, mask, valgc);
161 bgcolor = xcb_generate_id (data.conn);
162 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
163 valgc[0] = screen->white_pixel;
164 valgc[1] = 0; /* no graphics exposures */
165 xcb_create_gc (data.conn, bgcolor, win, mask, valgc);
167 data.draw = xcb_generate_id (data.conn);
168 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
169 valwin[0] = screen->white_pixel;
170 valwin[1] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_EXPOSURE;
171 valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
172 xcb_create_window (data.conn, 0,
177 XCB_WINDOW_CLASS_INPUT_OUTPUT,
180 xcb_map_window (data.conn, data.draw);
182 rect = xcb_generate_id (data.conn);
183 xcb_create_pixmap (data.conn, data.depth,
186 xcb_poly_fill_rectangle(data.conn, rect, bgcolor, 1, &rect_coord);
188 xcb_map_window (data.conn, data.draw);
190 data.format = XCB_IMAGE_FORMAT_Z_PIXMAP;
192 data.cmap = xcb_generate_id (data.conn);
193 xcb_create_colormap (data.conn,
194 XCB_COLORMAP_ALLOC_NONE,
197 screen->root_visual);
199 palette_julia (&data);
201 xcb_atom_t deleteWindowAtom = get_atom(data.conn, "WM_DELETE_WINDOW");
202 /* Listen to X client messages in order to be able to pickup
203 the "delete window" message that is generated for example
204 when someone clicks the top-right X button within the window
205 manager decoration (or when user hits ALT-F4). */
206 xcb_set_wm_protocols (data.conn, data.draw, 1, &deleteWindowAtom);
208 xcb_flush (data.conn);
210 bool finished = false;
213 xcb_generic_event_t *e;
214 if (e = xcb_wait_for_event(data.conn))
216 switch (e->response_type & 0x7f)
220 xcb_copy_area(data.conn, rect, data.draw, bgcolor,
221 0, 0, 0, 0, W_W, W_H);
223 xcb_flush (data.conn);
226 case XCB_CLIENT_MESSAGE:
228 if (((xcb_client_message_event_t *)e)->data.data32[0] == deleteWindowAtom)
234 case XCB_BUTTON_PRESS:
247 xcb_image_destroy (data.image);
248 xcb_disconnect (data.conn);