milkbucket/cmd/root.go

143 lines
3.4 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"
)
2024-09-05 01:27:01 +00:00
var presets []string
2024-09-05 01:04:47 +00:00
var transition bool
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-05 01:04:47 +00:00
m.nextPreset(transition)
2024-09-03 03:44:24 +00:00
} else if event.Type == sdl.KEYDOWN && scancode == sdl.SCANCODE_LEFT && event.Repeat == 0 {
2024-09-05 01:04:47 +00:00
m.prevPreset(transition)
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-04 03:38:13 +00:00
for _, p := range presets {
info, err := os.Stat(p)
if err != nil {
panic(err)
} else if info.IsDir() {
panic("preset " + p + " is a directory")
}
}
2024-09-03 04:33:35 +00:00
2024-09-03 04:22:56 +00:00
m, err := newMilkDropWindow(800, 600, presets)
2024-08-25 02:05:15 +00:00
if err != nil {
panic(err)
}
defer m.destroy()
2024-09-03 04:10:46 +00:00
m.loadPreset(false)
2024-08-25 02:05:15 +00:00
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.
2024-09-05 01:04:47 +00:00
rootCmd.Flags().BoolVarP(&transition, "transition", "t", false, "smoothly transition between presets")
2024-09-05 01:27:01 +00:00
rootCmd.Flags().StringArrayVarP(&presets, "presets", "p", []string{}, "preset files to use")
2024-08-25 02:05:15 +00:00
}