Removed ncurses

live
noah metz 2024-09-12 14:55:12 -06:00
parent 6106d10b06
commit e2c6cedb52
3 changed files with 12 additions and 329 deletions

@ -68,7 +68,6 @@ func NewChannel(modes map[ModeID]Mode) (*Channel, error) {
func(channel *Channel) update_state() {
for true {
select {
case <-channel.RemovedRoles:
case session := <-channel.ClosedSessions:
members := channel.Members.Load().([]*ServerSession)
idx := slices.Index(members, session)

@ -13,7 +13,6 @@ import (
"syscall"
"time"
"git.metznet.ca/MetzNet/go-ncurses"
"git.metznet.ca/MetzNet/pnyx"
"github.com/gen2brain/malgo"
"github.com/google/uuid"
@ -271,57 +270,6 @@ func get_private_key(path string, generate bool) ed25519.PrivateKey {
}
}
func print_decoration(window, user, channels, body ncurses.Window) {
max_y := ncurses.GetMaxY(*ncurses.CurScr)
max_x := ncurses.GetMaxX(*ncurses.CurScr)
ncurses.WResize(window, max_y, max_x)
ncurses.WResize(user, 1, max_x/4-2)
ncurses.MvWin(user, max_y-2, 1)
ncurses.WResize(channels, max_y-4, (max_x/4)-1)
ncurses.MvWin(channels, 1, 1)
ncurses.WResize(body, max_y-2, max_x-(max_x/4)-2)
ncurses.MvWin(body, 1, (max_x/4)+1)
for i := 1; i < max_x-1; i++ {
ncurses.MvWAddStr(window, 0, i, "═")
}
for i := 1; i < max_x/4; i++ {
ncurses.MvWAddStr(window, max_y - 3, i, "═")
}
for i := 1; i < max_y-1; i++ {
ncurses.MvWAddStr(window, i, 0, "║")
}
for i := 1; i < max_y-1; i++ {
ncurses.MvWAddStr(window, i, max_x-1, "║")
}
for i := 1; i < max_y-1; i++ {
ncurses.MvWAddStr(window, i, max_x/4, "║")
}
for i := 1; i < max_x-1; i++ {
ncurses.MvWAddStr(window, max_y-1, i, "═")
}
ncurses.MvWAddStr(window, 0, 0, "╔")
ncurses.MvWAddStr(window, 0, max_x-1, "╗")
ncurses.MvWAddStr(window, 0, max_x/4, "╦")
ncurses.MvWAddStr(window, max_y - 3, 0, "╠")
ncurses.MvWAddStr(window, max_y - 3, max_x/4, "╣")
ncurses.MvWAddStr(window, max_y - 1, 0, "╚")
ncurses.MvWAddStr(window, max_y - 1, max_x/4, "╩")
ncurses.MvWAddStr(window, max_y - 1, max_x-1, "╝")
ncurses.WRefresh(window)
ncurses.WRefresh(user)
ncurses.WRefresh(channels)
ncurses.WRefresh(body)
}
type ChannelState struct {
ID pnyx.ChannelID
Name string
@ -343,253 +291,9 @@ type UIState struct {
Peers map[pnyx.PeerID]PeerState
}
const (
AREA_CHANNELS = 0
AREA_BODY = 1
AREA_USER = 2
BODY_CHANNEL = 0
BODY_DETAIL = 1
BODY_QUIT = 2
BODY_USER = 3
)
func handle_back(state *UIState) {
if state.SelectedArea == AREA_BODY {
if state.SelectedBody == BODY_USER {
state.SelectedArea = AREA_USER
} else {
state.SelectedArea = AREA_CHANNELS
}
state.SelectedBody = BODY_CHANNEL
}
}
func handle_up(state *UIState) {
if state.SelectedArea == AREA_CHANNELS {
if state.SelectedPeer > 0 {
state.SelectedPeer -= 1
} else if state.SelectedChannel > 0 {
state.SelectedChannel -= 1
state.SelectedPeer = len(state.Channels[state.SelectedChannel].Members)
}
} else if state.SelectedArea == AREA_USER {
state.SelectedArea = AREA_CHANNELS
state.SelectedChannel = len(state.Channels) - 1
state.SelectedPeer = len(state.Channels[state.SelectedChannel].Members)
}
}
func handle_down(state *UIState) {
if state.SelectedArea == AREA_CHANNELS {
if state.SelectedPeer < len(state.Channels[state.SelectedChannel].Members) {
state.SelectedPeer += 1
} else if state.SelectedChannel < len(state.Channels) - 1 {
state.SelectedChannel += 1
state.SelectedPeer = 0
} else {
state.SelectedArea = AREA_USER
}
}
}
func handle_select(state *UIState) {
if state.SelectedArea == AREA_CHANNELS {
state.SelectedArea = AREA_BODY
state.SelectedBody = BODY_CHANNEL
} else if state.SelectedArea == AREA_USER {
state.SelectedArea = AREA_BODY
state.SelectedBody = BODY_USER
}
}
func handle_edit(state *UIState) {
if state.SelectedArea == AREA_CHANNELS {
state.SelectedArea = AREA_BODY
state.SelectedBody = BODY_DETAIL
}
}
func handle_quit(state *UIState) {
state.SelectedArea = AREA_BODY
state.SelectedBody = BODY_QUIT
}
func handle_input(state *UIState, char []byte) {
switch char[0] {
case 'q':
handle_quit(state)
case 'e':
handle_edit(state)
case '\r':
handle_select(state)
case 0x1B:
if len(char) == 1 {
handle_back(state)
} else {
switch char[1] {
case '[':
if len(char) == 2 {
} else {
switch char[2] {
case 'A': // Up
handle_up(state)
case 'B': // Down
handle_down(state)
case 'C': // Right
handle_select(state)
case 'D': // Left
handle_back(state)
}
}
}
}
}
}
func draw_user(state *UIState, user ncurses.Window) {
ncurses.WErase(user)
if state.SelectedArea == AREA_USER || state.SelectedBody == BODY_USER {
ncurses.WAttrOn(user, ncurses.COLOR_PAIR(1))
}
ncurses.MvWAddStr(user, 0, 0, state.Username)
if state.SelectedArea == AREA_USER || state.SelectedBody == BODY_USER {
ncurses.WAttrOff(user, ncurses.COLOR_PAIR(1))
}
ncurses.WRefresh(user)
}
func draw_channels(state *UIState, channels ncurses.Window) {
channel_highlight_color := 2
if state.SelectedArea == AREA_CHANNELS {
channel_highlight_color = 1
}
ncurses.WErase(channels)
ncurses.WMove(channels, 0, 0)
for i, channel_state := range(state.Channels) {
if i == state.SelectedChannel && state.SelectedPeer == 0 {
ncurses.WAttrOn(channels, ncurses.COLOR_PAIR(channel_highlight_color))
}
ncurses.WAddStr(channels, channel_state.Name)
ncurses.WAddCh(channels, '\n')
for j, peer_id := range(channel_state.Members) {
ncurses.WAddStr(channels, " ↳")
peer_state, found := state.Peers[peer_id]
if state.SelectedChannel == i && state.SelectedPeer == (j+1) {
ncurses.WAttrOn(channels, ncurses.COLOR_PAIR(channel_highlight_color))
}
if found {
ncurses.WAddStr(channels, peer_state.Name)
} else {
ncurses.WAddStr(channels, fmt.Sprintf("%s", peer_id))
}
if state.SelectedChannel == i && state.SelectedPeer == (j+1) {
ncurses.WAttrOff(channels, ncurses.COLOR_PAIR(channel_highlight_color))
}
ncurses.WAddCh(channels, '\n')
}
if i == state.SelectedChannel && state.SelectedPeer == 0 {
ncurses.WAttrOff(channels, ncurses.COLOR_PAIR(channel_highlight_color))
}
}
ncurses.WRefresh(channels)
}
func draw_body_channel(state *UIState, body ncurses.Window) {
ncurses.WErase(body)
ncurses.MvWAddStr(body, 0, 0, "channel")
ncurses.WRefresh(body)
}
func draw_body_detail(state *UIState, body ncurses.Window) {
ncurses.WErase(body)
ncurses.MvWAddStr(body, 0, 0, "detail")
ncurses.WRefresh(body)
}
func draw_body_quit(state *UIState, body ncurses.Window) {
ncurses.WErase(body)
ncurses.MvWAddStr(body, 0, 0, "quit")
ncurses.WRefresh(body)
}
func draw_body_user(state *UIState, body ncurses.Window) {
ncurses.WErase(body)
ncurses.MvWAddStr(body, 0, 0, "user")
ncurses.WRefresh(body)
}
func draw_gui(state *UIState, channels, user, body ncurses.Window) {
draw_channels(state, channels)
draw_user(state, user)
switch state.SelectedBody {
case BODY_CHANNEL:
draw_body_channel(state, body)
case BODY_DETAIL:
draw_body_detail(state, body)
case BODY_QUIT:
draw_body_quit(state, body)
case BODY_USER:
draw_body_user(state, body)
}
}
func main_loop(client *pnyx.Client, audio_data chan []int16, window ncurses.Window, packet_chan chan pnyx.Payload, user_chan chan []byte, sigwinch_channel chan os.Signal) {
channels := ncurses.NewWin(0, 0, 0, 0)
body := ncurses.NewWin(0, 0, 0, 0)
user := ncurses.NewWin(0, 0, 0, 0)
state := &UIState{
Username: "fake user",
Peers: map[pnyx.PeerID]PeerState{
[32]byte{}: {
Name: "Test User 0",
},
[32]byte{0x01}: {
Name: "Test User 1",
},
[32]byte{0x02}: {
Name: "Test User 2",
},
},
Channels: []ChannelState{
{
ID: 0,
Name: "Channel 0",
Members: []pnyx.PeerID{[32]byte{}, [32]byte{0x01}},
},
{
ID: 1,
Name: "Channel 1",
Members: []pnyx.PeerID{[32]byte{0x02}},
},
},
}
ncurses.InitColor(1, 1000, 1000, 1000)
ncurses.InitColor(2, 300, 300, 300)
ncurses.InitColor(3, 150, 150, 150)
ncurses.InitColor(4, 150, 150, 300)
ncurses.InitPair(1, 1, 2)
ncurses.InitPair(2, 1, 3)
ncurses.InitPair(3, 1, 4)
ncurses.ScrollOk(body, true)
print_decoration(window, user, channels, body)
draw_gui(state, channels, user, body)
func main_loop(client *pnyx.Client, audio_data chan []int16, packet_chan chan pnyx.Payload) {
for client.Active() {
select {
case <-sigwinch_channel:
ncurses.EndWin()
ncurses.WRefresh(window)
ncurses.WClear(window)
print_decoration(window, user, channels, body)
draw_gui(state, channels, user, body)
case packet := <-packet_chan:
switch packet := packet.(type) {
case pnyx.ChannelCommandPacket:
@ -630,9 +334,6 @@ func main_loop(client *pnyx.Client, audio_data chan []int16, window ncurses.Wind
}
}
}
case char := <-user_chan:
handle_input(state, char)
draw_gui(state, channels, user, body)
}
}
}
@ -667,7 +368,6 @@ func main() {
defer inDevice.Uninit()
defer inDevice.Stop()
user_chan := make(chan []byte, 1024)
packet_chan := make(chan pnyx.Payload, 1024)
key := get_private_key(os.ExpandEnv(*key_file_arg), *generate_key_arg)
@ -696,27 +396,11 @@ func main() {
}
go process_mic(client, mic)
ncurses.SetLocale(0, "")
user_chan, stdin_active := ncurses.UTF8Listener(100, os.Stdin)
window := ncurses.InitScr()
ncurses.CursSet(0)
ret := ncurses.StartColor()
if ret != 0 {
panic(ret)
}
sigwinch_channel := make(chan os.Signal, 1)
signal.Notify(sigwinch_channel, syscall.SIGWINCH)
go main_loop(client, audio_data, window, packet_chan, user_chan, sigwinch_channel)
go main_loop(client, audio_data, packet_chan)
os_sigs := make(chan os.Signal, 1)
signal.Notify(os_sigs, syscall.SIGINT, syscall.SIGABRT)
<-os_sigs
stdin_active.Store(false)
client.Close()
ncurses.EndWin()
}

@ -36,16 +36,11 @@ func(session *ServerSession) Send(payload Payload) {
}
}
type SessionPayload struct {
Session *ServerSession
Payload Payload
}
type Server struct {
key ed25519.PrivateKey
active atomic.Bool
connection *net.UDPConn
commands chan SessionPayload
commands chan SessionServerCommand
threads sync.WaitGroup
@ -85,7 +80,7 @@ func NewServer(listen string, key ed25519.PrivateKey, channels map[ChannelID]*Ch
key: key,
connection: connection,
active: atomic.Bool{},
commands: make(chan SessionPayload, SERVER_COMMAND_BUFFER_SIZE),
commands: make(chan SessionServerCommand, SERVER_COMMAND_BUFFER_SIZE),
sessions: map[SessionID]*ServerSession{},
channels: atomic.Value{},
@ -166,6 +161,11 @@ type SessionChannelCommand struct {
Packet *ChannelCommandPacket
}
type SessionServerCommand struct {
Session *ServerSession
Packet *CommandPacket
}
func handle_session_incoming(session *ServerSession, server *Server) {
server.Log("Starting session incoming goroutine %s", session.ID)
ping_timer := time.After(SESSION_PING_TIME)
@ -203,9 +203,9 @@ func handle_session_incoming(session *ServerSession, server *Server) {
switch packet := packet.(type) {
case CommandPacket:
server.commands<-SessionPayload{
server.commands<-SessionServerCommand{
Session: session,
Payload: packet,
Packet: &packet,
}
case ChannelCommandPacket:
channels := server.channels.Load().(map[ChannelID]*Channel)
@ -388,7 +388,7 @@ func(server *Server) update_state() {
for server.active.Load() {
select {
case command := <-server.commands:
if command.Payload == nil {
if command.Session == nil {
break
}
server.Log("Incoming server command %+v", command)