Updated X backend to match

This commit is contained in:
Sasha Koshka 2023-02-02 01:47:55 -05:00
parent da6fe2c845
commit 99942466f8
4 changed files with 124 additions and 120 deletions

View File

@ -3,64 +3,64 @@ package x
import "unicode" import "unicode"
import "github.com/jezek/xgb/xproto" import "github.com/jezek/xgb/xproto"
import "github.com/jezek/xgbutil/keybind" import "github.com/jezek/xgbutil/keybind"
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/input"
// when making changes to this file, look at keysymdef.h and // when making changes to this file, look at keysymdef.h and
// https://tronche.com/gui/x/xlib/input/keyboard-encoding.html // https://tronche.com/gui/x/xlib/input/keyboard-encoding.html
var buttonCodeTable = map[xproto.Keysym] tomo.Key { var buttonCodeTable = map[xproto.Keysym] input.Key {
0xFFFFFF: tomo.KeyNone, 0xFFFFFF: input.KeyNone,
0xFF63: tomo.KeyInsert, 0xFF63: input.KeyInsert,
0xFF67: tomo.KeyMenu, 0xFF67: input.KeyMenu,
0xFF61: tomo.KeyPrintScreen, 0xFF61: input.KeyPrintScreen,
0xFF6B: tomo.KeyPause, 0xFF6B: input.KeyPause,
0xFFE5: tomo.KeyCapsLock, 0xFFE5: input.KeyCapsLock,
0xFF14: tomo.KeyScrollLock, 0xFF14: input.KeyScrollLock,
0xFF7F: tomo.KeyNumLock, 0xFF7F: input.KeyNumLock,
0xFF08: tomo.KeyBackspace, 0xFF08: input.KeyBackspace,
0xFF09: tomo.KeyTab, 0xFF09: input.KeyTab,
0xFE20: tomo.KeyTab, 0xFE20: input.KeyTab,
0xFF0D: tomo.KeyEnter, 0xFF0D: input.KeyEnter,
0xFF1B: tomo.KeyEscape, 0xFF1B: input.KeyEscape,
0xFF52: tomo.KeyUp, 0xFF52: input.KeyUp,
0xFF54: tomo.KeyDown, 0xFF54: input.KeyDown,
0xFF51: tomo.KeyLeft, 0xFF51: input.KeyLeft,
0xFF53: tomo.KeyRight, 0xFF53: input.KeyRight,
0xFF55: tomo.KeyPageUp, 0xFF55: input.KeyPageUp,
0xFF56: tomo.KeyPageDown, 0xFF56: input.KeyPageDown,
0xFF50: tomo.KeyHome, 0xFF50: input.KeyHome,
0xFF57: tomo.KeyEnd, 0xFF57: input.KeyEnd,
0xFFE1: tomo.KeyLeftShift, 0xFFE1: input.KeyLeftShift,
0xFFE2: tomo.KeyRightShift, 0xFFE2: input.KeyRightShift,
0xFFE3: tomo.KeyLeftControl, 0xFFE3: input.KeyLeftControl,
0xFFE4: tomo.KeyRightControl, 0xFFE4: input.KeyRightControl,
0xFFE7: tomo.KeyLeftMeta, 0xFFE7: input.KeyLeftMeta,
0xFFE8: tomo.KeyRightMeta, 0xFFE8: input.KeyRightMeta,
0xFFE9: tomo.KeyLeftAlt, 0xFFE9: input.KeyLeftAlt,
0xFFEA: tomo.KeyRightAlt, 0xFFEA: input.KeyRightAlt,
0xFFEB: tomo.KeyLeftSuper, 0xFFEB: input.KeyLeftSuper,
0xFFEC: tomo.KeyRightSuper, 0xFFEC: input.KeyRightSuper,
0xFFED: tomo.KeyLeftHyper, 0xFFED: input.KeyLeftHyper,
0xFFEE: tomo.KeyRightHyper, 0xFFEE: input.KeyRightHyper,
0xFFFF: tomo.KeyDelete, 0xFFFF: input.KeyDelete,
0xFFBE: tomo.KeyF1, 0xFFBE: input.KeyF1,
0xFFBF: tomo.KeyF2, 0xFFBF: input.KeyF2,
0xFFC0: tomo.KeyF3, 0xFFC0: input.KeyF3,
0xFFC1: tomo.KeyF4, 0xFFC1: input.KeyF4,
0xFFC2: tomo.KeyF5, 0xFFC2: input.KeyF5,
0xFFC3: tomo.KeyF6, 0xFFC3: input.KeyF6,
0xFFC4: tomo.KeyF7, 0xFFC4: input.KeyF7,
0xFFC5: tomo.KeyF8, 0xFFC5: input.KeyF8,
0xFFC6: tomo.KeyF9, 0xFFC6: input.KeyF9,
0xFFC7: tomo.KeyF10, 0xFFC7: input.KeyF10,
0xFFC8: tomo.KeyF11, 0xFFC8: input.KeyF11,
0xFFC9: tomo.KeyF12, 0xFFC9: input.KeyF12,
// TODO: send this whenever a compose key, dead key, etc is pressed, // TODO: send this whenever a compose key, dead key, etc is pressed,
// and then send the resulting character while witholding the key // and then send the resulting character while witholding the key
@ -68,46 +68,46 @@ var buttonCodeTable = map[xproto.Keysym] tomo.Key {
// concerned, a magical key with the final character was pressed and the // concerned, a magical key with the final character was pressed and the
// KeyDead key is just so that the program might provide some visual // KeyDead key is just so that the program might provide some visual
// feedback to the user while input is being waited for. // feedback to the user while input is being waited for.
0xFF20: tomo.KeyDead, 0xFF20: input.KeyDead,
} }
var keypadCodeTable = map[xproto.Keysym] tomo.Key { var keypadCodeTable = map[xproto.Keysym] input.Key {
0xff80: tomo.Key(' '), 0xff80: input.Key(' '),
0xff89: tomo.KeyTab, 0xff89: input.KeyTab,
0xff8d: tomo.KeyEnter, 0xff8d: input.KeyEnter,
0xff91: tomo.KeyF1, 0xff91: input.KeyF1,
0xff92: tomo.KeyF2, 0xff92: input.KeyF2,
0xff93: tomo.KeyF3, 0xff93: input.KeyF3,
0xff94: tomo.KeyF4, 0xff94: input.KeyF4,
0xff95: tomo.KeyHome, 0xff95: input.KeyHome,
0xff96: tomo.KeyLeft, 0xff96: input.KeyLeft,
0xff97: tomo.KeyUp, 0xff97: input.KeyUp,
0xff98: tomo.KeyRight, 0xff98: input.KeyRight,
0xff99: tomo.KeyDown, 0xff99: input.KeyDown,
0xff9a: tomo.KeyPageUp, 0xff9a: input.KeyPageUp,
0xff9b: tomo.KeyPageDown, 0xff9b: input.KeyPageDown,
0xff9c: tomo.KeyEnd, 0xff9c: input.KeyEnd,
0xff9d: tomo.KeyHome, 0xff9d: input.KeyHome,
0xff9e: tomo.KeyInsert, 0xff9e: input.KeyInsert,
0xff9f: tomo.KeyDelete, 0xff9f: input.KeyDelete,
0xffbd: tomo.Key('='), 0xffbd: input.Key('='),
0xffaa: tomo.Key('*'), 0xffaa: input.Key('*'),
0xffab: tomo.Key('+'), 0xffab: input.Key('+'),
0xffac: tomo.Key(','), 0xffac: input.Key(','),
0xffad: tomo.Key('-'), 0xffad: input.Key('-'),
0xffae: tomo.Key('.'), 0xffae: input.Key('.'),
0xffaf: tomo.Key('/'), 0xffaf: input.Key('/'),
0xffb0: tomo.Key('0'), 0xffb0: input.Key('0'),
0xffb1: tomo.Key('1'), 0xffb1: input.Key('1'),
0xffb2: tomo.Key('2'), 0xffb2: input.Key('2'),
0xffb3: tomo.Key('3'), 0xffb3: input.Key('3'),
0xffb4: tomo.Key('4'), 0xffb4: input.Key('4'),
0xffb5: tomo.Key('5'), 0xffb5: input.Key('5'),
0xffb6: tomo.Key('6'), 0xffb6: input.Key('6'),
0xffb7: tomo.Key('7'), 0xffb7: input.Key('7'),
0xffb8: tomo.Key('8'), 0xffb8: input.Key('8'),
0xffb9: tomo.Key('9'), 0xffb9: input.Key('9'),
} }
// initializeKeymapInformation grabs keyboard mapping information from the X // initializeKeymapInformation grabs keyboard mapping information from the X
@ -168,7 +168,7 @@ func (backend *Backend) keycodeToKey (
keycode xproto.Keycode, keycode xproto.Keycode,
state uint16, state uint16,
) ( ) (
button tomo.Key, button input.Key,
numberPad bool, numberPad bool,
) { ) {
// PARAGRAPH 3 // PARAGRAPH 3
@ -359,7 +359,7 @@ func (backend *Backend) keycodeToKey (
if numberPad { return } if numberPad { return }
// otherwise, use the rune // otherwise, use the rune
button = tomo.Key(selectedRune) button = input.Key(selectedRune)
return return
} }

View File

@ -1,6 +1,7 @@
package x package x
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "github.com/jezek/xgbutil" import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto" import "github.com/jezek/xgb/xproto"
@ -98,9 +99,9 @@ func (window *Window) exposeEventFollows (event xproto.ConfigureNotifyEvent) (fo
func (window *Window) modifiersFromState ( func (window *Window) modifiersFromState (
state uint16, state uint16,
) ( ) (
modifiers tomo.Modifiers, modifiers input.Modifiers,
) { ) {
return tomo.Modifiers { return input.Modifiers {
Shift: Shift:
(state & xproto.ModMaskShift) > 0 || (state & xproto.ModMaskShift) > 0 ||
(state & window.backend.modifierMasks.shiftLock) > 0, (state & window.backend.modifierMasks.shiftLock) > 0,
@ -123,18 +124,18 @@ func (window *Window) handleKeyPress (
modifiers := window.modifiersFromState(keyEvent.State) modifiers := window.modifiersFromState(keyEvent.State)
modifiers.NumberPad = numberPad modifiers.NumberPad = numberPad
if key == tomo.KeyTab && modifiers.Alt { if key == input.KeyTab && modifiers.Alt {
if child, ok := window.child.(tomo.Focusable); ok { if child, ok := window.child.(elements.Focusable); ok {
direction := tomo.KeynavDirectionForward direction := input.KeynavDirectionForward
if modifiers.Shift { if modifiers.Shift {
direction = tomo.KeynavDirectionBackward direction = input.KeynavDirectionBackward
} }
if !child.HandleFocus(direction) { if !child.HandleFocus(direction) {
child.HandleUnfocus() child.HandleUnfocus()
} }
} }
} else if child, ok := window.child.(tomo.KeyboardTarget); ok { } else if child, ok := window.child.(elements.KeyboardTarget); ok {
child.HandleKeyDown(key, modifiers) child.HandleKeyDown(key, modifiers)
} }
} }
@ -168,7 +169,7 @@ func (window *Window) handleKeyRelease (
modifiers := window.modifiersFromState(keyEvent.State) modifiers := window.modifiersFromState(keyEvent.State)
modifiers.NumberPad = numberPad modifiers.NumberPad = numberPad
if child, ok := window.child.(tomo.KeyboardTarget); ok { if child, ok := window.child.(elements.KeyboardTarget); ok {
child.HandleKeyUp(key, modifiers) child.HandleKeyUp(key, modifiers)
} }
} }
@ -179,7 +180,7 @@ func (window *Window) handleButtonPress (
) { ) {
if window.child == nil { return } if window.child == nil { return }
if child, ok := window.child.(tomo.MouseTarget); ok { if child, ok := window.child.(elements.MouseTarget); ok {
buttonEvent := *event.ButtonPressEvent buttonEvent := *event.ButtonPressEvent
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 {
sum := scrollSum { } sum := scrollSum { }
@ -193,7 +194,7 @@ func (window *Window) handleButtonPress (
child.HandleMouseDown ( child.HandleMouseDown (
int(buttonEvent.EventX), int(buttonEvent.EventX),
int(buttonEvent.EventY), int(buttonEvent.EventY),
tomo.Button(buttonEvent.Detail)) input.Button(buttonEvent.Detail))
} }
} }
@ -205,13 +206,13 @@ func (window *Window) handleButtonRelease (
) { ) {
if window.child == nil { return } if window.child == nil { return }
if child, ok := window.child.(tomo.MouseTarget); ok { if child, ok := window.child.(elements.MouseTarget); ok {
buttonEvent := *event.ButtonReleaseEvent buttonEvent := *event.ButtonReleaseEvent
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return } if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return }
child.HandleMouseUp ( child.HandleMouseUp (
int(buttonEvent.EventX), int(buttonEvent.EventX),
int(buttonEvent.EventY), int(buttonEvent.EventY),
tomo.Button(buttonEvent.Detail)) input.Button(buttonEvent.Detail))
} }
} }
@ -221,7 +222,7 @@ func (window *Window) handleMotionNotify (
) { ) {
if window.child == nil { return } if window.child == nil { return }
if child, ok := window.child.(tomo.MouseTarget); ok { if child, ok := window.child.(elements.MouseTarget); ok {
motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent) motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent)
child.HandleMouseMove ( child.HandleMouseMove (
int(motionEvent.EventX), int(motionEvent.EventX),

View File

@ -7,14 +7,16 @@ import "github.com/jezek/xgbutil/icccm"
import "github.com/jezek/xgbutil/xevent" import "github.com/jezek/xgbutil/xevent"
import "github.com/jezek/xgbutil/xwindow" import "github.com/jezek/xgbutil/xwindow"
import "github.com/jezek/xgbutil/xgraphics" import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/elements"
type Window struct { type Window struct {
backend *Backend backend *Backend
xWindow *xwindow.Window xWindow *xwindow.Window
xCanvas *xgraphics.Image xCanvas *xgraphics.Image
canvas tomo.BasicCanvas canvas canvas.BasicCanvas
child tomo.Element child elements.Element
onClose func () onClose func ()
skipChildDrawCallback bool skipChildDrawCallback bool
@ -27,7 +29,7 @@ type Window struct {
func (backend *Backend) NewWindow ( func (backend *Backend) NewWindow (
width, height int, width, height int,
) ( ) (
output tomo.Window, output elements.Window,
err error, err error,
) { ) {
if backend == nil { panic("nil backend") } if backend == nil { panic("nil backend") }
@ -79,16 +81,16 @@ func (backend *Backend) NewWindow (
return return
} }
func (window *Window) Adopt (child tomo.Element) { func (window *Window) Adopt (child elements.Element) {
// disown previous child // disown previous child
if window.child != nil { if window.child != nil {
window.child.OnDamage(nil) window.child.OnDamage(nil)
window.child.OnMinimumSizeChange(nil) window.child.OnMinimumSizeChange(nil)
} }
if previousChild, ok := window.child.(tomo.Flexible); ok { if previousChild, ok := window.child.(elements.Flexible); ok {
previousChild.OnFlexibleHeightChange(nil) previousChild.OnFlexibleHeightChange(nil)
} }
if previousChild, ok := window.child.(tomo.Focusable); ok { if previousChild, ok := window.child.(elements.Focusable); ok {
previousChild.OnFocusRequest(nil) previousChild.OnFocusRequest(nil)
previousChild.OnFocusMotionRequest(nil) previousChild.OnFocusMotionRequest(nil)
if previousChild.Focused() { if previousChild.Focused() {
@ -98,10 +100,10 @@ func (window *Window) Adopt (child tomo.Element) {
// adopt new child // adopt new child
window.child = child window.child = child
if newChild, ok := child.(tomo.Flexible); ok { if newChild, ok := child.(elements.Flexible); ok {
newChild.OnFlexibleHeightChange(window.resizeChildToFit) newChild.OnFlexibleHeightChange(window.resizeChildToFit)
} }
if newChild, ok := child.(tomo.Focusable); ok { if newChild, ok := child.(elements.Focusable); ok {
newChild.OnFocusRequest(window.childSelectionRequestCallback) newChild.OnFocusRequest(window.childSelectionRequestCallback)
} }
if child != nil { if child != nil {
@ -116,7 +118,7 @@ func (window *Window) Adopt (child tomo.Element) {
} }
} }
func (window *Window) Child () (child tomo.Element) { func (window *Window) Child () (child elements.Element) {
child = window.child child = window.child
return return
} }
@ -229,7 +231,7 @@ func (window *Window) redrawChildEntirely () {
func (window *Window) resizeChildToFit () { func (window *Window) resizeChildToFit () {
window.skipChildDrawCallback = true window.skipChildDrawCallback = true
if child, ok := window.child.(tomo.Flexible); ok { if child, ok := window.child.(elements.Flexible); ok {
minimumHeight := child.FlexibleHeightFor(window.metrics.width) minimumHeight := child.FlexibleHeightFor(window.metrics.width)
minimumWidth, _ := child.MinimumSize() minimumWidth, _ := child.MinimumSize()
@ -252,12 +254,12 @@ func (window *Window) resizeChildToFit () {
window.skipChildDrawCallback = false window.skipChildDrawCallback = false
} }
func (window *Window) childDrawCallback (region tomo.Canvas) { func (window *Window) childDrawCallback (region canvas.Canvas) {
if window.skipChildDrawCallback { return } if window.skipChildDrawCallback { return }
window.pushRegion(window.paste(region)) window.pushRegion(window.paste(region))
} }
func (window *Window) paste (canvas tomo.Canvas) (updatedRegion image.Rectangle) { func (window *Window) paste (canvas canvas.Canvas) (updatedRegion image.Rectangle) {
data, stride := canvas.Buffer() data, stride := canvas.Buffer()
bounds := canvas.Bounds().Intersect(window.xCanvas.Bounds()) bounds := canvas.Bounds().Intersect(window.xCanvas.Bounds())
for x := bounds.Min.X; x < bounds.Max.X; x ++ { for x := bounds.Min.X; x < bounds.Max.X; x ++ {
@ -293,18 +295,18 @@ func (window *Window) childMinimumSizeChangeCallback (width, height int) {
} }
func (window *Window) childSelectionRequestCallback () (granted bool) { func (window *Window) childSelectionRequestCallback () (granted bool) {
if child, ok := window.child.(tomo.Focusable); ok { if child, ok := window.child.(elements.Focusable); ok {
child.HandleFocus(tomo.KeynavDirectionNeutral) child.HandleFocus(input.KeynavDirectionNeutral)
} }
return true return true
} }
func (window *Window) childSelectionMotionRequestCallback ( func (window *Window) childSelectionMotionRequestCallback (
direction tomo.KeynavDirection, direction input.KeynavDirection,
) ( ) (
granted bool, granted bool,
) { ) {
if child, ok := window.child.(tomo.Focusable); ok { if child, ok := window.child.(elements.Focusable); ok {
if !child.HandleFocus(direction) { if !child.HandleFocus(direction) {
child.HandleUnfocus() child.HandleUnfocus()
} }

View File

@ -1,6 +1,7 @@
package x package x
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "github.com/jezek/xgbutil" import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto" import "github.com/jezek/xgb/xproto"
@ -81,14 +82,14 @@ func (backend *Backend) Do (callback func ()) {
// Copy puts data into the clipboard. This method is not yet implemented and // Copy puts data into the clipboard. This method is not yet implemented and
// will do nothing! // will do nothing!
func (backend *Backend) Copy (data tomo.Data) { func (backend *Backend) Copy (data data.Data) {
backend.assert() backend.assert()
// TODO // TODO
} }
// Paste returns the data currently in the clipboard. This method may // Paste returns the data currently in the clipboard. This method may
// return nil. This method is not yet implemented and will do nothing! // return nil. This method is not yet implemented and will do nothing!
func (backend *Backend) Paste (accept []tomo.Mime) (data tomo.Data) { func (backend *Backend) Paste (accept []data.Mime) (data data.Data) {
backend.assert() backend.assert()
// TODO // TODO
return return