From 60f1ac451316e977e19f74edf8bf108a5b6c3c6e Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Sat, 9 Jan 2021 20:43:50 +1300 Subject: [PATCH] Handle case of no commandline arguments. Restores previous session, or reads welcome page followed by links table. --- src/Input.hs | 10 ++++++++++ src/Render.hs | 9 ++++++++- src/main.c | 22 ++++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Input.hs b/src/Input.hs index 1cd6a73..1c7823a 100644 --- a/src/Input.hs +++ b/src/Input.hs @@ -272,3 +272,13 @@ foreign export ccall c_writeLog :: CString -> StablePtr Session -> IO () c_writeLog c_path c_session = do path <- peekCString c_path withFile path AppendMode (\logfile -> deRefStablePtr c_session >>= writeLog logfile) + +foreign export ccall c_lastVisited :: CString -> IO CString +c_lastVisited def = do + path <- ( "history.gmni") <$> getXdgDirectory XdgData "rhapsode" + exists <- doesFileExist path + if not exists then return def else do + file <- readFile path + case map words $ lines file of + (_:url:_):_ -> newCString url + _ -> return def diff --git a/src/Render.hs b/src/Render.hs index a1a5b32..d9ba67a 100644 --- a/src/Render.hs +++ b/src/Render.hs @@ -147,10 +147,13 @@ downloadAssets session mimes (StyleAssets _ assets) = do Dir.removeDirectoryRecursive dir `catch` ignoreError -- Clear cache. Dir.createDirectoryIfMissing True dir - fetchURLs session mimes assets $ filterMIMEs mimes $ saveDownload nullURI dir + -- Ensure core audio cues are saved with predictable names for UI layer to use. + let assets' = nub $ (u "about:link.wav":u "about:bulletpoint.wav":assets) + fetchURLs session mimes assets' $ filterMIMEs mimes $ saveDownload nullURI dir where ignoreError :: IOError -> IO () ignoreError _ = return () + u = fromMaybe (URI "about:" Nothing "invalid" "" "") . parseAbsoluteURI filterMIMEs mimes cb download@(_, mime, _) | mime `elem` mimes = cb download @@ -178,3 +181,7 @@ c_renderDoc c_session c_page rewriteURLs = do B.toStrict ssml `useAsCString` \cstr -> do str <- peekCString cstr newCString str + +foreign export ccall c_cachedir :: IO CString + +c_cachedir = Dir.getXdgDirectory Dir.XdgCache "rhapsode" >>= newCString diff --git a/src/main.c b/src/main.c index d8b4c1e..51363c2 100644 --- a/src/main.c +++ b/src/main.c @@ -27,7 +27,7 @@ void *c_fetchURL(struct session*, char*, struct page*, char*); //struct page **c_fetchURLs(struct session*, struct page*, char**); // FIXME segfaults. void c_freePage(struct page*); -void c_enableLogging(struct session*); +struct session *c_enableLogging(struct session*); void c_writeLog(char*, struct session*); char *c_renderDoc(struct session*, struct page*, _Bool); @@ -35,6 +35,8 @@ char **c_extractLinks(struct page*); char **c_docLinksAndRendering(struct session*, struct page*, _Bool); // FIXME segfaults. int c_ssmlHasMark(char*, char*); +char *c_lastVisited(char*); + /* espeak-ng integration. Based on the espeak-ng command source code. */ SNDFILE *fd_wav = NULL; char *path_wav = NULL; @@ -204,10 +206,12 @@ char *select_link(char **links, char *command) { // Pass 3: Retrieve answer for (int i = 0; strcmp(links[i], " ") != 0; i += 3) { - if (score < levenshtein_distance(command, links[i]) && - score < levenshtein_distance(command, links[i+1]) && - score < levenshtein_distance(command, links[i+2])) continue; - if (num_matches == 1) return links[i+2]; + if (command[0] != '\0') {// "" to read entire link table... + if (score < levenshtein_distance(command, links[i]) && + score < levenshtein_distance(command, links[i+1]) && + score < levenshtein_distance(command, links[i+2])) continue; + if (num_matches == 1) return links[i+2]; + } // Communicate printf("%s\t%s\t%s\n", links[i+2], links[i], links[i+1]); @@ -429,6 +433,11 @@ int main(int argc, char **argv) { if (use_espeak) speak_err = speak_initialize(); char *ssml, **links, *uri; + if (optind >= argc) { + // No URLs specified, restore previous session or goto about:welcome + uri = c_lastVisited("about:welcome"); + goto read_uri; + } for (int i = optind; i < argc; i++) { uri = argv[i]; @@ -439,7 +448,7 @@ read_uri: #endif else printf("%s\n", argv[i]); - if (logpath != NULL) c_enableLogging(session); + if (logpath != NULL) session = c_enableLogging(session); struct page *page = c_fetchURL(session, mimes, referer, uri); ssml = c_renderDoc(session, page, use_espeak); @@ -448,6 +457,7 @@ read_uri: if (logpath != NULL) c_writeLog(logpath, session); if (fd_ssml != NULL) fprintf(fd_ssml, "%s\n", ssml); + if (read_keyboard && strcmp(uri, "about:welcome")) select_link(links, ""); if (fd_links != NULL) write_links(fd_links, links); if (use_espeak & speak_err == 0) speak(ssml, "main", NULL); #ifdef WITH_SPEECHD -- 2.30.2