3 using System.Runtime.CompilerServices;
4 using System.Runtime.InteropServices;
10 /// Handles recent screen shot of launched applications
15 /// Main window of the application
17 private IntPtr nativeWindow;
20 /// A path of storage for recent screen shot of launched application
22 private string storagePath;
25 /// A width of recent screen shot
27 private int imageWidth;
30 /// A height of recent screen shot
32 private int imageHeight;
35 /// A flag indicates whether updating recent screen shot or not
37 public bool SkipUpdateFlag;
40 /// Callbacks of sniper events
42 private static InterOp.SniperCallback callbacks;
45 /// A EventHandler handles recent screen shot update event
47 public event EventHandler<Event> UpdatedEvent;
50 /// A EventHandler handles add or remove application
52 public event EventHandler<Event> AddRemoveEvent;
55 /// A EventHandler handles skip update event
57 public event EventHandler<Event> SkipUpdateEvent;
60 /// A event argument class for app screen update notification.
62 public class Event : EventArgs
65 public string InstanceId;
70 /// A method for writing debug log
72 /// <param name="message">A log message</param>
73 /// <param name="file">A path of caller file</param>
74 /// <param name="func">A name of caller function</param>
75 /// <param name="line">A line number of caller line</param>
76 private void WriteLog(string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
78 Log.Debug("sniper", message);
82 /// A method for handling launched application
84 /// <param name="appId">An ID of launched application</param>
85 /// <param name="instanceId">An instance ID of launched application</param>
86 private void AddedCallback(string appId, string instanceId)
88 EventHandler<Event> handler = AddRemoveEvent;
91 WriteLog("Added " + appId + " " + instanceId);
100 eventInfo = new Event();
104 WriteLog("Updated Exception : " + e.Message);
108 eventInfo.AppId = appId;
109 eventInfo.InstanceId = instanceId;
110 eventInfo.Info = "Added";
112 handler(this, eventInfo);
116 /// A method for handling terminated application
118 /// <param name="appId">An ID of terminated application</param>
119 /// <param name="instanceId">An instance ID of terminated application</param>
120 private void RemovedCallback(string appId, string instanceId)
122 EventHandler<Event> handler = AddRemoveEvent;
125 WriteLog("Removed " + appId + " " + instanceId);
134 eventInfo = new Event();
138 WriteLog("Updated Exception : " + e.Message);
142 eventInfo.AppId = appId;
143 eventInfo.InstanceId = instanceId;
144 eventInfo.Info = "Removed";
146 handler(this, eventInfo);
150 /// A method for handling application screen is updated
152 /// <param name="appId">An ID of application that screen is updated</param>
153 /// <param name="instanceId">An instance ID of application that screen is updated</param>
154 /// <param name="Filename">A path of application screen shot</param>
155 private void UpdatedCallback(string appId, string instanceId, string Filename)
157 EventHandler<Event> handler = UpdatedEvent;
160 WriteLog("Updated " + appId + " " + instanceId + " " + Filename);
169 eventInfo = new Event();
173 WriteLog("Updated Exception : " + e.Message);
177 eventInfo.Info = Filename;
178 eventInfo.AppId = appId;
179 eventInfo.InstanceId = instanceId;
181 handler(this, eventInfo);
185 /// A method for handling screen update is skipped
187 /// <param name="appId">An ID of application that screen update is skipped</param>
188 /// <param name="instanceId">An instance ID of application that screen update is skipped</param>
189 /// <param name="Filename">A path of application screen shot</param>
190 /// <returns>Returns finish code</returns>
191 private int SkipUpdateCallback(string appId, string instanceId, string Filename)
193 EventHandler<Event> handler = SkipUpdateEvent;
196 WriteLog("SkipUpdate" + appId + " " + instanceId + " " + Filename);
205 eventInfo = new Event();
209 WriteLog("SkipUpdated Exception : " + e.Message);
213 eventInfo.Info = Filename;
214 eventInfo.AppId = appId;
215 eventInfo.InstanceId = instanceId;
217 handler(this, eventInfo);
221 WriteLog("Update is skipped : " + Filename);
222 SkipUpdateFlag = false;
232 /// <param name="window">Main window of this application</param>
233 /// <param name="path">Storage path</param>
234 /// <param name="width">Screen shot width</param>
235 /// <param name="height">Screen shot height</param>
236 public Sniper(IntPtr window, string path, int width, int height)
238 nativeWindow = window;
241 imageHeight = height;
242 SkipUpdateFlag = false;
246 /// A method for starting monitoring
247 /// Adds callbacks and initialize Sniper class
249 public void StartMonitor()
253 callbacks = new InterOp.SniperCallback();
254 callbacks.Added = new InterOp.CallbackAddedRemoved(AddedCallback);
255 callbacks.Removed = new InterOp.CallbackAddedRemoved(RemovedCallback);
256 callbacks.Updated = new InterOp.CallbackUpdated(UpdatedCallback);
257 callbacks.SkipUpdate = new InterOp.CallbackSkipUpdate(SkipUpdateCallback);
261 throw new SniperException(e.Message);
266 InterOp.sniper_init(nativeWindow, callbacks, storagePath, imageWidth, imageHeight);
268 catch (DllNotFoundException e)
270 WriteLog("Loadable library is not found " + e.StackTrace);
273 WriteLog("Sniper starts monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);
277 /// A method stops monitoring
279 public void StopMonitor()
283 InterOp.sniper_fini();
285 catch (DllNotFoundException e)
287 WriteLog("Loadable library is not found " + e.StackTrace);
290 WriteLog("Sniper stops monitoring : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);
294 /// A method requests updating application screen shot
296 /// <param name="instanceId">An instance ID of application</param>
297 public void RequestUpdate(string instanceId)
301 InterOp.sniper_request_update(instanceId);
303 catch (DllNotFoundException e)
305 WriteLog("Loadable library is not found " + e.StackTrace);
308 WriteLog("Sniper requests update (" + instanceId + ") : " + storagePath + "ImageSize : " + imageWidth + "x" + imageHeight);