Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanroom re-implementations for deleted code #360

Merged
44 changes: 27 additions & 17 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

Expand All @@ -14,18 +16,20 @@ import (
)

type CLIOpts struct {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Add the required variable for CLI parsing here
verbose bool
setcap bool
sinkName string
loadInput bool
loadOutput bool
unload bool
threshold int
list bool
checkUpdate bool
}

func parseCLIOpts() CLIOpts {
var opt CLIOpts
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Add a parameter for logs
flag.BoolVar(&opt.verbose, "v", false, "Verbose output (print logs to stderr)")
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
flag.BoolVar(&opt.setcap, "setcap", false, "for internal use only")
flag.StringVar(&opt.sinkName, "s", "", "Use the specified source/sink device ID")
flag.BoolVar(&opt.loadInput, "i", false, "Load supressor for input. If no source device ID is specified the default pulse audio source is used.")
Expand All @@ -40,6 +44,12 @@ func parseCLIOpts() CLIOpts {
}

func doCLI(opt CLIOpts, config *config, librnnoise string) {
if opt.verbose {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}

kdkasad marked this conversation as resolved.
Show resolved Hide resolved
if opt.checkUpdate {
latestRelease, err := getLatestRelease()
if err == nil {
Expand All @@ -56,11 +66,13 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) {
cleanupExit(librnnoise, 0)
}

// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Check for setcap parameter and handle it.
// The function makeBinarySetcapped will be called.
if opt.setcap {
exitCode := 0
if err := makeBinarySetcapped(); err != nil {
exitCode = 1
}
cleanupExit(librnnoise, exitCode)
}
kdkasad marked this conversation as resolved.
Show resolved Hide resolved

paClient, err := pulseaudio.NewClient()
if err != nil {
Expand Down Expand Up @@ -172,8 +184,6 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) {
}

func cleanupExit(librnnoise string, exitCode int) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Unloads the specified library then exit with the specified exit code
removeLib(librnnoise)
os.Exit(exitCode)
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func main() {

opt := parseCLIOpts()

// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Check for the log parameter and decide what
// will the std output be
log.Printf("Application starting. Version: %s (%s)\n", version, distribution)
log.Printf("CAP_SYS_RESOURCE: %t\n", hasCapSysResource(getCurrentCaps()))

Expand All @@ -70,6 +65,7 @@ func main() {
defer removeLib(rnnoisefile)

ctx := ntcontext{}
ctx.appName = appName
ctx.config = readConfig()
ctx.librnnoise = rnnoisefile

Expand Down
13 changes: 6 additions & 7 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,12 @@ func loadSupressor(ctx *ntcontext, inp *device, out *device) error {
return nil
}

func loadModule(ctx *ntcontext, module, args string) (uint32, error) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Loads a module using pcClient.
// Then checks if it loaded correctly.
// Will display an error if any.
func loadModule(ctx *ntcontext, module string, args string) (uint32, error) {
index, err := ctx.paClient.LoadModule(module, args)
if err != nil {
ctx.views.Push(makeErrorView(ctx, err.Error()))
}
return index, err
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}

func loadPipeWireInput(ctx *ntcontext, inp *device) error {
Expand Down
96 changes: 63 additions & 33 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ntcontext struct {
views *ViewStack
serverInfo audioserverinfo
virtualDeviceInUse bool
appName string
}

//TODO pull some of these strucs out of UI, they don't belong here
Expand All @@ -58,11 +59,12 @@ var lightBlue = color.RGBA{173, 216, 230, 255}

const notice = "NoiseTorch Next Gen (stylized NoiseTorch-ng) is a continuation of the NoiseTorch\nproject after it was abandoned by its original author. Please do not confuse\nboth programs. You may convey modified versions of this program under its name."

// updatefn() is run whenever the window needs to be re-drawn.
// This is usually only after user input.
func updatefn(ctx *ntcontext, w *nucular.Window) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Must display the view that's on top of the view stack
// Call view at the top of the stack
ctx.views.Peek()(ctx, w)
}
AXDOOMER marked this conversation as resolved.
Show resolved Hide resolved

func mainView(ctx *ntcontext, w *nucular.Window) {

Expand Down Expand Up @@ -436,50 +438,78 @@ func capabilitiesView(ctx *ntcontext, w *nucular.Window) {

func makeErrorView(ctx *ntcontext, errorMsg string) ViewFunc {
return func(ctx *ntcontext, w *nucular.Window) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Indicates that there is an error and display "errorMsg"
// The user is allowed to continue using the program
w.Row(15).Dynamic(1)
w.LabelColored("Error", "CB", color.RGBA{255, 0, 0, 255});
w.Row(15).Dynamic(1)
// this should probably use LabelWrap to avoid cutting off long
// messages, but when I tried, it made the text not appear at all
w.Label(errorMsg, "CB");

// whitespace
w.Row(40).Dynamic(1)

w.Row(25).Dynamic(1)
if w.ButtonText("OK") {
ctx.views.Pop()
}
}
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}

func makeFatalErrorView(ctx *ntcontext, errorMsg string) ViewFunc {
return func(ctx *ntcontext, w *nucular.Window) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Indicates that there is a fatal error and display "errorMsg"
// The user is not allowed to continue using the program
// The program will exit
w.Row(15).Dynamic(1)
w.LabelColored("Fatal Error", "CB", color.RGBA{255, 0, 0, 255});
w.Row(15).Dynamic(1)
// this should probably use LabelWrap to avoid cutting off long
// messages, but when I tried, it made the text not appear at all
w.Label(errorMsg, "CB");

// whitespace
w.Row(40).Dynamic(1)

w.Row(25).Dynamic(1)
if w.ButtonText("Exit") {
ctx.views.Pop()
os.Exit(1)
}
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}
}

func makeConfirmView(ctx *ntcontext, title, text, ?, ? string, ?, ? func()) ViewFunc {
return func(ctx *ntcontext, ? *nucular.Window) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Confirmation dialog
// You should deduce some of the variable names its implementation
// from looking at its usage in the code
func makeConfirmView(ctx *ntcontext, title, text, proceedText, cancelText string, proceedFunc, cancelFunc func()) ViewFunc {
return func(ctx *ntcontext, w *nucular.Window) {
w.Row(15).Dynamic(1)
w.LabelColored(title, "CB", color.RGBA{255, 255, 0, 255});
w.Row(15).Dynamic(1)
// this should probably use LabelWrap to avoid cutting off long
// messages, but when I tried, it made the text not appear at all
w.Label(text, "CB");

// whitespace
w.Row(40).Dynamic(1)

w.Row(25).Dynamic(2)
if w.ButtonText(cancelText) {
ctx.views.Pop()
cancelFunc()
}
if w.ButtonText(proceedText) {
ctx.views.Pop()
proceedFunc()
}
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}
}

func resetUI(ctx *ntcontext) {
// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Create a new viewstack and push the main view
ctx.views = NewViewStack()
ctx.views.Push(mainView)

if !ctx.haveCapabilities {
ctx.views.Push(capabilitiesView)
}


// TODO: CODE REMOVED
// MUST BE WRITTEN FROM SCRATCH WITHOUT LOOKING AT THE ORIGINAL CODE
// Description:
// Check the server info for an outdated pipewire
// Then display and error message if it's too old (0.3.28 required)
if ctx.serverInfo.outdatedPipeWire {
ctx.views.Push(makeFatalErrorView(ctx,
fmt.Sprintf("Detected outdated version of PipeWire. %s requires PipeWire v0.3.28 or newer.",
ctx.appName)))
}
kdkasad marked this conversation as resolved.
Show resolved Hide resolved
}