Initial commit
This commit is contained in:
389
backends/x/encoding.go
Normal file
389
backends/x/encoding.go
Normal file
@@ -0,0 +1,389 @@
|
||||
package x
|
||||
|
||||
import "unicode"
|
||||
import "github.com/jezek/xgb/xproto"
|
||||
import "github.com/jezek/xgbutil/keybind"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
|
||||
// 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] tomo.Key {
|
||||
0xFFFFFF: tomo.KeyNone,
|
||||
|
||||
0xFF63: tomo.KeyInsert,
|
||||
0xFF67: tomo.KeyMenu,
|
||||
0xFF61: tomo.KeyPrintScreen,
|
||||
0xFF6B: tomo.KeyPause,
|
||||
0xFFE5: tomo.KeyCapsLock,
|
||||
0xFF14: tomo.KeyScrollLock,
|
||||
0xFF7F: tomo.KeyNumLock,
|
||||
0xFF08: tomo.KeyBackspace,
|
||||
0xFF09: tomo.KeyTab,
|
||||
0xFF0D: tomo.KeyEnter,
|
||||
0xFF1B: tomo.KeyEscape,
|
||||
|
||||
0xFF52: tomo.KeyUp,
|
||||
0xFF54: tomo.KeyDown,
|
||||
0xFF51: tomo.KeyLeft,
|
||||
0xFF53: tomo.KeyRight,
|
||||
0xFF55: tomo.KeyPageUp,
|
||||
0xFF56: tomo.KeyPageDown,
|
||||
0xFF50: tomo.KeyHome,
|
||||
0xFF57: tomo.KeyEnd,
|
||||
|
||||
0xFFE1: tomo.KeyLeftShift,
|
||||
0xFFE2: tomo.KeyRightShift,
|
||||
0xFFE3: tomo.KeyLeftControl,
|
||||
0xFFE4: tomo.KeyRightControl,
|
||||
|
||||
0xFFE7: tomo.KeyLeftMeta,
|
||||
0xFFE8: tomo.KeyRightMeta,
|
||||
0xFFE9: tomo.KeyLeftAlt,
|
||||
0xFFEA: tomo.KeyRightAlt,
|
||||
0xFFEB: tomo.KeyLeftSuper,
|
||||
0xFFEC: tomo.KeyRightSuper,
|
||||
0xFFED: tomo.KeyLeftHyper,
|
||||
0xFFEE: tomo.KeyRightHyper,
|
||||
|
||||
0xFFFF: tomo.KeyDelete,
|
||||
|
||||
0xFFBE: tomo.KeyF1,
|
||||
0xFFBF: tomo.KeyF2,
|
||||
0xFFC0: tomo.KeyF3,
|
||||
0xFFC1: tomo.KeyF4,
|
||||
0xFFC2: tomo.KeyF5,
|
||||
0xFFC3: tomo.KeyF6,
|
||||
0xFFC4: tomo.KeyF7,
|
||||
0xFFC5: tomo.KeyF8,
|
||||
0xFFC6: tomo.KeyF9,
|
||||
0xFFC7: tomo.KeyF10,
|
||||
0xFFC8: tomo.KeyF11,
|
||||
0xFFC9: tomo.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: tomo.KeyDead,
|
||||
}
|
||||
|
||||
var keypadCodeTable = map[xproto.Keysym] tomo.Key {
|
||||
0xff80: tomo.Key(' '),
|
||||
0xff89: tomo.KeyTab,
|
||||
0xff8d: tomo.KeyEnter,
|
||||
0xff91: tomo.KeyF1,
|
||||
0xff92: tomo.KeyF2,
|
||||
0xff93: tomo.KeyF3,
|
||||
0xff94: tomo.KeyF4,
|
||||
0xff95: tomo.KeyHome,
|
||||
0xff96: tomo.KeyLeft,
|
||||
0xff97: tomo.KeyUp,
|
||||
0xff98: tomo.KeyRight,
|
||||
0xff99: tomo.KeyDown,
|
||||
0xff9a: tomo.KeyPageUp,
|
||||
0xff9b: tomo.KeyPageDown,
|
||||
0xff9c: tomo.KeyEnd,
|
||||
0xff9d: tomo.KeyHome,
|
||||
0xff9e: tomo.KeyInsert,
|
||||
0xff9f: tomo.KeyDelete,
|
||||
0xffbd: tomo.Key('='),
|
||||
0xffaa: tomo.Key('*'),
|
||||
0xffab: tomo.Key('+'),
|
||||
0xffac: tomo.Key(','),
|
||||
0xffad: tomo.Key('-'),
|
||||
0xffae: tomo.Key('.'),
|
||||
0xffaf: tomo.Key('/'),
|
||||
|
||||
0xffb0: tomo.Key('0'),
|
||||
0xffb1: tomo.Key('1'),
|
||||
0xffb2: tomo.Key('2'),
|
||||
0xffb3: tomo.Key('3'),
|
||||
0xffb4: tomo.Key('4'),
|
||||
0xffb5: tomo.Key('5'),
|
||||
0xffb6: tomo.Key('6'),
|
||||
0xffb7: tomo.Key('7'),
|
||||
0xffb8: tomo.Key('8'),
|
||||
0xffb9: tomo.Key('9'),
|
||||
}
|
||||
|
||||
// initializeKeymapInformation grabs keyboard mapping information from the X
|
||||
// server.
|
||||
func (backend *Backend) initializeKeymapInformation () {
|
||||
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)
|
||||
}
|
||||
|
||||
// keysymToKeycode converts an X keysym to an X keycode, instead of the other
|
||||
// way around.
|
||||
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
|
||||
}
|
||||
|
||||
// keysymToMask returns the X modmask for a given modifier key.
|
||||
func (backend *Backend) keysymToMask (
|
||||
symbol xproto.Keysym,
|
||||
) (
|
||||
mask uint16,
|
||||
) {
|
||||
mask = keybind.ModGet (
|
||||
backend.connection,
|
||||
backend.keysymToKeycode(symbol))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// keycodeToButton converts an X keycode to a tomo keycode. 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 tomo keycode.
|
||||
func (backend *Backend) keycodeToKey (
|
||||
keycode xproto.Keycode,
|
||||
state uint16,
|
||||
) (
|
||||
button tomo.Key,
|
||||
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 tomo'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 = tomo.Key(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
|
||||
}
|
||||
235
backends/x/event.go
Normal file
235
backends/x/event.go
Normal file
@@ -0,0 +1,235 @@
|
||||
package x
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
|
||||
import "github.com/jezek/xgbutil"
|
||||
import "github.com/jezek/xgb/xproto"
|
||||
import "github.com/jezek/xgbutil/xevent"
|
||||
|
||||
type scrollSum struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
func (sum *scrollSum) add (button xproto.Button) {
|
||||
switch button {
|
||||
case 4:
|
||||
sum.y --
|
||||
case 5:
|
||||
sum.y ++
|
||||
case 6:
|
||||
sum.x --
|
||||
case 7:
|
||||
sum.x ++
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) handleConfigureNotify (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.ConfigureNotifyEvent,
|
||||
) {
|
||||
configureEvent := *event.ConfigureNotifyEvent
|
||||
|
||||
newWidth := int(configureEvent.Width)
|
||||
newHeight := int(configureEvent.Height)
|
||||
sizeChanged :=
|
||||
window.metrics.width != newWidth ||
|
||||
window.metrics.height != newHeight
|
||||
window.metrics.width = newWidth
|
||||
window.metrics.height = newHeight
|
||||
|
||||
if sizeChanged {
|
||||
configureEvent = window.compressConfigureNotify(configureEvent)
|
||||
window.metrics.width = int(configureEvent.Width)
|
||||
window.metrics.height = int(configureEvent.Height)
|
||||
window.reallocateCanvas()
|
||||
window.resizeChildToFit()
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) handleKeyPress (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.KeyPressEvent,
|
||||
) {
|
||||
if window.child == nil { return}
|
||||
|
||||
keyEvent := *event.KeyPressEvent
|
||||
key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State)
|
||||
modifiers := tomo.Modifiers {
|
||||
Shift:
|
||||
(keyEvent.State & xproto.ModMaskShift) > 0 ||
|
||||
(keyEvent.State & window.backend.modifierMasks.shiftLock) > 0,
|
||||
Control: (keyEvent.State & xproto.ModMaskControl) > 0,
|
||||
Alt: (keyEvent.State & window.backend.modifierMasks.alt) > 0,
|
||||
Meta: (keyEvent.State & window.backend.modifierMasks.meta) > 0,
|
||||
Super: (keyEvent.State & window.backend.modifierMasks.super) > 0,
|
||||
Hyper: (keyEvent.State & window.backend.modifierMasks.hyper) > 0,
|
||||
NumberPad: numberPad,
|
||||
}
|
||||
|
||||
window.child.Handle (tomo.EventKeyDown {
|
||||
Key: key,
|
||||
Modifiers: modifiers,
|
||||
Repeated: false, // FIXME: return correct value here
|
||||
})
|
||||
}
|
||||
|
||||
func (window *Window) handleKeyRelease (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.KeyReleaseEvent,
|
||||
) {
|
||||
if window.child == nil { return }
|
||||
|
||||
keyEvent := *event.KeyReleaseEvent
|
||||
key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State)
|
||||
modifiers := tomo.Modifiers {
|
||||
Shift:
|
||||
(keyEvent.State & xproto.ModMaskShift) > 0 ||
|
||||
(keyEvent.State & window.backend.modifierMasks.shiftLock) > 0,
|
||||
Control: (keyEvent.State & xproto.ModMaskControl) > 0,
|
||||
Alt: (keyEvent.State & window.backend.modifierMasks.alt) > 0,
|
||||
Meta: (keyEvent.State & window.backend.modifierMasks.meta) > 0,
|
||||
Super: (keyEvent.State & window.backend.modifierMasks.super) > 0,
|
||||
Hyper: (keyEvent.State & window.backend.modifierMasks.hyper) > 0,
|
||||
NumberPad: numberPad,
|
||||
}
|
||||
|
||||
window.child.Handle (tomo.EventKeyUp {
|
||||
Key: key,
|
||||
Modifiers: modifiers,
|
||||
})
|
||||
}
|
||||
|
||||
func (window *Window) handleButtonPress (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.ButtonPressEvent,
|
||||
) {
|
||||
if window.child == nil { return }
|
||||
|
||||
buttonEvent := *event.ButtonPressEvent
|
||||
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 {
|
||||
sum := scrollSum { }
|
||||
sum.add(buttonEvent.Detail)
|
||||
window.compressScrollSum(buttonEvent, &sum)
|
||||
window.child.Handle (tomo.EventScroll {
|
||||
X: int(buttonEvent.EventX),
|
||||
Y: int(buttonEvent.EventY),
|
||||
ScrollX: sum.x,
|
||||
ScrollY: sum.y,
|
||||
})
|
||||
} else {
|
||||
window.child.Handle (tomo.EventMouseDown {
|
||||
Button: tomo.Button(buttonEvent.Detail),
|
||||
X: int(buttonEvent.EventX),
|
||||
Y: int(buttonEvent.EventY),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) handleButtonRelease (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.ButtonReleaseEvent,
|
||||
) {
|
||||
buttonEvent := *event.ButtonReleaseEvent
|
||||
if buttonEvent.Detail >= 4 && buttonEvent.Detail <= 7 { return }
|
||||
window.child.Handle (tomo.EventMouseUp {
|
||||
Button: tomo.Button(buttonEvent.Detail),
|
||||
X: int(buttonEvent.EventX),
|
||||
Y: int(buttonEvent.EventY),
|
||||
})
|
||||
}
|
||||
|
||||
func (window *Window) handleMotionNotify (
|
||||
connection *xgbutil.XUtil,
|
||||
event xevent.MotionNotifyEvent,
|
||||
) {
|
||||
motionEvent := window.compressMotionNotify(*event.MotionNotifyEvent)
|
||||
window.child.Handle (tomo.EventMouseMove {
|
||||
X: int(motionEvent.EventX),
|
||||
Y: int(motionEvent.EventY),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func (window *Window) compressConfigureNotify (
|
||||
firstEvent xproto.ConfigureNotifyEvent,
|
||||
) (
|
||||
lastEvent xproto.ConfigureNotifyEvent,
|
||||
) {
|
||||
window.backend.connection.Sync()
|
||||
xevent.Read(window.backend.connection, false)
|
||||
lastEvent = firstEvent
|
||||
|
||||
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
||||
if untypedEvent.Err != nil { continue }
|
||||
|
||||
typedEvent, ok := untypedEvent.Event.(xproto.ConfigureNotifyEvent)
|
||||
if !ok { continue }
|
||||
|
||||
if firstEvent.Event == typedEvent.Event &&
|
||||
firstEvent.Window == typedEvent.Window {
|
||||
|
||||
lastEvent = typedEvent
|
||||
defer func (index int) {
|
||||
xevent.DequeueAt(window.backend.connection, index)
|
||||
} (index)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) compressScrollSum (
|
||||
firstEvent xproto.ButtonPressEvent,
|
||||
sum *scrollSum,
|
||||
) {
|
||||
window.backend.connection.Sync()
|
||||
xevent.Read(window.backend.connection, false)
|
||||
|
||||
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
||||
if untypedEvent.Err != nil { continue }
|
||||
|
||||
typedEvent, ok := untypedEvent.Event.(xproto.ButtonPressEvent)
|
||||
if !ok { continue }
|
||||
|
||||
if firstEvent.Event == typedEvent.Event &&
|
||||
typedEvent.Detail >= 4 &&
|
||||
typedEvent.Detail <= 7 {
|
||||
|
||||
sum.add(typedEvent.Detail)
|
||||
defer func (index int) {
|
||||
xevent.DequeueAt(window.backend.connection, index)
|
||||
} (index)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) compressMotionNotify (
|
||||
firstEvent xproto.MotionNotifyEvent,
|
||||
) (
|
||||
lastEvent xproto.MotionNotifyEvent,
|
||||
) {
|
||||
window.backend.connection.Sync()
|
||||
xevent.Read(window.backend.connection, false)
|
||||
lastEvent = firstEvent
|
||||
|
||||
for index, untypedEvent := range xevent.Peek(window.backend.connection) {
|
||||
if untypedEvent.Err != nil { continue }
|
||||
|
||||
typedEvent, ok := untypedEvent.Event.(xproto.MotionNotifyEvent)
|
||||
if !ok { continue }
|
||||
|
||||
if firstEvent.Event == typedEvent.Event &&
|
||||
typedEvent.Detail >= 4 &&
|
||||
typedEvent.Detail <= 7 {
|
||||
|
||||
lastEvent = typedEvent
|
||||
defer func (index int) {
|
||||
xevent.DequeueAt(window.backend.connection, index)
|
||||
} (index)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
328
backends/x/window.go
Normal file
328
backends/x/window.go
Normal file
@@ -0,0 +1,328 @@
|
||||
package x
|
||||
|
||||
import "image"
|
||||
import "image/color"
|
||||
import "github.com/jezek/xgb/xproto"
|
||||
import "github.com/jezek/xgbutil/ewmh"
|
||||
import "github.com/jezek/xgbutil/icccm"
|
||||
import "github.com/jezek/xgbutil/xevent"
|
||||
import "github.com/jezek/xgbutil/xwindow"
|
||||
import "github.com/jezek/xgbutil/xgraphics"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
|
||||
type Window struct {
|
||||
backend *Backend
|
||||
xWindow *xwindow.Window
|
||||
xCanvas *xgraphics.Image
|
||||
child tomo.Element
|
||||
onClose func ()
|
||||
drawCallback func (region tomo.Image)
|
||||
minimumSizeChangeCallback func (width, height int)
|
||||
skipChildDrawCallback bool
|
||||
|
||||
metrics struct {
|
||||
width int
|
||||
height int
|
||||
}
|
||||
}
|
||||
|
||||
func (backend *Backend) NewWindow (
|
||||
width, height int,
|
||||
) (
|
||||
output tomo.Window,
|
||||
err error,
|
||||
) {
|
||||
if backend == nil { panic("nil backend") }
|
||||
|
||||
window := &Window { backend: backend }
|
||||
|
||||
window.xWindow, err = xwindow.Generate(backend.connection)
|
||||
if err != nil { return }
|
||||
window.xWindow.Create (
|
||||
backend.connection.RootWin(),
|
||||
0, 0, width, height, 0)
|
||||
err = window.xWindow.Listen (
|
||||
xproto.EventMaskStructureNotify,
|
||||
xproto.EventMaskPointerMotion,
|
||||
xproto.EventMaskKeyPress,
|
||||
xproto.EventMaskKeyRelease,
|
||||
xproto.EventMaskButtonPress,
|
||||
xproto.EventMaskButtonRelease)
|
||||
if err != nil { return }
|
||||
|
||||
window.xWindow.WMGracefulClose (func (xWindow *xwindow.Window) {
|
||||
window.Close()
|
||||
})
|
||||
|
||||
xevent.ConfigureNotifyFun(window.handleConfigureNotify).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
xevent.KeyPressFun(window.handleKeyPress).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
xevent.KeyReleaseFun(window.handleKeyRelease).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
xevent.ButtonPressFun(window.handleButtonPress).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
xevent.ButtonReleaseFun(window.handleButtonRelease).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
xevent.MotionNotifyFun(window.handleMotionNotify).
|
||||
Connect(backend.connection, window.xWindow.Id)
|
||||
|
||||
window.metrics.width = width
|
||||
window.metrics.height = height
|
||||
window.childMinimumSizeChangeCallback(8, 8)
|
||||
|
||||
window.reallocateCanvas()
|
||||
|
||||
backend.windows[window.xWindow.Id] = window
|
||||
output = window
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) ColorModel () (model color.Model) {
|
||||
return color.RGBAModel
|
||||
}
|
||||
|
||||
func (window *Window) At (x, y int) (pixel color.Color) {
|
||||
pixel = window.xCanvas.At(x, y)
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) RGBAAt (x, y int) (pixel color.RGBA) {
|
||||
sourcePixel := window.xCanvas.At(x, y).(xgraphics.BGRA)
|
||||
pixel = color.RGBA {
|
||||
R: sourcePixel.R,
|
||||
G: sourcePixel.G,
|
||||
B: sourcePixel.B,
|
||||
A: sourcePixel.A,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) Bounds () (bounds image.Rectangle) {
|
||||
bounds.Max = image.Point {
|
||||
X: window.metrics.width,
|
||||
Y: window.metrics.height,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) Handle (event tomo.Event) () {
|
||||
switch event.(type) {
|
||||
case tomo.EventResize:
|
||||
resizeEvent := event.(tomo.EventResize)
|
||||
// we will receive a resize event from X later which will be
|
||||
// handled by our event handler callbacks.
|
||||
if resizeEvent.Width < window.MinimumWidth() {
|
||||
resizeEvent.Width = window.MinimumWidth()
|
||||
}
|
||||
if resizeEvent.Height < window.MinimumHeight() {
|
||||
resizeEvent.Height = window.MinimumHeight()
|
||||
}
|
||||
window.xWindow.Resize(resizeEvent.Width, resizeEvent.Height)
|
||||
default:
|
||||
if window.child != nil { window.child.Handle(event) }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) SetDrawCallback (draw func (region tomo.Image)) {
|
||||
window.drawCallback = draw
|
||||
}
|
||||
|
||||
func (window *Window) SetMinimumSizeChangeCallback (
|
||||
notify func (width, height int),
|
||||
) {
|
||||
window.minimumSizeChangeCallback = notify
|
||||
}
|
||||
|
||||
func (window *Window) Selectable () (selectable bool) {
|
||||
if window.child != nil { selectable = window.child.Selectable() }
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) MinimumWidth () (minimum int) {
|
||||
if window.child != nil { minimum = window.child.MinimumWidth() }
|
||||
minimum = 8
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) MinimumHeight () (minimum int) {
|
||||
if window.child != nil { minimum = window.child.MinimumHeight() }
|
||||
minimum = 8
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) Adopt (child tomo.Element) {
|
||||
if window.child != nil {
|
||||
window.child.SetDrawCallback(nil)
|
||||
window.child.SetMinimumSizeChangeCallback(nil)
|
||||
}
|
||||
window.child = child
|
||||
if child != nil {
|
||||
child.SetDrawCallback(window.childDrawCallback)
|
||||
child.SetMinimumSizeChangeCallback (
|
||||
window.childMinimumSizeChangeCallback)
|
||||
window.resizeChildToFit()
|
||||
}
|
||||
window.childMinimumSizeChangeCallback (
|
||||
child.MinimumWidth(),
|
||||
child.MinimumHeight())
|
||||
}
|
||||
|
||||
func (window *Window) Child () (child tomo.Element) {
|
||||
child = window.child
|
||||
return
|
||||
}
|
||||
|
||||
func (window *Window) SetTitle (title string) {
|
||||
ewmh.WmNameSet (
|
||||
window.backend.connection,
|
||||
window.xWindow.Id,
|
||||
title)
|
||||
}
|
||||
|
||||
func (window *Window) SetIcon (sizes []image.Image) {
|
||||
wmIcons := []ewmh.WmIcon { }
|
||||
|
||||
for _, icon := range sizes {
|
||||
width := icon.Bounds().Max.X
|
||||
height := icon.Bounds().Max.Y
|
||||
wmIcon := ewmh.WmIcon {
|
||||
Width: uint(width),
|
||||
Height: uint(height),
|
||||
Data: make ([]uint, width * height),
|
||||
}
|
||||
|
||||
// manually convert image data beacuse of course we have to do
|
||||
// this
|
||||
index := 0
|
||||
for y := 0; y < height; y ++ {
|
||||
for x := 0; x < width; x ++ {
|
||||
r, g, b, a := icon.At(x, y).RGBA()
|
||||
r >>= 8
|
||||
g >>= 8
|
||||
b >>= 8
|
||||
a >>= 8
|
||||
wmIcon.Data[index] =
|
||||
(uint(a) << 24) |
|
||||
(uint(r) << 16) |
|
||||
(uint(g) << 8) |
|
||||
(uint(b) << 0)
|
||||
index ++
|
||||
}}
|
||||
|
||||
wmIcons = append(wmIcons, wmIcon)
|
||||
}
|
||||
|
||||
ewmh.WmIconSet (
|
||||
window.backend.connection,
|
||||
window.xWindow.Id,
|
||||
wmIcons)
|
||||
}
|
||||
|
||||
func (window *Window) Show () {
|
||||
if window.child == nil {
|
||||
window.xCanvas.For (func (x, y int) xgraphics.BGRA {
|
||||
return xgraphics.BGRA { }
|
||||
})
|
||||
|
||||
window.pushRegion(window.xCanvas.Bounds())
|
||||
}
|
||||
|
||||
window.xWindow.Map()
|
||||
}
|
||||
|
||||
func (window *Window) Hide () {
|
||||
window.xWindow.Unmap()
|
||||
}
|
||||
|
||||
func (window *Window) Close () {
|
||||
delete(window.backend.windows, window.xWindow.Id)
|
||||
if window.onClose != nil { window.onClose() }
|
||||
xevent.Detach(window.xWindow.X, window.xWindow.Id)
|
||||
window.xWindow.Destroy()
|
||||
}
|
||||
|
||||
func (window *Window) OnClose (callback func ()) {
|
||||
window.onClose = callback
|
||||
}
|
||||
|
||||
func (window *Window) reallocateCanvas () {
|
||||
if window.xCanvas != nil {
|
||||
window.xCanvas.Destroy()
|
||||
}
|
||||
window.xCanvas = xgraphics.New (
|
||||
window.backend.connection,
|
||||
image.Rect (
|
||||
0, 0,
|
||||
window.metrics.width,
|
||||
window.metrics.height))
|
||||
|
||||
window.xCanvas.XSurfaceSet(window.xWindow.Id)
|
||||
}
|
||||
|
||||
func (window *Window) redrawChildEntirely () {
|
||||
window.xCanvas.For (func (x, y int) (c xgraphics.BGRA) {
|
||||
rgba := window.child.RGBAAt(x, y)
|
||||
c.R, c.G, c.B, c.A = rgba.R, rgba.G, rgba.B, rgba.A
|
||||
return
|
||||
})
|
||||
|
||||
window.pushRegion(window.xCanvas.Bounds())
|
||||
}
|
||||
|
||||
func (window *Window) resizeChildToFit () {
|
||||
window.skipChildDrawCallback = true
|
||||
window.child.Handle(tomo.EventResize {
|
||||
Width: window.metrics.width,
|
||||
Height: window.metrics.height,
|
||||
})
|
||||
window.skipChildDrawCallback = false
|
||||
window.redrawChildEntirely()
|
||||
}
|
||||
|
||||
func (window *Window) childDrawCallback (region tomo.Image) {
|
||||
if window.skipChildDrawCallback { return }
|
||||
|
||||
bounds := region.Bounds()
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
||||
rgba := region.RGBAAt(x, y)
|
||||
window.xCanvas.SetBGRA (x, y, xgraphics.BGRA {
|
||||
R: rgba.R,
|
||||
G: rgba.G,
|
||||
B: rgba.B,
|
||||
A: rgba.A,
|
||||
})
|
||||
}}
|
||||
|
||||
window.pushRegion(region.Bounds())
|
||||
}
|
||||
|
||||
func (window *Window) childMinimumSizeChangeCallback (width, height int) {
|
||||
icccm.WmNormalHintsSet (
|
||||
window.backend.connection,
|
||||
window.xWindow.Id,
|
||||
&icccm.NormalHints {
|
||||
Flags: icccm.SizeHintPMinSize,
|
||||
MinWidth: uint(width),
|
||||
MinHeight: uint(height),
|
||||
})
|
||||
newWidth := window.metrics.width
|
||||
newHeight := window.metrics.height
|
||||
if newWidth < width { newWidth = width }
|
||||
if newHeight < height { newHeight = height }
|
||||
if newWidth != window.metrics.width ||
|
||||
newHeight != window.metrics.height {
|
||||
window.xWindow.Resize(newWidth, newHeight)
|
||||
}
|
||||
}
|
||||
|
||||
func (window *Window) pushRegion (region image.Rectangle) {
|
||||
if window.xCanvas == nil { panic("whoopsie!!!!!!!!!!!!!!") }
|
||||
image, ok := window.xCanvas.SubImage(region).(*xgraphics.Image)
|
||||
if ok {
|
||||
image.XDraw()
|
||||
window.xCanvas.XPaint(window.xWindow.Id)
|
||||
}
|
||||
}
|
||||
85
backends/x/x.go
Normal file
85
backends/x/x.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package x
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
|
||||
import "github.com/jezek/xgbutil"
|
||||
import "github.com/jezek/xgb/xproto"
|
||||
import "github.com/jezek/xgbutil/xevent"
|
||||
|
||||
// Backend is an instance of an X backend.
|
||||
type Backend struct {
|
||||
connection *xgbutil.XUtil
|
||||
|
||||
doChannel chan(func ())
|
||||
|
||||
modifierMasks struct {
|
||||
capsLock uint16
|
||||
shiftLock uint16
|
||||
numLock uint16
|
||||
modeSwitch uint16
|
||||
|
||||
alt uint16
|
||||
meta uint16
|
||||
super uint16
|
||||
hyper uint16
|
||||
}
|
||||
|
||||
windows map[xproto.Window] *Window
|
||||
}
|
||||
|
||||
// NewBackend instantiates an X backend.
|
||||
func NewBackend () (output tomo.Backend, err error) {
|
||||
backend := &Backend {
|
||||
windows: map[xproto.Window] *Window { },
|
||||
}
|
||||
|
||||
// connect to X
|
||||
backend.connection, err = xgbutil.NewConn()
|
||||
if err != nil { return }
|
||||
backend.initializeKeymapInformation()
|
||||
|
||||
output = backend
|
||||
return
|
||||
}
|
||||
|
||||
// Run runs the backend's event loop. This method will not exit until Stop() is
|
||||
// called, or the backend experiences a fatal error.
|
||||
func (backend *Backend) Run () (err error) {
|
||||
backend.assert()
|
||||
pingBefore,
|
||||
pingAfter,
|
||||
pingQuit := xevent.MainPing(backend.connection)
|
||||
for {
|
||||
select {
|
||||
case <- pingBefore: <- pingAfter
|
||||
case callback := <- backend.doChannel:
|
||||
callback()
|
||||
case <- pingQuit: return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop gracefully closes the connection and stops the event loop.
|
||||
func (backend *Backend) Stop () {
|
||||
backend.assert()
|
||||
for _, window := range backend.windows {
|
||||
window.Close()
|
||||
}
|
||||
xevent.Quit(backend.connection)
|
||||
backend.connection.Conn().Close()
|
||||
}
|
||||
|
||||
// Do executes the specified callback within the main thread as soon as
|
||||
// possible. This function can be safely called from other threads.
|
||||
func (backend *Backend) Do (callback func ()) {
|
||||
backend.assert()
|
||||
backend.doChannel <- callback
|
||||
}
|
||||
|
||||
func (backend *Backend) assert () {
|
||||
if backend == nil { panic("nil backend") }
|
||||
}
|
||||
|
||||
func init () {
|
||||
tomo.RegisterBackend(NewBackend)
|
||||
}
|
||||
Reference in New Issue
Block a user