util_funcs.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 DisplayFlip() {
  113. display.Flip()
  114. }
  115. func AsyncDisplayFlip() {
  116. display.ASync(func() {
  117. display.Flip()
  118. })
  119. }
  120. func ReadLines(path string) (lines []string, err error) {
  121. var (
  122. file *os.File
  123. part []byte
  124. prefix bool
  125. )
  126. if file, err = os.Open(path); err != nil {
  127. return
  128. }
  129. reader := bufio.NewReader(file)
  130. buffer := bytes.NewBuffer(make([]byte, 0))
  131. for {
  132. if part, prefix, err = reader.ReadLine(); err != nil {
  133. break
  134. }
  135. buffer.Write(part)
  136. if !prefix {
  137. lines = append(lines, buffer.String())
  138. buffer.Reset()
  139. }
  140. }
  141. if err == io.EOF {
  142. err = nil
  143. }
  144. return
  145. }
  146. func WriteLines(lines []string, path string) (err error) {
  147. var file *os.File
  148. if file, err = os.Create(path); err != nil {
  149. return
  150. }
  151. defer file.Close()
  152. for _, elem := range lines {
  153. _, err := file.WriteString(strings.TrimSpace(elem) + "\n")
  154. if err != nil {
  155. fmt.Println(err)
  156. break
  157. }
  158. }
  159. return
  160. }
  161. func GetGid(path string) int {
  162. s, err := os.Stat(path)
  163. if err != nil {
  164. return -1
  165. }
  166. sys_interface := s.(os.FileInfo).Sys()
  167. if sys_interface == nil {
  168. return -1
  169. }
  170. return int(sys_interface.(*syscall.Stat_t).Gid)
  171. }
  172. func GetUid(path string) int {
  173. s, err := os.Stat(path)
  174. if err != nil {
  175. return -1
  176. }
  177. sys_interface := s.(os.FileInfo).Sys()
  178. if sys_interface == nil {
  179. return -1
  180. }
  181. return int(sys_interface.(*syscall.Stat_t).Uid)
  182. }
  183. func CheckBattery() int {
  184. if FileExists(sysgo.Battery) == false {
  185. return -1
  186. }
  187. batinfos, err := ReadLines(sysgo.Battery)
  188. if err == nil {
  189. for _, v := range batinfos {
  190. if strings.HasPrefix(v, "POWER_SUPPLY_CAPACITY") {
  191. parts := strings.Split(v, "=")
  192. if len(parts) > 1 {
  193. cur_cap, err := strconv.Atoi(parts[1])
  194. if err == nil {
  195. return cur_cap
  196. } else {
  197. return 0
  198. }
  199. }
  200. }
  201. }
  202. } else {
  203. fmt.Println(err)
  204. }
  205. return 0
  206. }
  207. func System(cmd string) string {
  208. ret := ""
  209. out, err := exec.Command("bash", "-c", cmd).Output()
  210. if err != nil {
  211. if _, ok := err.(*exec.ExitError); ok {
  212. //exit code !=0 ,but it can be ignored
  213. } else {
  214. fmt.Println(err)
  215. }
  216. } else {
  217. ret = string(out)
  218. }
  219. return ret
  220. }
  221. func ArmSystem(cmd string) string {
  222. if strings.Contains(runtime.GOARCH, "arm") == true {
  223. return System(cmd)
  224. } else {
  225. return ""
  226. }
  227. }
  228. func SystemTrim(cmd string) string {
  229. ret := ""
  230. out, err := exec.Command("bash", "-c", cmd).Output()
  231. if err != nil {
  232. if _, ok := err.(*exec.ExitError); ok {
  233. //exit code !=0 ,but it can be ignored
  234. } else {
  235. fmt.Println(err)
  236. }
  237. } else {
  238. ret = string(out)
  239. }
  240. return strings.Trim(ret, "\r\n")
  241. }
  242. func cmdEnv() []string {
  243. return []string{"LANG=C", "LC_ALL=C"}
  244. }
  245. func ExecCmd(cmdArgs []string) ([]byte, error) {
  246. cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
  247. cmd.Env = append(os.Environ(), cmdEnv()...)
  248. out, err := cmd.Output()
  249. if err != nil {
  250. err = fmt.Errorf(`failed to execute "%v" (%+v)`, strings.Join(cmdArgs, " "), err)
  251. }
  252. return out, err
  253. }
  254. func CopyFile(src, dst string) (err error) {
  255. sfi, err := os.Stat(src)
  256. if err != nil {
  257. return
  258. }
  259. if !sfi.Mode().IsRegular() {
  260. // cannot copy non-regular files (e.g., directories,
  261. // symlinks, devices, etc.)
  262. return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
  263. }
  264. dfi, err := os.Stat(dst)
  265. if err != nil {
  266. if !os.IsNotExist(err) {
  267. return
  268. }
  269. } else {
  270. if !(dfi.Mode().IsRegular()) {
  271. return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
  272. }
  273. if os.SameFile(sfi, dfi) {
  274. return
  275. }
  276. }
  277. if err = os.Link(src, dst); err == nil {
  278. return
  279. }
  280. err = copyFileContents(src, dst)
  281. return
  282. }
  283. // copyFileContents copies the contents of the file named src to the file named
  284. // by dst. The file will be created if it does not already exist. If the
  285. // destination file exists, all it's contents will be replaced by the contents
  286. // of the source file.
  287. func copyFileContents(src, dst string) (err error) {
  288. in, err := os.Open(src)
  289. if err != nil {
  290. return
  291. }
  292. defer in.Close()
  293. out, err := os.Create(dst)
  294. if err != nil {
  295. return
  296. }
  297. defer func() {
  298. cerr := out.Close()
  299. if err == nil {
  300. err = cerr
  301. }
  302. }()
  303. if _, err = io.Copy(out, in); err != nil {
  304. return
  305. }
  306. err = out.Sync()
  307. return
  308. }