__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import subprocess
  4. #UI lib
  5. from UI.constants import RUNSYS
  6. from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
  7. from UI.confirm_page import ConfirmPage
  8. from UI.lang_manager import MyLangManager
  9. from UI.skin_manager import MySkinManager
  10. import config
  11. class PowerOffConfirmPage(ConfirmPage):
  12. _ConfirmText = MyLangManager.Tr("Awaiting Input")
  13. #_FootMsg = ["Nav","Suspend","Reboot","Cancel","Shutdown"]
  14. _FootMsg = ["Nav","","Reboot","Cancel","Shutdown"]
  15. # uname -r
  16. st = subprocess.check_output(["uname","-r"])
  17. st = st.strip("\n")
  18. st = st.strip("\t")
  19. if "5.7" in st:
  20. _FootMsg[1] = "Sleep"
  21. def CheckBattery(self):
  22. try:
  23. f = open(config.Battery)
  24. except IOError:
  25. print( "PowerOFF open %s failed" % config.Battery)
  26. return 0
  27. else:
  28. with f:
  29. bat_uevent = {}
  30. content = f.readlines()
  31. content = [x.strip() for x in content]
  32. for i in content:
  33. pis = i.split("=")
  34. if len(pis) > 1:
  35. bat_uevent[pis[0]] = pis[1]
  36. if "POWER_SUPPLY_CAPACITY" in bat_uevent:
  37. cur_cap = int(bat_uevent["POWER_SUPPLY_CAPACITY"])
  38. else:
  39. cur_cap = 0
  40. return cur_cap
  41. return 0
  42. def KeyDown(self,event):
  43. if IsKeyMenuOrB(event.key):
  44. self.ReturnToUpLevelPage()
  45. self._Screen.Draw()
  46. self._Screen.SwapAndShow()
  47. if IsKeyStartOrA(event.key):
  48. if self.CheckBattery() < 20:
  49. cmdpath = "feh --bg-center %s;" % MySkinManager.GiveWallpaper("gameover.png")
  50. else:
  51. cmdpath = "feh --bg-center %s;" % MySkinManager.GiveWallpaper("seeyou.png")
  52. cmdpath += "sleep 3;"
  53. #cmdpath += "echo 'halt -p' > /tmp/halt_cmd"
  54. cmdpath += "sudo halt -p"
  55. pygame.event.post( pygame.event.Event(RUNSYS, message=cmdpath))
  56. if event.key == CurKeys["X"]:
  57. cmdpath = "feh --bg-center %s;" % MySkinManager.GiveWallpaper("seeyou.png")
  58. cmdpath += "sleep 3;"
  59. cmdpath += "sudo reboot"
  60. pygame.event.post( pygame.event.Event(RUNSYS, message=cmdpath))
  61. if event.key == CurKeys["Y"]:
  62. if self._FootMsg[1] != "":
  63. cmdpath = "feh --bg-center %s;" % MySkinManager.GiveWallpaper("seeyou.png")
  64. cmdpath += "sleep 3;"
  65. cmdpath += "sudo pm-suspend"
  66. pygame.event.post( pygame.event.Event(RUNSYS, message=cmdpath))
  67. class APIOBJ(object):
  68. _StoragePage = None
  69. def __init__(self):
  70. pass
  71. def Init(self,main_screen):
  72. self._Page = PowerOffConfirmPage()
  73. self._Page._Screen = main_screen
  74. self._Page._Name ="Power OFF"
  75. self._Page.Init()
  76. def API(self,main_screen):
  77. if main_screen !=None:
  78. main_screen.PushPage(self._Page)
  79. main_screen.Draw()
  80. main_screen.SwapAndShow()
  81. OBJ = APIOBJ()
  82. def Init(main_screen):
  83. OBJ.Init(main_screen)
  84. def API(main_screen):
  85. OBJ.API(main_screen)