Server/Display.cs
author StephaneLenclud
Sun, 08 Jan 2017 12:19:35 +0100
changeset 278 2481c46d1f93
parent 123 0df386e37e29
permissions -rw-r--r--
Reading audio output is stopped when audio visualizers are not in used.
     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 using MiniDisplayInterop;
    30 
    31 namespace SharpDisplayManager
    32 {
    33 
    34 
    35     /// <summary>
    36     /// Provide access to our display hardware through MiniDisplay API.
    37     /// </summary>
    38     public class Display
    39     {
    40 		public delegate void OnOpenedHandler(Display aDisplay);
    41 		public event OnOpenedHandler OnOpened;
    42 
    43 		public delegate void OnClosedHandler(Display aDisplay);
    44 		public event OnClosedHandler OnClosed;
    45 
    46 		//Our display device handle
    47 		IntPtr iDevice;
    48 
    49 		//static functions
    50 		public static int TypeCount()
    51 		{
    52 			return MiniDisplay.TypeCount();
    53 		}
    54 
    55 		public static string TypeName(MiniDisplay.Type aType)
    56 		{
    57 			IntPtr ptr = MiniDisplay.TypeName(aType);
    58 			string str = Marshal.PtrToStringUni(ptr);
    59 			return str;
    60 		}
    61 
    62         //Constructor
    63         public Display()
    64         {
    65             iDevice = IntPtr.Zero;
    66         }
    67 
    68         //
    69         public bool Open(MiniDisplay.Type aType)
    70         {
    71 			if (IsOpen())
    72 			{
    73 				//Already open return an error
    74 				return false;
    75 			}
    76 
    77             iDevice = MiniDisplay.Open(aType);
    78 
    79             bool success = iDevice != IntPtr.Zero;
    80 			if (success)
    81 			{
    82 				//Broadcast opened event
    83 				OnOpened(this);
    84 			}
    85 
    86 			return success;
    87         }
    88 
    89         public void Close()
    90         {
    91 			if (!IsOpen())
    92 			{
    93 				//Pointless
    94 				return;
    95 			}
    96 
    97 			//
    98             MiniDisplay.Close(iDevice);
    99             iDevice = IntPtr.Zero;
   100 			//Broadcast closed event
   101 			OnClosed(this);
   102         }
   103 
   104         public bool IsOpen()
   105         {
   106             return iDevice != IntPtr.Zero;
   107         }
   108 
   109         public void Clear()
   110         {
   111             MiniDisplay.Clear(iDevice);
   112         }
   113 
   114         public void Fill()
   115         {
   116             MiniDisplay.Fill(iDevice);
   117         }
   118 
   119         public void SwapBuffers()
   120         {
   121             MiniDisplay.SwapBuffers(iDevice);
   122         }
   123 
   124         public int MaxBrightness()
   125         {
   126             return MiniDisplay.MaxBrightness(iDevice);
   127         }
   128 
   129         public int MinBrightness()
   130         {
   131             return MiniDisplay.MinBrightness(iDevice);
   132         }
   133 
   134         public void SetBrightness(int aBrightness)
   135         {
   136             if (!IsOpen()) return;
   137 
   138             MiniDisplay.SetBrightness(iDevice, aBrightness);
   139         }
   140 
   141         public int WidthInPixels()
   142         {
   143             return MiniDisplay.WidthInPixels(iDevice);
   144         }
   145 
   146         public int HeightInPixels()
   147         {
   148             return MiniDisplay.HeightInPixels(iDevice);
   149         }
   150 
   151         public void SetPixel(int aX, int aY, uint aValue)
   152         {
   153             MiniDisplay.SetPixel(iDevice,aX,aY,aValue);
   154         }
   155 
   156         public void RequestPowerSupplyStatus()
   157         {
   158             MiniDisplay.SendRequest(iDevice, MiniDisplay.Request.PowerSupplyStatus);
   159         }
   160 
   161         public void RequestDeviceId()
   162         {
   163             MiniDisplay.SendRequest(iDevice, MiniDisplay.Request.DeviceId);
   164         }
   165 
   166         public void RequestFirmwareRevision()
   167         {
   168             MiniDisplay.SendRequest(iDevice, MiniDisplay.Request.FirmwareRevision);
   169         }
   170 
   171         public void PowerOn()
   172         {
   173             MiniDisplay.PowerOn(iDevice);
   174         }
   175 
   176         public void PowerOff()
   177         {
   178             MiniDisplay.PowerOff(iDevice);
   179         }
   180 
   181         public bool SupportPowerOnOff()
   182         {
   183             return MiniDisplay.SupportPowerOnOff(iDevice);
   184         }
   185 
   186         public void ShowClock()
   187         {
   188             MiniDisplay.ShowClock(iDevice);
   189         }
   190 
   191         public void HideClock()
   192         {
   193             MiniDisplay.HideClock(iDevice);
   194         }
   195 
   196         public bool SupportClock()
   197         {
   198             return MiniDisplay.SupportClock(iDevice);
   199         }
   200 
   201         public bool PowerSupplyStatus()
   202         {
   203             bool res = MiniDisplay.PowerSupplyStatus(iDevice);
   204             return res;
   205         }
   206 
   207         public MiniDisplay.Request AttemptRequestCompletion()
   208         {
   209             return MiniDisplay.AttemptRequestCompletion(iDevice);
   210         }
   211 
   212         public MiniDisplay.Request CurrentRequest()
   213         {
   214             return MiniDisplay.CurrentRequest(iDevice);
   215         }
   216 
   217         public bool IsRequestPending()
   218         {
   219             return CurrentRequest() != MiniDisplay.Request.None;
   220         }
   221 
   222 		//
   223 		public int IconCount(MiniDisplay.IconType aIcon)
   224 		{
   225 			return MiniDisplay.IconCount(iDevice,aIcon);
   226 		}
   227 
   228 		public int IconStatusCount(MiniDisplay.IconType aIcon)
   229 		{
   230 			return MiniDisplay.IconStatusCount(iDevice, aIcon);
   231 		}
   232 
   233 		public void SetIconStatus(MiniDisplay.IconType aIcon, int aIndex, int aStatus)
   234 		{
   235 			MiniDisplay.SetIconStatus(iDevice, aIcon, aIndex, aStatus);
   236 		}
   237 
   238 		public void SetIconOn(MiniDisplay.IconType aIcon, int aIndex)
   239 		{
   240 			MiniDisplay.SetIconStatus(iDevice, aIcon, aIndex, IconStatusCount(aIcon) - 1);
   241 		}
   242 
   243 		public void SetIconOff(MiniDisplay.IconType aIcon, int aIndex)
   244 		{
   245 			MiniDisplay.SetIconStatus(iDevice, aIcon, aIndex, 0);
   246 		}
   247 
   248 
   249 		public void SetAllIconsStatus(int aStatus)
   250 		{
   251 			foreach (MiniDisplay.IconType icon in Enum.GetValues(typeof(MiniDisplay.IconType)))
   252 			{
   253 				int count=IconCount(icon);
   254 				for (int i = 0; i < count; i++)
   255 				{
   256 					SetIconStatus(icon,i,aStatus);
   257 				}
   258 			}
   259 		}
   260 
   261 		/// <summary>
   262 		/// Set all elements of an icon to the given status.
   263 		/// </summary>
   264 		/// <param name="aIcon"></param>
   265 		/// <param name="aStatus"></param>
   266 		public void SetIconStatus(MiniDisplay.IconType aIcon, int aStatus)
   267 		{
   268 			int iconCount = IconCount(aIcon);
   269 			for (int i = 0; i < iconCount; i++)
   270 			{
   271 				SetIconStatus(aIcon, i, aStatus);
   272 			}
   273 		}
   274 
   275 		/// <summary>
   276 		/// Set all elements of an icon to be either on or off.
   277 		/// </summary>
   278 		/// <param name="aIcon"></param>
   279 		/// <param name="aOn"></param>		
   280 		public void SetIconOnOff(MiniDisplay.IconType aIcon, bool aOn)
   281 		{
   282 			if (aOn)
   283 			{
   284 				SetIconOn(aIcon);
   285 			}
   286 			else
   287 			{
   288 				SetIconOff(aIcon);
   289 			}
   290 		}
   291 
   292 		/// <summary>
   293 		/// Set all elements of an icon to there maximum status.
   294 		/// </summary>
   295 		/// <param name="aIcon"></param>
   296 		public void SetIconOn(MiniDisplay.IconType aIcon)
   297 		{
   298 			int iconCount = IconCount(aIcon);
   299 			for (int i = 0; i < iconCount; i++)
   300 			{
   301 				SetIconStatus(aIcon, i, IconStatusCount(aIcon) - 1);
   302 			}
   303 		}
   304 
   305 		/// <summary>
   306 		/// Turn off all elements of an icon.
   307 		/// </summary>
   308 		/// <param name="aIcon"></param>
   309 		public void SetIconOff(MiniDisplay.IconType aIcon)
   310 		{
   311 			int iconCount = IconCount(aIcon);
   312 			for (int i = 0; i < iconCount; i++)
   313 			{
   314 				SetIconStatus(aIcon, i, 0);
   315 			}
   316 		}
   317 
   318 
   319 
   320         public string Vendor()
   321         {
   322             IntPtr ptr = MiniDisplay.Vendor(iDevice);
   323             string str = Marshal.PtrToStringUni(ptr);
   324             return str;
   325         }
   326 
   327         public string Product()
   328         {
   329             IntPtr ptr = MiniDisplay.Product(iDevice);
   330             string str = Marshal.PtrToStringUni(ptr);
   331             return str;
   332         }
   333 
   334         public string SerialNumber()
   335         {
   336             IntPtr ptr = MiniDisplay.SerialNumber(iDevice);
   337             string str = Marshal.PtrToStringUni(ptr);
   338             return str;
   339         }
   340 
   341         public string DeviceId()
   342         {
   343             IntPtr ptr = MiniDisplay.DeviceId(iDevice);
   344             string str = Marshal.PtrToStringAnsi(ptr);
   345             return str;
   346         }
   347 
   348         public string FirmwareRevision()
   349         {
   350             IntPtr ptr = MiniDisplay.FirmwareRevision(iDevice);
   351             string str = Marshal.PtrToStringAnsi(ptr);
   352             return str;
   353         }
   354 
   355 
   356     }
   357 }