# Rogalian's Notes

## GroovyMarquee

### DietPi installation

Any modern (systemd based) Linux set could be used. I've used a Raspberry Pi 3B+ with DietPi installed, and a 1680x342 LCD display.

1. https://dietpi.com/docs/install/
2. Disable boot wait for network
3. Switch NTP to Daemon + Drift
4. Connect Wi-Fi
5. Disable logging
6. `dietpi-config`
   * 1: Display Options
     * 0: Display Resolution and rotation
       * Display: HDMI-A-1
       * Mode: 1680x342
       * Rotation: 0
     * 16: Display Brightness: 100
     * 103: KMS/DRM: On
   * 3: Performance Options
     * ARM Initial Turbo: 60
   * 9: AutoStart Options
     * 7: Automatic login
       * dietpi     
7. `apt install fbi gesftpserver libdrm-tests`
8. `ln -s /usr/libexec/gesftpserver /usr/lib/sftp-server`
9. `/boot/config.txt`
   ```ini
   hdmi_ignore_edid=0xa5000080
   hdmi_force_hotplug=1
   hdmi_drive=1
   hdmi_group=2
   hdmi_mode=87
   hdmi_timings=1680 1 48 160 212 342 1 3 10 5 0 0 0 60 0 45300000 0
   disable_overscan=1
   disable_splash=1
   dtoverlay=vc4-kms-v3d
   dtoverlay=noaudio
   dtoverlay=disable-bt
   ```
10. `/etc/systemd/system/vcgencmd.service`
    ```sh
    [Unit]
    Description="Fixes vertical display alignment"
    After=graphical.target

    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c 'sleep 2; /usr/bin/vcgencmd hdmi_adjust_clock 60'

    [Install]
    WantedBy=graphical.target
    ```
11. **Remove any video= parameter that has been created in** `/boot/cmdline.txt`. Append/adjust following parameters `net.ifnames=0 console=tty3 logo.nologo quiet vt.global_cursor_default=0`
12. `touch /home/dietpi/.hushlogin` Hide messages from the autologin
13. `echo PS1='' >> /home/dietpi/.bashrc` Hide bash prompt

#### DietPi Systemd units
`/etc/systemd/system/fbi-slideshow.service`

```ini
[Unit]
Description="Display images in /root as a slideshow"
Conflicts=fbi-static.service
After=graphical.target

[Service]
Type=exec
ExecStart=/bin/sh -c '/usr/bin/fbi -autozoom -noverbose -timeout 10 -blend 500 /root/*'
StandardInput=tty
StandardOutput=tty

[Install]
WantedBy=graphical.target
Also=fbi-static-restart.path
```

`/etc/systemd/system/fbi-static-restart.path`

```ini
[Unit]
Description="Monitor /tmp/marquee.png for changes"

[Path]
PathChanged=/tmp/marquee.png
```

`/etc/systemd/system/fbi-static-restart.service`

```ini
[Unit]
Description="Restart the fbi-static service"
ConditionPathExists=/tmp/marquee.png
After=fbi-static-restart.path

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart fbi-static.service
```
   
`/etc/systemd/system/fbi-static.service`

```ini
[Unit]
Description="Display /tmp/marquee.png with fbi"
ConditionPathExists=/tmp/marquee.png
Conflicts=fbi-slideshow.service

[Service]
Type=exec
ExecStart=/usr/bin/fbi -autozoom -noverbose /tmp/marquee.png
StandardInput=tty
StandardOutput=tty
```

1. `systemctl enable fbi-slideshow.service`
2. Place marquee images into /root. Fbi will display its slideshow in alphanumeric order, and in order for the `vcgencmd` service to adjust the screen properly it needs something on-screen that has bright pixels at the very edge top/left/bottom/right. I name the first/default displayed image with a leading exclamation mark (!).
3. Reboot.

### Automatic marquee switching by GroovyArcade

I've used a port-to-port ethernet connection with static IP addresses between my GroovyArcade PC and DietPi 3B+. GroovyArcade set as IP 192.168.1.1, DietPi set as IP 192.168.1.2.

#### Static ethernet configuration for DietPi
`dietpi-config`
* 7 : Network Options: Adapters.
   * eth0
   * Change Mode: STATIC
   * Static IP: 192.168.1.2/24
   * Static Gateway: 0.0.0.0
   * Static DNS: 0.0.0.0

#### Static ethernet configuration for GroovyArcade

`/etc/systemd/network/20-ethernet.network`
```ini
[Match]
Name=en*
Name=eth*

[Network]
Address=192.168.1.1/24

[Route]
Gateway=0.0.0.0
Metric=1024
```

#### SSH public key authentication

To allow GroovyArcade's `arcade` user to connect without a password to the Pi, use the following commands in the gasetup shell:
1. `ssh-keygen -t ed25519 # No passphrase, creates /home/arcade/.ssh/id_ed25519 and /home/arcade/.ssh/id_ed25519.pub`
2. `ssh-copy-id root@192.168.1.2`

#### GroovyArcade galauncher module

This uses the 2nd and 3rd `args` parameters in `/home/arcade/shared/frontends/attract/emulators/` configurations, normally set as `args executable [system] [name]` where `[system]` is also set in the same `.config` file, and `[name]` is the ROM. Images are stored in the matching `/home/arcade/shared/media/[system]/marquee/` directories.

`/opt/galauncher/modules/marquee.sh`
```sh
#!/usr/bin/env bash
# DietPi host running dropbear and gesftpserver
host="root@192.168.1.2"

pre_launch() {
  local system="$1"
  local rom="$2"
  local marquee="/home/arcade/shared/media/$1/marquee/$2.png"

  if [ -f "$marquee" ]; then
    log_info  "Displaying marquee. Args : $@"
    scp "$marquee" "$host:/tmp/marquee.png"
  else
    log_info "No marquee to display..."
    return 1
  fi
}

post_launch() {
  log_info "Displaying slideshow."
  ssh "$host" 'systemctl start fbi-slideshow'
}
```

## Microsoft Surface Go 2 - Debian

1. Repository here: https://github.com/linux-surface/linux-surface/wiki/Package-Repositories
2. Packages to install: linux-image-surface linux-headers-surface linux-surface-secureboot-mok surface-control libwacom-surface thermald libcamera-tools maliit-keyboard
3. nano -w /etc/environment
    QT_QUICK_CONTROLS_STYLE="org.kde.desktop"

## Retroid Pocket Mini V2

### RetroArch

* Video driver: vulkan
* Scaling
    * Aspect Ratio: Core provided
    * Crop Overscan: Off
* Global Shader: crt-easymode-halation.slangp
    * Gamma Input: 3.00
    * Sharpness Horizontal: 0.00
    * Sharpness Vertical: 1.00
    * Mask Type: 6.00
    * Mask Strength Min: 0.50
    * Mask Strength Max: 0.50
    * Geom Warp: 0.01
    * Geom Corner Size: 0.02
    
#### SNES (bsnes)

* Core Options: Video
    * Preferred Aspect Ratio: 1:1 PAR
    * Crop Vertical Overscan: 12 lines
    * Filter: NTSC (RF)
* Core Options: HD Mode 7
    * Scale: 720p
    * Perspective Correction: On
    * Supersampling: On
    * HD->SD Mosaic: On
    
#### Game Boy (Gambatte)

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (Width): 1280 (8x)
    * Custom Aspect Ratio (Height): 1152 (8x)
    * Viewport Anchor Bias X: 0.30
    * Viewport Anchor Bias Y: 0.30

* Shader: handheld/console_border/dmg.slangp
    * Shader Passes: 5
    * Baseline Alpha: 0.20
    * LCD Response Time: 0.42
        * (Tested with Z.A.S.)
    * Video Scale: 7.00
    * Ambient Screen Light: 0.90
    * Shadow Opacity: 1.00
    * Shadow Offset Horiz: -3.00
    * Shadow Offset Vert: 3.00
    * Screen Offset Horiz: 3.00
    * Screen Offset Vert: 5.00
    
#### Game Boy Color (Gambatte)

* Shader: authentic_gbc.slangp

#### PC Engine (Beetle PCE)

* Core Options: Video
    * Initial Scanline: 8
    * Last Scanline: 236

#### NES (Mesen)

* Core Options: Video
    * NTSC Filter: Composite (Blarg)
    * Left Overscan: 12px
    * Right Overscan: 12px
    * Top Overscan: 16px
    * Bottom Overscan: 16px



#### Master System (Genesis Plus GX)
* Core Options: Video
    * Preferred Aspect Ratio: 4:3
    * Hide Master System Borders: Left & Right Borders
    * Blarg NTSC Filter: Composite

----

## PowKiddy RGB30

* Scaling
    * Crop Overscan: Off

### Shaders

#### CRT systems

* Shader: crt/fakelottes
* Shader parameters:
    * Shadow Mask: 3.00
    * Scanline Intensity: 0.00
    * Warp X: 0.00
    * Warp Y: 0.00
    * Mask Dark: 0.50
    * Mask Light: 1.50
    * CRT Gamma: 2.50
    * Monitor Gamma: 2.20
    * Scanline Sine Comp A: 0.00
    * Scanline Base Brightness: 0.95
    
#### LCD systems
* Shader: handheld/lcd3x

### Core Settings

#### Game Boy (Color) - Gambatte

* GB Colorization: Intenal
* Internal Palette: GB - DMG
* Color Correction: GBC Only
* Colour Correction Mode: Accurate
* Interframe Blending: LCD Ghosting (Accurate)

#### Game Boy Advance - mGBA

* Color Correction: Auto
* Interframe Blending: Off
    * Personal preference - GBA benefits from less blur

#### Game Gear - Genesis Plus GX

* Borders: Off

#### Master System - Genesis Plus GX

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (X Position): -30
    * Custom Aspect Ratio (Y Position): 76
    * Custom Aspect Ratio (Width): 756
    * Custom Aspect Ratio (Height): 567

* Video
    * Borders: Off
    * Hide Master System Side Borders: Off
    * Blarg NTSC Filter: Composite

#### Nintendo Entertainment System - FCEUmm

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (X Position): -38
    * Custom Aspect Ratio (Y Position): 0
    * Custom Aspect Ratio (Width): 794
    * Custom Aspect Ratio (Height): 720

* Video
    * Crop Horizontal Left Overscan: 0
    * Crop Horizontal Right Overscan: 0
    * Crop Vertical Left Overscan: 0
    * Crop Vertical Right Overscan: 0
    * NTSC Filter: Composite
* Audio
    * Sound quality: Very High
    * Audio RF Filter: On
    
#### Mega Drive - Genesis Plus GX

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (X Position): -24
    * Custom Aspect Ratio (Y Position): 72
    * Custom Aspect Ratio (Width): 768
    * Custom Aspect Ratio (Height): 538
    
* Video
    * Blarg NTSC Filter: Composite

#### PC Engine - Beetle PCE Fast

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (X Position): -24
    * Custom Aspect Ratio (Y Position): -8
    * Custom Aspect Ratio (Width): 768
    * Custom Aspect Ratio (Height): 728
    
* Video
    * Color Palette: Composite
    * Initial Scanline: 0
    * Last Scanline: 242
    
#### Super Nintendo - Snes9x

* Scaling
    * Aspect Ratio: Custom
    * Custom Aspect Ratio (X Position): -24
    * Custom Aspect Ratio (Y Position): 24
    * Custom Aspect Ratio (Width): 768
    * Custom Aspect Ratio (Height): 672

* Video
    * Crop Overscan: Off
    * Blarg NTSC Filter: Composite

----
    
## 8BitDo M30 2.4g

Hold button combinations for 5 seconds. LED will blink in red to indicate
success.

* Home + Up: X-input
* Home + Left: Nintendo Switch
* Home + Down: D-input (Genesis mini)
* Select + Up: Set Dpad as Dpad
* Select + Left: Set Dpad as left analogue stick
* Select + Right: Set Dpad as right analogue stick
* Select + Down: Swap A/B and X/Y mapping (on Switch mode only)

----

## Irkenlabs JAMMA Expander

From <https://github.com/misteraddons/mistercade_mappings>

1. Add the following section to the end of to the file downloader.ini that
   should be found in the root of the SD

    ```ini
    [misteraddons/mistercade_mappings]
    db_url = https://raw.githubusercontent.com/misteraddons/mistercade_mappings/db/db.json.zip
    ```

2. Run update_all.sh
3. Via SSH, run the following to rename the MiSTerCade
   input mapping for the JAMMA Expander VID/PID:

    ```sh
    cd /media/fat/config/inputs
    shopt -s nocaseglob
    for file in *16d0_10be_v3.map ; do cp $file ${file//16d0_10be/8282_3201} ; done
    shopt -u caseglob
    ```

3. MiSTer main menu > Define joystick buttons
4. Switch JAMMA Expander from Keyboard to gamepad input
5. Press left on Joystick
6. Use JAMMA Expander to input keypress for Space, twice (skipping Joystick detection)
5. Press inputs in this order
    1. Joystick Right
    2. Joystick Left
    3. Joystick Down
    4. Joystick Up
    5. Button 1 (A)
    6. Button 2 (B)
    7. Button 3 (X)
    8. Button 4 (Y)
    9. Button 5 (L)
    10. Button 6 (R)
    11. Coin (Select)
    12. Start (Start)
6. Use JAMMA Expander to input keypresses for Space to skip all other inputs

----

## Useful Shell (bash/zsh) One-liners

### Hyundai i30 USB flash drive format - needs to be FAT32 and small sector size
`mkfs.fat -S 512 /dev/sda1`

### Directly image a block device from a compressed image
`gunzip -c < "image_name_here.img.gz" | sudo dd of=/dev/mmcblk0 bs=10M conv=fsync status=progress`

### Delete duplicate files
`fdupes -rtdN . `

### Zip all sub directories individually
`for i in */; do zip -r "${i%/}.zip" "$i"; done`

### Zip all files individually
`for i in *.*; do zip -r "${i%.*}.zip" "$i"; done`

### Rename photos using EXIF metadata
`exiftool '-filename<CreateDate' -d ~/Pictures/photos/%Y/%Y-%m/%Y%m%d-%H%M%S%%-c.%%le -r -ext cr2 -ext dng -ext jpg -ext JPG -ext Jpg .`

### Remove empty folders
`find . -type d -empty -delete`

### Move all files in subdirectories to current directory
`find . -type f -exec mv {} . \;`

### Convert all WAV files in subdirectories to FLAC. Needs bash shell installed
`find . -type f -name '*.wav' -exec bash -c 'ffmpeg -y -i "$1" -compression_level 12 "${1%.wav}".flac' - '{}' \;`

### Convert VOBs to MKV losslessly, retaining audio and subtitle streams
`ffmpeg -fflags +genpts -analyzeduration 100M -probesize 100M -i "concat:$(echo VTS_03*.VOB|tr \  \|)" -y -dn -map 0:v -map -0:a -map 0:s -c copy -f dvd VTS_03.mkv`

### Rename music in current (subfolders) using ID3 tags
```sh
exiftool -r -P -ext mp3 -ext m4a \
 '-filename<~/Music/${Artist;} (${Year;DateFmt("%Y")}) ${Album;}/${track;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${Artist;} (${Year;DateFmt("%Y")}) ${Album;}/${PartOfSet;}${track;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${Band;} (${Year;DateFmt("%Y")}) ${Album;}/${track;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${Band;} (${Year;DateFmt("%Y")}) ${Album;}/${PartOfSet;}${track;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' .
```
```sh
exiftool -r -P -ext flac \
 '-filename<~/Music/${Artist;} (${Date;DateFmt("%Y")}) ${Album;} [LL]/${TrackNumber;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${Artist;} (${Date;DateFmt("%Y")}) ${Album;} [LL]/${Discnumber;}${TrackNumber;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${AlbumArtist;} (${Date;DateFmt("%Y")}) ${Album;} [LL]/${TrackNumber;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' \
 '-filename<~/Music/${AlbumArtist;} (${Date;DateFmt("%Y")}) ${Album;} [LL]/${Discnumber;}${TrackNumber;$_=sprintf("%02d",$_)} ${Artist;} - ${Title;}.%e' .
```
### Copy directory structure
`rsync -av -f"+ */" -f"- *" ~/Music_FLAC/ ~/Music/`

### Convert all FLAC files into MP3 preserving directory structure and metadata, scaling cover art. Runs across all CPU cores. Run in parent folder!
`fd -t f -e flac -x ffmpeg -i "{}" -c:v mjpeg -vf scale=600:600 -q:a 0 -map_metadata 0 -id3v2_version 3 "~/Music/{.}.mp3"`

### Convert all FLAC files into MP3 preserving metadata, scaling cover art. Runs across all CPU cores. Run in parent folder!
`find . -type f -name "*.flac" -exec bash -c 'NAME="{}" && ffmpeg -i "$NAME" -c:v mjpeg -vf scale=600:600 -q:a 0 -map_metadata 0 -id3v2_version 3 "${NAME/.flac/.mp3}"' \;`

### 2 pass reencode all MKV in subdirectories to MP4 480p 512MB per hour, do not overwrite existing files.
`find . -type f -name '*.mkv' -exec bash -c 'ffmpeg -y -i "$1" -pix_fmt yuv420p -tune film -vf scale=-8:480 -c:v libx264 -preset:v veryslow -b:v 980k -pass 1 -an -f mp4 -map_metadata -1 /dev/null && ffmpeg -i "$1" -pix_fmt yuv420p -tune film -vf scale=-8:480 -c:v libx264 -preset:v veryslow -b:v 980k -pass 2 -af aresample=async=1:min_hard_comp=0.100000:first_pts=0 -c:a aac -b:a 128k -ac 2 -f mp4 -map_metadata -1 ~/Movies_Portable/"${1%.mkv}"-480p.mp4' - '{}' \;`

### Reencode all MKV in directory to MP4 in parent directory for playback LG SmartShare TV. Overwriting existing.
`find . -type f -name '*.mkv' -exec bash -c 'ffmpeg -y -i "$1" -c:v libx264 -tune animation -pix_fmt yuv420p -strict strict -profile:v high -af aresample=async=1:min_hard_comp=0.100000:first_pts=0 -c:a aac -ac 2 -f mp4 -map_metadata -1 "../${1%.mkv}".mp4' - '{}' \;`

### Reencode all MP4 in directory, scale to 1920 wide, film preset
`find . -type f -name '*.mp4' -exec bash -c 'ffmpeg -y -i "$1" -c:v libx264 -vf scale=1920:-8 -tune film -pix_fmt yuv420p -strict strict -profile:v high -af aresample=async=1:min_hard_comp=0.100000:first_pts=0 -c:a aac -ac 2 -f mp4 -map_metadata -1 "${1%.mp4}"NEW.mp4' - '{}' \;`

### Make thumbnails of MKV files
`find . -type f -name '*.mkv' -exec bash -c 'ffmpegthumbnailer -i "$1" -o "${1%.mkv}".jpg -s0 -m' - '{}' \;`

### Max compression settings for 7z
`7z a file.7z -mx9 -mmt 2 -md1024m files_to_compress`

### Strip unnecessary metadata from mp4 files - change mp4 to required extension as appropriate
`find . -type f -name '*.mp4' -exec bash -c 'ffmpeg -y -i "$1" -map_metadata -1 -c copy "${1%.mp4}"new.mp4 && mv -v "${1%.mp4}"new.mp4 "$1"' - '{}' \; `

----
