Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 5d9165892e7bf58000eb72404d2e2111fd0fbc44 1 parent fd11b9f
Jeroen Janssen authored
4 CHANGES
View
@@ -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'
13 sdlmixer.js
View
@@ -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();
0  src/async.h → src/async_ev.h
View
File renamed without changes
75 src/async_uv.h
View
@@ -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
26 src/node-sdlmixer.cc
View
@@ -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]);
}
7 src/node-sdlmixer.h
View
@@ -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);
6 test/simple.js
View
@@ -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")
-});
+});
4 test/test.js
View
@@ -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.")
Please sign in to comment.
Something went wrong with that request. Please try again.