Carbon (API)
56891
219760432
2008-06-16T19:35:01Z
68.34.156.186
/* Timers */ Clarifications. Timers actually schedule through the runloop and may actually dispatch using the same mechanism as the Carbon Event Manager event loop
{{Cleanup|date=February 2007}}
'''Carbon''' is [[Apple Inc.]]'s procedural [[Application programming interface|API]] for the [[Apple Macintosh|Macintosh]] [[operating system]], which permits a good degree of forward and backward compatibility between [[source code]] written to run on the older and now dated [[Mac OS history|Classic Mac OS]] ([[Mac OS 8.1|Version 8.1]] and later), and the newer [[Mac OS X]]. It is one of five [[Application programming interface|API]]s natively available for Mac OS X; the others are [[Cocoa (API)|Cocoa]], [[POSIX]] with the [[X Window System]], [[Macintosh Toolbox|Toolbox]] (for the obsolete [[Classic (Mac OS X)|Classic]] environment), and [[Java platform|Java]]. Carbon is not fully compatible with [[64-bit]] programs under the [[Mac OS 10.5|latest]] Mac OS X.
==Overview==
[[Image:Adobe ImageReady 7.0 running on Mac OS X.png|thumb|right|275px| “Carbonized” application [[Adobe]] [[ImageReady]] v.7.0 running directly on [[Mac OS X]] version [[Mac_OS_X_v10.2|10.2]]]]
The Carbon [[API]]s are published and accessed in the form of [[C (programming language)|C]] [[header file]]s and dynamically linkable [[software library|libraries]]. In [[Mac OS X]], much functionality is contained in ''ApplicationServices.framework''. In Classic [[Mac OS]], most functions are in a single library called '''CarbonLib'''. These different implementations of the APIs are interchangeable from the perspective of the [[executable]]. This permits a program that conforms to the Carbon specification to run natively on both operating systems. However, if an application uses a single function ''not'' in Carbon, compatibility with Mac OS X requires the [[Classic (Mac OS X)|Classic environment]].
The Carbon APIs were designed to include as many of the older [[Macintosh Toolbox|Toolbox]] APIs as possible, to permit easy porting of most [[legacy code]] to Mac OS X. Such porting was known as ''Carbonization''. Carbon also added new functionality and new abstractions. Previously, many [[data structure]]s of the OS were exposed and manipulated directly by the program. In Carbon, most such structures became fully [[Opacity (optics)|opaque]], and many new APIs were added to access them. This encouraged cleaner and less error-prone code, and made it easier for Apple to modify the operating system. Carbon removed some functions that were specifically attached to the older Mac OS, and removed some obsolete technologies altogether. [[Backward compatibility]] remained a focus as long as [[Mac OS 9]] was developed, as later updates such as 9.2.2 were largely to improve support for newer software. However, little Carbon software today remains compatible with Mac OS 9, as the interface has continued to evolve. Carbon was not intended to guarantee backward compatibility. If a programmer desires compatibility with Mac OS 9.1, they must test and debug it with Mac OS 9.1 specifically. Between Mac OS 8.6 and Mac OS 9.2.2, CarbonLib gradually evolved from an adaptation of the QuickTime for Windows user interface API into the basis for much of the later Classic Mac OS development.
Carbon is sometimes seen as a transitional or legacy technology. This is incorrect, and it is misleading to describe it as a technology per se. Carbon is a set of application-level Mac OS X APIs for the C programming language. They are the most efficient alternative when the underlying operating system functionality is also implemented in C. They are also the most versatile, accessible from [[C (programming language)|C]], [[C++]], [[Pascal programming language|Pascal]], [[Ada programming language|Ada]], or any other language with suitable interface headers. A higher level approach may be taken with Carbon by using an application framework built on it, for example [[MacApp]], [[PowerPlant|Metrowerks PowerPlant]] or [[MacZoop]]. Many parts of the [[Cocoa (API)|Cocoa API]] likewise implement Carbon for [[Objective-C]]. Also, many Carbon APIs provide C language access to functionality implemented in Objective-C. In general, it is inefficient for a programmer to be overly concerned with the underlying operating system implementation.
At [[WWDC#WWDC_2007|WWDC 2007]], Apple revealed that it will not be possible to compile Carbon apps as [[64-bit]] code for [[Mac OS X v10.5|Leopard]], contrary to previous statements.<ref>[http://arstechnica.com/journals/apple.ars/2007/06/13/64-bit-support-in-leopard-no-carbon-love "64-bit support in Leopard: no Carbon love"]</ref> Some lower-level parts of Carbon, such as the File Manager, are expected to be available in 64 bit.
==Architecture==
Carbon descends from the [[Macintosh Toolbox|Toolbox]], and as such, is composed of "Managers". Each Manager is a functionally-related API, defining sets of data structures and functions to manipulate them. Managers are often interdependent or layered.
Newer parts of Carbon tend to be much more object-oriented in their conception, most of them based on [[Core Foundation]]. Some Managers, such as the HIView Manager (a superset of the Control Manager), are implemented in [[C++]], but Carbon remains a [[C (programming language)|C]] API.
Some examples of Carbon Managers:
* File Manager — manages access to the file system, opening closing, reading and writing files.
* Resource Manager — manages access to resources, which are predefined chunks of data a program may require. Calls File Manager to read and write resources from disk files. Examples of resources include icons, sounds, images, templates for widgets, etc.
* Font Manager — manages [[Typeface|font]]s. Deprecated since [[Mac OS X v10.4]] because it is part of QuickDraw in favor of [[Apple Type Services]] (ATS).
* [[QuickDraw]] — 2D graphics primitives. Deprecated since [[Mac OS X v10.4]] in favor of Quartz 2D.
* Carbon Event Manager <!-- NOT to be confused with Event Manager --> — converts user and system activity into events that code can recognise and respond to.
* HIObject — a completely new object-oriented API which brings to Carbon an OO model for building GUIs. This is available in [[Mac OS X v10.2]] or later, and gives Carbon programmers some of the tools that Cocoa developers have long been familiar with. Starting with [[Mac OS X v10.2]], HIObject is the base class for all GUI elements in Carbon. HIView is supported by [[Interface Builder]], part of Apple's developer tools. Traditionally GUI architectures of this sort have been left to third-party application frameworks to provide. Starting with Mac OS X v10.4, HIObjects are NSObjects and inherit the ability to be serialized into data streams for transport or saving to disk.
* HITheme — uses QuickDraw and Quartz to render [[graphical user interface]] (GUI) elements to the screen. HITheme was introduced in [[Mac OS X v10.3]], and Appearance Manager is a compatibility layer on top of HITheme since that version.
* HIView Manager — manages creation, drawing, hit-testing, and manipulation of controls. Since Mac OS X v10.2, all controls are HIViews. In Mac OS X v10.4, the Control Manager was renamed HIView Manager.
* Window Manager — manages creation, positioning, updating, and manipulation of windows. Since Mac OS X v10.2, windows have a root HIView.
* Menu Manager — manages creation, selection, and manipulation of menus. Since Mac OS X v10.2, menus are HIObjects. Since Mac OS X v10.3, menu content may be drawn using HIViews, and all standard menus use HIViews to draw.
==Event handling==
The Mac Toolbox's Event Manager originally used a [[polling (computer science)|polling]] model for application design. The application's main [[event loop]] asks the Event Manager for an event using GetNextEvent. If there is an event in the queue, the Event Manager passes it back to the application, where it is handled, otherwise it returns immediately. This behavior is called "[[Busy waiting|busy-waiting]]", running the event loop unnecessarily. Busy-waiting reduces the amount of CPU time available for other applications and decreases battery power on laptops. The classic Event Manager dates from the original Mac OS in 1984, when whatever application was running was guaranteed to be the ''only'' application running, and where power management was not a concern.
With the advent of System 7.0 and the ability to run more than one application simultaneously came a new Event Manager call, ''WaitNextEvent'', which allows an application to specify a sleep interval. One easy trick for legacy code to adopt a more efficient model without major changes to its source code is simply to set the sleep parameter passed to ''WaitNextEvent'' to a very large value—on OS X, this puts the thread to sleep whenever there is nothing to do, and only returns an event when there is one to process. In this way, the polling model is quickly inverted to become equivalent to the callback model, with the application performing its own event dispatching in the original manner. There are loopholes, though. For one, the legacy toolbox call ''ModalDialog'', for example, calls the older ''GetNextEvent'' function internally, resulting in polling in a tight loop without blocking.
Carbon introduces a replacement system, called the Carbon Event Manager. The original Event Manager still exists for compatibility with legacy applications). Carbon Event Manager provides the event loop for the developer (based on Core Foundation's <code>CFRunLoop</code> in the current implementation); the developer sets up event handlers and enters the event loop in the main function, and waits for Carbon Event Manager to dispatch events to the application.
==Timers==
In the classic Mac OS, there was no operating system support for application level timers (the lower level Time Manager was available, but executed timer callbacks at interrupt time, during which you could not safely make calls to most Toolbox routines). Timers were usually left to application developers to implement, and this was usually done by counting elapsed time during the ''idle'' event - that is, an event that was returned by ''WaitNextEvent'' when any other event wasn't available. In order for such timers to have reasonable resolution, developers could not afford WNE to delay too long, and so low "sleep" parameters were usually set. This results in highly inefficient scheduling behaviour, since the thread will not sleep for very long, instead repeatedly waking to return these idle events. Apple added timer support to Carbon to address this problem -- the system can schedule timers with great efficiency.
== Notes ==
{{reflist}}
==External links==
* [http://developer.apple.com/carbon/ Apple Developer Connection: Carbon]
* [http://carbondev.com/ Carbon Dev]
{{Mac OS X}}
[[Category:Mac OS X APIs]]
[[da:Carbon (programmering)]]
[[de:Carbon (Apple)]]
[[es:Carbon (API)]]
[[fr:Carbon (Mac OS X)]]
[[it:Carbon (API)]]
[[ja:Carbon]]
[[zh:Carbon (API)]]