1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 10 июл. 2017 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <ui/ctl/ctl.h>
23 
24 namespace lsp
25 {
26     namespace ctl
27     {
28         const ctl_class_t CtlLed::metadata = { "CtlLed", &CtlWidget::metadata };
29 
CtlLed(CtlRegistry * src,LSPLed * widget)30         CtlLed::CtlLed(CtlRegistry *src, LSPLed *widget): CtlWidget(src, widget)
31         {
32             pClass          = &metadata;
33             fValue          = 0;
34             pPort           = NULL;
35             fKey            = 1;
36 
37             bActivitySet    = false;
38             bInvert         = false;
39         }
40 
~CtlLed()41         CtlLed::~CtlLed()
42         {
43             destroy();
44         }
45 
init()46         void CtlLed::init()
47         {
48             CtlWidget::init();
49 
50             if (pWidget == NULL)
51                 return;
52 
53             LSPLed *led = static_cast<LSPLed *>(pWidget);
54 
55             // Initialize color controllers
56             sColor.init_hsl(pRegistry, led, led->color(), A_COLOR, A_HUE_ID, A_SAT_ID, A_LIGHT_ID);
57 
58             // Initialize activity
59             sActivity.init(pRegistry, NULL);
60         }
61 
set(widget_attribute_t att,const char * value)62         void CtlLed::set(widget_attribute_t att, const char *value)
63         {
64             LSPLed *led = (pWidget != NULL) ? static_cast<LSPLed *>(pWidget) : NULL;
65 
66             switch (att)
67             {
68                 case A_ID:
69                     BIND_PORT(pRegistry, pPort, value);
70                     break;
71                 case A_KEY:
72                     PARSE_FLOAT(value, fKey = __);
73                     break;
74                 case A_VALUE:
75                     PARSE_FLOAT(value, fValue = __);
76                     break;
77                 case A_SIZE:
78                     if (led != NULL)
79                         PARSE_INT(value, led->set_size(size_t(__)));
80                     break;
81                 case A_ACTIVITY:
82                     BIND_EXPR(sVisibility, value);
83                     bActivitySet = true;
84                     break;
85                 case A_INVERT:
86                     PARSE_BOOL(value, bInvert = __);
87                     break;
88                 default:
89                 {
90                     sColor.set(att, value);
91                     CtlWidget::set(att, value);
92                     break;
93                 }
94             }
95         }
96 
update_value()97         void CtlLed::update_value()
98         {
99             if (pWidget == NULL)
100                 return;
101 
102             bool on = false;
103             if ((bActivitySet) && (sActivity.valid()))
104             {
105                 float value = sActivity.evaluate();
106                 on = value >= 0.5f;
107             }
108             else if (pPort != NULL)
109             {
110                 float value = pPort->get_value();
111                 const port_t *meta = pPort->metadata();
112 
113                 on = (meta->unit == U_ENUM) ? (abs(value - fKey) <= CMP_TOLERANCE) : (value >= 0.5f);
114             }
115             else
116                 on = abs(fValue - fKey) <= CMP_TOLERANCE;
117 
118             // Update lighting
119             LSPLed *led     = widget_cast<LSPLed>(pWidget);
120             if (led != NULL)
121                 led->set_on(on ^ bInvert);
122         }
123 
notify(CtlPort * port)124         void CtlLed::notify(CtlPort *port)
125         {
126             CtlWidget::notify(port);
127 
128             if (sActivity.valid())
129                 sActivity.notify(port);
130 
131             update_value();
132        }
133 
end()134         void CtlLed::end()
135         {
136             CtlWidget::end();
137             update_value();
138         }
139 
destroy()140         void CtlLed::destroy()
141         {
142             sActivity.destroy();
143             CtlWidget::destroy();
144         }
145 
146     } /* namespace ctl */
147 } /* namespace lsp */
148