dialog_prefs.html 45.9 KB
Newer Older
pokergaming's avatar
pokergaming committed
1
<!DOCTYPE html>
2
<html id="prefs_html_element">
pokergaming's avatar
pokergaming committed
3
  <head>
4
    <link id="page_style" rel="stylesheet" type="text/css" href="css/default.css">
pokergaming's avatar
pokergaming committed
5
  </head>
6
  <body class="dialog_body prefs_body">
7 8 9 10 11 12 13 14 15
    <div class="container noselect prefs_container">
      <div class="prefs_tab_group">
        <input type="radio"
               name="prefs_radio_group"
               id="audio_tab_radio"
               class="tab1 prefs_tab"
               checked="checked" />
        <label for="audio_tab_radio" data-i18n="[title]prefs.heading.audio_tt">
          <span data-i18n="prefs.heading.audio"></span>
16
        </label>
17 18 19 20 21 22 23

        <input type="radio"
               name="prefs_radio_group"
               id="midi_tab_radio"
               class="tab2 prefs_tab"/>
        <label for="midi_tab_radio" data-i18n="[title]prefs.heading.midi_tt">
          <span data-i18n="prefs.heading.midi"></span>
24
        </label>
25 26 27 28 29 30 31

        <input type="radio"
               name="prefs_radio_group"
               id="gui_tab_radio"
               class="tab3 prefs_tab"/>
        <label for="gui_tab_radio" data-i18n="[title]prefs.heading.gui_tt">
          <span data-i18n="prefs.heading.gui"></span>
32
        </label>
33

34 35 36 37 38 39 40 41
        <input type="radio"
               name="prefs_radio_group"
               id="startup_tab_radio"
               class="tab4 prefs_tab"/>
        <label for="startup_tab_radio" data-i18n="[title]prefs.heading.startup_tt">
          <span data-i18n="prefs.heading.startup"></span>
        </label>

42
        <div class="tab1">
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
          <table class="tab_settings">
            <tr data-i18n="[title]prefs.audio.api_tt">
              <td>
                <span data-i18n="prefs.audio.api"></span>
              </td>
              <td>
                <select id="audio_api" onchange="change_api(this);">
                </select>
              </td>
            </tr>
            <tr data-i18n="[title]prefs.audio.sr_tt">
              <td>
                <span data-i18n="prefs.audio.sr"></span>
              </td>
              <td>
                <input type="text"
                       id="rate"
                       name="rate"
                       onchange="attr_change(this);">
              </td>
            </tr>
            <tr data-i18n="[title]prefs.audio.blocksize_tt">
              <td>
                <span data-i18n="prefs.audio.blocksize"></span>
              </td>
              <td>
                <select id="blocksize"
                        onchange="attr_change(this);">
                  <option value="64">64</option>
                  <option value="128">128</option>
                  <option value="256">256</option>
                  <option value="512">512</option>
                  <option value="1024">1024</option>
                  <option value="2048">2048</option>
                </select>
              </td>
            <tr data-i18n="[title]prefs.audio.advance_tt">
              <td>
                <span data-i18n="prefs.audio.advance"></span>
              </td>
              <td>
                <input type="text"
                       id="advance"
                       name="advance"
                       onchange="attr_change(this);">
              </td>
            </tr>
            <tr id="callback_container" class="hidden"
                data-i18n="[title]prefs.audio.callback_tt">
              <td>
93
                <span data-i18n="prefs.audio.callback"></span>
94 95
              </td>
              <td>
96
                <input type="checkbox" id="callback" name="callback">
97 98 99 100 101
              </td>
            </tr>
            <tr id="multi_dev_container" class="hidden"
                data-i18n="[title]prefs.audio.callback_tt">
              <td>
102
                <span data-i18n="prefs.audio.callback"></span>
103 104
              </td>
              <td>
105
                <input type="checkbox" id="multi-dev" name="multi-dev">
106 107 108
              </td>
            </tr>
          </table>
109
          <table class="tab_settings">
110
            <tr>
111 112
              <td data-i18n="[title]prefs.audio.input_title_tt">
                <span data-i18n="prefs.audio.input_title"></span>
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
              </td>
              <td>
                <span data-i18n="prefs.audio.channels"></span>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in1" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans1"
                   name="inchans1"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in2" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans2"
                   name="inchans2"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in3" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans3"
                   name="inchans3"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="in4" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="inchans4"
                   name="inchans4"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
171 172
              <td data-i18n="[title]prefs.audio.output_title_tt">
                <span data-i18n="prefs.audio.output_title"></span>
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
              </td>
              <td>
                <span data-i18n="prefs.audio.channels"></span>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out1" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans1"
                   name="outchans1"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out2" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans2"
                   name="outchans2"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out3" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans3"
                   name="outchans3"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
            <tr>
              <td>
                <select id="out4" onchange="dev_change(this);"></select>
              </td>
              <td>
                <label data-i18n="[title]prefs.audio.channels_tt">
                  <input type="text"
                   id="outchans4"
                   name="outchans4"
                   onchange="dev_change(this);">
                </label>
              </td>
            </tr>
          </table>
231 232
        </div>

233
        <div class="tab2">
234
          <div class="tab_settings midi_api_container">
235 236 237 238 239 240
            <label data-i18n="[title]prefs.midi.api_tt">
              <span data-i18n="prefs.midi.api"></span>
              <select id="midi_api" onchange="change_api(this);">
              </select>
            </label>
          </div>
241 242 243 244 245 246
          <div class="tab_settings alsa_midi">
            <label data-i18n="[title]prefs.midi.alsa_in_ports_tt">
              <span data-i18n="prefs.midi.alsa_in_ports"></span>
              <input type="text"
                     id="alsa_in_ports"
                     name="alsa_in_ports"
247
                     onchange="alsa_in_ports_change(this);">
248 249 250 251 252 253
            </label>
            <label data-i18n="[title]prefs.midi.alsa_out_ports_tt">
              <span data-i18n="prefs.midi.alsa_out_ports"></span>
              <input type="text"
                     id="alsa_out_ports"
                     name="alsa_out_ports"
254
                     onchange="alsa_out_ports_change(this);">
255 256
            </label>
          </div>
257

258 259 260 261
          <div class="tab_settings midi_devices">
            <span data-i18n="[title]prefs.midi.input_title_tt">
              <span data-i18n="prefs.midi.input_title"></span>
            </span>
262 263
            <table class="tab_settings">
              <tr>
264
                <td>1</td><td>
265 266
                  <select id="midi_in1" onchange="dev_change(this);"></select>
                </td>
267
                <td align="right">6</td><td>
268 269 270 271
                  <select id="midi_in6" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
272
                <td>2</td><td>
273 274
                  <select id="midi_in2" onchange="dev_change(this);"></select>
                </td>
275
                <td align="right">7</td><td>
276 277 278 279
                  <select id="midi_in7" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
280
                <td>3</td><td>
281 282
                  <select id="midi_in3" onchange="dev_change(this);"></select>
                </td>
283
                <td align="right">8</td><td>
284 285 286 287
                  <select id="midi_in8" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
288
                <td>4</td><td>
289 290
                  <select id="midi_in4" onchange="dev_change(this);"></select>
                </td>
291
                <td align="right">9</td><td>
292 293 294 295
                  <select id="midi_in9" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
296
                <td>5</td><td>
297 298
                  <select id="midi_in5" onchange="dev_change(this);"></select>
                </td>
299
                <td align="right">10</td><td>
300 301 302 303
                  <select id="midi_in10" onchange="dev_change(this);"></select>
                </td>
              </tr>
            </table>
304
            <br/>
305 306 307
            <span data-i18n="[title]prefs.midi.output_title_tt">
              <span data-i18n="prefs.midi.output_title"></span>
            </span>
308 309
            <table class="tab_settings">
              <tr>
310
                <td>1</td><td>
311 312
                  <select id="midi_out1" onchange="dev_change(this);"></select>
                </td>
313
                <td align="right">6</td><td>
314 315 316 317
                  <select id="midi_out6" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
318
                <td>2</td><td>
319 320
                  <select id="midi_out2" onchange="dev_change(this);"></select>
                </td>
321
                <td align="right">7</td><td>
322 323 324 325
                  <select id="midi_out7" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
326
                <td>3</td><td>
327 328
                  <select id="midi_out3" onchange="dev_change(this);"></select>
                </td>
329
                <td align="right">8</td><td>
330 331 332 333
                  <select id="midi_out8" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
334
                <td>4</td><td>
335 336
                  <select id="midi_out4" onchange="dev_change(this);"></select>
                </td>
337
                <td align="right">9</td><td>
338 339 340 341
                  <select id="midi_out9" onchange="dev_change(this);"></select>
                </td>
              </tr>
              <tr>
342
                <td>5</td><td>
343 344
                  <select id="midi_out5" onchange="dev_change(this);"></select>
                </td>
345
                <td align="right">10</td><td>
346 347 348 349
                  <select id="midi_out10" onchange="dev_change(this);"></select>
                </td>
              </tr>
            </table>
350
          </div>
351
        </div>
352

353
        <div class="tab3">
354
          <div class="tab_settings">
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
            <label data-i18n="[title]prefs.gui.presets.gui_preset_tt">
              <span data-i18n="prefs.gui.presets.gui_preset"></span>
            </label>
            <select id="gui_preset" onchange="gui_preset_change(this);">
              <option data-i18n="prefs.gui.presets.default" value="default">
              </option>
              <option data-i18n="prefs.gui.presets.inverted" value="inverted">
              </option>
              <option data-i18n="prefs.gui.presets.vanilla" value="vanilla">
              </option>
              <option data-i18n="prefs.gui.presets.vanilla_inverted" value="vanilla_inverted">
              </option>
              <option data-i18n="prefs.gui.presets.extended" value="extended">
              </option>
              <option data-i18n="prefs.gui.presets.c64" value="c64">
              </option>
              <option data-i18n="prefs.gui.presets.subdued" value="subdued">
              </option>
              <option data-i18n="prefs.gui.presets.strongbad" value="strongbad">
Jonathan Wilkes's avatar
Jonathan Wilkes committed
374 375
              </option>
              <option data-i18n="prefs.gui.presets.solarized" value="solarized">
376 377 378
              </option>
              <option data-i18n="prefs.gui.presets.solarized_inverted"
                      value="solarized_inverted">
379
              </option>
380 381
              <option data-i18n="prefs.gui.presets.footgun" value="footgun">
              </option>
382
            </select>
383 384 385 386 387
            <br/>
            <label data-i18n="[title]prefs.gui.zoom.save_zoom_tt">
              <input type="checkbox" id="save_zoom" name="save_zoom">
              <span data-i18n="prefs.gui.zoom.save_zoom"></span>
            </label>
388 389 390 391 392 393 394 395 396 397 398
            <br/>
            <label data-i18n="[title]prefs.gui.autopatch_yoffset_checkbox_tt">
              <input type="checkbox" id="autopatch_yoffset"
                     name="autopatch_yoffset"
                     onchange="autopatch_yoffset_toggle(this.checked);">
            <span data-i18n="prefs.gui.autopatch_yoffset"></span></span>:
            </label>
            <input type="text"
                   id="autopatch_yoffset_value"
                   name="autopatch_yoffset_value"
                   data-i18n="[title]prefs.gui.autopatch_yoffset_tt">
399 400
            <br/><br/>
            <span data-i18n="prefs.gui.browser.browser_title"></span>
401 402 403 404 405 406 407 408 409 410
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_doc_tt">
              <input type="checkbox" id="browser_doc" name="browser_doc">
              <span data-i18n="prefs.gui.browser.browser_doc"></span>
            </label>
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_path_tt">
              <input type="checkbox" id="browser_path" name="browser_path">
              <span data-i18n="prefs.gui.browser.browser_path"></span>
            </label>
411 412 413 414 415
            <br/>
            <label data-i18n="[title]prefs.gui.browser.browser_init_tt">
              <input type="checkbox" id="browser_init" name="browser_init">
              <span data-i18n="prefs.gui.browser.browser_init"></span>
            </label>
416
          </div>
417
        </div>
418 419 420 421 422 423

        <div class="tab4">
          <div class="tab_settings">
            <label data-i18n="[title]prefs.startup.paths_tt">
              <span data-i18n="prefs.startup.paths"></span>
            </label>
424
            <div style="height:24vh; width:89vw; background:white; border: 1px solid #bbb; overflow-y:auto; overflow-x:auto; padding:0px;">
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
              <table id="startup_paths" style="width:100%; background:white;">
              </table>
            </div>
            <div class="submit_buttons">
              <button type="button" onClick="startup_path_new()" data-i18n="[title]prefs.startup.new_tt">
                <span data-i18n="prefs.startup.new"></span>
              </button>
              <button type="button" id="startup_path_edit" onClick="startup_path_edit()" data-i18n="[title]prefs.startup.edit_tt">
                <span data-i18n="prefs.startup.edit"></span>
              </button>
              <button type="button" id="startup_path_delete" onClick="startup_path_delete()" data-i18n="[title]prefs.startup.del_tt">
                <span data-i18n="prefs.startup.del"></span>
              </button>
            </div>
            <label data-i18n="[title]prefs.startup.libs_tt">
              <span data-i18n="prefs.startup.libs"></span>
            </label>
442
            <div style="height:24vh; width:89vw; background:white; border: 1px solid #bbb; overflow-y:auto; overflow-x:auto; padding:0px;">
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
              <table id="startup_libs" style="width:100%; background:white;">
              </table>
            </div>
            <div class="submit_buttons">
              <button type="button" onClick="startup_lib_new()" data-i18n="[title]prefs.startup.new_tt">
                <span data-i18n="prefs.startup.new"></span>
              </button>
              <button type="button" id="startup_lib_edit" onClick="startup_lib_edit()" data-i18n="[title]prefs.startup.edit_tt">
                <span data-i18n="prefs.startup.edit"></span>
              </button>
              <button type="button" id="startup_lib_delete" onClick="startup_lib_delete()" data-i18n="[title]prefs.startup.del_tt">
                <span data-i18n="prefs.startup.del"></span>
              </button>
            </div>
            <label data-i18n="[title]prefs.startup.flags_tt">
              <span data-i18n="prefs.startup.flags"></span>
459
              <br/>
460 461
              <input type="text"
                     id="startup_flags"
462 463
                     name="startup_flags"
                     style="width:89vw">
464 465 466
            </label>
          </div>
        </div>
467
      </div>
468

469 470 471 472
      <div class="submit_buttons prefs_buttons">
        <button type="button" onClick="ok()" data-i18n="[title]prefs.ok_tt">
          <span data-i18n="prefs.ok"></span>
        </button>
473
        <button type="button" onClick="apply(false)" data-i18n="[title]prefs.apply_tt">
474 475 476 477 478
        <span data-i18n="prefs.apply"></span>
        </button>
        <button type="button" onClick="cancel()" data-i18n="[title]prefs.close_tt">
        <span data-i18n="prefs.close"></span>
        </button>
479
      </div>
480
    </div>
pokergaming's avatar
pokergaming committed
481
  <script>
482
"use strict";
483
var gui = require("nw.gui");
484
var pdgui = require("./pdgui.js");
pokergaming's avatar
pokergaming committed
485

486 487
// For translations
var l = pdgui.get_local_string;
pokergaming's avatar
pokergaming committed
488

489
// Gui presets
490
pdgui.skin.apply(window);
pokergaming's avatar
pokergaming committed
491

492
var pd_object_callback;
pokergaming's avatar
pokergaming committed
493

494 495
var pd_audio_attrs;
var pd_midi_attrs;
pokergaming's avatar
pokergaming committed
496

497
function ok() {
498
    apply(true);
499 500
    cancel();
}
pokergaming's avatar
pokergaming committed
501

502 503 504
function gui_preset_change(elem) {
    pdgui.skin.set(elem.value);
}
pokergaming's avatar
pokergaming committed
505

506 507 508 509
function get_gui_preset() {
    return document.getElementById("gui_preset").selectedOptions[0].value;
}

510 511
function get_bool_elem(elem) {
    return +document.getElementById(elem).checked;
512 513
}

514 515 516 517 518 519 520 521
function get_autopatch_yoffset() {
    if (get_bool_elem("autopatch_yoffset")) {
        return document.getElementById("autopatch_yoffset_value").value;
    } else {
        return "0";
    }
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
// startup config data

// XXXTODO: We should maybe include these in the dialog, but the startup tab
// is already crowded enough as it is, so currently we just record them in
// global variables and then resubmit them unchanged. Note that all of these
// options are rarely used by the average user, and they can also be entered
// using the startup flags field if needed.
var startup_use_stdpath = 1;
var startup_verbose = 0;
var startup_defeat_rt = 0;

function get_startup_flags() {
    return pdgui.encode_for_dialog(document.getElementById("startup_flags").value);
}

// Kludge alert: The user may create invalid empty entries in the paths and
// libs tables during editing. The easiest way to deal with these would be to
// just ignore them, but the table editor currently won't let us do that, so
// instead we flag them using the special text "N/A" in red. These invalid
// entries can be checked for with the valid_startup_item function below and
// are filtered out when submitting the data to Pd. If anyone has a better
// idea to deal with this corner case then please be my guest. -ag

function valid_startup_item(item)
{
    return item.textContent != "N/A" || item.style.color != "red";
}

function finalize_startup_entry(item)
{
    if (!item.textContent) {
        // flag empty entry as invalid
        item.textContent = "N/A";
        item.style.color = "red";
    }
}

function prepare_startup_entry(item)
{
    if (!valid_startup_item(item)) {
        // prepare an invalid entry for editing by turning it back into an
        // empty string
        item.textContent = "";
        item.style.color = "black";
    }
}

// retrieve the path and lib lists from the corresponding tables in the dialog
// and encode them in a form which can be safely transmitted via FUDI

function get_path_array() {
    var table = document.getElementById("startup_paths");
    var arr = [];
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            var cell = table.rows[i].cells[0];
            // filter out empty items
            if (valid_startup_item(cell)) {
                arr.push(pdgui.encode_for_dialog(cell.textContent));
            }
        }
    }
    return arr;
}

function get_lib_array() {
    var table = document.getElementById("startup_libs");
    var arr = [];
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            var cell = table.rows[i].cells[0];
            // filter out empty items
            if (valid_startup_item(cell)) {
                arr.push(pdgui.encode_for_dialog(cell.textContent));
            }
        }
    }
    return arr;
}

602 603 604 605 606 607
// callbacks for devices and/or their number of channels
function dev_change(elem) {
    var attrs, id, direction, index;
    // the same logic works for both channels and
    // devices-- we use the variable 'type' to
    // choose the parameter acoordingly
608
    var type, midi;
609
    id = elem.id;
610 611
    midi = id.slice(0, 4) === "midi" ? true : false;
    direction = id.replace("midi_", "").slice(0,2) === "in" ? "in" : "out";
612
    type = id.indexOf("chans") !== -1 ? "chans" : "devs";
613 614 615
    // This would need to change if there could ever be more than
    // 9 devices
    index = +(id.slice(-1)) - 1;
616 617
    attrs = get_attr("pd-" + direction + type,
        midi ?  pd_midi_attrs : pd_audio_attrs);
618
    attrs[index] = elem.value;
619 620 621

    disable_unused_chans("in");
    disable_unused_chans("out");
622
}
623

624 625 626 627 628 629
function attr_change(elem) {
    var attr, id;
    attr = pd_audio_attrs;
    id = elem.id;
    attr[attr.indexOf(id) + 1] = elem.value;
}
pokergaming's avatar
pokergaming committed
630

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
function alsa_ports_change(elem, type) {
    var attr, dummy, i, len;
    attr = pd_midi_attrs;
    dummy = [];
    len = Number(elem.value);
    if (len.isNan) {
        len = 0;
    }
    for (i = 0; i < len; i++) {
        dummy.push("dummy");
    }
    attr[attr.indexOf("midi-" + type + "dev-names") + 1] = dummy;
}

function alsa_in_ports_change(elem) {
    alsa_ports_change(elem, "in");
}

function alsa_out_ports_change(elem) {
    alsa_ports_change(elem, "out");
}

653 654
function get_input(name) {
    var val = document.getElementsByName(name)[0].value;
655
    return val === 0 ? "0" : val;
656
}
pokergaming's avatar
pokergaming committed
657

658 659 660 661
// get a value from the garray attr array
function get_array_value(name, attrs) {
    return attrs[attrs.indexOf(name) + 1];
}
pokergaming's avatar
pokergaming committed
662

663 664 665
// If dev is -1, just change it to 0. Not sure if the Pd audio
// backend requires this, but it's how it works currently.
function kludge_dev(type, attrs, index) {
666
    var dev = get_attr("pd-" + type + "devs", attrs)[index];
667 668 669
    if (dev < 0) { dev = 0; }
    return dev;
}
pokergaming's avatar
pokergaming committed
670

671
// If the device is -1 ('None'), make the number of channels negative.
672
// This is the way Pd's audio backend turns off the device. (It works
673 674 675
// this way so you can remember your number of channels even if
// you turn off the device.)
function kludge_chans(type, attrs, index) {
676 677
    var dev = get_attr("pd-" + type + "devs", attrs)[index],
        chans = get_attr("pd-" + type + "chans", attrs)[index];
678 679 680
    if (dev < 0 && chans >= 0) { chans *= -1; }
    return chans;
}
pokergaming's avatar
pokergaming committed
681

682
function apply(save_prefs) {
683 684
    var attrs = pd_audio_attrs,
        midi_use_alsa;
685 686

    // Audio dialog
687
    pdgui.pdsend(
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        "pd audio-dialog",
        kludge_dev("in", attrs, 0),
        kludge_dev("in", attrs, 1),
        kludge_dev("in", attrs, 2),
        kludge_dev("in", attrs, 3),
        kludge_chans("in", attrs, 0),
        kludge_chans("in", attrs, 1),
        kludge_chans("in", attrs, 2),
        kludge_chans("in", attrs, 3),
        kludge_dev("out", attrs, 0),
        kludge_dev("out", attrs, 1),
        kludge_dev("out", attrs, 2),
        kludge_dev("out", attrs, 3),
        kludge_chans("out", attrs, 0),
        kludge_chans("out", attrs, 1),
        kludge_chans("out", attrs, 2),
        kludge_chans("out", attrs, 3),
        get_attr("rate", attrs),
        get_attr("advance", attrs),
        get_attr("cancallback", attrs),
        get_attr("blocksize", attrs)
709
    );
pokergaming's avatar
pokergaming committed
710

711
    attrs = pd_midi_attrs;
712
    midi_use_alsa = !!get_attr("use_alsa", attrs);
713
    // Midi dialog
714
    pdgui.pdsend(
715 716 717 718 719
        "pd midi-dialog",
        get_attr("pd-indevs", attrs)[0],
        get_attr("pd-indevs", attrs)[1],
        get_attr("pd-indevs", attrs)[2],
        get_attr("pd-indevs", attrs)[3],
720 721 722 723 724 725
        get_attr("pd-indevs", attrs)[4],
        get_attr("pd-indevs", attrs)[5],
        get_attr("pd-indevs", attrs)[6],
        get_attr("pd-indevs", attrs)[7],
        get_attr("pd-indevs", attrs)[8],
        get_attr("pd-indevs", attrs)[9],
726 727 728 729
        get_attr("pd-outdevs", attrs)[0],
        get_attr("pd-outdevs", attrs)[1],
        get_attr("pd-outdevs", attrs)[2],
        get_attr("pd-outdevs", attrs)[3],
730 731 732 733 734 735
        get_attr("pd-outdevs", attrs)[4],
        get_attr("pd-outdevs", attrs)[5],
        get_attr("pd-outdevs", attrs)[6],
        get_attr("pd-outdevs", attrs)[7],
        get_attr("pd-outdevs", attrs)[8],
        get_attr("pd-outdevs", attrs)[9],
736 737
        midi_use_alsa ? get_attr("midi-indev-names", attrs).length : 0,
        midi_use_alsa ? get_attr("midi-outdev-names", attrs).length : 0
738
    );
739

740 741 742
    // Send the gui prefs (currently just the name of the gui preset, the
    // status of the save-zoom toggle and various options related to the help
    // browser) to Pd
743 744 745 746 747 748 749 750
    pdgui.pdsend("pd gui-prefs",
        get_gui_preset(),
        get_bool_elem("save_zoom"),
        get_bool_elem("browser_doc"),
        get_bool_elem("browser_path"),
        get_bool_elem("browser_init"),
        get_autopatch_yoffset()
    );
751

752 753 754 755
    // Send the startup config data to Pd
    pdgui.pdsend.apply(null, ["pd path-dialog", startup_use_stdpath, startup_verbose].concat(get_path_array()));
    pdgui.pdsend.apply(null, ["pd startup-dialog", startup_defeat_rt, get_startup_flags()].concat(get_lib_array()));

756 757 758 759
    if (save_prefs) {
        // save the prefs in Pd...
        pdgui.pdsend("pd save-preferences");
    }
760
}
pokergaming's avatar
pokergaming committed
761

762 763
function cancel() {
    var i, attrs, gfxstub;
764 765 766 767 768 769
    // There seems to be a bug in nwjs 0.13 beta3 that doesn't call the
    // "close" event below. This has the symptom of making it impossible
    // to click the "Ok" button and open the preferences dialog again after
    // that. To see if the bug is gone, try removing the next line and see
    // if you can open the Prefs dialog after clicking "Ok".
    pdgui.remove_dialogwin(pd_object_callback);
770
    window.close(true);
771
    //pdgui.pdsend(pd_object_callback, "cancel");
772
}
773

774
function change_api(elem) {
775
    var id = elem.getAttribute("id"),
776
        value = elem.value; // need js object property, DOM Attribute won't work
777
    if (id === "audio_api") {
778
        pdgui.pdsend("pd audio-setapi", value);
779
    } else {
780
        pdgui.pdsend("pd midi-setapi", value);
781
    }
782
}
783

784 785 786 787 788 789 790
function get_attr(name, attrs) {
    return attrs[attrs.indexOf(name) + 1];
}

function populate_apis(elem, apis, current_api) {
    var i, opt, api_select = elem;
    for (i = 0; i < apis.length; i += 2) {
791
        opt = document.createElement("option");
792
        opt.textContent = apis[i];
793
        opt.setAttribute("value", apis[i+1]);
794
        api_select.appendChild(opt);
pokergaming's avatar
pokergaming committed
795
    }
796 797
    api_select.value = current_api;
}
pokergaming's avatar
pokergaming committed
798

799
function populate_devs(type, attrs) {
800
    var devs = get_attr(type === "in" ? "sys-indevs" : "sys-outdevs", attrs);
801
    var i, j, opt, elem, chan_elem, chans;
802
    chans = get_attr("pd-" + type + "chans", attrs);
803 804
    for (i = 0; i < 4; i++) {
        elem = document.getElementById(type + (i+1));
805
        chan_elem = document.getElementById(type + "chans" + (i+1));
806
        chan_elem.value = chans[i];
pokergaming's avatar
pokergaming committed
807
        // if the user changed the API, we need to remove the old devs
808 809
        while (elem.firstChild) {
            elem.removeChild(elem.firstChild);
810
        }
pokergaming's avatar
pokergaming committed
811
        // make a dummy device named 'None' with value -1
812
        opt = document.createElement("option");
pokergaming's avatar
pokergaming committed
813
        opt.value = -1;
814
        opt.textContent = "None";
pokergaming's avatar
pokergaming committed
815
        elem.appendChild(opt);
816
        for (j = 0; j < devs.length; j++) {
817
            opt = document.createElement("option");
818
            opt.value = j;
819 820 821 822
            opt.textContent = devs[j];
            elem.appendChild(opt);
        }
    }
823 824
}

825
function populate_midi_devs(type, attrs) {
826 827 828
    var dev_names = get_attr(type === "in" ?
        "midi-indev-names" : "midi-outdev-names", attrs);
    var i, j, opt, elem;
829
    for (i = 0; i < 10; i++) {
830
        elem = document.getElementById("midi_" + type + (i+1));
831 832 833 834
        // if the user changed the API, we need to remove the old devs
        while (elem.firstChild) {
            elem.removeChild(elem.firstChild);
        }
835
        // make a dummy device named 'None' with value 0
836
        opt = document.createElement("option");
837
        opt.value = 0;
838
        opt.textContent = "None";
839
        elem.appendChild(opt);
840
        for (j = 0; j < dev_names.length; j++) {
841
            opt = document.createElement("option");
842
            opt.value = j+1;
843
            opt.textContent = dev_names[j];
844 845 846 847 848
            elem.appendChild(opt);
        }
    }
}

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
function disable_unused_chans(type) {
    var i, chan_elem;
    for (i = 0; i < 4; i++) {
        chan_elem = document.getElementById(type + "chans" + (i+1));
        if (+document.getElementById(type + (i+1)).value === -1) {
            chan_elem.disabled = true;
        } else {
            chan_elem.disabled = false;
        }
    }
}

function disable_unused_devs(type, canmulti) {
    var i;
    if (+canmulti < 2) {
        for (i = 0; i < 3; i++) {
            document.getElementById(type + (i+2)).disabled = true;
        }
    } else {
        for (i = 0; i < 4; i++) {
            document.getElementById(type + (i+1)).disabled = false;
        }
    }
}

874 875
function audio_prefs_callback(attrs) {
    pd_audio_attrs = attrs;
876
    var api_select = document.getElementById("audio_api");
877 878 879 880 881
    var callback, i, j, elem, devs, opt;
    // We can get this callback multiple times while the dialog
    // is open.  This is because an API change requires a query
    // for new properties. So we only populate the api options
    // if they don't already exist
882
    if (api_select.getElementsByTagName("option").length < 1) {
883 884
        populate_apis(
            api_select,
885 886
            attrs[attrs.indexOf("audio-apis") + 1],
            attrs[attrs.indexOf("current-api") + 1]);
887
    }
888

889 890 891
    document.getElementById("rate").value = get_attr("rate", attrs);
    document.getElementById("blocksize").value = get_attr("blocksize", attrs);
    document.getElementById("advance").value = get_attr("advance", attrs);
892

893 894
    callback = get_attr("cancallback", attrs);
    //show the checkbox if the API allows
895
    if (callback !== -1) {
896 897 898
        document.getElementById("callback_container")
            .classList.remove("hidden");
        document.getElementById("callback").checked = !!callback;
899
    }
900

901 902
    populate_devs("in", attrs);
    populate_devs("out", attrs);
903

904 905
    set_devs("in", get_attr("pd-indevs", attrs));
    set_devs("out", get_attr("pd-outdevs", attrs));
pokergaming's avatar
pokergaming committed
906

907 908
    // Set chans after the devs (because they may
    // have the side-effect of setting the dev to 'None')
909 910
    set_chans("in", get_attr("pd-inchans", attrs));
    set_chans("out", get_attr("pd-outchans", attrs));
911

912 913 914 915 916 917
    disable_unused_chans("in");
    disable_unused_chans("out");

    disable_unused_devs("in", get_attr("canmulti", attrs));
    disable_unused_devs("out", get_attr("canmulti", attrs));

918
    pdgui.resize_window(pd_object_callback);
919
}
pokergaming's avatar
pokergaming committed
920

921 922 923 924
function set_devs(type, devs) {
    var i;
    for (i = 0; i < devs.length; i++) {
        document.getElementById(type + (i+1)).value = devs[i];
pokergaming's avatar
pokergaming committed
925
    }
926
}
pokergaming's avatar
pokergaming committed
927

928 929 930
function set_chans(type, chans_array) {
    var i;
    for (i = 0; i < chans_array.length; i++) {
931
        document.getElementById(type + "chans" + (i+1)).value = chans_array[i];
932 933 934
        // If no chans, set device to -1 (None)
        if (chans_array[i] < 1) {
            document.getElementById(type + (i+1)).value = -1;
pokergaming's avatar
pokergaming committed
935
        }
pokergaming's avatar
pokergaming committed
936
    }
937
}
pokergaming's avatar
pokergaming committed
938

939 940
function midi_prefs_callback(attrs) {
    pd_midi_attrs = attrs;
941
    var api_select = document.getElementById("midi_api");
942

943
    if (api_select.getElementsByTagName("option").length < 1) {
944 945
        populate_apis(
            api_select,
946 947
            get_attr("midi-apis", attrs),
            get_attr("current-api", attrs)
948 949
        );
    }
950 951 952 953 954 955 956 957 958
    // If we still haven't gotten any apis, just assume that the
    // only option is a generic "system" midi and hide the select widget.
    // This should really be taken care of in Pd-- I don't know why, for
    // example, it doesn't just send "MMIO" on Windows when that's the
    // only API available...
    if (api_select.getElementsByTagName("option").length < 1) {
        document.getElementsByClassName("midi_api_container")[0].
            style.setProperty("display", "none");
    }
959 960 961 962 963 964 965 966 967
    // Hide devs and only show the alsa in/out boxes. In the future
    // we need an additional user friendly alsa interface here-- one that
    // just lets the user pick a single input device and single output
    // device, like LMMS does.
    if (+get_attr("current-api", attrs) === 1) { // ALSA API
        document.getElementsByClassName("midi_devices")[0]
            .style.setProperty("display", "none");
        document.getElementsByClassName("alsa_midi")[0]
            .style.setProperty("display", "inherit");
968 969 970 971 972
        // For ALSA, we just set the number of ports we want
        // for input and output. The tcl/tk interface just reused
        // the first of the "indevs" for this value, so (for now)
        // we repeat that mistake here...
        document.getElementById("alsa_in_ports").value =
973
            get_attr("midi-indev-names", attrs).length;
974
        document.getElementById("alsa_out_ports").value =
975
            get_attr("midi-outdev-names", attrs).length;
976 977 978 979 980 981 982
    } else {
        document.getElementsByClassName("midi_devices")[0]
            .style.setProperty("display", "inherit");
        document.getElementsByClassName("alsa_midi")[0]
            .style.setProperty("display", "none");
        populate_midi_devs("in", attrs);
        populate_midi_devs("out", attrs);
983 984
        set_devs("midi_in", get_attr("pd-indevs", attrs));
        set_devs("midi_out", get_attr("pd-outdevs", attrs));
985
    }
986
    pdgui.resize_window(pd_object_callback);
987
}
988

989 990 991 992 993 994 995
function autopatch_yoffset_toggle(checked) {
    document.getElementById("autopatch_yoffset_value").disabled = !checked;
    document.getElementById("autopatch_yoffset").checked = checked;
}

function gui_prefs_callback(name, save_zoom, browser_doc, browser_path,
    browser_init, autopatch_yoffset) {
996 997 998 999 1000 1001 1002 1003
    var s = document.getElementById("gui_preset"),
        i;
    for (i = 0; i < s.options.length; i++) {
        if (s.options[i].value.toLowerCase() === name.toLowerCase()) {
            s.selectedIndex = i;
            break;
        }
    }