tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

lib.rs (5097B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 use std::os::raw::{c_void, c_char};
      6 
      7 /*
      8 
      9  This is a very simple (and unsafe!) rust wrapper for the Wayland / EGL
     10  implementation in lib.cpp.
     11 
     12  It just proxies the calls from the Compositor impl to the C99 code. This is very
     13  hacky and not suitable for production!
     14 
     15 */
     16 
     17 // Opaque wrapper for the Window type in lib.cpp
     18 #[repr(C)]
     19 pub struct Window {
     20    _unused: [u8; 0]
     21 }
     22 
     23 // C99 functions that do the compositor work
     24 extern {
     25    fn com_wl_create_window(
     26        width: i32,
     27        height: i32,
     28        enable_compositor: bool,
     29        sync_mode: i32,
     30    ) -> *mut Window;
     31    fn com_wl_destroy_window(window: *mut Window);
     32    fn com_wl_tick(window: *mut Window) -> bool;
     33    fn com_wl_get_proc_address(name: *const c_char) -> *const c_void;
     34    fn com_wl_swap_buffers(window: *mut Window);
     35 
     36    fn com_wl_create_surface(
     37        window: *mut Window,
     38        id: u64,
     39        tile_width: i32,
     40        tile_height: i32,
     41        is_opaque: bool,
     42    );
     43 
     44    fn com_wl_create_tile(
     45        window: *mut Window,
     46        id: u64,
     47        x: i32,
     48        y: i32,
     49    );
     50 
     51    fn com_wl_destroy_tile(
     52        window: *mut Window,
     53        id: u64,
     54        x: i32,
     55        y: i32,
     56    );
     57 
     58    fn com_wl_destroy_surface(
     59        window: *mut Window,
     60        id: u64,
     61    );
     62 
     63    fn com_wl_bind_surface(
     64        window: *mut Window,
     65        surface_id: u64,
     66        tile_x: i32,
     67        tile_y: i32,
     68        x_offset: &mut i32,
     69        y_offset: &mut i32,
     70        dirty_x0: i32,
     71        dirty_y0: i32,
     72        dirty_width: i32,
     73        dirty_height: i32,
     74    ) -> u32;
     75    fn com_wl_unbind_surface(window: *mut Window);
     76 
     77    fn com_wl_begin_transaction(window: *mut Window);
     78 
     79    fn com_wl_add_surface(
     80        window: *mut Window,
     81        id: u64,
     82        x: i32,
     83        y: i32,
     84        clip_x: i32,
     85        clip_y: i32,
     86        clip_w: i32,
     87        clip_h: i32,
     88    );
     89 
     90    fn com_wl_end_transaction(window: *mut Window);
     91 
     92    fn com_wl_deinit(window: *mut Window);
     93 }
     94 
     95 pub fn create_window(
     96    width: i32,
     97    height: i32,
     98    enable_compositor: bool,
     99    sync_mode: i32,
    100 ) -> *mut Window {
    101    unsafe {
    102        com_wl_create_window(width, height, enable_compositor, sync_mode)
    103    }
    104 }
    105 
    106 pub fn destroy_window(window: *mut Window) {
    107    unsafe {
    108        com_wl_destroy_window(window);
    109    }
    110 }
    111 
    112 pub fn tick(window: *mut Window) -> bool {
    113    unsafe {
    114        com_wl_tick(window)
    115    }
    116 }
    117 
    118 pub fn get_proc_address(name: *const c_char) -> *const c_void {
    119    unsafe {
    120        com_wl_get_proc_address(name)
    121    }
    122 }
    123 
    124 pub fn create_surface(
    125    window: *mut Window,
    126    id: u64,
    127    tile_width: i32,
    128    tile_height: i32,
    129    is_opaque: bool,
    130 ) {
    131    unsafe {
    132        com_wl_create_surface(
    133            window,
    134            id,
    135            tile_width,
    136            tile_height,
    137            is_opaque,
    138        )
    139    }
    140 }
    141 
    142 pub fn create_tile(
    143    window: *mut Window,
    144    id: u64,
    145    x: i32,
    146    y: i32,
    147 ) {
    148    unsafe {
    149        com_wl_create_tile(
    150            window,
    151            id,
    152            x,
    153            y,
    154        )
    155    }
    156 }
    157 
    158 pub fn destroy_tile(
    159    window: *mut Window,
    160    id: u64,
    161    x: i32,
    162    y: i32,
    163 ) {
    164    unsafe {
    165        com_wl_destroy_tile(
    166            window,
    167            id,
    168            x,
    169            y,
    170        )
    171    }
    172 }
    173 
    174 pub fn destroy_surface(
    175    window: *mut Window,
    176    id: u64,
    177 ) {
    178    unsafe {
    179        com_wl_destroy_surface(
    180            window,
    181            id,
    182        )
    183    }
    184 }
    185 
    186 pub fn bind_surface(
    187    window: *mut Window,
    188    surface_id: u64,
    189    tile_x: i32,
    190    tile_y: i32,
    191    dirty_x0: i32,
    192    dirty_y0: i32,
    193    dirty_width: i32,
    194    dirty_height: i32,
    195 ) -> (u32, i32, i32) {
    196    unsafe {
    197        let mut x_offset = 0;
    198        let mut y_offset = 0;
    199 
    200        let fbo_id = com_wl_bind_surface(
    201            window,
    202            surface_id,
    203            tile_x,
    204            tile_y,
    205            &mut x_offset,
    206            &mut y_offset,
    207            dirty_x0,
    208            dirty_y0,
    209            dirty_width,
    210            dirty_height,
    211        );
    212 
    213        (fbo_id, x_offset, y_offset)
    214    }
    215 }
    216 
    217 pub fn add_surface(
    218    window: *mut Window,
    219    id: u64,
    220    x: i32,
    221    y: i32,
    222    clip_x: i32,
    223    clip_y: i32,
    224    clip_w: i32,
    225    clip_h: i32,
    226 ) {
    227    unsafe {
    228        com_wl_add_surface(
    229            window,
    230            id,
    231            x,
    232            y,
    233            clip_x,
    234            clip_y,
    235            clip_w,
    236            clip_h,
    237        )
    238    }
    239 }
    240 
    241 pub fn begin_transaction(window: *mut Window) {
    242    unsafe {
    243        com_wl_begin_transaction(window)
    244    }
    245 }
    246 
    247 pub fn unbind_surface(window: *mut Window) {
    248    unsafe {
    249        com_wl_unbind_surface(window)
    250    }
    251 }
    252 
    253 pub fn end_transaction(window: *mut Window) {
    254    unsafe {
    255        com_wl_end_transaction(window)
    256    }
    257 }
    258 
    259 pub fn swap_buffers(window: *mut Window) {
    260    unsafe {
    261        com_wl_swap_buffers(window);
    262    }
    263 }
    264 
    265 pub fn deinit(window: *mut Window) {
    266    unsafe {
    267        com_wl_deinit(window);
    268    }
    269 }