~aleteoryx/TabbDE

ref: bdac70b36dd3e17c5f0d7fda49335a5253377dc0 TabbDE/rust/src/state.rs -rw-r--r-- 2.6 KiB
bdac70b3Aleteoryx starting over! 3 days ago
                                                                                
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
use std::collections::HashMap;
use std::ffi::OsString;
use std::os::unix::net::UnixStream;
use std::os::fd::{AsRawFd, RawFd};
use std::process::Command;
use std::sync::{Arc, RwLock};


use smithay::{
  desktop::{PopupManager, Space, Window, WindowSurfaceType},
  input::{Seat, SeatState},
  reexports::{
    wayland_server::{
      backend::{ClientData, ClientId, DisconnectReason},
    },
    calloop::{generic::Generic, EventLoop, Interest, LoopSignal, Mode, PostAction, RegistrationToken}
  },
  wayland::{
    socket::ListeningSocketSource,
  },
};

pub struct TabbDE {
  pub start_time: std::time::Instant,

  pub client_contexts: HashMap<OsString, Arc<ClientContext>>
}

impl TabbDE {
  pub fn new(event_loop: &mut EventLoop<TabbDE>) -> Self {
    let start_time = std::time::Instant::now();

    Self {
      start_time,
      client_contexts: Default::default(),
    }
  }

  pub fn init_subprocess(&mut self, event_loop: &mut EventLoop<TabbDE>, command: &mut Command) {
    let created_time = std::time::Instant::now();

    let listening_socket = ListeningSocketSource::new_auto().unwrap();
    let socket_name = listening_socket.socket_name().to_os_string();
    command.env("WAYLAND_DISPLAY", &socket_name);

    let loop_handle = event_loop.handle();
    let listener_token = loop_handle
      .insert_source(listening_socket, move |client_stream, _, state| {
        let addr = client_stream
          .local_addr()
          .unwrap();
        let path = addr
          .as_pathname()
          .unwrap()
          .as_os_str();

        let fd = client_stream.as_raw_fd();
        let context = ClientConnection {
          created_time: std::time::Instant::now(),
          client_stream
        };

        state
          .client_contexts[path]
          .as_ref()
          .connections
          .write()
          .expect("Couldn't lock state.client_contexts[].connection")
          .insert(fd, context);
      })
      .expect("Failed to init the wayland event source.");

    let context = ClientContext {
      socket_name: socket_name.clone(),
      created_time,

      listener_token,

      connections: Default::default()
    };

    self.client_contexts.insert(socket_name, Arc::new(context));

    command
      .spawn()
      .expect("Couldn't open subprocess");

    command.env_remove("WAYLAND_DISPLAY");
  }
}

pub struct ClientContext {
  pub socket_name: OsString,
  pub created_time: std::time::Instant,

  pub listener_token: RegistrationToken,

  pub connections: RwLock<HashMap<RawFd, ClientConnection>>
}

pub struct ClientConnection {
  pub created_time: std::time::Instant,
  pub client_stream: UnixStream
}