forked from computergeek1507/ColorlightPlugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xScheduleWrapper.cs
214 lines (185 loc) · 5.89 KB
/
xScheduleWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// *******************************************
//
// To build an xSchedule plugin do NOT touch this
// code. All changes should be made in Plugin.cs
//
// *******************************************
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
// requires package UnmanagedExports - version 1.6
// https://github.com/3F/DllExport
// https://github.com/3F/DllExport/wiki/Quick-start
namespace xScheduleWrapper
{
public class PixelBuffer
{
IntPtr _buffer { get; }
int _bufferSize;
public PixelBuffer(IntPtr buffer, int bufferSize)
{
_buffer = buffer;
_bufferSize = bufferSize;
}
int GetSize() { return _bufferSize; }
int GetPixels() { return _bufferSize / 3; }
public byte this[int index]
{
get
{
if (index >= _bufferSize) return 0;
return Marshal.ReadByte(_buffer + index);
}
set
{
if (index >= _bufferSize) return; // out of bounds
Marshal.WriteByte(_buffer + index, value);
}
}
public Color GetPixel(int index)
{
if (3 * index + 2 >= _bufferSize) return Color.Black;
return Color.FromArgb(this[index * 3],
this[index * 3 + 1],
this[index * 3] + 2);
}
public void SetPixel(int index, Color c)
{
if (3 * index + 2 >= _bufferSize) return;
this[3 * index] = c.R;
this[3 * index+1] = c.G;
this[3 * index+2] = c.B;
}
}
public static class xScheduleWrapper
{
static IntPtr _action;
static ColorlightPlugin.ColorlightPlugin _plugin = new ColorlightPlugin.ColorlightPlugin();
static void SetString(IntPtr buffer, int bufferSize, string value)
{
if (bufferSize <= value.Length)
{
Marshal.WriteByte(buffer, 0x00);
}
else
{
int i = 0;
foreach(char c in value)
{
Marshal.WriteByte(buffer + i, (byte)c);
i++;
}
Marshal.WriteByte(buffer + i, 0x00);
}
}
static void SetWString(IntPtr buffer, int bufferSize, string value)
{
if (bufferSize <= value.Length / 2)
{
Marshal.WriteInt16(buffer, 0x00);
}
else
{
int i = 0;
foreach (char c in value)
{
Marshal.WriteInt16(buffer + i, (byte)c);
i += 2;
}
Marshal.WriteInt16(buffer + i, 0x00);
}
}
//delegate void xSchedule_Action_Delegate(string command, string parameters, string data, out StringBuilder buffer, int bufferSize);
public delegate bool xSchedule_Action_Delegate(
[MarshalAs(UnmanagedType.LPStr)]string command,
[MarshalAs(UnmanagedType.LPWStr)]string parameters,
[MarshalAs(UnmanagedType.LPStr)]string data,
IntPtr buffer,
int bufferSize);
public static bool Do_xSchedule_Action(string command, string parameters, string data, out string buffer)
{
xSchedule_Action_Delegate p_xSchedule_Action = (xSchedule_Action_Delegate)Marshal.GetDelegateForFunctionPointer(_action, typeof(xSchedule_Action_Delegate));
int bufferSize = 4096;
IntPtr lpBuffer = Marshal.AllocHGlobal(bufferSize);
bool res = p_xSchedule_Action(command, parameters, data, lpBuffer, bufferSize);
buffer = Marshal.PtrToStringAnsi(lpBuffer);
// Free the buffer.
Marshal.FreeHGlobal(lpBuffer);
lpBuffer = IntPtr.Zero;
return res;
}
[DllExport("xSchedule_Load", CallingConvention = CallingConvention.StdCall)]
public static bool xSchedule_Load([MarshalAs(UnmanagedType.LPStr)]string showDir)
{
return _plugin.Load(showDir);
}
[DllExport("xSchedule_Unload", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_Unload()
{
_plugin.Unload();
}
[DllExport("xSchedule_GetVirtualWebFolder", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_GetVirtualWebFolder(IntPtr buffer, int bufferSize)
{
SetString(buffer, bufferSize, _plugin.GetWebFolder());
}
[DllExport("xSchedule_GetMenuLabel", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_GetMenuLabel(IntPtr buffer, int bufferSize)
{
SetString(buffer, bufferSize, _plugin.GetMenuString());
}
[DllExport("xSchedule_HandleWeb", CallingConvention = CallingConvention.StdCall)]
public static bool xSchedule_HandleWeb(
[MarshalAs(UnmanagedType.LPStr)]string command,
[MarshalAs(UnmanagedType.LPWStr)]string parameters,
[MarshalAs(UnmanagedType.LPWStr)]string data,
[MarshalAs(UnmanagedType.LPWStr)]string reference,
IntPtr response, int responseSize)
{
string resp;
bool res = _plugin.HandleWeb(command, parameters, data, reference, out resp);
SetWString(response, responseSize, resp);
return res;
}
[DllExport("xSchedule_Start", CallingConvention = CallingConvention.StdCall)]
public static bool xSchedule_Start([MarshalAs(UnmanagedType.LPStr)]string showDir, [MarshalAs(UnmanagedType.LPStr)]string xScheduleURL, IntPtr action)
{
_action = action;
try
{
return _plugin.Start(showDir);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return false;
}
[DllExport("xSchedule_Stop", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_Stop()
{
_plugin.Stop();
}
[DllExport("xSchedule_WipeSettings", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_WipeSettings()
{
_plugin.WipeSettings();
}
[DllExport("xSchedule_NotifyStatus", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_NotifyStatus([MarshalAs(UnmanagedType.LPStr)]string status)
{
_plugin.NotifyStatus(status);
}
[DllExport("xSchedule_FireEvent", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_FireEvent([MarshalAs(UnmanagedType.LPStr)]string type, [MarshalAs(UnmanagedType.LPStr)]string parameters)
{
_plugin.FireEvent(type, parameters);
}
[DllExport("xSchedule_ManipulateBuffer", CallingConvention = CallingConvention.StdCall)]
public static void xSchedule_ManipulateBuffer(IntPtr buffer, int bufferSize)
{
_plugin.ManipulateBuffer(new PixelBuffer(buffer, bufferSize));
}
}
}