123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- package UI
- import (
- "bufio"
- "bytes"
- "fmt"
- "io"
- "log"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- "syscall"
- "github.com/cuu/gogame/display"
- "github.com/clockworkpi/LauncherGoDev/sysgo"
- )
- func ShowErr(e error) {
- if e != nil {
- fmt.Println(e)
- }
- }
- func Assert(e error) {
- if e != nil {
- log.Fatal("Assert: ", e)
- }
- }
- func CheckAndPanic(e error) {
- if e != nil {
- panic(e)
- }
- }
- func Abs(n int) int {
- y := n >> 63
- return (n ^ y) - y
- }
- func SkinMap(orig_file_or_dir string) string {
- DefaultSkin := "skin/default/"
- ret := ""
- if strings.HasPrefix(orig_file_or_dir, "/home/cpi/apps/Menu") {
- ret = strings.Replace(orig_file_or_dir, "/home/cpi/apps/Menu/", sysgo.SKIN+"/Menu/GameShell/", -1)
- if FileExists(ret) == false {
- ret = DefaultSkin + orig_file_or_dir
- }
- } else { // there is no need to add insert "sysgo" in the middle
- ret = sysgo.SKIN + orig_file_or_dir
- if FileExists(ret) == false {
- ret = DefaultSkin + orig_file_or_dir
- }
- }
- if FileExists(ret) {
- return ret
- } else { // if not existed both in default or custom skin ,return where it is
- return orig_file_or_dir
- }
- }
- func CmdClean(cmdpath string) string {
- spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
- for _, v := range spchars {
- cmdpath = strings.Replace(cmdpath, string(v), "\\"+string(v), -1)
- }
- return cmdpath
- }
- func FileExists(name string) bool {
- if _, err := os.Stat(name); err == nil {
- return true
- } else {
- return false
- }
- }
- func IsDirectory(path string) bool {
- fileInfo, err := os.Stat(path)
- if err != nil {
- return false
- } else {
- return fileInfo.IsDir()
- }
- }
- func IsAFile(path string) bool {
- fileInfo, err := os.Stat(path)
- if err != nil {
- return false
- } else {
- return fileInfo.Mode().IsRegular()
- }
- }
- func MakeExecutable(path string) {
- fileInfo, err := os.Stat(path)
- if err != nil {
- log.Printf("os.Stat %s failed\n", path)
- return
- }
- mode := fileInfo.Mode()
- mode |= (mode & 0444) >> 2
- os.Chmod(path, mode)
- }
- func GetExePath() string {
- dir, err := os.Getwd()
- if err != nil {
- log.Fatal(err)
- }
- return dir
- }
- func ReplaceSuffix(orig_file_str string, new_ext string) string {
- orig_ext := filepath.Ext(orig_file_str)
- if orig_ext != "" {
- las_pos := strings.LastIndex(orig_file_str, ".")
- return orig_file_str[0:las_pos] + "." + new_ext
- }
- return orig_file_str // failed just return back where it came
- }
- func SwapAndShow() {
- display.Flip()
- }
- func ReadLines(path string) (lines []string, err error) {
- var (
- file *os.File
- part []byte
- prefix bool
- )
- if file, err = os.Open(path); err != nil {
- return
- }
- reader := bufio.NewReader(file)
- buffer := bytes.NewBuffer(make([]byte, 0))
- for {
- if part, prefix, err = reader.ReadLine(); err != nil {
- break
- }
- buffer.Write(part)
- if !prefix {
- lines = append(lines, buffer.String())
- buffer.Reset()
- }
- }
- if err == io.EOF {
- err = nil
- }
- return
- }
- func WriteLines(lines []string, path string) (err error) {
- var file *os.File
- if file, err = os.Create(path); err != nil {
- return
- }
- defer file.Close()
- for _, elem := range lines {
- _, err := file.WriteString(strings.TrimSpace(elem) + "\n")
- if err != nil {
- fmt.Println(err)
- break
- }
- }
- return
- }
- func GetGid(path string) int {
- s, err := os.Stat(path)
- if err != nil {
- return -1
- }
- sys_interface := s.(os.FileInfo).Sys()
- if sys_interface == nil {
- return -1
- }
- return int(sys_interface.(*syscall.Stat_t).Gid)
- }
- func GetUid(path string) int {
- s, err := os.Stat(path)
- if err != nil {
- return -1
- }
- sys_interface := s.(os.FileInfo).Sys()
- if sys_interface == nil {
- return -1
- }
- return int(sys_interface.(*syscall.Stat_t).Uid)
- }
- func CheckBattery() int {
- if FileExists(sysgo.Battery) == false {
- return -1
- }
- batinfos, err := ReadLines(sysgo.Battery)
- if err == nil {
- for _, v := range batinfos {
- if strings.HasPrefix(v, "POWER_SUPPLY_CAPACITY") {
- parts := strings.Split(v, "=")
- if len(parts) > 1 {
- cur_cap, err := strconv.Atoi(parts[1])
- if err == nil {
- return cur_cap
- } else {
- return 0
- }
- }
- }
- }
- } else {
- fmt.Println(err)
- }
- return 0
- }
- func System(cmd string) string {
- ret := ""
- out, err := exec.Command("bash", "-c", cmd).Output()
- if err != nil {
- if _, ok := err.(*exec.ExitError); ok {
- //exit code !=0 ,but it can be ignored
- } else {
- fmt.Println(err)
- }
- } else {
- ret = string(out)
- }
- return ret
- }
- func ArmSystem(cmd string) string {
- if strings.Contains(runtime.GOARCH, "arm") == true {
- return System(cmd)
- } else {
- return ""
- }
- }
- func SystemTrim(cmd string) string {
- ret := ""
- out, err := exec.Command("bash", "-c", cmd).Output()
- if err != nil {
- if _, ok := err.(*exec.ExitError); ok {
- //exit code !=0 ,but it can be ignored
- } else {
- fmt.Println(err)
- }
- } else {
- ret = string(out)
- }
- return strings.Trim(ret, "\r\n")
- }
- func cmdEnv() []string {
- return []string{"LANG=C", "LC_ALL=C"}
- }
- func ExecCmd(cmdArgs []string) ([]byte, error) {
- cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
- cmd.Env = append(os.Environ(), cmdEnv()...)
- out, err := cmd.Output()
- if err != nil {
- err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(cmdArgs, " "), err)
- }
- return out, err
- }
- func CopyFile(src, dst string) (err error) {
- sfi, err := os.Stat(src)
- if err != nil {
- return
- }
- if !sfi.Mode().IsRegular() {
- // cannot copy non-regular files (e.g., directories,
- // symlinks, devices, etc.)
- return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
- }
- dfi, err := os.Stat(dst)
- if err != nil {
- if !os.IsNotExist(err) {
- return
- }
- } else {
- if !(dfi.Mode().IsRegular()) {
- return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
- }
- if os.SameFile(sfi, dfi) {
- return
- }
- }
- if err = os.Link(src, dst); err == nil {
- return
- }
- err = copyFileContents(src, dst)
- return
- }
- // copyFileContents copies the contents of the file named src to the file named
- // by dst. The file will be created if it does not already exist. If the
- // destination file exists, all it's contents will be replaced by the contents
- // of the source file.
- func copyFileContents(src, dst string) (err error) {
- in, err := os.Open(src)
- if err != nil {
- return
- }
- defer in.Close()
- out, err := os.Create(dst)
- if err != nil {
- return
- }
- defer func() {
- cerr := out.Close()
- if err == nil {
- err = cerr
- }
- }()
- if _, err = io.Copy(out, in); err != nil {
- return
- }
- err = out.Sync()
- return
- }
|