| @@ -285,12 +285,11 @@ func (c *MPVPlaylistController) episodeLabel(ep int, mode string) string { |
| 285 | 285 | base += " [Filler]" |
| 286 | 286 | } |
| 287 | 287 | mode = normalizeTranslationType(mode) |
| 288 | | if mode != "" && mode != c.preferredMode { |
| 288 | // Tag the row when its translation mode differs from what is currently |
| 289 | // playing, so the alternate track is always obvious (in dub, a plain sub row |
| 290 | // still shows "(SUB)"; in sub, the dub row shows "(DUB)"). |
| 291 | if c.currentMode != "" && mode != c.currentMode { |
| 289 | 292 | base += " (" + strings.ToUpper(mode) + ")" |
| 290 | | } else if mode == "dub" && c.preferredMode == "dub" { |
| 291 | | // Preferred is dub — no extra tag on every row |
| 292 | | } else if mode == "dub" { |
| 293 | | base += " (DUB)" |
| 294 | 293 | } |
| 295 | 294 | if ep == c.currentPlaying && mode == c.currentMode { |
| 296 | 295 | base += " ◀" |
| @@ -582,14 +581,23 @@ func (c *MPVPlaylistController) watchPlaylistSelection() { |
| 582 | 581 | // missed jumps (logs showed time-pos drop with no "pos changed"). |
| 583 | 582 | Log("MPV playlist: watching path + playlist-pos for episode picks") |
| 584 | 583 | poll := 150 * time.Millisecond |
| 585 | | var lastSeenPath string |
| 584 | // Sample tracking independent of the (possibly stale) cached lastPos so we |
| 585 | // never silently miss an mpv path/pos transition. |
| 586 | var samplePath string |
| 587 | var samplePos int |
| 588 | haveSample := false |
| 589 | lastHeartbeat := time.Time{} |
| 586 | 590 | |
| 587 | 591 | for { |
| 588 | 592 | if c.closed() { |
| 589 | 593 | return |
| 590 | 594 | } |
| 591 | 595 | if !IsMPVRunning(c.socket) { |
| 592 | | return |
| 596 | // A transient IPC blip (mpv can briefly refuse the unix socket while |
| 597 | // alive) must NOT kill the only episode-selection watcher. Retry on |
| 598 | // the next tick; true termination is signalled via the done channel. |
| 599 | time.Sleep(poll) |
| 600 | continue |
| 593 | 601 | } |
| 594 | 602 | if c.suppress.Load() || MPVPlaylistIsSwitching() { |
| 595 | 603 | time.Sleep(poll) |
| @@ -599,44 +607,64 @@ func (c *MPVPlaylistController) watchPlaylistSelection() { |
| 599 | 607 | path := mpvStringProperty(c.socket, "path") |
| 600 | 608 | pos, posErr := c.playlistPos() |
| 601 | 609 | |
| 602 | | // --- Primary: path became a placeholder for episode N --- |
| 603 | | if ep, ok := parseEpisodeFromPlaceholder(path); ok { |
| 604 | | // Debounce: confirm path still that placeholder briefly (scrub). |
| 610 | // Heartbeat ~once a second: captures mpv's real reported state over time |
| 611 | // even when nothing changes, so we can prove what mpv does on a click. |
| 612 | if time.Since(lastHeartbeat) >= time.Second { |
| 613 | lastHeartbeat = time.Now() |
| 614 | count, _ := c.playlistCount() |
| 615 | Log(fmt.Sprintf("MPV playlist: HB pos=%d count=%d path=%s", pos, count, truncateForLog(path, 45))) |
| 616 | } |
| 617 | |
| 618 | if !haveSample || path != samplePath || (posErr == nil && pos != samplePos) { |
| 619 | samplePath = path |
| 620 | samplePos = pos |
| 621 | haveSample = true |
| 622 | if isPlaceholderPath(path) { |
| 623 | if ep, ok := parseEpisodeFromPlaceholder(path); ok { |
| 624 | Log(fmt.Sprintf("MPV playlist: SAMPLE placeholder ep=%d path=%s", ep, truncateForLog(path, 60))) |
| 625 | } else { |
| 626 | Log(fmt.Sprintf("MPV playlist: SAMPLE placeholder path=%s", truncateForLog(path, 60))) |
| 627 | } |
| 628 | } else if posErr == nil { |
| 629 | Log(fmt.Sprintf("MPV playlist: SAMPLE pos=%d path=%s", pos, truncateForLog(path, 50))) |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // --- Primary: path became a placeholder (a row was picked) --- |
| 634 | // Resolve the episode from the frozen index + row title ONLY. The lavfi |
| 635 | // filename's WxH encoding is unreliable (decodes to random episodes like |
| 636 | // 192/300) and must never drive which episode we load. |
| 637 | if isPlaceholderPath(path) { |
| 638 | // Debounce: confirm the placeholder is still the active path (scrub). |
| 605 | 639 | time.Sleep(80 * time.Millisecond) |
| 606 | | path2 := mpvStringProperty(c.socket, "path") |
| 607 | | ep2, ok2 := parseEpisodeFromPlaceholder(path2) |
| 608 | | if !ok2 { |
| 640 | if !isPlaceholderPath(mpvStringProperty(c.socket, "path")) { |
| 609 | 641 | continue |
| 610 | 642 | } |
| 611 | | ep = ep2 |
| 612 | | path = path2 |
| 613 | | |
| 614 | | mode := c.preferredMode |
| 615 | | if posErr == nil && pos >= 0 { |
| 616 | | if _, title := c.playlistEntryMeta(pos); title != "" { |
| 617 | | if _, m, ok3 := parsePlaylistEpisodeTitle(title, c.preferredMode); ok3 { |
| 618 | | mode = m |
| 619 | | } |
| 620 | | } |
| 643 | |
| 644 | targetPos := pos |
| 645 | if posErr != nil || pos < 0 { |
| 646 | targetPos, _ = c.playlistPos() |
| 647 | } |
| 648 | slot, slotErr := c.resolveSlotAt(targetPos) |
| 649 | if slotErr != nil { |
| 650 | Log(fmt.Sprintf("MPV playlist: placeholder pick unresolvable at pos %d: %v", targetPos, slotErr)) |
| 651 | c.lastPos = targetPos |
| 652 | continue |
| 621 | 653 | } |
| 622 | 654 | |
| 623 | 655 | c.mu.Lock() |
| 624 | 656 | curEp := c.currentPlaying |
| 625 | 657 | c.mu.Unlock() |
| 626 | 658 | |
| 627 | | Log(fmt.Sprintf("MPV playlist: DETECT path placeholder ep=%d mode=%s (was ep=%d prevPath=%s)", |
| 628 | | ep, mode, curEp, truncateForLog(lastSeenPath, 50))) |
| 659 | Log(fmt.Sprintf("MPV playlist: DETECT placeholder row pos=%d → ep %d (%s) (was ep=%d)", |
| 660 | targetPos, slot.Episode, slot.Mode, curEp)) |
| 629 | 661 | |
| 630 | 662 | beginMPVPlaylistSwitch() |
| 631 | 663 | _, _ = MPVSendCommand(c.socket, []interface{}{"set_property", "pause", true}) |
| 632 | | _, _ = MPVSendCommand(c.socket, []interface{}{"show-text", fmt.Sprintf("Loading episode %d…", ep), 5000}) |
| 664 | _, _ = MPVSendCommand(c.socket, []interface{}{"show-text", fmt.Sprintf("Loading episode %d…", slot.Episode), 5000}) |
| 633 | 665 | |
| 634 | | if posErr == nil && pos >= 0 { |
| 635 | | c.lastPos = pos |
| 636 | | } |
| 637 | | slot := playlistSlot{Episode: ep, Mode: mode, Label: c.episodeLabel(ep, mode)} |
| 666 | c.lastPos = targetPos |
| 638 | 667 | c.handlePlaylistJumpSlot(slot, curEp) |
| 639 | | lastSeenPath = mpvStringProperty(c.socket, "path") |
| 640 | 668 | continue |
| 641 | 669 | } |
| 642 | 670 | |
| @@ -659,8 +687,7 @@ func (c *MPVPlaylistController) watchPlaylistSelection() { |
| 659 | 687 | _, _ = MPVSendCommand(c.socket, []interface{}{"set_property", "pause", true}) |
| 660 | 688 | c.lastPos = targetPos |
| 661 | 689 | c.handlePlaylistJump(targetPos) |
| 662 | | lastSeenPath = mpvStringProperty(c.socket, "path") |
| 663 | | continue |
| 690 | continue |
| 664 | 691 | } |
| 665 | 692 | |
| 666 | 693 | c.mu.Lock() |
| @@ -679,8 +706,7 @@ func (c *MPVPlaylistController) watchPlaylistSelection() { |
| 679 | 706 | beginMPVPlaylistSwitch() |
| 680 | 707 | _, _ = MPVSendCommand(c.socket, []interface{}{"set_property", "pause", true}) |
| 681 | 708 | c.handlePlaylistJumpSlot(slot, curEp) |
| 682 | | lastSeenPath = mpvStringProperty(c.socket, "path") |
| 683 | | } |
| 709 | } |
| 684 | 710 | time.Sleep(poll) |
| 685 | 711 | continue |
| 686 | 712 | } |
| @@ -690,12 +716,11 @@ func (c *MPVPlaylistController) watchPlaylistSelection() { |
| 690 | 716 | _, _ = MPVSendCommand(c.socket, []interface{}{"show-text", fmt.Sprintf("Loading episode %d…", slot.Episode), 5000}) |
| 691 | 717 | c.lastPos = targetPos |
| 692 | 718 | c.handlePlaylistJumpSlot(slot, curEp) |
| 693 | | lastSeenPath = mpvStringProperty(c.socket, "path") |
| 694 | 719 | continue |
| 695 | 720 | } |
| 696 | 721 | |
| 697 | 722 | if path != "" { |
| 698 | | lastSeenPath = path |
| 723 | |
| 699 | 724 | } |
| 700 | 725 | time.Sleep(poll) |
| 701 | 726 | } |
| @@ -721,22 +746,15 @@ func (c *MPVPlaylistController) handlePlaylistJumpSlot(slot playlistSlot, curEp |
| 721 | 746 | if c.config != nil && c.config.PercentageToMarkComplete > 0 { |
| 722 | 747 | threshold = c.config.PercentageToMarkComplete |
| 723 | 748 | } |
| 724 | | action, pErr := promptPlaylistEpisodeLeave(curEp, slot.Episode, pct, threshold) |
| 725 | | if pErr != nil { |
| 726 | | Log(fmt.Sprintf("MPV playlist leave prompt: %v (playing target anyway)", pErr)) |
| 727 | | action = playlistLeaveNone |
| 728 | | } |
| 729 | | if action == playlistLeaveCancel { |
| 730 | | Log("MPV playlist: user cancelled — reloading current ep real stream") |
| 731 | | // We're sitting on a dummy placeholder; put real current ep back. |
| 732 | | if err := c.playSlot(playlistSlot{Episode: curEp, Mode: curMode, Label: c.episodeLabel(curEp, curMode)}); err != nil { |
| 733 | | Log(fmt.Sprintf("MPV playlist: reload current failed: %v", err)) |
| 734 | | endMPVPlaylistSwitch() |
| 735 | | } |
| 736 | | return |
| 737 | | } |
| 738 | | leaveAction = action |
| 749 | // Never interrupt playback with a terminal menu while the user is in |
| 750 | // (possibly fullscreen) MPV. Resolve the remote action silently: backward |
| 751 | // jumps never regress upstream progress, +1 follows the standard "next" |
| 752 | // auto-mark behavior, other forward skips leave progress untouched. |
| 753 | leaveAction = resolvePlaylistLeaveDefault(curEp, slot.Episode, pct, threshold) |
| 754 | Log(fmt.Sprintf("MPV playlist: SWITCH %d → %d (%s) leave=%s (silent, no session menu)", |
| 755 | curEp, slot.Episode, slot.Mode, leaveAction)) |
| 739 | 756 | } else { |
| 757 | leaveAction = playlistLeaveNone |
| 740 | 758 | Log(fmt.Sprintf("MPV playlist: reloading real stream for ep %d (was on placeholder)", curEp)) |
| 741 | 759 | } |
| 742 | 760 | |
| @@ -851,6 +869,28 @@ func maxInt(a, b int) int { |
| 851 | 869 | return b |
| 852 | 870 | } |
| 853 | 871 | |
| 872 | // resolvePlaylistLeaveDefault decides what to do with upstream progress after an |
| 873 | // MPV-playlist episode pick WITHOUT prompting mid-playback. Rules: |
| 874 | // - same episode → nothing (mode toggle / reload) |
| 875 | // - going backward (toEp < fromEp) → nothing; never regress the tracker |
| 876 | // - forward next (+1) nearly finished → mark the left episode watched |
| 877 | // - any other forward jump → nothing (surfaced at session end) |
| 878 | func resolvePlaylistLeaveDefault(fromEp, toEp int, percentageWatched float64, threshold int) playlistLeaveAction { |
| 879 | if toEp <= 0 || toEp == fromEp { |
| 880 | return playlistLeaveNone |
| 881 | } |
| 882 | if toEp < fromEp { |
| 883 | return playlistLeaveNone |
| 884 | } |
| 885 | if toEp == fromEp+1 { |
| 886 | nearlyDone := int(percentageWatched) >= threshold && percentageWatched > 0 |
| 887 | if nearlyDone { |
| 888 | return playlistLeaveMarkLeft |
| 889 | } |
| 890 | } |
| 891 | return playlistLeaveNone |
| 892 | } |
| 893 | |
| 854 | 894 | // resolveSlotAt figures out which episode the user selected at playlist index pos. |
| 855 | 895 | // Order: placeholder filename encode → title → our slots map. |
| 856 | 896 | // Never re-query playlist-pos here — caller freezes the index. |
| @@ -858,18 +898,6 @@ func (c *MPVPlaylistController) resolveSlotAt(pos int) (playlistSlot, error) { |
| 858 | 898 | filename, title := c.playlistEntryMeta(pos) |
| 859 | 899 | Log(fmt.Sprintf("MPV playlist: resolve pos=%d filename=%q title=%q", pos, truncateForLog(filename, 60), title)) |
| 860 | 900 | |
| 861 | | if ep, ok := parseEpisodeFromPlaceholder(filename); ok { |
| 862 | | mode := c.preferredMode |
| 863 | | if _, m, ok2 := parsePlaylistEpisodeTitle(title, c.preferredMode); ok2 { |
| 864 | | mode = m |
| 865 | | } |
| 866 | | Log(fmt.Sprintf("MPV playlist: pos %d filename → ep %d (title=%q)", pos, ep, title)) |
| 867 | | label := title |
| 868 | | if label == "" { |
| 869 | | label = c.episodeLabel(ep, mode) |
| 870 | | } |
| 871 | | return playlistSlot{Episode: ep, Mode: mode, Label: label}, nil |
| 872 | | } |
| 873 | 901 | if title != "" { |
| 874 | 902 | if ep, mode, ok := parsePlaylistEpisodeTitle(title, c.preferredMode); ok { |
| 875 | 903 | Log(fmt.Sprintf("MPV playlist: pos %d title %q → ep %d (%s)", pos, title, ep, mode)) |
| @@ -1257,6 +1285,10 @@ func (c *MPVPlaylistController) playSlot(slot playlistSlot) error { |
| 1257 | 1285 | c.mu.Lock() |
| 1258 | 1286 | c.currentPlaying = targetEp |
| 1259 | 1287 | c.currentMode = mode |
| 1288 | // The alternate is always the *other* mode from what is now playing, so after |
| 1289 | // a sub→dub switch the offered extra row is sub (not dub again). Recompute |
| 1290 | // here so probeAndAttachAlternateAudio stays symmetric in both directions. |
| 1291 | c.alternateMode = alternateTranslationType(mode) |
| 1260 | 1292 | alreadyAlt := normalizeTranslationType(slot.Mode) == c.alternateMode |
| 1261 | 1293 | // After loadfile replace the playlist is a single entry at index 0. |
| 1262 | 1294 | c.lastPos = 0 |