aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2015-06-19 19:15:27 +0100
committerTomasz Kramkowski <tk@the-tk.com>2015-06-19 19:15:27 +0100
commita72bf3a44e8002181b2818ab557434a2a0a22c96 (patch)
tree30580e9c6a6d4389f62ef400153cc4c999ca926d
parent0dc36e29dd1e65b22745ffe4b348047e36f38d1b (diff)
downloadc-stuff-a72bf3a44e8002181b2818ab557434a2a0a22c96.tar.gz
c-stuff-a72bf3a44e8002181b2818ab557434a2a0a22c96.tar.xz
c-stuff-a72bf3a44e8002181b2818ab557434a2a0a22c96.zip
nkey.c; time_diff.c
-rw-r--r--nkey.c51
-rw-r--r--time_diff.c60
2 files changed, 111 insertions, 0 deletions
diff --git a/nkey.c b/nkey.c
new file mode 100644
index 0000000..de92d66
--- /dev/null
+++ b/nkey.c
@@ -0,0 +1,51 @@
+#include <ncurses.h>
+#include <stdio.h>
+
+int getch_nodelay(void)
+{
+ int retval;
+
+ nodelay(stdscr, TRUE);
+ retval = getch();
+ nodelay(stdscr, FALSE);
+
+ return retval;
+}
+
+int main(void)
+{
+ cbreak();
+ initscr();
+ noecho();
+ nonl();
+
+ keypad(stdscr, TRUE);
+
+ while (1) {
+ char number[32];
+ int c[5] = {
+ getch(),
+ getch_nodelay(),
+ getch_nodelay(),
+ getch_nodelay(),
+ getch_nodelay()
+ };
+
+ if (c[1] == ERR)
+ snprintf(number, 32, "%d ", c[0]);
+ else if (c[2] == ERR)
+ snprintf(number, 32, "%d-%d ", c[0], c[1]);
+ else if (c[3] == ERR)
+ snprintf(number, 32, "%d-%d-%d ", c[0], c[1], c[2]);
+ else if (c[4] == ERR)
+ snprintf(number, 32, "%d-%d-%d-%d ", c[0], c[1], c[2],
+ c[3]);
+ else if (c[5] == ERR)
+ snprintf(number, 32, "%d-%d-%d-%d-%d ", c[0], c[1],
+ c[2], c[3], c[4]);
+
+ addstr(number);
+ }
+
+ return 0;
+}
diff --git a/time_diff.c b/time_diff.c
new file mode 100644
index 0000000..726a6cd
--- /dev/null
+++ b/time_diff.c
@@ -0,0 +1,60 @@
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+const char *progname;
+
+inline uint64_t get_time_ms(void)
+{
+ struct timespec timespec;
+ static const uint64_t invmil = 1 / 1000000;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &timespec) != 0) {
+ fprintf(stderr, "%s: Unable to get time delta: %s\n",
+ progname, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ return timespec.tv_sec * 1000 + timespec.tv_nsec * invmil;
+}
+
+uint64_t get_delta(void)
+{
+ static uint64_t oldtime = 0;
+ uint64_t timediff, newtime;
+
+ newtime = get_time_ms();
+
+ timediff = newtime - oldtime;
+
+ oldtime = newtime;
+
+ return timediff;
+}
+
+int main(int argc, char **argv)
+{
+ struct {
+ int a;
+ char b;
+ } teststruct = {1, 2};
+
+ uint64_t buildup = 0, count = 0;
+
+ progname = argc != 0 && argv[0] != NULL ? argv[0] : "";
+
+ get_delta();
+
+ while (true) {
+ for (buildup += get_delta(); buildup >= 1000; buildup -= 1000)
+ printf("%lu, %lu\n", count++, buildup);
+ }
+
+ printf("%d\n", teststruct.a);
+
+ return 0;
+}