zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

Osc_SimpleListModel.h (2178B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   Osc_SimpleListModel.h - OSC List View
      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 #pragma once
     13 #include "Fl_Osc_Widget.H"
     14 #include <functional>
     15 #include <vector>
     16 #include <rtosc/rtosc.h>
     17 
     18 class Osc_SimpleListModel:public Fl_Osc_Widget
     19 {
     20     public:
     21         Osc_SimpleListModel(Fl_Osc_Interface *osc_)
     22             :Fl_Osc_Widget("", osc_), list_size(0)
     23         {
     24             assert(osc);
     25         }
     26 
     27         typedef std::vector<std::string> list_t;
     28         list_t list;
     29         std::function<void(list_t)> callback;
     30         unsigned list_size;
     31 
     32         void doUpdate(std::string url)
     33         {
     34             if(!ext.empty())
     35                 osc->removeLink(this);
     36             ext = url;
     37 
     38             oscRegister(ext.c_str());
     39         }
     40 
     41         void apply()
     42         {
     43             if(list.size() == 0) {
     44                 oscWrite("", "I");
     45             }
     46             char         types[list.size()+1];
     47             rtosc_arg_t  args[list.size()];
     48 
     49             //zero out data
     50             memset(types, 0, sizeof(types));
     51             memset(args,  0, sizeof(args));
     52 
     53             for(int i=0; i<(int)list.size(); ++i) {
     54                 types[i]  = 's';
     55                 args[i].s = list[i].c_str();
     56             }
     57             char buffer[1024*5];
     58             rtosc_amessage(buffer, sizeof(buffer), ext.c_str(), types, args);
     59             osc->writeRaw(buffer);
     60         }
     61 
     62         //Raw messages
     63         virtual void OSC_raw(const char *msg)
     64         {
     65             std::string args = rtosc_argument_string(msg);
     66             const int list_size = args.length();
     67             for(int i=0; i<list_size; ++i)
     68                 if(args[i] != 's')
     69                     return;
     70 
     71             list.clear();
     72             list.resize(list_size);
     73 
     74             for(int idx=0; idx<list_size; ++idx)
     75                 list[idx] = rtosc_argument(msg, idx).s;
     76             if(callback)
     77                 callback(list);
     78         }
     79 };