aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 8f9a8aa9807d398ba0fa0a3280a07d4fd679beb2 (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
use std::thread;
use std::time::{Duration, Instant};

fn main() {
    let win_width = 500u16;
    let win_height = 500u16;

    let (conn, screen_num) = xcb::Connection::connect(None).unwrap();
    let setup = conn.get_setup();
    let screen = setup.roots().nth(screen_num as usize).unwrap();

    let depths = screen.allowed_depths();
    let mut alpha_depths = depths.filter(|d| d.depth() == 32u8);

    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_width,
        win_height,
        2,
        xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
        visual.visual_id(),
        &[
            (xcb::CW_BACK_PIXEL, 0x00),
            (xcb::CW_BORDER_PIXEL, 0x00),
            //(xcb::CW_OVERRIDE_REDIRECT, 1u32),
            (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, screen.white_pixel()),
            (xcb::GC_GRAPHICS_EXPOSURES, 0),
            (xcb::GC_LINE_WIDTH, 1),
        ],
    );

    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 mut circles = (1u16..10u16)
        .map(|i| {
            xcb::Arc::new(
                win_start_x + 25 * i as i16,
                win_start_y + 25 * i as i16,
                win_width - 50 * i,
                win_height - 50 * i,
                0,
                360 << 6,
            )
        })
        .collect::<Vec<xcb::Arc>>()
        .into_iter();

    xcb::map_window(&conn, win);
    conn.flush();

    loop {
        let event = conn.wait_for_event();
        match event {
            None => {
                break;
            }
            Some(e) => {
                let r = e.response_type() & !0x80;
                match r {
                    xcb::EXPOSE => {
                        xcb::poly_arc(&conn, win, gfx_ctx, &[circles.next().unwrap()]);
                        conn.flush();
                        break;
                    }
                    _ => {
                        break;
                    }
                }
            }
        }
    }

    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_width, win_height, p_x, p_y);

    let loop_start = Instant::now();
    let anim_duration = Duration::from_millis(600);
    let circle_duration = Duration::from_millis(60);
    loop {
        match circles.next() {
            Some(c) => {
                let _ = xcb::poly_arc(&conn, win, gfx_ctx, &[c]);
                conn.flush();
            }
            None => {}
        };
        thread::sleep(circle_duration);
        let now = Instant::now();
        if now.duration_since(loop_start) > anim_duration {
            break;
        }
    }
    thread::sleep(Duration::from_millis(100));
}

fn move_win_to_cursor(
    conn: &xcb::Connection,
    win: u32,
    win_width: u16,
    win_height: u16,
    p_x: i16,
    p_y: i16,
) {
    let win_x = p_x - (win_width as i16) / 2;
    let win_y = p_y - (win_height as i16) / 2;
    xcb::configure_window(
        &conn,
        win,
        &[
            (
                xcb::CONFIG_WINDOW_X as u16,
                if win_x < 0 { 0 } else { win_x as u32 },
            ),
            (
                xcb::CONFIG_WINDOW_Y as u16,
                if win_y < 0 { 0 } else { win_y as u32 },
            ),
        ],
    );
    conn.flush();
}