Advertisement
Guest User

Cosmos Mouse Driver

a guest
Jul 1st, 2011
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Cosmos.Core;
  6. //using Cosmos.Kernel;
  7.  
  8. namespace Cosmos.Hardware
  9. {
  10.     /// <summary>
  11.     /// This class describes the mouse.
  12.     /// </summary>
  13.     public class Mouse
  14.     {
  15.         private Cosmos.Core.IOGroup.Mouse g = new Core.IOGroup.Mouse();
  16.  
  17.         /// <summary>
  18.         /// The X location of the mouse.
  19.         /// </summary>
  20.         public int X;
  21.         /// <summary>
  22.         /// The Y location of the mouse.
  23.         /// </summary>
  24.         public int Y;
  25.         /// <summary>
  26.         /// The state the mouse is currently in.
  27.         /// </summary>
  28.         public MouseState Buttons;
  29.  
  30.         /// <summary>
  31.         /// This is the required call to start
  32.         /// the mouse receiving interrupts.
  33.         /// </summary>
  34.         public void Initialize()
  35.         {
  36.             ////enable mouse
  37.             WaitSignal();
  38.             g.p64.Byte = (byte)0xA8;
  39.  
  40.             //// enable interrupt
  41.             WaitSignal();
  42.             g.p64.Byte = (byte)0x20;
  43.             WaitData();
  44.             //byte status1 = (byte)(g.p60.Byte);
  45.             byte status = (byte)(g.p60.Byte | 2);
  46.             WaitSignal();
  47.             g.p64.Byte = (byte)0x60;
  48.             WaitSignal();
  49.             g.p60.Byte = (byte)status;
  50.  
  51.             ////default
  52.             Write(0xF6);
  53.             Read();  //Acknowledge
  54.  
  55.             ////Enable the mouse
  56.             Write(0xF4);
  57.             Read();  //Acknowledge
  58.            
  59.             INTs.SetIrqHandler(12, HandleMouse);
  60.             //Console.WriteLine("INSTALLED");
  61.         }
  62.  
  63.         private byte Read()
  64.         {
  65.             WaitData();
  66.             return g.p60.Byte;
  67.         }
  68.  
  69.         private void Write(byte b)
  70.         {
  71.             WaitSignal();
  72.             g.p64.Byte = 0xD4;
  73.             WaitSignal();
  74.             g.p60.Byte = b;
  75.         }
  76.  
  77.         private void WaitData()
  78.         {
  79.             for (int i = 0; i < 100 & ((g.p64.Byte & 1) == 1); i++)
  80.                 ;
  81.         }
  82.  
  83.         private void WaitSignal()
  84.         {
  85.             for (int i = 0; i < 100 & ((g.p64.Byte & 2) != 0); i++)
  86.                 ;
  87.         }
  88.  
  89.         /// <summary>
  90.         /// The possible states of a mouse.
  91.         /// </summary>
  92.         public enum MouseState
  93.         {
  94.             /// <summary>
  95.             /// No button is pressed.
  96.             /// </summary>
  97.             None = 0,
  98.             /// <summary>
  99.             /// The left mouse button is pressed.
  100.             /// </summary>
  101.             Left = 1,
  102.             /// <summary>
  103.             /// The right mouse button is pressed.
  104.             /// </summary>
  105.             Right = 2,
  106.             /// <summary>
  107.             /// The middle mouse button is pressed.
  108.             /// </summary>
  109.             Middle = 4
  110.         }
  111.  
  112.  
  113.         private byte[] mouse_byte = new byte[4];
  114.         private static byte mouse_cycle = 0;
  115.  
  116.         public void HandleMouse(ref INTs.IRQContext context)
  117.         {
  118.             switch (mouse_cycle)
  119.             {
  120.                 case 0:
  121.                     mouse_byte[0] = Read();
  122.  
  123.                     //Bit 3 of byte 0 is 1, then we have a good package
  124.                     if ((mouse_byte[0] & 0x8) == 0x8)
  125.                         mouse_cycle++;
  126.  
  127.                     break;
  128.                 case 1:
  129.                     mouse_byte[1] = Read();
  130.                     mouse_cycle++;
  131.                     break;
  132.                 case 2:
  133.                     mouse_byte[2] = Read();
  134.                     mouse_cycle = 0;
  135.  
  136.                     if ((mouse_byte[0] & 0x10) == 0x10)
  137.                         X -= (mouse_byte[1] ^ 0xff);
  138.                     else
  139.                         X += mouse_byte[1];
  140.  
  141.                     if ((mouse_byte[0] & 0x20) == 0x20)
  142.                         Y += (mouse_byte[2] ^ 0xff);
  143.                     else
  144.                         Y -= mouse_byte[2];
  145.  
  146.                     if (X < 0)
  147.                         X = 0;
  148.                     else if (X > 319)
  149.                         X = 319;
  150.  
  151.                     if (Y < 0)
  152.                         Y = 0;
  153.                     else if (Y > 199)
  154.                         Y = 199;
  155.  
  156.                     Buttons = (MouseState)(mouse_byte[0] & 0x7);
  157.  
  158.                     break;
  159.             }
  160.              
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement