From c7f881c86913f309f00f79289b2f3c88ce6919eb Mon Sep 17 00:00:00 2001
From: a1xd <68629610+a1xd@users.noreply.github.com>
Date: Tue, 11 Aug 2020 23:15:02 -0400
Subject: define exceptions for invalid arg & io errors
---
common/accel-base.hpp | 17 ++++++++++++-----
common/accel-classic.hpp | 2 +-
common/accel-error.hpp | 11 -----------
common/accel-natural.hpp | 2 +-
common/accel-power.hpp | 4 ++--
common/accel-sigmoid.hpp | 4 ++--
common/common.vcxitems | 2 +-
common/rawaccel-error.hpp | 29 +++++++++++++++++++++++++++++
common/rawaccel-io.hpp | 9 ++++++---
common/rawaccel.hpp | 6 ++++--
10 files changed, 58 insertions(+), 28 deletions(-)
delete mode 100644 common/accel-error.hpp
create mode 100644 common/rawaccel-error.hpp
(limited to 'common')
diff --git a/common/accel-base.hpp b/common/accel-base.hpp
index da2c96b..91510a4 100644
--- a/common/accel-base.hpp
+++ b/common/accel-base.hpp
@@ -2,13 +2,20 @@
#include "vec2.h"
-namespace rawaccel {
+void bad_arg(const char*);
+
+#ifndef _KERNEL_MODE
- // Error throwing calls std libraries which are unavailable in kernel mode.
- void error(const char* s);
+#include "rawaccel-error.hpp"
- using milliseconds = double;
+inline void bad_arg(const char* s) {
+ throw rawaccel::invalid_argument(s);
+}
+#endif
+
+namespace rawaccel {
+
/// Struct to hold arguments for an acceleration function.
struct accel_args {
double offset = 0;
@@ -60,7 +67,7 @@ namespace rawaccel {
///
/// Arguments to verified.
void verify(const accel_args& args) const {
- if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate");
+ if (args.accel < 0) bad_arg("accel can not be negative, use a negative weight to compensate");
}
accel_base() = default;
diff --git a/common/accel-classic.hpp b/common/accel-classic.hpp
index 1a2adca..0a380dd 100644
--- a/common/accel-classic.hpp
+++ b/common/accel-classic.hpp
@@ -22,7 +22,7 @@ namespace rawaccel {
}
void verify(const accel_args& args) const {
- if (args.exponent <= 1) error("exponent must be greater than 1");
+ if (args.exponent <= 1) bad_arg("exponent must be greater than 1");
}
};
diff --git a/common/accel-error.hpp b/common/accel-error.hpp
deleted file mode 100644
index fa1f999..0000000
--- a/common/accel-error.hpp
+++ /dev/null
@@ -1,11 +0,0 @@
-#pragma once
-
-#include
-
-namespace rawaccel {
-
- void error(const char* s) {
- throw std::domain_error(s);
- }
-
-}
diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp
index c87fda8..8f002e4 100644
--- a/common/accel-natural.hpp
+++ b/common/accel-natural.hpp
@@ -24,7 +24,7 @@ namespace rawaccel {
}
void verify(const accel_args& args) const {
- if (args.limit <= 1) error("limit must be greater than 1");
+ if (args.limit <= 1) bad_arg("limit must be greater than 1");
}
};
diff --git a/common/accel-power.hpp b/common/accel-power.hpp
index 7f4c220..0d5e265 100644
--- a/common/accel-power.hpp
+++ b/common/accel-power.hpp
@@ -33,8 +33,8 @@ namespace rawaccel {
}
void verify(const accel_args& args) const {
- if (args.power_scale <= 0) error("scale must be positive");
- if (args.exponent <= 0) error("exponent must be greater than 0");
+ if (args.power_scale <= 0) bad_arg("scale must be positive");
+ if (args.exponent <= 0) bad_arg("exponent must be greater than 0");
}
};
diff --git a/common/accel-sigmoid.hpp b/common/accel-sigmoid.hpp
index 7cfa6c4..5bbe58f 100644
--- a/common/accel-sigmoid.hpp
+++ b/common/accel-sigmoid.hpp
@@ -24,8 +24,8 @@ namespace rawaccel {
}
void verify(const accel_args& args) const {
- if (args.limit <= 1) error("exponent must be greater than 1");
- if (args.midpoint < 0) error("midpoint must not be negative");
+ if (args.limit <= 1) bad_arg("exponent must be greater than 1");
+ if (args.midpoint < 0) bad_arg("midpoint must not be negative");
}
};
diff --git a/common/common.vcxitems b/common/common.vcxitems
index 7102164..9696ac6 100644
--- a/common/common.vcxitems
+++ b/common/common.vcxitems
@@ -22,7 +22,7 @@
-
+
diff --git a/common/rawaccel-error.hpp b/common/rawaccel-error.hpp
new file mode 100644
index 0000000..f5498f9
--- /dev/null
+++ b/common/rawaccel-error.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include
+
+namespace rawaccel {
+
+ class error : public std::runtime_error {
+ using std::runtime_error::runtime_error;
+ };
+
+ class invalid_argument : public error {
+ using error::error;
+ };
+
+ class io_error : public error {
+ using error::error;
+ };
+
+ class install_error : public io_error {
+ public:
+ install_error() : io_error("rawaccel is not installed") {}
+ };
+
+ class cooldown_error : public io_error {
+ public:
+ cooldown_error() : io_error("write is on cooldown") {}
+ };
+
+}
diff --git a/common/rawaccel-io.hpp b/common/rawaccel-io.hpp
index fc64c7d..7f55392 100644
--- a/common/rawaccel-io.hpp
+++ b/common/rawaccel-io.hpp
@@ -21,7 +21,7 @@ namespace rawaccel {
ra_handle = CreateFileW(L"\\\\.\\rawaccel", 0, 0, 0, OPEN_EXISTING, 0, 0);
if (ra_handle == INVALID_HANDLE_VALUE) {
- throw std::system_error(GetLastError(), std::system_category(), "CreateFile failed");
+ throw install_error();
}
mouse_modifier mod;
@@ -54,7 +54,7 @@ namespace rawaccel {
ra_handle = CreateFileW(L"\\\\.\\rawaccel", 0, 0, 0, OPEN_EXISTING, 0, 0);
if (ra_handle == INVALID_HANDLE_VALUE) {
- throw std::system_error(GetLastError(), std::system_category(), "CreateFile failed");
+ throw install_error();
}
DWORD dummy;
@@ -73,7 +73,10 @@ namespace rawaccel {
CloseHandle(ra_handle);
if (!success) {
- throw std::system_error(GetLastError(), std::system_category(), "DeviceIoControl failed");
+ if (auto err = GetLastError(); err != ERROR_BUSY) {
+ throw std::system_error(err, std::system_category(), "DeviceIoControl failed");
+ }
+ throw cooldown_error();
}
}
diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp
index 474f2aa..7aa1eb2 100644
--- a/common/rawaccel.hpp
+++ b/common/rawaccel.hpp
@@ -16,6 +16,8 @@
namespace rawaccel {
+ using milliseconds = double;
+
/// Struct to hold vector rotation details.
struct rotator {
@@ -104,8 +106,8 @@ namespace rawaccel {
vec2 clamp;
accel_function(const accel_fn_args& args) {
- if (args.time_min <= 0) error("min time must be positive");
- if (args.acc_args.offset < 0) error("offset must not be negative");
+ if (args.time_min <= 0) bad_arg("min time must be positive");
+ if (args.acc_args.offset < 0) bad_arg("offset must not be negative");
accel.tag = args.accel_mode;
accel.visit([&](auto& impl){ impl = { args.acc_args }; });
--
cgit v1.2.3