Browse Source

No more seg faults!

master
John Shaver 1 year ago
parent
commit
38bdc76ab2
2 changed files with 31 additions and 21 deletions
  1. 24
    4
      main.c
  2. 7
    17
      main.js

+ 24
- 4
main.c View File

@@ -1,5 +1,8 @@
#include <node_api.h>
#include <node/node_api.h>
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <unistd.h>
#include <pulse/pulseaudio.h>

struct PACBData {
@@ -25,15 +28,18 @@ napi_value connectToPulse(napi_env env, napi_callback_info info) {
napi_value args[1];
struct PACBData *paCBData;
napi_status status;
struct NapiCBData *napiCBData = malloc(sizeof(*napiCBData));
struct NapiCBData *napiCBData = malloc(sizeof(struct NapiCBData));
napiCBData->cbRef = malloc(sizeof(napi_ref));
status = napi_get_cb_info(env, info, &argc, args, NULL, (void**)&paCBData);
assert(argc == 1);
assert(status == napi_ok);

printf("finished getting calback info... \n");
napiCBData->env = &env;

status = napi_create_reference(env, args[0], 1, napiCBData->cbRef);
status = napi_create_reference(env, *args, 1, napiCBData->cbRef);
assert(status == napi_ok);
int ret = 1;
@@ -50,6 +56,7 @@ napi_value connectToPulse(napi_env env, napi_callback_info info) {
}

void connectToPulseCB(pa_context *context, void *userdata) {
printf("Callback called... \n");
napi_status status;
napi_value argv[1];
struct NapiCBData *napiCBData = (struct NapiCBData*)userdata;
@@ -69,6 +76,7 @@ void connectToPulseCB(pa_context *context, void *userdata) {
status = napi_create_error(*(napiCBData->env), NULL, errorString, &argv[0]);
assert(status == napi_ok);
} else {
return;
//eventually need to look into reconnecting on disconnections and handling
//connection failures.
}
@@ -80,6 +88,7 @@ void connectToPulseCB(pa_context *context, void *userdata) {

status = napi_get_reference_value(*(napiCBData->env), *(napiCBData->cbRef), &cb);
assert(status == napi_ok);
printf("Calling callback from native... \n");
status = napi_call_function(*(napiCBData->env), global, cb, 1, argv, &result);
assert(status == napi_ok);
}
@@ -109,7 +118,18 @@ void connectToPulseCB(pa_context *context, void *userdata) {
// return;
//}

void handler(int sig) {
void *array[10];
size_t size;

size = backtrace(array, 10);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}

napi_value init(napi_env env, napi_value exports) {
signal(SIGSEGV, handler);
pa_mainloop *mainloop = pa_mainloop_new();
pa_mainloop_api *mainloop_api = pa_mainloop_get_api(mainloop);
pa_context *context = pa_context_new(mainloop_api, NULL);

+ 7
- 17
main.js View File

@@ -1,18 +1,22 @@
const nativeCode = require('bindings')('miccontrol.node');
const {promisify} = require("util");
let connectionStarted = false;
let connected = false;
let queuedRequests = [];

let aAddon = [
"connectToPulse",
"getSources"
//"getSources"
].reduce((obj, fn) => {
obj[fn] = asyncify(nativeCode[fn]);
obj[fn] = promisify(nativeCode[fn]);
return obj;
}, {});

module.exports = () => {
console.log("Starting connection");
if(!connectionStarted) {
aAddon.connectToPulse().then((e) => {
nativeCode.connectToPulse((e) => {
console.log("CONNECTED!");
if(e) {
connectionStarted = false;
} else {
@@ -64,17 +68,3 @@ module.exports = () => {
return miccontrol;
}


function asyncify(fn) {
return async function(...args) {
return new Promise((res, rej) => {
fn(...args, (e, value) => {
if(e) {
return rej(e);
}
return res(value);
});;
});
};
}


Loading…
Cancel
Save