aethermap-gui 1.5.0

GUI client for aethermap input remapper
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use crate::gui::{azeron_keypad_layout, Message, State, Tab};
use aethermap_common::{DeviceCapabilities, DeviceInfo, RemapEntry, RemapProfileInfo};
use iced::Command;

// ---------------------------------------------------------------------------
// Device listing
// ---------------------------------------------------------------------------

pub fn load_devices(state: &mut State) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    state.loading = true;
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.get_devices().await.map_err(|e| e.to_string())
        },
        Message::DevicesLoaded,
    )
}

pub fn devices_loaded(state: &mut State, devices: Vec<DeviceInfo>) -> Command<Message> {
    let count = devices.len();
    state.devices = devices;
    state.loading = false;
    state.add_notification(&format!("Found {} devices", count), false);
    Command::perform(async { Message::LoadMacros }, |msg| msg)
}

pub fn devices_load_error(state: &mut State, e: String) -> Command<Message> {
    state.loading = false;
    state.add_notification(&format!("Error: {}", e), true);
    Command::none()
}

// ---------------------------------------------------------------------------
// Profile save / load (macro profiles)
// ---------------------------------------------------------------------------

pub fn save_profile(state: &mut State) -> Command<Message> {
    if state.profile_name.trim().is_empty() {
        state.add_notification("Enter a profile name", true);
        return Command::none();
    }
    let socket_path = state.socket_path.clone();
    let name = state.profile_name.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.save_profile(&name).await.map_err(|e| e.to_string())
        },
        Message::ProfileSaved,
    )
}

pub fn profile_saved(state: &mut State, name: String, count: usize) -> Command<Message> {
    state.add_notification(&format!("Saved '{}' ({} macros)", name, count), false);
    Command::none()
}

pub fn profile_save_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Save failed: {}", e), true);
    Command::none()
}

pub fn load_profile(state: &mut State) -> Command<Message> {
    if state.profile_name.trim().is_empty() {
        state.add_notification("Enter a profile name to load", true);
        return Command::none();
    }
    let socket_path = state.socket_path.clone();
    let name = state.profile_name.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.load_profile(&name).await.map_err(|e| e.to_string())
        },
        Message::ProfileLoaded,
    )
}

pub fn profile_loaded(state: &mut State, name: String, count: usize) -> Command<Message> {
    state.add_notification(&format!("Loaded '{}' ({} macros)", name, count), false);
    Command::perform(async { Message::LoadMacros }, |msg| msg)
}

pub fn profile_load_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Load failed: {}", e), true);
    Command::none()
}

// ---------------------------------------------------------------------------
// Device grab / ungrab
// ---------------------------------------------------------------------------

pub fn grab_device(state: &State, device_path: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path_clone = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client
                .grab_device(&path_clone)
                .await
                .map(|_| path_clone)
                .map_err(|e| e.to_string())
        },
        Message::DeviceGrabbed,
    )
}

pub fn ungrab_device(state: &State, device_path: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path_clone = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client
                .ungrab_device(&path_clone)
                .await
                .map(|_| path_clone)
                .map_err(|e| e.to_string())
        },
        Message::DeviceUngrabbed,
    )
}

pub fn device_grabbed(state: &mut State, device_path: String) -> Command<Message> {
    state.grabbed_devices.insert(device_path.clone());
    if let Some(idx) = state
        .devices
        .iter()
        .position(|d| d.path.to_string_lossy() == device_path)
    {
        state.selected_device = Some(idx);
    }
    state.add_notification("Device grabbed - ready for recording", false);
    Command::none()
}

pub fn device_grab_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Grab failed: {}", e), true);
    Command::none()
}

pub fn device_ungrabbed(state: &mut State, device_path: String) -> Command<Message> {
    state.grabbed_devices.remove(&device_path);
    state.add_notification("Device released", false);
    Command::none()
}

pub fn device_ungrab_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Release failed: {}", e), true);
    Command::none()
}

// ---------------------------------------------------------------------------
// Device profiles (auto-switch profiles per device)
// ---------------------------------------------------------------------------

pub fn load_device_profiles(state: &State, device_id: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let id = device_id.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            (id.clone(), client.get_device_profiles(id).await)
        },
        |(device_id, result)| {
            Message::DeviceProfilesLoaded(device_id, result.map_err(|e| e.to_string()))
        },
    )
}

pub fn device_profiles_loaded(
    state: &mut State,
    device_id: String,
    profiles: Vec<String>,
) -> Command<Message> {
    state.device_profiles.insert(device_id.clone(), profiles);
    state.add_notification(
        &format!(
            "Loaded {} profiles for {}",
            state
                .device_profiles
                .get(&device_id)
                .map(|p| p.len())
                .unwrap_or(0),
            device_id,
        ),
        false,
    );
    Command::none()
}

pub fn device_profiles_load_error(
    state: &mut State,
    device_id: String,
    e: String,
) -> Command<Message> {
    let _ = device_id;
    state.add_notification(&format!("Failed to load device profiles: {}", e), true);
    Command::none()
}

pub fn activate_profile(
    state: &State,
    device_id: String,
    profile_name: String,
) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let id = device_id.clone();
    let name = profile_name.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.activate_profile(id.clone(), name.clone()).await
        },
        move |result| match result {
            Ok(()) => Message::ProfileActivated(device_id, profile_name),
            Err(e) => Message::ProfileError(format!("Failed to activate profile: {}", e)),
        },
    )
}

pub fn profile_activated(
    state: &mut State,
    device_id: String,
    profile_name: String,
) -> Command<Message> {
    state
        .active_profiles
        .insert(device_id.clone(), profile_name.clone());
    state.add_notification(
        &format!("Activated profile '{}' on {}", profile_name, device_id),
        false,
    );
    Command::none()
}

pub fn deactivate_profile(state: &State, device_id: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let id = device_id.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.deactivate_profile(id.clone()).await
        },
        move |result| match result {
            Ok(()) => Message::ProfileDeactivated(device_id),
            Err(e) => Message::ProfileError(format!("Failed to deactivate profile: {}", e)),
        },
    )
}

pub fn profile_deactivated(state: &mut State, device_id: String) -> Command<Message> {
    state.active_profiles.remove(&device_id);
    state.add_notification(&format!("Deactivated profile on {}", device_id), false);
    Command::none()
}

pub fn profile_error(state: &mut State, msg: String) -> Command<Message> {
    state.add_notification(&msg, true);
    Command::none()
}

// ---------------------------------------------------------------------------
// Remap profiles
// ---------------------------------------------------------------------------

pub fn load_remap_profiles(state: &State, device_path: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            (path.clone(), client.list_remap_profiles(&path).await)
        },
        |(device_path, result)| {
            Message::RemapProfilesLoaded(device_path, result.map_err(|e| e.to_string()))
        },
    )
}

pub fn remap_profiles_loaded(
    state: &mut State,
    device_path: String,
    profiles: Vec<RemapProfileInfo>,
) -> Command<Message> {
    state.remap_profiles.insert(device_path.clone(), profiles);
    state.add_notification(
        &format!(
            "Loaded {} remap profiles for {}",
            state
                .remap_profiles
                .get(&device_path)
                .map(|p| p.len())
                .unwrap_or(0),
            device_path,
        ),
        false,
    );
    Command::none()
}

pub fn remap_profiles_load_error(
    state: &mut State,
    device_path: String,
    e: String,
) -> Command<Message> {
    let _ = device_path;
    state.add_notification(&format!("Failed to load remap profiles: {}", e), true);
    Command::none()
}

pub fn activate_remap_profile(
    state: &State,
    device_path: String,
    profile_name: String,
) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path = device_path.clone();
    let name = profile_name.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.activate_remap_profile(&path, &name).await
        },
        move |result| match result {
            Ok(()) => Message::RemapProfileActivated(device_path, profile_name),
            Err(e) => Message::ProfileError(format!("Failed to activate remap profile: {}", e)),
        },
    )
}

pub fn remap_profile_activated(
    state: &mut State,
    device_path: String,
    profile_name: String,
) -> Command<Message> {
    state
        .active_remap_profiles
        .insert(device_path.clone(), profile_name.clone());
    state.add_notification(
        &format!(
            "Activated remap profile '{}' on {}",
            profile_name, device_path
        ),
        false,
    );
    Command::perform(async move { device_path.clone() }, |path| {
        Message::LoadActiveRemaps(path)
    })
}

pub fn deactivate_remap_profile(state: &State, device_path: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            client.deactivate_remap_profile(&path).await
        },
        move |result| match result {
            Ok(()) => Message::RemapProfileDeactivated(device_path),
            Err(e) => Message::ProfileError(format!("Failed to deactivate remap profile: {}", e)),
        },
    )
}

pub fn remap_profile_deactivated(state: &mut State, device_path: String) -> Command<Message> {
    state.active_remap_profiles.remove(&device_path);
    state.active_remaps.remove(&device_path);
    state.add_notification(
        &format!("Deactivated remap profile on {}", device_path),
        false,
    );
    Command::none()
}

// ---------------------------------------------------------------------------
// Active remaps
// ---------------------------------------------------------------------------

pub fn load_active_remaps(state: &State, device_path: String) -> Command<Message> {
    let socket_path = state.socket_path.clone();
    let path = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            (path.clone(), client.get_active_remaps(&path).await)
        },
        |(device_path, result)| {
            Message::ActiveRemapsLoaded(device_path, result.map_err(|e| e.to_string()))
        },
    )
}

pub fn active_remaps_loaded(
    state: &mut State,
    device_path: String,
    result: Option<(String, Vec<RemapEntry>)>,
) -> Command<Message> {
    match result {
        Some((profile_name, remaps)) => {
            state
                .active_remaps
                .insert(device_path, (profile_name, remaps));
        }
        None => {
            state.active_remaps.remove(&device_path);
        }
    }
    Command::none()
}

pub fn active_remaps_load_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Failed to load active remaps: {}", e), true);
    Command::none()
}

// ---------------------------------------------------------------------------
// Mouse event recording (GUI-side)
// ---------------------------------------------------------------------------

pub fn record_mouse_event(
    state: &mut State,
    event_type: String,
    button: Option<u16>,
    x: i32,
    y: i32,
    delta: i32,
) -> Command<Message> {
    if state.recording {
        let event_desc = match event_type.as_str() {
            "button_press" => format!("Mouse button {} pressed", button.unwrap_or(0)),
            "button_release" => format!("Mouse button {} released", button.unwrap_or(0)),
            "movement" => format!("Mouse moved to ({}, {})", x, y),
            "scroll" => format!("Mouse scrolled {}", delta),
            _ => format!("Unknown mouse event: {}", event_type),
        };
        state.status = event_desc;
    }
    Command::none()
}

// ---------------------------------------------------------------------------
// Keypad view
// ---------------------------------------------------------------------------

pub fn show_keypad_view(state: &mut State, device_path: String) -> Command<Message> {
    if device_path.is_empty() {
        state.device_capabilities = None;
        state.keypad_layout.clear();
        state.keypad_view_device = None;
        state.selected_button = None;
        return Command::none();
    }
    state.keypad_view_device = Some(device_path.clone());
    let socket_path = state.socket_path.clone();
    let path_clone = device_path.clone();
    Command::perform(
        async move {
            let client = crate::ipc::IpcClient::new(socket_path);
            (
                path_clone.clone(),
                client.get_device_capabilities(&path_clone).await,
            )
        },
        |(device_path, result)| {
            Message::DeviceCapabilitiesLoaded(device_path, result.map_err(|e| e.to_string()))
        },
    )
}

pub fn device_capabilities_loaded(
    state: &mut State,
    device_path: String,
    capabilities: DeviceCapabilities,
) -> Command<Message> {
    state.device_capabilities = Some(capabilities);
    state.keypad_layout = azeron_keypad_layout();
    if let Some((profile_name, remaps)) = state.active_remaps.get(&device_path) {
        for remap in remaps {
            if let Some(button) = state
                .keypad_layout
                .iter_mut()
                .find(|b| b.id == remap.from_key)
            {
                button.current_remap = Some(remap.to_key.clone());
            }
        }
        state.add_notification(
            &format!("Loaded remaps from profile '{}'", profile_name),
            false,
        );
    }
    state.active_tab = Tab::Devices;
    Command::none()
}

pub fn device_capabilities_load_error(state: &mut State, e: String) -> Command<Message> {
    state.add_notification(&format!("Failed to load device capabilities: {}", e), true);
    Command::none()
}

pub fn select_keypad_button(state: &mut State, button_id: String) -> Command<Message> {
    state.selected_button = state.keypad_layout.iter().position(|b| b.id == button_id);
    state.status = format!(
        "Selected button: {} - Configure remapping in device profile",
        button_id
    );
    Command::none()
}