milkbucket/cmd/root.go

133 lines
3.2 KiB
Go
Raw Normal View History

2024-08-25 02:05:15 +00:00
/*
Copyright © 2024 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
2024-08-26 02:03:57 +00:00
"log"
2024-08-25 02:05:15 +00:00
"os"
"github.com/spf13/cobra"
"github.com/veandco/go-sdl2/sdl"
)
var preset string
2024-09-03 03:17:11 +00:00
var scriptPath string
2024-08-25 02:05:15 +00:00
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
switch event.Event {
case sdl.WINDOWEVENT_RESIZED:
w, h := event.Data1, event.Data2
2024-08-25 03:03:37 +00:00
m.resize(w, h)
2024-08-25 02:05:15 +00:00
}
}
2024-09-03 03:17:11 +00:00
func handleKeyboardEvent(event *sdl.KeyboardEvent, m *milkDropWindow) {
scancode := event.Keysym.Scancode
2024-09-03 03:44:24 +00:00
if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_RIGHT && event.Repeat == 0 {
2024-09-03 03:17:11 +00:00
m.nextPreset()
2024-09-03 03:44:24 +00:00
} else if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_LEFT && event.Repeat == 0 {
2024-09-03 03:31:25 +00:00
m.prevPreset()
2024-09-03 03:17:11 +00:00
}
}
2024-08-25 02:05:15 +00:00
func handleEvent(event sdl.Event, m *milkDropWindow) bool {
switch event.(type) {
case *sdl.QuitEvent:
return false
case *sdl.WindowEvent:
event := event.(*sdl.WindowEvent)
handleWindowEvent(event, m)
2024-09-03 03:17:11 +00:00
case *sdl.KeyboardEvent:
event := event.(*sdl.KeyboardEvent)
handleKeyboardEvent(event, m)
2024-08-25 02:05:15 +00:00
}
return true
}
func milkbucket(cmd *cobra.Command, args []string) {
stat, err := os.Stdin.Stat()
2024-08-26 02:03:57 +00:00
if err != nil {
panic(err)
} else if (stat.Mode() & os.ModeCharDevice) != 0 {
log.Fatal("nothing to read from stdin")
2024-08-26 02:03:57 +00:00
}
err = sdl.Init(sdl.INIT_VIDEO)
2024-08-25 02:05:15 +00:00
if err != nil {
panic(err)
}
defer sdl.Quit()
2024-09-03 03:17:11 +00:00
m, err := newMilkDropWindow(800, 600, scriptPath)
2024-08-25 02:05:15 +00:00
if err != nil {
panic(err)
}
defer m.destroy()
m.loadPreset(preset)
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
running = handleEvent(event, m)
if !running {
break
}
}
if !running {
break
}
running, err = m.render()
if err != nil {
panic(err)
}
}
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "milkbucket",
2024-09-03 02:03:17 +00:00
Short: "Audio visualizer",
Long: `milkbucket is an audio visualizer.
2024-08-25 02:05:15 +00:00
2024-09-03 02:03:17 +00:00
It uses Milkdrop preset files to generate visualizations from standard input.`,
2024-08-25 02:05:15 +00:00
Run: milkbucket,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().StringVarP(&preset, "preset", "p", "", "Preset file to use")
2024-09-03 03:17:11 +00:00
rootCmd.Flags().StringVarP(&scriptPath, "script", "s", "", "Script file to use")
2024-08-25 02:05:15 +00:00
}