summary refs log tree commit diff
path: root/src/x/context.rs
blob: 9e3796d7238460dd0330f3ece1160d9de06c498c (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
use crate::x::*;

use std::sync::Arc;

use xcb::x::{ Drawable, CreateGc, FreeGc };


pub struct GraphicContext {
  display: Arc<Display>,
  context_id: xcb::x::Gcontext,
}

impl HasDisplay for GraphicContext {
  fn display(&self) -> &Arc<Display> { &self.display }
}

impl HasConnection for GraphicContext {
  fn connection(&self) -> &Connection { self.display.connection() }
}


impl GraphicContext {
  pub fn new(display: Arc<Display>, drawable: Drawable)
    -> xcb::Result<Self>
  {
    let context_id: xcb::x::Gcontext = display.generate_id();
    let cookie = display.send_request_checked(&CreateGc {
      cid: context_id,
      drawable,
      value_list: &[],
    });
    display.check_request(cookie)?;

    Ok(GraphicContext { display, context_id })
  }
}

impl Drop for GraphicContext {
  fn drop(&mut self) {
    let cookie = self.send_request_checked(&FreeGc { gc: self.context_id });
    let _ = self.check_request(cookie); // TODO
  }
}


pub trait HasGraphicContext {
  fn graphic_context(&self) -> &GraphicContext;
}

impl HasGraphicContext for GraphicContext {
  fn graphic_context(&self) -> &GraphicContext { self }
}



pub trait GraphicContextExt: HasGraphicContext + HasDisplay + HasConnection {
  fn graphic_context_id(&self) -> xcb::x::Gcontext {
    self.graphic_context().context_id
  }
}

impl<T: HasGraphicContext + HasDisplay + HasConnection> GraphicContextExt for T { }