Server/Display.cs
author StephaneLenclud
Tue, 17 Mar 2015 20:12:00 +0100
changeset 125 66a68098a4d1
parent 122 300f3d3114a8
child 135 2edc5da1796e
permissions -rw-r--r--
Adding missing source file.
Updating SharpLibHid.
Now Green Start works better when app in background.
     1 //
     2 // Copyright (C) 2014-2015 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpDisplayManager.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Collections.Generic;
    22 using System.Linq;
    23 using System.Text;
    24 using System.Threading.Tasks;
    25 //
    26 using System.Runtime.InteropServices;
    27 //using System.Runtime.Serialization;
    28 
    29 namespace SharpDisplayManager
    30 {
    31 
    32 
    33     /// <summary>
    34     /// Provide access to our display hardware through MiniDisplay API.
    35     /// </summary>
    36     public class Display
    37     {
    38 		public delegate void OnOpenedHandler(Display aDisplay);
    39 		public event OnOpenedHandler OnOpened;
    40 
    41 		public delegate void OnClosedHandler(Display aDisplay);
    42 		public event OnClosedHandler OnClosed;
    43 
    44 		//Our display device handle
    45 		IntPtr iDevice;
    46 
    47 		//static functions
    48 		public static int TypeCount()
    49 		{
    50 			return MiniDisplayTypeCount();
    51 		}
    52 
    53 		public static string TypeName(TMiniDisplayType aType)
    54 		{
    55 			IntPtr ptr = MiniDisplayTypeName(aType);
    56 			string str = Marshal.PtrToStringUni(ptr);
    57 			return str;
    58 		}
    59 
    60         //Constructor
    61         public Display()
    62         {
    63             iDevice = IntPtr.Zero;
    64         }
    65 
    66         //
    67         public bool Open(TMiniDisplayType aType)
    68         {
    69 			if (IsOpen())
    70 			{
    71 				//Already open return an error
    72 				return false;
    73 			}
    74 
    75             iDevice = MiniDisplayOpen(aType);
    76 
    77             bool success = iDevice != IntPtr.Zero;
    78 			if (success)
    79 			{
    80 				//Broadcast opened event
    81 				OnOpened(this);
    82 			}
    83 
    84 			return success;
    85         }
    86 
    87         public void Close()
    88         {
    89 			if (!IsOpen())
    90 			{
    91 				//Pointless
    92 				return;
    93 			}
    94 
    95 			//
    96             MiniDisplayClose(iDevice);
    97             iDevice = IntPtr.Zero;
    98 			//Broadcast closed event
    99 			OnClosed(this);
   100         }
   101 
   102         public bool IsOpen()
   103         {
   104             return iDevice != IntPtr.Zero;
   105         }
   106 
   107         public void Clear()
   108         {
   109             MiniDisplayClear(iDevice);
   110         }
   111 
   112         public void Fill()
   113         {
   114             MiniDisplayFill(iDevice);
   115         }
   116 
   117         public void SwapBuffers()
   118         {
   119             MiniDisplaySwapBuffers(iDevice);
   120         }
   121 
   122         public int MaxBrightness()
   123         {
   124             return MiniDisplayMaxBrightness(iDevice);
   125         }
   126 
   127         public int MinBrightness()
   128         {
   129             return MiniDisplayMinBrightness(iDevice);
   130         }
   131 
   132         public void SetBrightness(int aBrightness)
   133         {
   134             if (!IsOpen()) return;
   135 
   136             MiniDisplaySetBrightness(iDevice, aBrightness);
   137         }
   138 
   139         public int WidthInPixels()
   140         {
   141             return MiniDisplayWidthInPixels(iDevice);
   142         }
   143 
   144         public int HeightInPixels()
   145         {
   146             return MiniDisplayHeightInPixels(iDevice);
   147         }
   148 
   149         public void SetPixel(int aX, int aY, uint aValue)
   150         {
   151             MiniDisplaySetPixel(iDevice,aX,aY,aValue);
   152         }
   153 
   154         public void RequestPowerSupplyStatus()
   155         {
   156             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus);
   157         }
   158 
   159         public void RequestDeviceId()
   160         {
   161             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestDeviceId);
   162         }
   163 
   164         public void RequestFirmwareRevision()
   165         {
   166             MiniDisplayRequest(iDevice, TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision);
   167         }
   168 
   169         public void PowerOn()
   170         {
   171             MiniDisplayPowerOn(iDevice);
   172         }
   173 
   174         public void PowerOff()
   175         {
   176             MiniDisplayPowerOff(iDevice);
   177         }
   178 
   179         public bool SupportPowerOnOff()
   180         {
   181             return MiniDisplaySupportPowerOnOff(iDevice);
   182         }
   183 
   184         public void ShowClock()
   185         {
   186             MiniDisplayShowClock(iDevice);
   187         }
   188 
   189         public void HideClock()
   190         {
   191             MiniDisplayHideClock(iDevice);
   192         }
   193 
   194         public bool SupportClock()
   195         {
   196             return MiniDisplaySupportClock(iDevice);
   197         }
   198 
   199         public bool PowerSupplyStatus()
   200         {
   201             bool res = MiniDisplayPowerSupplyStatus(iDevice);
   202             return res;
   203         }
   204 
   205         public TMiniDisplayRequest AttemptRequestCompletion()
   206         {
   207             return MiniDisplayAttemptRequestCompletion(iDevice);
   208         }
   209 
   210         public TMiniDisplayRequest CurrentRequest()
   211         {
   212             return MiniDisplayCurrentRequest(iDevice);
   213         }
   214 
   215         public bool IsRequestPending()
   216         {
   217             return CurrentRequest() != TMiniDisplayRequest.EMiniDisplayRequestNone;
   218         }
   219 
   220 		//
   221 		public int IconCount(TMiniDisplayIconType aIcon)
   222 		{
   223 			return MiniDisplayIconCount(iDevice,aIcon);
   224 		}
   225 
   226 		public int IconStatusCount(TMiniDisplayIconType aIcon)
   227 		{
   228 			return MiniDisplayIconStatusCount(iDevice, aIcon);
   229 		}
   230 
   231 		public void SetIconStatus(TMiniDisplayIconType aIcon, int aIndex, int aStatus)
   232 		{
   233 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, aStatus);
   234 		}
   235 
   236 		public void SetIconOn(TMiniDisplayIconType aIcon, int aIndex)
   237 		{
   238 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, IconStatusCount(aIcon) - 1);
   239 		}
   240 
   241 		public void SetIconOff(TMiniDisplayIconType aIcon, int aIndex)
   242 		{
   243 			MiniDisplaySetIconStatus(iDevice, aIcon, aIndex, 0);
   244 		}
   245 
   246 
   247 		public void SetAllIconsStatus(int aStatus)
   248 		{
   249 			foreach (TMiniDisplayIconType icon in Enum.GetValues(typeof(TMiniDisplayIconType)))
   250 			{
   251 				int count=IconCount(icon);
   252 				for (int i = 0; i < count; i++)
   253 				{
   254 					SetIconStatus(icon,i,aStatus);
   255 				}
   256 			}
   257 		}
   258 
   259 		/// <summary>
   260 		/// Set all elements of an icon to the given status.
   261 		/// </summary>
   262 		/// <param name="aIcon"></param>
   263 		/// <param name="aStatus"></param>
   264 		public void SetIconStatus(TMiniDisplayIconType aIcon, int aStatus)
   265 		{
   266 			int iconCount = IconCount(aIcon);
   267 			for (int i = 0; i < iconCount; i++)
   268 			{
   269 				SetIconStatus(aIcon, i, aStatus);
   270 			}
   271 		}
   272 
   273 		/// <summary>
   274 		/// Set all elements of an icon to be either on or off.
   275 		/// </summary>
   276 		/// <param name="aIcon"></param>
   277 		/// <param name="aOn"></param>		
   278 		public void SetIconOnOff(TMiniDisplayIconType aIcon, bool aOn)
   279 		{
   280 			if (aOn)
   281 			{
   282 				SetIconOn(aIcon);
   283 			}
   284 			else
   285 			{
   286 				SetIconOff(aIcon);
   287 			}
   288 		}
   289 
   290 		/// <summary>
   291 		/// Set all elements of an icon to there maximum status.
   292 		/// </summary>
   293 		/// <param name="aIcon"></param>
   294 		public void SetIconOn(TMiniDisplayIconType aIcon)
   295 		{
   296 			int iconCount = IconCount(aIcon);
   297 			for (int i = 0; i < iconCount; i++)
   298 			{
   299 				SetIconStatus(aIcon, i, IconStatusCount(aIcon) - 1);
   300 			}
   301 		}
   302 
   303 		/// <summary>
   304 		/// Turn off all elements of an icon.
   305 		/// </summary>
   306 		/// <param name="aIcon"></param>
   307 		public void SetIconOff(TMiniDisplayIconType aIcon)
   308 		{
   309 			int iconCount = IconCount(aIcon);
   310 			for (int i = 0; i < iconCount; i++)
   311 			{
   312 				SetIconStatus(aIcon, i, 0);
   313 			}
   314 		}
   315 
   316 
   317 
   318         public string Vendor()
   319         {
   320             IntPtr ptr = MiniDisplayVendor(iDevice);
   321             string str = Marshal.PtrToStringUni(ptr);
   322             return str;
   323         }
   324 
   325         public string Product()
   326         {
   327             IntPtr ptr = MiniDisplayProduct(iDevice);
   328             string str = Marshal.PtrToStringUni(ptr);
   329             return str;
   330         }
   331 
   332         public string SerialNumber()
   333         {
   334             IntPtr ptr = MiniDisplaySerialNumber(iDevice);
   335             string str = Marshal.PtrToStringUni(ptr);
   336             return str;
   337         }
   338 
   339         public string DeviceId()
   340         {
   341             IntPtr ptr = MiniDisplayDeviceId(iDevice);
   342             string str = Marshal.PtrToStringAnsi(ptr);
   343             return str;
   344         }
   345 
   346         public string FirmwareRevision()
   347         {
   348             IntPtr ptr = MiniDisplayFirmwareRevision(iDevice);
   349             string str = Marshal.PtrToStringAnsi(ptr);
   350             return str;
   351         }
   352 
   353         //[Serializable]
   354         public enum TMiniDisplayType
   355         {
   356             EMiniDisplayAutoDetect, /*Not yet implemented*/
   357             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   358             EMiniDisplayFutabaGP1212A01,
   359             //[EnumMember(Value = "EMiniDisplayFutabaGP1212A01")]
   360             EMiniDisplayFutabaGP1212A02
   361         };
   362 
   363 		/// <summary>
   364 		/// 
   365 		/// </summary>
   366         public enum TMiniDisplayRequest
   367         {
   368             EMiniDisplayRequestNone,
   369             EMiniDisplayRequestDeviceId,
   370             EMiniDisplayRequestFirmwareRevision,
   371             EMiniDisplayRequestPowerSupplyStatus
   372         };
   373 
   374 			
   375 		/// <summary>
   376 		/// Define the various type of icons we support.
   377 		/// For binary compatibility new entries must be added at the end.
   378 		/// </summary>
   379 		public enum TMiniDisplayIconType
   380 		{
   381 			EMiniDisplayIconNetworkSignal=0,
   382 			EMiniDisplayIconInternet,
   383 			EMiniDisplayIconEmail,
   384 			EMiniDisplayIconMute,
   385 			EMiniDisplayIconVolume,
   386 			EMiniDisplayIconVolumeLabel,
   387 			EMiniDisplayIconPlay,
   388 			EMiniDisplayIconPause,
   389 			EMiniDisplayIconRecording
   390 		};
   391 
   392         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   393         public static extern IntPtr MiniDisplayOpen(TMiniDisplayType aType);
   394 
   395         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   396         public static extern void MiniDisplayClose(IntPtr aDevice);
   397 
   398 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   399 		public static extern int MiniDisplayTypeCount();
   400 
   401 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   402 		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
   403 
   404         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   405         public static extern void MiniDisplayClear(IntPtr aDevice);
   406 
   407         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   408         public static extern void MiniDisplayFill(IntPtr aDevice);
   409 
   410         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   411         public static extern void MiniDisplaySwapBuffers(IntPtr aDevice);
   412 
   413         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   414         public static extern void MiniDisplaySetBrightness(IntPtr aDevice, int aBrightness);
   415 
   416         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   417         public static extern int MiniDisplayMinBrightness(IntPtr aDevice);
   418 
   419         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   420         public static extern int MiniDisplayMaxBrightness(IntPtr aDevice);
   421 
   422         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   423         public static extern int MiniDisplayWidthInPixels(IntPtr aDevice);
   424 
   425         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   426         public static extern int MiniDisplayHeightInPixels(IntPtr aDevice);
   427 
   428         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   429         public static extern int MiniDisplaySetPixel(IntPtr aDevice, int aX, int aY, uint aValue);
   430 
   431         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   432         public static extern IntPtr MiniDisplayVendor(IntPtr aDevice);
   433 
   434         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   435         public static extern IntPtr MiniDisplayProduct(IntPtr aDevice);
   436 
   437         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   438         public static extern IntPtr MiniDisplaySerialNumber(IntPtr aDevice);
   439 
   440         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   441         public static extern IntPtr MiniDisplayDeviceId(IntPtr aDevice);
   442 
   443         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   444         public static extern IntPtr MiniDisplayFirmwareRevision(IntPtr aDevice);
   445 
   446         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   447         [return: MarshalAs(UnmanagedType.I1)]
   448         public static extern bool MiniDisplayPowerSupplyStatus(IntPtr aDevice);
   449 
   450         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   451         public static extern void MiniDisplayRequest(IntPtr aDevice, TMiniDisplayRequest aRequest);
   452 
   453         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   454         public static extern TMiniDisplayRequest MiniDisplayAttemptRequestCompletion(IntPtr aDevice);
   455 
   456         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   457         public static extern TMiniDisplayRequest MiniDisplayCurrentRequest(IntPtr aDevice);
   458 
   459         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   460         public static extern void MiniDisplayCancelRequest(IntPtr aDevice);
   461 
   462         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   463         public static extern void MiniDisplayPowerOn(IntPtr aDevice);
   464 
   465         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   466         public static extern void MiniDisplayPowerOff(IntPtr aDevice);
   467 
   468         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   469         [return: MarshalAs(UnmanagedType.I1)]
   470         public static extern bool MiniDisplaySupportPowerOnOff(IntPtr aDevice);
   471 
   472         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   473         public static extern void MiniDisplayShowClock(IntPtr aDevice);
   474 
   475         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   476         public static extern void MiniDisplayHideClock(IntPtr aDevice);
   477 
   478         [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   479         [return: MarshalAs(UnmanagedType.I1)]
   480         public static extern bool MiniDisplaySupportClock(IntPtr aDevice);
   481 
   482 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   483 		public static extern int MiniDisplayIconCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   484 
   485 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   486 		public static extern int MiniDisplayIconStatusCount(IntPtr aDevice, TMiniDisplayIconType aIcon);
   487 		
   488 		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   489 		public static extern void MiniDisplaySetIconStatus(IntPtr aDevice, TMiniDisplayIconType aIcon, int aIndex, int aStatus);
   490 
   491     }
   492 }