summaryrefslogtreecommitdiff
path: root/src/waffle/x11/x11.c
blob: 8d77d08f5b1a6ecc461be5a7a208f7d97020be81 (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
// Copyright 2012 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "x11.h"

#include <waffle/core/wcore_error.h>

static void
x11_error(const char *func_call)
{
    wcore_errorf(WAFFLE_UNKNOWN_ERROR, "%s", func_call);
}

bool
x11_display_connect(
        const char *name,
        Display **xlib_dpy,
        xcb_connection_t **xcb_conn)
{
    *xlib_dpy = XOpenDisplay(name);
    if (!*xlib_dpy) {
        x11_error("XOpenDisplay");
        return false;
    }

    *xcb_conn = XGetXCBConnection(*xlib_dpy);
    if (!xcb_conn) {
        XCloseDisplay(*xlib_dpy);
        return false;
    }

    return true;
}

bool
x11_display_disconnect(Display *dpy)
{
    int error = XCloseDisplay(dpy);
    if (error)
        x11_error("XCloseDisplay");
    return !error;
}

xcb_window_t
x11_window_create(
        xcb_connection_t *conn,
        xcb_visualid_t visual_id,
        int width,
        int height)
{
    const uint32_t attrib_mask = XCB_CW_EVENT_MASK;
    const uint32_t attrib_list[] = {
        XCB_EVENT_MASK_BUTTON_PRESS
        | XCB_EVENT_MASK_EXPOSURE
        | XCB_EVENT_MASK_KEY_PRESS,
    };

    const xcb_setup_t *setup = xcb_get_setup(conn);
    if (!setup){
        wcore_errorf(WAFFLE_UNKNOWN_ERROR, "xcb_get_setup() failed");
        goto error;
    }

    const xcb_screen_t *screen = xcb_setup_roots_iterator(setup).data;
    if (!screen) {
        wcore_errorf(WAFFLE_UNKNOWN_ERROR, "failed to get xcb screen");
        goto error;
    }

    xcb_window_t window = xcb_generate_id(conn);
    if (window <= 0) {
        wcore_errorf(WAFFLE_UNKNOWN_ERROR, "xcb_generate_id() failed");
        goto error;
    }

    xcb_void_cookie_t create_cookie = xcb_create_window_checked(
            conn,
            XCB_COPY_FROM_PARENT, // depth
            window,
            screen->root, // parent
            0, 0, // x, y
            width, height,
            0, // border width
            XCB_WINDOW_CLASS_INPUT_OUTPUT,
            visual_id,
            attrib_mask,
            attrib_list);

    xcb_void_cookie_t map_cookie = xcb_map_window_checked(conn, window);

    // Check errors.
    xcb_generic_error_t *error;
    error = xcb_request_check(conn, create_cookie);
    if (error) {
        wcore_errorf(WAFFLE_UNKNOWN_ERROR,
                     "xcb_create_window_checked() failed: error=0x%x",
                     error->error_code);
        goto error;
    }
    error = xcb_request_check(conn, map_cookie);
    if (error) {
        wcore_errorf(WAFFLE_UNKNOWN_ERROR,
                     "xcb_map_window_checked() failed: error=0x%x",
                     error->error_code);
        goto error;
    }

    return window;

error:
    if (window)
        xcb_destroy_window(conn, window);
    return 0;
}

bool
x11_window_destroy(
        xcb_connection_t *conn,
        xcb_window_t window)
{
    bool ok = true;

    xcb_void_cookie_t cookie = xcb_destroy_window_checked(conn, window);
    xcb_generic_error_t *error = xcb_request_check(conn, cookie);

    if (error) {
        ok = false;
        wcore_errorf(WAFFLE_UNKNOWN_ERROR,
                     "xcb_destroy_window_checked() failed: error=0x%x",
                     error->error_code);
    }

    return ok;
}