private static void OnExit(object sender, EventArgs e) { // Clear tray icon on close if (TrayIcon != null) { TrayIcon.Visible = false; } // On send clear priority if (_protoClient != null) { _captureEnabled = false; ProtoClient.ClearPriority(Settings.HyperionMessagePriority); Thread.Sleep(50); ProtoClient.ClearPriority(Settings.HyperionMessagePriority); ProtoClient.Disconnect(); } if (Settings.ApiEnabled) { _apiServer.StopServer(); } Environment.Exit(0); }
private static void InitProtoClient() { if (ProtoClient.IsConnected()) { // Proto client already initialized return; } try { LOG.Info("Initializing Proto Client"); ProtoClient.Disconnect(); ProtoClient.Init(SettingsManager.HyperionServerIp, SettingsManager.HyperionServerPort, SettingsManager.HyperionMessagePriority); // Double checking since sometimes exceptions are not thrown even if connection fails if (ProtoClient.IsConnected()) { LOG.Info("Proto Client initialized"); NotificationUtils.Info($"Connected to Hyperion server on {SettingsManager.HyperionServerIp}:{SettingsManager.HyperionServerPort}!"); } else { throw new Exception(GetProtoInitFailedMsg()); } } catch (Exception ex) { throw new Exception(GetProtoInitFailedMsg(), ex); } }
private static void OnExit(object sender, EventArgs e) { LOG.Info("Exiting Application"); // Clear tray icon on close if (TrayIcon != null) { TrayIcon.Visible = false; } // Send clear priority if (ProtoClient.Initialized) { TerminateScreenCapture(); ProtoClient.TryClearPriority(SettingsManager.HyperionMessagePriority); DisconnectProtoClient(); } if (SettingsManager.ApiEnabled) { _apiServer.StopServer(); } // Unregister various event handlers SystemEvents.PowerModeChanged -= PowerModeChanged; SystemEvents.SessionSwitch -= SessionSwitched; LOG.Info("Exit cleanup complete"); Environment.Exit(0); }
private static void DisconnectProtoClient() { ProtoClient.Disconnect(); Thread.Sleep(500); TrayIcon.Text = AppConstants.TRAY_ICON_MSG_NOT_CONNECTED; LOG.Info("Proto Client disconnect action completed"); }
private static void TransmitNextFrame() { try { byte[] imageData = _screenCapture.Capture(); ProtoClient.SendImageToServer(imageData, _screenCapture.CaptureWidth, _screenCapture.CaptureHeight); // Uncomment the following to enable debugging // MiscUtils.SaveRGBArrayToImageFile(imageData, _screenCapture.CaptureWidth, _screenCapture.CaptureHeight, AppConstants.DEBUG_IMAGE_FILE_NAME); } catch (Exception ex) { throw new Exception("Error occured during capture: " + ex.Message, ex); } }
public static void Init(bool reInit = false, bool forceOn = false) { if (!_initLock) { _initLock = true; // Stop current capture first on reinit if (reInit) { _captureEnabled = false; Thread.Sleep(500 + Settings.CaptureInterval); if (_protoClient != null) { ProtoClient.Disconnect(); Thread.Sleep(500); } } _protoClient = new ProtoClient(); ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); if (Settings.CaptureOnStartup || forceOn) { if (ProtoClient.IsConnected()) { Notifications.Info($"Connected to Hyperion server on {Settings.HyperionServerIp}!"); ToggleCapture("ON"); } } if (Settings.ApiEnabled) { _apiServer = new ApiServer(); _apiServer.StartServer("localhost", Settings.ApiPort.ToString()); } else { _apiServer?.StopServer(); } _initLock = false; } }
private void PowerModeChanged(object sender, PowerModeChangedEventArgs powerMode) { // On resume restart capture instance after grace period in case that was resume if (powerMode.Mode == PowerModes.Resume) { if (_captureEnabled) { _captureEnabled = false; Thread.Sleep(2500); ToggleCapture("ON"); } Thread.Sleep(1500); _protoClient = new ProtoClient(); ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); } }
public Form1() { InitializeComponent(); // Create a simple tray menu with only one item. var trayMenu = new ContextMenu(); trayMenu.MenuItems.Add("Exit", OnExit); TrayIcon = new NotifyIcon { Text = @"Hyperion Screen Capture (Not Connected)" }; TrayIcon.DoubleClick += TrayIcon_DoubleClick; TrayIcon.Icon = Resources.Hyperion_disabled; // Add menu to tray icon and show it. TrayIcon.ContextMenu = trayMenu; TrayIcon.Visible = true; _d = new DxScreenCapture(MonitorIndex); _protoClient = new ProtoClient(); _protoClient.Init(HyperionServerIp, HyperionServerPort, HyperionMessagePriority); if (_protoClient.IsConnected()) { if (NotificationLevel == NotifcationLevels.Info) { Notifications.Info(string.Format("Connected to Hyperion server on {0}!", HyperionServerIp)); } CaptureEnabled = true; Thread t = new Thread(StartCapture) { IsBackground = true }; t.Start(); } TrayIcon.Icon = Resources.Hyperion_enabled; TrayIcon.Text = @"Hyperion Screen Capture (Enabled)"; }
private static void StartCapture() { try { _d = new DxScreenCapture(Settings.MonitorIndex); while (_captureEnabled) { if (!ProtoClient.IsConnected()) { // Reconnect every 5s (default) ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); Thread.Sleep(Settings.ReconnectInterval); continue; } var s = _d.CaptureScreen(Settings.HyperionWidth, Settings.HyperionHeight, _d.MonitorIndex); var dr = s.LockRectangle(LockFlags.None); var ds = dr.Data; var x = RemoveAlpha(ds); s.UnlockRectangle(); s.Dispose(); ds.Dispose(); ProtoClient.SendImageToServer(x); // Add small delay to reduce cpu usage (200FPS max) Thread.Sleep(Settings.CaptureInterval); } _d = null; } catch (Exception ex) { _captureEnabled = false; Notifications.Error("Error occured during capture: " + ex.Message); } }
public static void ToggleCapture(string command) { if (_captureEnabled && command == "OFF") { _captureEnabled = false; TrayIcon.Icon = Resources.Hyperion_disabled; TrayIcon.Text = @"Hyperion Screen Capture (Disabled)"; ProtoClient.ClearPriority(Settings.HyperionMessagePriority); } else if (!_captureEnabled && command == "ON") { _captureEnabled = true; TrayIcon.Icon = Resources.Hyperion_enabled; TrayIcon.Text = @"Hyperion Screen Capture (Enabled)"; Thread.Sleep(50); var t = new Thread(StartCapture) { IsBackground = true }; t.Start(); } }
private static void ExecuteToggleCaptureCommand(CaptureCommand command) { try { switch (command) { case CaptureCommand.ON: TrayIcon.Icon = Resources.Hyperion_enabled; TrayIcon.Text = AppConstants.TRAY_ICON_MSG_CAPTURE_ENABLED; _captureEnabled = true; _captureThread = new Thread(TryStartCapture) { IsBackground = true }; _captureThread.Start(); break; case CaptureCommand.OFF: TrayIcon.Icon = Resources.Hyperion_disabled; TrayIcon.Text = AppConstants.TRAY_ICON_MSG_CAPTURE_DISABLED; TerminateScreenCapture(); ProtoClient.TryClearPriority(SettingsManager.HyperionMessagePriority); DisconnectProtoClient(); break; default: throw new NotImplementedException($"The capture command {command} is not supported"); } LOG.Info($"Toggle capture command {command} completed"); } finally { _captureToggleInProgress = false; LOG.Info("Toggle capture lock unset"); } }