aethermapd 1.4.3

Privileged system daemon for aethermap
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
use aethermap_common::{tracing, serialize, deserialize, Request, Response, RemapProfileInfo, RemapEntry, DeviceCapabilities, LayerConfigInfo, LayerMode as CommonLayerMode, LedZone, LedPattern as CommonLedPattern, AnalogCalibrationConfig, AnalogMode as CommonAnalogMode, CameraOutputMode as CommonCameraOutputMode};
use std::path::Path;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{UnixListener, UnixStream};
use tokio::sync::RwLock;
use tokio::task;
use tracing::{debug, error, info, warn};

use crate::macro_engine;
use crate::config;
use crate::injector;
use crate::security;
use crate::layer_manager::{LayerConfig, LayerMode};
use crate::led_controller::LedZone as InternalLedZone;
use crate::auto_profile_switcher::AutoProfileSwitcher;
use crate::analog_calibration::{AnalogCalibration, DeadzoneShape, SensitivityCurve};
use crate::analog_processor::{AnalogMode, CameraOutputMode};

/// Convert IPC config to internal AnalogCalibration
fn config_to_calibration(config: AnalogCalibrationConfig) -> Result<AnalogCalibration, String> {
    // Validate deadzone range
    if config.deadzone < 0.0 || config.deadzone > 1.0 {
        return Err(format!(
            "Deadzone must be between 0.0 and 1.0, got {}",
            config.deadzone
        ));
    }

    // Validate sensitivity multiplier
    if config.sensitivity_multiplier < 0.1 || config.sensitivity_multiplier > 5.0 {
        return Err(format!(
            "Sensitivity multiplier must be between 0.1 and 5.0, got {}",
            config.sensitivity_multiplier
        ));
    }

    // Validate range
    if config.range_min >= config.range_max {
        return Err(format!(
            "Range min ({}) must be less than range max ({})",
            config.range_min, config.range_max
        ));
    }

    let deadzone_shape = match config.deadzone_shape.as_str() {
        "circular" => DeadzoneShape::Circular,
        "square" => DeadzoneShape::Square,
        _ => {
            return Err(format!(
                "Invalid deadzone shape: {}, expected 'circular' or 'square'",
                config.deadzone_shape
            ))
        }
    };

    let sensitivity = match config.sensitivity.as_str() {
        "linear" => SensitivityCurve::Linear,
        "quadratic" => SensitivityCurve::Quadratic,
        "exponential" => SensitivityCurve::Exponential {
            exponent: config.exponent,
        },
        _ => {
            return Err(format!(
                "Invalid sensitivity curve: {}, expected 'linear', 'quadratic', or 'exponential'",
                config.sensitivity
            ))
        }
    };

    Ok(AnalogCalibration {
        deadzone: config.deadzone,
        deadzone_shape,
        sensitivity,
        sensitivity_multiplier: config.sensitivity_multiplier,
        range_min: config.range_min,
        range_max: config.range_max,
        invert_x: config.invert_x,
        invert_y: config.invert_y,
    })
}

/// Convert internal AnalogCalibration to IPC config
fn calibration_to_config(calibration: &AnalogCalibration) -> AnalogCalibrationConfig {
    let (sensitivity, exponent) = match calibration.sensitivity {
        SensitivityCurve::Linear => ("linear".to_string(), 2.0),
        SensitivityCurve::Quadratic => ("quadratic".to_string(), 2.0),
        SensitivityCurve::Exponential { exponent } => ("exponential".to_string(), exponent),
    };

    AnalogCalibrationConfig {
        deadzone: calibration.deadzone,
        deadzone_shape: match calibration.deadzone_shape {
            DeadzoneShape::Circular => "circular".to_string(),
            DeadzoneShape::Square => "square".to_string(),
        },
        sensitivity,
        sensitivity_multiplier: calibration.sensitivity_multiplier,
        range_min: calibration.range_min,
        range_max: calibration.range_max,
        invert_x: calibration.invert_x,
        invert_y: calibration.invert_y,
        exponent,
        analog_mode: aethermap_common::AnalogMode::Disabled,
        camera_output_mode: None,
    }
}

/// Convert common AnalogMode to internal AnalogMode
fn common_to_internal_analog_mode(mode: CommonAnalogMode) -> AnalogMode {
    match mode {
        CommonAnalogMode::Disabled => AnalogMode::Disabled,
        CommonAnalogMode::Dpad => AnalogMode::Dpad,
        CommonAnalogMode::Gamepad => AnalogMode::Gamepad,
        CommonAnalogMode::Camera => AnalogMode::Camera,
        CommonAnalogMode::Mouse => AnalogMode::Mouse,
        CommonAnalogMode::Wasd => AnalogMode::Wasd,
    }
}

/// Convert common CameraOutputMode to internal CameraOutputMode
fn common_to_internal_camera_mode(mode: CommonCameraOutputMode) -> CameraOutputMode {
    match mode {
        CommonCameraOutputMode::Scroll => CameraOutputMode::Scroll,
        CommonCameraOutputMode::Keys => CameraOutputMode::Keys,
    }
}

/// IPC server for handling communication with GUI clients
pub struct IpcServer {
    socket_path: String,
    shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>,
    macro_engine: Option<Arc<macro_engine::MacroEngine>>,
    injector: Option<Arc<RwLock<dyn injector::Injector + Send + Sync>>>,
    security_manager: Option<Arc<RwLock<security::SecurityManager>>>,
    auto_profile_switcher: Option<Arc<AutoProfileSwitcher>>,
}


impl IpcServer {
    /// Create a new IPC server with the specified socket path
    pub fn new<P: AsRef<Path>>(socket_path: P) -> Result<Self, std::io::Error> {
        let path = socket_path.as_ref().to_string_lossy().to_string();

        // Remove any existing socket file
        if Path::new(&path).exists() {
            std::fs::remove_file(&path)?;
        }

        Ok(Self {
            socket_path: path,
            shutdown_tx: None,
            macro_engine: None,
            injector: None,
            security_manager: None,
            auto_profile_switcher: None,
        })
    }

    /// Start the IPC server with the provided daemon state
    pub async fn start(&mut self,
            state: Arc<RwLock<crate::DaemonState>>,
            macro_engine: Arc<macro_engine::MacroEngine>,
            injector: Arc<RwLock<dyn injector::Injector + Send + Sync>>,
            config_manager: Arc<config::ConfigManager>,
            security_manager: Arc<RwLock<security::SecurityManager>>,
            auto_profile_switcher: Option<Arc<AutoProfileSwitcher>>
        ) -> Result<(), Box<dyn std::error::Error>> {
        info!("Starting IPC server at {}", self.socket_path);

        // Store references to macro engine and injector
        self.macro_engine = Some(macro_engine);
        self.injector = Some(injector.clone());
        self.security_manager = Some(security_manager.clone());
        self.auto_profile_switcher = auto_profile_switcher;

        // Create Unix listener
        let listener = UnixListener::bind(&self.socket_path)?;

        // Set socket permissions using the security manager
        {
            let security = security_manager.read().await;
            if let Err(e) = security.set_socket_permissions(&self.socket_path) {
                warn!("Failed to set socket permissions: {}", e);
                // Continue anyway, as the daemon should still work even if permissions aren't ideal
            }
        }

        // Create shutdown channel
        let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();
        self.shutdown_tx = Some(shutdown_tx);

        // Spawn the main server loop
        let _state_clone = Arc::clone(&state);
        // Clone references before moving into task
        let macro_engine = self.macro_engine.as_ref().unwrap().clone();
        let injector = self.injector.as_ref().unwrap().clone();
        let auto_profile_switcher = self.auto_profile_switcher.clone();

        task::spawn(async move {
            loop {
                tokio::select! {
                    // Accept new connections
                    connection = listener.accept() => {
                        match connection {
                            Ok((stream, _)) => {
                                debug!("New client connected");
                                let state = Arc::clone(&state);
                                let macro_engine = Arc::clone(&macro_engine);
                                let injector = Arc::clone(&injector);
                                let config_manager = Arc::clone(&config_manager);
                                let security_manager = Arc::clone(&security_manager);
                                let switcher = auto_profile_switcher.clone();
                                task::spawn(async move {
                                    if let Err(e) = handle_client(
                                        stream,
                                        state,
                                        macro_engine,
                                        injector,
                                        config_manager,
                                        security_manager,
                                        switcher
                                    ).await {
                                        error!("Error handling client: {}", e);
                                    }
                                });
                            }
                            Err(e) => {
                                error!("Error accepting connection: {}", e);
                            }
                        }
                    }
                    // Handle shutdown signal
                    _ = &mut shutdown_rx => {
                        info!("Shutting down IPC server");
                        break;
                    }
                }
            }
        });

        // Note: We can't modify self.executing here because we're in a spawned task
        // In a real implementation, we would use a channel or other communication method
        debug!("Macro execution completed");
        Ok(())
    }

    /// Shutdown the IPC server
    pub async fn shutdown(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        info!("Shutting down IPC server");

        // Send shutdown signal to the server task
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(());
        }

        // Remove the socket file
        if Path::new(&self.socket_path).exists() {
            std::fs::remove_file(&self.socket_path)?;
        }

        Ok(())
    }
}

/// Handle a client connection
pub async fn handle_client(
    mut stream: UnixStream,
    state: Arc<RwLock<crate::DaemonState>>,
    macro_engine: Arc<macro_engine::MacroEngine>,
    injector: Arc<RwLock<dyn injector::Injector + Send + Sync>>,
    config_manager: Arc<config::ConfigManager>,
    security_manager: Arc<RwLock<security::SecurityManager>>,
    auto_profile_switcher: Option<Arc<AutoProfileSwitcher>>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Set a reasonable timeout for operations
    // Note: set_keepalive is not available on UnixStream in this version of tokio
    // stream.set_keepalive(Some(std::time::Duration::from_secs(30)))?;

    // Read message length first
    let mut len_buf = [0u8; 4];
    stream.read_exact(&mut len_buf).await?;
    let msg_len = u32::from_le_bytes(len_buf) as usize;

    // Validate message length to prevent excessive memory usage
    if msg_len > 1024 * 1024 { // 1MB max message size
        warn!("Received oversized message: {} bytes", msg_len);
        return Err("Message too large".into());
    }

    // Read the actual message
    let mut msg_buf = vec![0u8; msg_len];
    stream.read_exact(&mut msg_buf).await?;

    // Deserialize the request
    let request: Request = deserialize(&msg_buf)?;
    debug!("Received request: {:?}", request);

    // Check authentication if token auth is enabled
    let auth_required = cfg!(feature = "token-auth");

    if auth_required {
        // Handle authentication request
        if let Request::Authenticate { token } = &request {
            let security = security_manager.read().await;
            if security.validate_auth_token(token).await {
                debug!("Authentication successful");
                let response = Response::Authenticated;
                let response_bytes = serialize(&response);

                // Send the response length first
                let len = response_bytes.len() as u32;
                stream.write_all(&len.to_le_bytes()).await?;

                // Send the response
                stream.write_all(&response_bytes).await?;
                stream.flush().await?;

                return Ok(());
            } else {
                debug!("Authentication failed");
                let response = Response::Error("Invalid authentication token".to_string());
                let response_bytes = serialize(&response);

                // Send the response length first
                let len = response_bytes.len() as u32;
                stream.write_all(&len.to_le_bytes()).await?;

                // Send the response
                stream.write_all(&response_bytes).await?;
                stream.flush().await?;

                return Ok(());
            }
        }
        // Allow GenerateToken without authentication
        else if !matches!(request, Request::GenerateToken { .. }) {
            debug!("Authentication required but not provided");
            let response = Response::Error("Authentication required".to_string());
            let response_bytes = serialize(&response);

            // Send the response length first
            let len = response_bytes.len() as u32;
            stream.write_all(&len.to_le_bytes()).await?;

            // Send the response
            stream.write_all(&response_bytes).await?;
            stream.flush().await?;

            return Ok(());
        }
    }

    // Process the request and generate a response
    let response = handle_request(
        request,
        Arc::clone(&state),
        Arc::clone(&macro_engine),
        Arc::clone(&injector),
        Arc::clone(&config_manager),
        Arc::clone(&security_manager),
        auto_profile_switcher
    ).await;
    debug!("Sending response: {:?}", response);

    // Serialize the response
    let response_bytes = serialize(&response);

    // Send the response length first
    let len = response_bytes.len() as u32;
    stream.write_all(&len.to_le_bytes()).await?;

    // Send the response
    stream.write_all(&response_bytes).await?;
    stream.flush().await?;

    Ok(())
}

/// Process a request and generate a response
async fn handle_request(
    request: Request,
    state: Arc<RwLock<crate::DaemonState>>,
    macro_engine: Arc<macro_engine::MacroEngine>,
    _injector: Arc<RwLock<dyn injector::Injector + Send + Sync>>,
    config_manager: Arc<config::ConfigManager>,
    security_manager: Arc<RwLock<security::SecurityManager>>,
    auto_profile_switcher: Option<Arc<AutoProfileSwitcher>>,
) -> Response {
    match request {
        Request::GenerateToken { client_id } => {
            debug!("Generating token for client: {}", client_id);
            let security = security_manager.read().await;
            match security.generate_auth_token().await {
                Ok(token) => Response::Token(token),
                Err(e) => Response::Error(format!("Failed to generate token: {}", e)),
            }
        }
        Request::Authenticate { token } => {
            let security = security_manager.read().await;
            if security.validate_auth_token(&token).await {
                Response::Authenticated
            } else {
                Response::Error("Invalid authentication token".to_string())
            }
        }
        Request::GetDevices => {
            let state = state.read().await;
            let devices = state.devices.lock().unwrap().clone();
            Response::Devices(devices)
        }
        Request::ListMacros => {
            let state = state.read().await;
            let macros = state.macros.lock().unwrap().values().cloned().collect();
            return Response::Macros(macros);
        }
        Request::SetMacro { device_path, macro_entry } => {
            let state = state.write().await;

            // Check if the device exists
            let devices = state.devices.lock().unwrap();
            let device_exists = devices.iter().any(|d| d.path.to_string_lossy() == device_path);
            if !device_exists {
                return Response::Error(format!("Device not found: {}", device_path));
            }

            // Add or update the macro
            let mut macros = state.macros.lock().unwrap();
            macros.insert(macro_entry.name.clone(), macro_entry);

            return Response::Ack;
        }
        Request::DeleteMacro { name } => {
            let state = state.write().await;

            // Find and remove the macro
            let mut macros = state.macros.lock().unwrap();
            let original_len = macros.len();
            macros.remove(&name);

            if macros.len() == original_len {
                return Response::Error(format!("Macro not found: {}", name));
            } else {
                return Response::Ack;
            }
        }
        Request::ReloadConfig => {
            // This would trigger a config reload in a real implementation
            info!("Config reload requested");
            return Response::Ack;
        }
        Request::LedSet { device_path, color } => {
            // This would set LED colors in a real implementation
            info!("LED set request for {}: {:?}", device_path, color);
            return Response::Ack;
        }
        Request::RecordMacro { device_path, name, capture_mouse } => {
            // Start macro recording
            match macro_engine.start_recording(name.clone(), device_path.clone(), capture_mouse).await.map_err(|e| format!("Failed to start recording: {}", e)) {
                Ok(_) => {
                    info!("Macro recording started for {} on {} (capture_mouse: {})", name, device_path, capture_mouse);

                    // Store the recording in the daemon state for access by input event handlers
                    let mut state = state.write().await;
                    state.active_recording = Some((name.clone(), device_path.clone()));

                    return Response::RecordingStarted { device_path, name };
                }
                Err(e) => {
                    error!("Failed to start recording: {}", e);
                    return Response::Error(format!("Failed to start recording: {}", e));
                }
            }
        }
        Request::StopRecording => {
            // Stop macro recording
            match macro_engine.stop_recording().await.map_err(|e| format!("Failed to stop recording: {}", e)) {
                Ok(Some(macro_entry)) => {
                    let macro_name = macro_entry.name.clone();
                    info!("Macro recording stopped: {}", macro_name);

                    // Update the daemon state to remove the active recording
                    let mut state = state.write().await;
                    state.active_recording = None;

                    // Add the macro to the daemon state
                    let mut macros = state.macros.lock().unwrap();
                    macros.insert(macro_entry.name.clone(), macro_entry.clone());
                    drop(macros);

                    return Response::RecordingStopped { macro_entry };
                }
                Ok(None) => {
                    // Update the daemon state to remove the active recording
                    let mut state = state.write().await;
                    state.active_recording = None;

                    return Response::Error("Recording stopped but no macro was created".to_string());
                }
                Err(e) => {
                    error!("Failed to stop recording: {}", e);
                    return Response::Error(format!("Failed to stop recording: {}", e));
                }
            }
        }
        Request::TestMacro { name } => {
            // Test macro execution
            info!("Test macro execution requested: {}", name);
            // Get the macro to execute
            let macro_to_execute = {
                let macros = macro_engine.list_macros().await;
                macros.into_iter().find(|m| m.name == name)
            };

            match macro_to_execute {
                Some(macro_entry) => {
                    // Execute macro using macro engine
                    debug!("Macro execution requested: {}", macro_entry.name);
                    match macro_engine.execute_macro(macro_entry.clone()).await {
                        Ok(_) => {
                            info!("Successfully executed macro: {}", macro_entry.name);
                            Response::Ack
                        }
                        Err(e) => {
                            error!("Failed to execute macro '{}': {}", macro_entry.name, e);
                            Response::Error(format!("Failed to execute macro '{}': {}", macro_entry.name, e))
                        }
                    }
                }
                None => {
                    error!("Macro not found: {}", name);
                    Response::Error(format!("Macro not found: {}", name))
                }
            }
        }
        Request::ExecuteMacro { name } => {
            // Execute macro by name
            info!("Execute macro requested: {}", name);
            // Get the macro to execute
            let macro_to_execute = {
                let macros = macro_engine.list_macros().await;
                macros.into_iter().find(|m| m.name == name)
            };

            match macro_to_execute {
                Some(macro_entry) => {
                    // Use execute_macro method instead of manually executing actions
                    match macro_engine.execute_macro(macro_entry.clone()).await {
                        Ok(_) => {
                            info!("Successfully executed macro: {}", macro_entry.name);
                            Response::Ack
                        }
                        Err(e) => {
                            error!("Failed to execute macro '{}': {}", macro_entry.name, e);
                            Response::Error(format!("Failed to execute macro '{}': {}", macro_entry.name, e))
                        }
                    }
                }
                None => {
                    error!("Macro not found: {}", name);
                    Response::Error(format!("Macro not found: {}", name))
                }
            }
        }
        Request::GetStatus => {
            let state = state.read().await;
            let devices_count = state.devices.lock().unwrap().len();
            let macros_count = state.macros.lock().unwrap().len();
            return Response::Status {
                version: "0.1.0".to_string(),
                uptime_seconds: 0, // Would be calculated in real implementation
                devices_count,
                macros_count,
            };
        }
        Request::SaveProfile { name } => {
            // Save current macros as a profile
            let macros_count = {
                let state_guard = state.read().await;
                let macros = state_guard.macros.lock().unwrap();
                let count = macros.len();
                drop(macros);
                drop(state_guard);
                count
            };

            match config_manager.save_current_macros_as_profile(&name).await {
                Ok(_) => {
                    info!("Profile {} saved", name);
                    return Response::ProfileSaved {
                        name,
                        macros_count,
                    };
                }
                Err(e) => {
                    error!("Failed to save profile: {}", e);
                    return Response::Error(format!("Failed to save profile: {}", e));
                }
            }
        }
        Request::LoadProfile { name } => {
            // Load a profile
            match config_manager.load_profile(&name).await {
                Ok(profile) => {
                    info!("Profile {} loaded", name);
                    return Response::ProfileLoaded {
                        name,
                        macros_count: profile.macros.len()
                    };
                }
                Err(e) => {
                    error!("Failed to load profile: {}", e);
                    return Response::Error(format!("Failed to load profile: {}", e));
                }
            }
        }
        Request::ListProfiles => {
            // List available profiles
            match config_manager.list_profiles().await {
                Ok(profiles) => return Response::Profiles(profiles),
                Err(e) => {
                    error!("Failed to list profiles: {}", e);
                    return Response::Error(format!("Failed to list profiles: {}", e));
                }
            }
        }
        Request::DeleteProfile { name } => {
            // Delete a profile
            match config_manager.delete_profile(&name).await {
                Ok(_) => {
                    info!("Profile {} deleted", name);
                    return Response::Ack;
                }
                Err(e) => {
                    error!("Failed to delete profile: {}", e);
                    return Response::Error(format!("Failed to delete profile: {}", e));
                }
            }
        }
        Request::GrabDevice { device_path } => {
            // Grab a device exclusively for input interception
            let state = state.read().await;
            if let Some(device_manager) = &state.device_manager {
                let mut dm = device_manager.write().await;
                match dm.grab_device(&device_path).await {
                    Ok(_) => {
                        info!("Device {} grabbed successfully", device_path);
                        return Response::Ack;
                    }
                    Err(e) => {
                        error!("Failed to grab device {}: {}", device_path, e);
                        return Response::Error(format!("Failed to grab device: {}", e));
                    }
                }
            } else {
                return Response::Error("Device manager not initialized".to_string());
            }
        }
        Request::UngrabDevice { device_path } => {
            // Release exclusive access to a device
            let state = state.read().await;
            if let Some(device_manager) = &state.device_manager {
                let mut dm = device_manager.write().await;
                match dm.ungrab_device(&device_path).await {
                    Ok(_) => {
                        info!("Device {} ungrabbed successfully", device_path);
                        return Response::Ack;
                    }
                    Err(e) => {
                        error!("Failed to ungrab device {}: {}", device_path, e);
                        return Response::Error(format!("Failed to ungrab device: {}", e));
                    }
                }
            } else {
                return Response::Error("Device manager not initialized".to_string());
            }
        }
        Request::GetDeviceProfiles { device_id } => {
            debug!("GetDeviceProfiles request for device {}", device_id);

            let profiles = config_manager.list_device_profiles(&device_id).await;
            Response::DeviceProfiles {
                device_id: device_id.clone(),
                profiles,
            }
        }
        Request::ActivateProfile { device_id, profile_name } => {
            info!("ActivateProfile request: device={}, profile={}", device_id, profile_name);

            // Set manual override to prevent auto-switching until next focus change
            if let Some(switcher) = &auto_profile_switcher {
                switcher.set_manual_override(&device_id).await;
                debug!("Set manual override for device {} after manual profile activation", device_id);
            }

            // Get the profile from config
            match config_manager.get_device_profile(&device_id, &profile_name).await {
                Some(profile) => {
                    // Clone the device_manager Arc to avoid holding state lock
                    let device_manager_opt = {
                        let state = state.read().await;
                        state.device_manager.clone()
                    };

                    if let Some(device_manager) = device_manager_opt {
                        // Find the device path by device_id using the helper method
                        let device_path = {
                            let dm = device_manager.read().await;
                            dm.get_device_path_by_id(&device_id)
                        };

                        if let Some(path) = device_path {
                            let mut dm = device_manager.write().await;
                            match dm.activate_profile(&path, profile).await {
                                Ok(()) => {
                                    info!("Profile {} activated for device {}", profile_name, device_id);
                                    Response::ProfileActivated {
                                        device_id,
                                        profile_name,
                                    }
                                }
                                Err(e) => {
                                    error!("Failed to activate profile: {}", e);
                                    Response::Error(format!("Failed to activate profile: {}", e))
                                }
                            }
                        } else {
                            // Device not grabbed, but profile is valid - store for when it is grabbed
                            info!("Device {} not currently grabbed, profile will activate on grab", device_id);
                            Response::ProfileActivated {
                                device_id,
                                profile_name,
                            }
                        }
                    } else {
                        Response::Error("Device manager not initialized".to_string())
                    }
                }
                None => {
                    warn!("Profile {} not found for device {}", profile_name, device_id);
                    Response::Error(format!(
                        "Profile '{}' not found for device '{}'. Available profiles: {:?}",
                        profile_name,
                        device_id,
                        config_manager.list_device_profiles(&device_id).await
                    ))
                }
            }
        }
        Request::DeactivateProfile { device_id } => {
            info!("DeactivateProfile request for device {}", device_id);

            // Clone the device_manager Arc to avoid holding state lock
            let device_manager_opt = {
                let state = state.read().await;
                state.device_manager.clone()
            };

            if let Some(device_manager) = device_manager_opt {
                // Find the device path by device_id using the helper method
                let device_path = {
                    let dm = device_manager.read().await;
                    dm.get_device_path_by_id(&device_id)
                };

                if let Some(path) = device_path {
                    let mut dm = device_manager.write().await;
                    match dm.deactivate_profile(&path).await {
                        Ok(()) => {
                            info!("Profile deactivated for device {}", device_id);
                            Response::ProfileDeactivated { device_id }
                        }
                        Err(e) => {
                            error!("Failed to deactivate profile: {}", e);
                            Response::Error(format!("Failed to deactivate profile: {}", e))
                        }
                    }
                } else {
                    Response::ProfileDeactivated { device_id }
                }
            } else {
                Response::Error("Device manager not initialized".to_string())
            }
        }
        Request::GetActiveProfile { device_id } => {
            debug!("GetActiveProfile request for device {}", device_id);

            let state = state.read().await;
            if let Some(device_manager) = &state.device_manager {
                let dm = device_manager.read().await;
                let profile_name = dm.get_active_profile_by_id(&device_id);
                Response::ActiveProfile {
                    device_id,
                    profile_name,
                }
            } else {
                Response::Error("Device manager not initialized".to_string())
            }
        }
        Request::GetActiveRemaps { device_path } => {
            debug!("GetActiveRemaps request for {}", device_path);

            let state_guard = state.read().await;
            let device_manager = match state_guard.device_manager.as_ref() {
                Some(dm) => dm,
                None => return Response::Error("Device manager not initialized".to_string()),
            };

            let dm = device_manager.read().await;

            match dm.get_active_remaps(&device_path).await {
                Ok(Some((profile_name, remappings))) => {
                    // Read the remappings table
                    use crate::remap_engine::RemapTable;
                    let remaps_guard: tokio::sync::RwLockReadGuard<'_, RemapTable> = remappings.read().await;
                    // Convert remappings to Vec<RemapEntry>
                    let entries: Vec<RemapEntry> = remaps_guard.iter()
                        .map(|(from, to)| RemapEntry {
                            from_key: format!("{:?}", from),
                            to_key: format!("{:?}", to),
                        })
                        .collect();
                    drop(remaps_guard);

                    Response::ActiveRemaps {
                        device_path,
                        profile_name: Some(profile_name),
                        remaps: entries,
                    }
                }
                Ok(None) => {
                    Response::ActiveRemaps {
                        device_path,
                        profile_name: None,
                        remaps: vec![],
                    }
                }
                Err(e) => Response::Error(format!("Failed to get active remaps: {}", e)),
            }
        }
        Request::ListRemapProfiles { device_path } => {
            debug!("ListRemapProfiles request for {}", device_path);

            let state_guard = state.read().await;
            let device_manager = match state_guard.device_manager.as_ref() {
                Some(dm) => dm,
                None => return Response::Error("Device manager not initialized".to_string()),
            };

            let dm = device_manager.read().await;

            // Get device info to determine device_id
            let device_info = match dm.get_device_info_from_path(&device_path) {
                Some(info) => info,
                None => {
                    return Response::Error(format!("Device not found: {}", device_path));
                }
            };

            let device_id = crate::device::DeviceManager::format_device_id(
                device_info.vendor_id,
                device_info.product_id
            );

            // Get profiles from DeviceManager's stored profiles
            match dm.get_device_profiles(&device_id) {
                Some(profiles) => {
                    // Need to collect remap counts asynchronously
                    let mut profile_infos = Vec::new();
                    for p in profiles {
                        let remap_count = p.remap_count().await;
                        profile_infos.push(RemapProfileInfo {
                            name: p.name().to_string(),
                            description: None, // Profiles don't have descriptions yet
                            remap_count,
                        });
                    }

                    Response::RemapProfiles {
                        device_path,
                        profiles: profile_infos,
                    }
                }
                None => Response::RemapProfiles {
                    device_path,
                    profiles: vec![],
                },
            }
        }
        Request::ActivateRemapProfile { device_path, profile_name } => {
            info!("ActivateRemapProfile request: device={}, profile={}", device_path, profile_name);

            let state_guard = state.read().await;
            let device_manager = match state_guard.device_manager.as_ref() {
                Some(dm) => dm,
                None => return Response::Error("Device manager not initialized".to_string()),
            };

            let mut dm = device_manager.write().await;

            match dm.activate_profile_by_name(&device_path, &profile_name).await {
                Ok(()) => {
                    info!("Activated profile '{}' for device {}", profile_name, device_path);
                    Response::RemapProfileActivated {
                        device_path,
                        profile_name,
                    }
                }
                Err(e) => {
                    error!("Failed to activate profile: {}", e);
                    Response::Error(format!("Failed to activate profile: {}", e))
                }
            }
        }
        Request::DeactivateRemapProfile { device_path } => {
            info!("DeactivateRemapProfile request for {}", device_path);

            let state_guard = state.read().await;
            let device_manager = match state_guard.device_manager.as_ref() {
                Some(dm) => dm,
                None => return Response::Error("Device manager not initialized".to_string()),
            };

            let mut dm = device_manager.write().await;

            match dm.deactivate_profile(&device_path).await {
                Ok(()) => {
                    info!("Deactivated profile for device {}", device_path);
                    Response::RemapProfileDeactivated {
                        device_path,
                    }
                }
                Err(e) => {
                    error!("Failed to deactivate profile: {}", e);
                    Response::Error(format!("Failed to deactivate profile: {}", e))
                }
            }
        }
        Request::GetDeviceCapabilities { device_path } => {
            info!("GetDeviceCapabilities request for {}", device_path);

            let state_guard = state.read().await;

            // First, try to get device info from DeviceManager
            let device_info = if let Some(device_manager) = &state_guard.device_manager {
                let dm = device_manager.read().await;
                dm.get_device(&device_path)
            } else {
                // Fallback: try from state.devices
                let devices = state_guard.devices.lock().unwrap();
                devices.iter().find(|d| d.path.to_string_lossy() == device_path).cloned()
            };

            match device_info {
                Some(info) => {
                    // Detect capabilities from device
                    let capabilities = detect_device_capabilities(&info);

                    debug!("Device capabilities for {}: analog={}, hat={}, buttons={}",
                           device_path, capabilities.has_analog_stick,
                           capabilities.has_hat_switch, capabilities.joystick_button_count);

                    Response::DeviceCapabilities {
                        device_path,
                        capabilities,
                    }
                }
                None => {
                    warn!("Device not found: {}", device_path);
                    Response::Error(format!("Device not found: {}", device_path))
                }
            }
        }
        Request::GetActiveLayer { device_id } => {
            debug!("GetActiveLayer request for device {}", device_id);

            let state = state.read().await;
            let layer_manager = state.layer_manager.read().await;
            let effective_layer = layer_manager.get_effective_layer(&device_id).await;

            // Get layer name from DeviceLayerState
            let layer_name = if let Some(device_state) = layer_manager.get_device_state(&device_id).await {
                device_state.layer_configs.iter()
                    .find(|c| c.layer_id == effective_layer)
                    .map(|c| c.name.clone())
                    .unwrap_or_else(|| format!("Layer {}", effective_layer))
            } else {
                format!("Layer {}", effective_layer)
            };

            Response::ActiveLayer {
                device_id,
                layer_id: effective_layer,
                layer_name,
            }
        }
        Request::SetLayerConfig { device_id, layer_id, config } => {
            info!("SetLayerConfig request: device={}, layer={}, name={}", device_id, layer_id, config.name);

            let mut state = state.write().await;
            let mut layer_manager = state.layer_manager.write().await;

            // Convert LayerConfigInfo to LayerConfig for internal use
            let internal_mode = match config.mode {
                CommonLayerMode::Hold => LayerMode::Hold,
                CommonLayerMode::Toggle => LayerMode::Toggle,
            };

            // Convert common LedZone to internal LedZone
            let internal_led_zone = config.led_zone.map(|zone| match zone {
                aethermap_common::LedZone::Side => crate::led_controller::LedZone::Side,
                aethermap_common::LedZone::Logo => crate::led_controller::LedZone::Logo,
                aethermap_common::LedZone::Keys => crate::led_controller::LedZone::Keys,
                aethermap_common::LedZone::Thumbstick => crate::led_controller::LedZone::Thumbstick,
                aethermap_common::LedZone::All => crate::led_controller::LedZone::All,
                aethermap_common::LedZone::Global => crate::led_controller::LedZone::Global,
            });

            // Preserve existing analog_mode when updating layer config
            let existing_mode = layer_manager.get_device_state(&device_id).await
                .and_then(|state| {
                    state.layer_configs.iter()
                        .find(|lc| lc.layer_id == layer_id)
                        .map(|lc| lc.analog_mode)
                });

            let layer_config = LayerConfig {
                layer_id: config.layer_id,
                name: config.name.clone(),
                remaps: std::collections::HashMap::new(), // Remaps set separately via RemapEngine
                mode: internal_mode,
                led_color: config.led_color,
                led_zone: internal_led_zone,
                analog_calibration: None,  // Calibrations set separately via config system
                analog_mode: existing_mode.unwrap_or(AnalogMode::Disabled),  // Preserve existing or default
                camera_output_mode: CameraOutputMode::Scroll,  // Default to scroll for camera mode
            };

            // Apply config to layer_manager
            match layer_manager.set_layer_config(&device_id, layer_id, layer_config).await {
                Ok(_) => Response::LayerConfigured { device_id, layer_id },
                Err(e) => Response::Error(format!("Failed to set layer config: {}", e)),
            }
        }
        Request::ActivateLayer { device_id, layer_id, mode } => {
            info!("ActivateLayer request: device={}, layer={}, mode={:?}", device_id, layer_id, mode);

            let mut state = state.write().await;
            let mut layer_manager = state.layer_manager.write().await;

            let result = match mode {
                CommonLayerMode::Hold => layer_manager.activate_hold_layer(&device_id, layer_id).await,
                CommonLayerMode::Toggle => {
                    layer_manager.toggle_layer(&device_id, layer_id).await.map(|_| ())
                }
            };

            match result {
                Ok(_) => Response::LayerConfigured { device_id, layer_id },
                Err(e) => Response::Error(format!("Failed to activate layer: {}", e)),
            }
        }
        Request::ListLayers { device_id } => {
            debug!("ListLayers request for device {}", device_id);

            let state = state.read().await;
            let layer_manager = state.layer_manager.read().await;

            let layers = layer_manager.get_device_state(&device_id).await
                .map(|s| {
                    s.layer_configs.iter().map(|c| {
                        let ipc_mode = match c.mode {
                            LayerMode::Hold => CommonLayerMode::Hold,
                            LayerMode::Toggle => CommonLayerMode::Toggle,
                        };
                        // Convert internal LedZone to common LedZone
                        let ipc_led_zone = c.led_zone.map(|zone| match zone {
                            crate::led_controller::LedZone::Side => aethermap_common::LedZone::Side,
                            crate::led_controller::LedZone::Logo => aethermap_common::LedZone::Logo,
                            crate::led_controller::LedZone::Keys => aethermap_common::LedZone::Keys,
                            crate::led_controller::LedZone::Thumbstick => aethermap_common::LedZone::Thumbstick,
                            crate::led_controller::LedZone::All => aethermap_common::LedZone::All,
                            crate::led_controller::LedZone::Global => aethermap_common::LedZone::Global,
                            crate::led_controller::LedZone::Unknown(_) => aethermap_common::LedZone::Side, // Default to side for unknown
                        });
                        LayerConfigInfo {
                            layer_id: c.layer_id,
                            name: c.name.clone(),
                            mode: ipc_mode,
                            remap_count: c.remaps.len(),
                            led_color: c.led_color,
                            led_zone: ipc_led_zone,
                        }
                    }).collect()
                })
                .unwrap_or_default();

            Response::LayerList { device_id, layers }
        }
        Request::SetAnalogSensitivity { device_id, sensitivity } => {
            info!("SetAnalogSensitivity request: device={}, sensitivity={:.2}", device_id, sensitivity);

            // Validate sensitivity range
            if sensitivity < 0.1 || sensitivity > 5.0 {
                warn!("Invalid sensitivity value: {:.2} (valid range: 0.1-5.0)", sensitivity);
                return Response::Error(format!(
                    "Invalid sensitivity value: {:.2}. Valid range is 0.1-5.0",
                    sensitivity
                ));
            }

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Set sensitivity
                analog_processor.set_sensitivity(&device_id, sensitivity).await;

                Response::AnalogSensitivitySet {
                    device_id,
                    sensitivity,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::GetAnalogSensitivity { device_id } => {
            debug!("GetAnalogSensitivity request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Get device config
                match analog_processor.get_device_config(&device_id).await {
                    Some(config) => Response::AnalogSensitivity {
                        device_id,
                        sensitivity: config.sensitivity,
                    },
                    None => {
                        // Device not configured - return default
                        Response::AnalogSensitivity {
                            device_id,
                            sensitivity: 1.0,
                        }
                    }
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::SetAnalogResponseCurve { device_id, curve } => {
            debug!("SetAnalogResponseCurve request for device {}: {}", device_id, curve);

            // Parse curve string
            let response_curve = match curve.as_str() {
                "linear" => Ok(crate::analog_processor::ResponseCurve::Linear),
                s if s.starts_with("exponential") => {
                    // Parse exponent: "exponential" or "exponential(2.0)"
                    if s == "exponential" {
                        Ok(crate::analog_processor::ResponseCurve::Exponential { exponent: 2.0 })
                    } else {
                        // Extract exponent from "exponential(<number>)"
                        let start = s.find('(').unwrap_or(0);
                        let end = s.find(')').unwrap_or(s.len());
                        if start > 0 && end > start {
                            match s[start + 1..end].parse::<f32>() {
                                Ok(exp) => Ok(crate::analog_processor::ResponseCurve::Exponential { exponent: exp }),
                                Err(_) => Err(format!("Invalid exponent value: {}", &s[start + 1..end])),
                            }
                        } else {
                            Ok(crate::analog_processor::ResponseCurve::Exponential { exponent: 2.0 })
                        }
                    }
                }
                _ => Err(format!("Invalid curve type: {}. Use 'linear' or 'exponential'", curve)),
            };

            let response_curve = match response_curve {
                Ok(curve) => curve,
                Err(e) => return Response::Error(e),
            };

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                analog_processor.set_response_curve(&device_id, response_curve).await;
                // Return curve string in response for confirmation
                Response::AnalogResponseCurveSet {
                    device_id,
                    curve,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::GetAnalogResponseCurve { device_id } => {
            debug!("GetAnalogResponseCurve request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                match analog_processor.get_device_config(&device_id).await {
                    Some(config) => {
                        let curve_str = match config.response_curve {
                            crate::analog_processor::ResponseCurve::Linear => "linear".to_string(),
                            crate::analog_processor::ResponseCurve::Exponential { exponent } => {
                                if exponent == 2.0 {
                                    "exponential".to_string()
                                } else {
                                    format!("exponential({})", exponent)
                                }
                            }
                        };
                        Response::AnalogResponseCurve {
                            device_id,
                            curve: curve_str,
                        }
                    }
                    None => Response::Error(format!("Device {} not configured", device_id)),
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::SetAnalogDeadzone { device_id, percentage } => {
            info!("SetAnalogDeadzone request: device={}, percentage={}", device_id, percentage);

            // Validate percentage range
            if percentage > 100 {
                warn!("Invalid deadzone percentage: {} (valid range: 0-100)", percentage);
                return Response::Error(format!(
                    "Invalid deadzone percentage: {}. Valid range is 0-100",
                    percentage
                ));
            }

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Set deadzone percentage
                match analog_processor.set_deadzone_percentage(&device_id, percentage).await {
                    Ok(_) => Response::AnalogDeadzoneSet {
                        device_id,
                        percentage,
                    },
                    Err(e) => Response::Error(format!("Failed to set deadzone: {}", e)),
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::GetAnalogDeadzone { device_id } => {
            debug!("GetAnalogDeadzone request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Get deadzone percentage
                let percentage = analog_processor.get_deadzone_percentage(&device_id).await;
                Response::AnalogDeadzone {
                    device_id,
                    percentage,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::SetAnalogDeadzoneXY { device_id, x_percentage, y_percentage } => {
            info!("SetAnalogDeadzoneXY request: device={}, x={}%, y={}%", device_id, x_percentage, y_percentage);

            // Validate percentage ranges
            if x_percentage > 100 || y_percentage > 100 {
                warn!("Invalid deadzone percentage: x={}, y={} (valid range: 0-100)", x_percentage, y_percentage);
                return Response::Error(format!(
                    "Invalid deadzone percentage: x={}, y={}. Valid range is 0-100",
                    x_percentage, y_percentage
                ));
            }

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Set X and Y deadzone percentages
                let result_x = analog_processor.set_deadzone_percentage_x(&device_id, x_percentage).await;
                let result_y = analog_processor.set_deadzone_percentage_y(&device_id, y_percentage).await;

                match (result_x, result_y) {
                    (Ok(_), Ok(_)) => Response::AnalogDeadzoneXYSet {
                        device_id,
                        x_percentage,
                        y_percentage,
                    },
                    (Err(e), _) => Response::Error(format!("Failed to set X-axis deadzone: {}", e)),
                    (_, Err(e)) => Response::Error(format!("Failed to set Y-axis deadzone: {}", e)),
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::GetAnalogDeadzoneXY { device_id } => {
            debug!("GetAnalogDeadzoneXY request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Get X and Y deadzone percentages
                let x_percentage = analog_processor.get_deadzone_percentage_x(&device_id).await;
                let y_percentage = analog_processor.get_deadzone_percentage_y(&device_id).await;
                Response::AnalogDeadzoneXY {
                    device_id,
                    x_percentage,
                    y_percentage,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::SetAnalogOuterDeadzoneXY { device_id, x_percentage, y_percentage } => {
            info!("SetAnalogOuterDeadzoneXY request: device={}, x={}%, y={}%", device_id, x_percentage, y_percentage);

            // Validate percentage ranges
            if x_percentage > 100 || y_percentage > 100 {
                warn!("Invalid outer deadzone percentage: x={}, y={} (valid range: 0-100)", x_percentage, y_percentage);
                return Response::Error(format!(
                    "Invalid outer deadzone percentage: x={}, y={}. Valid range is 0-100",
                    x_percentage, y_percentage
                ));
            }

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Convert percentages to raw values
                let x_raw = (x_percentage as u32 * crate::analog_processor::MAX_ABS_VALUE as u32 / 100) as u16;
                let y_raw = (y_percentage as u32 * crate::analog_processor::MAX_ABS_VALUE as u32 / 100) as u16;

                // Set outer deadzones via AnalogProcessor methods
                analog_processor.set_outer_deadzone_x(&device_id, x_raw).await;
                analog_processor.set_outer_deadzone_y(&device_id, y_raw).await;

                info!("Outer deadzones updated: device={}, x={}%, y={}%", device_id, x_percentage, y_percentage);
                Response::AnalogOuterDeadzoneXYSet {
                    device_id,
                    x_percentage,
                    y_percentage,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }
        Request::GetAnalogOuterDeadzoneXY { device_id } => {
            debug!("GetAnalogOuterDeadzoneXY request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Get X and Y outer deadzone percentages
                let x_percentage = analog_processor.get_outer_deadzone_percentage_x(&device_id).await;
                let y_percentage = analog_processor.get_outer_deadzone_percentage_y(&device_id).await;
                Response::AnalogOuterDeadzoneXY {
                    device_id,
                    x_percentage,
                    y_percentage,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }

        Request::SetAnalogDpadMode { device_id, mode } => {
            info!("SetAnalogDpadMode request: device={}, mode={}", device_id, mode);

            // Parse mode string to DpadMode
            let dpad_mode = match mode.as_str() {
                "disabled" => crate::analog_processor::DpadMode::Disabled,
                "eight_way" => crate::analog_processor::DpadMode::EightWay,
                "four_way" => crate::analog_processor::DpadMode::FourWay,
                _ => {
                    warn!("Invalid D-pad mode: {} (valid: disabled, eight_way, four_way)", mode);
                    return Response::Error(format!(
                        "Invalid D-pad mode: {}. Valid modes are: disabled, eight_way, four_way",
                        mode
                    ));
                }
            };

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Set D-pad mode
                analog_processor.set_dpad_mode(&device_id, dpad_mode).await;
                Response::AnalogDpadModeSet {
                    device_id,
                    mode,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }

        Request::GetAnalogDpadMode { device_id } => {
            debug!("GetAnalogDpadMode request for device {}", device_id);

            // Get analog processor from state
            let state = state.read().await;
            if let Some(analog_processor) = &state.analog_processor {
                // Get D-pad mode
                let dpad_mode = analog_processor.get_dpad_mode(&device_id).await;
                let mode_str = match dpad_mode {
                    crate::analog_processor::DpadMode::Disabled => "disabled".to_string(),
                    crate::analog_processor::DpadMode::EightWay => "eight_way".to_string(),
                    crate::analog_processor::DpadMode::FourWay => "four_way".to_string(),
                };
                Response::AnalogDpadMode {
                    device_id,
                    mode: mode_str,
                }
            } else {
                Response::Error("Analog processor not initialized".to_string())
            }
        }

        Request::GetAnalogCalibration { device_id, layer_id } => {
            debug!("GetAnalogCalibration request: device={}, layer={}", device_id, layer_id);

            // Get calibration from config manager
            let calibration = config_manager.get_analog_calibration(&device_id, layer_id).await;
            let config = calibration.as_ref().map(calibration_to_config);

            Response::AnalogCalibration {
                device_id,
                layer_id,
                calibration: config,
            }
        }

        Request::SetAnalogCalibration { device_id, layer_id, calibration } => {
            info!(
                "SetAnalogCalibration request: device={}, layer={}, deadzone={}, sensitivity={}, curve={}, mode={:?}",
                device_id,
                layer_id,
                calibration.deadzone,
                calibration.sensitivity_multiplier,
                calibration.sensitivity,
                calibration.analog_mode
            );

            // Extract mode settings from calibration (convert from common types to internal)
            let layer_mode = common_to_internal_analog_mode(calibration.analog_mode);
            let camera_mode = calibration.camera_output_mode
                .map(common_to_internal_camera_mode)
                .unwrap_or(CameraOutputMode::Scroll);

            // Convert IPC config to internal calibration
            match config_to_calibration(calibration.clone()) {
                Ok(cal) => {
                    // Validate the calibration
                    if let Err(e) = cal.validate() {
                        warn!("Invalid calibration: {}", e);
                        return Response::Error(format!("Invalid calibration: {}", e));
                    }

                    // Update the analog processor's calibration
                    {
                        let state_guard = state.read().await;
                        if let Some(analog_processor) = &state_guard.analog_processor {
                            analog_processor.set_calibration(&device_id, layer_id, cal.clone()).await;
                        } else {
                            return Response::Error("Analog processor not initialized".to_string());
                        }
                    } // Release read lock

                    // Save to config
                    if let Err(e) = config_manager.save_analog_calibration(&device_id, layer_id, cal).await {
                        warn!("Failed to save analog calibration: {}", e);
                        return Response::Error(format!("Failed to save calibration: {}", e));
                    }

                    // Also update analog_mode in layer config
                    let state_guard = state.write().await;
                    let mut layer_manager = state_guard.layer_manager.write().await;

                    // Get existing config or create default
                    let device_state = layer_manager.get_device_state(&device_id).await;
                    let existing_config = device_state.as_ref().and_then(|s| s.get_layer_config(layer_id));

                    let mut layer_config = existing_config.cloned().unwrap_or_else(|| {
                        LayerConfig::new(
                            layer_id,
                            format!("Layer {}", layer_id),
                            LayerMode::Hold,
                        )
                    });

                    // Update mode from calibration
                    layer_config.analog_mode = layer_mode;
                    if layer_mode == AnalogMode::Camera {
                        layer_config.camera_output_mode = camera_mode;
                    }

                    // Apply the updated config
                    match layer_manager.set_layer_config(&device_id, layer_id, layer_config).await {
                        Ok(_) => Response::AnalogCalibrationAck,
                        Err(e) => Response::Error(format!("Failed to set analog mode: {}", e)),
                    }
                }
                Err(e) => Response::Error(format!("Invalid calibration: {}", e)),
            }
        }

        Request::SubscribeAnalogInput { device_id } => {
            debug!("SubscribeAnalogInput request: device={}", device_id);

            // Create channel for this subscription
            let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
            let state_guard = state.write().await;
            let mut subscribers = state_guard.analog_subscribers.write().await;
            subscribers.entry(device_id.clone()).or_default().push(tx);

            Response::AnalogInputSubscribed
        }

        Request::UnsubscribeAnalogInput { device_id } => {
            debug!("UnsubscribeAnalogInput request: device={}", device_id);

            let state_guard = state.write().await;
            let mut subscribers = state_guard.analog_subscribers.write().await;
            subscribers.remove(&device_id);

            Response::Ack
        }

        Request::SetMacroSettings(settings) => {
            info!("SetMacroSettings request: latency_offset_ms={}, jitter_pct={:.2}",
                  settings.latency_offset_ms, settings.jitter_pct);

            // Update in macro engine
            macro_engine.set_macro_settings(settings.clone()).await;

            // Update in config manager for persistence
            {
                let mut config = config_manager.config.write().await;
                config.macro_engine.latency_offset_ms = settings.latency_offset_ms;
                config.macro_engine.jitter_pct = settings.jitter_pct;
            }

            if let Err(e) = config_manager.save_config().await {
                warn!("Failed to save config after updating macro settings: {}", e);
            }

            Response::Ack
        }

        Request::GetMacroSettings => {
            debug!("GetMacroSettings request");
            let settings = macro_engine.get_macro_settings().await;
            Response::MacroSettings(settings)
        }

        Request::SetLedColor { device_id, zone, red, green, blue } => {
            info!("SetLedColor request: device={}, zone={:?}, RGB=({}, {}, {})", device_id, zone, red, green, blue);

            // Validate RGB range (0-255) - enforced by u8 type, but double-check
            // This is redundant since u8 can't exceed 255, but kept for clarity
            let state_guard = state.read().await;
            if let Some(led_controller) = &state_guard.led_controller {
                // Convert IPC LedZone to internal LedZone
                let internal_zone = match zone {
                    LedZone::Side => InternalLedZone::Side,
                    LedZone::Logo => InternalLedZone::Logo,
                    LedZone::Keys => InternalLedZone::Keys,
                    LedZone::Thumbstick => InternalLedZone::Thumbstick,
                    LedZone::All => InternalLedZone::All,
                    LedZone::Global => InternalLedZone::Global,
                };

                match led_controller.set_zone_color(internal_zone, red, green, blue).await {
                    Ok(_) => {
                        // Update DaemonState.led_state for hotplug persistence
                        drop(state_guard); // Release read lock before write
                        let mut state_guard = state.write().await;

                        // Update or create LED state entry for this device
                        let device_led_state = state_guard.led_state.read().await;
                        if let Some(led_state) = device_led_state.get(&device_id) {
                            // Clone and update
                            let mut updated = led_state.clone();
                            updated.zone_colors.insert(internal_zone, (red, green, blue));

                            // Write back
                            drop(device_led_state);
                            let mut device_led_state = state_guard.led_state.write().await;
                            device_led_state.insert(device_id.clone(), updated);
                        } else {
                            // Create new entry
                            let mut new_state = crate::led_controller::DeviceLedState::default();
                            new_state.zone_colors.insert(internal_zone, (red, green, blue));

                            drop(state_guard.led_state.read().await);
                            let mut device_led_state = state_guard.led_state.write().await;
                            device_led_state.insert(device_id.clone(), new_state);
                        }

                        Response::LedColorSet {
                            device_id,
                            zone,
                            color: (red, green, blue),
                        }
                    },
                    Err(e) => Response::Error(format!("Failed to set LED color: {}", e)),
                }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::GetLedColor { device_id, zone } => {
            debug!("GetLedColor request: device={}, zone={:?}", device_id, zone);

            let state = state.read().await;
            if let Some(led_controller) = &state.led_controller {
                // Convert IPC LedZone to internal LedZone
                let internal_zone = match zone {
                    LedZone::Side => InternalLedZone::Side,
                    LedZone::Logo => InternalLedZone::Logo,
                    LedZone::Keys => InternalLedZone::Keys,
                    LedZone::Thumbstick => InternalLedZone::Thumbstick,
                    LedZone::All => InternalLedZone::All,
                    LedZone::Global => InternalLedZone::Global,
                };

                let color = led_controller.get_zone_color(internal_zone).await;
                Response::LedColor { device_id, zone, color }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::GetAllLedColors { device_id } => {
            debug!("GetAllLedColors request: device={}", device_id);

            let state = state.read().await;
            if let Some(led_controller) = &state.led_controller {
                let internal_colors = led_controller.get_all_zone_colors().await;

                // Convert internal LedZone keys to IPC LedZone
                let mut colors = std::collections::HashMap::new();
                for (internal_zone, color) in internal_colors {
                    let ipc_zone = match internal_zone {
                        InternalLedZone::Side => LedZone::Side,
                        InternalLedZone::Logo => LedZone::Logo,
                        InternalLedZone::Keys => LedZone::Keys,
                        InternalLedZone::Thumbstick => LedZone::Thumbstick,
                        InternalLedZone::All => LedZone::All,
                        InternalLedZone::Global => LedZone::Global,
                        InternalLedZone::Unknown(_) => continue, // Skip unknown zones
                    };
                    colors.insert(ipc_zone, color);
                }

                Response::AllLedColors { device_id, colors }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::SetLedBrightness { device_id, zone, brightness } => {
            info!("SetLedBrightness request: device={}, zone={:?}, brightness={}", device_id, zone, brightness);

            // Validate brightness range (0-100)
            if brightness > 100 {
                warn!("Invalid brightness value: {} (valid range: 0-100)", brightness);
                return Response::Error(format!(
                    "Invalid brightness value: {}. Valid range is 0-100",
                    brightness
                ));
            }

            let state_guard = state.read().await;
            if let Some(led_controller) = &state_guard.led_controller {
                // Use set_global_brightness for None zone, set_zone_brightness for specific zone
                let result = match zone {
                    Some(ipc_zone) => {
                        // Convert IPC LedZone to internal LedZone
                        let internal_zone = match ipc_zone {
                            LedZone::Side => InternalLedZone::Side,
                    LedZone::Logo => InternalLedZone::Logo,
                            LedZone::Keys => InternalLedZone::Keys,
                            LedZone::Thumbstick => InternalLedZone::Thumbstick,
                            LedZone::All => InternalLedZone::All,
                            LedZone::Global => InternalLedZone::Global,
                        };
                        led_controller.set_zone_brightness(internal_zone, brightness).await
                    }
                    None => led_controller.set_global_brightness(brightness).await,
                };

                match result {
                    Ok(_) => {
                        // Update DaemonState.led_state for hotplug persistence
                        drop(state_guard); // Release read lock before write
                        let mut state_guard = state.write().await;

                        // Update or create LED state entry for this device
                        let device_led_state = state_guard.led_state.read().await;
                        if let Some(led_state) = device_led_state.get(&device_id) {
                            // Clone and update
                            let mut updated = led_state.clone();

                            // Update brightness based on zone
                            match zone {
                                Some(ipc_zone) => {
                                    let internal_zone = match ipc_zone {
                                        LedZone::Side => InternalLedZone::Side,
                    LedZone::Logo => InternalLedZone::Logo,
                                        LedZone::Keys => InternalLedZone::Keys,
                                        LedZone::Thumbstick => InternalLedZone::Thumbstick,
                                        LedZone::All => InternalLedZone::All,
                                        LedZone::Global => InternalLedZone::Global,
                                    };
                                    updated.zone_brightness.insert(internal_zone, brightness);
                                }
                                None => {
                                    updated.global_brightness = brightness;
                                }
                            }

                            // Write back
                            drop(device_led_state);
                            let mut device_led_state = state_guard.led_state.write().await;
                            device_led_state.insert(device_id.clone(), updated);
                        } else {
                            // Create new entry
                            let mut new_state = crate::led_controller::DeviceLedState::default();

                            match zone {
                                Some(ipc_zone) => {
                                    let internal_zone = match ipc_zone {
                                        LedZone::Side => InternalLedZone::Side,
                    LedZone::Logo => InternalLedZone::Logo,
                                        LedZone::Keys => InternalLedZone::Keys,
                                        LedZone::Thumbstick => InternalLedZone::Thumbstick,
                                        LedZone::All => InternalLedZone::All,
                                        LedZone::Global => InternalLedZone::Global,
                                    };
                                    new_state.zone_brightness.insert(internal_zone, brightness);
                                }
                                None => {
                                    new_state.global_brightness = brightness;
                                }
                            }

                            drop(state_guard.led_state.read().await);
                            let mut device_led_state = state_guard.led_state.write().await;
                            device_led_state.insert(device_id.clone(), new_state);
                        }

                        Response::LedBrightnessSet {
                            device_id,
                            zone,
                            brightness,
                        }
                    },
                    Err(e) => Response::Error(format!("Failed to set LED brightness: {}", e)),
                }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::GetLedBrightness { device_id, zone } => {
            debug!("GetLedBrightness request: device={}, zone={:?}", device_id, zone);

            let state = state.read().await;
            if let Some(led_controller) = &state.led_controller {
                // Get brightness for specific zone or global
                let brightness = match zone {
                    Some(ipc_zone) => {
                        // Convert IPC LedZone to internal LedZone
                        let internal_zone = match ipc_zone {
                            LedZone::Side => InternalLedZone::Side,
                            LedZone::Logo => InternalLedZone::Logo,
                            LedZone::Keys => InternalLedZone::Keys,
                            LedZone::Thumbstick => InternalLedZone::Thumbstick,
                            LedZone::All => InternalLedZone::All,
                            LedZone::Global => InternalLedZone::Global,
                        };
                        led_controller.get_zone_brightness(internal_zone).await
                    }
                    None => led_controller.get_global_brightness().await,
                };

                Response::LedBrightness {
                    device_id,
                    zone,
                    brightness,
                }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::SetLedPattern { device_id, pattern } => {
            info!("SetLedPattern request: device={}, pattern={:?}", device_id, pattern);

            let state = state.read().await;
            if let Some(led_controller) = &state.led_controller {
                // Convert IPC LedPattern to internal LedPattern
                let internal_pattern = match pattern {
                    CommonLedPattern::Static => crate::led_controller::LedPattern::Static,
                    CommonLedPattern::Breathing => crate::led_controller::LedPattern::Breathing,
                    CommonLedPattern::Rainbow => crate::led_controller::LedPattern::Rainbow,
                    CommonLedPattern::RainbowWave => crate::led_controller::LedPattern::RainbowWave,
                };

                match led_controller.set_pattern(internal_pattern).await {
                    Ok(_) => Response::LedPatternSet {
                        device_id,
                        pattern,
                    },
                    Err(e) => Response::Error(format!("Failed to set LED pattern: {}", e)),
                }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::GetLedPattern { device_id } => {
            debug!("GetLedPattern request: device={}", device_id);

            let state = state.read().await;
            if let Some(led_controller) = &state.led_controller {
                let internal_pattern = led_controller.get_pattern().await;
                // Convert internal LedPattern to IPC LedPattern
                let pattern = match internal_pattern {
                    crate::led_controller::LedPattern::Static => CommonLedPattern::Static,
                    crate::led_controller::LedPattern::Breathing => CommonLedPattern::Breathing,
                    crate::led_controller::LedPattern::Rainbow => CommonLedPattern::Rainbow,
                    crate::led_controller::LedPattern::RainbowWave => CommonLedPattern::RainbowWave,
                };

                Response::LedPattern {
                    device_id,
                    pattern,
                }
            } else {
                Response::Error("LED controller not available".to_string())
            }
        }
        Request::FocusChanged { app_id, window_title } => {
            debug!("Focus changed: app_id={}, window_title={:?}", app_id, window_title);

            // Apply focus-based profile switching via AutoProfileSwitcher
            if let Some(switcher) = &auto_profile_switcher {
                if let Err(e) = switcher.apply_focus_change(&app_id, &window_title).await {
                    warn!("Failed to apply focus change: {}", e);
                    Response::Error(format!("Failed to apply focus change: {}", e))
                } else {
                    Response::FocusChangedAck { app_id }
                }
            } else {
                // AutoProfileSwitcher not configured - acknowledge but no switching
                debug!("AutoProfileSwitcher not configured, skipping profile switch");
                Response::FocusChangedAck { app_id }
            }
        }

        Request::RegisterHotkey { device_id, binding } => {
            debug!("RegisterHotkey: device={}, key={:?}, profile={}",
                device_id, binding.key, binding.profile_name);

            // Validate profile exists
            let profiles = config_manager.device_profiles.read().await;
            let profile_exists = profiles.get(&device_id)
                .and_then(|device_profiles| device_profiles.get(&binding.profile_name))
                .is_some();
            drop(profiles);

            if !profile_exists {
                warn!("RegisterHotkey: profile '{}' not found for device {}",
                    binding.profile_name, device_id);
                return Response::Error(format!("Profile '{}' not found", binding.profile_name));
            }

            // Convert from common::HotkeyBinding to config::HotkeyBinding
            let internal_binding = crate::config::HotkeyBinding {
                modifiers: binding.modifiers.clone(),
                key: binding.key.clone(),
                profile_name: binding.profile_name.clone(),
                device_id: binding.device_id.clone(),
                layer_id: binding.layer_id,
            };

            // Add to config
            match config_manager.add_hotkey_binding(&device_id, internal_binding).await {
                Ok(()) => {
                    // Reload hotkey manager
                    let state = state.read().await;
                    if let Some(device_manager) = &state.device_manager {
                        let dm = device_manager.read().await;
                        if let Some(hotkey_manager) = dm.hotkey_manager() {
                            if let Err(e) = hotkey_manager.lock().await.load_bindings().await {
                                warn!("Failed to reload hotkey bindings: {}", e);
                            }
                        }
                    }

                    info!("Registered hotkey: {}+{:?} for device {}",
                        binding.modifiers.join("+"), binding.key, device_id);
                    Response::HotkeyRegistered {
                        device_id,
                        key: binding.key,
                        modifiers: binding.modifiers,
                    }
                }
                Err(e) => {
                    warn!("RegisterHotkey failed: {}", e);
                    Response::Error(format!("Failed to register hotkey: {}", e))
                }
            }
        }

        Request::ListHotkeys { device_id } => {
            debug!("ListHotkeys: device={}", device_id);

            match config_manager.get_hotkey_bindings(&device_id).await {
                Ok(bindings) => {
                    // Convert from config::HotkeyBinding to common::HotkeyBinding
                    let common_bindings: Vec<aethermap_common::HotkeyBinding> = bindings.into_iter()
                        .map(|b| aethermap_common::HotkeyBinding {
                            modifiers: b.modifiers,
                            key: b.key,
                            profile_name: b.profile_name,
                            device_id: b.device_id,
                            layer_id: b.layer_id,
                        })
                        .collect();

                    debug!("Returning {} hotkey bindings for device {}",
                        common_bindings.len(), device_id);
                    Response::HotkeyList {
                        device_id,
                        bindings: common_bindings,
                    }
                }
                Err(e) => {
                    warn!("ListHotkeys failed: {}", e);
                    Response::HotkeyList {
                        device_id,
                        bindings: Vec::new(),
                    }
                }
            }
        }

        Request::RemoveHotkey { device_id, key, modifiers } => {
            debug!("RemoveHotkey: device={}, key={}, modifiers={:?}",
                device_id, key, modifiers);

            match config_manager.remove_hotkey_binding(&device_id, &key, &modifiers).await {
                Ok(()) => {
                    // Reload hotkey manager
                    let state = state.read().await;
                    if let Some(device_manager) = &state.device_manager {
                        let dm = device_manager.read().await;
                        if let Some(hotkey_manager) = dm.hotkey_manager() {
                            if let Err(e) = hotkey_manager.lock().await.load_bindings().await {
                                warn!("Failed to reload hotkey bindings: {}", e);
                            }
                        }
                    }

                    info!("Removed hotkey: {}+{:?} for device {}",
                        modifiers.join("+"), key, device_id);
                    Response::HotkeyRemoved {
                        device_id,
                        key,
                        modifiers,
                    }
                }
                Err(e) => {
                    warn!("RemoveHotkey failed: {}", e);
                    Response::Error(format!("Failed to remove hotkey: {}", e))
                }
            }
        }
        Request::SetAutoSwitchRules { rules } => {
            info!("SetAutoSwitchRules request: {} rules", rules.len());

            // Keep a reference to rules for reloading the switcher
            let rules_count = rules.len();

            // Convert from common::AutoSwitchRule to config::AutoSwitchRule
            let internal_rules: Vec<crate::config::AutoSwitchRule> = rules.into_iter()
                .map(|r| crate::config::AutoSwitchRule {
                    app_id: r.app_id,
                    profile_name: r.profile_name,
                    device_id: r.device_id,
                    layer_id: r.layer_id,
                })
                .collect();

            // Clone for switcher reload
            let switcher_rules = internal_rules.clone();

            match config_manager.set_auto_switch_rules(internal_rules).await {
                Ok(()) => {
                    // Reload auto-profile switcher with new rules
                    if let Some(switcher) = auto_profile_switcher.as_ref() {
                        switcher.reload_rules(switcher_rules).await;
                        info!("Reloaded auto-profile switcher with {} rules", rules_count);
                    }

                    info!("Saved {} auto-switch rules", rules_count);
                    Response::AutoSwitchRulesAck
                }
                Err(e) => {
                    warn!("SetAutoSwitchRules failed: {}", e);
                    Response::Error(format!("Failed to save auto-switch rules: {}", e))
                }
            }
        }

        Request::GetAutoSwitchRules => {
            // Get all auto-switch rules from config
            let rules = config_manager.get_auto_switch_rules().await;

            // Convert internal AutoSwitchRule to IPC AutoSwitchRule
            let ipc_rules = rules
                .into_iter()
                .map(|r| aethermap_common::AutoSwitchRule {
                    app_id: r.app_id,
                    profile_name: r.profile_name,
                    device_id: r.device_id,
                    layer_id: r.layer_id,
                })
                .collect();

            Response::AutoSwitchRules { rules: ipc_rules }
        }
    }
}

/// Broadcast analog input update to all subscribers for a device
///
/// This function sends real-time analog stick position updates to all
/// subscribed GUI clients. It's called from the analog event processing
/// loop when analog input changes.
///
/// # Arguments
///
/// * `state` - The daemon state containing subscriber information
/// * `device_id` - Device identifier (vendor:product format)
/// * `axis_x` - Normalized X axis value (-1.0 to 1.0)
/// * `axis_y` - Normalized Y axis value (-1.0 to 1.0)
///
/// # Note
///
/// This broadcasts the RAW input values before calibration, so the GUI
/// shows actual stick position. The deadzone overlay in visualizer shows
/// how calibration will affect it.
pub async fn broadcast_analog_input(
    state: &Arc<RwLock<crate::DaemonState>>,
    device_id: &str,
    axis_x: f32,
    axis_y: f32,
) {
    use aethermap_common::Response;

    let update = Response::AnalogInputUpdate {
        device_id: device_id.to_string(),
        axis_x,
        axis_y,
    };

    // Get subscribers map and clone the Vec of senders for this device
    let state_guard = state.read().await;
    let subscribers = state_guard.analog_subscribers.read().await;
    if let Some(clients) = subscribers.get(device_id) {
        for sender in clients {
            let _ = sender.send(update.clone());
        }
    }
}

/// Detect device capabilities from DeviceInfo
///
/// This function infers device capabilities based on device type.
/// For more accurate detection, the device would need to be grabbed
/// and queried directly.
///
/// # Arguments
///
/// * `device_info` - The device information from discovery
///
/// # Returns
///
/// DeviceCapabilities struct with detected features
fn detect_device_capabilities(
    device_info: &aethermap_common::DeviceInfo,
) -> DeviceCapabilities {
    use aethermap_common::DeviceType;

    // Infer analog stick from device type
    // Keypads (like Azeron) and gamepads typically have analog sticks
    let has_analog = matches!(device_info.device_type, DeviceType::Keypad | DeviceType::Gamepad);

    // Azeron keypads have hat switches (detected via DeviceType::Keypad)
    let has_hat_switch = matches!(device_info.device_type, DeviceType::Keypad);

    // Button count: Azeron Cybo has 26 joystick buttons
    // For other devices, we use reasonable defaults
    let joystick_button_count = match device_info.device_type {
        DeviceType::Keypad => 26,  // Azeron Cybo
        DeviceType::Gamepad => 12,  // Generic gamepad
        _ => 0,
    };

    // LED zones: defer to Phase 12 (LED Control), empty for now
    let led_zones = Vec::new();

    DeviceCapabilities {
        has_analog_stick: has_analog,
        has_hat_switch,
        joystick_button_count,
        led_zones,
    }
}


/// Get the GID for a group name
#[cfg(target_os = "linux")]
fn get_group_gid(group_name: &str) -> Option<u32> {
    // Simplified implementation for now
    // In a real implementation, this would use libc or nix to resolve group names
    match group_name {
        "root" => Some(0),
        "input" => Some(1001), // Common GID for input group
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DaemonState;
    use aethermap_common::{DeviceInfo, MacroEntry, KeyCombo, Action};
    use std::path::PathBuf;
    use std::sync::Arc;
    use tempfile::TempDir;

    // Helper function to create a test injector or skip the test if permissions are insufficient
    fn create_test_injector() -> Arc<RwLock<dyn injector::Injector + Send + Sync>> {
        match injector::UinputInjector::new() {
            Ok(injector) => Arc::new(RwLock::new(injector)),
            Err(_) => {
                // Skip test if we don't have permission to create injector
                panic!("Test requires root access to create UinputInjector. Run with sudo or set CAP_SYS_ADMIN capability.");
            }
        }
    }

    // Helper function to create a test ConfigManager with temporary paths
    async fn create_test_config_manager() -> Arc<config::ConfigManager> {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.yaml");
        let macros_path = temp_dir.path().join("macros.yaml");
        let cache_path = temp_dir.path().join("macros.bin");
        let profiles_dir = temp_dir.path().join("profiles");
        let remaps_path = temp_dir.path().join("remaps.yaml");
        let device_profiles_path = temp_dir.path().join("device_profiles.yaml");

        let manager = config::ConfigManager {
            config_path,
            macros_path,
            cache_path,
            profiles_dir,
            remaps_path,
            device_profiles_path,
            layer_state_path: temp_dir.path().join("layer_state.yaml"),
            config: Arc::new(RwLock::new(config::DaemonConfig::default())),
            macros: Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new())),
            profiles: Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new())),
            remaps: Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new())),
            device_profiles: Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new())),
        };

        Arc::new(manager)
    }

    #[tokio::test]
    async fn test_ipc_server_creation() {
        let temp_dir = TempDir::new().unwrap();
        let socket_path = temp_dir.path().join("test.sock");

        let server = IpcServer::new(&socket_path).unwrap();
        assert_eq!(server.socket_path, socket_path.to_string_lossy());
    }

    #[tokio::test]
    async fn test_request_handling() {
        let state = Arc::new(RwLock::new(DaemonState::new()));
        let macro_engine = Arc::new(macro_engine::MacroEngine::new());
        let injector = create_test_injector();
        let config_manager = create_test_config_manager().await;
        let security_manager = Arc::new(RwLock::new(security::SecurityManager::new(false)));

        // Test GetDevices request
        let response = handle_request(Request::GetDevices, Arc::clone(&state), Arc::clone(&macro_engine), Arc::clone(&injector), Arc::clone(&config_manager), Arc::clone(&security_manager), None).await;
        assert!(matches!(response, Response::Devices(_)));

        // Test GetStatus request
        let response = handle_request(Request::GetStatus, Arc::clone(&state), Arc::clone(&macro_engine), Arc::clone(&injector), Arc::clone(&config_manager), Arc::clone(&security_manager), None).await;
        match response {
            Response::Status { version, .. } => assert_eq!(version, "0.1.0"),
            _ => panic!("Expected Status response"),
        }

        // Test SetMacro request with non-existent device
        let test_macro = MacroEntry {
            name: "test".to_string(),
            trigger: KeyCombo {
                keys: vec![30],
                modifiers: vec![],
            },
            actions: vec![Action::KeyPress(30)],
            device_id: None,
            enabled: true,
            humanize: false,
            capture_mouse: false,
        };

        let response = handle_request(
            Request::SetMacro {
                device_path: "/nonexistent".to_string(),
                macro_entry: test_macro,
            },
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::clone(&config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        match response {
            Response::Error(msg) => assert!(msg.contains("not found")),
            _ => panic!("Expected Error response"),
        }
    }

    #[tokio::test]
    async fn test_macro_addition() {
        // Create test state
        let state = Arc::new(RwLock::new(DaemonState::new()));
        let macro_engine = Arc::new(macro_engine::MacroEngine::new());
        let injector = create_test_injector();
        let config_manager = create_test_config_manager().await;
        let security_manager = Arc::new(RwLock::new(security::SecurityManager::new(false)));

        // Add a device first
        {
            let state = state.write().await;
            state.devices.lock().unwrap().push(DeviceInfo {
                name: "Test Device".to_string(),
                path: PathBuf::from("/dev/input/test"),
                vendor_id: 0x1234,
                product_id: 0x5678,
                phys: "test-phys".to_string(),
                device_type: aethermap_common::DeviceType::Other,
            });
        }

        // Now add a macro
        let test_macro = MacroEntry {
            name: "test".to_string(),
            trigger: KeyCombo {
                keys: vec![30],
                modifiers: vec![],
            },
            actions: vec![Action::KeyPress(30)],
            device_id: None,
            enabled: true,
            humanize: false,
            capture_mouse: false,
        };

        let response = handle_request(
            Request::SetMacro {
                device_path: "/dev/input/test".to_string(),
                macro_entry: test_macro.clone(),
            },
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::clone(&config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        assert!(matches!(response, Response::Ack));

        // Verify the macro was added
        let state = state.read().await;
        assert_eq!(state.macros.lock().unwrap().len(), 1);
        let macros = state.macros.lock().unwrap();
        let first_macro = macros.values().next().unwrap();
        assert_eq!(first_macro.name, test_macro.name);
    }

    #[tokio::test]
    async fn test_profile_activation_ipc_flow() {
        use crate::config::ConfigManager;
        use crate::device::DeviceManager;
        use crate::remap_engine::RemapProfile;
        use std::collections::HashMap;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create a config manager with test paths
        let config_manager = ConfigManager {
            config_path: temp_dir.path().join("config.yaml"),
            macros_path: temp_dir.path().join("macros.yaml"),
            cache_path: temp_dir.path().join("macros.bin"),
            profiles_dir: temp_dir.path().join("profiles"),
            remaps_path: temp_dir.path().join("remaps.yaml"),
            device_profiles_path: temp_dir.path().join("device_profiles.yaml"),
                layer_state_path: std::path::PathBuf::from("/tmp/layer_state.yaml"),
            config: Default::default(),
            macros: Arc::new(RwLock::new(HashMap::new())),
            profiles: Arc::new(RwLock::new(HashMap::new())),
            remaps: Arc::new(RwLock::new(HashMap::new())),
            device_profiles: Arc::new(RwLock::new(HashMap::new())),
        };

        // Create a test profile directly in storage
        let mut config = HashMap::new();
        config.insert("capslock".to_string(), "leftctrl".to_string());
        let profile = RemapProfile::new("test".to_string(), &config).unwrap();

        // Store in device_profiles
        let mut profiles = config_manager.device_profiles.write().await;
        profiles.entry("1532:0220".to_string())
            .or_insert_with(HashMap::new)
            .insert("test".to_string(), profile);
        drop(profiles);

        let state = Arc::new(RwLock::new(DaemonState::new()));
        let macro_engine = Arc::new(macro_engine::MacroEngine::new());
        let injector = create_test_injector();
        let security_manager = Arc::new(RwLock::new(security::SecurityManager::new(false)));

        // Test GetDeviceProfiles request
        let request = Request::GetDeviceProfiles {
            device_id: "1532:0220".to_string(),
        };

        let response = handle_request(
            request,
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::new(config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        assert!(matches!(response, Response::DeviceProfiles { .. }));
        if let Response::DeviceProfiles { profiles, .. } = response {
            assert!(profiles.contains(&"test".to_string()));
        }

        // Test GetActiveProfile request (no device manager initialized)
        let request = Request::GetActiveProfile {
            device_id: "1532:0220".to_string(),
        };

        let response = handle_request(
            request,
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::new(ConfigManager {
                config_path: temp_dir.path().join("config2.yaml"),
                macros_path: temp_dir.path().join("macros2.yaml"),
                cache_path: temp_dir.path().join("macros2.bin"),
                profiles_dir: temp_dir.path().join("profiles2"),
                remaps_path: temp_dir.path().join("remaps2.yaml"),
                device_profiles_path: temp_dir.path().join("device_profiles2.yaml"),
                layer_state_path: std::path::PathBuf::from("/tmp/layer_state.yaml"),
                config: Default::default(),
                macros: Arc::new(RwLock::new(HashMap::new())),
                profiles: Arc::new(RwLock::new(HashMap::new())),
                remaps: Arc::new(RwLock::new(HashMap::new())),
                device_profiles: Arc::new(RwLock::new(HashMap::new())),
            }),
            Arc::clone(&security_manager),
            None
        ).await;

        // Should return error since device manager is not initialized
        assert!(matches!(response, Response::Error(_)));
    }

    #[tokio::test]
    async fn test_analog_calibration_conversion() {
        use aethermap_common::AnalogCalibrationConfig;

        let config = AnalogCalibrationConfig {
            deadzone: 0.2,
            deadzone_shape: "circular".to_string(),
            sensitivity: "quadratic".to_string(),
            sensitivity_multiplier: 1.5,
            range_min: -32768,
            range_max: 32767,
            invert_x: true,
            invert_y: false,
            exponent: 2.0,
            analog_mode: aethermap_common::AnalogMode::Disabled,
            camera_output_mode: None,
        };

        let calibration = config_to_calibration(config.clone()).unwrap();
        let restored = calibration_to_config(&calibration);

        assert_eq!(restored.deadzone, config.deadzone);
        assert_eq!(restored.deadzone_shape, config.deadzone_shape);
        assert_eq!(restored.sensitivity, config.sensitivity);
        assert_eq!(restored.sensitivity_multiplier, config.sensitivity_multiplier);
        assert_eq!(restored.range_min, config.range_min);
        assert_eq!(restored.range_max, config.range_max);
        assert_eq!(restored.invert_x, config.invert_x);
        assert_eq!(restored.invert_y, config.invert_y);
    }

    #[tokio::test]
    async fn test_invalid_calibration_rejected() {
        use aethermap_common::AnalogCalibrationConfig;

        // Invalid deadzone (> 1.0)
        let invalid_config = AnalogCalibrationConfig {
            deadzone: 1.5,
            deadzone_shape: "circular".to_string(),
            sensitivity: "linear".to_string(),
            sensitivity_multiplier: 1.0,
            range_min: -32768,
            range_max: 32767,
            invert_x: false,
            invert_y: false,
            exponent: 2.0,
            analog_mode: aethermap_common::AnalogMode::Disabled,
            camera_output_mode: None,
        };

        let result = config_to_calibration(invalid_config);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_analog_calibration_ipc_roundtrip() {
        use aethermap_common::AnalogCalibrationConfig;
        use crate::analog_processor::AnalogProcessor;

        let mut state = DaemonState::new();
        state.analog_processor = Some(Arc::new(AnalogProcessor::new()));
        let state = Arc::new(RwLock::new(state));

        let macro_engine = Arc::new(macro_engine::MacroEngine::new());
        let injector = create_test_injector();
        let config_manager = create_test_config_manager().await;
        let security_manager = Arc::new(RwLock::new(security::SecurityManager::new(false)));

        // First, set a calibration
        let config = AnalogCalibrationConfig {
            deadzone: 0.25,
            deadzone_shape: "square".to_string(),
            sensitivity: "exponential".to_string(),
            sensitivity_multiplier: 2.0,
            range_min: -16384,
            range_max: 16383,
            invert_x: true,
            invert_y: false,
            exponent: 3.0,
            analog_mode: aethermap_common::AnalogMode::Wasd,
            camera_output_mode: None,
        };

        let response = handle_request(
            Request::SetAnalogCalibration {
                device_id: "32b6:12f7".to_string(),
                layer_id: 0,
                calibration: config.clone(),
            },
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::clone(&config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        assert!(matches!(response, Response::AnalogCalibrationAck));

        // Now retrieve it
        let response = handle_request(
            Request::GetAnalogCalibration {
                device_id: "32b6:12f7".to_string(),
                layer_id: 0,
            },
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::clone(&config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        match response {
            Response::AnalogCalibration { calibration: Some(cal), .. } => {
                assert_eq!(cal.deadzone, config.deadzone);
                assert_eq!(cal.deadzone_shape, config.deadzone_shape);
                assert_eq!(cal.sensitivity, config.sensitivity);
                assert_eq!(cal.sensitivity_multiplier, config.sensitivity_multiplier);
            }
            _ => panic!("Expected AnalogCalibration response with calibration data, got {:?}", response),
        }
    }

    #[tokio::test]
    async fn test_analog_calibration_default_on_missing() {
        use aethermap_common::AnalogCalibrationConfig;

        let state = Arc::new(RwLock::new(DaemonState::new()));
        let macro_engine = Arc::new(macro_engine::MacroEngine::new());
        let injector = create_test_injector();
        let config_manager = create_test_config_manager().await;
        let security_manager = Arc::new(RwLock::new(security::SecurityManager::new(false)));

        // Request calibration for non-existent device
        let response = handle_request(
            Request::GetAnalogCalibration {
                device_id: "nonexistent".to_string(),
                layer_id: 0,
            },
            Arc::clone(&state),
            Arc::clone(&macro_engine),
            Arc::clone(&injector),
            Arc::clone(&config_manager),
            Arc::clone(&security_manager),
            None
        ).await;

        // Should return default calibration
        match response {
            Response::AnalogCalibration { calibration: None, .. } => {
                // Expected - no custom calibration configured
            }
            Response::AnalogCalibration { calibration: Some(cal), .. } => {
                // Should have default values
                assert_eq!(cal.deadzone, 0.15);
                assert_eq!(cal.deadzone_shape, "circular");
            }
            _ => panic!("Expected AnalogCalibration response"),
        }
    }
}