UserSessions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.SystemLayer
  5. {
  6. /// <summary>
  7. /// Encapsulates information and events about the current user session.
  8. /// This relates to Terminal Services in Windows.
  9. /// </summary>
  10. public static class UserSessions
  11. {
  12. private static OurControl messageControl;
  13. private static bool lastRemoteSessionValue;
  14. private static EventHandler sessionChanged;
  15. private static int sessionChangedCount;
  16. private static object lockObject = new object();
  17. private sealed class OurControl
  18. : Control
  19. {
  20. public event EventHandler WmWtSessionChange;
  21. private void OnWmWtSessionChange()
  22. {
  23. if (WmWtSessionChange != null)
  24. {
  25. WmWtSessionChange(this, EventArgs.Empty);
  26. }
  27. }
  28. protected override void WndProc(ref Message m)
  29. {
  30. switch (m.Msg)
  31. {
  32. case NativeConstants.WM_WTSSESSION_CHANGE:
  33. OnWmWtSessionChange();
  34. break;
  35. default:
  36. base.WndProc(ref m);
  37. break;
  38. }
  39. }
  40. }
  41. private static void OnSessionChanged()
  42. {
  43. if (sessionChanged != null)
  44. {
  45. sessionChanged(null, EventArgs.Empty);
  46. }
  47. }
  48. /// <summary>
  49. /// Occurs when the user changes between sessions. This event will only be
  50. /// raised when the value returned by IsRemote() changes.
  51. /// </summary>
  52. /// <remarks>
  53. /// For example, if the user is currently logged in at the console, and then
  54. /// switches to a remote session (they use Remote Desktop from another computer),
  55. /// then this event will be raised.
  56. /// Note to implementors: This may be implemented as a no-op.
  57. /// </remarks>
  58. public static event EventHandler SessionChanged
  59. {
  60. add
  61. {
  62. lock (lockObject)
  63. {
  64. sessionChanged += value;
  65. ++sessionChangedCount;
  66. if (sessionChangedCount == 1)
  67. {
  68. messageControl = new OurControl();
  69. messageControl.CreateControl(); // force the HWND to be created
  70. messageControl.WmWtSessionChange += new EventHandler(SessionStrobeHandler);
  71. SafeNativeMethods.WTSRegisterSessionNotification(messageControl.Handle, NativeConstants.NOTIFY_FOR_ALL_SESSIONS);
  72. lastRemoteSessionValue = IsRemote;
  73. }
  74. }
  75. }
  76. remove
  77. {
  78. lock (lockObject)
  79. {
  80. sessionChanged -= value;
  81. int decremented = Interlocked.Decrement(ref sessionChangedCount);
  82. if (decremented == 0)
  83. {
  84. try
  85. {
  86. SafeNativeMethods.WTSUnRegisterSessionNotification(messageControl.Handle);
  87. }
  88. catch (EntryPointNotFoundException)
  89. {
  90. }
  91. messageControl.Dispose();
  92. messageControl = null;
  93. }
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Determines whether the user is running within a remoted session (Terminal Server, Remote Desktop).
  99. /// </summary>
  100. /// <returns>
  101. /// <b>true</b> if we're running in a remote session, <b>false</b> otherwise.
  102. /// </returns>
  103. /// <remarks>
  104. /// You can use this to optimize the presentation of visual elements. Remote sessions
  105. /// are often bandwidth limited and less suitable for complex drawing.
  106. /// Note to implementors: This may be implemented as a no op; in this case, always return false.
  107. /// </remarks>
  108. public static bool IsRemote
  109. {
  110. get
  111. {
  112. return 0 != SafeNativeMethods.GetSystemMetrics(NativeConstants.SM_REMOTESESSION);
  113. }
  114. }
  115. private static void SessionStrobeHandler(object sender, EventArgs e)
  116. {
  117. if (IsRemote != lastRemoteSessionValue)
  118. {
  119. lastRemoteSessionValue = IsRemote;
  120. OnSessionChanged();
  121. }
  122. }
  123. }
  124. }