xref: /haiku/docs/user/book.dox (revision 1de2d7281247a6bd1e65a75cfb16c9d9e8be7a36)
10524a6a8Smahlzeit/*!
26ac7032dSJohn Scipione	\mainpage Welcome to the Haiku Book
36ac7032dSJohn Scipione
46ac7032dSJohn Scipione	Below you will find documentation on the Application Programming
56ac7032dSJohn Scipione	Interface (API) of the Haiku operating system. This API describes
66ac7032dSJohn Scipione	the internals of the operating system allowing developers to write
76ac7032dSJohn Scipione	native C++ applications and device drivers. See the
8*1de2d728SAugustin Cavalier	<a href="https://api.haiku-os.org">online version</a> for the most
96ac7032dSJohn Scipione	updated version of this document. If you would like to help contribute
104d556706SAugustin Cavalier	contact the <a href="https://www.freelists.org/list/haiku-doc">documentation
116ac7032dSJohn Scipione	mailing list</a>. For guidelines on how to help document the API see
126ac7032dSJohn Scipione	the \link apidoc Documenting the API\endlink page. A list of
136ac7032dSJohn Scipione	contributors can be found \ref credits page. Documenting the API is
146ac7032dSJohn Scipione	an ongoing process so contributions are greatly appreciated.
156ac7032dSJohn Scipione
166ac7032dSJohn Scipione	The Haiku API is based on the BeOS R5 API but changes and additions have
176ac7032dSJohn Scipione	been included where appropriate. Important compatibility differences are
186ac7032dSJohn Scipione	detailed on the \ref compatibility page. New classes and methods
196ac7032dSJohn Scipione	and incompatible API changes to the BeOS R5 API are noted in the
206ac7032dSJohn Scipione	appropriate sections.
216ac7032dSJohn Scipione
226ac7032dSJohn Scipione	A complete reference to the BeOS R5 API is available on the web in
23*1de2d728SAugustin Cavalier	<a href="https://haiku-os.org/legacy-docs/bebook/">The Be Book</a>.
246ac7032dSJohn Scipione	The Be Book is used with permission from
254d556706SAugustin Cavalier	<a href="https://www.access-company.com/">Access Co.</a>, the current
266ac7032dSJohn Scipione	owners of Be's intellectual property.
270524a6a8Smahlzeit
280524a6a8Smahlzeit	\section kits Kits and Servers
290524a6a8Smahlzeit
306ac7032dSJohn Scipione	The API is split into several kits and servers each detailing a different
316ac7032dSJohn Scipione	aspect of the operating system.
326ac7032dSJohn Scipione		- The \ref app is the starting point for developing applications
336ac7032dSJohn Scipione			and includes classes for messaging and for interacting with
346ac7032dSJohn Scipione			the rest of the system.
358786818cSJohn Scipione		- The \ref game provides classes for producing game sounds and
368786818cSJohn Scipione			working with full screen apps.
376ac7032dSJohn Scipione		- The \ref interface is used to create responsive and attractive
386ac7032dSJohn Scipione			graphical user interfaces building on the messaging facilities
396ac7032dSJohn Scipione			provided by the Application Kit.
406ac7032dSJohn Scipione			- The \link layout_intro Layout API \endlink is a new addition
416ac7032dSJohn Scipione				to the Interface Kit in Haiku which provides resources to
426ac7032dSJohn Scipione				layout your application flexibly and easily.
436ac7032dSJohn Scipione		- The \ref locale includes classes to localize your application to
446ac7032dSJohn Scipione			different languages, timezones, number formatting conventions and
456ac7032dSJohn Scipione			much more.
466ac7032dSJohn Scipione		- The \ref media provides a unified and consistent interface for media
476ac7032dSJohn Scipione			streams and applications to intercommunicate.
486ac7032dSJohn Scipione		- The \ref midi2 describes an interface to generating, processing,
496ac7032dSJohn Scipione			and playing music in MIDI format. For reference documentation on the
506ac7032dSJohn Scipione			\ref midi1 is also included.
514cf62172SAdrien Destugues		- The \ref network handles everything network related, from interface
524cf62172SAdrien Destugues			IP address settings to HTTP connections.
536ac7032dSJohn Scipione		- The \ref storage is a collection of classes that deal with storing and
546ac7032dSJohn Scipione			retrieving information from disk.
556ac7032dSJohn Scipione		- The \ref support contains support classes to use in your application
566ac7032dSJohn Scipione			including resources for thread safety, IO, and serialization.
573528f5bbSJohn Scipione		- The \ref translation provides a framework for converting data streams
583528f5bbSJohn Scipione			between media formats.
5989e18a51SNiels Sascha Reedijk
606ac7032dSJohn Scipione	\section special_topics Special Topics
616ac7032dSJohn Scipione
626ac7032dSJohn Scipione	- \ref drivers
634466b89cSJohn Scipione	- \ref keyboard
64d419ca3bSNiels Sascha Reedijk*/
65d419ca3bSNiels Sascha Reedijk
669889ca4aSNiels Sascha Reedijk///// Define main kits /////
6789e18a51SNiels Sascha Reedijk
68d419ca3bSNiels Sascha Reedijk/*!
69bef614efSNiels Sascha Reedijk	\defgroup app Application Kit
706ac7032dSJohn Scipione	\brief The Application Kit is the starting point for writing native Haiku
716ac7032dSJohn Scipione		GUI applications.
726ac7032dSJohn Scipione
736ac7032dSJohn Scipione	The application kit is exactly what its name suggests &mdash; it is the
746ac7032dSJohn Scipione	basis of Haiku applications. You should first read through this document
756ac7032dSJohn Scipione	and the references here before moving on to the other parts of the API.
766ac7032dSJohn Scipione
776ac7032dSJohn Scipione	The Application Kit classes can be divided into two groups: the messaging
786ac7032dSJohn Scipione	classes and the system interaction classes. The larger of the two groups is
796ac7032dSJohn Scipione	the messaging classes. Since the Haiku API relies on pervasive
806ac7032dSJohn Scipione	multithreading messaging is an essential topic for any application. Have a
816ac7032dSJohn Scipione	look at the \link app_messaging Introduction to Messaging \endlink for more
826ac7032dSJohn Scipione	information.
836ac7032dSJohn Scipione
846ac7032dSJohn Scipione	The following messaging classes which allow you to easily and securely
856ac7032dSJohn Scipione	communicate between threads.
866ac7032dSJohn Scipione		- BHandler
876ac7032dSJohn Scipione		- BInvoker
886ac7032dSJohn Scipione		- BLooper
896ac7032dSJohn Scipione		- BMessage
906ac7032dSJohn Scipione		- BMessageFilter
916ac7032dSJohn Scipione		- BMessageQueue
926ac7032dSJohn Scipione		- BMessageRunner
936ac7032dSJohn Scipione		- BMessenger
946ac7032dSJohn Scipione
956ac7032dSJohn Scipione	The second group is the system interaction classes. These classes
966ac7032dSJohn Scipione	provide hooks for your application to interact with the rest of the system.
976ac7032dSJohn Scipione	The most important class in this group is BApplication. Below is a list of
986ac7032dSJohn Scipione	all system interaction classes:
996ac7032dSJohn Scipione		- BApplication
1006ac7032dSJohn Scipione		- BClipboard
1016ac7032dSJohn Scipione		- BCursor
1026ac7032dSJohn Scipione		- BPropertyInfo
1036ac7032dSJohn Scipione		- BRoster
1046ac7032dSJohn Scipione
1056ac7032dSJohn Scipione
1068786818cSJohn Scipione	\defgroup game Game Kit
1078786818cSJohn Scipione	\brief The Game Kit provides classes for producing game sounds and
1088786818cSJohn Scipione		working with full screen apps.
1098786818cSJohn Scipione
1108786818cSJohn Scipione
111cc19e7c0SAlex Wilson	\defgroup interface Interface Kit
112cc19e7c0SAlex Wilson	\brief API for displaying a graphical user interface.
1136ac7032dSJohn Scipione
1146ac7032dSJohn Scipione	The Interface Kit holds all the classes you'll need to develop a GUI.
1156ac7032dSJohn Scipione	Building on the messaging facilities provided by the Application Kit,
1166ac7032dSJohn Scipione	the Interface Kit can be used to create a responsive and attractive
1176ac7032dSJohn Scipione	graphical user interface.
1186ac7032dSJohn Scipione
1196ac7032dSJohn Scipione 	The most important class in the Interface Kit is the BView class, which
1206ac7032dSJohn Scipione	handles drawing and user interaction. Pointer and keyboard events are
1216ac7032dSJohn Scipione	processed in this class.
1226ac7032dSJohn Scipione
1236ac7032dSJohn Scipione	Another important class is the BWindow class, which holds BViews and makes
1246ac7032dSJohn Scipione	them visible to the user. The BWindow class also handles BView focusing
1256ac7032dSJohn Scipione	and BMessage dispatching, among other things.
1266ac7032dSJohn Scipione
1276ac7032dSJohn Scipione	A new addition Haiku has added over the BeOS API is the Layout API, which
1286ac7032dSJohn Scipione	is based around the BLayoutItem and BLayout classes. These classes will
1296ac7032dSJohn Scipione	take care of making sure all your GUI widgets end up where you want them,
1306ac7032dSJohn Scipione	with enough space to be useful. You can start learning the Layout API
1316ac7032dSJohn Scipione	by reading the \link layout_intro introduction \endlink.
1326ac7032dSJohn Scipione
1336ac7032dSJohn Scipione
1340a4e27c5SAdrien Destugues	\defgroup locale Locale Kit
135c6247544SAdrien Destugues	\brief Collection of classes for localizing applications.
1366ac7032dSJohn Scipione
1376ac7032dSJohn Scipione	The Locale Kit provides a set of tools for internationalizing,
1386ac7032dSJohn Scipione	localizing and translating your software. This includes not only
1396ac7032dSJohn Scipione	replacing string with their translations at runtime, but also more
1406ac7032dSJohn Scipione	complex tasks such as formatting numbers, dates, and times in a way
1416ac7032dSJohn Scipione	that match the locale preferences of the user.
1426ac7032dSJohn Scipione
1436ac7032dSJohn Scipione	The main way to access locale data is through the be_locale_roster.
1446ac7032dSJohn Scipione	This is a global instance of the BLocaleRoster class, storing the data
1456ac7032dSJohn Scipione	for localizing an application according to the user's preferred settings.
1466ac7032dSJohn Scipione	The locale roster also acts as a factory to instantiate most of the other
1476ac7032dSJohn Scipione	classes. However, there are some cases where you will need to instantiate
1486ac7032dSJohn Scipione	another class by yourself, to use it with custom settings. For example, you
1496ac7032dSJohn Scipione	may need to format a date with a fixed format in english for including in an
1506ac7032dSJohn Scipione	e-mail header, as it is the only format accepted there.
1516ac7032dSJohn Scipione
1526ac7032dSJohn Scipione	Unlike the other kits in Haiku, the Locale kit does not live in libbe.
1536ac7032dSJohn Scipione	When building a localized application, you have to link it to
1546ac7032dSJohn Scipione	liblocale.so. If you want to use the catalog macros, you also have to
1556ac7032dSJohn Scipione	link each of your images (that is, applications, libraries and add-ons)
1566ac7032dSJohn Scipione	to liblocalestub.a.
1576ac7032dSJohn Scipione
1586ac7032dSJohn Scipione	\defgroup media Media Kit
1596ac7032dSJohn Scipione	\brief Collection of classes that deal with audio and video.
1606ac7032dSJohn Scipione
1616ac7032dSJohn Scipione
1626ac7032dSJohn Scipione	\defgroup midi1 The old MIDI Kit (libmidi.so)
1636ac7032dSJohn Scipione	\brief The old MIDI kit.
1646ac7032dSJohn Scipione
1656ac7032dSJohn Scipione
1666ac7032dSJohn Scipione	\defgroup midi2 MIDI 2 Kit
1676ac7032dSJohn Scipione	\brief The Midi Kit is the API that implements support for generating,
1686ac7032dSJohn Scipione		processing, and playing music in MIDI format.
1696ac7032dSJohn Scipione
1704d556706SAugustin Cavalier	<A HREF="https://www.midi.org/">MIDI</A>, which  stands for 'Musical
1716ac7032dSJohn Scipione	Instrument Digital Interface', is a well-established  standard for
1726ac7032dSJohn Scipione	representing and communicating musical data. This document serves as
1736ac7032dSJohn Scipione	an overview. If you would like to see all the components, please look
1746ac7032dSJohn Scipione	at \link midi2 the list with classes \endlink.
1756ac7032dSJohn Scipione
1766ac7032dSJohn Scipione	\section midi2twokits A Tale of Two MIDI Kits
1776ac7032dSJohn Scipione
1786ac7032dSJohn Scipione	BeOS comes with two different, but compatible Midi Kits. This
1796ac7032dSJohn Scipione	documentation focuses on the "new" Midi Kit, or midi2 as we like to
1806ac7032dSJohn Scipione	call it, that was  introduced with BeOS R5. The old kit, which we'll
1816ac7032dSJohn Scipione	refer to as midi1, is more complete than the new kit, but less powerful.
1826ac7032dSJohn Scipione
1836ac7032dSJohn Scipione	Both kits let you create so-called MIDI endpoints, but the endpoints
1846ac7032dSJohn Scipione	from midi1 cannot be shared between different applications. The midi2
1856ac7032dSJohn Scipione	kit solves that  problem, but unlike midi1 it does not include a General
1866ac7032dSJohn Scipione	MIDI softsynth, nor does it have a facility for reading and playing
1876ac7032dSJohn Scipione	Standard MIDI Files. Don't worry: both kits are compatible and you can
1886ac7032dSJohn Scipione	mix-and-match them in your applications.
1896ac7032dSJohn Scipione
1906ac7032dSJohn Scipione	The main differences between the two kits:
1916ac7032dSJohn Scipione		- Instead of one BMidi object that both produces and consumes events,
1926ac7032dSJohn Scipione			we have BMidiProducer and BMidiConsumer.
1936ac7032dSJohn Scipione		- Applications are capable of sharing MIDI producers and consumers
1946ac7032dSJohn Scipione			with other applications via the centralized Midi Roster.
1956ac7032dSJohn Scipione		- Physical MIDI ports are now sharable without apps "stealing" events
1966ac7032dSJohn Scipione			from each other.
1976ac7032dSJohn Scipione		- Applications can now send/receive raw MIDI byte streams (useful if
1986ac7032dSJohn Scipione			an application has its own MIDI parser/engine).
1996ac7032dSJohn Scipione		- Channels are numbered 0&ndash;15, not 1&ndash;16
2006ac7032dSJohn Scipione		- Timing is now specified in microseconds rather than milliseconds.
2016ac7032dSJohn Scipione
2026ac7032dSJohn Scipione	\section midi2concepts Midi Kit Concepts
2036ac7032dSJohn Scipione
2046ac7032dSJohn Scipione	A brief overview of the elements that comprise the Midi Kit:
2056ac7032dSJohn Scipione		- \b Endpoints. This is what the Midi Kit is all about: sending MIDI
2066ac7032dSJohn Scipione			messages between endpoints. An endpoint is like a MIDI In or MIDI
2076ac7032dSJohn Scipione			Out socket on your equipment; it either receives information or it
2086ac7032dSJohn Scipione			sends information. Endpoints that send MIDI events are called
2096ac7032dSJohn Scipione			\b producers; the endpoints that receive those events are called
2106ac7032dSJohn Scipione			\b consumers. An endpoint that is created by your own application
2116ac7032dSJohn Scipione			is called \b local; endpoints from other applications are
2126ac7032dSJohn Scipione			\b remote. You can access remote endpoints using \b proxies.
2136ac7032dSJohn Scipione		- \b Filters. A filter is an object that has a consumer and a producer
2146ac7032dSJohn Scipione			endpoint. It reads incoming events from its consumer, performs some
2156ac7032dSJohn Scipione			operation, and tells its producer to send out the results. In its
2166ac7032dSJohn Scipione			current form, the Midi  Kit doesn't provide any special facilities
2176ac7032dSJohn Scipione			for writing filters.
2186ac7032dSJohn Scipione		- \b Midi \b Roster. The roster is the list of all published producers
2196ac7032dSJohn Scipione			and consumers. By publishing an endpoint, you allow other
2206ac7032dSJohn Scipione			applications to talk to it. You are not required to publish your
2216ac7032dSJohn Scipione			endpoints, in which case only your own application can use them.
2226ac7032dSJohn Scipione		- \b Midi \b Server. The Midi Server does the behind-the-scenes work.
2236ac7032dSJohn Scipione			It manages the roster, it connects endpoints, it makes sure that
2246ac7032dSJohn Scipione			endpoints can communicate, and so on. The Midi Server is started
2256ac7032dSJohn Scipione			automatically when BeOS boots, and you never have to deal with it
2266ac7032dSJohn Scipione			directly. Just remember that it runs the show.
2276ac7032dSJohn Scipione		- \b libmidi. The BMidi* classes live inside two shared libraries:
2286ac7032dSJohn Scipione			libmidi.so and libmidi2.so. If you write an application that uses
2296ac7032dSJohn Scipione			old Midi Kit, you must link it to libmidi.so. Applications that use
2306ac7032dSJohn Scipione			the new Midi Kit must link to libmidi2.so. If you want to
2316ac7032dSJohn Scipione			mix-and-match both kits, you should also link to both libraries.
2326ac7032dSJohn Scipione
2336ac7032dSJohn Scipione	Here is a pretty picture:
2346ac7032dSJohn Scipione
2356ac7032dSJohn Scipione	\image html midi2concepts.png
2366ac7032dSJohn Scipione
2376ac7032dSJohn Scipione	\section midi2mediakit Midi Kit != Media Kit
2386ac7032dSJohn Scipione
2396ac7032dSJohn Scipione	Be chose not to integrate the Midi Kit into the Media Kit as another media
2406ac7032dSJohn Scipione	type, mainly because MIDI doesn't require any of the format negotiation that
2416ac7032dSJohn Scipione	other media types need. Although the two kits look similar -- both have a
2426ac7032dSJohn Scipione	"roster" for finding or registering "consumers" and "producers" -- there are
2436ac7032dSJohn Scipione	some very important differences.
2446ac7032dSJohn Scipione
2456ac7032dSJohn Scipione	The first and most important point to note is that BMidiConsumer and
2466ac7032dSJohn Scipione	BMidiProducer in the Midi Kit are \b NOT directly analogous to
2476ac7032dSJohn Scipione	BBufferConsumer and  BBufferProducer in the Media Kit! In the Media Kit,
2486ac7032dSJohn Scipione	consumers and producers are the data consuming and producing properties
2496ac7032dSJohn Scipione	of a media node. A filter in the Media Kit, therefore, inherits from both
2506ac7032dSJohn Scipione	BBufferConsumer and BBufferProducer, and implements their virtual member
2516ac7032dSJohn Scipione	functions to do its work.
2526ac7032dSJohn Scipione
2536ac7032dSJohn Scipione	In the Midi Kit, consumers and producers act as endpoints of MIDI data
2546ac7032dSJohn Scipione	connections, much as media_source and media_destination do in the Media Kit.
2556ac7032dSJohn Scipione	Thus, a MIDI filter does not derive from BMidiConsumer and BMidiProducer;
2566ac7032dSJohn Scipione	instead, it contains BMidiConsumer and BMidiProducer objects for each of its
2576ac7032dSJohn Scipione	distinct endpoints that connect to other MIDI objects. The Midi Kit does not
2586ac7032dSJohn Scipione	allow the use of multiple virtual inheritance, so you can't create an object
2596ac7032dSJohn Scipione	that's both a BMidiConsumer and a BMidiProducer.
2606ac7032dSJohn Scipione
2616ac7032dSJohn Scipione	This also contrasts with the old Midi Kit's conception of a BMidi object,
2626ac7032dSJohn Scipione	which stood for an object that both received and sent MIDI data. In the new
2636ac7032dSJohn Scipione	Midi Kit, the endpoints of MIDI connections are all that matters. What lies
2646ac7032dSJohn Scipione	between the endpoints, i.e. how a MIDI filter is actually structured, is
2656ac7032dSJohn Scipione	entirely at your discretion.
2666ac7032dSJohn Scipione
2676ac7032dSJohn Scipione	Also, rather than use token structs like media_node to make connections
2686ac7032dSJohn Scipione	via the MediaRoster, the new kit makes the connections directly via the
2696ac7032dSJohn Scipione	BMidiProducer object.
2706ac7032dSJohn Scipione
2716ac7032dSJohn Scipione	\section midi2remotelocal Remote vs. Local Objects
2726ac7032dSJohn Scipione
2736ac7032dSJohn Scipione	The Midi Kit makes a distinction between remote and local MIDI objects.
2746ac7032dSJohn Scipione	You can  only create local MIDI endpoints, which derive from either
2756ac7032dSJohn Scipione	BMidiLocalConsumer or BMidiLocalProducer. Remote endpoints are endpoints
2766ac7032dSJohn Scipione	that live in other applications, and you access them through BMidiRoster.
2776ac7032dSJohn Scipione
2786ac7032dSJohn Scipione	BMidiRoster only gives you access to BMidiEndpoints, BMidiConsumers, and
2796ac7032dSJohn Scipione	BMidiProducers. When you want to talk to remote MIDI objects, you do so
2806ac7032dSJohn Scipione	through the proxy objects that BMidiRoster provides. Unlike
2816ac7032dSJohn Scipione	BMidiLocalConsumer and BMidiLocalProducer, these classes do not provide a
2826ac7032dSJohn Scipione	lot of functions. That is intentional. In order to hide the details of
2836ac7032dSJohn Scipione	communication with MIDI endpoints in other applications, the Midi Kit must
2846ac7032dSJohn Scipione	hide the details of how a particular endpoint is implemented.
2856ac7032dSJohn Scipione
2866ac7032dSJohn Scipione	So what can you do with remote objects? Only what BMidiConsumer,
2876ac7032dSJohn Scipione	BMidiProducer, and BMidiEndpoint will let you do. You can connect
2886ac7032dSJohn Scipione	objects, get the properties of these objects -- and that's about it.
2896ac7032dSJohn Scipione
2906ac7032dSJohn Scipione	\section midi2lifespan Creating and Destroying Objects
2916ac7032dSJohn Scipione
2926ac7032dSJohn Scipione	The constructors and destructors of most midi2 classes are private,
2936ac7032dSJohn Scipione	which means that you cannot directly create them using the C++
2946ac7032dSJohn Scipione	<CODE>new</CODE> operator, on the  stack, or as globals. Nor can you
2956ac7032dSJohn Scipione	<CODE>delete</CODE> them. Instead, these objects are obtained through
2966ac7032dSJohn Scipione	BMidiRoster. The only two exceptions to this rule are BMidiLocalConsumer
2976ac7032dSJohn Scipione	and BMidiLocalProducer. These two objects may be directly created and
2986ac7032dSJohn Scipione	subclassed by developers.
2996ac7032dSJohn Scipione
3006ac7032dSJohn Scipione	\section midi2refcount Reference Counting
3016ac7032dSJohn Scipione
3026ac7032dSJohn Scipione	Each MIDI endpoint has a reference count associated with it, so that
3036ac7032dSJohn Scipione	the Midi Roster can do proper bookkeeping. When you construct a
3046ac7032dSJohn Scipione	BMidiLocalProducer or  BMidiLocalConsumer endpoint, it starts with a
3056ac7032dSJohn Scipione	reference count of 1. In addition, BMidiRoster increments the reference
3066ac7032dSJohn Scipione	count of any object it hands to you as a result of
3076ac7032dSJohn Scipione	\link BMidiRoster::NextEndpoint() NextEndpoint() \endlink or
3086ac7032dSJohn Scipione	\link BMidiRoster::FindEndpoint() FindEndpoint() \endlink.
3096ac7032dSJohn Scipione	Once the count hits  0, the endpoint will be deleted.
3106ac7032dSJohn Scipione
3116ac7032dSJohn Scipione	This means that, to delete an endpoint, you don't call the
3126ac7032dSJohn Scipione	<CODE>delete</CODE>  operator directly; instead, you call
3136ac7032dSJohn Scipione	\link BMidiEndpoint::Release() Release() \endlink.
3146ac7032dSJohn Scipione	To balance this call, there's also an
3156ac7032dSJohn Scipione	\link BMidiEndpoint::Acquire() Acquire() \endlink, in case you have two
3166ac7032dSJohn Scipione	disparate parts of your application working with the endpoint, and you
3176ac7032dSJohn Scipione	don't want to have to keep track of who needs to Release() the endpoint.
3186ac7032dSJohn Scipione
3196ac7032dSJohn Scipione	When you're done with any endpoint object, you must Release() it.
3206ac7032dSJohn Scipione	This is true  for both local and remote objects. Repeat after me:
3216ac7032dSJohn Scipione	Release() when you're done.
3226ac7032dSJohn Scipione
3236ac7032dSJohn Scipione	\section midi2events MIDI Events
3246ac7032dSJohn Scipione
3256ac7032dSJohn Scipione	To make some actual music, you need to
3266ac7032dSJohn Scipione	\link BMidiProducer::Connect() Connect() \endlink your consumers to
3276ac7032dSJohn Scipione	your producers. Then you tell the producer to "spray" MIDI events to all
3286ac7032dSJohn Scipione	the connected consumers. The consumers are notified of these incoming
3296ac7032dSJohn Scipione	events through a set of hook functions.
3306ac7032dSJohn Scipione
3316ac7032dSJohn Scipione	The Midi Kit already provides a set of commonly used spray functions,
3326ac7032dSJohn Scipione	such as  \link BMidiLocalProducer::SprayNoteOn() SprayNoteOn() \endlink,
3336ac7032dSJohn Scipione	\link BMidiLocalProducer::SprayControlChange() SprayControlChange()
3346ac7032dSJohn Scipione	\endlink, and so on. These correspond one-to-one with the message types
3356ac7032dSJohn Scipione	from the MIDI spec. You don't need to be a MIDI expert to use the kit, but
3366ac7032dSJohn Scipione	of course some knowledge of the protocol helps. If you are really hardcore,
3376ac7032dSJohn Scipione	you can also use the
3386ac7032dSJohn Scipione	\link BMidiLocalProducer::SprayData() SprayData() \endlink to send raw MIDI
3396ac7032dSJohn Scipione	events to the consumers.
3406ac7032dSJohn Scipione
3416ac7032dSJohn Scipione	At the consumer side, a dedicated thread invokes a hook function for every
3426ac7032dSJohn Scipione	incoming MIDI event. For every spray function, there is a corresponding hook
3436ac7032dSJohn Scipione	function, e.g. \link BMidiLocalConsumer::NoteOn() NoteOn() \endlink and
3446ac7032dSJohn Scipione	\link  BMidiLocalConsumer::ControlChange() ControlChange() \endlink.
3456ac7032dSJohn Scipione	The hardcore MIDI fanatics among you will be pleased to know that you can
3466ac7032dSJohn Scipione	also tap into the \link BMidiLocalConsumer::Data() Data() \endlink hook and
3476ac7032dSJohn Scipione	get your hands dirty with the raw MIDI data.
3486ac7032dSJohn Scipione
3496ac7032dSJohn Scipione	\section midi2time Time
3506ac7032dSJohn Scipione
3516ac7032dSJohn Scipione	The spray and hook functions accept a bigtime_t parameter named "time". This
3526ac7032dSJohn Scipione	indicates when the MIDI event should be performed. The time is given in
3536ac7032dSJohn Scipione	microseconds since the computer booted. To get the current tick measurement,
3546ac7032dSJohn Scipione	you call the system_time() function from the Kernel Kit.
3556ac7032dSJohn Scipione
3566ac7032dSJohn Scipione	If you override a hook function in one of your consumer objects, it should
3576ac7032dSJohn Scipione	look  at the time argument, wait until the designated time, and then perform
3586ac7032dSJohn Scipione	its action. The preferred method is to use the Kernel Kit's
3596ac7032dSJohn Scipione	<CODE>snooze_until()</CODE> function, which sends the consumer thread to
3606ac7032dSJohn Scipione	sleep  until the requested time has come. (Or, if the time has already
3616ac7032dSJohn Scipione	passed, returns immediately.)
3626ac7032dSJohn Scipione
3636ac7032dSJohn Scipione	Like this:
3646ac7032dSJohn Scipione
3656ac7032dSJohn Scipione	\code
3666ac7032dSJohn Scipionevoid MyConsumer::NoteOn(
3676ac7032dSJohn Scipione    uchar channel, uchar note, uchar velocity, bigtime_t time)
3686ac7032dSJohn Scipione{
3696ac7032dSJohn Scipione    snooze_until(time, B_SYSTEM_TIMEBASE);
3706ac7032dSJohn Scipione    ...do your thing...
3716ac7032dSJohn Scipione}
3726ac7032dSJohn Scipione	\endcode
3736ac7032dSJohn Scipione
3746ac7032dSJohn Scipione	If you want your producers to run in real time, i.e. they produce MIDI data
3756ac7032dSJohn Scipione	that needs to be performed immediately, you should pass time 0 to the spray
3766ac7032dSJohn Scipione	functions (which also happens to be the default value). Since time 0 has
3776ac7032dSJohn Scipione	already passed, <CODE>snooze_until()</CODE> returns immediately, and the
3786ac7032dSJohn Scipione	consumer will process the events as soon as they are received.
3796ac7032dSJohn Scipione
3806ac7032dSJohn Scipione	To schedule MIDI events for a performance time that lies somewhere in the
3816ac7032dSJohn Scipione	future, the producer must take into account the consumer's latency.
3826ac7032dSJohn Scipione	Producers  should attempt to get notes to the consumer by or before
3836ac7032dSJohn Scipione	<I>(scheduled_performance_time - latency)</I>. The time argument is still
3846ac7032dSJohn Scipione	the scheduled performance time, so if your consumer has latency, it should
3856ac7032dSJohn Scipione	snooze like this before it starts to perform the events:
3866ac7032dSJohn Scipione
3876ac7032dSJohn Scipione	\code
3886ac7032dSJohn Scipionesnooze_until(time - Latency(), B_SYSTEM_TIMEBASE);
3896ac7032dSJohn Scipione	\endcode
3906ac7032dSJohn Scipione
3916ac7032dSJohn Scipione	Note that a typical producer sends out its events as soon as it can;
3926ac7032dSJohn Scipione	unlike a consumer, it does not have to snooze.
3936ac7032dSJohn Scipione
3946ac7032dSJohn Scipione	\section midi2ports Other Timing Issues
3956ac7032dSJohn Scipione
3966ac7032dSJohn Scipione	Each consumer object uses a Kernel Kit port to receive MIDI events from
3976ac7032dSJohn Scipione	connected producers. The queue for this port is only 1 message deep.
3986ac7032dSJohn Scipione	This means that if the consumer thread is asleep in a
3996ac7032dSJohn Scipione	<CODE>snooze_until()</CODE>, it will not read its port. Consequently,
4006ac7032dSJohn Scipione	any producer that tries to write a new event to this port will block until
4016ac7032dSJohn Scipione	the consumer thread is ready to receive a new message. This is intentional,
4026ac7032dSJohn Scipione	because it prevents producers from generating and queueing up thousands of
4036ac7032dSJohn Scipione	events.
4046ac7032dSJohn Scipione
4056ac7032dSJohn Scipione	This mechanism, while simple, puts on the producer the responsibility
4066ac7032dSJohn Scipione	for sorting the events in time. Suppose your producer sends three Note
4076ac7032dSJohn Scipione	On events, the first on t + 0, the second on t + 4, and the third on t + 2.
4086ac7032dSJohn Scipione	This last event won't be received until after t + 4, so it will be two ticks
4096ac7032dSJohn Scipione	too late. If this sort of thing can happen with your producer, you should
4106ac7032dSJohn Scipione	somehow sort the events before you spray them. Of course, if you have two or
4116ac7032dSJohn Scipione	more producers connected to the same consumer, it is nearly impossible to
4126ac7032dSJohn Scipione	sort this all out (pardon the pun). So it is not wise to send the same kinds
4136ac7032dSJohn Scipione	of events from more than one producer to one consumer at the same time.
4146ac7032dSJohn Scipione
4156ac7032dSJohn Scipione	The article Introduction to MIDI, Part 2 in <A
4164d556706SAugustin Cavalier	HREF="https://open-beos.sourceforge.net/nsl.php?mode=display&id=36">OpenBeOS
4176ac7032dSJohn Scipione	Newsletter 36</A> describes this problem in more detail, and provides a
4186ac7032dSJohn Scipione	solution. Go read it now!
4196ac7032dSJohn Scipione
4206ac7032dSJohn Scipione	\section midi2filters Writing a Filter
4216ac7032dSJohn Scipione
4226ac7032dSJohn Scipione	A typical filter contains a consumer and a producer endpoint. It receives
4236ac7032dSJohn Scipione	events from the consumer, processes them, and sends them out again using the
4246ac7032dSJohn Scipione	producer. The consumer endpoint is a subclass of BMidiLocalConsumer, whereas
4256ac7032dSJohn Scipione	the producer is simply a BMidiLocalProducer, not a subclass. This is a
4266ac7032dSJohn Scipione	common  configuration, because consumers work by overriding the event hooks
4276ac7032dSJohn Scipione	to do work  when MIDI data arrives. Producers work by sending an event when
4286ac7032dSJohn Scipione	you call their  member functions. You should hardly ever need to derive from
4296ac7032dSJohn Scipione	BMidiLocalProducer (unless you need to know when the producer gets connected
4306ac7032dSJohn Scipione	or disconnected, perhaps), but you'll always have to override one or more of
4316ac7032dSJohn Scipione	BMidiLocalConsumer's member functions to do something useful with incoming
4326ac7032dSJohn Scipione	data.
4336ac7032dSJohn Scipione
4346ac7032dSJohn Scipione	Filters should ignore the time argument from the spray and hook functions,
4356ac7032dSJohn Scipione	and  simply pass it on unchanged. Objects that only filter data should
4366ac7032dSJohn Scipione	process the  event as quickly as possible and be done with it. Do not
4376ac7032dSJohn Scipione	<CODE>snooze_until()</CODE> in the consumer endpoint of a filter!
4386ac7032dSJohn Scipione
4396ac7032dSJohn Scipione	\section midi2apidiffs API Differences
4406ac7032dSJohn Scipione
4416ac7032dSJohn Scipione	As far as the end user is concerned, the Haiku Midi Kit is mostly the same
4426ac7032dSJohn Scipione	as the BeOS R5 kits, although there are a few small differences in the API
4436ac7032dSJohn Scipione	(mostly bug fixes):
4446ac7032dSJohn Scipione		- BMidiEndpoint::IsPersistent() always returns false.
4456ac7032dSJohn Scipione		- The B_MIDI_CHANGE_LATENCY notification is now properly sent. The Be
4466ac7032dSJohn Scipione			kit  incorrectly set be:op to B_MIDI_CHANGED_NAME, even though the
4476ac7032dSJohn Scipione			rest of the  message was properly structured.
4486ac7032dSJohn Scipione		- If creating a local endpoint fails, you can still Release() the object
4496ac7032dSJohn Scipione		  without crashing into the debugger.
4506ac7032dSJohn Scipione
4516ac7032dSJohn Scipione	\section midi2seealso See also
4526ac7032dSJohn Scipione
4536ac7032dSJohn Scipione	More about the Midi Kit:
4546ac7032dSJohn Scipione		- \ref Midi2Defs.h
4556ac7032dSJohn Scipione		- Be Newsletter Volume 3, Issue 47 - Motor Mix sample code
4566ac7032dSJohn Scipione		- Be Newsletter Volume 4, Issue 3 - Overview of the new kit
4574d556706SAugustin Cavalier		- <A HREF="https://haiku-os.org/documents/dev/introduction_to_midi_part_1">Newsletter
4586ac7032dSJohn Scipione		  33</A>, Introduction to MIDI, Part 1
4594d556706SAugustin Cavalier		- <A HREF="https://haiku-os.org/documents/dev/introduction_to_midi_part_2">Newsletter
4606ac7032dSJohn Scipione		  36</A>, Introduction to MIDI, Part 2
4616ac7032dSJohn Scipione		- Sample code and other goodies at the
4624d556706SAugustin Cavalier		  <A HREF="https://haiku-os.org/about/teams/midi_kit">Haiku Midi Kit team page</A>
4636ac7032dSJohn Scipione
4646ac7032dSJohn Scipione	Information about MIDI in general:
4654d556706SAugustin Cavalier		- <A HREF="https://www.midi.org">MIDI Manufacturers Association</A>
4664d556706SAugustin Cavalier		- <A HREF="https://www.borg.com/~jglatt/tutr/miditutr.htm">MIDI Tutorials</A>
4674d556706SAugustin Cavalier		- <A HREF="https://www.borg.com/~jglatt/tech/midispec.htm">MIDI Specification</A>
4684d556706SAugustin Cavalier		- <A HREF="https://www.borg.com/~jglatt/tech/midifile.htm">Standard MIDI File Format</A>
4694d556706SAugustin Cavalier		- <A HREF="https://www.io.com/~jimm/midi_ref.html">Jim Menard's MIDI Reference</A>
4706ac7032dSJohn Scipione
4716ac7032dSJohn Scipione
4724cf62172SAdrien Destugues	\defgroup network Network Kit
4734cf62172SAdrien Destugues	\brief Classes that deal with all network connections and communications.
4746ac7032dSJohn Scipione
475b1a9f638SJohn Scipione	The Haiku Network Kit consists of:
476b1a9f638SJohn Scipione	- A modular, add-ons based network stack
477b1a9f638SJohn Scipione	- Two shared libraries, libnet.so and libnetapi.so
478b1a9f638SJohn Scipione	- A stack driver, acting as interface between the network stack and
479b1a9f638SJohn Scipione	  libnet.so
480b1a9f638SJohn Scipione	- Basic network apps
481b1a9f638SJohn Scipione	- A modular GUI preflet
482b1a9f638SJohn Scipione
483b1a9f638SJohn Scipione	The libnet.so shared library is the way that BeOS R5 provided POSIX/BSD
484b1a9f638SJohn Scipione	API sockets to apps. Being binary compatible with BeOS R5 has made this
485b1a9f638SJohn Scipione	library implementation tedious. To counter this, the libnetapi.so shared
486b1a9f638SJohn Scipione	library was developed. It contains thin C++ classes wrapping the C
487b1a9f638SJohn Scipione	sockets POSIX/BSD API into these BNet* classes we're used under BeOS.
488b1a9f638SJohn Scipione
489b1a9f638SJohn Scipione	The stack driver is the interface between libnet.so and the real stack
490b1a9f638SJohn Scipione	behind it, hosted by the network stack kernel modules. Its purposes
491b1a9f638SJohn Scipione	include:
492b1a9f638SJohn Scipione	-# Providing sockets to file descriptors translation support
493b1a9f638SJohn Scipione	-# Providing support for select() on sockets
494b1a9f638SJohn Scipione	-# Loading the network stack on first access, and then keeping it for
495b1a9f638SJohn Scipione	   further accesses
496b1a9f638SJohn Scipione
497b1a9f638SJohn Scipione	The following diagram illustrates the network stack design on Haiku:
498b1a9f638SJohn Scipione
499b1a9f638SJohn Scipione	\image html obos_net_stack_design_1.gif
500b1a9f638SJohn Scipione
501b1a9f638SJohn Scipione	The Network Kit includes a handful of useful networking related apps
502b1a9f638SJohn Scipione	including ping, ifconfig, route, traceroute, and arp.
503b1a9f638SJohn Scipione
504b1a9f638SJohn Scipione	See the User Guide for more information about the
5054d556706SAugustin Cavalier	<a href="https://haiku-os.org/docs/userguide/en/preferences/network.html">Network preferences app</a>
506b1a9f638SJohn Scipione	included as part of the Network Kit.
507b1a9f638SJohn Scipione
508b1a9f638SJohn Scipione
5096ac7032dSJohn Scipione	\defgroup storage Storage Kit
5106ac7032dSJohn Scipione	\brief Collection of classes that deal with storing and retrieving
5116ac7032dSJohn Scipione		information from disk.
5126ac7032dSJohn Scipione
5136ac7032dSJohn Scipione
5146ac7032dSJohn Scipione	\defgroup support Support Kit
5156ac7032dSJohn Scipione	\brief Collection of utility classes that are used throughout the API.
5166ac7032dSJohn Scipione
5176ac7032dSJohn Scipione	The Support Kit provides a handy set of classes that you can use in your
5186ac7032dSJohn Scipione	applications. These classes provide:
5196ac7032dSJohn Scipione		- \b Thread \b Safety. Haiku can execute multiple threads of an
5206ac7032dSJohn Scipione			application in parallel, letting certain parts of an application
5216ac7032dSJohn Scipione			continue when one part is stalled, as well as letting an application
5226ac7032dSJohn Scipione			process multiple pieces of data at the same time on multicore or
5236ac7032dSJohn Scipione			multiprocessor systems. However, there are times when multiple
5246ac7032dSJohn Scipione			threads desire to work on the same piece of data at the same time,
5256ac7032dSJohn Scipione			potentially causing a conflict where variables or pointers are
5266ac7032dSJohn Scipione			changed by one thread causing another to execute incorrectly. To
5276ac7032dSJohn Scipione			prevent this, Haiku implements a \"locking\" mechanism, allowing one
5286ac7032dSJohn Scipione			thread to \"lock out\" other threads from executing code that might
5296ac7032dSJohn Scipione			modify the same data.
5306ac7032dSJohn Scipione		  - \b Archiving \b and \b IO. These classes allow a programmer to
5316ac7032dSJohn Scipione			convert objects into a form that can more easily be transferred to
5326ac7032dSJohn Scipione			other applications or stored to disk, as well as performing basic
5336ac7032dSJohn Scipione			input and output operations.
5346ac7032dSJohn Scipione		  - \b Memory \b Allocation. This class allows a programmer to hand off
5356ac7032dSJohn Scipione			some of the duties of memory accounting and management.
5366ac7032dSJohn Scipione		  - \b Common \b Datatypes. To avoid unnecessary duplication of code
5376ac7032dSJohn Scipione			and to make life easier for programmers, Haiku includes classes that
5386ac7032dSJohn Scipione			handle management of ordered lists and strings.
5396ac7032dSJohn Scipione
5406ac7032dSJohn Scipione	There are also a number of utility functions to time actions, play system
5416ac7032dSJohn Scipione	alert sounds, compare strings, and atomically manipulate integers. Have a
5426ac7032dSJohn Scipione	look at the overview, or go straight to the complete
5436ac7032dSJohn Scipione	\link support list of components \endlink of this kit.
5446ac7032dSJohn Scipione
5456ac7032dSJohn Scipione	\section Overview
5466ac7032dSJohn Scipione		- Thread Safety:
5476ac7032dSJohn Scipione			- BLocker provides a semaphore-like locking mechanism allowing for
5486ac7032dSJohn Scipione				recursive locks.
5496ac7032dSJohn Scipione			- BAutolock provides a simple method of automatically removing a
5506ac7032dSJohn Scipione				lock when a function ends.
5516ac7032dSJohn Scipione			- \ref TLS.h "Thread Local Storage" allows a global variable\'s
5526ac7032dSJohn Scipione				content to be sensitive to thread context.
5536ac7032dSJohn Scipione		- Archiving and IO:
5546ac7032dSJohn Scipione			- BArchivable provides an interface for \"archiving\" objects so
5556ac7032dSJohn Scipione				that they may be sent to other applications where an identical
5566ac7032dSJohn Scipione				copy will be recreated.
5576ac7032dSJohn Scipione			- BArchiver simplifies archiving of BArchivable hierarchies.
5586ac7032dSJohn Scipione			- BUnarchiver simplifies unarchiving hierarchies that have been
5596ac7032dSJohn Scipione				archived using BArchiver.
5606ac7032dSJohn Scipione			- BFlattenable provides an interface for \"flattening\" objects so
5616ac7032dSJohn Scipione				that they may be easily stored to disk.
5626ac7032dSJohn Scipione		- BDataIO provides an interface for generalized read/write streams.
5636ac7032dSJohn Scipione			- BPositionIO extends BDataIO to allow seeking within the data.
5646ac7032dSJohn Scipione			- BBufferIO creates a buffer and attaches it to a BPositionIO
5656ac7032dSJohn Scipione				stream, allowing for reduced load on the underlying stream.
5666ac7032dSJohn Scipione			- BMemoryIO allows operation on an already-existing buffer.
5676ac7032dSJohn Scipione			- BMallocIO creates and allows operation on a buffer.
5686ac7032dSJohn Scipione		- Memory Allocation:
5696ac7032dSJohn Scipione			- BBlockCache allows an application to allocate a \"pool\" of
5706ac7032dSJohn Scipione				memory blocks that the application can fetch and dispose of as
5716ac7032dSJohn Scipione				it pleases, letting the application make only a few large memory
5726ac7032dSJohn Scipione				allocations, instead of many small expensive allocations.
5736ac7032dSJohn Scipione		- Common Datatypes:
5746ac7032dSJohn Scipione			- BList allows simple ordered lists and provides common access,
5756ac7032dSJohn Scipione				modification, and comparison functions.
5766ac7032dSJohn Scipione			- BString allows strings and provides common access, modification,
5776ac7032dSJohn Scipione				and comparison functions.
5786ac7032dSJohn Scipione		- BStopWatch allows an application to measure the time an action takes.
5796ac7032dSJohn Scipione			- \ref support_globals "Global functions"
5806ac7032dSJohn Scipione			- \ref TypeConstants.h "Common types and constants"
5816ac7032dSJohn Scipione			- Error codes for all kits
5826ac7032dSJohn Scipione
5836ac7032dSJohn Scipione
5843528f5bbSJohn Scipione	\defgroup translation Translation Kit
5853528f5bbSJohn Scipione	\brief Provides a framework for converting data streams between media
5863528f5bbSJohn Scipione		formats.
5873528f5bbSJohn Scipione
5883528f5bbSJohn Scipione
58902bdb5dbSFrançois Revol	\defgroup libtranslation (libtranslation.so)
59002bdb5dbSFrançois Revol
5916ac7032dSJohn Scipione	\defgroup libbe (libbe.so)
5926ac7032dSJohn Scipione
5936ac7032dSJohn Scipione
5946ac7032dSJohn Scipione	\defgroup libroot (libroot.so)
5950524a6a8Smahlzeit*/
5969d54b143SNiels Sascha Reedijk
5979889ca4aSNiels Sascha Reedijk///// Subgroups /////
5989d54b143SNiels Sascha Reedijk
5999d54b143SNiels Sascha Reedijk/*!
600dba29137Swaddlesplash	\defgroup support_globals Global functions
6019d54b143SNiels Sascha Reedijk	\ingroup support
602cc19e7c0SAlex Wilson
603dba29137Swaddlesplash	\defgroup layout Layout API
604dba29137Swaddlesplash	\brief Provides classes for automatically laying out UIs.
605cc19e7c0SAlex Wilson	\ingroup interface
6069d54b143SNiels Sascha Reedijk*/
6074466b89cSJohn Scipione
6084466b89cSJohn Scipione
6094466b89cSJohn Scipione///// Special Topics /////
6104466b89cSJohn Scipione
611aa6ef665SAugustin Cavalier/*!
6124466b89cSJohn Scipione	\defgroup drivers Device Drivers
613aa6ef665SAugustin Cavalier*/
614