Fix menu navigation, boolean editing, and unexpected exits #3
No files matched your search
@@ -78,7 +78,7 @@ func (l *settings) HandleButtonPress(ctx app.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.menu = menu
|
l.menu = menu
|
||||||
} else {
|
} else if item.IsEditable() {
|
||||||
log.Debug("enter edit mode")
|
log.Debug("enter edit mode")
|
||||||
l.menu.ToggleEdit()
|
l.menu.ToggleEdit()
|
||||||
}
|
}
|
||||||
@@ -90,12 +90,14 @@ func (l *settings) HandleButtonPress(ctx app.Context) {
|
|||||||
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||||
// Rewind to root menu
|
// Rewind to root menu
|
||||||
for {
|
for {
|
||||||
|
l.menu.Editing = false
|
||||||
parent := l.menu.Parent
|
parent := l.menu.Parent
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
l.menu = parent
|
l.menu = parent
|
||||||
}
|
}
|
||||||
|
l.menuWidget.Update(l.menu)
|
||||||
|
|
||||||
// Close settings layout.
|
// Close settings layout.
|
||||||
ctx.Show(nil)
|
ctx.Show(nil)
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package layout
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/BeatGlow/beatglow/app"
|
||||||
|
"github.com/BeatGlow/beatglow/app/menu"
|
||||||
|
"github.com/BeatGlow/beatglow/app/widget"
|
||||||
|
"github.com/BeatGlow/beatglow/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type settingsTestContext struct {
|
||||||
|
app.Context
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *settingsTestContext) Show(layout app.Layout) { c.closed = layout == nil }
|
||||||
|
|
||||||
|
func TestSettingsBackAndInformation(t *testing.T) {
|
||||||
|
data := config.DefaultSettings
|
||||||
|
root, err := menu.New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
l := &settings{menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)}
|
||||||
|
ctx := &settingsTestContext{}
|
||||||
|
root.Position = 4 // System
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu.Parent != root {
|
||||||
|
t.Fatal("did not enter System submenu")
|
||||||
|
}
|
||||||
|
for _, position := range []int{1, 2} {
|
||||||
|
l.menu.Position = position
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu.IsEditing() {
|
||||||
|
t.Fatal("system information entered edit mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.HandleRotary(ctx, -2)
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu != root || l.menuWidget.Root != root {
|
||||||
|
t.Fatal("Back did not restore the root menu")
|
||||||
|
}
|
||||||
|
if root.Position != 4 {
|
||||||
|
t.Fatal("parent selection was lost")
|
||||||
|
}
|
||||||
|
root.Position = 0
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if !ctx.closed {
|
||||||
|
t.Fatal("root Back did not close settings")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSettingsLongPressSynchronizesMenu(t *testing.T) {
|
||||||
|
data := config.DefaultSettings
|
||||||
|
root, err := menu.New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
root.Position = 1 // Display
|
||||||
|
child := root.EnterSubMenu()
|
||||||
|
child.Position = 1
|
||||||
|
child.ToggleEdit()
|
||||||
|
l := &settings{menu: child, menuWidget: widget.NewMenu(image.Pt(256, 64), child)}
|
||||||
|
ctx := &settingsTestContext{}
|
||||||
|
l.HandleButtonLongPress(ctx)
|
||||||
|
if !ctx.closed || l.menu != root || l.menuWidget.Root != root {
|
||||||
|
t.Fatal("long press left the rendered menu out of sync")
|
||||||
|
}
|
||||||
|
if child.IsEditing() || root.IsEditing() {
|
||||||
|
t.Fatal("long press left an item in edit mode")
|
||||||
|
}
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu != child || l.menu.IsEditing() {
|
||||||
|
t.Fatal("reopening submenu did not restore navigation mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBooleanSelectionSurvivesConfirmation(t *testing.T) {
|
||||||
|
data := config.DefaultSettings
|
||||||
|
data.Player.ShowArt = true
|
||||||
|
root, err := menu.New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
root.Position = 2 // Player
|
||||||
|
l := &settings{settings: &data, menu: root, menuWidget: widget.NewMenu(image.Pt(256, 64), root)}
|
||||||
|
ctx := &settingsTestContext{}
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
l.HandleRotary(ctx, 2) // Show album art
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
for _, delta := range []int{-1, -1, -3} {
|
||||||
|
l.HandleRotary(ctx, delta)
|
||||||
|
}
|
||||||
|
if data.Player.ShowArt {
|
||||||
|
t.Fatal("left did not keep checkbox unchecked")
|
||||||
|
}
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu.IsEditing() || data.Player.ShowArt {
|
||||||
|
t.Fatal("press did not confirm unchecked value")
|
||||||
|
}
|
||||||
|
l.HandleRotary(ctx, -2)
|
||||||
|
l.HandleButtonPress(ctx) // Back to root
|
||||||
|
l.HandleButtonPress(ctx) // Reopen Player
|
||||||
|
l.HandleRotary(ctx, 2) // Show album art
|
||||||
|
l.HandleButtonPress(ctx) // Edit selected Show album art
|
||||||
|
if data.Player.ShowArt {
|
||||||
|
t.Fatal("reopening checkbox reset its value")
|
||||||
|
}
|
||||||
|
for _, delta := range []int{1, 1, 3} {
|
||||||
|
l.HandleRotary(ctx, delta)
|
||||||
|
}
|
||||||
|
l.HandleButtonPress(ctx)
|
||||||
|
if l.menu.IsEditing() || !data.Player.ShowArt {
|
||||||
|
t.Fatal("press did not confirm checked value")
|
||||||
|
}
|
||||||
|
}
|
||||||
+46
-14
@@ -26,6 +26,9 @@ type Menu struct {
|
|||||||
pageSize int
|
pageSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New builds a menu over the exported fields of a struct pointer. It returns an
|
||||||
|
// error for non-struct or unaddressable data, unsupported fields, or invalid tags.
|
||||||
|
// Values remain owned by the caller; menu edits and reads must be serialized.
|
||||||
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
||||||
ref := reflect.ValueOf(data)
|
ref := reflect.ValueOf(data)
|
||||||
|
|
||||||
@@ -41,6 +44,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
|||||||
if ref.Kind() != reflect.Struct {
|
if ref.Kind() != reflect.Struct {
|
||||||
return nil, fmt.Errorf("menu: %T is not a struct", data)
|
return nil, fmt.Errorf("menu: %T is not a struct", data)
|
||||||
}
|
}
|
||||||
|
if !ref.CanAddr() {
|
||||||
|
return nil, fmt.Errorf("menu: %T must be a pointer to a struct", data)
|
||||||
|
}
|
||||||
|
|
||||||
menu := &Menu{
|
menu := &Menu{
|
||||||
Parent: parent,
|
Parent: parent,
|
||||||
@@ -51,16 +57,20 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
|||||||
Value: Back,
|
Value: Back,
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
menu.SetPageSize(pageSize)
|
||||||
|
|
||||||
typ := ref.Type()
|
typ := ref.Type()
|
||||||
for i, n := 0, ref.NumField(); i < n; i++ {
|
for i, n := 0, ref.NumField(); i < n; i++ {
|
||||||
|
field := typ.Field(i)
|
||||||
|
if field.Tag.Get("menu") == "-" || !field.IsExported() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
field = typ.Field(i)
|
|
||||||
fieldRef = ref.Field(i)
|
fieldRef = ref.Field(i)
|
||||||
value = ref.Field(i).Addr().Interface()
|
value = ref.Field(i).Addr().Interface()
|
||||||
icon = field.Tag.Get("icon")
|
icon = field.Tag.Get("icon")
|
||||||
label = field.Tag.Get("menu")
|
label = field.Tag.Get("menu")
|
||||||
options = strings.Split(field.Tag.Get("options"), ",")
|
options []string
|
||||||
step = 1
|
step = 1
|
||||||
min, max int
|
min, max int
|
||||||
err error
|
err error
|
||||||
@@ -68,8 +78,9 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
|||||||
|
|
||||||
if len(label) == 0 {
|
if len(label) == 0 {
|
||||||
label = field.Name
|
label = field.Name
|
||||||
} else if label == "-" {
|
}
|
||||||
continue
|
if tag := field.Tag.Get("options"); tag != "" {
|
||||||
|
options = strings.Split(tag, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
if stepTag := field.Tag.Get("step"); stepTag != "" {
|
if stepTag := field.Tag.Get("step"); stepTag != "" {
|
||||||
@@ -89,10 +100,10 @@ func New(data any, pageSize int, parent *Menu) (*Menu, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !fieldRef.CanAddr() {
|
if !fieldRef.CanAddr() {
|
||||||
panic(fmt.Sprintf("fieldref %T for %T is not addressable", fieldRef, value))
|
return nil, fmt.Errorf("menu: field %s is not addressable", field.Name)
|
||||||
}
|
}
|
||||||
if !fieldRef.CanSet() {
|
if !fieldRef.CanSet() {
|
||||||
panic(fmt.Sprintf("fieldref %T for %T is not settable", fieldRef, value))
|
return nil, fmt.Errorf("menu: field %s is not settable", field.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch field.Type.Kind() {
|
switch field.Type.Kind() {
|
||||||
@@ -198,9 +209,9 @@ func (m *Menu) edit(delta int) {
|
|||||||
// Adjust the value if editing
|
// Adjust the value if editing
|
||||||
switch v := item.Value.(type) {
|
switch v := item.Value.(type) {
|
||||||
case *uint8:
|
case *uint8:
|
||||||
newValue := *v + uint8(delta*item.Step)
|
newValue := int(*v) + delta*item.Step
|
||||||
if newValue >= uint8(item.Min) && newValue <= uint8(item.Max) {
|
if newValue >= item.Min && newValue <= item.Max {
|
||||||
*v = newValue
|
*v = uint8(newValue)
|
||||||
}
|
}
|
||||||
case *int:
|
case *int:
|
||||||
newValue := *v + (delta * item.Step)
|
newValue := *v + (delta * item.Step)
|
||||||
@@ -209,13 +220,13 @@ func (m *Menu) edit(delta int) {
|
|||||||
}
|
}
|
||||||
case *bool:
|
case *bool:
|
||||||
if delta != 0 {
|
if delta != 0 {
|
||||||
*v = !*v
|
*v = delta > 0
|
||||||
}
|
}
|
||||||
case *string:
|
case *string:
|
||||||
if len(item.Options) > 0 {
|
if len(item.Options) > 0 {
|
||||||
currentIndex := indexOf(*v, item.Options)
|
currentIndex := indexOf(*v, item.Options)
|
||||||
if currentIndex != -1 {
|
if currentIndex != -1 {
|
||||||
newIndex := (currentIndex + delta + len(item.Options)) % len(item.Options)
|
newIndex := (currentIndex + delta%len(item.Options) + len(item.Options)) % len(item.Options)
|
||||||
*v = item.Options[newIndex]
|
*v = item.Options[newIndex]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,8 +265,11 @@ func (m *Menu) PageSize() int {
|
|||||||
return m.pageSize
|
return m.pageSize
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPageSize updates the page size.
|
// SetPageSize updates this menu and its submenus, with a minimum of one item.
|
||||||
func (m *Menu) SetPageSize(pageSize int) {
|
func (m *Menu) SetPageSize(pageSize int) {
|
||||||
|
if pageSize < 1 {
|
||||||
|
pageSize = 1
|
||||||
|
}
|
||||||
m.pageSize = pageSize
|
m.pageSize = pageSize
|
||||||
for _, item := range m.Items {
|
for _, item := range m.Items {
|
||||||
if item.SubMenu != nil {
|
if item.SubMenu != nil {
|
||||||
@@ -269,9 +283,14 @@ func (m *Menu) IsEditing() bool {
|
|||||||
return m.Editing
|
return m.Editing
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToggleEdit switches between navigation and editing modes.
|
// ToggleEdit exits editing or starts editing an editable item. Informational
|
||||||
|
// items, submenu entries and Back remain in navigation mode.
|
||||||
func (m *Menu) ToggleEdit() {
|
func (m *Menu) ToggleEdit() {
|
||||||
m.Editing = !m.Editing
|
if m.Editing {
|
||||||
|
m.Editing = false
|
||||||
|
} else if m.ActiveItem().IsEditable() {
|
||||||
|
m.Editing = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) IsSubMenu() bool {
|
func (m *Menu) IsSubMenu() bool {
|
||||||
@@ -324,6 +343,19 @@ type Item struct {
|
|||||||
Options []string
|
Options []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEditable reports whether the item accepts rotary input. Strings without
|
||||||
|
// enumerated options are informational and remain read-only.
|
||||||
|
func (item Item) IsEditable() bool {
|
||||||
|
switch item.Value.(type) {
|
||||||
|
case *bool, *uint8, *int:
|
||||||
|
return true
|
||||||
|
case *string:
|
||||||
|
return len(item.Options) > 0
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// indexOf returns the index of a value in a slice, or -1 if not found
|
// indexOf returns the index of a value in a slice, or -1 if not found
|
||||||
func indexOf(needle string, haystack []string) int {
|
func indexOf(needle string, haystack []string) int {
|
||||||
for i, opt := range haystack {
|
for i, opt := range haystack {
|
||||||
|
|||||||
+146
-4
@@ -49,10 +49,152 @@ func TestMenuNil(t *testing.T) {
|
|||||||
|
|
||||||
func TestMenuNotPointer(t *testing.T) {
|
func TestMenuNotPointer(t *testing.T) {
|
||||||
var settings testSettings
|
var settings testSettings
|
||||||
menu, err := New(settings, 4, nil)
|
_, err := New(settings, 4, nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for an unaddressable struct")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInformationalItemsRemainNavigable(t *testing.T) {
|
||||||
|
data := struct {
|
||||||
|
LoadAverage string
|
||||||
|
LocalTime string
|
||||||
|
}{LoadAverage: "0.12 0.10 0.08", LocalTime: "12:00"}
|
||||||
|
m, err := New(&data, 4, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, position := range []int{1, 2} {
|
||||||
|
m.Position = position
|
||||||
|
m.ToggleEdit()
|
||||||
|
if m.IsEditing() {
|
||||||
|
t.Fatal("informational item entered edit mode")
|
||||||
|
}
|
||||||
|
m.Change(-position)
|
||||||
|
if m.ActiveItem().Value != Back {
|
||||||
|
t.Fatal("cannot navigate back after selecting information")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if data.LoadAverage != "0.12 0.10 0.08" || data.LocalTime != "12:00" {
|
||||||
|
t.Fatal("informational values changed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubmenuBackPreservesParentPosition(t *testing.T) {
|
||||||
|
var data testSettings
|
||||||
|
root, err := New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
root.Position = 2
|
||||||
|
child := root.EnterSubMenu()
|
||||||
|
if child == nil {
|
||||||
|
t.Fatal("missing submenu")
|
||||||
|
}
|
||||||
|
if child.ActiveItem().Value != Back {
|
||||||
|
t.Fatal("submenu should initially select Back")
|
||||||
|
}
|
||||||
|
if got := child.ExitSubMenu(); got != root || got.Position != 2 {
|
||||||
|
t.Fatal("Back did not restore parent selection")
|
||||||
|
}
|
||||||
|
root.Position = 0
|
||||||
|
root.ToggleEdit()
|
||||||
|
if root.IsEditing() {
|
||||||
|
t.Fatal("Back entered edit mode")
|
||||||
|
}
|
||||||
|
if root.ExitSubMenu() != nil {
|
||||||
|
t.Fatal("root should exit settings")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStringOptionsLargeNegativeDelta(t *testing.T) {
|
||||||
|
data := struct {
|
||||||
|
Sleep string `options:"5min,15min,hour,day"`
|
||||||
|
}{Sleep: "hour"}
|
||||||
|
m, err := New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m.Position = 1
|
||||||
|
m.ToggleEdit()
|
||||||
|
if !m.IsEditing() {
|
||||||
|
t.Fatal("option item should be editable")
|
||||||
|
}
|
||||||
|
m.Change(-11)
|
||||||
|
if data.Sleep != "day" {
|
||||||
|
t.Fatalf("got %q, want day", data.Sleep)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUint8DoesNotWrap(t *testing.T) {
|
||||||
|
data := struct{ Contrast uint8 }{Contrast: 255}
|
||||||
|
m, err := New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m.Position = 1
|
||||||
|
m.ToggleEdit()
|
||||||
|
m.Change(1)
|
||||||
|
if data.Contrast != 255 {
|
||||||
|
t.Fatal("contrast wrapped above maximum")
|
||||||
|
}
|
||||||
|
data.Contrast = 0
|
||||||
|
m.Change(-1)
|
||||||
|
if data.Contrast != 0 {
|
||||||
|
t.Fatal("contrast wrapped below minimum")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMinimumPageSize(t *testing.T) {
|
||||||
|
var data testSettings
|
||||||
|
m, err := New(&data, 0, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if m.PageSize() != 1 {
|
||||||
|
t.Fatal("page size must be positive")
|
||||||
|
}
|
||||||
|
m.SetPageSize(-1)
|
||||||
|
if m.Items[1].SubMenu.PageSize() != 1 {
|
||||||
|
t.Fatal("submenu page size must be positive")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBooleanEditingUsesDirection(t *testing.T) {
|
||||||
|
for _, initial := range []bool{false, true} {
|
||||||
|
data := struct{ Enabled bool }{Enabled: initial}
|
||||||
|
m, err := New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m.Position = 1
|
||||||
|
m.ToggleEdit()
|
||||||
|
for _, delta := range []int{1, 1, 3, 0, -1, -1, -4, 0, 1} {
|
||||||
|
before := data.Enabled
|
||||||
|
m.Change(delta)
|
||||||
|
want := delta > 0
|
||||||
|
if delta == 0 {
|
||||||
|
want = before
|
||||||
|
}
|
||||||
|
if data.Enabled != want {
|
||||||
|
t.Fatalf("initial=%v delta=%d: got %v, want %v", initial, delta, data.Enabled, want)
|
||||||
|
}
|
||||||
|
if m.Position != 1 || !m.IsEditing() {
|
||||||
|
t.Fatal("rotating left boolean edit mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.ToggleEdit()
|
||||||
|
if m.IsEditing() || !data.Enabled {
|
||||||
|
t.Fatal("confirming did not preserve the selected value")
|
||||||
|
}
|
||||||
|
m.Change(-1)
|
||||||
|
if m.Position != 0 || !data.Enabled {
|
||||||
|
t.Fatal("navigation changed the confirmed value")
|
||||||
|
}
|
||||||
|
m.Change(1)
|
||||||
|
m.ToggleEdit()
|
||||||
|
if !data.Enabled {
|
||||||
|
t.Fatal("reopening edit mode reset the value")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("menu: %#+v", menu)
|
|
||||||
}
|
}
|
||||||
+7
-2
@@ -22,6 +22,7 @@ func NewMenu(size image.Point, root *menu.Menu) *Menu {
|
|||||||
w := new(Menu)
|
w := new(Menu)
|
||||||
w.Extend(w)
|
w.Extend(w)
|
||||||
w.Resize(size)
|
w.Resize(size)
|
||||||
|
w.Update(root)
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ func (w *Menu) Redraw() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Menu) Refresh(ctx app.Context) bool {
|
func (w *Menu) Refresh(ctx app.Context) bool {
|
||||||
if !w.IsVisible() {
|
if !w.IsVisible() || w.Root == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) {
|
if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) {
|
||||||
if w.buffer == nil {
|
if w.buffer == nil || w.buffer.Bounds().Size() != size {
|
||||||
w.buffer = app.BufferFor(ctx.Display().ColorModel(), size)
|
w.buffer = app.BufferFor(ctx.Display().ColorModel(), size)
|
||||||
w.Root.SetPageSize(size.Y / 16)
|
w.Root.SetPageSize(size.Y / 16)
|
||||||
}
|
}
|
||||||
@@ -63,6 +64,8 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
|||||||
w.curr, w.next = w.next, nil
|
w.curr, w.next = w.next, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.curr.SetPageSize(size.Y / 16)
|
||||||
|
|
||||||
// Clear the buffer.
|
// Clear the buffer.
|
||||||
app.ImageClear(w.buffer)
|
app.ImageClear(w.buffer)
|
||||||
|
|
||||||
@@ -101,6 +104,8 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
|||||||
y := size.Y / len(menu.Items) * menu.Position
|
y := size.Y / len(menu.Items) * menu.Position
|
||||||
draw.Box(w.buffer, image.Rect(size.X-3, y, size.X, y+size.Y/len(menu.Items)), color.Gray{Y: 0x7f})
|
draw.Box(w.buffer, image.Rect(size.X-3, y, size.X, y+size.Y/len(menu.Items)), color.Gray{Y: 0x7f})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load()))
|
app.ImageCopyBounds(ctx.Display(), w.buffer, bounds.Add(w.position.Load()))
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package widget
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/BeatGlow/beatglow/app"
|
||||||
|
"github.com/BeatGlow/beatglow/app/menu"
|
||||||
|
"github.com/BeatGlow/display"
|
||||||
|
"github.com/BeatGlow/display/pixel"
|
||||||
|
)
|
||||||
|
|
||||||
|
type menuTestDisplay struct {
|
||||||
|
display.Display
|
||||||
|
image *image.RGBA
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *menuTestDisplay) Bounds() image.Rectangle { return d.image.Bounds() }
|
||||||
|
func (d *menuTestDisplay) ColorModel() color.Model { return pixel.Gray4Model }
|
||||||
|
func (d *menuTestDisplay) At(x, y int) color.Color { return d.image.At(x, y) }
|
||||||
|
func (d *menuTestDisplay) Set(x, y int, c color.Color) { d.image.Set(x, y, c) }
|
||||||
|
|
||||||
|
type menuTestContext struct {
|
||||||
|
app.Context
|
||||||
|
display display.Display
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *menuTestContext) Display() display.Display { return c.display }
|
||||||
|
|
||||||
|
func TestMenuRefreshOnlyOnChanges(t *testing.T) {
|
||||||
|
data := struct{ Enabled bool }{}
|
||||||
|
root, err := menu.New(&data, 4, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx := &menuTestContext{display: &menuTestDisplay{image: image.NewRGBA(image.Rect(0, 0, 256, 48))}}
|
||||||
|
w := NewMenu(image.Pt(256, 48), root)
|
||||||
|
w.SetStyles(*app.StylesFor(pixel.Gray4Model))
|
||||||
|
w.Show(ctx)
|
||||||
|
if !w.Refresh(ctx) {
|
||||||
|
t.Fatal("initial menu was not drawn")
|
||||||
|
}
|
||||||
|
if w.Refresh(ctx) {
|
||||||
|
t.Fatal("unchanged menu requested another hardware refresh")
|
||||||
|
}
|
||||||
|
root.Change(1)
|
||||||
|
w.Redraw()
|
||||||
|
if !w.Refresh(ctx) || w.Refresh(ctx) {
|
||||||
|
t.Fatal("menu change should trigger exactly one refresh")
|
||||||
|
}
|
||||||
|
w.Hide(ctx)
|
||||||
|
w.Show(ctx)
|
||||||
|
if !w.Refresh(ctx) {
|
||||||
|
t.Fatal("reopened menu was not drawn")
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -41,8 +41,8 @@ type SpectrumSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SystemSettings struct {
|
type SystemSettings struct {
|
||||||
LoadAverage string `label:"Load average"`
|
LoadAverage string `menu:"Load average"`
|
||||||
LocalTime string `label:"Local time"`
|
LocalTime string `menu:"Local time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultSettings = Settings{
|
var DefaultSettings = Settings{
|
||||||
|
|||||||
+6
-12
@@ -115,8 +115,7 @@ func (c *controllerContext) Show(layout app.Layout) {
|
|||||||
|
|
||||||
for _, other := range c.layouts {
|
for _, other := range c.layouts {
|
||||||
if other == layout {
|
if other == layout {
|
||||||
if layout != c.settingsLayout {
|
if layout != c.active {
|
||||||
// Only store a "previous" screen if the active screen isn't the settings.
|
|
||||||
c.previous, c.active = c.active, layout
|
c.previous, c.active = c.active, layout
|
||||||
}
|
}
|
||||||
log.WithField("layout", layout).Debug("show layout")
|
log.WithField("layout", layout).Debug("show layout")
|
||||||
@@ -174,37 +173,32 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) {
|
|||||||
if handler, ok := layout.(app.RotaryInputHandler); ok {
|
if handler, ok := layout.(app.RotaryInputHandler); ok {
|
||||||
log.WithField("layout", layout.String()).Debug("dispatch rotary input")
|
log.WithField("layout", layout.String()).Debug("dispatch rotary input")
|
||||||
handler.HandleRotary(c, event.Delta)
|
handler.HandleRotary(c, event.Delta)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case hardware.RotaryPressEvent:
|
case hardware.RotaryPressEvent:
|
||||||
var handled bool
|
|
||||||
for _, layout := range c.layouts {
|
for _, layout := range c.layouts {
|
||||||
if layout.IsVisible() {
|
if layout.IsVisible() {
|
||||||
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
||||||
log.WithField("layout", layout.String()).Debug("dispatch button press")
|
log.WithField("layout", layout.String()).Debug("dispatch button press")
|
||||||
handler.HandleButtonPress(c)
|
handler.HandleButtonPress(c)
|
||||||
handled = true
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !handled {
|
c.handleRotaryPress()
|
||||||
c.handleRotaryPress()
|
|
||||||
}
|
|
||||||
case hardware.RotaryLongPressEvent:
|
case hardware.RotaryLongPressEvent:
|
||||||
var handled bool
|
|
||||||
for _, layout := range c.layouts {
|
for _, layout := range c.layouts {
|
||||||
if layout.IsVisible() {
|
if layout.IsVisible() {
|
||||||
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
||||||
log.WithField("layout", layout.String()).Debug("dispatch button long press")
|
log.WithField("layout", layout.String()).Debug("dispatch button long press")
|
||||||
handler.HandleButtonLongPress(c)
|
handler.HandleButtonLongPress(c)
|
||||||
handled = true
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !handled {
|
c.handleRotaryLongPress()
|
||||||
c.handleRotaryLongPress()
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
log.Warn("unhandled input event")
|
log.Warn("unhandled input event")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package beatglow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/BeatGlow/beatglow/app"
|
||||||
|
"github.com/BeatGlow/beatglow/hardware"
|
||||||
|
)
|
||||||
|
|
||||||
|
type navigationTestLayout struct {
|
||||||
|
app.Layout
|
||||||
|
name string
|
||||||
|
visible bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *navigationTestLayout) String() string { return l.name }
|
||||||
|
func (l *navigationTestLayout) Show(app.Context) { l.visible = true }
|
||||||
|
func (l *navigationTestLayout) Hide(app.Context) { l.visible = false }
|
||||||
|
func (l *navigationTestLayout) IsVisible() bool { return l.visible }
|
||||||
|
|
||||||
|
func TestSettingsReturnsToActiveLayout(t *testing.T) {
|
||||||
|
settings := &navigationTestLayout{name: "settings"}
|
||||||
|
playing := &navigationTestLayout{name: "playing"}
|
||||||
|
analyzer := &navigationTestLayout{name: "analyzer"}
|
||||||
|
c := &controllerContext{settingsLayout: settings, layouts: []app.Layout{settings, playing, analyzer}}
|
||||||
|
c.Show(playing)
|
||||||
|
c.Show(analyzer)
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
c.Show(settings)
|
||||||
|
if c.active != settings || c.previous != analyzer {
|
||||||
|
t.Fatal("settings did not remember the active screen")
|
||||||
|
}
|
||||||
|
c.Show(nil)
|
||||||
|
if c.active != analyzer || !analyzer.visible || settings.visible {
|
||||||
|
t.Fatal("exiting settings did not restore analyzer")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type inputTestLayout struct {
|
||||||
|
navigationTestLayout
|
||||||
|
onPress func(app.Context)
|
||||||
|
presses int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *inputTestLayout) HandleButtonPress(ctx app.Context) {
|
||||||
|
l.presses++
|
||||||
|
if l.onPress != nil {
|
||||||
|
l.onPress(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (l *inputTestLayout) HandleButtonLongPress(ctx app.Context) { l.HandleButtonPress(ctx) }
|
||||||
|
|
||||||
|
func TestButtonEventIsNotRedispatchedAfterScreenChange(t *testing.T) {
|
||||||
|
for _, long := range []bool{false, true} {
|
||||||
|
first := &inputTestLayout{navigationTestLayout: navigationTestLayout{name: "first"}}
|
||||||
|
second := &inputTestLayout{navigationTestLayout: navigationTestLayout{name: "second"}}
|
||||||
|
c := &controllerContext{layouts: []app.Layout{first, second}}
|
||||||
|
c.Show(first)
|
||||||
|
first.onPress = func(ctx app.Context) { ctx.Show(second) }
|
||||||
|
if long {
|
||||||
|
c.handleInput(hardware.RotaryLongPressEvent{})
|
||||||
|
} else {
|
||||||
|
c.handleInput(hardware.RotaryPressEvent{})
|
||||||
|
}
|
||||||
|
if first.presses != 1 || second.presses != 0 {
|
||||||
|
t.Fatalf("event was redispatched: first=%d second=%d", first.presses, second.presses)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/BeatGlow/beatglow/hardware"
|
"github.com/BeatGlow/beatglow/hardware"
|
||||||
)
|
)
|
||||||
|
|
||||||
var LongPressDuration = 350 * time.Millisecond
|
var LongPressDuration = 750 * time.Millisecond
|
||||||
|
|
||||||
type Encoder interface {
|
type Encoder interface {
|
||||||
hardware.Driver
|
hardware.Driver
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// linuxInputEvent holds a decoded evdev record, independent of native word size.
|
||||||
|
type linuxInputEvent struct {
|
||||||
|
Secs, USecs int64
|
||||||
|
Type, Code uint16
|
||||||
|
Value int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e linuxInputEvent) String() string {
|
||||||
|
return fmt.Sprintf("time=%s type=%#04x code=%#04x value=%d", e.Time(), e.Type, e.Code, e.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e linuxInputEvent) Time() time.Time {
|
||||||
|
return time.Unix(e.Secs, e.USecs*int64(time.Microsecond))
|
||||||
|
}
|
||||||
|
|
||||||
|
// readLinuxInputEvent reads one little-endian Linux event for a 4- or 8-byte
|
||||||
|
// native word. Short records return the error from io.ReadFull.
|
||||||
|
func readLinuxInputEvent(r io.Reader, wordSize int) (linuxInputEvent, error) {
|
||||||
|
var e linuxInputEvent
|
||||||
|
if wordSize != 4 && wordSize != 8 {
|
||||||
|
return e, fmt.Errorf("input: unsupported word size %d", wordSize)
|
||||||
|
}
|
||||||
|
var data [24]byte
|
||||||
|
if _, err := io.ReadFull(r, data[:2*wordSize+8]); err != nil {
|
||||||
|
return e, err
|
||||||
|
}
|
||||||
|
if wordSize == 4 {
|
||||||
|
e.Secs = int64(int32(binary.LittleEndian.Uint32(data[:4])))
|
||||||
|
e.USecs = int64(binary.LittleEndian.Uint32(data[4:8]))
|
||||||
|
} else {
|
||||||
|
e.Secs = int64(binary.LittleEndian.Uint64(data[:8]))
|
||||||
|
e.USecs = int64(binary.LittleEndian.Uint64(data[8:16]))
|
||||||
|
}
|
||||||
|
offset := 2 * wordSize
|
||||||
|
e.Type = binary.LittleEndian.Uint16(data[offset:])
|
||||||
|
e.Code = binary.LittleEndian.Uint16(data[offset+2:])
|
||||||
|
e.Value = int32(binary.LittleEndian.Uint32(data[offset+4:]))
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// buttonState pairs one press with one release and ignores contact bounce.
|
||||||
|
// Access is confined to the button's event-reading goroutine.
|
||||||
|
type buttonState struct {
|
||||||
|
pressedAt, releasedAt time.Time
|
||||||
|
pressed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buttonState) press(at time.Time) {
|
||||||
|
if b.pressed || (!b.releasedAt.IsZero() && at.Sub(b.releasedAt) < 30*time.Millisecond) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.pressedAt, b.pressed = at, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buttonState) release(at time.Time) (time.Duration, bool) {
|
||||||
|
if !b.pressed {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
b.pressed = false
|
||||||
|
b.releasedAt = at
|
||||||
|
duration := at.Sub(b.pressedAt)
|
||||||
|
return duration, duration >= 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package input
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInputEventRecordSizes(t *testing.T) {
|
||||||
|
for _, wordSize := range []int{4, 8} {
|
||||||
|
var stream bytes.Buffer
|
||||||
|
for _, value := range []int32{1, 0} {
|
||||||
|
if wordSize == 4 {
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint32(100))
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint32(950000))
|
||||||
|
} else {
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint64(100))
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint64(950000))
|
||||||
|
}
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint16(1))
|
||||||
|
binary.Write(&stream, binary.LittleEndian, uint16(28))
|
||||||
|
binary.Write(&stream, binary.LittleEndian, value)
|
||||||
|
}
|
||||||
|
for _, value := range []int32{1, 0} {
|
||||||
|
event, err := readLinuxInputEvent(&stream, wordSize)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if event.Type != 1 || event.Code != 28 || event.Value != value {
|
||||||
|
t.Fatalf("wordSize=%d: misaligned event %+v", wordSize, event)
|
||||||
|
}
|
||||||
|
if !event.Time().Equal(time.Unix(100, 950000000)) {
|
||||||
|
t.Fatal("microseconds decoded incorrectly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := readLinuxInputEvent(&stream, wordSize); !errors.Is(err, io.EOF) {
|
||||||
|
t.Fatalf("got %v, want EOF", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestButtonShortPressAcrossSecondBoundary(t *testing.T) {
|
||||||
|
var b buttonState
|
||||||
|
b.press((linuxInputEvent{Secs: 100, USecs: 950000}).Time())
|
||||||
|
duration, ok := b.release((linuxInputEvent{Secs: 101, USecs: 50000}).Time())
|
||||||
|
if !ok || duration != 100*time.Millisecond {
|
||||||
|
t.Fatalf("got %s, %v, want 100ms short press", duration, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestButtonIgnoresDuplicateAndBouncedEvents(t *testing.T) {
|
||||||
|
var b buttonState
|
||||||
|
start := time.Unix(100, 0)
|
||||||
|
if _, ok := b.release(start); ok {
|
||||||
|
t.Fatal("unpaired release produced a click")
|
||||||
|
}
|
||||||
|
b.press(start)
|
||||||
|
b.press(start.Add(200 * time.Millisecond))
|
||||||
|
if duration, ok := b.release(start.Add(500 * time.Millisecond)); !ok || duration != 500*time.Millisecond {
|
||||||
|
t.Fatal("repeated press reset the long-press timer")
|
||||||
|
}
|
||||||
|
if _, ok := b.release(start.Add(501 * time.Millisecond)); ok {
|
||||||
|
t.Fatal("duplicate release produced a second click")
|
||||||
|
}
|
||||||
|
b.press(start.Add(505 * time.Millisecond))
|
||||||
|
if _, ok := b.release(start.Add(510 * time.Millisecond)); ok {
|
||||||
|
t.Fatal("contact bounce produced another click")
|
||||||
|
}
|
||||||
|
b.press(start.Add(time.Second))
|
||||||
|
if duration, ok := b.release(start.Add(1100 * time.Millisecond)); !ok || duration != 100*time.Millisecond {
|
||||||
|
t.Fatal("subsequent intentional click lost")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncatedInputEvent(t *testing.T) {
|
||||||
|
for _, wordSize := range []int{4, 8} {
|
||||||
|
_, err := readLinuxInputEvent(bytes.NewReader(make([]byte, 2*wordSize+7)), wordSize)
|
||||||
|
if !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
|
t.Fatalf("got %v, want unexpected EOF", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
package input
|
package input
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@@ -168,32 +166,16 @@ func linuxPheripherals() (ps []*linuxPheripheral, err error) {
|
|||||||
return ps, nil
|
return ps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type linuxInputEvent struct {
|
|
||||||
Secs uint32
|
|
||||||
USecs uint32
|
|
||||||
Type uint16
|
|
||||||
Code uint16
|
|
||||||
Value int32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e linuxInputEvent) String() string {
|
|
||||||
return fmt.Sprintf("time=%s type=%#04x code=%#04x value=%d", e.Time(), e.Type, e.Code, e.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e linuxInputEvent) Time() time.Time {
|
|
||||||
return time.Unix(int64(e.Secs), int64(e.USecs))
|
|
||||||
}
|
|
||||||
|
|
||||||
func readLinuxInputEvents(name string, cb func(linuxInputEvent) error) error {
|
func readLinuxInputEvents(name string, cb func(linuxInputEvent) error) error {
|
||||||
f, err := os.Open(name)
|
f, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var raw linuxInputEvent
|
defer f.Close()
|
||||||
for {
|
for {
|
||||||
if err := binary.Read(f, binary.LittleEndian, &raw); err != nil {
|
raw, err := readLinuxInputEvent(f, strconv.IntSize/8)
|
||||||
_ = f.Close()
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := cb(raw); err != nil {
|
if err := cb(raw); err != nil {
|
||||||
@@ -232,24 +214,28 @@ func (s *linuxButton) readEvents(events chan<- hardware.InputEvent) {
|
|||||||
WithField("input", "button").
|
WithField("input", "button").
|
||||||
WithField("path", s.p.path)
|
WithField("path", s.p.path)
|
||||||
|
|
||||||
var last time.Time
|
var button buttonState
|
||||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||||
if ev.Code == 0 {
|
if ev.Type != 1 || ev.Code == 0 { // EV_KEY only; ignore synchronization events.
|
||||||
return nil // Ignore
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithField("data", ev).Trace("button event")
|
|
||||||
|
|
||||||
var event hardware.InputEvent
|
var event hardware.InputEvent
|
||||||
switch ev.Value {
|
switch ev.Value {
|
||||||
case 0: // Released
|
case 1: // Pressed; repeated presses must not restart the timer.
|
||||||
if ev.Time().Sub(last) > LongPressDuration {
|
button.press(ev.Time())
|
||||||
|
return nil
|
||||||
|
case 0: // Released.
|
||||||
|
duration, ok := button.release(ev.Time())
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if duration >= LongPressDuration {
|
||||||
event = hardware.RotaryLongPressEvent{Time: ev.Time()}
|
event = hardware.RotaryLongPressEvent{Time: ev.Time()}
|
||||||
} else {
|
} else {
|
||||||
event = hardware.RotaryPressEvent{Time: ev.Time()}
|
event = hardware.RotaryPressEvent{Time: ev.Time()}
|
||||||
}
|
}
|
||||||
case 1: // Pressed
|
default: // Ignore key autorepeat and unknown values.
|
||||||
last = ev.Time()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,7 +283,7 @@ func (s *linuxRotary) readEvents(events chan<- hardware.InputEvent) {
|
|||||||
WithField("path", s.p.path)
|
WithField("path", s.p.path)
|
||||||
|
|
||||||
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
err := readLinuxInputEvents(s.p.path, func(ev linuxInputEvent) error {
|
||||||
if ev.Value == 0 {
|
if ev.Type != 2 || ev.Value == 0 { // EV_REL only.
|
||||||
return nil // Ignore
|
return nil // Ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user