all files / sync/gpii/node_modules/alsa/ alsa_bridge.js

94.87% Statements 37/39
50% Branches 2/4
83.33% Functions 5/6
94.87% Lines 37/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                                                                                                                                                 
/*
 * GPII Node.js ALSA Volume Bridge
 *
 * Copyright 2013 Emergya
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/linux/blob/master/LICENSE.txt
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 */
 
"use strict";
 
var fluid = require("universal");
var gpii = fluid.registerNamespace("gpii");
var alsa = require("./nodealsa/build/Release/nodealsa.node");
 
fluid.registerNamespace("gpii.alsa");
 
fluid.defaults("gpii.alsa.getSystemVolume", {
    gradeNames: "fluid.function",
    argumentMap: {
    }
});
 
fluid.defaults("gpii.alsa.setSystemVolume", {
    gradeNames: "fluid.function",
    argumentMap: {
        value: 0
    }
});
 
fluid.defaults("gpii.alsa.get", {
    gradeNames: "fluid.function",
    argumentMap: {
        payload: 0
    }
});
 
fluid.defaults("gpii.alsa.set", {
    gradeNames: "fluid.function",
    argumentMap: {
        payload: 0
    }
});
 
gpii.alsa.getSystemVolume = function () {
    return alsa.getSystemVolume();
};
 
gpii.alsa.setSystemVolume = function (value) {
    return alsa.setSystemVolume(value);
};
 
// TODO: http://issues.gpii.net/browse/GPII-1028
// This mechanism has been literaly copied/pasted from XRandR's settings
// handler, which looks like a good approach to deal with this kind of
// settings handler. The use of this approach should be consolidated
// as an utility inside the GPII.
//
gpii.alsa.allSettings = {
    masterVolume: {
        get: "gpii.alsa.getSystemVolume",
        set: "gpii.alsa.setSystemVolume"
    }
};
 
gpii.alsa.getImpl = function (settingsRequest) {
    settingsRequest = settingsRequest || gpii.alsa.allSettings;
    var settings = fluid.transform(settingsRequest, function (value, key) {
        var funcEntry = gpii.alsa.allSettings[key];
        Eif (funcEntry) {
            return fluid.invokeGlobalFunction(funcEntry.get);
        } else {
            fluid.fail("Invalid key to ALSA settings handler - " +
                key + " - valid choices are " + JSON.stringify(fluid.keys(gpii.alsa.allSettings)));
        }
    });
    return settings;
};
 
gpii.alsa.get = function (payload) {
    var app = fluid.copy(payload);
    for (var appId in app) {
        for (var j = 0; j < app[appId].length; j++) {
            var settings = gpii.alsa.getImpl(app[appId][j].settings);
 
            var noOptions = { settings: settings };
            app[appId][j] = noOptions;
        }
    }
    return app;
};
 
gpii.alsa.set = function (payload) {
    var app = fluid.copy(payload);
    var settings = app["org.alsa-project"][0].settings;
 
    var oldValue = alsa.getSystemVolume();
    alsa.setSystemVolume(settings.masterVolume);
 
    var newSettingsResponse = {};
    newSettingsResponse.masterVolume = {
        "oldValue": oldValue,
        "newValue": settings.masterVolume
    };
 
    var noOptions = {settings: newSettingsResponse};
    fluid.set(app, ["org.alsa-project", 0], noOptions);
 
    return app;
};