scarab-protocol 0.3.3

IPC protocol definitions and shared-memory layout for the Scarab terminal emulator
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
#![no_std]
// This crate defines the data layout shared between Daemon and Client.
// It must be #[repr(C)] to ensure memory layout consistency across processes.

use bytemuck::{Pod, Zeroable};

// Safe abstraction layer for SharedState access
pub mod terminal_state;
pub use terminal_state::TerminalStateReader;

// Semantic zones for deep shell integration
pub mod zones;
pub use zones::{CommandBlock, SemanticZone, ZoneTracker, ZoneType};

/// Default shared memory path for terminal state.
/// Can be overridden via SCARAB_SHMEM_PATH environment variable.
pub const SHMEM_PATH: &str = "/scarab_shm_v1";

/// Environment variable to override the shared memory path.
/// Useful for sandboxed environments where /dev/shm is not writable.
pub const SHMEM_PATH_ENV: &str = "SCARAB_SHMEM_PATH";

/// Environment variable to override the image shared memory path.
pub const IMAGE_SHMEM_PATH_ENV: &str = "SCARAB_IMAGE_SHMEM_PATH";
pub const GRID_WIDTH: usize = 200;
pub const GRID_HEIGHT: usize = 100;
pub const BUFFER_SIZE: usize = GRID_WIDTH * GRID_HEIGHT;

#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
pub struct Cell {
    pub char_codepoint: u32,
    pub fg: u32,           // RGBA
    pub bg: u32,           // RGBA
    pub flags: u8,         // Bold, Italic, etc.
    pub _padding: [u8; 3], // Align to 16 bytes
}

impl Default for Cell {
    fn default() -> Self {
        Self {
            char_codepoint: b' ' as u32,
            fg: 0xFFA8DF5A, // Slime green foreground (ARGB: #a8df5a)
            bg: 0xFF0D1208, // Slime dark background (ARGB: #0d1208)
            flags: 0,
            _padding: [0; 3],
        }
    }
}

// A double-buffered grid state living in shared memory
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SharedState {
    pub sequence_number: u64, // Atomic sequence for synchronization
    pub dirty_flag: u8,
    pub error_mode: u8, // 0 = normal mode, 1 = error mode (PTY/SHM unavailable)
    pub cursor_x: u16,
    pub cursor_y: u16,
    pub _padding2: [u8; 2], // Align to u64 boundary for cells array
    // Fixed size buffer for the "visible" screen.
    // In production, use offset pointers to a larger ring buffer.
    pub cells: [Cell; BUFFER_SIZE],
}

// Manual implementations needed for large arrays
unsafe impl Pod for SharedState {}
unsafe impl Zeroable for SharedState {}

// Image buffer constants
/// Maximum number of concurrent image placements
pub const MAX_IMAGES: usize = 64;

/// Maximum total image buffer size (16MB)
pub const IMAGE_BUFFER_SIZE: usize = 16 * 1024 * 1024;

/// Default shared memory path for image buffer (separate from terminal state).
/// Can be overridden via SCARAB_IMAGE_SHMEM_PATH environment variable.
pub const IMAGE_SHMEM_PATH: &str = "/scarab_img_shm_v1";

/// Image placement metadata for shared memory
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SharedImagePlacement {
    /// Unique identifier for this placement
    pub image_id: u64,
    /// Column position in terminal grid
    pub x: u16,
    /// Row position in terminal grid
    pub y: u16,
    /// Width in terminal cells
    pub width_cells: u16,
    /// Height in terminal cells
    pub height_cells: u16,
    /// Pixel width of decoded image
    pub pixel_width: u32,
    /// Pixel height of decoded image
    pub pixel_height: u32,
    /// Offset into blob_data buffer
    pub blob_offset: u32,
    /// Size of image data in bytes
    pub blob_size: u32,
    /// Image format (0=PNG, 1=JPEG, 2=GIF, 3=RGBA)
    pub format: u8,
    /// Flags (bit 0: valid/active)
    pub flags: u8,
    /// Padding for alignment
    pub _padding: [u8; 6],
}

// Manual Pod/Zeroable implementations
unsafe impl Pod for SharedImagePlacement {}
unsafe impl Zeroable for SharedImagePlacement {}

impl SharedImagePlacement {
    /// Check if this placement is valid/active
    pub const fn is_valid(&self) -> bool {
        (self.flags & 0x01) != 0
    }

    /// Mark this placement as valid/active
    pub fn set_valid(&mut self) {
        self.flags |= 0x01;
    }

    /// Mark this placement as invalid/inactive
    pub fn set_invalid(&mut self) {
        self.flags &= !0x01;
    }
}

/// Shared memory buffer for image data
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SharedImageBuffer {
    /// Sequence number for synchronization (increment on any change)
    pub sequence_number: u64,
    /// Number of active placements
    pub count: u32,
    /// Next blob write offset (circular buffer pointer)
    pub next_blob_offset: u32,
    /// Image placement metadata array
    pub placements: [SharedImagePlacement; MAX_IMAGES],
    /// Raw image blob data (circular buffer)
    pub blob_data: [u8; IMAGE_BUFFER_SIZE],
}

// Manual Pod/Zeroable implementations for large array
unsafe impl Pod for SharedImageBuffer {}
unsafe impl Zeroable for SharedImageBuffer {}

// Log levels for plugin logging
#[derive(Debug, Clone, Copy, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum LogLevel {
    Error,
    Warn,
    Info,
    Debug,
}

// Notification severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum NotifyLevel {
    Error,
    Warning,
    Info,
    Success,
}

// Tab/Pane split direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum SplitDirection {
    Horizontal,
    Vertical,
}

// Menu action types from plugin API
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum MenuActionType {
    Command { command: alloc::string::String },
    Remote { id: alloc::string::String },
}

// Navigation focusable action types
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum NavFocusableAction {
    /// Open a URL in the default browser
    OpenUrl(alloc::string::String),
    /// Open a file in the configured editor
    OpenFile(alloc::string::String),
    /// Custom plugin-defined action
    Custom(alloc::string::String),
}

// Control messages (Sent via Socket/Pipe, not ShMem)
// Using rkyv for zero-copy serialization
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum ControlMessage {
    Resize {
        cols: u16,
        rows: u16,
    },
    Input {
        data: alloc::vec::Vec<u8>,
    },
    LoadPlugin {
        path: alloc::string::String,
    },
    Ping {
        timestamp: u64,
    },
    Disconnect {
        client_id: u64,
    },

    // Session management commands
    SessionCreate {
        name: alloc::string::String,
    },
    SessionDelete {
        id: alloc::string::String,
    },
    SessionList,
    SessionAttach {
        id: alloc::string::String,
    },
    SessionDetach {
        id: alloc::string::String,
    },
    SessionRename {
        id: alloc::string::String,
        new_name: alloc::string::String,
    },

    // Tab management commands
    TabCreate {
        title: Option<alloc::string::String>,
    },
    TabClose {
        tab_id: u64,
    },
    TabSwitch {
        tab_id: u64,
    },
    TabRename {
        tab_id: u64,
        new_title: alloc::string::String,
    },
    TabList,

    // Pane management commands
    PaneSplit {
        pane_id: u64,
        direction: SplitDirection,
    },
    PaneClose {
        pane_id: u64,
    },
    PaneFocus {
        pane_id: u64,
    },
    PaneResize {
        pane_id: u64,
        width: u16,
        height: u16,
    },
    /// Focus the next pane in the current tab (for navigation)
    PaneFocusNext,
    /// Focus the previous pane in the current tab (for navigation)
    PaneFocusPrev,

    // Tab navigation commands
    /// Switch to the next tab
    TabNext,
    /// Switch to the previous tab
    TabPrev,

    // Mouse input commands
    /// Send mouse click event to the terminal
    MouseClick {
        col: u16,
        row: u16,
        button: u8,
    },

    // Remote UI Responses
    CommandSelected {
        id: alloc::string::String,
    },

    // Plugin inspection commands
    PluginListRequest,
    PluginEnable {
        name: alloc::string::String,
    },
    PluginDisable {
        name: alloc::string::String,
    },
    PluginReload {
        name: alloc::string::String,
    },

    // Plugin menu commands
    PluginMenuRequest {
        plugin_name: alloc::string::String,
    },
    PluginMenuExecute {
        plugin_name: alloc::string::String,
        action: MenuActionType,
    },

    // Plugin logging and notifications (sent from daemon to client)
    PluginLog {
        plugin_name: alloc::string::String,
        level: LogLevel,
        message: alloc::string::String,
    },
    PluginNotify {
        title: alloc::string::String,
        body: alloc::string::String,
        level: NotifyLevel,
    },

    // Navigation API commands (sent from plugins via client to daemon/client)
    NavEnterHintMode {
        plugin_name: alloc::string::String,
    },
    NavExitMode {
        plugin_name: alloc::string::String,
    },
    NavRegisterFocusable {
        plugin_name: alloc::string::String,
        x: u16,
        y: u16,
        width: u16,
        height: u16,
        label: alloc::string::String,
        action: NavFocusableAction,
    },
    NavUnregisterFocusable {
        plugin_name: alloc::string::String,
        focusable_id: u64,
    },

    // Semantic zone commands (deep shell integration)
    /// Request semantic zones update from daemon
    ZonesRequest,

    /// Copy the output from the last completed command
    CopyLastOutput,

    /// Select a specific zone by ID
    SelectZone {
        zone_id: u64,
    },

    /// Extract text from a zone
    ExtractZoneText {
        zone_id: u64,
    },
}

// Session response messages
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum SessionResponse {
    Created {
        id: alloc::string::String,
        name: alloc::string::String,
    },
    Deleted {
        id: alloc::string::String,
    },
    List {
        sessions: alloc::vec::Vec<SessionInfo>,
    },
    Attached {
        id: alloc::string::String,
    },
    Detached {
        id: alloc::string::String,
    },
    Renamed {
        id: alloc::string::String,
        new_name: alloc::string::String,
    },
    Error {
        message: alloc::string::String,
    },
}

#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct SessionInfo {
    pub id: alloc::string::String,
    pub name: alloc::string::String,
    pub created_at: u64,
    pub last_attached: u64,
    pub attached_clients: u32,
}

// Tab information
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct TabInfo {
    pub id: u64,
    pub title: alloc::string::String,
    pub session_id: Option<alloc::string::String>,
    pub is_active: bool,
    pub pane_count: u32,
}

// Pane layout information
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct PaneInfo {
    pub id: u64,
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
    pub is_focused: bool,
}

// Plugin information for inspector and dock display
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct PluginInspectorInfo {
    pub name: alloc::string::String,
    pub version: alloc::string::String,
    pub description: alloc::string::String,
    pub author: alloc::string::String,
    pub homepage: Option<alloc::string::String>,
    pub api_version: alloc::string::String,
    pub min_scarab_version: alloc::string::String,
    pub enabled: bool,
    pub failure_count: u32,
    /// Plugin emoji for visual display (e.g., "🦠")
    pub emoji: Option<alloc::string::String>,
    /// Plugin color as hex code (e.g., "#FF5733")
    pub color: Option<alloc::string::String>,
    /// Verification status
    pub verification: PluginVerificationStatus,
}

/// Verification status for plugins (zero-copy compatible)
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum PluginVerificationStatus {
    /// Plugin was verified with valid GPG signature
    Verified {
        key_fingerprint: alloc::string::String,
        signature_timestamp: u64,
    },
    /// Plugin checksum was verified but no signature
    ChecksumOnly { checksum: alloc::string::String },
    /// Plugin was installed without verification
    Unverified { warning: alloc::string::String },
}

// Status bar side specification
#[derive(Debug, Clone, Copy, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum StatusBarSide {
    Left,
    Right,
}

// Render item for status bar content
// This is a simplified version for IPC - full version is in scarab-plugin-api
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum StatusRenderItem {
    Text(alloc::string::String),
    Icon(alloc::string::String),
    Foreground { r: u8, g: u8, b: u8 },
    Background { r: u8, g: u8, b: u8 },
    Bold,
    Italic,
    ResetAttributes,
    Spacer,
    Padding(u8),
    Separator(alloc::string::String),
}

// Messages sent from Daemon to Client (Remote UI & Responses)
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum DaemonMessage {
    // Wrap existing session responses
    Session(SessionResponse),

    // Tab state updates
    TabCreated {
        tab: TabInfo,
    },
    TabClosed {
        tab_id: u64,
    },
    TabSwitched {
        tab_id: u64,
    },
    TabListResponse {
        tabs: alloc::vec::Vec<TabInfo>,
    },

    // Pane state updates
    PaneCreated {
        pane: PaneInfo,
    },
    PaneClosed {
        pane_id: u64,
    },
    PaneFocused {
        pane_id: u64,
    },
    PaneLayoutUpdate {
        panes: alloc::vec::Vec<PaneInfo>,
    },

    // Status bar updates
    StatusBarUpdate {
        window_id: u64,
        side: StatusBarSide,
        items: alloc::vec::Vec<StatusRenderItem>,
    },

    // Remote UI Commands
    DrawOverlay {
        id: u64, // UUID-like identifier
        x: u16,
        y: u16,
        text: alloc::string::String,
        style: OverlayStyle,
    },
    ClearOverlays {
        id: Option<u64>, // None = Clear All
    },
    ShowModal {
        title: alloc::string::String,
        items: alloc::vec::Vec<ModalItem>,
    },
    HideModal,

    // Plugin inspection responses
    PluginList {
        plugins: alloc::vec::Vec<PluginInspectorInfo>,
    },
    PluginStatusChanged {
        name: alloc::string::String,
        enabled: bool,
    },
    PluginError {
        name: alloc::string::String,
        error: alloc::string::String,
    },

    // Plugin logging and notifications
    PluginLog {
        plugin_name: alloc::string::String,
        level: LogLevel,
        message: alloc::string::String,
    },
    PluginNotification {
        title: alloc::string::String,
        body: alloc::string::String,
        level: NotifyLevel,
    },

    // Plugin menu response
    PluginMenuResponse {
        plugin_name: alloc::string::String,
        menu_json: alloc::string::String, // Serialized Vec<MenuItem>
    },
    PluginMenuError {
        plugin_name: alloc::string::String,
        error: alloc::string::String,
    },

    // Theme system updates
    ThemeUpdate {
        theme_json: alloc::string::String, // Serialized Theme
    },

    // Shell prompt markers update (OSC 133 shell integration)
    PromptMarkersUpdate {
        /// List of current prompt markers from daemon
        markers: alloc::vec::Vec<PromptMarkerInfo>,
    },

    // Semantic zones update (deep shell integration)
    SemanticZonesUpdate {
        /// List of current semantic zones (prompt, input, output regions)
        zones: alloc::vec::Vec<SemanticZone>,
    },

    // Command blocks update (grouped command sequences)
    CommandBlocksUpdate {
        /// List of completed command blocks
        blocks: alloc::vec::Vec<CommandBlock>,
    },

    /// Response to ExtractZoneText with the zone's text content
    ZoneTextExtracted {
        zone_id: u64,
        text: alloc::string::String,
    },

    // Event forwarding to clients
    Event(EventMessage),

    // Navigation API responses
    NavFocusableRegistered {
        plugin_name: alloc::string::String,
        focusable_id: u64,
    },
    NavFocusableUnregistered {
        plugin_name: alloc::string::String,
        focusable_id: u64,
    },
    NavModeEntered {
        plugin_name: alloc::string::String,
    },
    NavModeExited {
        plugin_name: alloc::string::String,
    },
    /// Forward focusable registration from daemon to client
    NavRegisterFocusable {
        plugin_name: alloc::string::String,
        x: u16,
        y: u16,
        width: u16,
        height: u16,
        label: alloc::string::String,
        action: NavFocusableAction,
    },
    /// Forward focusable unregistration from daemon to client
    NavUnregisterFocusable {
        plugin_name: alloc::string::String,
        focusable_id: u64,
    },
    /// Spawn an overlay at a given position
    SpawnOverlay {
        plugin_name: alloc::string::String,
        overlay_id: u64,
        x: u16,
        y: u16,
        content: alloc::string::String,
        style: OverlayStyle,
    },
    /// Remove a previously spawned overlay
    RemoveOverlay {
        plugin_name: alloc::string::String,
        overlay_id: u64,
    },
    /// Add a status bar item
    AddStatusItem {
        plugin_name: alloc::string::String,
        item_id: u64,
        label: alloc::string::String,
        content: alloc::string::String,
        priority: i32,
    },
    /// Remove a status bar item
    RemoveStatusItem {
        plugin_name: alloc::string::String,
        item_id: u64,
    },
    /// Trigger prompt jump navigation
    PromptJump {
        plugin_name: alloc::string::String,
        direction: PromptJumpDirection,
    },
    /// Apply a theme by name
    ThemeApply {
        theme_name: alloc::string::String,
    },
    /// Set a specific palette color
    PaletteColorSet {
        color_name: alloc::string::String,
        value: alloc::string::String,
    },
    /// Response with current theme info
    ThemeInfoResponse {
        plugin_name: alloc::string::String,
        theme_name: alloc::string::String,
    },
}

/// Direction for prompt jump navigation
#[derive(Debug, Clone, Copy, PartialEq, Eq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub enum PromptJumpDirection {
    Up,
    Down,
    First,
    Last,
}

/// Event message for IPC forwarding
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct EventMessage {
    /// Event type name
    pub event_type: alloc::string::String,
    /// Window ID if applicable
    pub window_id: Option<u64>,
    /// Pane ID if applicable
    pub pane_id: Option<u64>,
    /// Tab ID if applicable
    pub tab_id: Option<u64>,
    /// Serialized event data
    pub data: alloc::vec::Vec<u8>,
    /// Timestamp in microseconds since UNIX epoch
    pub timestamp_micros: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct OverlayStyle {
    pub fg: u32, // RGBA
    pub bg: u32, // RGBA
    pub z_index: f32,
}

impl Default for OverlayStyle {
    fn default() -> Self {
        Self {
            fg: 0xFFFFFFFF, // White
            bg: 0xFF0000FF, // Red background for high visibility by default
            z_index: 100.0,
        }
    }
}

#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct ModalItem {
    pub id: alloc::string::String,
    pub label: alloc::string::String,
    pub description: Option<alloc::string::String>,
}

// IPC configuration constants
pub const SOCKET_PATH: &str = "/tmp/scarab-daemon.sock";
pub const MAX_MESSAGE_SIZE: usize = 8192;
pub const MAX_CLIENTS: usize = 16;
pub const RECONNECT_DELAY_MS: u64 = 100;
pub const MAX_RECONNECT_ATTEMPTS: u32 = 10;

/// Terminal display metrics shared between rendering and input systems
///
/// This provides the coordinate conversion information needed by:
/// - Mouse input handlers (screen to grid coordinate conversion)
/// - Text rendering systems (grid to screen coordinate conversion)
/// - Selection and UI overlays (coordinate mapping)
///
/// This type can be used as a Bevy Resource when the bevy feature is enabled.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::prelude::Resource))]
pub struct TerminalMetrics {
    /// Width of a single character cell in pixels
    pub cell_width: f32,
    /// Height of a single character cell in pixels
    pub cell_height: f32,
    /// Number of columns in the terminal grid
    pub columns: u16,
    /// Number of rows in the terminal grid
    pub rows: u16,
}

impl Default for TerminalMetrics {
    fn default() -> Self {
        Self {
            cell_width: 9.0,   // Typical monospace width at 15px font
            cell_height: 18.0, // Typical line height
            columns: 80,
            rows: 24,
        }
    }
}

impl TerminalMetrics {
    /// Create metrics from font size and terminal dimensions
    pub fn new(font_size: f32, line_height_multiplier: f32, columns: u16, rows: u16) -> Self {
        Self {
            cell_width: font_size * 0.6, // Typical monospace ratio
            cell_height: font_size * line_height_multiplier,
            columns,
            rows,
        }
    }

    /// Convert screen coordinates to grid coordinates
    ///
    /// # Arguments
    /// * `screen_x` - X coordinate in pixels (from left edge)
    /// * `screen_y` - Y coordinate in pixels (from top edge, Y-down)
    ///
    /// # Returns
    /// Grid position clamped to valid bounds (col, row)
    pub fn screen_to_grid(&self, screen_x: f32, screen_y: f32) -> (u16, u16) {
        let col = (screen_x / self.cell_width).floor() as i32;
        let row = (screen_y / self.cell_height).floor() as i32;

        // Clamp to valid grid bounds
        let col = col.max(0).min((self.columns - 1) as i32) as u16;
        let row = row.max(0).min((self.rows - 1) as i32) as u16;

        (col, row)
    }

    /// Convert grid coordinates to screen coordinates (top-left of cell)
    ///
    /// # Returns
    /// Screen position in pixels (x, y)
    pub fn grid_to_screen(&self, col: u16, row: u16) -> (f32, f32) {
        let x = col as f32 * self.cell_width;
        let y = row as f32 * self.cell_height;
        (x, y)
    }

    /// Get total terminal size in pixels
    pub fn screen_size(&self) -> (f32, f32) {
        (
            self.columns as f32 * self.cell_width,
            self.rows as f32 * self.cell_height,
        )
    }
}

/// Image format specification for image protocol support
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ImageFormat {
    /// PNG image format
    Png = 0,
    /// JPEG image format
    Jpeg = 1,
    /// GIF image format
    Gif = 2,
    /// Raw RGBA pixel data
    Rgba = 3,
}

/// Represents an image placement in the terminal grid
///
/// Images are transferred via shared memory to avoid protocol overhead.
/// This struct contains the metadata and reference to the image data.
#[derive(Debug, Clone)]
pub struct ImagePlacement {
    /// Unique identifier for this placement
    pub id: u64,
    /// Column position in terminal grid
    pub x: u16,
    /// Row position in terminal grid
    pub y: u16,
    /// Width in terminal cells
    pub width_cells: u16,
    /// Height in terminal cells
    pub height_cells: u16,
    /// Offset into shared memory image buffer
    pub shm_offset: usize,
    /// Size of image data in shared memory
    pub shm_size: usize,
    /// Image format
    pub format: ImageFormat,
}

/// Shell prompt marker for IPC (OSC 133 shell integration)
///
/// This is a simplified, serializable version of the daemon's internal
/// PromptMarker type. Used to communicate shell integration markers
/// from daemon to client for features like:
/// - Semantic prompt navigation (jump to previous/next prompt)
/// - Command output extraction
/// - Command duration tracking
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct PromptMarkerInfo {
    /// Marker type encoded as u8:
    /// - 0 = PromptStart (OSC 133;A)
    /// - 1 = CommandStart (OSC 133;B)
    /// - 2 = CommandExecuted (OSC 133;C)
    /// - 3 = CommandFinished (OSC 133;D)
    pub marker_type: u8,
    /// Absolute line number in scrollback
    pub line: u32,
    /// Exit code (only valid for CommandFinished markers)
    pub exit_code: Option<i32>,
    /// Timestamp in microseconds since UNIX epoch
    pub timestamp_micros: u64,
}

impl PromptMarkerInfo {
    /// Create a PromptStart marker
    pub fn prompt_start(line: u32, timestamp_micros: u64) -> Self {
        Self {
            marker_type: 0,
            line,
            exit_code: None,
            timestamp_micros,
        }
    }

    /// Create a CommandStart marker
    pub fn command_start(line: u32, timestamp_micros: u64) -> Self {
        Self {
            marker_type: 1,
            line,
            exit_code: None,
            timestamp_micros,
        }
    }

    /// Create a CommandExecuted marker
    pub fn command_executed(line: u32, timestamp_micros: u64) -> Self {
        Self {
            marker_type: 2,
            line,
            exit_code: None,
            timestamp_micros,
        }
    }

    /// Create a CommandFinished marker with exit code
    pub fn command_finished(line: u32, exit_code: i32, timestamp_micros: u64) -> Self {
        Self {
            marker_type: 3,
            line,
            exit_code: Some(exit_code),
            timestamp_micros,
        }
    }

    /// Check if this is a PromptStart marker
    pub fn is_prompt_start(&self) -> bool {
        self.marker_type == 0
    }

    /// Check if this is a CommandStart marker
    pub fn is_command_start(&self) -> bool {
        self.marker_type == 1
    }

    /// Check if this is a CommandExecuted marker
    pub fn is_command_executed(&self) -> bool {
        self.marker_type == 2
    }

    /// Check if this is a CommandFinished marker
    pub fn is_command_finished(&self) -> bool {
        self.marker_type == 3
    }
}

extern crate alloc;