healslut 0.1.0

Activates intimate hardware in response to screen contents.
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use async_std::sync::{Mutex, Receiver, Sender};
use async_std::task::JoinHandle;

use bit_vec::{BitBlock, BitVec};

use buttplug::client::device::VibrateCommand;
use buttplug::client::{
    ButtplugClient, ButtplugClientError, ButtplugClientEvent,
};
use buttplug::connector::ButtplugInProcessClientConnector;
use buttplug::core::errors::{ButtplugDeviceError, ButtplugError};
use buttplug::server::comm_managers::btleplug::BtlePlugCommunicationManager;
use buttplug::util::async_manager;

use crate::config::{Config, Target, TargetSpec};
use crate::gui::Gui;
use crate::{Error, State};

use futures::stream::{Stream, StreamExt};

use gdk_pixbuf::Pixbuf;

use snafu::{ensure, OptionExt, Snafu};

use std::convert::TryFrom;
use std::fmt;
use std::marker::Unpin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

#[derive(Debug, Clone)]
pub struct Rect {
    pub x0: usize,
    pub y0: usize,
    pub x1: usize,
    pub y1: usize,
}

pub struct Capture {
    pub width: usize,
    pub height: usize,
    pub rowstride: usize,
    pub bytes: Vec<u8>,
}

impl fmt::Debug for Capture {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Capture({}x{}, {} bytes)",
            self.width,
            self.height,
            self.bytes.len()
        )
    }
}

#[derive(Debug, Snafu)]
pub enum FromPixbufError {
    ChannelCount,
    Colorspace,
    NoAlpha,
}

impl TryFrom<Pixbuf> for Capture {
    type Error = FromPixbufError;

    fn try_from(mut p: Pixbuf) -> Result<Self, Self::Error> {
        ensure!(
            p.get_colorspace() == gdk_pixbuf::Colorspace::Rgb,
            Colorspace
        );

        if !p.get_has_alpha() {
            p = p.add_alpha(false, 0, 0, 0).context(NoAlpha)?;
        }

        ensure!(p.get_n_channels() == 4, ChannelCount);
        ensure!(p.get_has_alpha(), NoAlpha);

        Ok(Self {
            width: p.get_width() as usize,
            height: p.get_height() as usize,
            rowstride: p.get_rowstride() as usize,
            bytes: p.read_pixel_bytes().unwrap().to_vec(),
        })
    }
}

impl Capture {
    fn at(&self, x: usize, y: usize) -> Option<(u8, u8, u8, u8)> {
        let i = self.rowstride * y + 4 * x;
        let b = self.bytes.get(i)?;
        let g = self.bytes.get(i + 1)?;
        let r = self.bytes.get(i + 2)?;
        let a = self.bytes.get(i + 3)?;

        Some((*r, *g, *b, *a))
    }

    pub fn dims(&self, target: &TargetSpec) -> Rect {
        let half = target.size / 2;

        let x1 = std::cmp::min(target.x + half + 1, self.width);
        let x0 = if target.x < half { 0 } else { target.x - half };

        let y1 = std::cmp::min(target.y + half + 1, self.height);
        let y0 = if target.y < half { 0 } else { target.y - half };

        Rect { x0, x1, y0, y1 }
    }

    pub fn extract(&self, target: &TargetSpec) -> BitVec {
        let d = self.dims(target);
        let w = d.x1 - d.x0;
        let h = d.y1 - d.y0;

        let mut min = u8::max_value();
        let mut max = u8::min_value();

        for y in d.y0..d.y1 {
            for x in d.x0..d.x1 {
                let (r, g, b, _) = self.at(x, y).unwrap();
                let value = target.channel.extract(r, g, b);
                min = std::cmp::min(min, value);
                max = std::cmp::max(max, value);
            }
        }

        let mul = if max == min {
            1.
        } else {
            255. / (max - min) as f64
        };

        let mut buffer = BitVec::with_capacity(w * h);

        for y in d.y0..d.y1 {
            for x in d.x0..d.x1 {
                let (r, g, b, _) = self.at(x, y).unwrap();
                let value = target.channel.extract(r, g, b) - min;
                let rounded = (value as f64 * mul).round();
                let casted = if rounded > 255. {
                    u8::max_value()
                } else {
                    rounded as u8
                };

                buffer.push(casted >= target.threshold);
            }
        }

        buffer
    }

    fn check(&self, target: &Target) -> bool {
        let mut actual = self.extract(&target.spec);
        actual.xor(&target.mask);
        let distance: usize = actual.blocks().map(BitBlock::count_ones).sum();
        distance < target.spec.count
    }
}

#[derive(Debug)]
enum Msg {
    Scan,
    StopScan,
    Disconnect,
    SetTarget(Target),
    Capture(Capture),
}

#[derive(Debug, Clone)]
pub struct Plug(Sender<Msg>);

impl Plug {
    pub fn scan(&self) {
        async_std::task::block_on(self.0.send(Msg::Scan));
    }

    pub fn stop_scan(&self) {
        async_std::task::block_on(self.0.send(Msg::StopScan));
    }

    pub fn disconnect(&self) {
        async_std::task::block_on(self.0.send(Msg::Disconnect));
    }

    pub fn set_target(&self, target: Target) {
        async_std::task::block_on(self.0.send(Msg::SetTarget(target)));
    }

    pub fn capture(&self, capture: Capture) {
        async_std::task::block_on(self.0.send(Msg::Capture(capture)));
    }
}

pub fn exit(state: &State) {
    async_manager::block_on(async { state.exit.exit().await })
}

pub fn spawn(state: State) -> Result<(Plug, JoinHandle<()>), Error> {
    let (sender, receiver) = async_std::sync::channel(5);

    // Setup a client, and wait until everything is done before exiting.
    let handle = async_std::task::spawn(async move {
        let r = plug(state.clone(), receiver).await;
        state.exit.exit().await;
        if let Err(err) = r {
            eprintln!("{}", err);
            std::process::abort();
        }
    });

    Ok((Plug(sender), handle))
}

async fn plug(state: State, recv: Receiver<Msg>) -> Result<(), Error> {
    let config = Arc::new(Mutex::new(Config::load().await?));
    let vibing = Arc::new(AtomicBool::new(false));

    let connector = ButtplugInProcessClientConnector::new("Healslut Server", 0);
    let server = connector.server_ref();
    server.add_comm_manager::<BtlePlugCommunicationManager>()?;

    let (client, events) =
        ButtplugClient::connect("healslut", connector).await?;

    let res = async_std::task::spawn(handle_events(
        state.clone(),
        state.exit.from(events).await,
    ));

    let pattern_vibing = vibing.clone();
    let pattern_state = state.clone();
    let pattern = async_std::task::spawn(async move {
        let timer = async_std::stream::interval(Duration::from_millis(230));
        let mut timer = pattern_state.exit.from(timer).await;
        let mut on = false;

        while timer.next().await.is_some() {
            let shared = pattern_state.shared.lock().await;

            if let Some(ref active) = shared.active {
                if pattern_vibing.load(Ordering::SeqCst) {
                    if on {
                        active.vibrate(VibrateCommand::Speed(0.3)).await.ok();
                        on = false;
                    } else {
                        active.vibrate(VibrateCommand::Speed(0.8)).await.ok();
                        on = true;
                    }
                } else {
                    active.vibrate(VibrateCommand::Speed(0.0)).await.ok();
                    active.stop().await.ok();
                }
            }
        }
    });

    let from_ui = async_std::task::spawn(async move {
        let mut messages = state.exit.from(recv).await;

        while let Some(msg) = messages.next().await {
            eprintln!("UI -> Plug : {:?}", msg);

            match msg {
                Msg::StopScan => stop_scan(&client).await,
                Msg::Scan => scan(&state, &client).await,
                Msg::Disconnect => {
                    let mut shared = state.shared.lock().await;
                    if let Some(active) = shared.active.take() {
                        active.stop().await.ok();
                        Gui::send(|gui| gui.disconnected());
                        stop_scan(&client).await;
                    }
                }
                Msg::SetTarget(target) => {
                    let mut cfg = config.lock().await;
                    cfg.target = Some(target);
                    cfg.save().await.expect("unable to save config");
                }
                Msg::Capture(capture) => {
                    let cfg = config.lock().await;
                    let mut on = false;
                    if let Some(ref target) = cfg.target {
                        on = capture.check(target);
                    }
                    if on {
                        eprintln!("ON!");
                    }
                    vibing.store(on, Ordering::SeqCst);
                }
            }
        }
    });

    res.await;
    pattern.await;
    from_ui.await;

    Ok(())
}

async fn stop_scan(client: &ButtplugClient) {
    match client.stop_scanning().await {
        Ok(()) => (),
        Err(ButtplugClientError::ButtplugError(
            ButtplugError::ButtplugDeviceError(
                ButtplugDeviceError::DeviceScanningAlreadyStopped,
            ),
        )) => (),
        err => err.unwrap(),
    }

    Gui::send(move |gui| gui.stop_scan());
}

async fn scan(state: &State, client: &ButtplugClient) {
    let mut shared = state.shared.lock().await;

    if shared.active.is_some() {
        return;
    }

    if let Some(device) = client.devices().get(0) {
        shared.active = Some(device.clone());

        let name = device.name.clone();
        Gui::send(move |gui| {
            gui.connected(&name);
        });

        return;
    }

    match client.start_scanning().await {
        Ok(()) => (),
        Err(ButtplugClientError::ButtplugError(
            ButtplugError::ButtplugDeviceError(
                ButtplugDeviceError::DeviceScanningAlreadyStarted,
            ),
        )) => (),
        err => err.unwrap(),
    }

    Gui::send(move |gui| gui.start_scan());
}

async fn handle_events<S>(state: State, mut events: S)
where
    S: Stream<Item = ButtplugClientEvent> + Unpin,
{
    println!("Waiting for events");

    while let Some(event) = events.next().await {
        match event {
            ButtplugClientEvent::Error(e) => {
                eprintln!("Error: {}", e);
            }
            ButtplugClientEvent::DeviceAdded(device) => {
                eprintln!("Device Added: {}", device.name);

                let mut shared = state.shared.lock().await;
                if shared.active.is_none() {
                    let name = device.name.clone();
                    Gui::send(move |gui| {
                        gui.connected(&name);
                    });

                    shared.active = Some(device);
                }
            }
            ButtplugClientEvent::DeviceRemoved(device) => {
                eprintln!("Device Removed: {}", device.device_name);

                let mut shared = state.shared.lock().await;
                let devname = device.device_name.as_str();

                if shared.active.as_ref().map(|x| x.name.as_str())
                    == Some(devname)
                {
                    Gui::send(|gui| gui.disconnected());
                    shared.active = None;
                }
            }
            ButtplugClientEvent::Log(level, msg) => {
                eprintln!("LOG: {:?} {}", level, msg);
            }
            ButtplugClientEvent::ScanningFinished => {
                eprintln!("Scanning Finished");
                Gui::send(move |gui| gui.stop_scan());
            }
            ButtplugClientEvent::PingTimeout => {
                eprintln!("Ping Timeout");
            }
            ButtplugClientEvent::ServerDisconnect => {
                eprintln!("Server Disconnect");
            }
        }
    }

    println!("No more events");
    state.exit.exit().await;
}