From 42fbabe9d12d2a0a9c69a3b228850f5a93b7c470 Mon Sep 17 00:00:00 2001
From: yellows8 <yellows8@users.noreply.github.com>
Date: Wed, 15 Nov 2017 22:34:07 -0500
Subject: [PATCH] Added nvQueryEvent().

---
 nx/include/switch/services/nv.h |  1 +
 nx/source/services/nv.c         | 37 +++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/nx/include/switch/services/nv.h b/nx/include/switch/services/nv.h
index 5a8d974d..dac51169 100644
--- a/nx/include/switch/services/nv.h
+++ b/nx/include/switch/services/nv.h
@@ -12,4 +12,5 @@ void nvExit(void);
 Result nvOpen(u32 *fd, const char *devicepath);
 Result nvIoctl(u32 fd, u32 request, void* argp);
 Result nvClose(u32 fd);
+Result nvQueryEvent(u32 fd, u32 event_id, Handle *handle_out);
 
diff --git a/nx/source/services/nv.c b/nx/source/services/nv.c
index 84c43c35..fe9cdf7c 100644
--- a/nx/source/services/nv.c
+++ b/nx/source/services/nv.c
@@ -297,3 +297,40 @@ Result nvClose(u32 fd) {
     return rc;
 }
 
+Result nvQueryEvent(u32 fd, u32 event_id, Handle *handle_out) {
+    IpcCommand c;
+    ipcInitialize(&c);
+
+    struct {
+        u64 magic;
+        u64 cmd_id;
+        u32 fd;
+        u32 event_id;
+    } *raw;
+
+    raw = ipcPrepareHeader(&c, sizeof(*raw));
+    raw->magic = SFCI_MAGIC;
+    raw->cmd_id = 4;
+    raw->fd = fd;
+    raw->event_id = event_id;
+
+    Result rc = ipcDispatch(g_nvServiceSession);
+
+    if (R_SUCCEEDED(rc)) {
+        IpcCommandResponse r;
+        ipcParseResponse(&r);
+
+        struct {
+            u64 magic;
+            u64 result;
+            u32 error;
+        } *resp = r.Raw;
+
+        rc = resp->result;
+        if (R_SUCCEEDED(rc)) rc = resp->error;
+        if (R_SUCCEEDED(rc)) *handle_out = r.Handles[0];
+    }
+
+    return rc;
+}
+