LV2: Avoid Connecting To Invalid OSC Ports
[zynaddsubfx-code.git] / src / Plugin / ZynAddSubFX / ZynAddSubFX-UI-Zest.cpp
blob79d4e1c9be1cbae42052844677385b665d693e72
1 /*
2 ZynAddSubFX - a software synthesizer
4 ZynAddSubFX-UI.cpp - DPF + ZynAddSubFX External UI
5 Copyright (C) 2015-2016 Filipe Coelho
6 Author: Filipe Coelho
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
14 // DPF includes
15 #include "DistrhoUI.hpp"
16 #include <dlfcn.h>
18 typedef void *zest_t;
19 struct zest_handles {
20 zest_t *(*zest_open)(const char *);
21 void (*zest_close)(zest_t*);
22 void (*zest_setup)(zest_t*);
23 void (*zest_draw)(zest_t*);
24 void (*zest_motion)(zest_t*, int x, int y);
25 void (*zest_scroll)(zest_t*, int x, int y, int dx, int dy);
26 void (*zest_mouse)(zest_t *z, int button, int action, int x, int y);
27 void (*zest_key)(zest_t *z, char *key, bool press);
28 void (*zest_resize)();
29 void (*zest_special)(zest_t *z, int key, int press);
30 int (*zest_tick)(zest_t*);
31 zest_t *zest;
34 /* ------------------------------------------------------------------------------------------------------------
35 * ZynAddSubFX UI class */
37 class ZynAddSubFXUI : public UI
39 public:
40 ZynAddSubFXUI()
41 : UI(1181, 659)
43 printf("[INFO] Opened the zynaddsubfx UI...\n");
44 handle = dlopen("/opt/zyn-fusion/libzest.so", RTLD_LAZY);
45 if(!handle) {
46 printf("[ERROR] Cannot Open libzest.so\n");
47 printf("[ERROR] '%s'\n", dlerror());
49 memset(&z, 0, sizeof(z));
50 #define get_sym(x) z.zest_##x = (decltype(z.zest_##x))dlsym(handle, "zest_"#x)
51 if(handle) {
52 get_sym(open);
53 get_sym(setup);
54 get_sym(close);
55 get_sym(draw);
56 get_sym(tick);
57 get_sym(key);
58 get_sym(motion);
59 get_sym(scroll);
60 get_sym(mouse);
61 get_sym(special);
63 oscPort = -1;
64 printf("[INFO] Ready to run\n");
67 ~ZynAddSubFXUI() override
69 printf("[INFO:Zyn] zest_close()\n");
70 if(z.zest)
71 z.zest_close(z.zest);
72 if(handle)
73 dlclose(handle);
76 protected:
77 /* --------------------------------------------------------------------------------------------------------
78 * DSP/Plugin Callbacks */
80 /**
81 A parameter has changed on the plugin side.
82 This is called by the host to inform the UI about parameter changes.
84 void parameterChanged(uint32_t index, float value) override
86 switch (index)
88 case kParamOscPort: {
89 const int port = int(value+0.5f);
91 if (oscPort != port)
93 oscPort = port;
95 } break;
99 /**
100 A program has been loaded on the plugin side.
101 This is called by the host to inform the UI about program changes.
103 void programLoaded(uint32_t index) override
108 A state has changed on the plugin side.
109 This is called by the host to inform the UI about state changes.
111 void stateChanged(const char* key, const char* value) override
115 /* --------------------------------------------------------------------------------------------------------
116 * UI Callbacks */
118 bool onScroll(const ScrollEvent &ev) override
120 if(z.zest)
121 z.zest_scroll(z.zest, ev.pos.getX(), ev.pos.getY(), ev.delta.getX(), ev.delta.getY());
122 return false;
125 bool onSpecial(const SpecialEvent &ev) override
127 printf("special event = %d, %d\n", ev.key, ev.press);
128 if(z.zest)
129 z.zest_special(z.zest, ev.key, ev.press);
130 return false;
133 bool onMouse(const MouseEvent &m) override
135 if(z.zest)
136 z.zest_mouse(z.zest, m.button, m.press, m.pos.getX(), m.pos.getY());
137 return false;
140 bool onMotion(const MotionEvent &m) override
142 if(z.zest)
143 z.zest_motion(z.zest, m.pos.getX(), m.pos.getY());
144 return false;
148 A function called to draw the view contents with OpenGL.
150 void onDisplay() override
152 if(oscPort == -1)
153 return;
154 if(!z.zest) {
155 if(!z.zest_open)
156 return;
157 //printf("[INFO:Zyn] zest_open()\n");
158 char address[1024];
159 snprintf(address, sizeof(address), "osc.udp://127.0.0.1:%d",oscPort);
160 printf("[INFO:Zyn] zest_open(%s)\n", address);
162 z.zest = z.zest_open(address);
163 printf("[INFO:Zyn] zest_setup()\n");
164 z.zest_setup(z.zest);
167 z.zest_draw(z.zest);
168 repaint();
171 bool onKeyboard(const KeyboardEvent &ev)
173 char c[2] = {0};
174 if(ev.key < 128)
175 c[0] = ev.key;
176 if(z.zest && c[0])
177 z.zest_key(z.zest, c, ev.press);
178 return true;
181 void uiIdle(void) override
183 if(z.zest)
184 z.zest_tick(z.zest);
187 void uiReshape(uint width, uint height)
191 private:
192 int oscPort;
193 zest_handles z;
194 void *handle;
197 DISTRHO_DECLARE_NON_COPY_CLASS(ZynAddSubFXUI)
200 /* ------------------------------------------------------------------------------------------------------------
201 * Create UI, entry point */
203 START_NAMESPACE_DISTRHO
205 UI* createUI()
207 return new ZynAddSubFXUI();
210 END_NAMESPACE_DISTRHO