Compare commits
No commits in common. "main" and "v0.1.0" have entirely different histories.
@ -49,25 +49,11 @@ func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
|
|||||||
|
|
||||||
cell := backend.application.GetForRendering(x, y)
|
cell := backend.application.GetForRendering(x, y)
|
||||||
content := cell.Rune()
|
content := cell.Rune()
|
||||||
style := cell.Style()
|
|
||||||
|
|
||||||
if
|
if forceRedraw && content < 32 { continue }
|
||||||
forceRedraw &&
|
|
||||||
content < 32 &&
|
|
||||||
style & (
|
|
||||||
stone.StyleHighlight |
|
|
||||||
stone.StyleUnderline) == 0 {
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
areas = append(areas, backend.boundsOfCell(x, y))
|
areas = append(areas, backend.boundsOfCell(x, y))
|
||||||
backend.drawRune (
|
backend.drawRune(x, y, content, cell.Color(), !forceRedraw)
|
||||||
x, y,
|
|
||||||
content,
|
|
||||||
cell.Color(),
|
|
||||||
cell.Style(),
|
|
||||||
!forceRedraw)
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
if backend.drawBufferBounds && forceRedraw {
|
if backend.drawBufferBounds && forceRedraw {
|
||||||
@ -88,78 +74,25 @@ func (backend *Backend) drawRune (
|
|||||||
x, y int,
|
x, y int,
|
||||||
character rune,
|
character rune,
|
||||||
runeColor stone.Color,
|
runeColor stone.Color,
|
||||||
runeStyle stone.Style,
|
|
||||||
drawBackground bool,
|
drawBackground bool,
|
||||||
) {
|
) {
|
||||||
// TODO: cache these draws as non-transparent buffers with the
|
// TODO: cache these draws as non-transparent buffers with the
|
||||||
// application background color as the background. that way, we won't
|
// application background color as the background. that way, we won't
|
||||||
// need to redraw the characters *or* composite them.
|
// need to redraw the characters *or* composite them.
|
||||||
|
|
||||||
face := backend.font.normal
|
if drawBackground {
|
||||||
|
|
||||||
highlight := runeStyle & stone.StyleHighlight > 0
|
|
||||||
bold := runeStyle & stone.StyleBold > 0
|
|
||||||
italic := runeStyle & stone.StyleItalic > 0
|
|
||||||
|
|
||||||
boldTransform := false
|
|
||||||
italicTransform := false
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case bold && italic:
|
|
||||||
if backend.font.boldItalic == nil {
|
|
||||||
switch {
|
|
||||||
case
|
|
||||||
backend.font.bold == nil && backend.font.italic != nil,
|
|
||||||
backend.font.bold != nil && backend.font.italic != nil:
|
|
||||||
|
|
||||||
boldTransform = true
|
|
||||||
face = backend.font.italic
|
|
||||||
case backend.font.italic == nil && backend.font.bold != nil:
|
|
||||||
italicTransform = true
|
|
||||||
face = backend.font.bold
|
|
||||||
default:
|
|
||||||
boldTransform = true
|
|
||||||
italicTransform = true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
face = backend.font.boldItalic
|
|
||||||
}
|
|
||||||
case bold:
|
|
||||||
if backend.font.bold == nil {
|
|
||||||
boldTransform = true
|
|
||||||
} else {
|
|
||||||
face = backend.font.bold
|
|
||||||
}
|
|
||||||
case italic:
|
|
||||||
if backend.font.italic == nil {
|
|
||||||
italicTransform = true
|
|
||||||
} else {
|
|
||||||
face = backend.font.italic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var background xgraphics.BGRA
|
|
||||||
var foreground xgraphics.BGRA
|
|
||||||
|
|
||||||
if highlight {
|
|
||||||
background = backend.colors[runeColor]
|
|
||||||
foreground = backend.colors[stone.ColorBackground]
|
|
||||||
} else {
|
|
||||||
background = backend.colors[stone.ColorBackground]
|
|
||||||
foreground = backend.colors[runeColor]
|
|
||||||
}
|
|
||||||
|
|
||||||
if drawBackground || highlight {
|
|
||||||
fillRectangle (
|
fillRectangle (
|
||||||
&image.Uniform { C: background },
|
&image.Uniform {
|
||||||
|
C: backend.config.Color(stone.ColorBackground),
|
||||||
|
},
|
||||||
backend.canvas,
|
backend.canvas,
|
||||||
backend.boundsOfCell(x, y))
|
backend.boundsOfCell(x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
origin := backend.originOfCell(x, y + 1)
|
if character < 32 { return }
|
||||||
|
|
||||||
if character >= 32 {
|
origin := backend.originOfCell(x, y + 1)
|
||||||
destinationRectangle, mask, maskPoint, _, ok := face.Glyph (
|
destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
|
||||||
fixed.Point26_6 {
|
fixed.Point26_6 {
|
||||||
X: fixed.I(origin.X),
|
X: fixed.I(origin.X),
|
||||||
Y: fixed.I(origin.Y),
|
Y: fixed.I(origin.Y),
|
||||||
@ -168,7 +101,9 @@ func (backend *Backend) drawRune (
|
|||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
strokeRectangle (
|
strokeRectangle (
|
||||||
&image.Uniform { C: foreground },
|
&image.Uniform {
|
||||||
|
C: backend.config.Color(stone.ColorForeground),
|
||||||
|
},
|
||||||
backend.canvas,
|
backend.canvas,
|
||||||
backend.boundsOfCell(x, y))
|
backend.boundsOfCell(x, y))
|
||||||
return
|
return
|
||||||
@ -176,33 +111,23 @@ func (backend *Backend) drawRune (
|
|||||||
|
|
||||||
if backend.drawCellBounds {
|
if backend.drawCellBounds {
|
||||||
strokeRectangle (
|
strokeRectangle (
|
||||||
&image.Uniform { C: foreground },
|
&image.Uniform {
|
||||||
|
C: backend.config.Color(stone.ColorForeground),
|
||||||
|
},
|
||||||
backend.canvas,
|
backend.canvas,
|
||||||
backend.boundsOfCell(x, y))
|
backend.boundsOfCell(x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
// alphaMask, isAlpha := mask.(*image.Alpha)
|
// cue a series of pointless optimizations
|
||||||
// if isAlpha {
|
alphaMask, isAlpha := mask.(*image.Alpha)
|
||||||
// backend.sprayRuneMaskAlpha (
|
if isAlpha {
|
||||||
// alphaMask, destinationRectangle,
|
backend.sprayRuneMaskAlpha (
|
||||||
// maskPoint, foreground, background)
|
alphaMask, destinationRectangle,
|
||||||
// } else {
|
maskPoint, backend.colors[runeColor])
|
||||||
|
} else {
|
||||||
backend.sprayRuneMask (
|
backend.sprayRuneMask (
|
||||||
mask, destinationRectangle,
|
mask, destinationRectangle,
|
||||||
maskPoint, foreground, background,
|
maskPoint, backend.colors[runeColor])
|
||||||
italicTransform, boldTransform)
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// underline
|
|
||||||
if runeStyle & stone.StyleUnderline > 0 {
|
|
||||||
maxX := origin.X + backend.metrics.cellWidth
|
|
||||||
y :=
|
|
||||||
origin.Y -
|
|
||||||
backend.metrics.descent
|
|
||||||
for x := origin.X; x < maxX; x ++ {
|
|
||||||
backend.canvas.SetBGRA(x, y, foreground)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,83 +136,54 @@ func (backend *Backend) sprayRuneMask (
|
|||||||
bounds image.Rectangle,
|
bounds image.Rectangle,
|
||||||
maskPoint image.Point,
|
maskPoint image.Point,
|
||||||
fill xgraphics.BGRA,
|
fill xgraphics.BGRA,
|
||||||
background xgraphics.BGRA,
|
|
||||||
italic bool,
|
|
||||||
bold bool,
|
|
||||||
) {
|
) {
|
||||||
maxX := bounds.Max.X - bounds.Min.X
|
maxX := bounds.Max.X - bounds.Min.X
|
||||||
maxY := bounds.Max.Y - bounds.Min.Y
|
maxY := bounds.Max.Y - bounds.Min.Y
|
||||||
|
|
||||||
for y := 0; y < maxY; y ++ {
|
for y := 0; y < maxY; y ++ {
|
||||||
var previousAlpha uint32
|
|
||||||
offset := 0
|
|
||||||
if italic {
|
|
||||||
offset = (maxY - y) / 4
|
|
||||||
}
|
|
||||||
for x := 0; x < maxX; x ++ {
|
for x := 0; x < maxX; x ++ {
|
||||||
_, _, _,
|
_, _, _,
|
||||||
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
|
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
|
||||||
currentAlpha := alpha
|
|
||||||
if bold && previousAlpha > alpha {
|
|
||||||
alpha = previousAlpha
|
|
||||||
}
|
|
||||||
backend.canvas.SetBGRA (
|
backend.canvas.SetBGRA (
|
||||||
x + bounds.Min.X + offset,
|
x + bounds.Min.X,
|
||||||
y + bounds.Min.Y - backend.metrics.descent,
|
y + bounds.Min.Y - backend.metrics.descent,
|
||||||
xgraphics.BlendBGRA (
|
xgraphics.BlendBGRA (
|
||||||
background,
|
backend.colors[stone.ColorBackground],
|
||||||
xgraphics.BGRA {
|
xgraphics.BGRA {
|
||||||
R: fill.R,
|
R: fill.R,
|
||||||
G: fill.G,
|
G: fill.G,
|
||||||
B: fill.B,
|
B: fill.B,
|
||||||
A: uint8(alpha >> 8),
|
A: uint8(alpha >> 8),
|
||||||
}))
|
}))
|
||||||
previousAlpha = currentAlpha
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
if bold {
|
func (backend *Backend) sprayRuneMaskAlpha (
|
||||||
|
mask *image.Alpha,
|
||||||
|
bounds image.Rectangle,
|
||||||
|
maskPoint image.Point,
|
||||||
|
fill xgraphics.BGRA,
|
||||||
|
) {
|
||||||
|
maxX := bounds.Max.X - bounds.Min.X
|
||||||
|
maxY := bounds.Max.Y - bounds.Min.Y
|
||||||
|
|
||||||
|
for y := 0; y < maxY; y ++ {
|
||||||
|
for x := 0; x < maxX; x ++ {
|
||||||
|
alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A
|
||||||
backend.canvas.SetBGRA (
|
backend.canvas.SetBGRA (
|
||||||
bounds.Max.X + offset,
|
x + bounds.Min.X,
|
||||||
y + bounds.Min.Y - backend.metrics.descent,
|
y + bounds.Min.Y - backend.metrics.descent,
|
||||||
xgraphics.BlendBGRA (
|
xgraphics.BlendBGRA (
|
||||||
background,
|
backend.colors[stone.ColorBackground],
|
||||||
xgraphics.BGRA {
|
xgraphics.BGRA {
|
||||||
R: fill.R,
|
R: fill.R,
|
||||||
G: fill.G,
|
G: fill.G,
|
||||||
B: fill.B,
|
B: fill.B,
|
||||||
A: uint8(previousAlpha >> 8),
|
A: alpha,
|
||||||
}))
|
}))
|
||||||
}
|
}}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (backend *Backend) sprayRuneMaskAlpha (
|
|
||||||
// mask *image.Alpha,
|
|
||||||
// bounds image.Rectangle,
|
|
||||||
// maskPoint image.Point,
|
|
||||||
// fill xgraphics.BGRA,
|
|
||||||
// background xgraphics.BGRA,
|
|
||||||
// ) {
|
|
||||||
// maxX := bounds.Max.X - bounds.Min.X
|
|
||||||
// maxY := bounds.Max.Y - bounds.Min.Y
|
|
||||||
//
|
|
||||||
// for y := 0; y < maxY; y ++ {
|
|
||||||
// for x := 0; x < maxX; x ++ {
|
|
||||||
// alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A
|
|
||||||
// backend.canvas.SetBGRA (
|
|
||||||
// x + bounds.Min.X,
|
|
||||||
// y + bounds.Min.Y - backend.metrics.descent,
|
|
||||||
// xgraphics.BlendBGRA (
|
|
||||||
// background,
|
|
||||||
// xgraphics.BGRA {
|
|
||||||
// R: fill.R,
|
|
||||||
// G: fill.G,
|
|
||||||
// B: fill.B,
|
|
||||||
// A: alpha,
|
|
||||||
// }))
|
|
||||||
// }}
|
|
||||||
// }
|
|
||||||
|
|
||||||
func fillRectangle (
|
func fillRectangle (
|
||||||
source image.Image,
|
source image.Image,
|
||||||
destination draw.Image,
|
destination draw.Image,
|
||||||
|
@ -1,339 +0,0 @@
|
|||||||
package x
|
|
||||||
|
|
||||||
import "unicode"
|
|
||||||
import "github.com/jezek/xgb/xproto"
|
|
||||||
import "github.com/jezek/xgbutil/keybind"
|
|
||||||
import "git.tebibyte.media/sashakoshka/stone"
|
|
||||||
|
|
||||||
// when making changes to this file, look at keysymdef.h and
|
|
||||||
// https://tronche.com/gui/x/xlib/input/keyboard-encoding.html
|
|
||||||
|
|
||||||
var buttonCodeTable = map[xproto.Keysym] stone.Button {
|
|
||||||
0xFFFFFF: stone.ButtonUnknown,
|
|
||||||
|
|
||||||
0xFF63: stone.KeyInsert,
|
|
||||||
0xFF67: stone.KeyMenu,
|
|
||||||
0xFF61: stone.KeyPrintScreen,
|
|
||||||
0xFF6B: stone.KeyPause,
|
|
||||||
0xFFE5: stone.KeyCapsLock,
|
|
||||||
0xFF14: stone.KeyScrollLock,
|
|
||||||
0xFF7F: stone.KeyNumLock,
|
|
||||||
0xFF08: stone.KeyBackspace,
|
|
||||||
0xFF09: stone.KeyTab,
|
|
||||||
0xFF0D: stone.KeyEnter,
|
|
||||||
0xFF1B: stone.KeyEscape,
|
|
||||||
|
|
||||||
0xFF52: stone.KeyUp,
|
|
||||||
0xFF54: stone.KeyDown,
|
|
||||||
0xFF51: stone.KeyLeft,
|
|
||||||
0xFF53: stone.KeyRight,
|
|
||||||
0xFF55: stone.KeyPageUp,
|
|
||||||
0xFF56: stone.KeyPageDown,
|
|
||||||
0xFF50: stone.KeyHome,
|
|
||||||
0xFF57: stone.KeyEnd,
|
|
||||||
|
|
||||||
0xFFE1: stone.KeyLeftShift,
|
|
||||||
0xFFE2: stone.KeyRightShift,
|
|
||||||
0xFFE3: stone.KeyLeftControl,
|
|
||||||
0xFFE4: stone.KeyRightControl,
|
|
||||||
|
|
||||||
0xFFE7: stone.KeyLeftMeta,
|
|
||||||
0xFFE8: stone.KeyRightMeta,
|
|
||||||
0xFFE9: stone.KeyLeftAlt,
|
|
||||||
0xFFEA: stone.KeyRightAlt,
|
|
||||||
0xFFEB: stone.KeyLeftSuper,
|
|
||||||
0xFFEC: stone.KeyRightSuper,
|
|
||||||
0xFFED: stone.KeyLeftHyper,
|
|
||||||
0xFFEE: stone.KeyRightHyper,
|
|
||||||
|
|
||||||
0xFFFF: stone.KeyDelete,
|
|
||||||
|
|
||||||
0xFFBE: stone.KeyF1,
|
|
||||||
0xFFBF: stone.KeyF2,
|
|
||||||
0xFFC0: stone.KeyF3,
|
|
||||||
0xFFC1: stone.KeyF4,
|
|
||||||
0xFFC2: stone.KeyF5,
|
|
||||||
0xFFC3: stone.KeyF6,
|
|
||||||
0xFFC4: stone.KeyF7,
|
|
||||||
0xFFC5: stone.KeyF8,
|
|
||||||
0xFFC6: stone.KeyF9,
|
|
||||||
0xFFC7: stone.KeyF10,
|
|
||||||
0xFFC8: stone.KeyF11,
|
|
||||||
0xFFC9: stone.KeyF12,
|
|
||||||
|
|
||||||
// TODO: send this whenever a compose key, dead key, etc is pressed,
|
|
||||||
// and then send the resulting character while witholding the key
|
|
||||||
// presses that were used to compose it. As far as the program is
|
|
||||||
// concerned, a magical key with the final character was pressed and the
|
|
||||||
// KeyDead key is just so that the program might provide some visual
|
|
||||||
// feedback to the user while input is being waited for.
|
|
||||||
0xFF20: stone.KeyDead,
|
|
||||||
}
|
|
||||||
|
|
||||||
var keypadCodeTable = map[xproto.Keysym] stone.Button {
|
|
||||||
0xff80: stone.Button(' '),
|
|
||||||
0xff89: stone.KeyTab,
|
|
||||||
0xff8d: stone.KeyEnter,
|
|
||||||
0xff91: stone.KeyF1,
|
|
||||||
0xff92: stone.KeyF2,
|
|
||||||
0xff93: stone.KeyF3,
|
|
||||||
0xff94: stone.KeyF4,
|
|
||||||
0xff95: stone.KeyHome,
|
|
||||||
0xff96: stone.KeyLeft,
|
|
||||||
0xff97: stone.KeyUp,
|
|
||||||
0xff98: stone.KeyRight,
|
|
||||||
0xff99: stone.KeyDown,
|
|
||||||
0xff9a: stone.KeyPageUp,
|
|
||||||
0xff9b: stone.KeyPageDown,
|
|
||||||
0xff9c: stone.KeyEnd,
|
|
||||||
0xff9d: stone.KeyHome,
|
|
||||||
0xff9e: stone.KeyInsert,
|
|
||||||
0xff9f: stone.KeyDelete,
|
|
||||||
0xffbd: stone.Button('='),
|
|
||||||
0xffaa: stone.Button('*'),
|
|
||||||
0xffab: stone.Button('+'),
|
|
||||||
0xffac: stone.Button(','),
|
|
||||||
0xffad: stone.Button('-'),
|
|
||||||
0xffae: stone.Button('.'),
|
|
||||||
0xffaf: stone.Button('/'),
|
|
||||||
|
|
||||||
0xffb0: stone.Button('0'),
|
|
||||||
0xffb1: stone.Button('1'),
|
|
||||||
0xffb2: stone.Button('2'),
|
|
||||||
0xffb3: stone.Button('3'),
|
|
||||||
0xffb4: stone.Button('4'),
|
|
||||||
0xffb5: stone.Button('5'),
|
|
||||||
0xffb6: stone.Button('6'),
|
|
||||||
0xffb7: stone.Button('7'),
|
|
||||||
0xffb8: stone.Button('8'),
|
|
||||||
0xffb9: stone.Button('9'),
|
|
||||||
}
|
|
||||||
|
|
||||||
// keycodeToButton converts an X keycode to a stone button code. It implements
|
|
||||||
// a more fleshed out version of some of the logic found in
|
|
||||||
// xgbutil/keybind/encoding.go to get a full keycode to keysym conversion, but
|
|
||||||
// eliminates redundant work by going straight to a button code.
|
|
||||||
func (backend *Backend) keycodeToButton (
|
|
||||||
keycode xproto.Keycode,
|
|
||||||
state uint16,
|
|
||||||
) (
|
|
||||||
button stone.Button,
|
|
||||||
numberPad bool,
|
|
||||||
) {
|
|
||||||
// PARAGRAPH 3
|
|
||||||
//
|
|
||||||
// A list of KeySyms is associated with each KeyCode. The list is
|
|
||||||
// intended to convey the set of symbols on the corresponding key. If
|
|
||||||
// the list (ignoring trailing NoSymbol entries) is a single KeySym
|
|
||||||
// ``K'', then the list is treated as if it were the list ``K NoSymbol
|
|
||||||
// K NoSymbol''. If the list (ignoring trailing NoSymbol entries) is a
|
|
||||||
// pair of KeySyms ``K1 K2'', then the list is treated as if it were the
|
|
||||||
// list ``K1 K2 K1 K2''. If the list (ignoring trailing NoSymbol
|
|
||||||
// entries) is a triple of KeySyms ``K1 K2 K3'', then the list is
|
|
||||||
// treated as if it were the list ``K1 K2 K3 NoSymbol''. When an
|
|
||||||
// explicit ``void'' element is desired in the list, the value
|
|
||||||
// VoidSymbol can be used.
|
|
||||||
symbol1 := keybind.KeysymGet(backend.connection, keycode, 0)
|
|
||||||
symbol2 := keybind.KeysymGet(backend.connection, keycode, 1)
|
|
||||||
symbol3 := keybind.KeysymGet(backend.connection, keycode, 2)
|
|
||||||
symbol4 := keybind.KeysymGet(backend.connection, keycode, 3)
|
|
||||||
switch {
|
|
||||||
case symbol2 == 0 && symbol3 == 0 && symbol4 == 0:
|
|
||||||
symbol3 = symbol1
|
|
||||||
case symbol3 == 0 && symbol4 == 0:
|
|
||||||
symbol3 = symbol1
|
|
||||||
symbol4 = symbol2
|
|
||||||
case symbol4 == 0:
|
|
||||||
symbol4 = 0
|
|
||||||
}
|
|
||||||
symbol1Rune := keysymToRune(symbol1)
|
|
||||||
symbol2Rune := keysymToRune(symbol2)
|
|
||||||
symbol3Rune := keysymToRune(symbol3)
|
|
||||||
symbol4Rune := keysymToRune(symbol4)
|
|
||||||
|
|
||||||
// PARAGRAPH 4
|
|
||||||
//
|
|
||||||
// The first four elements of the list are split into two groups of
|
|
||||||
// KeySyms. Group 1 contains the first and second KeySyms; Group 2
|
|
||||||
// contains the third and fourth KeySyms. Within each group, if the
|
|
||||||
// second element of the group is NoSymbol , then the group should be
|
|
||||||
// treated as if the second element were the same as the first element,
|
|
||||||
// except when the first element is an alphabetic KeySym ``K'' for which
|
|
||||||
// both lowercase and uppercase forms are defined. In that case, the
|
|
||||||
// group should be treated as if the first element were the lowercase
|
|
||||||
// form of ``K'' and the second element were the uppercase form of
|
|
||||||
// ``K.''
|
|
||||||
cased := false
|
|
||||||
if symbol2 == 0 {
|
|
||||||
upper := unicode.IsUpper(symbol1Rune)
|
|
||||||
lower := unicode.IsLower(symbol1Rune)
|
|
||||||
if upper || lower {
|
|
||||||
symbol1Rune = unicode.ToLower(symbol1Rune)
|
|
||||||
symbol2Rune = unicode.ToUpper(symbol1Rune)
|
|
||||||
cased = true
|
|
||||||
} else {
|
|
||||||
symbol2 = symbol1
|
|
||||||
symbol2Rune = symbol1Rune
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if symbol4 == 0 {
|
|
||||||
upper := unicode.IsUpper(symbol3Rune)
|
|
||||||
lower := unicode.IsLower(symbol3Rune)
|
|
||||||
if upper || lower {
|
|
||||||
symbol3Rune = unicode.ToLower(symbol3Rune)
|
|
||||||
symbol4Rune = unicode.ToUpper(symbol3Rune)
|
|
||||||
cased = true
|
|
||||||
} else {
|
|
||||||
symbol4 = symbol3
|
|
||||||
symbol4Rune = symbol3Rune
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PARAGRAPH 5
|
|
||||||
//
|
|
||||||
// The standard rules for obtaining a KeySym from a KeyPress event make
|
|
||||||
// use of only the Group 1 and Group 2 KeySyms; no interpretation of/
|
|
||||||
// other KeySyms in the list is given. Which group to use is determined
|
|
||||||
// by the modifier state. Switching between groups is controlled by the
|
|
||||||
// KeySym named MODE SWITCH, by attaching that KeySym to some KeyCode
|
|
||||||
// and attaching that KeyCode to any one of the modifiers Mod1 through
|
|
||||||
// Mod5. This modifier is called the group modifier. For any KeyCode,
|
|
||||||
// Group 1 is used when the group modifier is off, and Group 2 is used
|
|
||||||
// when the group modifier is on.
|
|
||||||
modeSwitch := state & backend.modifierMasks.modeSwitch > 0
|
|
||||||
if modeSwitch {
|
|
||||||
symbol1 = symbol3
|
|
||||||
symbol1Rune = symbol3Rune
|
|
||||||
symbol2 = symbol4
|
|
||||||
symbol2Rune = symbol4Rune
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// PARAGRAPH 6
|
|
||||||
//
|
|
||||||
// The Lock modifier is interpreted as CapsLock when the KeySym named
|
|
||||||
// XK_Caps_Lock is attached to some KeyCode and that KeyCode is attached
|
|
||||||
// to the Lock modifier. The Lock modifier is interpreted as ShiftLock
|
|
||||||
// when the KeySym named XK_Shift_Lock is attached to some KeyCode and
|
|
||||||
// that KeyCode is attached to the Lock modifier. If the Lock modifier
|
|
||||||
// could be interpreted as both CapsLock and ShiftLock, the CapsLock
|
|
||||||
// interpretation is used.
|
|
||||||
shift :=
|
|
||||||
state & xproto.ModMaskShift > 0 ||
|
|
||||||
state & backend.modifierMasks.shiftLock > 0
|
|
||||||
capsLock := state & backend.modifierMasks.capsLock > 0
|
|
||||||
|
|
||||||
// PARAGRAPH 7
|
|
||||||
//
|
|
||||||
// The operation of keypad keys is controlled by the KeySym named
|
|
||||||
// XK_Num_Lock, by attaching that KeySym to some KeyCode and attaching
|
|
||||||
// that KeyCode to any one of the modifiers Mod1 through Mod5 . This
|
|
||||||
// modifier is called the numlock modifier. The standard KeySyms with
|
|
||||||
// the prefix ``XK_KP_'' in their name are called keypad KeySyms; these
|
|
||||||
// are KeySyms with numeric value in the hexadecimal range 0xFF80 to
|
|
||||||
// 0xFFBD inclusive. In addition, vendor-specific KeySyms in the
|
|
||||||
// hexadecimal range 0x11000000 to 0x1100FFFF are also keypad KeySyms.
|
|
||||||
numLock := state & backend.modifierMasks.numLock > 0
|
|
||||||
|
|
||||||
// PARAGRAPH 8
|
|
||||||
//
|
|
||||||
// Within a group, the choice of KeySym is determined by applying the
|
|
||||||
// first rule that is satisfied from the following list:
|
|
||||||
var selectedKeysym xproto.Keysym
|
|
||||||
var selectedRune rune
|
|
||||||
_, symbol2IsNumPad := keypadCodeTable[symbol2]
|
|
||||||
switch {
|
|
||||||
case numLock && symbol2IsNumPad:
|
|
||||||
// The numlock modifier is on and the second KeySym is a keypad
|
|
||||||
// KeySym. In this case, if the Shift modifier is on, or if the
|
|
||||||
// Lock modifier is on and is interpreted as ShiftLock, then the
|
|
||||||
// first KeySym is used, otherwise the second KeySym is used.
|
|
||||||
if shift {
|
|
||||||
selectedKeysym = symbol1
|
|
||||||
selectedRune = symbol1Rune
|
|
||||||
} else {
|
|
||||||
selectedKeysym = symbol2
|
|
||||||
selectedRune = symbol2Rune
|
|
||||||
}
|
|
||||||
|
|
||||||
case !shift && !capsLock:
|
|
||||||
// The Shift and Lock modifiers are both off. In this case, the
|
|
||||||
// first KeySym is used.
|
|
||||||
selectedKeysym = symbol1
|
|
||||||
selectedRune = symbol1Rune
|
|
||||||
|
|
||||||
case !shift && capsLock:
|
|
||||||
// The Shift modifier is off, and the Lock modifier is on and is
|
|
||||||
// interpreted as CapsLock. In this case, the first KeySym is
|
|
||||||
// used, but if that KeySym is lowercase alphabetic, then the
|
|
||||||
// corresponding uppercase KeySym is used instead.
|
|
||||||
if cased && unicode.IsLower(symbol1Rune) {
|
|
||||||
selectedRune = symbol2Rune
|
|
||||||
} else {
|
|
||||||
selectedKeysym = symbol1
|
|
||||||
selectedRune = symbol1Rune
|
|
||||||
}
|
|
||||||
|
|
||||||
case shift && capsLock:
|
|
||||||
// The Shift modifier is on, and the Lock modifier is on and is
|
|
||||||
// interpreted as CapsLock. In this case, the second KeySym is
|
|
||||||
// used, but if that KeySym is lowercase alphabetic, then the
|
|
||||||
// corresponding uppercase KeySym is used instead.
|
|
||||||
if cased && unicode.IsLower(symbol2Rune) {
|
|
||||||
selectedRune = unicode.ToUpper(symbol2Rune)
|
|
||||||
} else {
|
|
||||||
selectedKeysym = symbol2
|
|
||||||
selectedRune = symbol2Rune
|
|
||||||
}
|
|
||||||
|
|
||||||
case shift:
|
|
||||||
// The Shift modifier is on, or the Lock modifier is on and is
|
|
||||||
// interpreted as ShiftLock, or both. In this case, the second
|
|
||||||
// KeySym is used.
|
|
||||||
selectedKeysym = symbol2
|
|
||||||
selectedRune = symbol2Rune
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////
|
|
||||||
// all of the below stuff is specific to stone's button codes. //
|
|
||||||
/////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// look up in control code table
|
|
||||||
var isControl bool
|
|
||||||
button, isControl = buttonCodeTable[selectedKeysym]
|
|
||||||
if isControl { return }
|
|
||||||
|
|
||||||
// look up in keypad table
|
|
||||||
button, numberPad = keypadCodeTable[selectedKeysym]
|
|
||||||
if numberPad { return }
|
|
||||||
|
|
||||||
// otherwise, use the rune
|
|
||||||
button = stone.Button(selectedRune)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// keysymToRune takes in an X keysym and outputs a utf32 code point. This
|
|
||||||
// function does not and should not handle keypad keys, as those are handled
|
|
||||||
// by Backend.keycodeToButton.
|
|
||||||
func keysymToRune (keysym xproto.Keysym) (character rune) {
|
|
||||||
// X keysyms like 0xFF.. or 0xFE.. are non-character keys. these cannot
|
|
||||||
// be converted so we return a zero.
|
|
||||||
if (keysym >> 8) == 0xFF || (keysym >> 8) == 0xFE {
|
|
||||||
character = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// some X keysyms have a single bit set to 1 here. i believe this is to
|
|
||||||
// prevent conflicts with existing codes. if we mask it off we will get
|
|
||||||
// a correct utf-32 code point.
|
|
||||||
if keysym & 0xF000000 == 0x1000000 {
|
|
||||||
character = rune(keysym & 0x0111111)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// if none of these things happened, we can safely (i think) assume that
|
|
||||||
// the keysym is an exact utf-32 code point.
|
|
||||||
character = rune(keysym)
|
|
||||||
return
|
|
||||||
}
|
|
@ -111,17 +111,15 @@ func (backend *Backend) handleKeyPress (
|
|||||||
event xevent.KeyPressEvent,
|
event xevent.KeyPressEvent,
|
||||||
) {
|
) {
|
||||||
keyEvent := *event.KeyPressEvent
|
keyEvent := *event.KeyPressEvent
|
||||||
button, num := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
|
button := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
|
||||||
backend.callbackManager.RunPress (button, stone.Modifiers {
|
backend.callbackManager.RunPress (button, stone.Modifiers {
|
||||||
Shift:
|
// FIXME these may not be correct in all cases
|
||||||
(keyEvent.State & xproto.ModMaskShift) > 0 ||
|
Shift: (keyEvent.State & xproto.ModMaskShift) > 0,
|
||||||
(keyEvent.State & backend.modifierMasks.shiftLock) > 0,
|
|
||||||
Control: (keyEvent.State & xproto.ModMaskControl) > 0,
|
Control: (keyEvent.State & xproto.ModMaskControl) > 0,
|
||||||
Alt: (keyEvent.State & backend.modifierMasks.alt) > 0,
|
Alt: (keyEvent.State & xproto.ModMask1) > 0,
|
||||||
Meta: (keyEvent.State & backend.modifierMasks.meta) > 0,
|
// Meta: (keyEvent.State & xproto.??) > 0,
|
||||||
Super: (keyEvent.State & backend.modifierMasks.super) > 0,
|
Super: (keyEvent.State & xproto.ModMask4) > 0,
|
||||||
Hyper: (keyEvent.State & backend.modifierMasks.hyper) > 0,
|
// Hyper: (keyEvent.State & xproto.??) > 0,
|
||||||
NumberPad: num,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +128,7 @@ func (backend *Backend) handleKeyRelease (
|
|||||||
event xevent.KeyReleaseEvent,
|
event xevent.KeyReleaseEvent,
|
||||||
) {
|
) {
|
||||||
keyEvent := *event.KeyReleaseEvent
|
keyEvent := *event.KeyReleaseEvent
|
||||||
button, _ := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
|
button := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
|
||||||
backend.callbackManager.RunRelease(button)
|
backend.callbackManager.RunRelease(button)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,20 +32,11 @@ func factory (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load font
|
// load font
|
||||||
backend.font.normal = findAndLoadFont (
|
backend.font.face = findAndLoadFont (
|
||||||
backend.config.FontNameNormal(),
|
backend.config.FontName(),
|
||||||
float64(backend.config.FontSize()))
|
float64(backend.config.FontSize()))
|
||||||
backend.font.bold = findAndLoadFont (
|
if backend.font.face == nil {
|
||||||
backend.config.FontNameBold(),
|
backend.font.face = basicfont.Face7x13
|
||||||
float64(backend.config.FontSize()))
|
|
||||||
backend.font.italic = findAndLoadFont (
|
|
||||||
backend.config.FontNameItalic(),
|
|
||||||
float64(backend.config.FontSize()))
|
|
||||||
backend.font.boldItalic = findAndLoadFont (
|
|
||||||
backend.config.FontNameBoldItalic(),
|
|
||||||
float64(backend.config.FontSize()))
|
|
||||||
if backend.font.normal == nil {
|
|
||||||
backend.font.normal = basicfont.Face7x13
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pre-calculate colors
|
// pre-calculate colors
|
||||||
@ -65,8 +56,8 @@ func factory (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calculate metrics
|
// calculate metrics
|
||||||
metrics := backend.font.normal.Metrics()
|
metrics := backend.font.face.Metrics()
|
||||||
glyphAdvance, _ := backend.font.normal.GlyphAdvance('M')
|
glyphAdvance, _ := backend.font.face.GlyphAdvance('M')
|
||||||
backend.metrics.cellWidth = glyphAdvance.Round()
|
backend.metrics.cellWidth = glyphAdvance.Round()
|
||||||
backend.metrics.cellHeight = metrics.Height.Round()
|
backend.metrics.cellHeight = metrics.Height.Round()
|
||||||
backend.metrics.descent = metrics.Descent.Round()
|
backend.metrics.descent = metrics.Descent.Round()
|
||||||
@ -83,18 +74,7 @@ func factory (
|
|||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
backend.window, err = xwindow.Generate(backend.connection)
|
backend.window, err = xwindow.Generate(backend.connection)
|
||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
|
|
||||||
// get keyboard mapping information
|
|
||||||
keybind.Initialize(backend.connection)
|
keybind.Initialize(backend.connection)
|
||||||
backend.modifierMasks.capsLock = backend.keysymToMask(0xFFE5)
|
|
||||||
backend.modifierMasks.shiftLock = backend.keysymToMask(0xFFE6)
|
|
||||||
backend.modifierMasks.numLock = backend.keysymToMask(0xFF7F)
|
|
||||||
backend.modifierMasks.modeSwitch = backend.keysymToMask(0xFF7E)
|
|
||||||
|
|
||||||
backend.modifierMasks.hyper = backend.keysymToMask(0xffed)
|
|
||||||
backend.modifierMasks.super = backend.keysymToMask(0xffeb)
|
|
||||||
backend.modifierMasks.meta = backend.keysymToMask(0xffe7)
|
|
||||||
backend.modifierMasks.alt = backend.keysymToMask(0xffe9)
|
|
||||||
|
|
||||||
// create the window
|
// create the window
|
||||||
backend.window.Create (
|
backend.window.Create (
|
||||||
@ -178,38 +158,6 @@ func findAndLoadFont (name string, size float64) (face font.Face) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (backend *Backend) keysymToKeycode (
|
|
||||||
symbol xproto.Keysym,
|
|
||||||
) (
|
|
||||||
code xproto.Keycode,
|
|
||||||
) {
|
|
||||||
mapping := keybind.KeyMapGet(backend.connection)
|
|
||||||
|
|
||||||
for index, testSymbol := range mapping.Keysyms {
|
|
||||||
if testSymbol == symbol {
|
|
||||||
code = xproto.Keycode (
|
|
||||||
index /
|
|
||||||
int(mapping.KeysymsPerKeycode) +
|
|
||||||
int(backend.connection.Setup().MinKeycode))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (backend *Backend) keysymToMask (
|
|
||||||
symbol xproto.Keysym,
|
|
||||||
) (
|
|
||||||
mask uint16,
|
|
||||||
) {
|
|
||||||
mask = keybind.ModGet (
|
|
||||||
backend.connection,
|
|
||||||
backend.keysymToKeycode(symbol))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// init registers this backend when the program starts.
|
// init registers this backend when the program starts.
|
||||||
func init () {
|
func init () {
|
||||||
stone.RegisterBackend(factory)
|
stone.RegisterBackend(factory)
|
||||||
|
203
backends/x/unicode.go
Normal file
203
backends/x/unicode.go
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
package x
|
||||||
|
|
||||||
|
// import "fmt"
|
||||||
|
import "unicode"
|
||||||
|
import "github.com/jezek/xgb/xproto"
|
||||||
|
import "github.com/jezek/xgbutil/keybind"
|
||||||
|
import "git.tebibyte.media/sashakoshka/stone"
|
||||||
|
|
||||||
|
// when making changes to this file, look at keysymdef.h and
|
||||||
|
// https://tronche.com/gui/x/xlib/input/keyboard-encoding.html
|
||||||
|
|
||||||
|
var buttonCodeTable = map[xproto.Keysym] stone.Button {
|
||||||
|
0xFFFFFF: stone.ButtonUnknown,
|
||||||
|
|
||||||
|
0xFF63: stone.KeyInsert,
|
||||||
|
0xFF67: stone.KeyMenu,
|
||||||
|
0xFF61: stone.KeyPrintScreen,
|
||||||
|
0xFF6B: stone.KeyPause,
|
||||||
|
0xFFE5: stone.KeyCapsLock,
|
||||||
|
0xFF14: stone.KeyScrollLock,
|
||||||
|
0xFF7F: stone.KeyNumLock,
|
||||||
|
0xFF08: stone.KeyBackspace,
|
||||||
|
0xFF09: stone.KeyTab,
|
||||||
|
0xFF0D: stone.KeyEnter,
|
||||||
|
0xFF1B: stone.KeyEscape,
|
||||||
|
|
||||||
|
0xFF52: stone.KeyUp,
|
||||||
|
0xFF54: stone.KeyDown,
|
||||||
|
0xFF51: stone.KeyLeft,
|
||||||
|
0xFF53: stone.KeyRight,
|
||||||
|
0xFF55: stone.KeyPageUp,
|
||||||
|
0xFF56: stone.KeyPageDown,
|
||||||
|
0xFF50: stone.KeyHome,
|
||||||
|
0xFF57: stone.KeyEnd,
|
||||||
|
|
||||||
|
0xFFE1: stone.KeyLeftShift,
|
||||||
|
0xFFE2: stone.KeyRightShift,
|
||||||
|
0xFFE3: stone.KeyLeftControl,
|
||||||
|
0xFFE4: stone.KeyRightControl,
|
||||||
|
|
||||||
|
0xFFE7: stone.KeyLeftMeta,
|
||||||
|
0xFFE8: stone.KeyRightMeta,
|
||||||
|
0xFFE9: stone.KeyLeftAlt,
|
||||||
|
0xFFEA: stone.KeyRightAlt,
|
||||||
|
0xFFEB: stone.KeyLeftSuper,
|
||||||
|
0xFFEC: stone.KeyRightSuper,
|
||||||
|
0xFFED: stone.KeyLeftHyper,
|
||||||
|
0xFFEE: stone.KeyRightHyper,
|
||||||
|
|
||||||
|
0xFFFF: stone.KeyDelete,
|
||||||
|
|
||||||
|
0xFFBE: stone.KeyF1,
|
||||||
|
0xFFBF: stone.KeyF2,
|
||||||
|
0xFFC0: stone.KeyF3,
|
||||||
|
0xFFC1: stone.KeyF4,
|
||||||
|
0xFFC2: stone.KeyF5,
|
||||||
|
0xFFC3: stone.KeyF6,
|
||||||
|
0xFFC4: stone.KeyF7,
|
||||||
|
0xFFC5: stone.KeyF8,
|
||||||
|
0xFFC6: stone.KeyF9,
|
||||||
|
0xFFC7: stone.KeyF10,
|
||||||
|
0xFFC8: stone.KeyF11,
|
||||||
|
0xFFC9: stone.KeyF12,
|
||||||
|
|
||||||
|
// TODO: send this whenever a compose key, dead key, etc is pressed,
|
||||||
|
// and then send the resulting character while witholding the key
|
||||||
|
// presses that were used to compose it. As far as the program is
|
||||||
|
// concerned, a magical key with the final character was pressed and the
|
||||||
|
// KeyDead key is just so that the program might provide some visual
|
||||||
|
// feedback to the user while input is being waited for.
|
||||||
|
0xFF20: stone.KeyDead,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (backend *Backend) keycodeToButton (
|
||||||
|
keycode xproto.Keycode,
|
||||||
|
state uint16,
|
||||||
|
) (
|
||||||
|
button stone.Button,
|
||||||
|
) {
|
||||||
|
// FIXME: also set shift to true if the lock modifier is on and the lock
|
||||||
|
// modifier is interpreted as shiftLock
|
||||||
|
shift := state & xproto.ModMaskShift > 0
|
||||||
|
|
||||||
|
// FIXME: only set this to true if the lock modifier is on and the lock
|
||||||
|
// modifier is interpreted as capsLock
|
||||||
|
capsLock := state & xproto.ModMaskLock > 0
|
||||||
|
|
||||||
|
symbol1 := keybind.KeysymGet(backend.connection, keycode, 0)
|
||||||
|
symbol2 := keybind.KeysymGet(backend.connection, keycode, 1)
|
||||||
|
symbol3 := keybind.KeysymGet(backend.connection, keycode, 2)
|
||||||
|
symbol4 := keybind.KeysymGet(backend.connection, keycode, 3)
|
||||||
|
|
||||||
|
cased := false
|
||||||
|
|
||||||
|
// third paragraph
|
||||||
|
switch {
|
||||||
|
case symbol2 == 0 && symbol3 == 0 && symbol4 == 0:
|
||||||
|
symbol3 = symbol1
|
||||||
|
case symbol3 == 0 && symbol4 == 0:
|
||||||
|
symbol3 = symbol1
|
||||||
|
symbol4 = symbol2
|
||||||
|
case symbol4 == 0:
|
||||||
|
symbol4 = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
symbol1Rune := keysymToRune(symbol1)
|
||||||
|
symbol2Rune := keysymToRune(symbol2)
|
||||||
|
symbol3Rune := keysymToRune(symbol3)
|
||||||
|
symbol4Rune := keysymToRune(symbol4)
|
||||||
|
|
||||||
|
// FIXME: we ignore mode switch stuff
|
||||||
|
_ = symbol4Rune
|
||||||
|
|
||||||
|
// fourth paragraph
|
||||||
|
if symbol2 == 0 {
|
||||||
|
upper := unicode.IsUpper(symbol1Rune)
|
||||||
|
lower := unicode.IsLower(symbol1Rune)
|
||||||
|
if upper || lower {
|
||||||
|
symbol1Rune = unicode.ToLower(symbol1Rune)
|
||||||
|
symbol2Rune = unicode.ToUpper(symbol1Rune)
|
||||||
|
cased = true
|
||||||
|
} else {
|
||||||
|
symbol2 = symbol1
|
||||||
|
symbol2Rune = symbol1Rune
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if symbol4 == 0 {
|
||||||
|
upper := unicode.IsUpper(symbol3Rune)
|
||||||
|
lower := unicode.IsLower(symbol3Rune)
|
||||||
|
if upper || lower {
|
||||||
|
symbol3Rune = unicode.ToLower(symbol3Rune)
|
||||||
|
symbol4Rune = unicode.ToUpper(symbol3Rune)
|
||||||
|
cased = true
|
||||||
|
} else {
|
||||||
|
symbol4 = symbol3
|
||||||
|
symbol4Rune = symbol3Rune
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var selectedKeysym xproto.Keysym
|
||||||
|
var selectedRune rune
|
||||||
|
|
||||||
|
// big ol list in the middle
|
||||||
|
switch {
|
||||||
|
// FIXME: take into account numlock
|
||||||
|
case !shift && !capsLock:
|
||||||
|
selectedKeysym = symbol1
|
||||||
|
selectedRune = symbol1Rune
|
||||||
|
|
||||||
|
case !shift && capsLock:
|
||||||
|
if cased && unicode.IsLower(symbol1Rune) {
|
||||||
|
selectedRune = symbol2Rune
|
||||||
|
} else {
|
||||||
|
selectedKeysym = symbol1
|
||||||
|
selectedRune = symbol1Rune
|
||||||
|
}
|
||||||
|
|
||||||
|
case shift && capsLock:
|
||||||
|
if cased && unicode.IsLower(symbol2Rune) {
|
||||||
|
selectedRune = unicode.ToUpper(symbol2Rune)
|
||||||
|
} else {
|
||||||
|
selectedKeysym = symbol2
|
||||||
|
selectedRune = symbol2Rune
|
||||||
|
}
|
||||||
|
|
||||||
|
case shift:
|
||||||
|
selectedKeysym = symbol2
|
||||||
|
selectedRune = symbol2Rune
|
||||||
|
}
|
||||||
|
|
||||||
|
// look up in table
|
||||||
|
var isControl bool
|
||||||
|
button, isControl = buttonCodeTable[selectedKeysym]
|
||||||
|
|
||||||
|
// if it wasn't found,
|
||||||
|
if !isControl {
|
||||||
|
button = stone.Button(selectedRune)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func keysymToRune (keysym xproto.Keysym) (character rune) {
|
||||||
|
// X keysyms like 0xFF.. or 0xFE.. are non-character keys. these cannot
|
||||||
|
// be converted so we return a zero.
|
||||||
|
if (keysym >> 8) == 0xFF || (keysym >> 8) == 0xFE {
|
||||||
|
character = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// some X keysyms have a single bit set to 1 here. i believe this is to
|
||||||
|
// prevent conflicts with existing codes. if we mask it off we will get
|
||||||
|
// a correct utf-32 code point.
|
||||||
|
if keysym & 0xF000000 == 0x1000000 {
|
||||||
|
character = rune(keysym & 0x0111111)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// if none of these things happened, we can safely (i think) assume that
|
||||||
|
// the keysym is an exact utf-32 code point.
|
||||||
|
character = rune(keysym)
|
||||||
|
return
|
||||||
|
}
|
@ -27,10 +27,7 @@ type Backend struct {
|
|||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
font struct {
|
font struct {
|
||||||
normal font.Face
|
face font.Face
|
||||||
bold font.Face
|
|
||||||
italic font.Face
|
|
||||||
boldItalic font.Face
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colors [8]xgraphics.BGRA
|
colors [8]xgraphics.BGRA
|
||||||
@ -46,18 +43,6 @@ type Backend struct {
|
|||||||
descent int
|
descent int
|
||||||
}
|
}
|
||||||
|
|
||||||
modifierMasks struct {
|
|
||||||
capsLock uint16
|
|
||||||
shiftLock uint16
|
|
||||||
numLock uint16
|
|
||||||
modeSwitch uint16
|
|
||||||
|
|
||||||
alt uint16
|
|
||||||
meta uint16
|
|
||||||
super uint16
|
|
||||||
hyper uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
windowBoundsClean bool
|
windowBoundsClean bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
buffer.go
18
buffer.go
@ -21,11 +21,11 @@ const (
|
|||||||
type Style uint8
|
type Style uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
StyleNormal Style = 0
|
StyleNormal Style = iota
|
||||||
StyleBold Style = 1
|
StyleBold Style = iota >> 1
|
||||||
StyleItalic Style = 2
|
StyleItalic
|
||||||
StyleUnderline Style = 4
|
StyleUnderline
|
||||||
StyleHighlight Style = 8
|
StyleHighlight
|
||||||
StyleBoldItalic Style = StyleBold | StyleItalic
|
StyleBoldItalic Style = StyleBold | StyleItalic
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ func (cell Cell) Color () (color Color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Style returns the styling information associated with the cell
|
// Style returns the styling information associated with the cell
|
||||||
func (cell Cell) Style () (style Style) {
|
func (cell Cell) Style (style Style) {
|
||||||
style = cell.style
|
style = cell.style
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -192,13 +192,9 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
|
|||||||
bytesWritten = len(bytes)
|
bytesWritten = len(bytes)
|
||||||
|
|
||||||
for _, character := range text {
|
for _, character := range text {
|
||||||
if character == '\n' {
|
|
||||||
buffer.dot.x = 0
|
|
||||||
buffer.dot.y ++
|
|
||||||
} else {
|
|
||||||
buffer.setRune(buffer.dot.x, buffer.dot.y, character)
|
buffer.setRune(buffer.dot.x, buffer.dot.y, character)
|
||||||
buffer.dot.x ++
|
buffer.dot.x ++
|
||||||
}
|
if buffer.dot.x > buffer.width { break }
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
218
config.go
218
config.go
@ -1,139 +1,171 @@
|
|||||||
package stone
|
package stone
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
import "bufio"
|
||||||
|
import "strings"
|
||||||
|
import "strconv"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
|
import "path/filepath"
|
||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/stone/config"
|
// Config stores configuration parameters. Backends only should honor parameters
|
||||||
|
// that they can support.
|
||||||
// Config stores global, read-only configuration parameters that apply to all
|
|
||||||
// applications. Backends only should honor parameters that they can support.
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
private config.Config
|
|
||||||
colors [8]color.Color
|
colors [8]color.Color
|
||||||
padding int
|
padding int
|
||||||
center bool
|
center bool
|
||||||
fontSize int
|
fontSize int
|
||||||
fontNameNormal string
|
fontName string
|
||||||
fontNameBold string
|
|
||||||
fontNameItalic string
|
|
||||||
fontNameBoldItalic string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color returns the color value at the specified index.
|
// Color returns the color value at the specified index.
|
||||||
func (public *Config) Color (index Color) (value color.Color) {
|
func (config *Config) Color (index Color) (value color.Color) {
|
||||||
value = public.colors[index]
|
value = config.colors[index]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Padding specifies how many cell's worth of padding should be on all sides of
|
// Padding specifies how many cell's worth of padding should be on all sides of
|
||||||
// the buffer.
|
// the buffer.
|
||||||
func (public *Config) Padding () (padding int) {
|
func (config *Config) Padding () (padding int) {
|
||||||
padding = public.padding
|
padding = config.padding
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Center returns whether the buffer should be displayed in the center of the
|
// Center returns whether the buffer should be displayed in the center of the
|
||||||
// window like in kitty, or aligned to one corner like in gnome-terminal.
|
// window like in kitty, or aligned to one corner like in gnome-terminal.
|
||||||
func (public *Config) Center () (center bool) {
|
func (config *Config) Center () (center bool) {
|
||||||
center = public.center
|
center = config.center
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FontSize specifies how big the font should be.
|
// FontSize specifies how big the font should be.
|
||||||
func (public *Config) FontSize () (fontSize int) {
|
func (config *Config) FontSize () (fontSize int) {
|
||||||
fontSize = public.fontSize
|
fontSize = config.fontSize
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FontNameNormal specifies the name of the font to use for normal text.
|
// FontName specifies the name of the font to use.
|
||||||
func (public *Config) FontNameNormal () (fontName string) {
|
func (config *Config) FontName () (fontName string) {
|
||||||
fontName = public.fontNameNormal
|
fontName = config.fontName
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FontNameBold specifies the name of the font to use for bold text.
|
func (config *Config) load () {
|
||||||
func (public *Config) FontNameBold () (fontName string) {
|
config.colors = [8]color.Color {
|
||||||
fontName = public.fontNameBold
|
// background
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FontName specifies the name of the font to use for text.
|
|
||||||
func (public *Config) FontNameItalic () (fontName string) {
|
|
||||||
fontName = public.fontNameItalic
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// FontName specifies the name of the font to use for text.
|
|
||||||
func (public *Config) FontNameBoldItalic () (fontName string) {
|
|
||||||
fontName = public.fontNameBoldItalic
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (public *Config) load () {
|
|
||||||
public.private = config.Config {
|
|
||||||
LegalParameters: map[string] config.Type {
|
|
||||||
"fontNormal": config.TypeString,
|
|
||||||
"fontBold": config.TypeString,
|
|
||||||
"fontItalic": config.TypeString,
|
|
||||||
"fontBoldItalic": config.TypeString,
|
|
||||||
"fontSize": config.TypeInteger,
|
|
||||||
"padding": config.TypeInteger,
|
|
||||||
"center": config.TypeBoolean,
|
|
||||||
"colorBackground": config.TypeColor,
|
|
||||||
"colorForeground": config.TypeColor,
|
|
||||||
"colorDim": config.TypeColor,
|
|
||||||
"colorRed": config.TypeColor,
|
|
||||||
"colorYellow": config.TypeColor,
|
|
||||||
"colorGreen": config.TypeColor,
|
|
||||||
"colorBlue": config.TypeColor,
|
|
||||||
"colorPurple": config.TypeColor,
|
|
||||||
},
|
|
||||||
|
|
||||||
Parameters: map[string] any {
|
|
||||||
"fontNormal": "",
|
|
||||||
"fontBold": "",
|
|
||||||
"fontItalic": "",
|
|
||||||
"fontBoldItalic": "",
|
|
||||||
"fontSize": 11,
|
|
||||||
"padding": 2,
|
|
||||||
"center": false,
|
|
||||||
"colorBackground":
|
|
||||||
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
|
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
|
||||||
"colorForeground":
|
// foreground
|
||||||
color.RGBA { R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF },
|
color.RGBA { R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF },
|
||||||
"colorDim":
|
// dim
|
||||||
color.RGBA { R: 0x80, G: 0x80, B: 0x80, A: 0xFF },
|
color.RGBA { R: 0x80, G: 0x80, B: 0x80, A: 0xFF },
|
||||||
"colorRed":
|
// red
|
||||||
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
|
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
|
||||||
"colorYellow":
|
// yellow
|
||||||
color.RGBA { R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF },
|
color.RGBA { R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF },
|
||||||
"colorGreen":
|
// green
|
||||||
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
|
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
|
||||||
"colorBlue":
|
// blue
|
||||||
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
|
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
|
||||||
"colorPurple":
|
// purple
|
||||||
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
|
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
|
||||||
},
|
}
|
||||||
|
config.fontName = ""
|
||||||
|
config.fontSize = 11
|
||||||
|
config.padding = 2
|
||||||
|
|
||||||
|
config.loadFile("/etc/stone/stone.conf")
|
||||||
|
homeDirectory, err := os.UserHomeDir()
|
||||||
|
if err != nil { return }
|
||||||
|
config.loadFile(filepath.Join(homeDirectory, "/.config/stone/stone.conf"))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) loadFile (path string) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
|
if len(line) == 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
public.private.Load("stone")
|
if line[0] == '#' {
|
||||||
params := public.private.Parameters
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
public.fontNameNormal = params["fontNormal"].(string)
|
key, value, found := strings.Cut(scanner.Text(), ":")
|
||||||
public.fontNameBold = params["fontBold"].(string)
|
if !found {
|
||||||
public.fontNameItalic = params["fontItalic"].(string)
|
println (
|
||||||
public.fontNameBoldItalic = params["fontBoldItalic"].(string)
|
"config: error in file", path +
|
||||||
public.fontSize = params["fontSize"].(int)
|
": key-value separator missing")
|
||||||
public.padding = params["padding"].(int)
|
println(scanner.Text())
|
||||||
public.center = params["center"].(bool)
|
continue
|
||||||
|
}
|
||||||
|
key = strings.TrimSpace(key)
|
||||||
|
value = strings.TrimSpace(value)
|
||||||
|
|
||||||
public.colors[ColorBackground] = params["colorBackground"].(color.RGBA)
|
var valueInt int
|
||||||
public.colors[ColorForeground] = params["colorForeground"].(color.RGBA)
|
var valueColor color.Color
|
||||||
public.colors[ColorDim] = params["colorDim" ].(color.RGBA)
|
var valueBoolean bool
|
||||||
public.colors[ColorRed] = params["colorRed" ].(color.RGBA)
|
|
||||||
public.colors[ColorYellow] = params["colorYellow" ].(color.RGBA)
|
|
||||||
public.colors[ColorGreen] = params["colorGreen" ].(color.RGBA)
|
|
||||||
public.colors[ColorBlue] = params["colorBlue" ].(color.RGBA)
|
|
||||||
public.colors[ColorPurple] = params["colorPurple" ].(color.RGBA)
|
|
||||||
|
|
||||||
return
|
if value == "true" {
|
||||||
|
valueBoolean = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if value[0] == '#' {
|
||||||
|
if len(value) != 7 {
|
||||||
|
println (
|
||||||
|
"config: error in file", path +
|
||||||
|
": malformed color literal")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
colorInt, err := strconv.ParseUint(value[1:7], 16, 24)
|
||||||
|
if err != nil {
|
||||||
|
println (
|
||||||
|
"config: error in file", path +
|
||||||
|
": malformed color literal")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
valueColor = color.RGBA {
|
||||||
|
R: uint8(colorInt >> 16),
|
||||||
|
G: uint8(colorInt >> 8),
|
||||||
|
B: uint8(colorInt),
|
||||||
|
A: 0xFF,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
valueInt, _ = strconv.Atoi(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch key {
|
||||||
|
case "fontNormal":
|
||||||
|
config.fontName = value
|
||||||
|
case "fontSize":
|
||||||
|
config.fontSize = valueInt
|
||||||
|
case "padding":
|
||||||
|
config.padding = valueInt
|
||||||
|
case "center":
|
||||||
|
config.center = valueBoolean
|
||||||
|
case "colorBackground":
|
||||||
|
config.colors[ColorBackground] = valueColor
|
||||||
|
case "colorForeground":
|
||||||
|
config.colors[ColorForeground] = valueColor
|
||||||
|
case "colorDim":
|
||||||
|
config.colors[ColorDim] = valueColor
|
||||||
|
case "colorRed":
|
||||||
|
config.colors[ColorRed] = valueColor
|
||||||
|
case "colorYellow":
|
||||||
|
config.colors[ColorYellow] = valueColor
|
||||||
|
case "colorGreen":
|
||||||
|
config.colors[ColorGreen] = valueColor
|
||||||
|
case "colorBlue":
|
||||||
|
config.colors[ColorBlue] = valueColor
|
||||||
|
case "colorPurple":
|
||||||
|
config.colors[ColorPurple] = valueColor
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
324
config/config.go
324
config/config.go
@ -1,324 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
import "os"
|
|
||||||
import "fmt"
|
|
||||||
import "sort"
|
|
||||||
import "bufio"
|
|
||||||
import "strings"
|
|
||||||
import "strconv"
|
|
||||||
import "image/color"
|
|
||||||
import "path/filepath"
|
|
||||||
|
|
||||||
// when making changes to this file, look at
|
|
||||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
||||||
|
|
||||||
// Error represents an error that can be returned by functions or methods in
|
|
||||||
// this module.
|
|
||||||
type Error int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// ErrorIllegalName is thrown when an application name contains illegal
|
|
||||||
// characters such as a slash.
|
|
||||||
ErrorIllegalName Error = iota
|
|
||||||
|
|
||||||
// ErrorNoSeparator is thrown when a configuration file has an
|
|
||||||
// incorrectly formatted key-value pair.
|
|
||||||
ErrorNoSeparator
|
|
||||||
|
|
||||||
// ErrorUnknownParameter is thrown when an unknown key is encountered in
|
|
||||||
// a configuration file.
|
|
||||||
ErrorUnknownParameter
|
|
||||||
|
|
||||||
// ErrorWrongColorLength is thrown when a configuration file has a color
|
|
||||||
// literal with a total length unequal to 7.
|
|
||||||
ErrorWrongColorLength
|
|
||||||
|
|
||||||
// ErrorMalformedColorLiteral is thrown when a configuration file has an
|
|
||||||
// improperly formatted color literal, or a color literal was expected
|
|
||||||
// and something else was encountered.
|
|
||||||
ErrorMalformedColorLiteral
|
|
||||||
|
|
||||||
// ErrorMalformedIntegerLiteral is thrown when a configuration file has
|
|
||||||
// an improperly formatted integer literal, or an integer literal was
|
|
||||||
// expected and something else was encountered.
|
|
||||||
ErrorMalformedIntegerLiteral
|
|
||||||
|
|
||||||
// ErrorMalformedFloatLiteral is thrown when a configuration file has
|
|
||||||
// an improperly formatted float literal, or a float literal was
|
|
||||||
// expected and something else was encountered.
|
|
||||||
ErrorMalformedFloatLiteral
|
|
||||||
)
|
|
||||||
|
|
||||||
// Error returns a description of the error.
|
|
||||||
func (err Error) Error () (description string) {
|
|
||||||
switch err {
|
|
||||||
case ErrorIllegalName:
|
|
||||||
description = "name contains illegal characters"
|
|
||||||
case ErrorNoSeparator:
|
|
||||||
description = "key:value pair has no separator"
|
|
||||||
case ErrorUnknownParameter:
|
|
||||||
description = "unknown parameter"
|
|
||||||
case ErrorWrongColorLength:
|
|
||||||
description = "color literal has the wrong length"
|
|
||||||
case ErrorMalformedColorLiteral:
|
|
||||||
description = "malformed color literal"
|
|
||||||
case ErrorMalformedIntegerLiteral:
|
|
||||||
description = "malformed integer literal"
|
|
||||||
case ErrorMalformedFloatLiteral:
|
|
||||||
description = "malformed float literal"
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type represents the data type of a configuration parameter.
|
|
||||||
type Type int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// string
|
|
||||||
// It is just a basic string with inner whitespace preserved. No quotes
|
|
||||||
// should be used in the file.
|
|
||||||
TypeString Type = iota
|
|
||||||
|
|
||||||
// Type: image/color.RGBA
|
|
||||||
// Represented as a 24 bit hexadecimal number (case insensitive)
|
|
||||||
// preceded with a # sign where the first two digits represent the red
|
|
||||||
// channel, the middle two digits represent the green channel, and the
|
|
||||||
// last two digits represent the blue channel.
|
|
||||||
TypeColor
|
|
||||||
|
|
||||||
// Type: int
|
|
||||||
// An integer literal, like 123456789
|
|
||||||
TypeInteger
|
|
||||||
|
|
||||||
// Type: float64
|
|
||||||
// A floating point literal, like 1234.56789
|
|
||||||
TypeFloat
|
|
||||||
|
|
||||||
// Type: bool
|
|
||||||
// Values true, yes, on, and 1 are all truthy (case insensitive) and
|
|
||||||
// anything else is falsy.
|
|
||||||
TypeBoolean
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config holds a list of configuration parameters.
|
|
||||||
type Config struct {
|
|
||||||
// LegalParameters holds the names and types of all parameters that can
|
|
||||||
// be parsed. If the parser runs into a parameter that is not listed
|
|
||||||
// here, it will print out an error message and keep on parsing.
|
|
||||||
LegalParameters map[string] Type
|
|
||||||
|
|
||||||
// Parameters holds the names and values of all parsed parameters. If a
|
|
||||||
// value is non-nil, it can be safely type asserted into whatever type
|
|
||||||
// was requested.
|
|
||||||
Parameters map[string] any
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads and parses the files /etc/xdg/<name>/<name>.conf and
|
|
||||||
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
|
|
||||||
// variables are set - then it uses those.
|
|
||||||
func (config *Config) Load (name string) (err error) {
|
|
||||||
if nameIsIllegal(name) {
|
|
||||||
err = ErrorIllegalName
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, directory := range configDirs {
|
|
||||||
path := filepath.Join(directory, name, name + ".conf")
|
|
||||||
|
|
||||||
file, fileErr := os.Open(path)
|
|
||||||
if fileErr != nil { continue }
|
|
||||||
parseErr := config.LoadFrom(file)
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
if parseErr != nil {
|
|
||||||
println (
|
|
||||||
"config: error in file", path +
|
|
||||||
":", parseErr.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadFrom parses a configuration file from an io.Reader. Configuration files
|
|
||||||
// are divided into lines where each line may be blank, a comment, or a
|
|
||||||
// key-value pair. If the line is blank or begins with a # character, it is
|
|
||||||
// ignored. Else, the line must have a key and a value separated by a colon.
|
|
||||||
// Before they are processed, leading and trailing whitespace is trimmed from
|
|
||||||
// the key and the value. Keys are case sensitive.
|
|
||||||
func (config *Config) LoadFrom (reader io.Reader) (err error) {
|
|
||||||
if config.LegalParameters == nil {
|
|
||||||
config.LegalParameters = make(map[string] Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Parameters == nil {
|
|
||||||
config.Parameters = make(map[string] any)
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(reader)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := strings.TrimSpace(scanner.Text())
|
|
||||||
|
|
||||||
if len(line) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if line[0] == '#' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
key, value, found := strings.Cut(scanner.Text(), ":")
|
|
||||||
if !found {
|
|
||||||
err = ErrorNoSeparator
|
|
||||||
return
|
|
||||||
}
|
|
||||||
key = strings.TrimSpace(key)
|
|
||||||
value = strings.TrimSpace(value)
|
|
||||||
|
|
||||||
what, isKnown := config.LegalParameters[key]
|
|
||||||
if !isKnown {
|
|
||||||
err = ErrorUnknownParameter
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch what {
|
|
||||||
case TypeString:
|
|
||||||
config.Parameters[key] = value
|
|
||||||
|
|
||||||
case TypeColor:
|
|
||||||
var valueColor color.Color
|
|
||||||
valueColor, err = parseColor(value)
|
|
||||||
if err != nil { return }
|
|
||||||
config.Parameters[key] = valueColor
|
|
||||||
|
|
||||||
case TypeInteger:
|
|
||||||
var valueInt int
|
|
||||||
valueInt, err = strconv.Atoi(value)
|
|
||||||
if err != nil {
|
|
||||||
err = ErrorMalformedIntegerLiteral
|
|
||||||
return
|
|
||||||
}
|
|
||||||
config.Parameters[key] = valueInt
|
|
||||||
|
|
||||||
case TypeFloat:
|
|
||||||
var valueFloat float64
|
|
||||||
valueFloat, err = strconv.ParseFloat(value, 64)
|
|
||||||
if err != nil {
|
|
||||||
err = ErrorMalformedFloatLiteral
|
|
||||||
return
|
|
||||||
}
|
|
||||||
config.Parameters[key] = valueFloat
|
|
||||||
|
|
||||||
case TypeBoolean:
|
|
||||||
value = strings.ToLower(value)
|
|
||||||
truthy :=
|
|
||||||
value == "true" ||
|
|
||||||
value == "yes" ||
|
|
||||||
value == "on" ||
|
|
||||||
value == "1"
|
|
||||||
config.Parameters[key] = truthy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save overwrites the main user configuration file, which is located at
|
|
||||||
// <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in
|
|
||||||
// which case the value of that variable is used instead.
|
|
||||||
func (config *Config) Save (name string) (err error) {
|
|
||||||
if nameIsIllegal(name) {
|
|
||||||
err = ErrorIllegalName
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.MkdirAll(configHome, 0755)
|
|
||||||
if err != nil { return }
|
|
||||||
|
|
||||||
file, err := os.OpenFile (
|
|
||||||
filepath.Join(configHome, name, name + ".conf"),
|
|
||||||
os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0744)
|
|
||||||
if err != nil { return }
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
err = config.SaveTo(file)
|
|
||||||
if err != nil { return }
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaveTo writes the configuration data to the specified io.Writer. Keys are
|
|
||||||
// alphabetically sorted.
|
|
||||||
func (config *Config) SaveTo (writer io.Writer) (err error) {
|
|
||||||
keys := make([]string, len(config.Parameters))
|
|
||||||
index := 0
|
|
||||||
for key, _ := range config.Parameters {
|
|
||||||
keys[index] = key
|
|
||||||
index ++
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
|
|
||||||
for _, key := range keys {
|
|
||||||
value := config.Parameters[key]
|
|
||||||
switch value.(type) {
|
|
||||||
case string:
|
|
||||||
fmt.Fprintf(writer,"%s: %s\n", key, value.(string))
|
|
||||||
|
|
||||||
case color.RGBA:
|
|
||||||
colorValue := value.(color.RGBA)
|
|
||||||
colorInt :=
|
|
||||||
uint64(colorValue.R) << 16 |
|
|
||||||
uint64(colorValue.G) << 8 |
|
|
||||||
uint64(colorValue.B)
|
|
||||||
fmt.Fprintf(writer,"%s: #%06x\n", key, colorInt)
|
|
||||||
|
|
||||||
case int:
|
|
||||||
fmt.Fprintf(writer,"%s: %d\n", key, value.(int))
|
|
||||||
|
|
||||||
case float64:
|
|
||||||
fmt.Fprintf(writer,"%s: %f\n", key, value.(float64))
|
|
||||||
|
|
||||||
case bool:
|
|
||||||
fmt.Fprintf(writer,"%s: %t\n", key, value.(bool))
|
|
||||||
default:
|
|
||||||
fmt.Fprintf(writer,"# %s: unknown type\n", key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseColor (value string) (valueColor color.Color, err error) {
|
|
||||||
if value[0] == '#' {
|
|
||||||
if len(value) != 7 {
|
|
||||||
err = ErrorWrongColorLength
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var colorInt uint64
|
|
||||||
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
|
|
||||||
if err != nil {
|
|
||||||
err = ErrorMalformedColorLiteral
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
valueColor = color.RGBA {
|
|
||||||
R: uint8(colorInt >> 16),
|
|
||||||
G: uint8(colorInt >> 8),
|
|
||||||
B: uint8(colorInt),
|
|
||||||
A: 0xFF,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err = ErrorMalformedColorLiteral
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func nameIsIllegal (name string) (legal bool) {
|
|
||||||
legal = strings.ContainsAny(name, "/\\|:.%")
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
import "strings"
|
|
||||||
import "path/filepath"
|
|
||||||
|
|
||||||
var homeDirectory string
|
|
||||||
var configHome string
|
|
||||||
var configDirs []string
|
|
||||||
var dataHome string
|
|
||||||
var cacheHome string
|
|
||||||
|
|
||||||
func init () {
|
|
||||||
var err error
|
|
||||||
homeDirectory, err = os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
panic("could not get user home directory: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
configHome = os.Getenv("XDG_CONFIG_HOME")
|
|
||||||
if configHome == "" {
|
|
||||||
configHome = filepath.Join(homeDirectory, "/.config/")
|
|
||||||
}
|
|
||||||
|
|
||||||
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
|
|
||||||
if configDirsString == "" {
|
|
||||||
configDirsString = "/etc/xdg/"
|
|
||||||
}
|
|
||||||
|
|
||||||
configDirs = append(strings.Split(configDirsString, ":"), configHome)
|
|
||||||
|
|
||||||
dataHome = os.Getenv("XDG_DATA_HOME")
|
|
||||||
if dataHome == "" {
|
|
||||||
dataHome = filepath.Join(homeDirectory, "/.local/share/")
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheHome = os.Getenv("XDG_CACHE_HOME")
|
|
||||||
if cacheHome == "" {
|
|
||||||
cacheHome = filepath.Join(homeDirectory, "/.cache/")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DataHome returns the path to the directory where user data should be stored.
|
|
||||||
func DataHome (name string) (home string) {
|
|
||||||
home = filepath.Join(dataHome, name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// CacheHome returns the path to the directory where cache files should be
|
|
||||||
// stored.
|
|
||||||
func CacheHome (name string) (home string) {
|
|
||||||
home = filepath.Join(cacheHome, name)
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
import "image"
|
|
||||||
import "image/color"
|
|
||||||
import _ "image/png"
|
|
||||||
import "git.tebibyte.media/sashakoshka/stone"
|
|
||||||
import "git.tebibyte.media/sashakoshka/stone/config"
|
|
||||||
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
|
|
||||||
|
|
||||||
var application = &stone.Application { }
|
|
||||||
var inputState struct {
|
|
||||||
x int
|
|
||||||
y int
|
|
||||||
}
|
|
||||||
var globalConfig config.Config
|
|
||||||
|
|
||||||
func main () {
|
|
||||||
application.SetTitle("configuration viewer")
|
|
||||||
application.SetSize(32, 16)
|
|
||||||
|
|
||||||
iconFile16, err := os.Open("assets/scaffold16.png")
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
icon16, _, err := image.Decode(iconFile16)
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
iconFile16.Close()
|
|
||||||
iconFile32, err := os.Open("assets/scaffold32.png")
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
icon32, _, err := image.Decode(iconFile32)
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
iconFile16.Close()
|
|
||||||
|
|
||||||
application.SetIcon([]image.Image { icon16, icon32 })
|
|
||||||
|
|
||||||
application.OnPress(onPress)
|
|
||||||
application.OnRelease(onRelease)
|
|
||||||
application.OnMouseMove(onMouseMove)
|
|
||||||
application.OnStart(onStart)
|
|
||||||
application.OnResize(redraw)
|
|
||||||
|
|
||||||
err = application.Run()
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
func onStart () {
|
|
||||||
// this is just copy pasted from config.go
|
|
||||||
globalConfig = config.Config {
|
|
||||||
LegalParameters: map[string] config.Type {
|
|
||||||
"fontNormal": config.TypeString,
|
|
||||||
"fontSize": config.TypeInteger,
|
|
||||||
"padding": config.TypeInteger,
|
|
||||||
"center": config.TypeBoolean,
|
|
||||||
"colorBackground": config.TypeColor,
|
|
||||||
"colorForeground": config.TypeColor,
|
|
||||||
"colorDim": config.TypeColor,
|
|
||||||
"colorRed": config.TypeColor,
|
|
||||||
"colorYellow": config.TypeColor,
|
|
||||||
"colorGreen": config.TypeColor,
|
|
||||||
"colorBlue": config.TypeColor,
|
|
||||||
"colorPurple": config.TypeColor,
|
|
||||||
},
|
|
||||||
|
|
||||||
Parameters: map[string] any {
|
|
||||||
"fontNormal": "",
|
|
||||||
"fontSize": 11,
|
|
||||||
"padding": 2,
|
|
||||||
"center": false,
|
|
||||||
"colorBackground":
|
|
||||||
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
|
|
||||||
"colorForeground":
|
|
||||||
color.RGBA { R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF },
|
|
||||||
"colorDim":
|
|
||||||
color.RGBA { R: 0x80, G: 0x80, B: 0x80, A: 0xFF },
|
|
||||||
"colorRed":
|
|
||||||
color.RGBA { R: 0xFF, G: 0x00, B: 0x00, A: 0xFF },
|
|
||||||
"colorYellow":
|
|
||||||
color.RGBA { R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF },
|
|
||||||
"colorGreen":
|
|
||||||
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
|
|
||||||
"colorBlue":
|
|
||||||
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
|
|
||||||
"colorPurple":
|
|
||||||
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
globalConfig.Load("stone")
|
|
||||||
redraw()
|
|
||||||
}
|
|
||||||
|
|
||||||
func redraw () {
|
|
||||||
// i fucking love go interfaces
|
|
||||||
application.Clear()
|
|
||||||
application.SetDot(0, 0)
|
|
||||||
globalConfig.SaveTo(application)
|
|
||||||
}
|
|
||||||
|
|
||||||
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
|
||||||
}
|
|
||||||
|
|
||||||
func onRelease (button stone.Button) {
|
|
||||||
}
|
|
||||||
|
|
||||||
func onMouseMove (x, y int) {
|
|
||||||
inputState.x = x
|
|
||||||
inputState.y = y
|
|
||||||
}
|
|
@ -50,8 +50,7 @@ func onRelease (button stone.Button) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func onMouseMove (x, y int) {
|
func onMouseMove (x, y int) { if mousePressed {
|
||||||
if mousePressed {
|
|
||||||
application.SetRune(x, y, '#')
|
application.SetRune(x, y, '#')
|
||||||
application.Draw()
|
application.Draw()
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
import "fmt"
|
|
||||||
import "image"
|
import "image"
|
||||||
import _ "image/png"
|
import _ "image/png"
|
||||||
import "git.tebibyte.media/sashakoshka/stone"
|
import "git.tebibyte.media/sashakoshka/stone"
|
||||||
@ -34,18 +33,9 @@ func main () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
||||||
fmt.Printf (
|
println("press", button)
|
||||||
"=>>\t0x%X\tsh: %t\tctrl: %t\talt: %t\tm: %t\ts: %t \th: %t\tnumpad: %t\n",
|
|
||||||
button,
|
|
||||||
modifiers.Shift,
|
|
||||||
modifiers.Control,
|
|
||||||
modifiers.Alt,
|
|
||||||
modifiers.Meta,
|
|
||||||
modifiers.Super,
|
|
||||||
modifiers.Hyper,
|
|
||||||
modifiers.NumberPad)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func onRelease (button stone.Button) {
|
func onRelease (button stone.Button) {
|
||||||
fmt.Printf("<--\t0x%X\n", button)
|
println("release", button)
|
||||||
}
|
}
|
||||||
|
@ -1,77 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
import "fmt"
|
|
||||||
import "image"
|
|
||||||
import "math/rand"
|
|
||||||
import _ "image/png"
|
|
||||||
import "git.tebibyte.media/sashakoshka/stone"
|
|
||||||
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
|
|
||||||
|
|
||||||
var application = &stone.Application { }
|
|
||||||
|
|
||||||
func main () {
|
|
||||||
application.SetTitle("style demo")
|
|
||||||
application.SetSize(11, 8)
|
|
||||||
|
|
||||||
iconFile16, err := os.Open("assets/scaffold16.png")
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
icon16, _, err := image.Decode(iconFile16)
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
iconFile16.Close()
|
|
||||||
iconFile32, err := os.Open("assets/scaffold32.png")
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
icon32, _, err := image.Decode(iconFile32)
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
iconFile16.Close()
|
|
||||||
|
|
||||||
application.SetIcon([]image.Image { icon16, icon32 })
|
|
||||||
|
|
||||||
application.OnStart(redraw)
|
|
||||||
application.OnResize(redraw)
|
|
||||||
application.OnPress(onPress)
|
|
||||||
|
|
||||||
err = application.Run()
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
|
||||||
redraw()
|
|
||||||
application.Draw()
|
|
||||||
}
|
|
||||||
|
|
||||||
func redraw () {
|
|
||||||
width, _ := application.Size()
|
|
||||||
application.SetDot(0, 0)
|
|
||||||
fmt.Fprint (
|
|
||||||
application,
|
|
||||||
"normal\n",
|
|
||||||
"bold\n",
|
|
||||||
"italic\n",
|
|
||||||
"underline\n",
|
|
||||||
"all 3\n",
|
|
||||||
"highlighted\n",
|
|
||||||
"all 4\n",
|
|
||||||
"highlight?")
|
|
||||||
fillStyle(0, width, stone.StyleNormal)
|
|
||||||
fillStyle(1, width, stone.StyleBold)
|
|
||||||
fillStyle(2, width, stone.StyleItalic)
|
|
||||||
fillStyle(3, width, stone.StyleUnderline)
|
|
||||||
fillStyle(4, width, stone.StyleBoldItalic | stone.StyleUnderline)
|
|
||||||
fillStyle(5, width, stone.StyleHighlight)
|
|
||||||
fillStyle(6, width, stone.StyleBoldItalic | stone.StyleUnderline |
|
|
||||||
stone.StyleHighlight)
|
|
||||||
|
|
||||||
if rand.Int() % 2 == 0 {
|
|
||||||
fillStyle(7, width, stone.StyleNormal)
|
|
||||||
} else {
|
|
||||||
fillStyle(7, width, stone.StyleHighlight)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fillStyle (yOffset, width int, style stone.Style) {
|
|
||||||
for x := 0; x < width; x ++ {
|
|
||||||
application.SetStyle(x, yOffset, style)
|
|
||||||
application.SetColor(x, yOffset, stone.Color(x % 7 + 1))
|
|
||||||
}
|
|
||||||
}
|
|
6
input.go
6
input.go
@ -94,10 +94,4 @@ type Modifiers struct {
|
|||||||
Meta bool
|
Meta bool
|
||||||
Super bool
|
Super bool
|
||||||
Hyper bool
|
Hyper bool
|
||||||
|
|
||||||
// NumberPad does not represent a key, but it behaves like one. If it is
|
|
||||||
// set to true, the button was pressed on the number pad. It is treated
|
|
||||||
// as a modifier key because if you don't care whether a key was pressed
|
|
||||||
// on the number pad or not, you can just ignore this value.
|
|
||||||
NumberPad bool
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user