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

use crate::base::{self, BaseError, BaseEvent, GeEvent, Reply, Wired, Xid};
use crate::ext;
use crate::ffi::base::*;
use crate::ffi::ext::*;
use crate::xproto;
use crate::xproto::PropEl;

use bitflags::bitflags;
use libc::{self, iovec};
use std::convert::TryInto;
use std::os::unix::prelude::RawFd;

pub const XNAME: &str = "BIG-REQUESTS";
pub const MAJOR_VERSION: u32 = 0;
pub const MINOR_VERSION: u32 = 0;
pub const VERSION_STRING: &str = "0.0";

pub(crate) static mut FFI_EXT: xcb_extension_t = xcb_extension_t {
    name: "BIG-REQUESTS\0".as_ptr() as *const _,
    global_id: 0,
};

pub fn prefetch_extension_data(conn: &base::Connection) {
    unsafe {
        xcb_prefetch_extension_data(conn.get_raw_conn(), &mut FFI_EXT as *mut _);
    }
}

pub fn get_extension_data(conn: &base::Connection) -> std::option::Option<ext::ExtensionData> {
    unsafe {
        let reply = xcb_get_extension_data(conn.get_raw_conn(), &mut FFI_EXT as *mut _);
        assert!(!reply.is_null(), "Could not fetch BigRequests extension data");
        let reply = xproto::QueryExtensionReply::from_raw(reply);
        if !reply.present() {
            std::mem::forget(reply);
            return None;
        }
        let res = ext::ExtensionData{
            ext: ext::Extension::BigRequests,
            major_opcode: reply.major_opcode(),
            first_event: reply.first_event(),
            first_error: reply.first_error(),
        };
        std::mem::forget(reply);
        Some(res)
    }
}

pub struct EnableReply {
    raw: *const u8,
}

impl EnableReply {

    fn wire_ptr(&self) -> *const u8 {
        self.raw
    }

    pub fn response_type(&self) -> u8 {
        unsafe {
            let offset = 0usize;
            let ptr = self.wire_ptr().add(offset) as *const u8;
            *ptr
        }
    }


    pub fn sequence(&self) -> u16 {
        unsafe {
            let offset = 2usize;
            let ptr = self.wire_ptr().add(offset) as *const u16;
            *ptr
        }
    }

    pub fn length(&self) -> u32 {
        unsafe {
            let offset = 4usize;
            let ptr = self.wire_ptr().add(offset) as *const u32;
            *ptr
        }
    }

    pub fn maximum_request_length(&self) -> u32 {
        unsafe {
            let offset = 8usize;
            let ptr = self.wire_ptr().add(offset) as *const u32;
            *ptr
        }
    }
}

impl base::Reply for EnableReply {
    unsafe fn from_raw(raw: *const u8) -> Self {
        Self { raw }
    }

    unsafe fn into_raw(self) -> *const u8 {
        let raw = self.raw;
        std::mem::forget(self);
        raw
    }
}

impl std::fmt::Debug for EnableReply {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EnableReply")
            .field("response_type", &self.response_type())
            .field("pad", &1)
            .field("sequence", &self.sequence())
            .field("length", &self.length())
            .field("maximum_request_length", &self.maximum_request_length())
            .finish()
    }
}

impl Drop for EnableReply {
    fn drop(&mut self) {
        unsafe { libc::free(self.raw as *mut _); }
    }
}

/// Cookie type for [Enable]
///
/// This cookie can be used to get a EnableReply
/// result with `Connection::wait_for_reply`
#[derive(Debug)]
pub struct EnableCookie {
    seq: u64,
}

#[derive(Debug)]
/// Unchecked cookie type for [Enable]
///
/// This cookie can be used to get a EnableReply
/// result with `Connection::wait_for_reply_unchecked`
pub struct UncheckedEnableCookie {
    seq: u64,
}

impl base::Cookie for EnableCookie {
    unsafe fn from_sequence(seq: u64) -> Self {
        EnableCookie { seq }
    }

    fn sequence(&self) -> u64 {
        self.seq
    }
}

unsafe impl base::CheckedCookie for EnableCookie {
}

unsafe impl base::CheckedCookieWithReply for EnableCookie {
    type Reply = EnableReply;
}

impl base::Cookie for UncheckedEnableCookie {
    unsafe fn from_sequence(seq: u64) -> Self {
        UncheckedEnableCookie { seq }
    }

    fn sequence(&self) -> u64 {
        self.seq
    }
}

unsafe impl base::UncheckedCookieWithReply for UncheckedEnableCookie {
    type Reply = EnableReply;
}

#[derive(Clone, Debug)]
pub struct Enable {
}

unsafe impl base::RawRequest for Enable {
    fn raw_request(&self, c: &base::Connection, checked: bool) -> u64 { unsafe {
        let mut protocol_request = xcb_protocol_request_t {
            count: 2,
            ext: (&mut FFI_EXT) as *mut _,
            opcode: 0,
            isvoid: 0,
        };

        let mut sections: [iovec; 4] = [iovec {
            iov_base: std::ptr::null_mut(),
            iov_len: 0,
        }; 4];

        let buf0: &mut [u8] = &mut [0; 4];
        sections[2].iov_base = buf0.as_mut_ptr() as *mut _;
        sections[2].iov_len = 4;
        sections[3].iov_len = base::align_pad(sections[2].iov_len, 4);

        let flags = if checked { 1 } else { 0 };

        xcb_send_request64(
            c.get_raw_conn(),
            flags,
            sections.as_mut_ptr().add(2),
            &mut protocol_request as *mut _
        )
    }
}}

impl base::Request for Enable {
    type Cookie = EnableCookie;
    const IS_VOID: bool = false;
}

impl base::RequestWithReply for Enable {
    type Reply = EnableReply;
    type Cookie = EnableCookie;
    type UncheckedCookie = UncheckedEnableCookie;
}