jf-roku/source/Main.brs

457 lines
18 KiB
Plaintext
Raw Normal View History

2021-07-09 20:08:32 +00:00
sub Main (args as dynamic) as void
2021-07-09 20:08:32 +00:00
' If the Rooibos files are included in deployment, run tests
'bs:disable-next-line
if type(Rooibos__Init) = "Function" then Rooibos__Init()
2021-07-09 20:08:32 +00:00
' The main function that runs when the application is launched.
m.screen = CreateObject("roSGScreen")
' Set global constants
setConstants()
m.port = CreateObject("roMessagePort")
m.screen.setMessagePort(m.port)
m.scene = m.screen.CreateScene("JFScene")
m.screen.show()
m.overhang = m.scene.findNode("overhang")
2021-07-09 20:08:32 +00:00
' Set any initial Global Variables
m.global = m.screen.getGlobalNode()
playstateTask = CreateObject("roSGNode", "PlaystateTask")
playstateTask.id = "playstateTask"
groupStack = CreateObject("roSGNode", "GroupStack")
2021-07-09 20:08:32 +00:00
m.global.addFields({ app_loaded: false, playstateTask: playstateTask, groupStack: groupStack })
2021-07-09 20:08:32 +00:00
app_start:
' First thing to do is validate the ability to use the API
' TODO: Should eventually switch the loginflow over to using the pushGroup() code. It's
' safe to leave for now because wipe_groups() ensures the equivalent to an empty group
' stack.
2021-07-09 20:08:32 +00:00
if not LoginFlow() then return
wipe_groups()
' load home page
m.overhang.currentUser = m.user.Name
group = CreateHomeGroup()
group.userConfig = m.user.configuration
group.callFunc("loadLibraries")
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
m.scene.observeField("exit", m.port)
2021-07-09 20:08:32 +00:00
' Handle input messages
input = CreateObject("roInput")
input.SetMessagePort(m.port)
m.device = CreateObject("roDeviceInfo")
m.device.setMessagePort(m.port)
m.device.EnableScreensaverExitedEvent(true)
m.device.EnableAppFocusEvent(false)
2021-07-09 20:08:32 +00:00
' Check if we were sent content to play with the startup command (Deep Link)
if (args.mediaType <> invalid) and (args.contentId <> invalid)
video = CreateVideoPlayerGroup(args.contentId)
if video <> invalid
groupStack.callFunc("push", video)
2020-11-30 07:46:20 +00:00
else
2021-07-09 20:08:32 +00:00
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
dialog.title = tr("Not found")
dialog.message = tr("The requested content does not exist on the server")
dialog.buttons = [tr("OK")]
m.scene.dialog = dialog
m.scene.dialog.observeField("buttonSelected", m.port)
2020-11-30 07:46:20 +00:00
end if
2021-07-09 20:08:32 +00:00
end if
' This is the core logic loop. Mostly for transitioning between scenes
' This now only references m. fields so could be placed anywhere, in theory
' "group" is always "whats on the screen"
' m.scene's children is the "previous view" stack
while true
msg = wait(0, m.port)
if type(msg) = "roSGScreenEvent" and msg.isScreenClosed()
print "CLOSING SCREEN"
return
else if isNodeEvent(msg, "exit")
return
2021-07-09 20:08:32 +00:00
else if isNodeEvent(msg, "closeSidePanel")
group = groupStack.callFunc("peek")
2021-07-09 20:08:32 +00:00
if group.lastFocus <> invalid
group.lastFocus.setFocus(true)
else
group.setFocus(true)
end if
else if isNodeEvent(msg, "quickPlayNode")
reportingNode = msg.getRoSGNode()
itemNode = reportingNode.quickPlayNode
if itemNode = invalid or itemNode.id = "" then return
if itemNode.type = "Episode" or itemNode.type = "Movie" or itemNode.type = "Video"
video = CreateVideoPlayerGroup(itemNode.id)
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
end if
end if
else if isNodeEvent(msg, "selectedItem")
' If you select a library from ANYWHERE, follow this flow
selectedItem = msg.getData()
if selectedItem.type = "CollectionFolder" or selectedItem.type = "UserView" or selectedItem.type = "Folder" or selectedItem.type = "Channel" or selectedItem.type = "Boxset"
group = CreateItemGrid(selectedItem)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if selectedItem.type = "Episode"
' play episode
' todo: create an episode page to link here
video_id = selectedItem.id
video = CreateVideoPlayerGroup(video_id)
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
end if
else if selectedItem.type = "Series"
group = CreateSeriesDetailsGroup(selectedItem.json)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if selectedItem.type = "Movie"
' open movie detail page
group = CreateMovieDetailsGroup(selectedItem)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if selectedItem.type = "TvChannel" or selectedItem.type = "Video"
' play channel feed
video_id = selectedItem.id
' Show Channel Loading spinner
dialog = createObject("roSGNode", "ProgressDialog")
dialog.title = tr("Loading Channel Data")
m.scene.dialog = dialog
video = CreateVideoPlayerGroup(video_id)
dialog.close = true
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
else
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
dialog.title = tr("Error loading Channel Data")
dialog.message = tr("Unable to load Channel Data from the server")
dialog.buttons = [tr("OK")]
m.scene.dialog = dialog
m.scene.dialog.observeField("buttonSelected", m.port)
end if
else
' TODO - switch on more node types
message_dialog("This type is not yet supported: " + selectedItem.type + ".")
end if
else if isNodeEvent(msg, "movieSelected")
' If you select a movie from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
group = CreateMovieDetailsGroup(node)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if isNodeEvent(msg, "seriesSelected")
' If you select a TV Series from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
group = CreateSeriesDetailsGroup(node)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if isNodeEvent(msg, "seasonSelected")
' If you select a TV Season from ANYWHERE, follow this flow
ptr = msg.getData()
' ptr is for [row, col] of selected item... but we only have 1 row
series = msg.getRoSGNode()
node = series.seasonData.items[ptr[1]]
group = CreateSeasonDetailsGroup(series.itemContent, node)
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if isNodeEvent(msg, "episodeSelected")
' If you select a TV Episode from ANYWHERE, follow this flow
node = getMsgPicker(msg, "picker")
video_id = node.id
video = CreateVideoPlayerGroup(video_id)
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
end if
else if isNodeEvent(msg, "search_value")
query = msg.getRoSGNode().search_value
group.findNode("SearchBox").visible = false
options = group.findNode("SearchSelect")
options.visible = true
options.setFocus(true)
dialog = createObject("roSGNode", "ProgressDialog")
dialog.title = tr("Loading Search Data")
m.scene.dialog = dialog
results = SearchMedia(query)
dialog.close = true
options.itemData = results
options.query = query
else if isNodeEvent(msg, "itemSelected")
' Search item selected
node = getMsgPicker(msg)
' TODO - swap this based on target.mediatype
' types: [ Series (Show), Episode, Movie, Audio, Person, Studio, MusicArtist ]
if node.type = "Series"
group = CreateSeriesDetailsGroup(node)
else
2021-07-09 20:08:32 +00:00
group = CreateMovieDetailsGroup(node)
2021-04-14 07:03:15 +00:00
end if
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
else if isNodeEvent(msg, "buttonSelected")
' If a button is selected, we have some determining to do
btn = getButton(msg)
if btn <> invalid and btn.id = "play-button"
' Check is a specific Audio Stream was selected
audio_stream_idx = 1
if group.selectedAudioStreamIndex <> invalid
audio_stream_idx = group.selectedAudioStreamIndex
end if
' TODO - Do a better job of picking the last focus
' This is currently page layout Group, button Group, then button
video_id = group.id
video = CreateVideoPlayerGroup(video_id, audio_stream_idx)
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
end if
else if btn <> invalid and btn.id = "watched-button"
movie = group.itemContent
if movie.watched
UnmarkItemWatched(movie.id)
else
MarkItemWatched(movie.id)
end if
movie.watched = not movie.watched
else if btn <> invalid and btn.id = "favorite-button"
movie = group.itemContent
if movie.favorite
UnmarkItemFavorite(movie.id)
else
MarkItemFavorite(movie.id)
end if
movie.favorite = not movie.favorite
else
' If there are no other button matches, check if this is a simple "OK" Dialog & Close if so
dialog = msg.getRoSGNode()
if dialog.id = "OKDialog"
dialog.unobserveField("buttonSelected")
dialog.close = true
end if
end if
else if isNodeEvent(msg, "optionSelected")
button = msg.getRoSGNode()
if button.id = "goto_search"
' Exit out of the side panel
panel.visible = false
if group.lastFocus <> invalid
group.lastFocus.setFocus(true)
else
group.setFocus(true)
end if
group = CreateSearchPage()
groupStack.callFunc("push", group)
2021-07-09 20:08:32 +00:00
group.findNode("SearchBox").findNode("search-input").setFocus(true)
group.findNode("SearchBox").findNode("search-input").active = true
else if button.id = "change_server"
unset_setting("server")
unset_setting("port")
SignOut()
wipe_groups()
goto app_start
else if button.id = "sign_out"
SignOut()
wipe_groups()
goto app_start
else if button.id = "play_mpeg2"
playMpeg2 = get_setting("playback.mpeg2")
if playMpeg2 = "true"
playMpeg2 = "false"
button.title = tr("MPEG2 Support: Off")
else
playMpeg2 = "true"
button.title = tr("MPEG2 Support: On")
end if
set_setting("playback.mpeg2", playMpeg2)
2021-07-09 20:08:32 +00:00
end if
else if isNodeEvent(msg, "selectSubtitlePressed")
node = m.scene.focusedChild
if node.isSubType("JFVideo")
trackSelected = selectSubtitleTrack(node.Subtitles, node.SelectedSubtitle)
if trackSelected <> invalid and trackSelected <> -2
changeSubtitleDuringPlayback(trackSelected)
end if
end if
else if isNodeEvent(msg, "state")
node = msg.getRoSGNode()
if node.state = "finished"
node.control = "stop"
2021-07-09 20:08:32 +00:00
if node.showID = invalid
groupStack.callFunc("pop")
2021-07-09 20:08:32 +00:00
else
autoPlayNextEpisode(node.id, node.showID)
2021-07-09 20:08:32 +00:00
end if
end if
else if type(msg) = "roDeviceInfoEvent"
event = msg.GetInfo()
if event.exitedScreensaver = true
m.overhang.callFunc("resetTime")
if group.subtype() = "Home"
currentTime = CreateObject("roDateTime").AsSeconds()
group.timeLastRefresh = currentTime
group.callFunc("refresh")
end if
' todo: add other screens to be refreshed - movie detail, tv series, episode list etc.
else
print "Unhandled roDeviceInfoEvent:"
print msg.GetInfo()
end if
else if type(msg) = "roInputEvent"
if msg.IsInput()
info = msg.GetInfo()
if info.DoesExist("mediatype") and info.DoesExist("contentid")
video = CreateVideoPlayerGroup(info.contentId)
if video <> invalid
groupStack.callFunc("push", video)
2021-07-09 20:08:32 +00:00
else
dialog = createObject("roSGNode", "Dialog")
dialog.id = "OKDialog"
dialog.title = tr("Not found")
dialog.message = tr("The requested content does not exist on the server")
dialog.buttons = [tr("OK")]
m.scene.dialog = dialog
m.scene.dialog.observeField("buttonSelected", m.port)
end if
end if
end if
else
print "Unhandled " type(msg)
print msg
end if
end while
end sub
2020-03-22 22:40:47 +00:00
function LoginFlow(startOver = false as boolean)
2021-07-09 20:08:32 +00:00
if m.scene <> invalid
m.scene.unobserveField("backPressed")
end if
'Collect Jellyfin server and user information
start_login:
2021-04-04 13:41:44 +00:00
2021-07-09 20:08:32 +00:00
if get_setting("server") = invalid then startOver = true
2021-04-04 13:41:44 +00:00
2021-07-09 20:08:32 +00:00
invalidServer = true
if not startOver
2021-04-04 13:41:44 +00:00
' Show Connecting to Server spinner
dialog = createObject("roSGNode", "ProgressDialog")
dialog.title = tr("Connecting to Server")
m.scene.dialog = dialog
invalidServer = ServerInfo().Error
2021-04-04 13:41:44 +00:00
dialog.close = true
end if
2021-07-09 20:08:32 +00:00
if startOver or invalidServer
print "Get server details"
SendPerformanceBeacon("AppDialogInitiate") ' Roku Performance monitoring - Dialog Starting
serverSelection = CreateServerGroup()
SendPerformanceBeacon("AppDialogComplete") ' Roku Performance monitoring - Dialog Closed
if serverSelection = "backPressed"
print "backPressed"
wipe_groups()
return false
end if
2021-07-09 20:08:32 +00:00
end if
if get_setting("active_user") = invalid
SendPerformanceBeacon("AppDialogInitiate") ' Roku Performance monitoring - Dialog Starting
publicUsers = GetPublicUsers()
if publicUsers.count()
publicUsersNodes = []
for each item in publicUsers
user = CreateObject("roSGNode", "PublicUserData")
user.id = item.Id
user.name = item.Name
if item.PrimaryImageTag <> invalid
user.ImageURL = UserImageURL(user.id, { "tag": item.PrimaryImageTag })
end if
publicUsersNodes.push(user)
end for
userSelected = CreateUserSelectGroup(publicUsersNodes)
m.scene.focusedChild.visible = false
if userSelected = "backPressed"
SendPerformanceBeacon("AppDialogComplete") ' Roku Performance monitoring - Dialog Closed
return LoginFlow(true)
else
'Try to login without password. If the token is valid, we're done
get_token(userSelected, "")
if get_setting("active_user") <> invalid
m.user = AboutMe()
LoadUserPreferences()
SendPerformanceBeacon("AppDialogComplete") ' Roku Performance monitoring - Dialog Closed
return true
end if
end if
else
userSelected = ""
end if
passwordEntry = CreateSigninGroup(userSelected)
SendPerformanceBeacon("AppDialogComplete") ' Roku Performance monitoring - Dialog Closed
2021-07-09 20:08:32 +00:00
if passwordEntry = "backPressed"
m.scene.focusedChild.visible = false
return LoginFlow(true)
end if
end if
2021-07-09 20:08:32 +00:00
m.user = AboutMe()
if m.user = invalid or m.user.id <> get_setting("active_user")
print "Login failed, restart flow"
unset_setting("active_user")
goto start_login
end if
2021-07-09 20:08:32 +00:00
LoadUserPreferences()
wipe_groups()
'Send Device Profile information to server
body = getDeviceCapabilities()
req = APIRequest("/Sessions/Capabilities/Full")
req.SetRequest("POST")
postJson(req, FormatJson(body))
return true
end function
2019-10-05 07:50:05 +00:00
sub RunScreenSaver()
2021-07-09 20:08:32 +00:00
print "Starting screensaver..."
screen = createObject("roSGScreen")
m.port = createObject("roMessagePort")
screen.setMessagePort(m.port)
screen.createScene("Screensaver")
screen.Show()
while true
msg = wait(8000, m.port)
if msg <> invalid
msgType = type(msg)
if msgType = "roSGScreenEvent"
if msg.isScreenClosed() then return
end if
end if
end while
2019-10-05 07:50:05 +00:00
2019-10-13 19:33:14 +00:00
end sub
sub wipe_groups()
2021-07-09 20:08:32 +00:00
' The 1 remaining child should be the overhang
while m.scene.getChildCount() > 1
m.scene.removeChildIndex(1)
end while
2020-02-15 17:56:56 +00:00
end sub
' Roku Performance monitoring
sub SendPerformanceBeacon(signalName as string)
2021-07-09 20:08:32 +00:00
if m.global.app_loaded = false
m.scene.signalBeacon(signalName)
end if
2021-07-09 10:21:24 +00:00
end sub