util_funcs.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package UI
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "syscall"
  15. "github.com/cuu/gogame/display"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo"
  17. )
  18. func ShowErr(e error) {
  19. if e != nil {
  20. fmt.Println(e)
  21. }
  22. }
  23. func Assert(e error) {
  24. if e != nil {
  25. log.Fatal("Assert: ", e)
  26. }
  27. }
  28. func CheckAndPanic(e error) {
  29. if e != nil {
  30. panic(e)
  31. }
  32. }
  33. func Abs(n int) int {
  34. y := n >> 63
  35. return (n ^ y) - y
  36. }
  37. func SkinMap(orig_file_or_dir string) string {
  38. DefaultSkin := "skin/default/"
  39. ret := ""
  40. if strings.HasPrefix(orig_file_or_dir, "/home/cpi/apps/Menu") {
  41. ret = strings.Replace(orig_file_or_dir, "/home/cpi/apps/Menu/", sysgo.SKIN+"/Menu/GameShell/", -1)
  42. if FileExists(ret) == false {
  43. ret = DefaultSkin + orig_file_or_dir
  44. }
  45. } else { // there is no need to add insert "sysgo" in the middle
  46. ret = sysgo.SKIN + orig_file_or_dir
  47. if FileExists(ret) == false {
  48. ret = DefaultSkin + orig_file_or_dir
  49. }
  50. }
  51. if FileExists(ret) {
  52. return ret
  53. } else { // if not existed both in default or custom skin ,return where it is
  54. return orig_file_or_dir
  55. }
  56. }
  57. func CmdClean(cmdpath string) string {
  58. spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
  59. for _, v := range spchars {
  60. cmdpath = strings.Replace(cmdpath, string(v), "\\"+string(v), -1)
  61. }
  62. return cmdpath
  63. }
  64. func FileExists(name string) bool {
  65. if _, err := os.Stat(name); err == nil {
  66. return true
  67. } else {
  68. return false
  69. }
  70. }
  71. func IsDirectory(path string) bool {
  72. fileInfo, err := os.Stat(path)
  73. if err != nil {
  74. return false
  75. } else {
  76. return fileInfo.IsDir()
  77. }
  78. }
  79. func IsAFile(path string) bool {
  80. fileInfo, err := os.Stat(path)
  81. if err != nil {
  82. return false
  83. } else {
  84. return fileInfo.Mode().IsRegular()
  85. }
  86. }
  87. func MakeExecutable(path string) {
  88. fileInfo, err := os.Stat(path)
  89. if err != nil {
  90. log.Printf("os.Stat %s failed\n", path)
  91. return
  92. }
  93. mode := fileInfo.Mode()
  94. mode |= (mode & 0444) >> 2
  95. os.Chmod(path, mode)
  96. }
  97. func GetExePath() string {
  98. dir, err := os.Getwd()
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. return dir
  103. }
  104. func ReplaceSuffix(orig_file_str string, new_ext string) string {
  105. orig_ext := filepath.Ext(orig_file_str)
  106. if orig_ext != "" {
  107. las_pos := strings.LastIndex(orig_file_str, ".")
  108. return orig_file_str[0:las_pos] + "." + new_ext
  109. }
  110. return orig_file_str // failed just return back where it came
  111. }
  112. func SwapAndShow() {
  113. display.Flip()
  114. }
  115. func ReadLines(path string) (lines []string, err error) {
  116. var (
  117. file *os.File
  118. part []byte
  119. prefix bool
  120. )
  121. if file, err = os.Open(path); err != nil {
  122. return
  123. }
  124. reader := bufio.NewReader(file)
  125. buffer := bytes.NewBuffer(make([]byte, 0))
  126. for {
  127. if part, prefix, err = reader.ReadLine(); err != nil {
  128. break
  129. }
  130. buffer.Write(part)
  131. if !prefix {
  132. lines = append(lines, buffer.String())
  133. buffer.Reset()
  134. }
  135. }
  136. if err == io.EOF {
  137. err = nil
  138. }
  139. return
  140. }
  141. func WriteLines(lines []string, path string) (err error) {
  142. var file *os.File
  143. if file, err = os.Create(path); err != nil {
  144. return
  145. }
  146. defer file.Close()
  147. for _, elem := range lines {
  148. _, err := file.WriteString(strings.TrimSpace(elem) + "\n")
  149. if err != nil {
  150. fmt.Println(err)
  151. break
  152. }
  153. }
  154. return
  155. }
  156. func GetGid(path string) int {
  157. s, err := os.Stat(path)
  158. if err != nil {
  159. return -1
  160. }
  161. sys_interface := s.(os.FileInfo).Sys()
  162. if sys_interface == nil {
  163. return -1
  164. }
  165. return int(sys_interface.(*syscall.Stat_t).Gid)
  166. }
  167. func GetUid(path string) int {
  168. s, err := os.Stat(path)
  169. if err != nil {
  170. return -1
  171. }
  172. sys_interface := s.(os.FileInfo).Sys()
  173. if sys_interface == nil {
  174. return -1
  175. }
  176. return int(sys_interface.(*syscall.Stat_t).Uid)
  177. }
  178. func CheckBattery() int {
  179. if FileExists(sysgo.Battery) == false {
  180. return -1
  181. }
  182. batinfos, err := ReadLines(sysgo.Battery)
  183. if err == nil {
  184. for _, v := range batinfos {
  185. if strings.HasPrefix(v, "POWER_SUPPLY_CAPACITY") {
  186. parts := strings.Split(v, "=")
  187. if len(parts) > 1 {
  188. cur_cap, err := strconv.Atoi(parts[1])
  189. if err == nil {
  190. return cur_cap
  191. } else {
  192. return 0
  193. }
  194. }
  195. }
  196. }
  197. } else {
  198. fmt.Println(err)
  199. }
  200. return 0
  201. }
  202. func System(cmd string) string {
  203. ret := ""
  204. out, err := exec.Command("bash", "-c", cmd).Output()
  205. if err != nil {
  206. if _, ok := err.(*exec.ExitError); ok {
  207. //exit code !=0 ,but it can be ignored
  208. } else {
  209. fmt.Println(err)
  210. }
  211. } else {
  212. ret = string(out)
  213. }
  214. return ret
  215. }
  216. func ArmSystem(cmd string) string {
  217. if strings.Contains(runtime.GOARCH, "arm") == true {
  218. return System(cmd)
  219. } else {
  220. return ""
  221. }
  222. }
  223. func SystemTrim(cmd string) string {
  224. ret := ""
  225. out, err := exec.Command("bash", "-c", cmd).Output()
  226. if err != nil {
  227. if _, ok := err.(*exec.ExitError); ok {
  228. //exit code !=0 ,but it can be ignored
  229. } else {
  230. fmt.Println(err)
  231. }
  232. } else {
  233. ret = string(out)
  234. }
  235. return strings.Trim(ret, "\r\n")
  236. }
  237. func cmdEnv() []string {
  238. return []string{"LANG=C", "LC_ALL=C"}
  239. }
  240. func ExecCmd(cmdArgs []string) ([]byte, error) {
  241. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  242. cmd.Env = append(os.Environ(), cmdEnv()...)
  243. out, err := cmd.Output()
  244. if err != nil {
  245. err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(cmdArgs, " "), err)
  246. }
  247. return out, err
  248. }
  249. func CopyFile(src, dst string) (err error) {
  250. sfi, err := os.Stat(src)
  251. if err != nil {
  252. return
  253. }
  254. if !sfi.Mode().IsRegular() {
  255. // cannot copy non-regular files (e.g., directories,
  256. // symlinks, devices, etc.)
  257. return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
  258. }
  259. dfi, err := os.Stat(dst)
  260. if err != nil {
  261. if !os.IsNotExist(err) {
  262. return
  263. }
  264. } else {
  265. if !(dfi.Mode().IsRegular()) {
  266. return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
  267. }
  268. if os.SameFile(sfi, dfi) {
  269. return
  270. }
  271. }
  272. if err = os.Link(src, dst); err == nil {
  273. return
  274. }
  275. err = copyFileContents(src, dst)
  276. return
  277. }
  278. // copyFileContents copies the contents of the file named src to the file named
  279. // by dst. The file will be created if it does not already exist. If the
  280. // destination file exists, all it's contents will be replaced by the contents
  281. // of the source file.
  282. func copyFileContents(src, dst string) (err error) {
  283. in, err := os.Open(src)
  284. if err != nil {
  285. return
  286. }
  287. defer in.Close()
  288. out, err := os.Create(dst)
  289. if err != nil {
  290. return
  291. }
  292. defer func() {
  293. cerr := out.Close()
  294. if err == nil {
  295. err = cerr
  296. }
  297. }()
  298. if _, err = io.Copy(out, in); err != nil {
  299. return
  300. }
  301. err = out.Sync()
  302. return
  303. }