|
From: <sch...@us...> - 2008-12-04 10:06:45
|
Revision: 58
http://deraciel.svn.sourceforge.net/deraciel/?rev=58&view=rev
Author: schnippi001
Date: 2008-12-04 10:06:31 +0000 (Thu, 04 Dec 2008)
Log Message:
-----------
Made the keyboard reader synchronized to main program flow, removing pthread dependency.
Modified Paths:
--------------
trunk/CMakeLists.txt
trunk/cpp/CMakeLists.txt
trunk/cpp/main.cpp
trunk/keyboard.c
trunk/keyboard.h
trunk/main.c
Modified: trunk/CMakeLists.txt
===================================================================
--- trunk/CMakeLists.txt 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/CMakeLists.txt 2008-12-04 10:06:31 UTC (rev 58)
@@ -23,5 +23,4 @@
add_executable(deraciel ${DERACIEL_SOURCES})
target_link_libraries(deraciel
${NCURSES_LIBRARY}
- pthread
)
Modified: trunk/cpp/CMakeLists.txt
===================================================================
--- trunk/cpp/CMakeLists.txt 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/cpp/CMakeLists.txt 2008-12-04 10:06:31 UTC (rev 58)
@@ -23,5 +23,4 @@
add_executable(deraciel ${DERACIEL_SOURCES})
target_link_libraries(deraciel
${NCURSES_LIBRARY}
- pthread
)
Modified: trunk/cpp/main.cpp
===================================================================
--- trunk/cpp/main.cpp 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/cpp/main.cpp 2008-12-04 10:06:31 UTC (rev 58)
@@ -47,9 +47,6 @@
/* Start keyboard handler loop */
KeyboardReader::startLoop();
- doupdate();
- sleep(2);
-
finish(0);
return 0;
}
Modified: trunk/keyboard.c
===================================================================
--- trunk/keyboard.c 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/keyboard.c 2008-12-04 10:06:31 UTC (rev 58)
@@ -3,25 +3,24 @@
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>
-#include <pthread.h>
+#include "util.h"
-static pthread_t keyreader_thread;
static void (*handler_cb)(const int key);
-
+static bool keyreader_is_active = false;
/*
The keyreader is an infinite loop that gets one character at iteration and
passes it to a keyboard handling callback. The callback then can respond
appropriately to the given key. This function is running inside a thread
created by keyreader_create().
*/
-static void *keyreader(void *unused)
+static void keyreader()
{
/* This variable will hold the keycode of pressed key */
int key;
/* Run an infinite loop */
- while(1) {
+ while(keyreader_is_active) {
/* Fetch a key from keyboard buffer */
key = getch();
@@ -33,25 +32,27 @@
fprintf(stderr, "ERROR: Handler callback was NULL!\n");
}
}
-
- return NULL;
}
/*
Creates a thread that runs keyreader().
*/
-void keyreader_create()
+void keyreader_start()
{
- int rc = pthread_create(&keyreader_thread, NULL, keyreader, NULL);
- if (rc) {
- fprintf(stderr, "ERROR: Keyreader thread creation failed! (error code: %d)\n", rc);
- exit(-1);
- }
+ keyreader_is_active = true;
+ keyreader();
}
+void keyreader_stop()
+{
+ keyreader_is_active = false;
+}
+
+
+
void keyreader_set_handler(void(*cb)(const int key))
{
/* If the callback is not NULL set it as active keyboard handling function */
Modified: trunk/keyboard.h
===================================================================
--- trunk/keyboard.h 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/keyboard.h 2008-12-04 10:06:31 UTC (rev 58)
@@ -3,8 +3,8 @@
#include <stdint.h>
-void keyreader_create(void);
-
+void keyreader_start(void);
+void keyreader_stop(void);
void keyreader_set_handler(void(*cb)(const int key));
#endif // !DERACIEL_KEYBOARD_H
Modified: trunk/main.c
===================================================================
--- trunk/main.c 2008-11-27 20:13:02 UTC (rev 57)
+++ trunk/main.c 2008-12-04 10:06:31 UTC (rev 58)
@@ -58,12 +58,12 @@
invt_init(&main_char.inventory);
- /* Create keyboard handler */
- keyreader_create();
-
- /* Assign key handler callback */
+ /* Assign keyboard handler callback */
keyreader_set_handler(test_keyhandler);
+ /* Start keyboard reader */
+ keyreader_start();
+
/* Let thread work */
while(1) { sleep(5); };
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|