Permalink
Browse files

initial support for node 0.6.x (using libuv), not fully working yet

  • Loading branch information...
1 parent fd11b9f commit 5d9165892e7bf58000eb72404d2e2111fd0fbc44 @japj committed Apr 12, 2012
Showing with 124 additions and 11 deletions.
  1. +4 −0 CHANGES
  2. +12 −1 sdlmixer.js
  3. 0 src/{async.h → async_ev.h}
  4. +75 −0 src/async_uv.h
  5. +23 −3 src/node-sdlmixer.cc
  6. +5 −2 src/node-sdlmixer.h
  7. +3 −3 test/simple.js
  8. +2 −2 test/test.js
View
4 CHANGES
@@ -1,3 +1,7 @@
+Unreleased
+* node 0.6.x (libuv) support
+ 'known' issue atm: SEGV at end of program (not good!)
+
2011.05.14, Version 0.0.9
* changed npm package to 'sdlmixer'
View
13 sdlmixer.js
@@ -1,3 +1,14 @@
-var sdlmixer_lib = require('./build/default/node-sdlmixer');
+var sdlmixer_lib;
+var node_version = process.version.split('.');
+if ((node_version[0] == "v0") && (node_version[1] == "4"))
+{
+// console.log("v0.4.x");
+ sdlmixer_lib = require('./build/default/node-sdlmixer');
+}
+else
+{
+// console.log("!v0.4.x");
+ sdlmixer_lib = require('./build/Release/node-sdlmixer');
+}
module.exports = new sdlmixer_lib.SDLMixer();
View
0 src/async.h → src/async_ev.h
File renamed without changes.
View
75 src/async_uv.h
@@ -0,0 +1,75 @@
+#ifndef NODE_SQLITE3_SRC_ASYNC_H
+#define NODE_SQLITE3_SRC_ASYNC_H
+
+
+// Generic uv_async handler.
+template <class Item, class Parent> class Async {
+ typedef void (*Callback)(Parent* parent, Item* item);
+
+protected:
+ uv_async_t watcher;
+ pthread_mutex_t mutex;
+ std::vector<Item*> data;
+ Callback callback;
+public:
+ Parent* parent;
+
+public:
+ Async(Parent* parent_, Callback cb_)
+ : callback(cb_), parent(parent_) {
+ watcher.data = this;
+ pthread_mutex_init(&mutex, NULL);
+ uv_async_init(uv_default_loop(), &watcher, listener);
+ }
+
+ static void listener(uv_async_t* handle, int status) {
+ Async* async = static_cast<Async*>(handle->data);
+ std::vector<Item*> rows;
+ pthread_mutex_lock(&async->mutex);
+ rows.swap(async->data);
+ pthread_mutex_unlock(&async->mutex);
+ for (unsigned int i = 0, size = rows.size(); i < size; i++) {
+ uv_unref(uv_default_loop());
+ async->callback(async->parent, rows[i]);
+ }
+ }
+
+ static void close(uv_handle_t* handle) {
+ assert(handle != NULL);
+ assert(handle->data != NULL);
+ Async* async = static_cast<Async*>(handle->data);
+ delete async;
+ handle->data = NULL;
+ }
+
+ void finish() {
+ // Need to call the listener again to ensure all items have been
+ // processed. Is this a bug in uv_async? Feels like uv_close
+ // should handle that.
+ listener(&watcher, 0);
+ uv_close((uv_handle_t*)&watcher, close);
+ }
+
+ void add(Item* item) {
+ // Make sure node runs long enough to deliver the messages.
+ uv_ref(uv_default_loop());
+ pthread_mutex_lock(&mutex);
+ data.push_back(item);
+ pthread_mutex_unlock(&mutex);
+ }
+
+ void send() {
+ uv_async_send(&watcher);
+ }
+
+ void send(Item* item) {
+ add(item);
+ send();
+ }
+
+ ~Async() {
+ pthread_mutex_destroy(&mutex);
+ }
+};
+
+#endif
View
26 src/node-sdlmixer.cc
@@ -30,7 +30,20 @@ static void releaseAudioChannel(int channel) {
availableChannels.push_back(channel);
}
-static int DoPlay(eio_req *req) {
+#ifdef UV_VERSION_MAJOR
+static void UV_Play(uv_work_t *req)
+{
+ struct playInfo * pi = (struct playInfo *) req->data;
+
+ /* Load the requested wave file */
+ pi->wave = Mix_LoadWAV(pi->name);
+
+ //printf("Playing [%s] on channel[%d]\n", pi->name, pi->channel);
+ /* Play and then exit */
+ Mix_PlayChannel(pi->channel, pi->wave, 0);
+}
+#else
+static int EIO_Play(eio_req *req) {
struct playInfo * pi = (struct playInfo *) req->data;
/* Load the requested wave file */
@@ -43,12 +56,13 @@ static int DoPlay(eio_req *req) {
return 0;
}
-int SDLMixer::NotifyPlayed(eio_req *req) {
+int EIO_AfterPlay(eio_req *req) {
HandleScope scope;
ev_unref( EV_DEFAULT_UC);
//printf("SDLMixer::NotifyPlayed\n");
return 0;
}
+#endif
Handle<Value> SDLMixer::Play(const Arguments& args) {
HandleScope scope;
@@ -86,8 +100,14 @@ Handle<Value> SDLMixer::Play(const Arguments& args) {
playDoneEvent = new AsyncPlayDone(sm, PlayDoneCallback);
}
- eio_custom(DoPlay, EIO_PRI_DEFAULT, NotifyPlayed, pi);
+#ifdef UV_VERSION_MAJOR
+ uv_work_t* work_req = (uv_work_t *) (calloc(1, sizeof(uv_work_t)));
+ work_req->data = pi;
+ uv_queue_work(uv_default_loop(), work_req, UV_Play, NULL);
+#else
+ eio_custom(EIO_Play, EIO_PRI_DEFAULT, EIO_AfterPlay, pi);
ev_ref( EV_DEFAULT_UC);
+#endif
return scope.Close(args[0]);
}
View
7 src/node-sdlmixer.h
@@ -11,7 +11,11 @@
#include "SDL_mixer.h"
#include <pthread.h>
-#include "async.h"
+#ifdef UV_VERSION_MAJOR
+#include "async_uv.h"
+#else
+#include "async_ev.h"
+#endif
using namespace v8;
using namespace node;
@@ -36,7 +40,6 @@ class SDLMixer: public ObjectWrap {
typedef Async<playInfo, SDLMixer> AsyncPlayDone;
protected:
- static int NotifyPlayed(eio_req *req);
static Handle<Value> Play(const Arguments& args);
static void ChannelFinished(int channel);
View
6 test/simple.js
@@ -1,6 +1,6 @@
var sdlmixer = require("../sdlmixer"),
- sys = require("sys"),
- puts = sys.puts;
+ util = require("util"),
+ puts = util.puts;
puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + " bit " +
sdlmixer.audioChannels + " and " + sdlmixer.numberOfAudioChannels + " channels.")
@@ -9,4 +9,4 @@ puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + "
sdlmixer.play(__dirname + "/../wavs/sfx/alarm.wav", function () {
puts("done")
-});
+});
View
4 test/test.js
@@ -1,6 +1,6 @@
var sdlmixer = require("../sdlmixer"),
- sys = require("sys"),
- puts = sys.puts;
+ util = require("util"),
+ puts = util.puts;
puts("Opened audio at " + sdlmixer.audioRate + " Hz " + sdlmixer.audioFormat + " bit " +
sdlmixer.audioChannels + " and " + sdlmixer.numberOfAudioChannels + " channels.")

0 comments on commit 5d91658

Please sign in to comment.