rainmeter-studio/Plugins/PluginInputText/Main.cs

209 lines
6.1 KiB
C#
Raw Normal View History

2013-03-01 07:26:05 +00:00
/*
Copyright (C) 2013 Rainmeter Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
2012-01-08 17:35:29 +00:00
using Rainmeter;
namespace InputText
{
2013-03-01 07:26:05 +00:00
internal partial class Measure
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
private Rainmeter.API rm;
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
private string LastInput = string.Empty;
private bool IsExecuteBangRunning = false;
private object locker = new object();
internal Measure(Rainmeter.API rm)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
this.rm = rm;
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
internal void Reload(Rainmeter.API rm, ref double maxValue)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
// Do nothing here.
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
internal double Update()
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
return 0.0;
}
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
internal string GetString()
{
lock (this.locker)
{
return this.LastInput;
}
}
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
internal void ExecuteBang(string args)
{
bool go = false;
lock (this.locker)
{
if (!this.IsExecuteBangRunning)
{
this.IsExecuteBangRunning = true;
go = true;
}
}
if (go)
{
ExecuteBangParam param = new ExecuteBangParam(args);
2013-03-05 19:03:43 +00:00
if (ReadOptions(param)) // Read all options in main thread for thread-safety
{
2013-03-05 19:03:43 +00:00
ThreadPool.QueueUserWorkItem(_ =>
{
try
{
ExecuteCommands(param);
}
catch (Exception ex)
{
API.Log(API.LogType.Error, "C# plugin in ExecuteBang(), " + ex.GetType().ToString() + ": " + ex.Message);
}
lock (this.locker)
{
this.IsExecuteBangRunning = false;
}
});
}
else
{
// No need to continue
lock (this.locker)
{
this.IsExecuteBangRunning = false;
}
}
2013-03-01 07:26:05 +00:00
}
2012-01-08 17:35:29 +00:00
}
2013-03-05 19:03:43 +00:00
private class ExecuteBangParam
{
internal enum BangType
{
Unknown,
SetVariable,
ExecuteBatch
};
internal Dictionary<string, string> Options;
internal List<Dictionary<string, string>> OverrideOptions;
internal List<string> Commands;
internal string Command;
2013-03-25 13:28:39 +00:00
internal string DismissAction;
internal BangType Type;
internal ExecuteBangParam(string args)
{
this.Options = new Dictionary<string, string>();
this.OverrideOptions = new List<Dictionary<string, string>>();
this.Commands = new List<string>();
this.Command = args.Trim();
2013-03-25 13:28:39 +00:00
this.DismissAction = null;
this.Type = BangType.Unknown;
}
};
2013-03-05 19:03:43 +00:00
private bool _IsFinalizing = false;
internal void Dispose()
{
2013-03-05 19:03:43 +00:00
this._IsFinalizing = true;
FinalizePluginCode();
}
2013-03-01 07:26:05 +00:00
}
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
#region Plugin
public static class Plugin
{
2014-01-23 09:59:33 +00:00
static IntPtr StringBuffer = IntPtr.Zero;
2012-01-08 17:35:29 +00:00
[DllExport]
2014-01-23 09:59:33 +00:00
public static void Initialize(ref IntPtr data, IntPtr rm)
2012-01-08 17:35:29 +00:00
{
2014-01-23 09:59:33 +00:00
data = GCHandle.ToIntPtr(GCHandle.Alloc(new Measure(new Rainmeter.API(rm))));
2012-01-08 17:35:29 +00:00
}
[DllExport]
2014-01-23 09:59:33 +00:00
public static void Finalize(IntPtr data)
2012-01-08 17:35:29 +00:00
{
2014-01-23 09:59:33 +00:00
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
measure.Dispose();
GCHandle.FromIntPtr(data).Free();
if (StringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(StringBuffer);
StringBuffer = IntPtr.Zero;
}
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
[DllExport]
2014-01-23 09:59:33 +00:00
public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
2013-03-01 07:26:05 +00:00
{
2014-01-23 09:59:33 +00:00
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
measure.Reload(new Rainmeter.API(rm), ref maxValue);
2013-03-01 07:26:05 +00:00
}
2012-01-08 17:35:29 +00:00
[DllExport]
2014-01-23 09:59:33 +00:00
public static double Update(IntPtr data)
2012-01-08 17:35:29 +00:00
{
2014-01-23 09:59:33 +00:00
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
return measure.Update();
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
[DllExport]
2014-01-23 09:59:33 +00:00
public static IntPtr GetString(IntPtr data)
2013-03-01 07:26:05 +00:00
{
2014-01-23 09:59:33 +00:00
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
if (StringBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(StringBuffer);
StringBuffer = IntPtr.Zero;
}
string stringValue = measure.GetString();
if (stringValue != null)
{
StringBuffer = Marshal.StringToHGlobalUni(stringValue);
}
return StringBuffer;
2013-03-01 07:26:05 +00:00
}
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
[DllExport]
2014-01-23 09:59:33 +00:00
public static void ExecuteBang(IntPtr data, IntPtr args)
2012-01-08 17:35:29 +00:00
{
2014-01-23 09:59:33 +00:00
Measure measure = (Measure)GCHandle.FromIntPtr(data).Target;
measure.ExecuteBang(Marshal.PtrToStringUni(args));
2012-01-08 17:35:29 +00:00
}
}
2013-03-01 07:26:05 +00:00
#endregion
2012-01-08 17:35:29 +00:00
}