Zzz – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Diagnostics;
3 using System.Linq;
4 using System.Reflection;
5 using Serilog;
6  
7 namespace Zzz.Action
8 {
9 public class ActionTrigger : IDisposable
10 {
11 #region Public Events & Delegates
12  
13 public event EventHandler<ActionEventArgs> Action;
14  
15 #endregion
16  
17 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
18  
19 private readonly IntPtr _handle;
20  
21 #endregion
22  
23 #region Constructors, Destructors and Finalizers
24  
25 public ActionTrigger(IntPtr handle) : this()
26 {
27 _handle = handle;
28 }
29  
30 private ActionTrigger()
31 {
32 }
33  
34 public void Dispose()
35 {
36 }
37  
38 #endregion
39  
40 #region Public Methods
41  
42 public void Execute(ZzzAction zzzAction)
43 {
44 var action = Enum.GetName(typeof(Action), zzzAction.Action);
45 if (string.IsNullOrEmpty(action))
46 {
47 return;
48 }
49  
50 Execute(action);
51 }
52  
53 [Action]
54 public void Lock()
55 {
56 ActionHelper.LockWorkStation();
57 }
58  
59 [Action]
60 public void Dim()
61 {
62 Action?.Invoke(this, new ActionEventArgs(Zzz.Action.Action.Dim));
63  
64 ActionHelper.SendMessage((IntPtr)ActionHelper.HwndBroadcast, (uint)ActionHelper.WmSysCommand,
65 (UIntPtr) ActionHelper.ScMonitorPower,
66 (IntPtr) 2);
67 }
68  
69 [Action]
70 public void Hibernate()
71 {
72 Action?.Invoke(this, new ActionEventArgs(Zzz.Action.Action.Hibernate));
73  
74 ActionHelper.SetSuspendState(true, true, true);
75 }
76  
77 [Action]
78 public void Standby()
79 {
80 Action?.Invoke(this, new ActionEventArgs(Zzz.Action.Action.Standby));
81  
82 ActionHelper.SetSuspendState(false, true, true);
83 }
84  
85 [Action]
86 public void ShutDown()
87 {
88 Action?.Invoke(this, new ActionEventArgs(Zzz.Action.Action.Shutdown));
89  
90 const string seShutdownName = "SeShutdownPrivilege";
91 const short sePrivilegeEnabled = 2;
92 const uint ewxShutdown = 1;
93 const short tokenAdjustPrivileges = 32;
94 const short tokenQuery = 8;
95 ActionHelper.TokenPrivileges tkp;
96  
97 // Get shutdown privileges...
98 ActionHelper.OpenProcessToken(Process.GetCurrentProcess().Handle,
99 tokenAdjustPrivileges | tokenQuery, out var hToken);
100 tkp.PrivilegeCount = 1;
101 tkp.Privileges.Attributes = sePrivilegeEnabled;
102 ActionHelper.LookupPrivilegeValue("", seShutdownName, out tkp.Privileges.PLuid);
103 ActionHelper.AdjustTokenPrivileges(hToken, false, ref tkp, 0U, IntPtr.Zero,
104 IntPtr.Zero);
105  
106 // Now we have the privileges, shutdown Windows
107 ActionHelper.ExitWindowsEx(ewxShutdown, 0);
108 }
109  
110 #endregion
111  
112 #region Private Methods
113  
114 private void Execute(string action)
115 {
116 var type = GetType();
117 var methodInfo = type.GetMethods()
118 .FirstOrDefault(method => string.Equals(method.Name, action, StringComparison.OrdinalIgnoreCase));
119  
120 if (methodInfo == null)
121 {
122 Log.Warning($"Zzz action \"{action}\" could not be resolved.");
123  
124 return;
125 }
126  
127 var attribute = methodInfo.GetCustomAttribute(typeof(ActionAttribute));
128 if (attribute is ActionAttribute)
129 {
130 methodInfo.Invoke(this, null);
131 }
132 }
133  
134 #endregion
135 }
136 }