public ResultOrError <Stream> ReadFile(string path) { string newPath = ResolvePath(path); try { var data = new MemoryStream(); clovershell.Execute("cat \"" + newPath + "\"", null, data, null, 1000, true); data.Seek(0, SeekOrigin.Begin); return(MakeResult <Stream>(data)); } catch (Exception ex) { return(MakeError <Stream>(ex.Message)); } }
// -- Emulator stop-and-go routines -- // Stop emulator process to suppress tearing of the frame buffer. public static Int64 findEmulatorProcess(ClovershellConnection sh, string grepPattern) { var stdoutResult = new MemoryStream(); sh.Execute($"ps|grep {grepPattern}", null, stdoutResult, null, 1000, false); var resultStr = Encoding.UTF8.GetString(stdoutResult.ToArray()); resultStr = System.Text.RegularExpressions.Regex.Replace(resultStr, "^\\s+", ""); // Trim whitespaces var fields = System.Text.RegularExpressions.Regex.Split(resultStr, " +"); if (fields.Length < 1) { return(-1); // emulator not found } try { Int64 pid = Convert.ToInt64(fields[0], 10); Debug.WriteLine($"Found PID: {grepPattern} -> {pid}"); return(pid); } catch { Debug.WriteLine("Bad PID : " + resultStr); return(-1); } }
public Bitmap captureScreen() { // First, search canoe(official emulator) process Int64 emulatorPid = findEmulatorProcess(clvshell, "[c]anoe"); if (emulatorPid < 0) { Debug.WriteLine("Canoe can't be found. Searching home menu..."); // Second, search home menu process emulatorPid = findEmulatorProcess(clvshell, "[R]eedPlayer-Clover"); if (emulatorPid < 0) { Debug.WriteLine("Valid PIDs can't be found. Capturing without SIGSTOP."); } } var screenshotImage = new Bitmap(ConsoleScreenWidth, ConsoleScreenHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var rawStream = new MemoryStream(); pauseEmulatorProcess(clvshell, emulatorPid); clvshell.Execute("cat /dev/fb0", null, rawStream, null, 1000, true); resumeEmulatorProcess(clvshell, emulatorPid); var raw = rawStream.ToArray(); BitmapData data = screenshotImage.LockBits( new Rectangle(0, 0, screenshotImage.Width, screenshotImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); copyBitmapData( data, raw, screenshotImage.Width, screenshotImage.Height); screenshotImage.UnlockBits(data); return(screenshotImage); }
public static void resumeEmulatorProcess(ClovershellConnection sh, Int64 pid) { if (pid < 0) { return; } // emulator not found sh.Execute($"kill -s SIGCONT {pid}", null, null, null, 1000, false); }