various fixes for push2 support. Now setups video display when enabled
[ardour.git] / libs / surfaces / push2 / push2.cc
1 /*
2         Copyright (C) 2016 Paul Davis
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cairomm/context.h>
20 #include <cairomm/surface.h>
21 #include <pangomm/layout.h>
22
23 #include "pbd/compose.h"
24 #include "pbd/debug.h"
25 #include "pbd/failed_constructor.h"
26
27 #include "ardour/debug.h"
28
29 #include "push2.h"
30
31 using namespace ARDOUR;
32 using namespace std;
33 using namespace PBD;
34 using namespace Glib;
35 using namespace ArdourSurface;
36
37 #include "i18n.h"
38
39 #include "pbd/abstract_ui.cc" // instantiate template
40
41 const int Push2::cols = 960;
42 const int Push2::rows = 160;
43 const int Push2::pixels_per_row = 1024;
44
45 #define ABLETON 0x2982
46 #define PUSH2   0x1967
47
48 Push2::Push2 (Session& s)
49         : ControlProtocol (s, string (X_("Ableton Push2")))
50         , AbstractUI<Push2Request> (name())
51         , handle (0)
52         , device_buffer (0)
53         , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
54 {
55 }
56
57 Push2::~Push2 ()
58 {
59         close ();
60 }
61
62 int
63 Push2::open ()
64 {
65         if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
66                 return -1;
67         }
68
69         libusb_claim_interface (handle, 0x00);
70
71         device_frame_buffer[0] = new uint16_t[rows*pixels_per_row];
72         device_frame_buffer[1] = new uint16_t[rows*pixels_per_row];
73
74         memset (device_frame_buffer[0], 0, sizeof (uint16_t) * rows * pixels_per_row);
75         memset (device_frame_buffer[1], 0, sizeof (uint16_t) * rows * pixels_per_row);
76
77         frame_header[0] = 0xef;
78         frame_header[1] = 0xcd;
79         frame_header[2] = 0xab;
80         frame_header[3] = 0x89;
81         memset (&frame_header[4], 0, 12);
82
83         return 0;
84 }
85
86 int
87 Push2::close ()
88 {
89         vblank_connection.disconnect ();
90
91         if (handle) {
92                 libusb_release_interface (handle, 0x00);
93                 libusb_close (handle);
94         }
95
96         delete [] device_frame_buffer[0];
97         device_frame_buffer[0] = 0;
98
99         delete [] device_frame_buffer[1];
100         device_frame_buffer[1] = 0;
101
102         return 0;
103 }
104
105 bool
106 Push2::probe ()
107 {
108         libusb_device_handle *h;
109         libusb_init (NULL);
110
111         if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
112                 DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
113                 return false;
114         }
115
116         libusb_close (h);
117         DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
118         return true;
119 }
120
121 void*
122 Push2::request_factory (uint32_t num_requests)
123 {
124         /* AbstractUI<T>::request_buffer_factory() is a template method only
125            instantiated in this source module. To provide something visible for
126            use in the interface/descriptor, we have this static method that is
127            template-free.
128         */
129         return request_buffer_factory (num_requests);
130 }
131
132 void
133 Push2::do_request (Push2Request * req)
134 {
135         DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
136         if (req->type == CallSlot) {
137
138                 call_slot (MISSING_INVALIDATOR, req->the_slot);
139
140         } else if (req->type == Quit) {
141
142                 stop ();
143         }
144 }
145
146 int
147 Push2::stop ()
148 {
149         close ();
150         BaseUI::quit ();
151
152         return 0;
153 }
154
155 /** render host-side frame buffer (a Cairo ImageSurface) to the current
156  * device-side frame buffer. The device frame buffer will be pushed to the
157  * device on the next call to vblank()
158  */
159
160 int
161 Push2::render ()
162 {
163         /* ensure that all drawing has been done before we fetch pixel data */
164
165         frame_buffer->flush ();
166
167         const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
168         const uint8_t* data = frame_buffer->get_data ();
169
170         /* fill frame buffer (320kB) */
171
172         Glib::Threads::Mutex::Lock lm (fb_lock);
173
174         uint16_t* fb = (uint16_t*) device_frame_buffer[device_buffer];
175
176         for (int row = 0; row < rows; ++row) {
177
178                 const uint8_t* dp = data + row * stride;
179
180                 for (int col = 0; col < cols; ++col) {
181
182                         /* fetch r, g, b (range 0..255). Ignore alpha */
183
184                         const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
185                         const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
186                         const int b = *((const uint32_t*)dp) & 0xff;
187
188                         /* convert to 5 bits, 6 bits, 5 bits, respectively */
189                         /* generate 16 bit BGB565 value */
190
191                         *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
192
193                         dp += 4;
194                 }
195
196                 /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
197                    byte USB buffers
198                 */
199
200                 fb += 64; /* 128 bytes = 64 int16_t */
201         }
202
203         /* swap buffers (under lock protection) */
204         // device_buffer = (device_buffer ? 0 : 1);
205
206         return 0;
207 }
208
209 bool
210 Push2::vblank ()
211 {
212         int transferred = 0;
213         const int timeout_msecs = 1000;
214         int err;
215
216         if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
217                 return false;
218         }
219
220         {
221                 Glib::Threads::Mutex::Lock lm (fb_lock);
222
223                 if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer[device_buffer] , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
224                         return false;
225                 }
226         }
227
228         return true;
229 }
230
231 int
232 Push2::set_active (bool yn)
233 {
234         DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
235
236         if (yn == active()) {
237                 return 0;
238         }
239
240         if (yn) {
241
242                 if (open ()) {
243                         DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
244                         close ();
245                         return -1;
246                 }
247
248                 /* start event loop */
249
250                 BaseUI::run ();
251
252                 // connect_session_signals ();
253
254                 /* say hello */
255
256                 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (frame_buffer);
257                 if (!context) {
258                         cerr << "Cannot create context\n";
259                         return -1;
260                 }
261                 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
262                 if (!layout) {
263                         cerr << "Cannot create layout\n";
264                         return -1;
265                 }
266
267                 layout->set_text ("hello, Ardour");
268                 Pango::FontDescription fd ("Sans Bold 12");
269                 layout->set_font_description (fd);
270
271                 context->set_source_rgb (0.0, 1.0, 1.0);
272                 context->rectangle (0, 0, 960, 160);
273                 context->fill ();
274                 context->set_source_rgb (0.0, 0.0, 0.0);
275                 context->rectangle (50, 50, 860, 60);
276                 context->fill ();
277                 context->move_to (60, 60);
278                 context->set_source_rgb ((random()%255) / 255.0, (random()%255) / 255.0, (random()%255) / 255.0);
279                 layout->update_from_cairo_context (context);
280                 layout->show_in_cairo_context (context);
281
282                 render ();
283
284                 /* set up periodic task used to push a frame buffer to the
285                  * device (25fps). The device can handle 60fps, but we don't
286                  * need that frame rate.
287                  */
288
289                 Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
290                 vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
291                 vblank_timeout->attach (main_loop()->get_context());
292
293         } else {
294
295                 BaseUI::quit ();
296                 close ();
297
298         }
299
300         ControlProtocol::set_active (yn);
301
302         DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
303
304         return 0;
305 }