Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 386 lines (329 sloc) 9.107 kB
2d503f5 @ry skelton of xnotify using libev.
ry authored
1 #include <math.h>
2 #include <stdio.h>
3 #include <X11/Xlib.h>
4 #include <X11/Xatom.h>
5 #include <X11/Xutil.h>
6
7 #define EV_STANDALONE 1
8 #include <ev.c>
9
10 #define LOG_MESSAGE(...) printf(__VA_ARGS__)
11 #define LOG_ERROR(...) printf(__VA_ARGS__)
12 #define LOG_WARNING(...) printf(__VA_ARGS__)
13
0459044 @ry added some structural things
ry authored
14 #define WINDOW_WIDTH 200
15 #define WINDOW_HEIGHT 200
16
17 static char *text;
18
2d503f5 @ry skelton of xnotify using libev.
ry authored
19 enum {
20 XATOM_WM_STATE,
21 XATOM_NET_DESKTOP_NAMES,
22 XATOM_NET_WM_STATE,
23 XATOM_NET_ACTIVE_WINDOW,
24 XATOM_NET_WM_NAME,
25 XATOM_NET_WORKAREA,
26 XATOM_NET_WM_ICON,
27 XATOM_NET_WM_VISIBLE_NAME,
28 XATOM_NET_WM_STATE_SKIP_TASKBAR,
29 XATOM_NET_WM_STATE_SHADED,
30 XATOM_NET_WM_STATE_HIDDEN,
31 XATOM_NET_WM_DESKTOP,
32 XATOM_NET_MOVERESIZE_WINDOW,
33 XATOM_NET_WM_WINDOW_TYPE,
34 XATOM_NET_WM_WINDOW_TYPE_DOCK,
35 XATOM_NET_WM_WINDOW_TYPE_DESKTOP,
36 XATOM_NET_WM_STRUT,
37 XATOM_NET_CLIENT_LIST,
38 XATOM_NET_NUMBER_OF_DESKTOPS,
39 XATOM_NET_CURRENT_DESKTOP,
40 XATOM_NET_SYSTEM_TRAY_OPCODE,
41 XATOM_UTF8_STRING,
42 XATOM_MOTIF_WM_HINTS,
43 XATOM_COUNT
44 };
45
46 static char *atom_names[] = {
47 "WM_STATE",
48 "_NET_DESKTOP_NAMES",
49 "_NET_WM_STATE",
50 "_NET_ACTIVE_WINDOW",
51 "_NET_WM_NAME",
52 "_NET_WORKAREA",
53 "_NET_WM_ICON",
54 "_NET_WM_VISIBLE_NAME",
55 "_NET_WM_STATE_SKIP_TASKBAR",
56 "_NET_WM_STATE_SHADED",
57 "_NET_WM_STATE_HIDDEN",
58 "_NET_WM_DESKTOP",
59 "_NET_MOVERESIZE_WINDOW",
60 "_NET_WM_WINDOW_TYPE",
61 "_NET_WM_WINDOW_TYPE_DOCK",
62 "_NET_WM_WINDOW_TYPE_DESKTOP",
63 "_NET_WM_STRUT",
64 "_NET_CLIENT_LIST",
65 "_NET_NUMBER_OF_DESKTOPS",
66 "_NET_CURRENT_DESKTOP",
67 "_NET_SYSTEM_TRAY_OPCODE",
68 "UTF8_STRING",
69 "_MOTIF_WM_HINTS"
70 };
71
72 static struct {
73 Display *display;
74 int screen;
75 int screen_width;
76 int screen_height;
77
78 int wa_x;
79 int wa_y;
80 int wa_w;
81 int wa_h;
82
83 Visual *visual;
84 Colormap colmap;
85 XSetWindowAttributes attrs;
86 uint32_t amask;
87 int depth;
88
89 Window root;
90 Atom atoms[XATOM_COUNT];
91 Atom trayselatom;
92 } X;
93
94 static void cleanup()
95 {
7cadb93 @ry added window from hello_win.c
ry authored
96 LOG_MESSAGE("cleanup\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
97 exit(1);
98 }
99
100 /**************************************************************************
101 X error handlers
102 **************************************************************************/
103
104 static int X_error_handler(Display *dpy, XErrorEvent *error)
105 {
106 char buf[1024];
107 if (error->error_code == BadWindow)
108 return 0;
109 XGetErrorText(dpy, error->error_code, buf, sizeof(buf));
7cadb93 @ry added window from hello_win.c
ry authored
110 LOG_WARNING("X error: %s (resource id: %d)\n", buf, error->resourceid);
2d503f5 @ry skelton of xnotify using libev.
ry authored
111 return 0;
112 }
113
114 static int X_io_error_handler(Display *dpy)
115 {
7cadb93 @ry added window from hello_win.c
ry authored
116 LOG_WARNING("fatal error, connection to X server lost? cleaning up\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
117 cleanup();
118 return 0;
119 }
120
121 /**************************************************************************
122 window properties
123 **************************************************************************/
124
125 static void *get_prop_data(Window win, Atom prop, Atom type, int *items)
126 {
127 Atom type_ret;
128 int format_ret;
129 unsigned long items_ret;
130 unsigned long after_ret;
131 unsigned char *prop_data;
132
133 prop_data = 0;
134
135 XGetWindowProperty(X.display, win, prop, 0, 0x7fffffff, False,
136 type, &type_ret, &format_ret, &items_ret,
137 &after_ret, &prop_data);
138 if (items)
139 *items = items_ret;
140
141 return prop_data;
142 }
143
144 /**************************************************************************
145 initialization
146 **************************************************************************/
147
148 static void initX()
149 {
150 /* open connection to X server */
151 X.display = XOpenDisplay(0);
152 if (!X.display)
7cadb93 @ry added window from hello_win.c
ry authored
153 LOG_ERROR("failed connect to X server\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
154 XSetErrorHandler(X_error_handler);
155 XSetIOErrorHandler(X_io_error_handler);
156
157 memset(&X.attrs, 0, sizeof(X.attrs));
158
159 /* useful variables */
160 X.screen = DefaultScreen(X.display);
161 X.screen_width = DisplayWidth(X.display, X.screen);
162 X.screen_height = DisplayHeight(X.display, X.screen);
163 X.visual = DefaultVisual(X.display, X.screen);
164 X.colmap = CopyFromParent;
165 X.root = RootWindow(X.display, X.screen);
166 X.amask = 0;
167 X.depth = DefaultDepth(X.display, X.screen);
168 X.wa_x = 0;
169 X.wa_y = 0;
170 X.wa_w = X.screen_width;
171 X.wa_h = X.screen_height;
172
173 /* get internal atoms */
174 XInternAtoms(X.display, atom_names, XATOM_COUNT, False, X.atoms);
7cadb93 @ry added window from hello_win.c
ry authored
175 XSelectInput( X.display
176 , X.root
177 , PropertyChangeMask
178 | ButtonPressMask
179 | KeyPressMask
180 | ExposureMask
181 | FocusChangeMask
182 | StructureNotifyMask
183 );
2d503f5 @ry skelton of xnotify using libev.
ry authored
184
185 /* append_font_path_to_imlib(); */
186
187 /* get workarea */
188 int32_t *workarea = get_prop_data(X.root, X.atoms[XATOM_NET_WORKAREA], XA_CARDINAL, 0);
189 if (workarea) {
190 X.wa_x = workarea[0];
191 X.wa_y = workarea[1];
192 X.wa_w = workarea[2];
193 X.wa_h = workarea[3];
194 XFree(workarea);
195 }
196 }
197
198 /**************************************************************************
199 libev callbacks
200 **************************************************************************/
201
7cadb93 @ry added window from hello_win.c
ry authored
202 static int counter = 0;
203
2d503f5 @ry skelton of xnotify using libev.
ry authored
204 static void xconnection_cb(EV_P_ struct ev_io *w, int revents)
205 {
206 XEvent e;
207 while (XPending(X.display)) {
7cadb93 @ry added window from hello_win.c
ry authored
208
209 printf("hello world %d\n", counter++);
210
2d503f5 @ry skelton of xnotify using libev.
ry authored
211 XNextEvent(X.display, &e);
212 switch (e.type) {
213 case SelectionClear:
214 /*
215 handle_selection_clear(&e.xselectionclear);
216 */
217 break;
218 case Expose:
219 /* commence_panel_redraw = 1; */
220 break;
221 case ButtonPress:
7cadb93 @ry added window from hello_win.c
ry authored
222 case KeyPress:
223 printf("button press\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
224 /*
225 handle_button(e.xbutton.x, e.xbutton.y, e.xbutton.button);
226 */
227 break;
228 case ConfigureNotify:
229 /*
230 handle_configure_notify(e.xconfigure.window);
231 */
232 break;
233 case PropertyNotify:
234 /*
235 handle_property_notify(e.xproperty.window, e.xproperty.atom);
236 */
237 break;
238 case FocusIn:
239 /*
240 handle_focusin(e.xfocus.window);
241 render_update_panel_positions(&P);
242 commence_taskbar_redraw = 1;
243 */
244 break;
245 case ClientMessage:
246 /*
247 handle_client_message(&e.xclient);
248 */
249 break;
250 case ReparentNotify:
251 /*
252 handle_reparent_notify(e.xreparent.window, e.xreparent.parent);
253 */
254 break;
255 case DestroyNotify:
256 /*
257 del_tray_icon(e.xdestroywindow.window);
258 render_update_panel_positions(&P);
259 commence_panel_redraw = 1;
260 */
261 break;
262 default:
263 break;
264 }
265 XSync(X.display, 0);
266 /*
267 if (commence_panel_redraw) {
268 render_panel(&P);
269 } else if (commence_switcher_redraw || commence_taskbar_redraw) {
270 if (commence_switcher_redraw) {
271 render_switcher(P.desktops);
272 }
273 if (commence_taskbar_redraw) {
274 render_taskbar(P.tasks, P.desktops);
275 }
276 render_present();
277 }
278 commence_panel_redraw = 0;
279 commence_switcher_redraw = 0;
280 commence_taskbar_redraw = 0;
281 */
282 }
283 }
284
285 /**************************************************************************
286 signal handlers
287 **************************************************************************/
288
289 static void sighup_handler(int xxx)
290 {
7cadb93 @ry added window from hello_win.c
ry authored
291 LOG_MESSAGE("sighup signal received\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
292 cleanup();
293 /* xmemleaks(); */
294 exit(0);
295 }
296
297 static void sigint_handler(int xxx)
298 {
7cadb93 @ry added window from hello_win.c
ry authored
299 LOG_MESSAGE("sigint signal received\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
300 cleanup();
301 /* xmemleaks(); */
302 exit(0);
303 }
304
305 /**************************************************************************
306 libev loop and main
307 **************************************************************************/
308
309 static void init_and_start_ev_loop(int xfd)
310 {
311 struct ev_loop *el = ev_default_loop(0);
312 ev_io xconnection;
313
314 /* macros?! whuut?! */
315 xconnection.active = xconnection.pending = xconnection.priority = 0;
316 xconnection.cb = xconnection_cb;
317 xconnection.fd = xfd;
318 xconnection.events = EV_READ | EV_IOFDSET;
319
320 ev_io_start(el, &xconnection);
321 ev_loop(el, 0);
322 }
323
7cadb93 @ry added window from hello_win.c
ry authored
324 void display_window()
325 {
326 XClassHint class_hint;
511722b modified: hello_win.c
mos authored
327 XSizeHints *size_hints;
7cadb93 @ry added window from hello_win.c
ry authored
328
329 int black_color = BlackPixel(X.display, X.screen);
330 int white_color = WhitePixel(X.display, X.screen);
331
332 Window window = XCreateSimpleWindow( X.display
333 , X.root
334 , 0
335 , 0
0459044 @ry added some structural things
ry authored
336 , WINDOW_WIDTH
337 , WINDOW_HEIGHT
7cadb93 @ry added window from hello_win.c
ry authored
338 , black_color
339 , 35
340 , white_color
341 );
511722b modified: hello_win.c
mos authored
342 XStoreName(X.display, window, "test_programme");
7cadb93 @ry added window from hello_win.c
ry authored
343 class_hint.res_name = "test";
344 class_hint.res_class = "test";
345 XSetClassHint(X.display, window, &class_hint);
511722b modified: hello_win.c
mos authored
346
347
348
349 size_hints = XAllocSizeHints();
350 size_hints->flags = PPosition | PSize | PMinSize;
351 size_hints->min_width = WINDOW_WIDTH;
352 size_hints->min_height = WINDOW_HEIGHT;
353 XSetWMNormalHints(X.display, window, size_hints);
354
355
356
7cadb93 @ry added window from hello_win.c
ry authored
357 XMapWindow(X.display, window);
358 XFlush(X.display);
359 }
360
2d503f5 @ry skelton of xnotify using libev.
ry authored
361 int main(int argc, char **argv)
362 {
0459044 @ry added some structural things
ry authored
363
364 if(argc == 2)
365 text = argv[1];
366 else {
367 printf("give an argument.\n");
368 exit(1);
369 }
370
7cadb93 @ry added window from hello_win.c
ry authored
371 LOG_MESSAGE("starting xnotify\n");
2d503f5 @ry skelton of xnotify using libev.
ry authored
372
373 initX();
374
375 signal(SIGHUP, sighup_handler);
376 signal(SIGINT, sigint_handler);
7cadb93 @ry added window from hello_win.c
ry authored
377
378 display_window();
2d503f5 @ry skelton of xnotify using libev.
ry authored
379
380 init_and_start_ev_loop(ConnectionNumber(X.display));
381
382 cleanup();
383
384 return 0;
385 }
Something went wrong with that request. Please try again.