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

use std::sync::Arc;

use xcb::{ Xid };
use xcb::x::{
  ClientMessageData,
  Cw, CreateWindow,
  Drawable,
  EventMask,
  PolyFillRectangle,
  Screen,
  WindowClass
};

mod x;


#[derive(Clone, Copy, PartialEq, Eq)]
enum ScreenSide { Top, Bottom, Left, Right }

struct DockConfiguration {
  thickness: u16,
  side: ScreenSide,
  background_pixel: u32,
}

struct Dock {
  configuration: DockConfiguration,
  window: Window,
  bounds: Arc<Rectangle<u16>>,
}

impl HasWindow for Dock {
  fn window(&self) -> &Window { &self.window }
}


#[derive(Debug)]
struct Rectangle<T> {
  left: T,
  top: T,
  width: T,
  height: T,
}



fn main() -> xcb::Result<()> {
  let display = Arc::new(Display::new()?);

  // #d4ffcc
  let background_pixel = display.allocate_color(54484, 65535, 52428)?;

  let dock = Dock::new(Arc::clone(&display), DockConfiguration {
    thickness: 50,
    side: ScreenSide::Bottom,
    background_pixel,
  })?;

  loop {
    match display.wait_for_event()? {
      xcb::Event::X(xcb::x::Event::Expose(_event)) => {
	let _ = dock.redraw(); // TODO
      }

      xcb::Event::X(xcb::x::Event::ButtonPress(event)) => {
        println!("click {:?}", event);
      }

      xcb::Event::X(xcb::x::Event::ClientMessage(event)) => {
        if let ClientMessageData::Data32([atom, ..]) = event.data() {
          if atom == display.atoms().wm_delete_window.resource_id() {
            break;
          }
        }
      }

      event => { println!("unknown event {:?}", event); }
    }
  }

  Ok(())
}


impl Dock {
  fn new(display: Arc<Display>, configuration: DockConfiguration)
    -> xcb::Result<Dock>
  {
    let screen = display.screen();

    let dock_bounds = Dock::compute_bounds(&screen, &configuration);

    let window = Window::new(Arc::clone(&display), &mut CreateWindow {
      depth: xcb::x::COPY_FROM_PARENT as u8,
      wid: xcb::x::Window::none(),
      parent: screen.root(),
      x: dock_bounds.left as i16,
      y: dock_bounds.top as i16,
      width: dock_bounds.width,
      height: dock_bounds.height,
      border_width: 0,
      class: WindowClass::InputOutput,
      visual: screen.root_visual(),
      value_list: &[
        Cw::BackPixel(configuration.background_pixel),
        Cw::EventMask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS),
      ],
    })?;
    let dock = Dock { window, configuration, bounds: Arc::new(dock_bounds) };

    dock.set_string_property(&xcb::x::ATOM_WM_NAME, b"sandbar")?;

    dock.set_atom_property(&dock.atoms().wm_protocols,
                           &[dock.atoms().wm_delete_window])?;

    dock.set_atom_property(&dock.atoms().wm_type,
                           &[dock.atoms().wm_type_dock])?;

    dock.set_strut_properties(&screen)?;

    dock.map_window()?;

    Ok(dock)
  }


  fn set_strut_properties(&self, screen: &Screen) -> xcb::Result<()> {
    let atoms = self.atoms();

    let thickness = self.configuration.thickness as u32;

    let strut_values = match self.configuration.side {
      ScreenSide::Left => &[thickness, 0 as u32, 0 as u32, 0 as u32],
      ScreenSide::Right => &[0 as u32, thickness, 0 as u32, 0 as u32],
      ScreenSide::Top => &[0 as u32, 0 as u32, thickness, 0 as u32],
      ScreenSide::Bottom => &[0 as u32, 0 as u32, 0 as u32, thickness],
    };
    self.set_cardinal_property(&atoms.wm_strut, strut_values)?;

    let strut_partial_values = &[
      strut_values[0],
      strut_values[1],
      strut_values[2],
      strut_values[3],

      0 as u32,
      if self.configuration.side == ScreenSide::Left {
        screen.height_in_pixels() as u32
      } else { 0 as u32 },

      0 as u32,
      if self.configuration.side == ScreenSide::Right {
        screen.height_in_pixels() as u32
      } else { 0 as u32 },

      0 as u32,
      if self.configuration.side == ScreenSide::Top {
        screen.width_in_pixels() as u32
      } else { 0 as u32 },

      0 as u32,
      if self.configuration.side == ScreenSide::Bottom {
        screen.width_in_pixels() as u32
      } else { 0 as u32 },
    ];

    self.set_cardinal_property(&atoms.wm_strut_partial,
                               strut_partial_values)?;

    Ok(())
  }


  fn compute_bounds(screen: &Screen, configuration: &DockConfiguration)
    -> Rectangle<u16>
  {
    if configuration.side == ScreenSide::Top
       || configuration.side == ScreenSide::Bottom
    {
      let left = 0;
      let width = screen.width_in_pixels();
      let height = configuration.thickness;

      let top = if configuration.side == ScreenSide::Bottom {
        screen.height_in_pixels() - height
      } else { 0 };

      Rectangle { left, top, width, height }
    } else {
      let top = 0;
      let height = screen.height_in_pixels();
      let width = configuration.thickness;

      let left = if configuration.side == ScreenSide::Right {
        screen.width_in_pixels() - width
      } else { 0 };

      Rectangle { left, top, width, height }
    }
  }


  fn redraw(&self) -> xcb::Result<()> {
    println!("redraw");

    let drawable = Drawable::Window(self.window_id());
    let context = GraphicContext::new(
	              Arc::clone(self.display()), drawable)?;

    let black = self.screen().black_pixel();

    let cookie = self.send_request_checked(&PolyFillRectangle {
      drawable: drawable,
      gc: context.graphic_context_id(),
      rectangles: &[xcb::x::Rectangle {
	x: 0, y: 0, width: 25, height: 25
      }],
    });
    self.check_request(cookie)?;

    Ok(())
  }
}

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

impl HasConnection for Dock {
  fn connection(&self) -> &Connection { self.window.connection() }
}