adjustments to build nascent push2 surface support
[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 "pbd/failed_constructor.h"
20
21 #include "push2.h"
22
23 using namespace ARDOUR;
24 using namespace std;
25 using namespace PBD;
26 using namespace Glib;
27 using namespace ArdourSurface;
28
29 #include "i18n.h"
30
31 #include "pbd/abstract_ui.cc" // instantiate template
32
33 Push2::Push2 (Session& s)
34         : ControlProtocol (s, string (X_("Ableton Push2")))
35         , AbstractUI<Push2Request> (name())
36         , handle (0)
37 {
38         if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
39                 throw failed_constructor ();
40         }
41
42         libusb_claim_interface (handle, 0x00);
43 }
44
45 Push2::~Push2 ()
46 {
47         if (handle) {
48                 libusb_release_interface (handle, 0x00);
49                 libusb_close (handle);
50         }
51 }
52
53 bool
54 Push2::probe ()
55 {
56         libusb_device_handle *h;
57         libusb_init (NULL);
58
59         if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
60                 return false;
61         }
62
63         libusb_close (h);
64         return true;
65 }
66
67 void*
68 Push2::request_factory (uint32_t num_requests)
69 {
70         /* AbstractUI<T>::request_buffer_factory() is a template method only
71            instantiated in this source module. To provide something visible for
72            use in the interface/descriptor, we have this static method that is
73            template-free.
74         */
75         return request_buffer_factory (num_requests);
76 }
77
78 void
79 Push2::do_request (Push2Request * req)
80 {
81         // DEBUG_TRACE (DEBUG::MackieControl, string_compose ("doing request type %1\n", req->type));
82         if (req->type == CallSlot) {
83
84                 call_slot (MISSING_INVALIDATOR, req->the_slot);
85
86         } else if (req->type == Quit) {
87
88                 stop ();
89         }
90 }
91
92 int
93 Push2::stop ()
94 {
95         BaseUI::quit ();
96
97         return 0;
98 }