I have been spending more time looking into the porting process and thinking
about solutions. In addition to the replacement of various Toolbox calls as
already discussed, there are a few other steps that need to be taken. These
are described in Apple's book "Carbon Porting Guide" available at
<http://developer.apple.com/documentation/Carbon/Conceptual/carbon_porting_g
uide/index.html>
Here is an overview as it pertains to BP2:
1. Make sure all code is PPC-native
This is simple since there are no custom definition procedures. The
built-in MIDI driver will have to go but that was already true for
other reasons as well.
2. Update to recent Universal Interfaces (and CodeWarrior 8)
This will require some work as BP2 currently does not compile with
CW8 (but does with Cwpro4). Many of the errors seem to be because
of name conflicts with newer Toolbox calls and greater strictness
in the compiler. Hopefully only 4-8 hours of work to finish.
Targeting OS 9 first (this applies to steps 3-6 below)
It is easiest to do the porting on OS 9 so that changes can be made
slowly and the application will still compile and run. (I also want
to conditionalize all changes so that a non-Carbon build can be
maintained for now -- that way we will not leave behind anyone who
is still using BP2 on OS 7-9 until it is absolutely necessary). Once
the application can be built linking only to CarbonLib, then we can
begin testing on OS X.
3. Eliminate direct access to opaque structures
Many common toolbox structs are opaque in Carbon and their members
must be accessed via accessor functions. This includes WindowRecords,
DialogRecords, GrafPorts, ControlRecords, QuickDraw globals, etc.
I have not audited BP2 in this regard, and I expect a significant
amount of work (maybe as much as "simple replacements" for obsolete
Toolbox calls). Fortunately, Apple has made it easy to do this one
file at a time and keep the application working (on OS 9).
4. Use "casting functions" for converting between certain pointer types.
WindowPtr, GrafPtr, and DialogPtr can no longer be used interchangably
in Carbon and every occurrence of code that assumes they can be must
instead call a function to convert between pointer types. This will be
quite a bit of work too but can be done at the same time as step #4.
5. Remove direct access to Low-Memory Globals
The Carbon Report did not find any instances of this. Hopefully, it is
correct.
6. Update modified or obsolete functions
I have already described this part in detail. I have found that many
of the functions will take at least one extra parameter so it will be
marginally more difficult than an auotmatic search and replace.
Adopting replacements for entire toolboxes (Navigation Services,
Carbon Printing, and possibly MLTE) will take the most time.
7. Adding a 'plst' resource and conditionalizing Quit menu item
These are required for a Carbon app to run on OS X (the Quit menu item
is in a different location). Both are trivial.
8. Making sure we don't write files to places we shouldn't
We cannot assume that the application resource fork or the application
directory are writable on OS X. I don't think BP2 writes to its
resource fork, but it does try to create files in its own folder.
Should not be too hard to add some OS X-only code to write these files
somewhere else instead (such as the user's preferences folder).
9. BP2's "message" windows at the bottom of the screen will have to be
relocated when the OS X Dock is in their way?
10. Apple has lots of recommendations for optimizing Carbon applications,
many of which may apply to BP. Some of these can be tackled in the
"second round" of changes after we have a working Carbon build.
More comments in response to Bernard's previous comments are below! :)
Bernard Bel wrote on 11/14/06 12:01 PM:
> Memory management may also be simplified. In "BP2main.c" I had created a
> procedure "GiveSpace()" to replace "NewHandle()", in which we first check
> whether enough space is available in the heap, and if it not we
> temporarily use the stack of the system. This is indeed obsolete.
Yes, this will likely need to be redone as allocating system memory is no
longer an option. (I think it will compile and run OK under Carbon, it is
just pointless).
> Another point that needs to be decided is the naming of files. I used
> prefixes "-gr", "-da", "+sc" etc. so that BP recognizes different sorts
> of text files. In MacOS X, file extensions do the same and the file
> "creator/type" information is overrun by the extension. We might simply
> decide to go on with prefixes (they are nice if you put files in a list
> and sort them on names) and append the ".txt" extension to all text
> files. Or rather use a specific extension, such as "bp3", that will
> launch BP3 when double-clicking any of its files.
This is an issue that I may even address for the final pre-Carbon release
(2.9.5 final). As it is, the prefix files will not get the correct file
types on OS 9 or OS X when checked out from CVS. And BP2 will not open a
file with type 'TEXT' as a grammar, etc. I think an immediate improvement
would be to allow opening any file with type 'TEXT' on OS 9.
As for OS X, I think we should adopt some file extension suffixes as
"standard" for each of BP's file types, and continue to set file and creator
types when saving files from BP (this is acceptable in OS X if frowned upon
by Apple). When opening a file, a popup menu could be provided for choosing
the type. For example, when opening a grammar, the choices would be "BP
grammar", "Any text file", "Any file". The filter for "BP grammar" could
include any file with the correct OS 9 type, the prefix "-gr", or the new
suffix (maybe .bpgr ?).
The user can then decide how they wish to name files (using prefixes,
suffixes, or any scheme of their own) and it will still be possible to open
them with BP.
> When using a different MIDI driver we will need to see whether it takes
> time-stamped events or it plays them immediately. The advantage of the
> second solution (i.e. the one of OMS) is that we keep control of the
> scheduling process and this may be useful when we need to flush the
> driver or suppress queued events beyond a certain limit of time, etc. It
> would be nice to keep it open for future development. In other words I
> suggest that we go on using our "schedule" procedure even if the MIDI
> driver allows for time-stamped data.
I think MidiShare and CoreMidi (the two drivers I have in mind for the OS X
port) will probably function like OMS, but I have not looked at them yet.
>> GetMenu
>>
>> I don't think the changed behavior of GetMenu will affect BP2 at all unless
>> it means that the GetMenu call in Script.c will be a memory leak (??).
>
> In "Script.c" the procedure ExecSriptLine() scans all the menus to try to
> match a line of the script with one of the items in a menu. It was a
> simple way of enhancing the script language automatically when a new menu
> item has been implemented. To do this we need to call GetMenu() and
> GetMenuItem().
Yes, I will just need to check whether we have to deallocate the structures
returned by GetMenu.
>> InsXTime
>>
>> Called in InstallTMTask() of Schedule.c. Should be #ifdefed to use
>> InstallXTimeTask for Carbon build. TDesc() and TDesc68K() may need to be
>> changed as well.
>
> This is hard stuff and we need to figure out the best way of implementing
> the scheduling procedure.
I think that the suggestion above will work as a first attempt.
>> --TextEdit calls--
>>
>> Most of these seem to be conditionally included in BP2 based on WASTE usage?
>> Not interested in converting to MLTE as recommended by Apple. However,
>> WASTE may require significant modification (?) and newer, already Carbonized
>> versions of WASTE are not free.
>
> I used WASTE for the only reason that it allows more than 32,000 signs in
> a text window. I do not think WASTE should be recycled (!) because I
> assume that in MacOS X you will have a variant of TextEdit that allows
> for more than 32,000 signs.
Yes, MLTE is a replacement for TextEdit allowing > 32K of text. It runs on
OS 8.6 and greater. The chief difficulty with it however is that it is
based on Unicode for storing text. I do not want to completely rewrite BP2
to support Unicode throughout just to get a Carbon port up and running. In
addition, we need to plan for the future port to Linux where Unicode may be
problematic.
What I would like to do is investigate three different options for the first
round of porting: 1) carbonizing WASTE, 2) using MLTE if it is simple
enough to wrap the code so that we can stick with ASCII text throughout the
rest of BP, or 3) "downgrading" to TextEdit temporarily until the text
editing can be completely reworked.
I would give preference to either 1 or 2 and my choice would depend on which
option is simpler. Option 3 would be a last resort since limiting text
files to 32K is very annoying.
My goal is to get to a mostly functional Carbon port of BP2 as fast as
possible. This means that in the first round of changes, I will be focusing
only on those parts of the code that HAVE to be changed and I would like to
give priority to solutions that are simpler to code when there is more than
one choice. Certainly, once we have a version of BP that can actually run
on OS X, we can go back and implement more ideal solutions for the long
term.
> In "BP2.h" there is a WASTE flag that sets up conditional compilation
> depending on whether we use WASTE or standard TextEdit. Let us set it to
> 0 and keep only the remaining code.
It is very handy that compatibility with TextEdit was maintained in the code
and this would be a useful route for a short term solution if it is both too
complicated to carbonize WASTE and too much of a rewrite to immediately add
MLTE support.
>> From the report: "The original Printing Manager defined in Printing.h is not
>> supported in Carbon. Carbon applications must use the Carbon Printing
>> Manager." This is a bummer :( I have no idea yet how much work this will
>> be. All seems to be confined to Print.c though. Perhaps Carbon printing
>> routines can go in a new file if the code will be significantly different.
>
> If you use a standard TextEdit procedure then most likely you will have a
> simple procedure for printing a text window... It may only be a bit more
> complicated with graphic windows.
> Although it is not of great interest to print BP graphics. :-(
MLTE is supposed to make it very easy to print text windows as well. I
honestly do not consider printing to be essential to the first round port
though. Since all of BP's data files are plain text, they can be opened in
any text editor (such as BBEdit) and printed from there. Therefore, I will
likely focus on getting the rest of BP2 ported before working on printing
and I think we could release a version of Carbon BP without printing if it
will take significantly longer to add.
Anthony Kozar
anthonykozar AT sbcglobal DOT net
|