]> git.sur5r.net Git - i3/i3/blob - i3-nagbar/xcb.c
add i3-nagbar. tells you about config file errors (for example)
[i3/i3] / i3-nagbar / xcb.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 #include <xcb/xcb.h>
17 #include <xcb/xcb_keysyms.h>
18
19 #include <X11/keysym.h>
20
21 #include "i3-nagbar.h"
22
23 /*
24  * Convenience-wrapper around xcb_change_gc which saves us declaring a variable
25  *
26  */
27 void xcb_change_gc_single(xcb_connection_t *conn, xcb_gcontext_t gc, uint32_t mask, uint32_t value) {
28         xcb_change_gc(conn, gc, mask, &value);
29 }
30
31 /*
32  * Returns the colorpixel to use for the given hex color (think of HTML).
33  *
34  * The hex_color has to start with #, for example #FF00FF.
35  *
36  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
37  * This has to be done by the caller.
38  *
39  */
40 uint32_t get_colorpixel(xcb_connection_t *conn, char *hex) {
41         char strgroups[3][3] = {{hex[1], hex[2], '\0'},
42                                 {hex[3], hex[4], '\0'},
43                                 {hex[5], hex[6], '\0'}};
44         uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
45                              (strtol(strgroups[1], NULL, 16)),
46                              (strtol(strgroups[2], NULL, 16))};
47
48         return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
49 }
50
51 /*
52  * Opens the window we use for input/output and maps it
53  *
54  */
55 xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height) {
56         xcb_window_t win = xcb_generate_id(conn);
57         //xcb_cursor_t cursor_id = xcb_generate_id(conn);
58
59 #if 0
60         /* Use the default cursor (left pointer) */
61         if (cursor > -1) {
62                 i3Font *cursor_font = load_font(conn, "cursor");
63                 xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id,
64                                 XCB_CURSOR_LEFT_PTR, XCB_CURSOR_LEFT_PTR + 1,
65                                 0, 0, 0, 65535, 65535, 65535);
66         }
67 #endif
68
69         uint32_t mask = 0;
70         uint32_t values[3];
71
72         mask |= XCB_CW_BACK_PIXEL;
73         values[0] = 0;
74
75         mask |= XCB_CW_EVENT_MASK;
76         values[1] = XCB_EVENT_MASK_EXPOSURE |
77                 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
78                 XCB_EVENT_MASK_BUTTON_PRESS |
79                 XCB_EVENT_MASK_BUTTON_RELEASE;
80
81         xcb_create_window(conn,
82                           XCB_COPY_FROM_PARENT,
83                           win, /* the window id */
84                           root, /* parent == root */
85                           50, 50, width, height, /* dimensions */
86                           0, /* border = 0, we draw our own */
87                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
88                           XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
89                           mask,
90                           values);
91
92 #if 0
93         if (cursor > -1)
94                 xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id);
95 #endif
96
97         /* Map the window (= make it visible) */
98         xcb_map_window(conn, win);
99
100         return win;
101 }
102
103 /*
104  * Returns the ID of the font matching the given pattern and stores the height
105  * of the font (in pixels) in *font_height. die()s if no font matches.
106  *
107  */
108 int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
109         xcb_void_cookie_t font_cookie;
110         xcb_list_fonts_with_info_cookie_t info_cookie;
111
112         /* Send all our requests first */
113         int result;
114         result = xcb_generate_id(conn);
115         font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
116         info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
117
118         xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
119         if (error != NULL) {
120                 fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
121                 exit(1);
122         }
123
124         /* Get information (height/name) for this font */
125         xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
126         if (reply == NULL)
127                 die("Could not load font \"%s\"\n", pattern);
128
129         *font_height = reply->font_ascent + reply->font_descent;
130
131         return result;
132 }