Exemplo n.º 1
0
        public static void EventLoop()
        {
            while (true)
            {
                IntPtr    ptr = mpv_wait_event(Handle, -1);
                mpv_event evt = (mpv_event)Marshal.PtrToStructure(ptr, typeof(mpv_event));

                if (WindowHandle == IntPtr.Zero)
                {
                    WindowHandle = FindWindowEx(MainForm.Hwnd, IntPtr.Zero, "mpv", null);
                }

                // Debug.WriteLine(evt.event_id.ToString());

                try
                {
                    switch (evt.event_id)
                    {
                    case mpv_event_id.MPV_EVENT_SHUTDOWN:
                        if (App.DebugMode)
                        {
                            Trace.WriteLine("before Shutdown.Invoke");
                        }
                        Shutdown?.Invoke();
                        if (App.DebugMode)
                        {
                            Trace.WriteLine("after Shutdown.Invoke");
                        }
                        WriteHistory(null);
                        ShutdownAutoResetEvent.Set();
                        return;

                    case mpv_event_id.MPV_EVENT_LOG_MESSAGE:
                        LogMessage?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_GET_PROPERTY_REPLY:
                        GetPropertyReply?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_SET_PROPERTY_REPLY:
                        SetPropertyReply?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_COMMAND_REPLY:
                        CommandReply?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_START_FILE:
                        StartFile?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_END_FILE:
                        var end_fileData        = (mpv_event_end_file)Marshal.PtrToStructure(evt.data, typeof(mpv_event_end_file));
                        EndFileEventMode reason = (EndFileEventMode)end_fileData.reason;
                        EndFile?.Invoke(reason);
                        break;

                    case mpv_event_id.MPV_EVENT_FILE_LOADED:
                        HideLogo();
                        FileLoaded?.Invoke();
                        WriteHistory(get_property_string("path"));
                        break;

                    case mpv_event_id.MPV_EVENT_TRACKS_CHANGED:
                        TracksChanged?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_TRACK_SWITCHED:
                        TrackSwitched?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_IDLE:
                        Idle?.Invoke();
                        if (get_property_int("playlist-count") == 0)
                        {
                            ShowLogo();
                        }
                        break;

                    case mpv_event_id.MPV_EVENT_PAUSE:
                        Pause?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_UNPAUSE:
                        Unpause?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_TICK:
                        Tick?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_SCRIPT_INPUT_DISPATCH:
                        ScriptInputDispatch?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_CLIENT_MESSAGE:
                        var      client_messageData = (mpv_event_client_message)Marshal.PtrToStructure(evt.data, typeof(mpv_event_client_message));
                        string[] args = NativeUtf8StrArray2ManagedStrArray(client_messageData.args, client_messageData.num_args);
                        if (args.Length > 1 && args[0] == "mpv.net")
                        {
                            Command.Execute(args[1], args.Skip(2).ToArray());
                        }
                        else if (args.Length > 0)
                        {
                            ClientMessage?.Invoke(args);
                        }
                        break;

                    case mpv_event_id.MPV_EVENT_VIDEO_RECONFIG:
                        VideoReconfig?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_AUDIO_RECONFIG:
                        AudioReconfig?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_METADATA_UPDATE:
                        MetadataUpdate?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_SEEK:
                        Seek?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_PROPERTY_CHANGE:
                        var propData = (mpv_event_property)Marshal.PtrToStructure(evt.data, typeof(mpv_event_property));

                        if (propData.format == mpv_format.MPV_FORMAT_FLAG)
                        {
                            foreach (var i in BoolPropChangeActions)
                            {
                                if (i.Key == propData.name)
                                {
                                    i.Value.Invoke(Marshal.PtrToStructure <int>(propData.data) == 1);
                                }
                            }
                        }

                        if (propData.format == mpv_format.MPV_FORMAT_STRING)
                        {
                            foreach (var i in StringPropChangeActions)
                            {
                                if (i.Key == propData.name)
                                {
                                    i.Value.Invoke(StringFromNativeUtf8(Marshal.PtrToStructure <IntPtr>(propData.data)));
                                }
                            }
                        }

                        if (propData.format == mpv_format.MPV_FORMAT_INT64)
                        {
                            foreach (var i in IntPropChangeActions)
                            {
                                if (i.Key == propData.name)
                                {
                                    i.Value.Invoke(Marshal.PtrToStructure <int>(propData.data));
                                }
                            }
                        }
                        break;

                    case mpv_event_id.MPV_EVENT_PLAYBACK_RESTART:
                        PlaybackRestart?.Invoke();
                        string path = get_property_string("path");
                        if (LastPlaybackRestartFile != path)
                        {
                            Size vidSize = new Size(get_property_int("dwidth"), get_property_int("dheight"));
                            if (vidSize.Width == 0 || vidSize.Height == 0)
                            {
                                vidSize = new Size(1, 1);
                            }
                            if (VideoSize != vidSize)
                            {
                                VideoSize = vidSize;
                                VideoSizeChanged?.Invoke();
                            }
                            VideoSizeAutoResetEvent.Set();
                            Task.Run(new Action(() => ReadMetaData()));
                            LastPlaybackRestartFile = path;
                        }
                        break;

                    case mpv_event_id.MPV_EVENT_CHAPTER_CHANGE:
                        ChapterChange?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_QUEUE_OVERFLOW:
                        QueueOverflow?.Invoke();
                        break;

                    case mpv_event_id.MPV_EVENT_HOOK:
                        Hook?.Invoke();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Msg.ShowException(ex);
                }
            }
        }
Exemplo n.º 2
0
Arquivo: mp.cs Projeto: amnek0/mpv.net
        public static void EventLoop()
        {
            while (true)
            {
                IntPtr    ptr = mpv_wait_event(MpvHandle, -1);
                mpv_event evt = (mpv_event)Marshal.PtrToStructure(ptr, typeof(mpv_event));

                if (MpvWindowHandle == IntPtr.Zero)
                {
                    MpvWindowHandle = FindWindowEx(MainForm.Hwnd, IntPtr.Zero, "mpv", null);
                }

                //Debug.WriteLine(evt.event_id.ToString());

                switch (evt.event_id)
                {
                case mpv_event_id.MPV_EVENT_SHUTDOWN:
                    Shutdown?.Invoke();
                    AutoResetEvent.Set();
                    return;

                case mpv_event_id.MPV_EVENT_LOG_MESSAGE:
                    LogMessage?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_GET_PROPERTY_REPLY:
                    GetPropertyReply?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_SET_PROPERTY_REPLY:
                    SetPropertyReply?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_COMMAND_REPLY:
                    CommandReply?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_START_FILE:
                    StartFile?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_END_FILE:
                    var end_fileData = (mpv_event_end_file)Marshal.PtrToStructure(evt.data, typeof(mpv_event_end_file));
                    EndFile?.Invoke((EndFileEventMode)end_fileData.reason);
                    break;

                case mpv_event_id.MPV_EVENT_FILE_LOADED:
                    FileLoaded?.Invoke();
                    LoadFolder();
                    break;

                case mpv_event_id.MPV_EVENT_TRACKS_CHANGED:
                    TracksChanged?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_TRACK_SWITCHED:
                    TrackSwitched?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_IDLE:
                    Idle?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_PAUSE:
                    Pause?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_UNPAUSE:
                    Unpause?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_TICK:
                    Tick?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_SCRIPT_INPUT_DISPATCH:
                    ScriptInputDispatch?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_CLIENT_MESSAGE:
                    if (ClientMessage != null)
                    {
                        try
                        {
                            var      client_messageData = (mpv_event_client_message)Marshal.PtrToStructure(evt.data, typeof(mpv_event_client_message));
                            string[] args = NativeUtf8StrArray2ManagedStrArray(client_messageData.args, client_messageData.num_args);

                            if (args != null && args.Length > 1 && args[0] == "mpv.net")
                            {
                                bool found = false;

                                foreach (var i in mpvnet.Command.Commands)
                                {
                                    if (args[1] == i.Name)
                                    {
                                        found = true;
                                        i.Action.Invoke(args.Skip(2).ToArray());
                                    }
                                }
                                if (!found)
                                {
                                    List <string> names = mpvnet.Command.Commands.Select((item) => item.Name).ToList();
                                    names.Sort();
                                    MainForm.Instance.ShowMsgBox($"No command '{args[1]}' found. Available commands are:\n\n{string.Join("\n", names)}\n\nHow to bind these commands can be seen in the default input bindings and menu definition located at:\n\nhttps://github.com/stax76/mpv.net/blob/master/mpv.net/Resources/input.conf.txt", MessageBoxIcon.Error);
                                }
                            }
                            ClientMessage?.Invoke(args);
                        }
                        catch (Exception ex)
                        {
                            MainForm.Instance.ShowMsgBox(ex.GetType().Name + "\n\n" + ex.ToString(), MessageBoxIcon.Error);
                        }
                    }
                    break;

                case mpv_event_id.MPV_EVENT_VIDEO_RECONFIG:
                    VideoReconfig?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_AUDIO_RECONFIG:
                    AudioReconfig?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_METADATA_UPDATE:
                    MetadataUpdate?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_SEEK:
                    Seek?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_PROPERTY_CHANGE:
                    var event_propertyData = (mpv_event_property)Marshal.PtrToStructure(evt.data, typeof(mpv_event_property));

                    if (event_propertyData.format == mpv_format.MPV_FORMAT_FLAG)
                    {
                        foreach (var i in BoolPropChangeActions)
                        {
                            if (i.Key == event_propertyData.name)
                            {
                                i.Value.Invoke(Marshal.PtrToStructure <int>(event_propertyData.data) == 1);
                            }
                        }
                    }
                    break;

                case mpv_event_id.MPV_EVENT_PLAYBACK_RESTART:
                    PlaybackRestart?.Invoke();
                    Size s = new Size(get_property_int("dwidth"), get_property_int("dheight"));

                    if (VideoSize != s && s != Size.Empty)
                    {
                        VideoSize = s;
                        VideoSizeChanged?.Invoke();
                    }
                    break;

                case mpv_event_id.MPV_EVENT_CHAPTER_CHANGE:
                    ChapterChange?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_QUEUE_OVERFLOW:
                    QueueOverflow?.Invoke();
                    break;

                case mpv_event_id.MPV_EVENT_HOOK:
                    Hook?.Invoke();
                    break;
                }
            }
        }