summaryrefslogtreecommitdiff
path: root/libs/midi++2/midiport.cc
blob: a4b28ce461fcccacd4e0900e1394e681cffc031a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
    Copyright (C) 1998 Paul Barton-Davis
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id$
*/

#include <cstdio>
#include <fcntl.h>

#include <pbd/xml++.h>

#include <midi++/types.h>
#include <midi++/port.h>
#include <midi++/channel.h>
#include <midi++/port_request.h>
#include <midi++/factory.h>

//using namespace Select;
using namespace MIDI;

size_t Port::nports = 0;

Port::Port (PortRequest &req)

{
	_ok = false;  /* derived class must set to true if constructor
			 succeeds.
		      */

	bytes_written = 0;
	bytes_read = 0;
	input_parser = 0;
	output_parser = 0;
	slowdown = 0;

	_devname = req.devname;
	_tagname = req.tagname;
	_mode = req.mode;

	if (_mode == O_RDONLY || _mode == O_RDWR) {
		input_parser = new Parser (*this);
	} else {
		input_parser = 0;
	}

	if (_mode == O_WRONLY || _mode == O_RDWR) {
		output_parser = new Parser (*this);
	} else {
		output_parser = 0;
	}

	for (int i = 0; i < 16; i++) {
		_channel[i] =  new Channel (i, *this);

		if (input_parser) {
			_channel[i]->connect_input_signals ();
		}

		if (output_parser) {
			_channel[i]->connect_output_signals ();
		}
	}
}


Port::~Port ()
	
{
	for (int i = 0; i < 16; i++) {
		delete _channel[i];
	}
}

XMLNode&
Port::get_state () const
{
	XMLNode* node = new XMLNode ("MIDI-port");
	node->add_property ("tag", _tagname);
	node->add_property ("device", _devname);
	node->add_property ("mode", PortFactory::mode_to_string (_mode));
	node->add_property ("type", get_typestring());

	return *node;
}

int
Port::clock ()
	
{
	static byte clockmsg = 0xf8;
	
	if (_mode != O_RDONLY) {
		return midimsg (&clockmsg, 1);
	}
	
	return 0;
}

/*
void
Port::selector_read_callback (Selectable *s, Select::Condition cond) 

{
	byte buf[64];
	read (buf, sizeof (buf));
}
*/

void
Port::xforms_read_callback (int cond, int fd, void *ptr) 

{
	byte buf[64];
	
	((Port *)ptr)->read (buf, sizeof (buf));
}

void
Port::gtk_read_callback (void *ptr, int fd, int cond)

{
	byte buf[64];
	
	((Port *)ptr)->read (buf, sizeof (buf));
}

void
Port::write_callback (byte *msg, unsigned int len, void *ptr)
	
{
	((Port *)ptr)->write (msg, len);
}