aboutsummaryrefslogtreecommitdiffstats
path: root/src/daemon.rs
blob: dab0e5af985d58a8098110274a4b6d6ae5e60a36 (plain) (blame)
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
use crate::audio::AudioEngine;
use crate::config::Config;
use std::io::{Read, Write};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

#[derive(Debug, Clone)]
enum ShuffleMode {
    None,
    All,
    Album,
}

#[derive(Debug, Clone)]
struct Track {
    path: PathBuf,
    title: Option<String>,
    artist: Option<String>,
    album: Option<String>,
}

impl Track {
    fn from_path(path: PathBuf) -> Self {
        let metadata = extract_metadata(&path);
        let metadata = metadata.as_ref();
        Track {
            path,
            title: metadata.and_then(|m| m.title.clone()),
            artist: metadata.and_then(|m| m.artist.clone()),
            album: metadata.and_then(|m| m.album.clone()),
        }
    }

    fn display_string(&self, index: usize) -> String {
        let album_artist = self
            .artist
            .clone()
            .or_else(|| self.album.clone())
            .unwrap_or_default();
        let title = self.title.clone().unwrap_or_else(|| {
            self.path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("Unknown")
                .to_string()
        });

        if album_artist.is_empty() {
            format!("{}. {}", index + 1, title)
        } else {
            format!("{}. {} - {}", index + 1, album_artist, title)
        }
    }
}

#[derive(Debug, Default, Clone)]
struct AudioMetadata {
    title: Option<String>,
    artist: Option<String>,
    album: Option<String>,
}

fn extract_metadata(path: &Path) -> Option<AudioMetadata> {
    use symphonia::core::io::MediaSourceStream;
    use symphonia::core::meta::StandardTagKey;
    use symphonia::core::probe::Hint;

    let file = std::fs::File::open(path).ok()?;
    let mss = MediaSourceStream::new(Box::new(file), Default::default());

    let hint = Hint::new();
    let mut probed = symphonia::default::get_probe()
        .format(&hint, mss, &Default::default(), &Default::default())
        .ok()?;

    let mut metadata = AudioMetadata::default();

    // First check probed metadata (e.g., ID3v2 headers before container)
    if let Some(meta) = probed.metadata.get() {
        if let Some(rev) = meta.current() {
            for tag in rev.tags() {
                match tag.std_key {
                    Some(StandardTagKey::TrackTitle) => {
                        metadata.title = Some(tag.value.to_string());
                    }
                    Some(StandardTagKey::Artist) => {
                        metadata.artist = Some(tag.value.to_string());
                    }
                    Some(StandardTagKey::Album) => {
                        metadata.album = Some(tag.value.to_string());
                    }
                    _ => {}
                }
            }
        }
    }

    // If nothing found, try container metadata
    if metadata.title.is_none() && metadata.artist.is_none() && metadata.album.is_none() {
        let mut format = probed.format;
        if let Some(rev) = format.metadata().current() {
            for tag in rev.tags() {
                match tag.std_key {
                    Some(StandardTagKey::TrackTitle) => {
                        metadata.title = Some(tag.value.to_string());
                    }
                    Some(StandardTagKey::Artist) => {
                        metadata.artist = Some(tag.value.to_string());
                    }
                    Some(StandardTagKey::Album) => {
                        metadata.album = Some(tag.value.to_string());
                    }
                    _ => {}
                }
            }
        }
    }

    if metadata.title.is_some() || metadata.artist.is_some() || metadata.album.is_some() {
        Some(metadata)
    } else {
        None
    }
}

struct Queue {
    tracks: Vec<Track>,
    position: usize,
    shuffle: ShuffleMode,
    shuffled_indices: Vec<usize>,
}

impl Queue {
    fn new() -> Self {
        Queue {
            tracks: Vec::new(),
            position: 0,
            shuffle: ShuffleMode::None,
            shuffled_indices: Vec::new(),
        }
    }

    fn add_track(&mut self, path: PathBuf) {
        self.tracks.push(Track::from_path(path));
        self.update_shuffle_indices();
    }

    fn insert_after_current(&mut self, path: PathBuf) {
        let insert_idx = self.position + 1;
        self.tracks.insert(insert_idx, Track::from_path(path));
        self.update_shuffle_indices();
    }

    fn track_at(&self, idx: usize) -> Option<&Track> {
        self.tracks.get(idx)
    }

    fn set_shuffle(&mut self, mode: ShuffleMode) {
        self.shuffle = mode.clone();
        self.update_shuffle_indices();
    }

    fn update_shuffle_indices(&mut self) {
        self.shuffled_indices = (0..self.tracks.len()).collect();
        if let ShuffleMode::All = self.shuffle {
            use rand::seq::SliceRandom;
            let mut rng = rand::rng();
            self.shuffled_indices.shuffle(&mut rng);
        }
    }

    fn current(&self) -> Option<&Track> {
        if self.tracks.is_empty() {
            None
        } else if let ShuffleMode::All = self.shuffle {
            self.shuffled_indices
                .get(self.position)
                .and_then(|&idx| self.tracks.get(idx))
        } else if let ShuffleMode::Album = self.shuffle {
            // TODO: When database is ready, only shuffle tracks in same album/folder
            // For now, behave like All
            self.shuffled_indices
                .get(self.position)
                .and_then(|&idx| self.tracks.get(idx))
        } else {
            self.tracks.get(self.position)
        }
    }

    fn next(&mut self) -> Option<PathBuf> {
        let max_pos = self.tracks.len().saturating_sub(1);
        if self.position < max_pos {
            self.position += 1;
            self.current().map(|t| t.path.clone())
        } else {
            None
        }
    }

    fn previous(&mut self) -> Option<PathBuf> {
        if self.position > 0 {
            self.position -= 1;
            self.current().map(|t| t.path.clone())
        } else {
            None
        }
    }
}

pub fn start_daemon() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::load()?;
    let socket_path = config.get_socket_path();
    let pid_path = get_pid_path(&config);

    // Check if daemon is already running
    if let Ok(pid) = check_running_daemon(&pid_path) {
        return Err(format!("Daemon already running with PID {}", pid).into());
    }

    // Clean up stale socket file if it exists
    if socket_path.exists() {
        std::fs::remove_file(&socket_path).ok();
    }

    // Create runtime directory
    if let Some(parent) = socket_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if let Some(parent) = pid_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Write PID file
    let pid = std::process::id();
    std::fs::write(&pid_path, pid.to_string())?;
    println!("Daemon started with PID {}", pid);

    // Shared shutdown flag - can be triggered by signal or command
    let shutdown_flag = Arc::new(AtomicBool::new(false));
    let shutdown_flag_signal = Arc::clone(&shutdown_flag);

    // Handle SIGTERM and SIGINT
    ctrlc::set_handler(move || {
        println!("Received shutdown signal");
        shutdown_flag_signal.store(true, Ordering::SeqCst);
    })?;

    let listener = UnixListener::bind(&socket_path)?;
    println!("Listening on {:?}", socket_path);

    let audio_engine = Arc::new(AudioEngine::new());
    let queue = Arc::new(Mutex::new(Queue::new()));

    // Load and apply saved settings
    let config = Config::load().unwrap_or_default();
    if let Some(vol) = config.volume {
        audio_engine.set_volume(vol as f32 / 100.0);
    }
    if let Some(ref shuffle_str) = config.shuffle {
        if let Ok(mut q) = queue.lock() {
            match shuffle_str.as_str() {
                "on" | "all" => q.set_shuffle(ShuffleMode::All),
                "album" => q.set_shuffle(ShuffleMode::Album),
                _ => {}
            }
        }
    }

    // Accept connections until shutdown
    listener.set_nonblocking(true)?;
    // Helper: poll until a specific track actually starts playing or timeout
    fn wait_for_start(audio: &AudioEngine, target: &PathBuf, timeout_ms: u64) -> bool {
        use std::time::{Duration, Instant};
        let start = Instant::now();
        while start.elapsed() < Duration::from_millis(timeout_ms) {
            // If playing and the current track matches, assume started
            if audio.is_playing() {
                if let Some(cur) = audio.current_track() {
                    if &cur == target {
                        return true;
                    }
                }
            }
            std::thread::sleep(Duration::from_millis(10));
        }
        false
    }
    loop {
        // Check for shutdown signal
        if shutdown_flag.load(Ordering::SeqCst) {
            break;
        }

        // Check if track ended and play next (robust with startup feedback)
        if audio_engine.poll_track_ended() {
            if let Ok(mut q) = queue.lock() {
                // Save the current position in case we need to revert
                let original_position = q.position;
                // Try a handful of next candidates until one starts
                let mut started = false;
                // We'll attempt up to 4 candidates ahead
                let max_trials = 4usize;
                for _ in 0..max_trials {
                    if let Some(next_track) = q.next() {
                        audio_engine.play(next_track.clone());
                        // Wait a short time to see if playback actually starts
                        if wait_for_start(&audio_engine, &next_track, 500) {
                            started = true;
                            break;
                        } else {
                            // Try next candidate - q.next() already advanced position
                            continue;
                        }
                    } else {
                        break;
                    }
                }
                if !started {
                    // Revert position since we couldn't start any track
                    q.position = original_position;
                    // No candidate started; stop playback safely
                    audio_engine.stop();
                }
            }
        }

        match listener.accept() {
            Ok((stream, _)) => {
                let audio_clone = Arc::clone(&audio_engine);
                let shutdown_flag_clone = Arc::clone(&shutdown_flag);
                let queue_clone = Arc::clone(&queue);
                thread::spawn(move || {
                    handle_client(stream, audio_clone, shutdown_flag_clone, queue_clone);
                });
            }
            Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                thread::sleep(Duration::from_millis(50));
                continue;
            }
            Err(e) => eprintln!("Connection failed: {}", e),
        }

        thread::sleep(Duration::from_millis(50));
    }

    // Cleanup
    cleanup_daemon(&socket_path, &pid_path);
    println!("Daemon stopped");
    Ok(())
}

pub fn stop_daemon() -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::load()?;
    let pid_path = get_pid_path(&config);

    match check_running_daemon(&pid_path) {
        Ok(pid) => {
            // Try to send stop command first for graceful shutdown
            if let Ok(()) = send_command_raw("stop_daemon".to_string()) {
                // Wait for daemon to stop
                for _ in 0..50 {
                    thread::sleep(Duration::from_millis(100));
                    if !is_process_running(pid) {
                        break;
                    }
                }
            }

            // Force kill if still running
            if is_process_running(pid) {
                unsafe {
                    libc::kill(pid as i32, libc::SIGTERM);
                }
                thread::sleep(Duration::from_millis(500));

                // Force kill if still alive
                if is_process_running(pid) {
                    unsafe {
                        libc::kill(pid as i32, libc::SIGKILL);
                    }
                }
            }

            // Clean up files
            let socket_path = config.get_socket_path();
            std::fs::remove_file(&socket_path).ok();
            std::fs::remove_file(&pid_path).ok();

            println!("Daemon stopped");
            Ok(())
        }
        Err(e) => Err(format!("Daemon not running: {}", e).into()),
    }
}

pub fn restart_daemon() -> Result<(), Box<dyn std::error::Error>> {
    // Stop existing daemon
    if let Ok(()) = stop_daemon() {
        // Wait a bit for cleanup
        thread::sleep(Duration::from_millis(500));
    }

    // Start new daemon in background
    let current_exe = std::env::current_exe()?;
    std::process::Command::new(current_exe)
        .arg("start")
        .spawn()?;

    println!("Daemon restarted");
    Ok(())
}

pub fn send_command(cmd: String) -> Result<(), Box<dyn std::error::Error>> {
    send_command_raw(cmd)?;
    Ok(())
}

fn send_command_raw(cmd: String) -> Result<(), Box<dyn std::error::Error>> {
    let config = Config::load()?;
    let socket_path = config.get_socket_path();

    let mut stream = UnixStream::connect(socket_path)?;
    stream.write_all(cmd.as_bytes())?;

    let mut response = String::new();
    stream.read_to_string(&mut response)?;
    print!("{}", response);

    Ok(())
}

fn handle_client(
    mut stream: UnixStream,
    audio: Arc<AudioEngine>,
    shutdown_flag: Arc<AtomicBool>,
    queue: Arc<Mutex<Queue>>,
) {
    let mut buffer = [0; 1024];
    if let Ok(size) = stream.read(&mut buffer) {
        let cmd = String::from_utf8_lossy(&buffer[..size]);
        let response = process_command(cmd.trim(), &audio, &shutdown_flag, &queue);
        let _ = stream.write_all(response.as_bytes());
    }
}

fn process_command(
    cmd: &str,
    audio: &AudioEngine,
    shutdown_flag: &Arc<AtomicBool>,
    queue: &Arc<Mutex<Queue>>,
) -> String {
    match cmd {
        "status" => {
            if audio.is_playing() {
                if let Some(track_path) = audio.current_track() {
                    let track = Track::from_path(track_path);
                    let track_str = track.display_string(0);
                    format!("Playing\nTrack: {}\n", track_str)
                } else {
                    "Playing\n".to_string()
                }
            } else {
                "Stopped\n".to_string()
            }
        }
        "pause" => {
            audio.pause();
            "Paused\n".to_string()
        }
        "resume" => {
            audio.resume();
            "Resumed\n".to_string()
        }
        "stop" => {
            audio.stop();
            // Clear queue on stop
            if let Ok(mut q) = queue.lock() {
                q.tracks.clear();
                q.position = 0;
            }
            "Stopped\n".to_string()
        }
        "stop_daemon" => {
            // Signal the daemon to shut down
            shutdown_flag.store(true, Ordering::SeqCst);
            "Shutting down\n".to_string()
        }
        "next" => {
            if let Ok(mut q) = queue.lock() {
                if let Some(next_track) = q.next() {
                    audio.play(next_track);
                    "Playing next track\n".to_string()
                } else {
                    audio.stop();
                    "Queue empty, stopped\n".to_string()
                }
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        "previous" => {
            if let Ok(mut q) = queue.lock() {
                if let Some(prev_track) = q.previous() {
                    audio.play(prev_track);
                    "Playing previous track\n".to_string()
                } else {
                    "No previous track\n".to_string()
                }
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        "shuffle" => {
            if let Ok(q) = queue.lock() {
                match q.shuffle {
                    ShuffleMode::None => "Shuffle: off\n".to_string(),
                    ShuffleMode::All => "Shuffle: on (all)\n".to_string(),
                    ShuffleMode::Album => "Shuffle: on (album)\n".to_string(),
                }
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        _ if cmd.starts_with("shuffle ") => {
            let mode_str = cmd[8..].trim();
            if let Ok(mut q) = queue.lock() {
                let result = if mode_str == "on" || mode_str == "all" {
                    q.set_shuffle(ShuffleMode::All);
                    "Shuffle: on (all)\n".to_string()
                } else if mode_str == "album" {
                    q.set_shuffle(ShuffleMode::Album);
                    "Shuffle: on (album)\n".to_string()
                } else if mode_str == "off" || mode_str == "none" {
                    q.set_shuffle(ShuffleMode::None);
                    "Shuffle: off\n".to_string()
                } else {
                    "Usage: shuffle [on|off|album]\n".to_string()
                };
                // Save to config
                if let Ok(mut config) = Config::load() {
                    if mode_str == "off" || mode_str == "none" {
                        config.shuffle = None;
                    } else {
                        config.shuffle = Some(mode_str.to_string());
                    }
                    let _ = config.save();
                }
                result
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        _ if cmd.starts_with("play ") => {
            let rest = &cmd[5..].trim();
            let (position, path_str): (&str, &str) = if let Some((pos, p)) = rest.split_once(' ') {
                if pos == "now" || pos == "next" || pos == "last" {
                    (pos, p)
                } else {
                    ("last", rest)
                }
            } else {
                ("last", rest)
            };
            let path_str = path_str.to_string();

            if path_str.is_empty() {
                return "Usage: play [now|next|last] <path>\n".to_string();
            }

            let path = std::path::PathBuf::from(path_str.clone());
            if path.is_file() {
                if let Ok(mut q) = queue.lock() {
                    match position {
                        "now" => {
                            q.insert_after_current(path.clone());
                            q.position += 1;
                            if let Some(track) = q.track_at(q.position) {
                                audio.play(track.path.clone());
                            }
                            format!("Playing now: {}\n", path_str)
                        }
                        "next" => {
                            q.insert_after_current(path.clone());
                            format!("Queued next: {}\n", path_str)
                        }
                        _ => {
                            q.add_track(path.clone());
                            audio.play(path);
                            format!("Playing: {}\n", path_str)
                        }
                    }
                } else {
                    "Error accessing queue\n".to_string()
                }
            } else if path.is_dir() {
                match find_audio_in_dir(&path) {
                    Some(files) if !files.is_empty() => {
                        if let Ok(mut q) = queue.lock() {
                            match position {
                                "now" => {
                                    for file in &files {
                                        q.insert_after_current(file.clone());
                                    }
                                    q.position += 1;
                                    if let Some(track) = q.track_at(q.position) {
                                        audio.play(track.path.clone());
                                    }
                                    format!(
                                        "Playing now: {} tracks from {}\n",
                                        files.len(),
                                        path_str
                                    )
                                }
                                "next" => {
                                    for file in &files {
                                        q.insert_after_current(file.clone());
                                    }
                                    format!(
                                        "Queued next: {} tracks from {}\n",
                                        files.len(),
                                        path_str
                                    )
                                }
                                _ => {
                                    for file in &files {
                                        q.add_track(file.clone());
                                    }
                                    if let Some(track) = q.current() {
                                        audio.play(track.path.clone());
                                    }
                                    format!("Queued {} tracks from: {}\n", files.len(), path_str)
                                }
                            }
                        } else {
                            "Error accessing queue\n".to_string()
                        }
                    }
                    _ => format!("Error: No audio files found in '{}'\n", path_str),
                }
            } else {
                format!("File not found: {}\n", path_str)
            }
        }
        _ if cmd == "volume" => {
            let vol = audio.get_volume();
            format!("Volume: {}\n", vol)
        }
        _ if cmd.starts_with("volume ") => {
            if let Ok(level) = cmd[7..].trim().parse::<u8>() {
                audio.set_volume(level as f32 / 100.0);
                // Save to config
                if let Ok(mut config) = Config::load() {
                    config.volume = Some(level);
                    let _ = config.save();
                }
                format!("Volume set to {}\n", level)
            } else {
                "Usage: volume <0-100>\n".to_string()
            }
        }
        _ if cmd.starts_with("shuffle ") => {
            let mode_str = cmd[8..].trim();
            if let Ok(mut q) = queue.lock() {
                if mode_str == "on" || mode_str == "all" {
                    q.set_shuffle(ShuffleMode::All);
                    "Shuffle: on\n".to_string()
                } else if mode_str == "off" || mode_str == "none" {
                    q.set_shuffle(ShuffleMode::None);
                    "Shuffle: off\n".to_string()
                } else {
                    "Usage: shuffle [on|off]\n".to_string()
                }
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        _ if cmd.starts_with("queue") => {
            if let Ok(q) = queue.lock() {
                if q.tracks.is_empty() {
                    "Queue is empty\n".to_string()
                } else {
                    let mut output = String::new();
                    let current_position = q.position;
                    // Use shuffled_indices for display order if shuffle is on
                    let indices: Vec<usize> = if let ShuffleMode::None = q.shuffle {
                        (0..q.tracks.len()).collect()
                    } else {
                        q.shuffled_indices.clone()
                    };
                    for (display_idx, &track_idx) in indices.iter().enumerate() {
                        if let Some(track) = q.tracks.get(track_idx) {
                            let track_str = track.display_string(display_idx);
                            if track_idx == current_position {
                                output.push_str(&format!("> {} (playing)\n", track_str));
                            } else {
                                output.push_str(&format!("  {}\n", track_str));
                            }
                        }
                    }
                    // Use pager if output is long (more than 24 lines)
                    if output.lines().count() > 24 {
                        use std::process::{Command, Stdio};
                        let pager = Command::new("less").arg("-R").stdin(Stdio::piped()).spawn();
                        if let Ok(mut child) = pager {
                            use std::io::Write;
                            let _ = child.stdin.as_mut().unwrap().write_all(output.as_bytes());
                            let _ = child.wait();
                            "".to_string()
                        } else {
                            output
                        }
                    } else {
                        output
                    }
                }
            } else {
                "Error accessing queue\n".to_string()
            }
        }
        _ => "Unknown command\n".to_string(),
    }
}

fn find_audio_in_dir(dir: &Path) -> Option<Vec<std::path::PathBuf>> {
    use std::ffi::OsStr;

    let mut results = Vec::new();

    if let Ok(entries) = std::fs::read_dir(dir) {
        let mut entries: Vec<_> = entries.filter_map(|e| e.ok()).collect();
        entries.sort_by_key(|e| e.file_name());

        for entry in entries {
            let path = entry.path();
            if path.is_file() {
                if let Some(ext) = path.extension().and_then(OsStr::to_str) {
                    if matches!(
                        ext.to_lowercase().as_str(),
                        "mp3" | "flac" | "ogg" | "m4a" | "wav" | "opus"
                    ) {
                        results.push(path);
                    }
                }
            } else if path.is_dir() {
                if let Some(mut found) = find_audio_in_dir(&path) {
                    results.append(&mut found);
                }
            }
        }
    }

    if results.is_empty() {
        None
    } else {
        Some(results)
    }
}

/// Get the PID file path for the daemon
fn get_pid_path(_config: &Config) -> std::path::PathBuf {
    let runtime_dir = std::env::var("XDG_RUNTIME_DIR")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));
    runtime_dir.join("rsplayer").join("rsplayer.pid")
}

/// Check if a process with the given PID is running
fn is_process_running(pid: u32) -> bool {
    unsafe {
        // send signal 0 to check if process exists
        libc::kill(pid as i32, 0) == 0
    }
}

/// Check if daemon is running by reading PID file
fn check_running_daemon(pid_path: &std::path::Path) -> Result<u32, Box<dyn std::error::Error>> {
    if !pid_path.exists() {
        return Err("PID file not found".into());
    }

    let contents = std::fs::read_to_string(pid_path)?;
    let pid = contents.trim().parse::<u32>()?;

    if is_process_running(pid) {
        Ok(pid)
    } else {
        // Stale PID file, clean it up
        std::fs::remove_file(pid_path).ok();
        Err("Stale PID file, daemon not running".into())
    }
}

/// Clean up daemon resources (socket and PID file)
fn cleanup_daemon(socket_path: &std::path::Path, pid_path: &std::path::Path) {
    std::fs::remove_file(socket_path).ok();
    std::fs::remove_file(pid_path).ok();
}