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
|
use std::thread;
use std::time::Duration;
use serde::{Deserialize, Serialize};
mod animations;
use animations::Animation;
#[derive(Serialize, Deserialize)]
enum Kind {
Grow,
GrowOutline,
Shrink,
ShrinkOutline,
}
#[derive(Serialize, Deserialize)]
pub struct IndicatorConfig {
max_size: u16, // display pixels
duration: u64, // milliseconds
thickness: u32, // display pixels
framerate: u16, // number of circles to display
color: u32, // color in hex, eg.: 0x00FECA
animation: Kind, // 'Grow' | 'Shrink' | 'GrowOutline' | 'ShrinkOutline'
}
// sane defaults
impl std::default::Default for IndicatorConfig {
fn default() -> IndicatorConfig {
IndicatorConfig {
max_size: 300u16,
duration: 500u64,
thickness: 1,
framerate: 30,
color: 0xFFFFFF,
animation: Kind::Grow,
}
}
}
fn main() {
let config: IndicatorConfig = confy::load("xcursorlocate").unwrap();
let padding = config.thickness as u16; // (???) largest circle gets clipped
let win_size = config.max_size + padding;
let (conn, screen_num) = xcb::Connection::connect(None)
.unwrap_or_else(|e| panic!("Unable to connect to X session: {}", e));
let setup = conn.get_setup();
let screen = setup
.roots()
.nth(screen_num as usize)
.unwrap_or_else(|| panic!("Error accessing screen!"));
// fetch all depths
let depths = screen.allowed_depths();
let mut alpha_depths = depths.filter(|d| d.depth() == 32u8).peekable();
if alpha_depths.peek().is_none() {
panic!("Alpha channel not found!");
}
// fetch a visual supporting alpha channels
let visual = alpha_depths
.next()
.unwrap()
.visuals()
.nth(1 as usize)
.unwrap();
let win_start_x = 0;
let win_start_y = 0;
let colormap = conn.generate_id();
xcb::create_colormap(
&conn,
xcb::COLORMAP_ALLOC_NONE as u8,
colormap,
screen.root(),
visual.visual_id(),
);
let win = conn.generate_id();
xcb::create_window(
&conn,
32u8,
win,
screen.root(),
win_start_x,
win_start_y,
win_size,
win_size,
0,
xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
visual.visual_id(),
&[
(xcb::CW_BACK_PIXEL, 0x00),
(xcb::CW_BORDER_PIXEL, 0x00), // you need this if you use alpha apparently
(xcb::CW_OVERRIDE_REDIRECT, 1u32), // dont take focus
(xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE),
(xcb::CW_COLORMAP, colormap),
],
);
let gfx_ctx = conn.generate_id();
xcb::create_gc(
&conn,
gfx_ctx,
win,
&[
(xcb::GC_FOREGROUND, config.color),
(xcb::GC_GRAPHICS_EXPOSURES, 0),
(xcb::GC_LINE_WIDTH, config.thickness),
],
);
xcb::free_colormap(&conn, colormap);
let win_type_atom = xcb::intern_atom(&conn, true, "_NET_WM_WINDOW_TYPE")
.get_reply()
.unwrap()
.atom();
let win_menu_atom = xcb::intern_atom(&conn, true, "_NET_WM_WINDOW_TYPE_SPLASH")
.get_reply()
.unwrap()
.atom();
let win_state_atom = xcb::intern_atom(&conn, true, "_NET_WM_STATE")
.get_reply()
.unwrap()
.atom();
let win_on_top_atom = xcb::intern_atom(&conn, true, "_NET_WM_STATE_STAYS_ON_TOP")
.get_reply()
.unwrap()
.atom();
xcb::change_property(
&conn,
xcb::PROP_MODE_REPLACE as u8,
win,
win_type_atom,
xcb::ATOM_ATOM,
32,
&[win_menu_atom],
);
xcb::change_property(
&conn,
xcb::PROP_MODE_APPEND as u8,
win,
win_state_atom,
xcb::ATOM_ATOM,
32,
&[win_on_top_atom],
);
let fr = config.framerate;
let animation = match config.animation {
Kind::Grow => Animation::circles(config, Box::new((0..fr).rev()), false),
Kind::GrowOutline => Animation::circles(config, Box::new((0..fr).rev()), true),
Kind::Shrink => Animation::circles(config, Box::new(0..fr), false),
Kind::ShrinkOutline => Animation::circles(config, Box::new(0..fr), true),
};
xcb::map_window(&conn, win);
conn.flush();
// wait till window is mapped
let event = conn.wait_for_event();
match event {
None => {}
Some(e) => {
let r = e.response_type() & !0x80;
match r {
// the window is mapped to display
xcb::EXPOSE => {
let pointer = xcb::query_pointer(&conn, win).get_reply().unwrap();
let p_x = pointer.root_x();
let p_y = pointer.root_y();
move_win_to_cursor(&conn, win, win_size, p_x, p_y);
animation.play(&conn, win, gfx_ctx);
thread::sleep(Duration::from_millis(100));
}
_ => eprintln!("how did you even get here: {}", r),
}
}
}
}
fn move_win_to_cursor(
conn: &xcb::Connection,
win: u32,
win_size: u16,
p_x: i16,
p_y: i16,
) -> (u32, u32) {
let win_x = p_x - (win_size as i16) / 2;
let win_y = p_y - (win_size as i16) / 2;
let win_x = if win_x < 0 { 0 } else { win_x as u32 };
let win_y = if win_y < 0 { 0 } else { win_y as u32 };
xcb::configure_window(
&conn,
win,
&[
(xcb::CONFIG_WINDOW_X as u16, win_x),
(xcb::CONFIG_WINDOW_Y as u16, win_y),
],
);
conn.flush();
return (win_x, win_y);
}
|