Bladeren bron

Bring Posix-less poll

Miguel de Icaza 7 jaren geleden
bovenliggende
commit
715c385d82

+ 27 - 22
Terminal.Gui/Core.cs

@@ -75,7 +75,8 @@ namespace Terminal.Gui {
 		///     other View subclasses.
 		///   </para>
 		/// </remarks>
-		public virtual bool ProcessKey (KeyEvent kb)
+		/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
+		public virtual bool ProcessKey (KeyEvent keyEvent)
 		{
 			return false;
 		}
@@ -101,7 +102,8 @@ namespace Terminal.Gui {
 		///    keypress when they have the focus.
 		///  </para>
 		/// </remarks>
-		public virtual bool ProcessColdKey (KeyEvent kb)
+		/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
+		public virtual bool ProcessColdKey (KeyEvent keyEvent)
 		{
 			return false;
 		}
@@ -110,8 +112,8 @@ namespace Terminal.Gui {
 		/// Method invoked when a mouse event is generated
 		/// </summary>
 		/// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
-		/// <param name="me">Me.</param>
-		public virtual bool MouseEvent (MouseEvent me)
+		/// <param name="mouseEvent">Contains the details about the mouse event.</param>
+		public virtual bool MouseEvent (MouseEvent mouseEvent)
 		{
 			return false;
 		}
@@ -698,30 +700,33 @@ namespace Terminal.Gui {
 			focused.EnsureFocus ();
 		}
 
-		public override bool ProcessKey (KeyEvent kb)
+		/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
+		public override bool ProcessKey (KeyEvent keyEvent)
 		{
-			if (Focused?.ProcessKey (kb) == true)
+			if (Focused?.ProcessKey (keyEvent) == true)
 				return true;
 
 			return false;
 		}
 
-		public override bool ProcessHotKey (KeyEvent kb)
+		/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
+		public override bool ProcessHotKey (KeyEvent keyEvent)
 		{
 			if (subviews == null || subviews.Count == 0)
 				return false;
 			foreach (var view in subviews)
-				if (view.ProcessHotKey (kb))
+				if (view.ProcessHotKey (keyEvent))
 					return true;
 			return false;
 		}
 
-		public override bool ProcessColdKey (KeyEvent kb)
+		/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
+		public override bool ProcessColdKey (KeyEvent keyEvent)
 		{
 			if (subviews == null || subviews.Count == 0)
 				return false;
 			foreach (var view in subviews)
-				if (view.ProcessColdKey (kb))
+				if (view.ProcessColdKey (keyEvent))
 					return true;
 			return false;
 		}
@@ -916,12 +921,12 @@ namespace Terminal.Gui {
 			get => true;
 		}
 
-		public override bool ProcessKey (KeyEvent kb)
+		public override bool ProcessKey (KeyEvent keyEvent)
 		{
-			if (base.ProcessKey (kb))
+			if (base.ProcessKey (keyEvent))
 				return true;
 
-			switch (kb.Key) {
+			switch (keyEvent.Key) {
 			case Key.ControlC:
 				// TODO: stop current execution of this container
 				break;
@@ -1048,7 +1053,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// <remarks>
 		/// </remarks>
-		public virtual void Remove (View view)
+		public override void Remove (View view)
 		{
 			if (view == null)
 				return;
@@ -1088,7 +1093,7 @@ namespace Terminal.Gui {
 		// need to figure that out.
 		//
 		Point? dragPosition;
-		public override bool MouseEvent(MouseEvent me)
+		public override bool MouseEvent(MouseEvent mouseEvent)
 		{
 			// The code is currently disabled, because the 
 			// Driver.UncookMouse does not seem to have an effect if there is 
@@ -1096,11 +1101,11 @@ namespace Terminal.Gui {
 			if (true)
 				return false;
 			
-			if ((me.Flags == MouseFlags.Button1Pressed|| me.Flags == MouseFlags.Button4Pressed)){
+			if ((mouseEvent.Flags == MouseFlags.Button1Pressed|| mouseEvent.Flags == MouseFlags.Button4Pressed)){
 				
 				if (dragPosition.HasValue) {
-					var dx = me.X - dragPosition.Value.X;
-					var dy = me.Y - dragPosition.Value.Y;
+					var dx = mouseEvent.X - dragPosition.Value.X;
+					var dy = mouseEvent.Y - dragPosition.Value.Y;
 
 					var nx = Frame.X + dx;
 					var ny = Frame.Y + dy;
@@ -1110,7 +1115,7 @@ namespace Terminal.Gui {
 						ny = 0;
 
 					//Demo.ml2.Text = $"{dx},{dy}";
-					dragPosition = new Point (me.X, me.Y);
+					dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
 
 					// TODO: optimize, only SetNeedsDisplay on the before/after regions.
 					if (SuperView == null)
@@ -1122,8 +1127,8 @@ namespace Terminal.Gui {
 					return true;
 				} else {
 					// Only start grabbing if the user clicks on the title bar.
-					if (me.Y == 0) {
-						dragPosition = new Point (me.X, me.Y);
+					if (mouseEvent.Y == 0) {
+						dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
 						Application.GrabMouse (this);
 					}
 
@@ -1132,7 +1137,7 @@ namespace Terminal.Gui {
 				}
 			}
 
-			if (me.Flags == MouseFlags.Button1Released) {
+			if (mouseEvent.Flags == MouseFlags.Button1Released) {
 				Application.UngrabMouse ();
 				Driver.UncookMouse ();
 

+ 28 - 29
Terminal.Gui/MonoCurses/mainloop.cs

@@ -25,7 +25,6 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-using Mono.Unix.Native;
 using System.Collections.Generic;
 using System;
 using System.Runtime.InteropServices;
@@ -38,10 +37,10 @@ namespace Mono.Terminal {
 	/// </summary>
 	public class MainLoop {
 		/// <summary>
-		///   Condition on which to wake up from file descriptor activity
+		///   Condition on which to wake up from file descriptor activity.  These match the Linux/BSD poll definitions.
 		/// </summary>
 		[Flags]
-		public enum Condition {
+		public enum Condition : short {
 			/// <summary>
 			/// There is data to read
 			/// </summary>
@@ -49,11 +48,11 @@ namespace Mono.Terminal {
 			/// <summary>
 			/// Writing to the specified descriptor will not block
 			/// </summary>
-			PollOut = 2,
+			PollOut = 4,
 			/// <summary>
 			/// There is urgent data to read
 			/// </summary>
-			PollPri = 4,
+			PollPri = 2,
 			/// <summary>
 			///  Error condition on output
 			/// </summary>
@@ -82,7 +81,25 @@ namespace Mono.Terminal {
 		Dictionary <int, Watch> descriptorWatchers = new Dictionary<int,Watch>();
 		SortedList <double, Timeout> timeouts = new SortedList<double,Timeout> ();
 		List<Func<bool>> idleHandlers = new List<Func<bool>> ();
-		
+
+		[StructLayout(LayoutKind.Sequential)]
+		struct Pollfd {
+			public int fd;
+			public short events, revents;
+		}
+
+		[DllImport ("libc")]
+		extern static int poll ([In,Out]Pollfd[] ufds, uint nfds, int timeout);
+
+		[DllImport ("libc")]
+		extern static int pipe ([In,Out]int [] pipes);
+
+		[DllImport ("libc")]
+		extern static int read (int fd, IntPtr buf, IntPtr n);
+
+		[DllImport ("libc")]
+		extern static int write (int fd, IntPtr buf, IntPtr n);
+
 		Pollfd [] pollmap;
 		bool poll_dirty = true;
 		int [] wakeupPipes = new int [2];
@@ -93,16 +110,16 @@ namespace Mono.Terminal {
 		/// </summary>
 		public MainLoop ()
 		{
-			Syscall.pipe (wakeupPipes);
+			pipe (wakeupPipes);
 			AddWatch (wakeupPipes [0], Condition.PollIn, ml => {
-				Syscall.read (wakeupPipes [0], ignore, 1);
+					read (wakeupPipes [0], ignore, (IntPtr) 1);
 				return true;
 			});
 		}
 
 		void Wakeup ()
 		{
-			Syscall.write (wakeupPipes [1], ignore, 1);
+			write (wakeupPipes [1], ignore, (IntPtr) 1);
 		}
 		
 		/// <summary>
@@ -214,24 +231,6 @@ namespace Mono.Terminal {
 			timeouts.RemoveAt (idx);
 		}
 
-		static PollEvents MapCondition (Condition condition)
-		{
-			PollEvents ret = 0;
-			if ((condition & Condition.PollIn) != 0)
-				ret |= PollEvents.POLLIN;
-			if ((condition & Condition.PollOut) != 0)
-				ret |= PollEvents.POLLOUT;
-			if ((condition & Condition.PollPri) != 0)
-				ret |= PollEvents.POLLPRI;
-			if ((condition & Condition.PollErr) != 0)
-				ret |= PollEvents.POLLERR;
-			if ((condition & Condition.PollHup) != 0)
-				ret |= PollEvents.POLLHUP;
-			if ((condition & Condition.PollNval) != 0)
-				ret |= PollEvents.POLLNVAL;
-			return ret;
-		}
-		
 		void UpdatePollMap ()
 		{
 			if (!poll_dirty)
@@ -242,7 +241,7 @@ namespace Mono.Terminal {
 			int i = 0;
 			foreach (var fd in descriptorWatchers.Keys){
 				pollmap [i].fd = fd;
-				pollmap [i].events = MapCondition (descriptorWatchers [fd].Condition);
+				pollmap [i].events = (short) descriptorWatchers [fd].Condition;
 				i++;
 			}
 		}
@@ -310,7 +309,7 @@ namespace Mono.Terminal {
 			
 			UpdatePollMap ();
 
-			n = Syscall.poll (pollmap, (uint) pollmap.Length, pollTimeout);
+			n = poll (pollmap, (uint) pollmap.Length, pollTimeout);
 			int ic;
 			lock (idleHandlers)
 				ic = idleHandlers.Count;

+ 0 - 1
Terminal.Gui/Terminal.Gui.csproj

@@ -30,7 +30,6 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
-    <Reference Include="Mono.Posix" />
     <Reference Include="System.ValueTuple">
       <HintPath>..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll</HintPath>
     </Reference>

+ 16 - 6
Terminal.Gui/Views/Button.cs

@@ -61,7 +61,8 @@ namespace Terminal.Gui {
 		///   The size of the button is computed based on the
 		///   text length.   This button is not a default button.
 		/// </remarks>
-		public Button (string s) : this (0, 0, s) { }
+		/// <param name="text">The button's text</param>
+		public Button (string text) : this (0, 0, text) { }
 
 		/// <summary>
 		///   Public constructor, creates a button based on
@@ -72,7 +73,9 @@ namespace Terminal.Gui {
 		///   decoration is used, and the enter key on a
 		///   dialog would implicitly activate this button.
 		/// </remarks>
-		public Button (string s, bool is_default) : this (0, 0, s, is_default) { }
+		/// <param name="text">The button's text</param>
+		/// <param name="is_default">If set, this makes the button the default button in the current view, which means that if the user presses return on a view that does not handle return, it will be treated as if he had clicked on the button</param>
+		public Button (string text, bool is_default) : this (0, 0, text, is_default) { }
 
 		/// <summary>
 		///   Public constructor, creates a button based on
@@ -82,7 +85,10 @@ namespace Terminal.Gui {
 		///   The size of the button is computed based on the
 		///   text length.   This button is not a default button.
 		/// </remarks>
-		public Button (int x, int y, string s) : this (x, y, s, false) { }
+		/// <param name="x">X position where the button will be shown.</param>
+		/// <param name="y">Y position where the button will be shown.</param>
+		/// <param name="text">The button's text</param>
+		public Button (int x, int y, string text) : this (x, y, text, false) { }
 
 		/// <summary>
 		///   The text displayed by this widget.
@@ -128,13 +134,17 @@ namespace Terminal.Gui {
 		///   decoration is used, and the enter key on a
 		///   dialog would implicitly activate this button.
 		/// </remarks>
-		public Button (int x, int y, string s, bool is_default)
-		    : base (new Rect (x, y, s.Length + 4 + (is_default ? 2 : 0), 1))
+		/// <param name="x">X position where the button will be shown.</param>
+		/// <param name="y">Y position where the button will be shown.</param>
+		/// <param name="text">The button's text</param>
+		/// <param name="is_default">If set, this makes the button the default button in the current view, which means that if the user presses return on a view that does not handle return, it will be treated as if he had clicked on the button</param>
+		public Button (int x, int y, string text, bool is_default)
+		    : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
 		{
 			CanFocus = true;
 
 			this.IsDefault = is_default;
-			Text = s;
+			Text = text;
 		}
 
 		public override void Redraw (Rect region)

+ 1 - 1
Terminal.Gui/Views/FrameView.cs

@@ -69,7 +69,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// <remarks>
 		/// </remarks>
-		public virtual void Remove (View view)
+		public override void Remove (View view)
 		{
 			if (view == null)
 				return;

+ 1 - 1
Terminal.Gui/Views/ListView.cs

@@ -70,7 +70,7 @@ namespace Terminal.Gui {
 	///   and optionally mark elements of the list (controlled by the AllowsMark property).  
 	/// </para>
 	/// <para>
-	///   The ListView can either render an arbitrary IList object (for example, arrays, List<T>
+	///   The ListView can either render an arbitrary IList object (for example, arrays, List&lt;T&gt;
 	///   and other collections) which are drawn by drawing the string/ustring contents or the 
 	///   result of calling ToString().   Alternatively, you can provide you own IListDataSource
 	///   object that gives you full control of what is rendered.