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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Utility functions for working with X11 cursors
//!
//! The code in this module is only available when the `cursor` feature of the library is enabled.

use crate::connection::Connection;
use crate::cookie::Cookie as X11Cookie;
use crate::errors::{ConnectionError, ReplyOrIdError};
use crate::protocol::render::{self, Pictformat};
use crate::protocol::xproto::{self, Font, Window};
use crate::resource_manager::Database;
use crate::NONE;

use std::fs::File;

mod find_cursor;
mod parse_cursor;

/// The level of cursor support of the X11 server
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RenderSupport {
    /// Render extension not available
    None,

    /// Static cursor support (CreateCursor added in RENDER 0.5)
    StaticCursor,

    /// Animated cursor support (CreateAnimCursor added in RENDER 0.8)
    AnimatedCursor,
}

/// A cookie for creating a `Handle`
#[derive(Debug)]
pub struct Cookie<'a, 'b, C: Connection> {
    conn: &'a C,
    screen: &'a xproto::Screen,
    resource_database: &'b Database,
    render_info: Option<(
        X11Cookie<'a, C, render::QueryVersionReply>,
        X11Cookie<'a, C, render::QueryPictFormatsReply>,
    )>,
}

impl<C: Connection> Cookie<'_, '_, C> {
    /// Get the handle from the replies from the X11 server
    pub fn reply(self) -> Result<Handle, ReplyOrIdError> {
        let mut render_version = (0, 0);
        let mut picture_format = NONE;
        if let Some((version, formats)) = self.render_info {
            let version = version.reply()?;
            render_version = (version.major_version, version.minor_version);
            picture_format = find_format(&formats.reply()?);
        }
        Self::from_replies(
            self.conn,
            self.screen,
            self.resource_database,
            render_version,
            picture_format,
        )
    }

    /// Get the handle from the replies from the X11 server
    pub fn reply_unchecked(self) -> Result<Option<Handle>, ReplyOrIdError> {
        let mut render_version = (0, 0);
        let mut picture_format = NONE;
        if let Some((version, formats)) = self.render_info {
            match (version.reply_unchecked()?, formats.reply_unchecked()?) {
                (Some(version), Some(formats)) => {
                    render_version = (version.major_version, version.minor_version);
                    picture_format = find_format(&formats);
                }
                _ => return Ok(None),
            }
        }
        Ok(Some(Self::from_replies(
            self.conn,
            self.screen,
            self.resource_database,
            render_version,
            picture_format,
        )?))
    }

    fn from_replies(
        conn: &C,
        screen: &xproto::Screen,
        resource_database: &Database,
        render_version: (u32, u32),
        picture_format: Pictformat,
    ) -> Result<Handle, ReplyOrIdError> {
        let render_support = if render_version.0 >= 1 || render_version.1 >= 8 {
            RenderSupport::AnimatedCursor
        } else if render_version.0 >= 1 || render_version.1 >= 5 {
            RenderSupport::StaticCursor
        } else {
            RenderSupport::None
        };
        let theme = resource_database
            .get_string("Xcursor.theme", "")
            .map(|theme| theme.to_string());
        let cursor_size = match resource_database.get_value("Xcursor.size", "") {
            Ok(Some(value)) => value,
            _ => 0,
        };
        let xft_dpi = match resource_database.get_value("Xft.dpi", "") {
            Ok(Some(value)) => value,
            _ => 0,
        };
        let cursor_size = get_cursor_size(cursor_size, xft_dpi, screen);
        let cursor_font = conn.generate_id()?;
        let _ = xproto::open_font(conn, cursor_font, b"cursor")?;
        Ok(Handle {
            root: screen.root,
            cursor_font,
            picture_format,
            render_support,
            theme,
            cursor_size,
        })
    }
}

/// A handle necessary for loading cursors
#[derive(Debug)]
pub struct Handle {
    root: Window,
    cursor_font: Font,
    picture_format: Pictformat,
    render_support: RenderSupport,
    theme: Option<String>,
    cursor_size: u32,
}

impl Handle {
    /// Create a new cursor handle for creating cursors on the given screen.
    ///
    /// The `resource_database` is used to look up settings like the current cursor theme and the
    /// cursor size to use.
    ///
    /// This function returns a cookie that can be used to later get the actual handle.
    ///
    /// If you want this function not to block, you should prefetch the RENDER extension's data on
    /// the connection.
    #[allow(clippy::new_ret_no_self)]
    pub fn new<'a, 'b, C: Connection>(
        conn: &'a C,
        screen: usize,
        resource_database: &'b Database,
    ) -> Result<Cookie<'a, 'b, C>, ConnectionError> {
        let screen = &conn.setup().roots[screen];
        let render_info = if conn
            .extension_information(render::X11_EXTENSION_NAME)?
            .is_some()
        {
            let render_version = render::query_version(conn, 0, 8)?;
            let render_pict_format = render::query_pict_formats(conn)?;
            Some((render_version, render_pict_format))
        } else {
            None
        };
        Ok(Cookie {
            conn,
            screen,
            resource_database,
            render_info,
        })
    }

    /// Loads the specified cursor, either from the cursor theme or by falling back to the X11
    /// "cursor" font.
    pub fn load_cursor<C>(&self, conn: &C, name: &str) -> Result<xproto::Cursor, ReplyOrIdError>
    where
        C: Connection,
    {
        load_cursor(conn, self, name)
    }
}

fn open_cursor(theme: &Option<String>, name: &str) -> Option<find_cursor::Cursor<File>> {
    if let Some(theme) = theme {
        if let Ok(cursor) = find_cursor::find_cursor(theme, name) {
            return Some(cursor);
        }
    }
    if let Ok(cursor) = find_cursor::find_cursor("default", name) {
        Some(cursor)
    } else {
        None
    }
}

fn create_core_cursor<C: Connection>(
    conn: &C,
    cursor_font: Font,
    cursor: u16,
) -> Result<xproto::Cursor, ReplyOrIdError> {
    let result = conn.generate_id()?;
    let _ = xproto::create_glyph_cursor(
        conn,
        result,
        cursor_font,
        cursor_font,
        cursor,
        cursor + 1,
        // foreground color
        0,
        0,
        0,
        // background color
        u16::max_value(),
        u16::max_value(),
        u16::max_value(),
    )?;
    Ok(result)
}

fn create_render_cursor<C: Connection>(
    conn: &C,
    handle: &Handle,
    image: &parse_cursor::Image,
    storage: &mut Option<(xproto::Pixmap, xproto::Gcontext, u16, u16)>,
) -> Result<render::Animcursorelt, ReplyOrIdError> {
    let (cursor, picture) = (conn.generate_id()?, conn.generate_id()?);

    // Get a pixmap of the right size and a gc for it
    let (pixmap, gc) = if storage.map(|(_, _, w, h)| (w, h)) == Some((image.width, image.height)) {
        storage.map(|(pixmap, gc, _, _)| (pixmap, gc)).unwrap()
    } else {
        let (pixmap, gc) = if let Some((pixmap, gc, _, _)) = storage {
            let _ = xproto::free_gc(conn, *gc)?;
            let _ = xproto::free_pixmap(conn, *pixmap)?;
            (*pixmap, *gc)
        } else {
            (conn.generate_id()?, conn.generate_id()?)
        };
        let _ = xproto::create_pixmap(conn, 32, pixmap, handle.root, image.width, image.height)?;
        let _ = xproto::create_gc(conn, gc, pixmap, &Default::default())?;

        *storage = Some((pixmap, gc, image.width, image.height));
        (pixmap, gc)
    };

    // Sigh. We need the pixel data as a bunch of bytes.
    let pixels = crate::x11_utils::Serialize::serialize(&image.pixels[..]);
    let _ = xproto::put_image(
        conn,
        xproto::ImageFormat::Z_PIXMAP,
        pixmap,
        gc,
        image.width,
        image.height,
        0,
        0,
        0,
        32,
        &pixels,
    )?;

    let _ = render::create_picture(
        conn,
        picture,
        pixmap,
        handle.picture_format,
        &Default::default(),
    )?;
    let _ = render::create_cursor(conn, cursor, picture, image.x_hot, image.y_hot)?;
    let _ = render::free_picture(conn, picture)?;

    Ok(render::Animcursorelt {
        cursor,
        delay: image.delay,
    })
}

fn load_cursor<C: Connection>(
    conn: &C,
    handle: &Handle,
    name: &str,
) -> Result<xproto::Cursor, ReplyOrIdError> {
    // Find the right cursor, load it directly if it is a core cursor
    let cursor_file = match open_cursor(&handle.theme, name) {
        None => return Ok(NONE),
        Some(find_cursor::Cursor::CoreChar(c)) => {
            return create_core_cursor(conn, handle.cursor_font, c)
        }
        Some(find_cursor::Cursor::File(f)) => f,
    };

    // We have to load a file and use RENDER to create a cursor
    if handle.render_support == RenderSupport::None {
        return Ok(NONE);
    }

    // Load the cursor from the file
    use std::io::BufReader;
    let images = parse_cursor::parse_cursor(&mut BufReader::new(cursor_file), handle.cursor_size)
        .or(Err(crate::errors::ParseError::InvalidValue))?;
    let mut images = &images[..];

    // No animated cursor support? Only use the first image
    if handle.render_support == RenderSupport::StaticCursor {
        images = &images[0..1];
    }

    // Now transfer the cursors to the X11 server
    let mut storage = None;
    let cursors = images
        .iter()
        .map(|image| create_render_cursor(conn, handle, image, &mut storage))
        .collect::<Result<Vec<_>, _>>()?;
    if let Some((pixmap, gc, _, _)) = storage {
        let _ = xproto::free_gc(conn, gc)?;
        let _ = xproto::free_pixmap(conn, pixmap)?;
    }

    if cursors.len() == 1 {
        Ok(cursors[0].cursor)
    } else {
        let result = conn.generate_id()?;
        let _ = render::create_anim_cursor(conn, result, &cursors)?;
        for elem in cursors {
            let _ = xproto::free_cursor(conn, elem.cursor)?;
        }
        Ok(result)
    }
}

fn find_format(reply: &render::QueryPictFormatsReply) -> Pictformat {
    reply
        .formats
        .iter()
        .filter(|format| {
            format.type_ == render::PictType::DIRECT
                && format.depth == 32
                && format.direct.red_shift == 16
                && format.direct.red_mask == 0xff
                && format.direct.green_shift == 8
                && format.direct.green_mask == 0xff
                && format.direct.blue_shift == 0
                && format.direct.blue_mask == 0xff
                && format.direct.alpha_shift == 24
                && format.direct.alpha_mask == 0xff
        })
        .map(|format| format.id)
        .next()
        .expect("The X11 server is missing the RENDER ARGB_32 standard format!")
}

fn get_cursor_size(rm_cursor_size: u32, rm_xft_dpi: u32, screen: &xproto::Screen) -> u32 {
    if let Some(size) = std::env::var("XCURSOR_SIZE")
        .ok()
        .and_then(|s| s.parse().ok())
    {
        return size;
    }
    if rm_cursor_size > 0 {
        return rm_cursor_size;
    }
    if rm_xft_dpi > 0 {
        return rm_xft_dpi * 16 / 72;
    }
    u32::from(screen.height_in_pixels.min(screen.width_in_pixels) / 48)
}