/*
* Process Hacker -
* Plugin API
*
* Copyright (C) 2011 wj32
* Copyright (C) 2011 dmex
*
* This file is part of Process Hacker.
*
* Process Hacker 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 3 of the License, or
* (at your option) any later version.
*
* Process Hacker 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 Process Hacker. If not, see .
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ProcessHacker.Api
{
public delegate void SimplePluginDelegate(PluginInstance instance);
public delegate void GeneralGetHighlightingColorDelegate(PluginInstance instance, ref GeneralGetHighlightingColorArgs args);
public delegate void PluginParamsCallbackDelegate(PluginInstance instance, IntPtr Parameter, IntPtr Context);
public unsafe class PluginInstance
{
private static readonly List _registrations = new List();
public static PluginInstance Register(string name, string displayName, string author, string description, string url, bool hasOptions)
{
PhPluginInformation* info;
PhPlugin* plugin = NativeApi.PhRegisterPlugin(name, IntPtr.Zero, &info);
if (plugin != null)
{
info->DisplayName = (void*)NativeApi.StringToNativeUni(displayName);
info->Author = (void*)NativeApi.StringToNativeUni(author);
info->Description = (void*)NativeApi.StringToNativeUni(description);
info->Url = (void*)NativeApi.StringToNativeUni(url);
info->HasOptions = hasOptions;
return new PluginInstance(plugin);
}
return null;
}
public PhPlugin* Plugin { get; private set; }
private PluginInstance(PhPlugin* plugin)
{
this.Plugin = plugin;
}
public void RegisterGetProcessHighlightingColorHandler(GeneralGetHighlightingColorDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetGeneralCallback(PhGeneralCallback.GetProcessHighlightingColor),
(parameter, context) =>
{
PhPluginGetHighlightingColor* pargs = (PhPluginGetHighlightingColor*)parameter;
GeneralGetHighlightingColorArgs args;
args.Parameter = (IntPtr)pargs->Parameter;
args.BackColor = ColorTranslator.FromWin32(pargs->BackColor);
args.Handled = pargs->Handled;
args.Cache = pargs->Cache;
handler(this, ref args);
pargs->BackColor = ColorTranslator.ToWin32(args.BackColor);
pargs->Handled = args.Handled;
pargs->Cache = args.Cache;
}
);
_registrations.Add(registration);
}
///
/// Registers a callback for Plugin load notifications.
///
/// The delegate to invoke.
public void RegisterLoadHandler(SimplePluginDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetPluginCallback(this.Plugin, PhPluginCallback.Load),
(parameter, context) => handler(this)
);
_registrations.Add(registration);
}
///
/// Registers a callback for Plugin unload notifications.
///
/// The delegate to invoke.
public void RegisterUnLoadHandler(SimplePluginDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetPluginCallback(this.Plugin, PhPluginCallback.Unload),
(parameter, context) => handler(this)
);
_registrations.Add(registration);
}
///
/// Registers a callback for a (one-time) Main Window showing notification.
///
/// The delegate to invoke.
public void RegisterMainWindowShowingHandler(SimplePluginDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetGeneralCallback(PhGeneralCallback.MainWindowShowing),
(parameter, context) => handler(this)
);
_registrations.Add(registration);
}
///
/// Registers a callback for the Options button, found on the plugin Options Window.
///
///
/// Registering a Options Window handler enables the options button,
/// Allowing the user to easily configure your plugin.
///
/// The delegate to invoke.
public void RegisterOptionsWindowHandler(PluginParamsCallbackDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetPluginCallback(this.Plugin, PhPluginCallback.ShowOptions),
(parameter, context) => handler(this, parameter, context)
);
_registrations.Add(registration);
}
///
/// Registers a callback for interval update notifications.
///
///
/// Registering for Interval udpates allows you to use the interval settings
/// set by the user to update your display if required.
///
/// The delegate to invoke.
public void RegisterIntervalUpdateHandler(SimplePluginDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetGeneralCallback(PhGeneralCallback.IntervalUpdate),
(parameter, context) => handler(this)
);
_registrations.Add(registration);
}
///
/// Registers a Menu Item handler on the View menu.
///
/// The delegate to invoke.
public void RegisterMenuItemHandler(PluginParamsCallbackDelegate handler)
{
if (handler == null)
throw new InvalidOperationException("Delegate handler can not be null.");
CallbackRegistration registration = new CallbackRegistration(
NativeApi.PhGetPluginCallback(this.Plugin, PhPluginCallback.MenuItem),
(parameter, context) => handler(this, parameter, context)
);
_registrations.Add(registration);
}
}
}