aboutsummaryrefslogtreecommitdiff
path: root/depends/libxcb-cursor/xcb/cursor.c
blob: 2f56f196d6cd75269282a3634c451826ece7ab92 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * vim:ts=4:sw=4:expandtab
 *
 * Copyright © 2013 Michael Stapelberg
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the names of the authors or their
 * institutions shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization from the authors.
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <xcb/xcb.h>
#include <xcb/xcb_renderutil.h>

#include "cursor.h"
#include "xcb_cursor.h"

/*
 * Parses the root window’s RESOURCE_MANAGER atom contents and stores the
 * attributes declared above in resource_manager_val.
 *
 */
static void parse_resource_manager(xcb_cursor_context_t *c, const xcb_get_property_reply_t *rm_reply) {
    int rm_length;
    char *rm = NULL;
    char *saveptr = NULL;
    char *line = NULL;
    char *sep = NULL;

    if (rm_reply == NULL || (rm_length = xcb_get_property_value_length(rm_reply)) == 0)
        return;

    if (asprintf(&rm, "%.*s", rm_length, (char*)xcb_get_property_value(rm_reply)) == -1)
        return;

    for (char *str = rm; ; str = NULL) {
        if ((line = strtok_r(str, "\n", &saveptr)) == NULL)
            break;
        /* Split the string at the delimiting : */
        if ((sep = strchr(line, ':')) == NULL) {
            /* Invalid line?! */
            free(rm);
            return;
        }
        *(sep++) = '\0';
        while (isspace(*sep))
            sep++;
        /* strdup() may return NULL, which is interpreted later as the key not
         * being available. */
        if (strcmp(line, "Xcursor.theme") == 0) {
            free(c->rm[RM_XCURSOR_THEME]);
            c->rm[RM_XCURSOR_THEME] = strdup(sep);
        } else if (strcmp(line, "Xcursor.size") == 0) {
            free(c->rm[RM_XCURSOR_SIZE]);
            c->rm[RM_XCURSOR_SIZE] = strdup(sep);
        } else if (strcmp(line, "Xft.dpi") == 0) {
            free(c->rm[RM_XFT_DPI]);
            c->rm[RM_XFT_DPI] = strdup(sep);
        }
    }

    free(rm);
}

/*
 * Tries to figure out the cursor size by checking:
 * 1. The environment variable XCURSOR_SIZE
 * 2. The RESOURCE_MANAGER entry Xcursor.size
 * 3. Guess with the RESOURCE_MANAGER entry Xft.dpi * 16 / 72
 * 4. Guess with the display size.
 *
 */
static uint32_t get_default_size(xcb_cursor_context_t *c, xcb_screen_t *screen) {
    char *env;
    uint16_t dim;

    if ((env = getenv("XCURSOR_SIZE")) != NULL)
        return atoi(env);

    if (c->rm[RM_XCURSOR_SIZE] != NULL)
        return atoi(c->rm[RM_XCURSOR_SIZE]);

    if (c->rm[RM_XFT_DPI] != NULL) {
        const int dpi = atoi(c->rm[RM_XFT_DPI]);
        if (dpi > 0)
            return dpi * 16 / 72;
    }

    if (screen->height_in_pixels < screen->width_in_pixels)
        dim = screen->height_in_pixels;
    else
        dim = screen->width_in_pixels;

    return dim / 48;
}

int xcb_cursor_context_new(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_context_t **ctx) {
    xcb_cursor_context_t *c;
    const xcb_query_extension_reply_t *ext;
    xcb_get_property_cookie_t rm_cookie;
    xcb_get_property_reply_t *rm_reply;
    xcb_render_query_pict_formats_cookie_t pf_cookie;
    xcb_render_query_version_cookie_t ver_cookie;

    if ((*ctx = calloc(1, sizeof(struct xcb_cursor_context_t))) == NULL)
        return -errno;

    c = *ctx;
    c->conn = conn;
    c->root = screen->root;
    c->render_version = RV_NONE;

    ext = xcb_get_extension_data(conn, &xcb_render_id);

    // XXX: Is it maybe necessary to ever use long_offset != 0?
    // XXX: proper length? xlib seems to use 100 MB o_O
    rm_cookie = xcb_get_property(conn, 0, c->root, XCB_ATOM_RESOURCE_MANAGER, XCB_ATOM_STRING, 0, 16 * 1024);
    if (ext && ext->present) {
        ver_cookie = xcb_render_query_version(conn, XCB_RENDER_MAJOR_VERSION, XCB_RENDER_MINOR_VERSION);
        pf_cookie = xcb_render_query_pict_formats(conn);
    }
    c->cursor_font = xcb_generate_id(conn);
    xcb_open_font(conn, c->cursor_font, strlen("cursor"), "cursor");

    rm_reply = xcb_get_property_reply(conn, rm_cookie, NULL);
    parse_resource_manager(c, rm_reply);
    free(rm_reply);

    if (ext && ext->present) {
        xcb_render_query_version_reply_t *reply = xcb_render_query_version_reply(conn, ver_cookie, NULL);

        if (reply && (reply->major_version >= 1 || reply->minor_version >= 8))
            c->render_version = RV_ANIM_CURSOR;
        else if (reply && (reply->major_version >= 1 || reply->minor_version >= 5))
            c->render_version = RV_CURSOR;
        free(reply);

        c->pf_reply = xcb_render_query_pict_formats_reply(conn, pf_cookie, NULL);
        c->pict_format = xcb_render_util_find_standard_format(c->pf_reply, XCB_PICT_STANDARD_ARGB_32);
    }

    c->size = get_default_size(c, screen);

    return 0;
}

void xcb_cursor_context_free(xcb_cursor_context_t *c) {
    free(c->rm[RM_XCURSOR_THEME]);
    free(c->rm[RM_XCURSOR_SIZE]);
    free(c->rm[RM_XFT_DPI]);
    free(c->pf_reply);
    free(c);
}