mirror of
https://github.com/clockworkpi/LauncherGoDev.git
synced 2026-03-19 10:22:41 +01:00
sound and brightenss
This commit is contained in:
40
Menu/GameShell/10_Settings/Sound/plugin_init.go
Normal file
40
Menu/GameShell/10_Settings/Sound/plugin_init.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
* need amixer
|
||||
* `sudo apt-get install alsa-utils`
|
||||
*/
|
||||
import (
|
||||
/*
|
||||
"github.com/veandco/go-sdl2/ttf"
|
||||
|
||||
"github.com/cuu/gogame/surface"
|
||||
"github.com/cuu/gogame/event"
|
||||
"github.com/cuu/gogame/rect"
|
||||
"github.com/cuu/gogame/color"
|
||||
*/
|
||||
"github.com/cuu/LauncherGo/sysgo/UI"
|
||||
//"github.com/cuu/LauncherGo/sysgo/DBUS"
|
||||
)
|
||||
|
||||
/******************************************************************************/
|
||||
type SoundPlugin struct {
|
||||
UI.Plugin
|
||||
SoundPage *SoundPage
|
||||
}
|
||||
|
||||
|
||||
func (self *SoundPlugin) Init( main_screen *UI.MainScreen ) {
|
||||
|
||||
}
|
||||
|
||||
func (self *SoundPlugin) Run( main_screen *UI.MainScreen ) {
|
||||
if main_screen != nil {
|
||||
main_screen.PushCurPage()
|
||||
main_screen.SetCurPage(self.SoundPage)
|
||||
main_screen.Draw()
|
||||
main_screen.SwapAndShow()
|
||||
}
|
||||
}
|
||||
|
||||
var APIOBJ SoundPlugin
|
||||
230
Menu/GameShell/10_Settings/Sound/sound_page.go
Normal file
230
Menu/GameShell/10_Settings/Sound/sound_page.go
Normal file
@@ -0,0 +1,230 @@
|
||||
package main
|
||||
|
||||
import(
|
||||
"fmt"
|
||||
"github.com/cuu/gogame/event"
|
||||
"github.com/cuu/LauncherGo/sysgo/UI"
|
||||
)
|
||||
|
||||
type OnChangeCB_T func(int)
|
||||
|
||||
type SliderIcon struct {
|
||||
UI.IconItem
|
||||
Parent *SoundSlider
|
||||
|
||||
}
|
||||
func NewSliderIcon() *SliderIcon {
|
||||
p := &SliderIcon{}
|
||||
p.MyType = ICON_TYPES["EXE"]
|
||||
p.Align = ALIGN["VCenter"]
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
type SliderMultiIcon struct {
|
||||
UI.MultiIconItem
|
||||
Parent *SoundSlider
|
||||
}
|
||||
|
||||
func NewSliderMultiIcon() *SliderMultiIcon {
|
||||
p := &SliderMultiIcon{}
|
||||
p.MyType = ICON_TYPES["EXE"]
|
||||
p.Align = ALIGN["VCenter"]
|
||||
|
||||
p.IconIndex = 0
|
||||
p.IconWidth = 18
|
||||
p.IconHeight = 18
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
type SoundSlider struct {
|
||||
UI.Slider
|
||||
|
||||
BGpng *SliderIcon
|
||||
BGwidth int
|
||||
BGheight int
|
||||
NeedleSurf
|
||||
Scale *SliderMultiIcon
|
||||
Parent *SoundPage
|
||||
|
||||
OnChangeCB OnChangeCB_T
|
||||
|
||||
snd_segs [][2]int
|
||||
}
|
||||
|
||||
func NewSoundSlider() *SoundSlider {
|
||||
p := &SoundSlider{}
|
||||
p.Range = [2]int{0,255}
|
||||
p.Value = 0
|
||||
|
||||
p.BGwidth = 192
|
||||
p.BGheight = 173
|
||||
|
||||
p.snd_segs = [][2]int{ [2]int{0,20},[2]int{21,40},[2]int{41,50},
|
||||
[2]int{51,60},[2]int{61,70},[2]int{71,85},
|
||||
[2]int{86,90},[2]int{91,95},[2]int{96,100}}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (self *SoundSlider) GetCanvasHWND() *sdl.Surface {
|
||||
return self.CanvasHWND
|
||||
}
|
||||
|
||||
func (self *SoundSlider) Init() {
|
||||
self.Width = self.Parent.Width
|
||||
self.Height = self.Parent.Height
|
||||
|
||||
self.BGpng = NewSliderIcon()
|
||||
self.BGpng.ImgSurf = UI.MyIconPool.GetImgSurf("vol")
|
||||
self.BGpng.MyType = UI.ICON_TYPES["STAT"]
|
||||
self.BGpng.Parent = self
|
||||
self.BGpng.Adjust(0,0,self.BGwidth,self.BGheight,0)
|
||||
|
||||
self.Scale = NewSliderMultiIcon()
|
||||
self.Scale.MyType = UI.ICON_TYPES["STAT"]
|
||||
self.Scale.Parent = self
|
||||
self.Scale.ImgSurf = UI.MyIconPool.GetImgSurf("scale")
|
||||
self.Scale.IconWidth = 82
|
||||
self.Scale.IconHeight = 63
|
||||
self.Scale.Adjust(0,0,82,63,0)
|
||||
}
|
||||
|
||||
func (self *SoundSlider) SetValue(vol int) { // pct 0 - 100
|
||||
for i,v := range self.snd_segs {
|
||||
if vol >= v[0] && vol <= v[1] {
|
||||
self.Value = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SoundSlider) Further() {
|
||||
self.Value += 1
|
||||
|
||||
if self.Value >= len(self.snd_segs) -1 {
|
||||
self.Value = len(self.snd_segs) -1
|
||||
}
|
||||
|
||||
vol := self.snd_segs[self.Value][0] + (self.snd_segs[self.Value][1]-self.snd_segs[self.Value][0])/2
|
||||
|
||||
if self.OnChangeCB != nil {
|
||||
self.OnChangeCB(vol)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SoundSlider) StepBack() {
|
||||
self.Value -= 1
|
||||
|
||||
if self.Value < 0 {
|
||||
self.Value = 0
|
||||
}
|
||||
|
||||
vol := self.snd_segs[self.Value][0]
|
||||
|
||||
if self.OnChangeCB != nil {
|
||||
self.OnChangeCB(vol)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SoundSlider) Draw() {
|
||||
self.BGpng.NewCoord(self.Width/2,self.Height/2)
|
||||
self.BGpng.Draw()
|
||||
|
||||
self.Scale.NewCoord(self.Width/2,self.Height/2)
|
||||
|
||||
self.Scale.IconIndex = self.Value
|
||||
|
||||
self.Scale.Draw()
|
||||
|
||||
}
|
||||
|
||||
|
||||
type SoundPage struct {
|
||||
UI.Page
|
||||
|
||||
MySlider *SoundSlider
|
||||
|
||||
}
|
||||
|
||||
func NewSoundPage() *SoundPage {
|
||||
p := &SoundPage{}
|
||||
|
||||
p.PageIconMargin = 20
|
||||
p.SelectedIconTopOffset = 20
|
||||
p.EasingDur = 10
|
||||
p.Align = UI.ALIGN["SLeft"]
|
||||
|
||||
p.FootMsg = [5]string{"Nav","","","Back","Enter"}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (self *SoundPage) Init() {
|
||||
self.CanvasHWND = self.Screen.CanvasHWND
|
||||
self.Width = self.Screen.Width
|
||||
self.Height = self.Screen.Height
|
||||
|
||||
self.MySlider = NewSoundSlider()
|
||||
|
||||
self.MySlider.Parent = self
|
||||
self.MySlider.SetCanvasHWND(self.CanvasHWND)
|
||||
|
||||
self.MySlider.OnChangeCB = self.WhenSliderDrag
|
||||
|
||||
self.MySlider.Init()
|
||||
|
||||
v,err := GetVolume()
|
||||
if err == nil {
|
||||
self.MySlider.SetValue(v)
|
||||
}else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SoundPage) OnLoadCb() {
|
||||
v,err := GetVolume()
|
||||
if err == nil {
|
||||
self.MySlider.SetValue(v)
|
||||
}else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SoundPage) WhenSliderDrag(val int) { //value 0 - 100
|
||||
if value <0 || value > 100 {
|
||||
return
|
||||
}
|
||||
|
||||
SetVolume(val)
|
||||
}
|
||||
|
||||
func (self *SoundPage) KeyDown(ev *event.Event) {
|
||||
|
||||
if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
|
||||
self.ReturnToUpLevelPage()
|
||||
self.Screen.Draw()
|
||||
self.Screen.SwapAndShow()
|
||||
}
|
||||
|
||||
if ev.Data["Key"] == UI.CurKeys["Right"] {
|
||||
self.MySlider.Further()
|
||||
self.Screen.Draw()
|
||||
self.Screen.SwapAndShow()
|
||||
}
|
||||
|
||||
if ev.Data["Key"] == UI.CurKeys["Left"] {
|
||||
self.MySlider.StepBack()
|
||||
self.Screen.Draw()
|
||||
self.Screen.SwapAndShow()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (self *SoundPage) Draw() {
|
||||
self.ClearCanvas()
|
||||
self.MySlider.Draw()
|
||||
}
|
||||
|
||||
105
Menu/GameShell/10_Settings/Sound/volume_linux.go
Normal file
105
Menu/GameShell/10_Settings/Sound/volume_linux.go
Normal file
@@ -0,0 +1,105 @@
|
||||
// +build !windows,!darwin
|
||||
/*
|
||||
* Copied from https://github.com/itchyny/volume-go, MIT License
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var useAmixer bool
|
||||
|
||||
func init() {
|
||||
if _, err := exec.LookPath("pactl"); err != nil {
|
||||
useAmixer = true
|
||||
}
|
||||
}
|
||||
|
||||
func cmdEnv() []string {
|
||||
return []string{"LANG=C", "LC_ALL=C"}
|
||||
}
|
||||
|
||||
func getVolumeCmd() []string {
|
||||
if useAmixer {
|
||||
return []string{"amixer", "get", "Master"}
|
||||
}
|
||||
return []string{"pactl", "list", "sinks"}
|
||||
}
|
||||
|
||||
var volumePattern = regexp.MustCompile(`\d+%`)
|
||||
|
||||
func parseVolume(out string) (int, error) {
|
||||
lines := strings.Split(out, "\n")
|
||||
for _, line := range lines {
|
||||
s := strings.TrimLeft(line, " \t")
|
||||
if useAmixer && strings.Contains(s, "Playback") && strings.Contains(s, "%") ||
|
||||
!useAmixer && strings.HasPrefix(s, "Volume:") {
|
||||
volumeStr := volumePattern.FindString(s)
|
||||
return strconv.Atoi(volumeStr[:len(volumeStr)-1])
|
||||
}
|
||||
}
|
||||
return 0, errors.New("no volume found")
|
||||
}
|
||||
|
||||
func setVolumeCmd(volume int) []string {
|
||||
if useAmixer {
|
||||
return []string{"amixer", "set", "Master", strconv.Itoa(volume) + "%"}
|
||||
}
|
||||
return []string{"pactl", "set-sink-volume", "0", strconv.Itoa(volume) + "%"}
|
||||
}
|
||||
|
||||
func increaseVolumeCmd(diff int) []string {
|
||||
var sign string
|
||||
if diff >= 0 {
|
||||
sign = "+"
|
||||
} else if useAmixer {
|
||||
diff = -diff
|
||||
sign = "-"
|
||||
}
|
||||
if useAmixer {
|
||||
return []string{"amixer", "set", "Master", strconv.Itoa(diff) + "%" + sign}
|
||||
}
|
||||
return []string{"pactl", "--", "set-sink-volume", "0", sign + strconv.Itoa(diff) + "%"}
|
||||
}
|
||||
|
||||
func getMutedCmd() []string {
|
||||
if useAmixer {
|
||||
return []string{"amixer", "get", "Master"}
|
||||
}
|
||||
return []string{"pactl", "list", "sinks"}
|
||||
}
|
||||
|
||||
func parseMuted(out string) (bool, error) {
|
||||
lines := strings.Split(out, "\n")
|
||||
for _, line := range lines {
|
||||
s := strings.TrimLeft(line, " \t")
|
||||
if useAmixer && strings.Contains(s, "Playback") && strings.Contains(s, "%") ||
|
||||
!useAmixer && strings.HasPrefix(s, "Mute: ") {
|
||||
if strings.Contains(s, "[off]") || strings.Contains(s, "yes") {
|
||||
return true, nil
|
||||
} else if strings.Contains(s, "[on]") || strings.Contains(s, "no") {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, errors.New("no muted information found")
|
||||
}
|
||||
|
||||
func muteCmd() []string {
|
||||
if useAmixer {
|
||||
return []string{"amixer", "-D", "pulse", "set", "Master", "mute"}
|
||||
}
|
||||
return []string{"pactl", "set-sink-mute", "0", "1"}
|
||||
}
|
||||
|
||||
func unmuteCmd() []string {
|
||||
if useAmixer {
|
||||
return []string{"amixer", "-D", "pulse", "set", "Master", "unmute"}
|
||||
}
|
||||
return []string{"pactl", "set-sink-mute", "0", "0"}
|
||||
}
|
||||
68
Menu/GameShell/10_Settings/Sound/volume_unix.go
Normal file
68
Menu/GameShell/10_Settings/Sound/volume_unix.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// +build !windows
|
||||
/*
|
||||
* Copied from https://github.com/itchyny/volume-go, MIT License
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func execCmd(cmdArgs []string) ([]byte, error) {
|
||||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
||||
cmd.Env = append(os.Environ(), cmdEnv()...)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(cmdArgs, " "), err)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
// GetVolume returns the current volume (0 to 100).
|
||||
func GetVolume() (int, error) {
|
||||
out, err := execCmd(getVolumeCmd())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return parseVolume(string(out))
|
||||
}
|
||||
|
||||
// SetVolume sets the sound volume to the specified value.
|
||||
func SetVolume(volume int) error {
|
||||
if volume < 0 || 100 < volume {
|
||||
return errors.New("out of valid volume range")
|
||||
}
|
||||
_, err := execCmd(setVolumeCmd(volume))
|
||||
return err
|
||||
}
|
||||
|
||||
// IncreaseVolume increases (or decreases) the audio volume by the specified value.
|
||||
func IncreaseVolume(diff int) error {
|
||||
_, err := execCmd(increaseVolumeCmd(diff))
|
||||
return err
|
||||
}
|
||||
|
||||
// GetMuted returns the current muted status.
|
||||
func GetMuted() (bool, error) {
|
||||
out, err := execCmd(getMutedCmd())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return parseMuted(string(out))
|
||||
}
|
||||
|
||||
// Mute mutes the audio.
|
||||
func Mute() error {
|
||||
_, err := execCmd(muteCmd())
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmute unmutes the audio.
|
||||
func Unmute() error {
|
||||
_, err := execCmd(unmuteCmd())
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user