frontend 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. LAUNCHER_FILE="$HOME/Launchers/launcher.txt"
  6. FRONTEND_FILE="$HOME/.frontend"
  7. PREVENT_LAUNCHER_FILE="/mnt/prevent_launcher"
  8. DISABLE_FRONTEND_FILE="/mnt/disable_frontend"
  9. DEFAULT_FRONTEND=retrofe
  10. LOCK_FILE="/var/lock/frontend.lock"
  11. REBOOTING_FILE="/run/rebooting"
  12. LAST_OPK_FILE="/mnt/last_opk"
  13. # Convert old launcher file to new frontend
  14. if [ -f "${LAUNCHER_FILE}" ]; then
  15. mv "${LAUNCHER_FILE}" "${FRONTEND_FILE}"
  16. rm -rf $(dirname "${LAUNCHER_FILE}") 2>/dev/null
  17. fi
  18. # Convert old prevent launcher file to new frontend disable file
  19. if [ -f "${PREVENT_LAUNCHER_FILE}" ]; then
  20. mv "${PREVENT_LAUNCHER_FILE}" "${DISABLE_FRONTEND_FILE}"
  21. fi
  22. usage() {
  23. >&2 echo "Usage: ${SELF} init"
  24. >&2 echo " ${SELF} get"
  25. >&2 echo " ${SELF} set gmenu2x|retrofe|none"
  26. exit 1
  27. }
  28. set_frontend() {
  29. mkdir -p "$(dirname "$FRONTEND_FILE")"
  30. local frontend=$(get_frontend)
  31. local new_frontend="${1}"
  32. echo "Setting frontend: ${new_frontend}"
  33. if [ "${new_frontend}" = "none" ]; then
  34. touch "${DISABLE_FRONTEND_FILE}"
  35. else
  36. rm -f "${DISABLE_FRONTEND_FILE}"
  37. fi
  38. if [ "${frontend}" != "none" ]; then
  39. pkill "${frontend}"
  40. fi
  41. echo "${new_frontend}" > "${FRONTEND_FILE}"
  42. }
  43. get_frontend() {
  44. local frontend=$(cat "${FRONTEND_FILE}" 2>/dev/null | head -1)
  45. # Check if not empty
  46. if [ "x${frontend}" = "x" ]; then
  47. frontend="${DEFAULT_FRONTEND}"
  48. echo "${frontend}" > "${FRONTEND_FILE}"
  49. fi
  50. # Return frontend name
  51. echo "${frontend}"
  52. }
  53. init_frontend() {
  54. if [ -f "${LOCK_FILE}" ]; then
  55. >&2 echo "${LOCK_FILE} already exists"
  56. exit 1
  57. fi
  58. touch "${LOCK_FILE}"
  59. # Then loop to launch the frontend indefinitely
  60. while true; do
  61. # Check if frontend disable file is present
  62. if [ -f "${DISABLE_FRONTEND_FILE}" -o -f "${REBOOTING_FILE}" ]; then
  63. echo "${DISABLE_FRONTEND_FILE} file found, not starting frontend"
  64. sleep 5
  65. else
  66. # Umount any remaining OPK, if any
  67. umount /opk >/dev/null 2>&1
  68. if [ -r "${LAST_OPK_FILE}" -a ! -f "${REBOOTING_FILE}" ]; then
  69. rm "${LAST_OPK_FILE}"
  70. fi
  71. # Launch selected frontend
  72. local frontend="$(get_frontend)"
  73. case "${frontend}" in
  74. gmenu2x|retrofe)
  75. "${frontend}"&
  76. ;;
  77. none)
  78. echo "no frontend"
  79. sleep 5
  80. ;;
  81. *)
  82. DEFAULT_FRONTEND=retrofe
  83. echo "Unrecognized frontend: $frontend, setting $DEFAULT_FRONTEND"
  84. set_frontend "${DEFAULT_FRONTEND}"
  85. ;;
  86. esac
  87. # Record the PID into a file, wait for the process to
  88. # terminate and erase the recorded PID
  89. pid record $!
  90. wait $!
  91. pid erase
  92. # In case retrofe/opkrun quits with errors, clear graphic VT
  93. termfix_all
  94. # In case retrofe/opkrun quits with errors, reset default key mapping
  95. keymap default
  96. fi
  97. # Prevent 100% CPU usage
  98. sleep 0.5
  99. # Exit if console rebooting
  100. if [ -f "${REBOOTING_FILE}" ]; then
  101. break
  102. fi
  103. done
  104. # Remove lock file and exit
  105. rm "${LOCK_FILE}"
  106. }
  107. # Check number of arguments
  108. if [ ${#} -lt 1 -o ${#} -gt 2 ]; then
  109. usage
  110. fi
  111. case "${1}" in
  112. set)
  113. if [ ${#} -ne 2 ]; then
  114. usage
  115. fi
  116. set_frontend "${2}"
  117. ;;
  118. get)
  119. if [ ${#} -ne 1 ]; then
  120. usage
  121. fi
  122. get_frontend
  123. ;;
  124. init)
  125. if [ ${#} -ne 1 ]; then
  126. usage
  127. fi
  128. init_frontend
  129. ;;
  130. *)
  131. usage
  132. ;;
  133. esac
  134. exit 0