rainmeter-studio/Plugins/PluginInputText/Main.cs

188 lines
5.3 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<string> Commands;
internal string Command;
internal BangType Type;
internal ExecuteBangParam(string args)
{
this.Options = new Dictionary<string, string>();
this.Commands = new List<string>();
this.Command = args.Trim();
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
{
internal static Dictionary<uint, Measure> Measures = new Dictionary<uint, Measure>();
2012-01-08 17:35:29 +00:00
[DllExport]
2013-03-01 07:26:05 +00:00
public unsafe static void Initialize(void** data, void* rm)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
uint id = (uint)((void*)*data);
Measures.Add(id, new Measure(new Rainmeter.API((IntPtr)rm)));
2012-01-08 17:35:29 +00:00
}
[DllExport]
2013-03-01 07:26:05 +00:00
public unsafe static void Finalize(void* data)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
uint id = (uint)data;
Measures[id].Dispose();
2013-03-01 07:26:05 +00:00
Measures.Remove(id);
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
[DllExport]
public unsafe static void Reload(void* data, void* rm, double* maxValue)
{
uint id = (uint)data;
Measures[id].Reload(new Rainmeter.API((IntPtr)rm), ref *maxValue);
}
2012-01-08 17:35:29 +00:00
[DllExport]
2013-03-01 07:26:05 +00:00
public unsafe static double Update(void* data)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
uint id = (uint)data;
return Measures[id].Update();
2012-01-08 17:35:29 +00:00
}
2013-03-01 07:26:05 +00:00
[DllExport]
public unsafe static char* GetString(void* data)
{
uint id = (uint)data;
fixed (char* s = Measures[id].GetString()) return s;
}
2012-01-08 17:35:29 +00:00
2013-03-01 07:26:05 +00:00
[DllExport]
public unsafe static void ExecuteBang(void* data, char* args)
2012-01-08 17:35:29 +00:00
{
2013-03-01 07:26:05 +00:00
uint id = (uint)data;
Measures[id].ExecuteBang(new string(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
}