MKlegoman357

Screenshare program

Jun 18th, 2014 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. --[[
  2. Screenshare program by @MKlegoman357
  3.  
  4. This is a program you which mirrors computer's screen
  5. onto a monitor. This program automatically adapts to
  6. both, terminal and monitor for the best user experience.
  7. It also auto-adapts to monitor and screen size changes,
  8. as well as notices when a monitor gets disconnected.
  9.  
  10. Usage:
  11.  
  12. screenshare <side> [text scale] [program] [arguments]
  13.  
  14. side - this is the side or network name the monitor is connected to
  15. text scale - this is the text scale of the monitor (default: 1)
  16. program, arguments - this is the program you want to run (default: shell or multishell, depends if advanced computer or not)
  17.  
  18. Notes:
  19. * only works with CC 1.6 and above
  20. * all arguments, except side, are optional
  21. * uses default 'window' API, which is pretty laggy
  22. ]]
  23.  
  24. local args = {...}
  25. local side = args[1]
  26. local textScale = tonumber(args[2]) or 1
  27. local program = {select(3, ...)}
  28. local name = shell and shell.getRunningProgram and fs.getName(shell.getRunningProgram()) or "screenshare"
  29.  
  30. if #args == 0 or args[1] == "?" or args[1] == "help" then
  31.   print("Usage:")
  32.   print(name, " <side> [text scale] [program] [arguments] ...")
  33.  
  34.   error("", 0)
  35. end
  36.  
  37. if peripheral.getType(side) ~= "monitor" then
  38.   error("No monitor on \"" .. side .. "\" side!", 0)
  39. end
  40.  
  41. local function clamp (n, min, max)
  42.   return n > max and max or n < min and min or n
  43. end
  44.  
  45. local function clear (t, bc)
  46.   t = t or term
  47.  
  48.   t.setBackgroundColor(bc or colors.black)
  49.   t.setTextColor(colors.white)
  50.   t.clear()
  51.   t.setCursorPos(1, 1)
  52. end
  53.  
  54. local prevTerm = term.current()
  55. local mon = peripheral.wrap(side)
  56.  
  57. textScale = clamp(textScale, 0.5, 5)
  58.  
  59. mon.setTextScale(textScale)
  60.  
  61. local termW, termH, monW, monH, termX, termY, monX, monY, width, height
  62.  
  63. local function updateVariables ()
  64.   termW, termH = prevTerm.getSize()
  65.   monW, monH = mon.getSize()
  66.  
  67.   width = termW < monW and termW or monW
  68.   height = termH < monH and termH or monH
  69.  
  70.   termX, termY = clamp(math.ceil((termW - width) / 2), 1, termW), clamp(math.ceil((termH - height) / 2), 1, termH)
  71.   monX, monY = clamp(math.ceil((monW - width) / 2), 1, monW), clamp(math.ceil((monH - height) / 2), 1, monH)
  72. end
  73.  
  74. updateVariables()
  75.  
  76. local termWin = window.create(prevTerm, termX, termY, width, height, true)
  77. local monWin = window.create(mon, monX, monY, width, height, true)
  78.  
  79. local redirect = {}
  80.  
  81. for name in pairs(term.native()) do
  82.   redirect[name] = function (...)
  83.     termWin[name](...)
  84.        
  85.     return monWin[name](...)
  86.   end
  87. end
  88.  
  89. redirect.isColor = function ()
  90.   return term.native().isColor() and mon.isColor()
  91. end
  92.  
  93. redirect.isColour = function ()
  94.   return term.native().isColour() and mon.isColour()
  95. end
  96.  
  97. local function updateDisplays ()
  98.   mon.setTextScale(textScale)
  99.   updateVariables()
  100.  
  101.   termWin.reposition(termX, termY, width, height)
  102.   monWin.reposition(monX, monY, width, height)
  103.  
  104.   clear(prevTerm, colors.white)
  105.   clear(mon, colors.white)
  106.  
  107.   termWin.redraw()
  108.   monWin.redraw()
  109. end
  110.  
  111. clear(prevTerm, colors.white)
  112. clear(mon, colors.white)
  113.  
  114. term.redirect(redirect)
  115. clear()
  116.  
  117. if not program[1] then
  118.   if term.isColor() then
  119.     program[1] = "/rom/programs/advanced/multishell.lua"
  120.   else
  121.     program[1] = "/rom/programs/shell.lua"
  122.   end
  123. end
  124.  
  125. local env = setmetatable({shell = shell, multishell = multishell}, {__index = _G})
  126.  
  127. local co
  128.  
  129. do
  130.   local func, err = loadfile(program[1])
  131.  
  132.   if not func then
  133.     printError("Error loading file:")
  134.     error(err, 0)
  135.   end
  136.  
  137.   setfenv(func, env)
  138.  
  139.   co = coroutine.create(func)
  140. end
  141.  
  142. local ok, filter = coroutine.resume(co, unpack(program, 2))
  143.  
  144. while ok and coroutine.status(co) ~= "dead" do
  145.   local event = {coroutine.yield(filter)}
  146.  
  147.   if event[1] == "mouse_click" or event[1] == "mouse_drag" or event[1] == "mouse_scroll" then
  148.     event[3] = event[3] - termX + 1
  149.     event[4] = event[4] - termY + 1
  150.   elseif event[1] == "monitor_resize" and event[2] == side then
  151.     updateDisplays()
  152.  
  153.     os.queueEvent("term_resize", "monitor")
  154.   elseif event[1] == "term_resize" then
  155.     updateDisplays()
  156.   elseif event[1] == "peripheral_detach" and event[2] == side then
  157.     term.redirect(prevTerm)
  158.     clear()
  159.     printError("Monitor was disconnected! Please attach the monitor on \"" .. side .. "\" side or press [E] to exit.")
  160.     term.setCursorBlink(false)
  161.  
  162.     repeat
  163.       local event, k = os.pullEvent()
  164.  
  165.       if event == "key" and k == keys.e then
  166.         clear()
  167.         print("Screenshare stopped!")
  168.  
  169.         error("", 0)
  170.       end
  171.     until peripheral.getType(side) == "monitor"
  172.  
  173.     updateDisplays()
  174.   end
  175.  
  176.   ok, filter = coroutine.resume(co, unpack(event))
  177. end
  178.  
  179. if not ok then
  180.   error(filter, 0)
  181. end
  182.  
  183. term.redirect(prevTerm)
  184.  
  185. clear(prevTerm)
  186. clear(mon)
  187.  
  188. print("Screenshare stopped!")
Advertisement
Add Comment
Please, Sign In to add comment