main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "embed"
  4. "flag"
  5. "io/fs"
  6. "log"
  7. "net/http"
  8. "os"
  9. "os/signal"
  10. "syscall"
  11. "time"
  12. "imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
  13. "imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
  14. )
  15. const defaultDevMode = true
  16. var (
  17. developent = flag.Bool("dev", defaultDevMode, "Enable development mode with local static files")
  18. mode = flag.String("mode", "usbkvm", "Mode of operation: kvm or capture")
  19. usbKVMDeviceName = flag.String("usbkvm", "/dev/ttyACM0", "USB KVM device file path")
  20. usbKVMBaudRate = flag.Int("baudrate", 115200, "USB KVM baud rate")
  21. captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
  22. usbKVM *remdeshid.Controller
  23. videoCapture *usbcapture.Instance
  24. )
  25. /* Web Server Static Files */
  26. //go:embed www
  27. var embeddedFiles embed.FS
  28. var webfs http.FileSystem
  29. func init() {
  30. // Initiate the web server static files
  31. if *developent {
  32. webfs = http.Dir("./www")
  33. } else {
  34. // Embed the ./www folder and trim the prefix
  35. subFS, err := fs.Sub(embeddedFiles, "www")
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. webfs = http.FS(subFS)
  40. }
  41. }
  42. func main() {
  43. flag.Parse()
  44. // Initiate the HID controller
  45. usbKVM = remdeshid.NewHIDController(&remdeshid.Config{
  46. PortName: *usbKVMDeviceName,
  47. BaudRate: *usbKVMBaudRate,
  48. ScrollSensitivity: 0x01, // Set mouse scroll sensitivity
  49. })
  50. switch *mode {
  51. case "cfgchip":
  52. //Start the HID controller
  53. err := usbKVM.Connect()
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. time.Sleep(2 * time.Second) // Wait for the controller to initialize
  58. //Configure the HID controller
  59. usbKVM.ConfigureChipTo115200()
  60. case "usbkvm":
  61. log.Println("Starting in USB KVM mode...")
  62. //Start the HID controller
  63. err := usbKVM.Connect()
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. // Initiate the video capture device
  68. videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
  69. DeviceName: *captureDeviceName,
  70. })
  71. if err != nil {
  72. log.Fatalf("Failed to create video capture instance: %v", err)
  73. }
  74. //Get device information for debug
  75. usbcapture.PrintV4L2FormatInfo(*captureDeviceName)
  76. //Start the video capture device
  77. err = videoCapture.StartVideoCapture(&usbcapture.CaptureResolution{
  78. Width: 1920,
  79. Height: 1080,
  80. FPS: 10,
  81. })
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. // Handle program exit to close the HID controller
  86. c := make(chan os.Signal, 1)
  87. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  88. go func() {
  89. <-c
  90. log.Println("Shutting down usbKVM...")
  91. //usbKVM.Close() //To fix close stuck layer
  92. log.Println("Shutting down capture device...")
  93. videoCapture.Close()
  94. os.Exit(0)
  95. }()
  96. // Start the web server
  97. http.Handle("/", http.FileServer(webfs))
  98. http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
  99. http.HandleFunc("/stream", videoCapture.ServeVideoStream)
  100. addr := ":9000"
  101. log.Printf("Serving on http://localhost%s\n", addr)
  102. log.Fatal(http.ListenAndServe(addr, nil))
  103. default:
  104. log.Fatalf("Unknown mode: %s. Supported modes are: usbkvm, capture", *mode)
  105. }
  106. }