Server/MainForm.cs
author sl
Thu, 14 Aug 2014 09:12:59 +0200
changeset 24 f442150ff735
parent 21 274a6b27c3f9
child 25 6f10207a89a8
permissions -rw-r--r--
Adding interface project.
sl@0
     1
using System;
sl@0
     2
using System.Collections.Generic;
sl@0
     3
using System.ComponentModel;
sl@0
     4
using System.Data;
sl@0
     5
using System.Drawing;
sl@0
     6
using System.Linq;
sl@0
     7
using System.Text;
sl@0
     8
using System.Threading.Tasks;
sl@0
     9
using System.Windows.Forms;
sl@14
    10
using System.IO;
sl@0
    11
using CodeProject.Dialog;
sl@14
    12
using System.Drawing.Imaging;
sl@17
    13
using System.ServiceModel;
sl@22
    14
using SharpDisplayInterface;
sl@14
    15
sl@0
    16
sl@0
    17
namespace SharpDisplayManager
sl@0
    18
{
sl@0
    19
    public partial class MainForm : Form
sl@0
    20
    {
sl@2
    21
        DateTime LastTickTime;
sl@3
    22
        Display iDisplay;
sl@14
    23
        System.Drawing.Bitmap iBmp;
sl@14
    24
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@17
    25
        ServiceHost iServiceHost;
sl@21
    26
        /// <summary>
sl@21
    27
        /// Our collection of clients
sl@21
    28
        /// </summary>
sl@21
    29
        public Dictionary<string, IDisplayServiceCallback> iClients;
sl@2
    30
sl@0
    31
        public MainForm()
sl@0
    32
        {
sl@2
    33
            LastTickTime = DateTime.Now;
sl@3
    34
            iDisplay = new Display();
sl@21
    35
            iClients = new Dictionary<string, IDisplayServiceCallback>();
sl@2
    36
sl@0
    37
            InitializeComponent();
sl@7
    38
            UpdateStatus();
sl@13
    39
sl@8
    40
            //Load settings
sl@8
    41
            marqueeLabelTop.Font = Properties.Settings.Default.DisplayFont;
sl@8
    42
            marqueeLabelBottom.Font = Properties.Settings.Default.DisplayFont;
sl@9
    43
            checkBoxShowBorders.Checked = Properties.Settings.Default.DisplayShowBorders;
sl@13
    44
            checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
sl@16
    45
            checkBoxReverseScreen.Checked = Properties.Settings.Default.DisplayReverseScreen;
sl@13
    46
            //
sl@13
    47
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@14
    48
            //We have a bug when drawing minimized and reusing our bitmap
sl@14
    49
            iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
    50
            iCreateBitmap = false;
sl@0
    51
        }
sl@0
    52
sl@13
    53
        private void MainForm_Load(object sender, EventArgs e)
sl@13
    54
        {
sl@17
    55
            StartServer();
sl@17
    56
sl@13
    57
            if (Properties.Settings.Default.DisplayConnectOnStartup)
sl@13
    58
            {
sl@13
    59
                iDisplay.Open();
sl@13
    60
                UpdateStatus();
sl@13
    61
            }
sl@13
    62
        }
sl@13
    63
sl@13
    64
sl@0
    65
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
    66
        {
sl@0
    67
            //fontDialog.ShowColor = true;
sl@0
    68
            //fontDialog.ShowApply = true;
sl@0
    69
            fontDialog.ShowEffects = true;
sl@11
    70
            fontDialog.Font = marqueeLabelTop.Font;
sl@0
    71
            //fontDialog.ShowHelp = true;
sl@0
    72
sl@0
    73
            //fontDlg.MaxSize = 40;
sl@0
    74
            //fontDlg.MinSize = 22;
sl@0
    75
sl@0
    76
            //fontDialog.Parent = this;
sl@0
    77
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
    78
sl@0
    79
            //DlgBox.ShowDialog(fontDialog);
sl@0
    80
sl@0
    81
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
    82
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
    83
            {
sl@0
    84
sl@4
    85
                //MsgBox.Show("MessageBox MsgBox", "MsgBox caption");
sl@0
    86
sl@0
    87
                //MessageBox.Show("Ok");
sl@4
    88
                marqueeLabelTop.Font = fontDialog.Font;
sl@4
    89
                marqueeLabelBottom.Font = fontDialog.Font;
sl@8
    90
                Properties.Settings.Default.DisplayFont = fontDialog.Font;
sl@8
    91
                Properties.Settings.Default.Save();
sl@0
    92
                //label1.Font = fontDlg.Font;
sl@0
    93
                //textBox1.BackColor = fontDlg.Color;
sl@0
    94
                //label1.ForeColor = fontDlg.Color;
sl@0
    95
            }
sl@0
    96
        }
sl@0
    97
sl@0
    98
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
    99
        {
sl@0
   100
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
sl@0
   101
            tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
sl@14
   102
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
   103
            bmp.Save("D:\\capture.png");
sl@14
   104
sl@17
   105
            marqueeLabelTop.Text = "Sweet";
sl@17
   106
sl@14
   107
            /*
sl@14
   108
            string outputFileName = "d:\\capture.png";
sl@14
   109
            using (MemoryStream memory = new MemoryStream())
sl@14
   110
            {
sl@14
   111
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
   112
                {
sl@14
   113
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
   114
                    byte[] bytes = memory.ToArray();
sl@14
   115
                    fs.Write(bytes, 0, bytes.Length);
sl@14
   116
                }
sl@14
   117
            }
sl@14
   118
             */
sl@14
   119
sl@0
   120
        }
sl@2
   121
sl@12
   122
        private void CheckForRequestResults()
sl@12
   123
        {
sl@12
   124
            if (iDisplay.IsRequestPending())
sl@12
   125
            {
sl@12
   126
                switch (iDisplay.AttemptRequestCompletion())
sl@12
   127
                {
sl@12
   128
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestPowerSupplyStatus:
sl@12
   129
                        if (iDisplay.PowerSupplyStatus())
sl@12
   130
                        {
sl@12
   131
                            toolStripStatusLabelPower.Text = "ON";
sl@12
   132
                        }
sl@12
   133
                        else
sl@12
   134
                        {
sl@12
   135
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
   136
                        }
sl@12
   137
                        //Issue next request then
sl@12
   138
                        iDisplay.RequestDeviceId();
sl@12
   139
                        break;
sl@12
   140
sl@12
   141
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestDeviceId:
sl@12
   142
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
   143
                        //Issue next request then
sl@12
   144
                        iDisplay.RequestFirmwareRevision();
sl@12
   145
                        break;
sl@12
   146
sl@12
   147
                    case Display.TMiniDisplayRequest.EMiniDisplayRequestFirmwareRevision:
sl@12
   148
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@12
   149
                        //No more request to issue
sl@12
   150
                        break;
sl@12
   151
                }
sl@12
   152
            }
sl@12
   153
        }
sl@12
   154
sl@16
   155
sl@16
   156
        public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
sl@16
   157
sl@16
   158
sl@16
   159
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   160
        {
sl@16
   161
            return aBmp.Width - aX - 1;
sl@16
   162
        }
sl@16
   163
sl@16
   164
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   165
        {
sl@16
   166
            return iBmp.Height - aY - 1;
sl@16
   167
        }
sl@16
   168
sl@16
   169
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
   170
        {
sl@16
   171
            return aX;
sl@16
   172
        }
sl@16
   173
sl@16
   174
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
   175
        {
sl@16
   176
            return aY;
sl@16
   177
        }
sl@16
   178
sl@16
   179
sl@16
   180
        //This is our timer tick responsible to perform our render
sl@2
   181
        private void timer_Tick(object sender, EventArgs e)
sl@14
   182
        {
sl@2
   183
            //Update our animations
sl@2
   184
            DateTime NewTickTime = DateTime.Now;
sl@2
   185
sl@2
   186
            marqueeLabelTop.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   187
            marqueeLabelBottom.UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   188
sl@4
   189
            //Update our display
sl@4
   190
            if (iDisplay.IsOpen())
sl@4
   191
            {
sl@12
   192
                CheckForRequestResults();
sl@12
   193
sl@22
   194
                //Draw to bitmap
sl@14
   195
                if (iCreateBitmap)
sl@14
   196
                {
sl@14
   197
                    iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   198
                }
sl@14
   199
                tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
sl@14
   200
                //iBmp.Save("D:\\capture.png");
sl@16
   201
sl@16
   202
                //Select proper coordinate translation functions
sl@16
   203
                //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@16
   204
                CoordinateTranslationDelegate screenX;
sl@16
   205
                CoordinateTranslationDelegate screenY;
sl@16
   206
sl@16
   207
                if (Properties.Settings.Default.DisplayReverseScreen)
sl@16
   208
                {
sl@16
   209
                    screenX = ScreenReversedX;
sl@16
   210
                    screenY = ScreenReversedY;
sl@16
   211
                }
sl@16
   212
                else
sl@16
   213
                {
sl@16
   214
                    screenX = ScreenX;
sl@16
   215
                    screenY = ScreenY;
sl@16
   216
                }
sl@22
   217
sl@7
   218
                //Send it to our display
sl@14
   219
                for (int i = 0; i < iBmp.Width; i++)
sl@4
   220
                {
sl@14
   221
                    for (int j = 0; j < iBmp.Height; j++)
sl@4
   222
                    {
sl@4
   223
                        unchecked
sl@4
   224
                        {
sl@14
   225
                            uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
sl@14
   226
                            //For some reason when the app is minimized in the task bar only the alpha of our color is set.
sl@14
   227
                            //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
sl@16
   228
                            iDisplay.SetPixel(screenX(iBmp, i), screenY(iBmp, j), Convert.ToInt32(!(color != 0xFF000000)));
sl@4
   229
                        }
sl@4
   230
                    }
sl@4
   231
                }
sl@4
   232
sl@4
   233
                iDisplay.SwapBuffers();
sl@4
   234
sl@4
   235
            }
sl@8
   236
sl@8
   237
            //Compute instant FPS
sl@8
   238
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " FPS";
sl@8
   239
sl@8
   240
            LastTickTime = NewTickTime;
sl@8
   241
sl@2
   242
        }
sl@3
   243
sl@3
   244
        private void buttonOpen_Click(object sender, EventArgs e)
sl@3
   245
        {
sl@3
   246
            if (iDisplay.Open())
sl@3
   247
            {
sl@7
   248
                UpdateStatus();
sl@12
   249
                iDisplay.RequestPowerSupplyStatus();
sl@3
   250
            }
sl@7
   251
            else
sl@7
   252
            {
sl@7
   253
                UpdateStatus();
sl@7
   254
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
   255
            }
sl@7
   256
sl@3
   257
        }
sl@3
   258
sl@3
   259
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
   260
        {
sl@3
   261
            iDisplay.Close();
sl@9
   262
            UpdateStatus();
sl@3
   263
        }
sl@3
   264
sl@3
   265
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
   266
        {
sl@3
   267
            iDisplay.Clear();
sl@3
   268
            iDisplay.SwapBuffers();
sl@3
   269
        }
sl@3
   270
sl@3
   271
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
   272
        {
sl@3
   273
            iDisplay.Fill();
sl@3
   274
            iDisplay.SwapBuffers();
sl@3
   275
        }
sl@3
   276
sl@3
   277
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
   278
        {
sl@9
   279
            Properties.Settings.Default.DisplayBrightness = trackBarBrightness.Value;
sl@9
   280
            Properties.Settings.Default.Save();
sl@3
   281
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
   282
sl@3
   283
        }
sl@7
   284
sl@7
   285
        private void UpdateStatus()
sl@7
   286
        {
sl@7
   287
            if (iDisplay.IsOpen())
sl@7
   288
            {
sl@7
   289
                buttonFill.Enabled = true;
sl@7
   290
                buttonClear.Enabled = true;
sl@7
   291
                buttonOpen.Enabled = false;
sl@7
   292
                buttonClose.Enabled = true;
sl@7
   293
                trackBarBrightness.Enabled = true;
sl@7
   294
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@11
   295
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
sl@9
   296
                trackBarBrightness.Value = Properties.Settings.Default.DisplayBrightness;
sl@9
   297
                trackBarBrightness.LargeChange = Math.Max(1,(iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@9
   298
                trackBarBrightness.SmallChange = 1;
sl@9
   299
                iDisplay.SetBrightness(Properties.Settings.Default.DisplayBrightness);
sl@9
   300
sl@10
   301
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
   302
                //+ " - " + iDisplay.SerialNumber();
sl@7
   303
            }
sl@7
   304
            else
sl@7
   305
            {
sl@7
   306
                buttonFill.Enabled = false;
sl@7
   307
                buttonClear.Enabled = false;
sl@7
   308
                buttonOpen.Enabled = true;
sl@7
   309
                buttonClose.Enabled = false;
sl@7
   310
                trackBarBrightness.Enabled = false;
sl@9
   311
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@7
   312
            }
sl@7
   313
        }
sl@9
   314
sl@13
   315
sl@13
   316
sl@9
   317
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
   318
        {
sl@16
   319
            //Save our show borders setting
sl@13
   320
            tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
sl@9
   321
            Properties.Settings.Default.DisplayShowBorders = checkBoxShowBorders.Checked;
sl@9
   322
            Properties.Settings.Default.Save();
sl@9
   323
        }
sl@13
   324
sl@13
   325
        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
sl@13
   326
        {
sl@16
   327
            //Save our connect on startup setting
sl@13
   328
            Properties.Settings.Default.DisplayConnectOnStartup = checkBoxConnectOnStartup.Checked;
sl@13
   329
            Properties.Settings.Default.Save();
sl@13
   330
        }
sl@13
   331
sl@16
   332
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
   333
        {
sl@16
   334
            //Save our reverse screen setting
sl@16
   335
            Properties.Settings.Default.DisplayReverseScreen = checkBoxReverseScreen.Checked;
sl@16
   336
            Properties.Settings.Default.Save();
sl@16
   337
        }
sl@16
   338
sl@14
   339
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
   340
        {
sl@14
   341
            if (WindowState == FormWindowState.Minimized)
sl@14
   342
            {
sl@14
   343
                // Do some stuff
sl@14
   344
                //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
sl@14
   345
                iCreateBitmap = true;
sl@14
   346
            }
sl@14
   347
        }
sl@14
   348
sl@17
   349
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
   350
        {
sl@17
   351
            StopServer();
sl@17
   352
        }
sl@17
   353
sl@17
   354
        public void StartServer()
sl@17
   355
        {
sl@17
   356
            iServiceHost = new ServiceHost
sl@17
   357
                (
sl@17
   358
                    typeof(DisplayServer),
sl@20
   359
                    new Uri[] { new Uri("net.tcp://localhost:8001/") }
sl@17
   360
                );
sl@17
   361
sl@20
   362
            iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetTcpBinding(), "DisplayService");
sl@17
   363
            iServiceHost.Open();
sl@17
   364
        }
sl@17
   365
sl@17
   366
        public void StopServer()
sl@17
   367
        {
sl@17
   368
            //Tell connected client first? Is that possible?
sl@21
   369
            BroadcastCloseEvent();
sl@17
   370
            iServiceHost.Close();
sl@17
   371
        }
sl@17
   372
sl@21
   373
        public void BroadcastCloseEvent()
sl@21
   374
        {
sl@21
   375
            var inactiveClients = new List<string>();
sl@21
   376
            foreach (var client in iClients)
sl@21
   377
            {
sl@21
   378
                //if (client.Key != eventData.ClientName)
sl@21
   379
                {
sl@21
   380
                    try
sl@21
   381
                    {
sl@21
   382
                        client.Value.OnServerClosing(/*eventData*/);
sl@21
   383
                    }
sl@21
   384
                    catch (Exception ex)
sl@21
   385
                    {
sl@21
   386
                        inactiveClients.Add(client.Key);
sl@21
   387
                    }
sl@21
   388
                }
sl@21
   389
            }
sl@21
   390
sl@21
   391
            if (inactiveClients.Count > 0)
sl@21
   392
            {
sl@21
   393
                foreach (var client in inactiveClients)
sl@21
   394
                {
sl@21
   395
                    iClients.Remove(client);
sl@21
   396
                }
sl@21
   397
            }
sl@21
   398
        }
sl@21
   399
sl@21
   400
sl@17
   401
sl@0
   402
    }
sl@0
   403
}