NioUI.cpp (4145B)
1 /* 2 ZynAddSubFX - a software synthesizer 3 4 NioUI.cpp - UI For Runtime IO Changes 5 Copyright (C) 2016 Mark McCurry 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 as published by the Free Software Foundation; either version 2 10 of the License, or (at your option) any later version. 11 */ 12 #include "NioUI.h" 13 #include "../Nio/Nio.h" 14 #include <cstdio> 15 #include <iostream> 16 #include <cstring> 17 #include <FL/Fl_Pack.H> 18 #include <FL/Fl_Spinner.H> 19 #include <FL/Enumerations.H> 20 #include <FL/Fl_Choice.H> 21 #include <FL/Fl_Tabs.H> 22 #include <FL/Fl_Group.H> 23 #include <FL/Fl_Text_Display.H> 24 #include "Osc_SimpleListModel.h" 25 #include "Fl_Osc_Pane.H" 26 27 using namespace std; 28 29 static void callback_fn_choice_nio(Fl_Widget *w, void *); 30 class Fl_Osc_StrChoice:public Fl_Choice, public Fl_Osc_Widget 31 { 32 public: 33 Fl_Osc_StrChoice(int X, int Y, int W, int H, const char *label = NULL) 34 :Fl_Choice(X,Y,W,H, label), Fl_Osc_Widget(this), cb_data(NULL, NULL) 35 { 36 Fl_Choice::callback(callback_fn_choice_nio, NULL); 37 } 38 39 virtual ~Fl_Osc_StrChoice(void) {}; 40 void init(std::string path_) 41 { 42 ext = path_; 43 Fl_Osc_Pane *pane = fetch_osc_pane(this); 44 assert(pane); 45 assert(pane->osc); 46 osc = pane->osc; 47 oscRegister(path_.c_str()); 48 }; 49 50 51 void OSC_value(const char *S) override 52 { 53 for(int i=0; i<size()-1; ++i) { 54 printf("size = %d, i=%d, text='%s'\n", size(), i, text(i)); 55 if(!strcmp(S, text(i))) 56 value(i); 57 } 58 } 59 60 //Refetch parameter information 61 void update(void) 62 { 63 assert(osc); 64 oscWrite(ext); 65 } 66 void callback(Fl_Callback *cb, void *p = NULL) 67 { 68 cb_data.first = cb; 69 cb_data.second = p; 70 } 71 72 void cb(void) 73 { 74 assert(osc); 75 if(text(value())) 76 oscWrite(ext, "s", text(value())); 77 if(cb_data.first) 78 cb_data.first(this, cb_data.second); 79 } 80 private: 81 std::pair<Fl_Callback*, void*> cb_data; 82 }; 83 static void callback_fn_choice_nio(Fl_Widget *w, void *) 84 { 85 ((Fl_Osc_StrChoice*)w)->cb(); 86 } 87 88 NioUI::NioUI(Fl_Osc_Interface *osc) 89 :Fl_Window(200, 100, 400, 400, "New IO Controls") 90 { 91 //hm, I appear to be leaking memory 92 Fl_Osc_Group *settings = new Fl_Osc_Group(0, 20, 400, 400 - 35, "Settings"); 93 { 94 settings->osc = osc; 95 audio = new Fl_Osc_StrChoice(60, 80, 100, 25, "Audio"); 96 //audio->callback(audioCallback); 97 audio->init("/io/sink"); 98 midi = new Fl_Osc_StrChoice(60, 100, 100, 25, "Midi"); 99 //midi->callback(midiCallback); 100 midi->init("/io/source"); 101 } 102 settings->end(); 103 104 //Get options 105 midi_opt = new Osc_SimpleListModel(osc); 106 audio_opt = new Osc_SimpleListModel(osc); 107 108 using list_t = Osc_SimpleListModel::list_t; 109 110 //initialize midi list 111 midi_opt->callback = [this](list_t list) { 112 printf("midi list updating...\n"); 113 midi->clear(); 114 for(auto io:list) 115 midi->add(io.c_str()); 116 }; 117 118 //initialize audio list 119 audio_opt->callback = [this](list_t list) { 120 audio->clear(); 121 for(auto io:list) 122 audio->add(io.c_str()); 123 }; 124 125 midi_opt->doUpdate("/io/source-list"); 126 audio_opt->doUpdate("/io/sink-list"); 127 128 resizable(this); 129 size_range(400, 300); 130 } 131 132 NioUI::~NioUI() 133 {} 134 135 void NioUI::refresh() 136 { 137 midi_opt->doUpdate("/io/source-list"); 138 audio_opt->doUpdate("/io/sink-list"); 139 midi->update(); 140 audio->update(); 141 } 142 143 void NioUI::midiCallback(Fl_Widget *) 144 { 145 //bool good = Nio::setSource(static_cast<Fl_Choice *>(c)->text()); 146 //static_cast<Fl_Choice *>(c)->textcolor(fl_rgb_color(255 * !good, 0, 0)); 147 } 148 149 void NioUI::audioCallback(Fl_Widget *) 150 { 151 //bool good = Nio::setSink(static_cast<Fl_Choice *>(c)->text()); 152 //static_cast<Fl_Choice *>(c)->textcolor(fl_rgb_color(255 * !good, 0, 0)); 153 }