<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[James's Programming Page — Create an event when attach/detach removable drive HidSharp Linux]]></title>
		<link>https://swforum.seekye.com/topic/10113/</link>
		<atom:link href="https://swforum.seekye.com/feed/rss/topic/10113/" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Create an event when attach/detach removable drive HidSharp Linux.]]></description>
		<lastBuildDate>Tue, 18 Dec 2018 08:15:01 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Create an event when attach/detach removable drive HidSharp Linux]]></title>
			<link>https://swforum.seekye.com/post/13020/#p13020</link>
			<description><![CDATA[<p>Thank you for your reply! I think it&#039;s not a HID device, just regular removable drive. I think I need somethink like this:<br /></p><div class="codebox"><pre><code>protected override void Run(Action readyCallback)
ret = NativeMethodsLibudev.Instance.udev_monitor_filter_add_match_subsystem_devtype(monitor, &quot;hidraw&quot;, null);</code></pre></div><p>Am I right?<br />So, then I should try to add action from udev:<br /></p><div class="codebox"><pre><code>var device = udev_monitor_receive_device(monitor);
                try {
                    var action = udev_device_get_action(device);
                    var syspath = udev_device_get_syspath(device);
                    switch (action) {
                    case &quot;add&quot;:
                        if (syspath != null) {
                            paths.Add(syspath);
                        }
                        break;
                    case &quot;remove&quot;:
                        paths.Remove(syspath);
                        break;
                    }</code></pre></div><p>But I dont know, how to throw action result to main program.</p><p>PS: In my case&nbsp; events in my first post does not work with any HID devices on Win10.<br />PSS:<br />I try:<br /></p><div class="codebox"><pre><code>class LinuxHidManager 
    {
        IntPtr udev;
        IntPtr monitor;
        IList&lt;string&gt; paths = new SynchronizedCollection&lt;string&gt;(); // System.ServiceModel

        public void Init()
        {
            // TODO: should probably do some better error checking here
            udev = udev_new();

            Console.WriteLine(&quot;Path udev = udev_new();&quot;);

            monitor = udev_monitor_new_from_netlink(udev, &quot;udev&quot;);
            udev_monitor_filter_add_match_subsystem_devtype(monitor, &quot;hidraw&quot;, null);
            udev_monitor_enable_receiving(monitor);
            var enumerate = udev_enumerate_new(udev);
            try
            {
                udev_enumerate_add_match_subsystem(enumerate, &quot;hidraw&quot;);
                udev_enumerate_scan_devices(enumerate);
                for (var entry = udev_enumerate_get_list_entry(enumerate);
                     entry != IntPtr.Zero;
                     entry = udev_list_entry_get_next(entry))
                {
                    var syspath = udev_list_entry_get_name(entry);
                    if (syspath != null)
                    {
                        paths.Add(syspath);
                    }
                }

                Console.WriteLine(&quot;Path udev_enumerate_add_match_subsystem&quot;);
            }
            finally
            {
                udev_enumerate_unref(enumerate);
            }
            // FIXME: Need to implement IDisposeable and free udev and monitor
        }

        public string Runing()
        {
            Console.WriteLine(&quot;Entered in Runing()&quot;);

            string action;
            var fds = new pollfd[1];
            fds[0].fd = udev_monitor_get_fd(monitor);
            fds[0].events = pollev.IN;
            Console.WriteLine(&quot;Pass fds[0].events = pollev.IN;&quot;);
            while (true)
            {
                int ret = retry(() =&gt; poll(fds, (IntPtr)fds.Length, -1)); // What happened here??? Program will freeze!!!
                Console.WriteLine(&quot;Pass в retry(() =&gt; poll;&quot;);
                if (ret == -1)
                {
                    Console.WriteLine(&quot;Something went wrong&quot;);
                    // FIXME: how do we notify the main program that something bad happened here?
                    break;
                }
                var device = udev_monitor_receive_device(monitor);
                try
                {
                    Console.WriteLine(&quot;Catch an action&quot;);
                    action = udev_device_get_action(device);
                    var syspath = udev_device_get_syspath(device);
                    Console.WriteLine(syspath);
                    switch (action)
                    {
                        case &quot;add&quot;:
                            if (syspath != null)
                            {
                                paths.Add(syspath);
                            }
                            break;
                        case &quot;remove&quot;:
                            paths.Remove(syspath);
                            break;
                    }
                    Console.WriteLine(&quot;What message in action&quot;);
                    

                    return action;
                }
                finally
                {
                    udev_device_unref(device);
                }
            }

            return &quot;Test&quot;;
        }

        protected object[] Refresh()
        {
            return paths.Cast&lt;object&gt;().ToArray();
        }

        public bool IsSupported
        {
            get
            {
                // basically, we are just testing if libudev is present
                try
                {
                    var udev = NativeMethods.udev_new();
                    if (udev == IntPtr.Zero)
                    {
                        return false;
                    }
                    NativeMethods.udev_unref(udev);
                    return true;
                }
                catch (DllNotFoundException)
                {
                    return false;
                }
            }
        }
    }
    
    
  // ++++++++++++++++++++++++ MAIN (TEST) ++++++++++++++++++++++++ 
  class Program
    {
        static LinuxHidManager ld = new LinuxHidManager();
        private static string Message { get; set; }

        static void Main(string[] args)
        {
            
            Console.WriteLine(&quot;++++++++++++++++++++++++++++&quot;);
            Init();
            Console.ReadLine();
        }

        private static void Init()
        {
            ld.Init();
            Message = ld.Runing();</code></pre></div><p>I use only NativeMethods and LinuxHidManger classes for test. And I want to get action messages from udev... I&#039;ve added Console.WriteLine for debugging line by line...</p>]]></description>
			<author><![CDATA[null@example.com (Jman)]]></author>
			<pubDate>Tue, 18 Dec 2018 08:15:01 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13020/#p13020</guid>
		</item>
		<item>
			<title><![CDATA[Re: Create an event when attach/detach removable drive HidSharp Linux]]></title>
			<link>https://swforum.seekye.com/post/13019/#p13019</link>
			<description><![CDATA[<p>Hmm. Is the removable drive also a USB HID device? If not, you&#039;d probably want to use the udev API or similar. (Internally, HidSharp uses udev, but only to listen for HID device changes.)</p>]]></description>
			<author><![CDATA[null@example.com (Zer)]]></author>
			<pubDate>Mon, 17 Dec 2018 16:05:38 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13019/#p13019</guid>
		</item>
		<item>
			<title><![CDATA[Create an event when attach/detach removable drive HidSharp Linux]]></title>
			<link>https://swforum.seekye.com/post/13016/#p13016</link>
			<description><![CDATA[<p>Hi all!<br />I try to use nuget package HidSharp 2.0.5 for catch an event when removable drive is connected/disconnected. I&#039;m debugging on Windows now, but my code is does not catch any hid devices connection events. I would be run this app at Linux in future via Mono. Can anyone help me please?</p><div class="codebox"><pre><code>static void Main(string[] args)
    {
        var list = DeviceList.Local;

        Console.WriteLine(&quot;USB monitor&quot;);

        list.Changed += (sender, e) =&gt; Console.WriteLine(&quot;Device list changed.&quot;);

        var hidDeviceList = list.GetHidDevices().ToArray();

        Console.WriteLine(&quot;Complete device list {0} devices:&quot;, hidDeviceList.Length);

        foreach (var device in hidDeviceList)
        {
            Console.WriteLine(device);
        }

        Console.ReadLine();
    }</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Jman)]]></author>
			<pubDate>Mon, 17 Dec 2018 10:01:00 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13016/#p13016</guid>
		</item>
	</channel>
</rss>
