home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / development / pilot-template-1.31.pl < prev    next >
Perl Script  |  2000-12-07  |  9KB  |  366 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # pilot-template 1.31 : Create the template files for a PalmOS application
  4. # Copyright (C) 1998 Ian Goldberg <iang@cs.berkeley.edu>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. use strict 'vars';
  21.  
  22. print <<'EOC';
  23.  
  24. pilot-template 1.31 by Ian Goldberg <iang@cs.berkeley.edu>
  25.  
  26. EOC
  27.  
  28. my $Usage = <<EOU;
  29. Usage: $0 [-pbitm] prcname appname iconname CrID
  30.   [-pbitm]: If -pbitm is specified, make a blank icon in pbitm format;
  31.             otherwise, make a blank icon in pbm format
  32.   prcname : The desired name for the .prc file
  33.   appname : The desired name for the application (up to 31 chars)
  34.   iconname: The desired (short) name to appear in the application launcher
  35.   CrID    : The desired four-character Creator ID
  36.              (This _must_ be unique for every application in the world;
  37.           get a creator id at
  38.           http://palmpilot.3com.com/devzone/crid/crid.html)
  39.  
  40. Example: $0 template.prc 'Template application' 'Test app' Tmpl
  41. EOU
  42.  
  43. my $iconext = 'pbm';
  44. if ($#ARGV >= 0 && $ARGV[0] eq '-pbitm') {
  45.     $iconext = 'pbitm';
  46.     shift @ARGV;
  47. }
  48.  
  49. die $Usage unless $#ARGV == 3;
  50.  
  51. my ($prcname, $appname, $iconname, $crid) = @ARGV;
  52.  
  53. die $Usage unless length($crid) == 4 && length($appname) < 32;
  54.  
  55. ## Strip the .prc, if any, from the prcname
  56. $prcname =~ s/\.prc$//;
  57.  
  58. ## Make sure none of the files already exists
  59. my $f;
  60. foreach $f ("Makefile", $prcname.".c", $prcname.".".${iconext},
  61.         $prcname.".rcp", $prcname."Rsc.h", "callback.h") {
  62.     die "File $f already exists - aborting.\n" if -e $f;
  63. }
  64.  
  65. ## Create the files
  66. open(F, ">Makefile") or die "Cannot write Makefile: $!\n";
  67. print F <<EOFILE;
  68. ## Makefile for $appname
  69.  
  70. TARGET = $prcname
  71. APPNAME = "$appname"
  72. APPID = "$crid"
  73. EOFILE
  74. print F <<'EOFILE';
  75.  
  76. OBJS = $(TARGET).o
  77. LIBS =
  78.  
  79. CC = m68k-palmos-coff-gcc
  80.  
  81. CFLAGS = -Wall -g -O2
  82.  
  83. PILRC = pilrc
  84. OBJRES = m68k-palmos-coff-obj-res
  85. NM = m68k-palmos-coff-nm
  86. BUILDPRC = build-prc
  87. PILOTXFER = pilot-xfer
  88.  
  89. all: $(TARGET).prc
  90.  
  91. .S.o:
  92.     $(CC) $(TARGETFLAGS) -c $<
  93.  
  94. .c.s:
  95.     $(CC) $(CSFLAGS) $<
  96.  
  97. $(TARGET).prc: code0000.$(TARGET).grc code0001.$(TARGET).grc data0000.$(TARGET).grc pref0000.$(TARGET).grc rloc0000.$(TARGET).grc bin.res
  98.     $(BUILDPRC) $(TARGET).prc $(APPNAME) $(APPID) code0001.$(TARGET).grc code0000.$(TARGET).grc data0000.$(TARGET).grc *.bin pref0000.$(TARGET).grc rloc0000.$(TARGET).grc
  99.  
  100. code0000.$(TARGET).grc: $(TARGET)
  101.     $(OBJRES) $(TARGET)
  102.  
  103. code0001.$(TARGET).grc: code0000.$(TARGET).grc
  104.  
  105. data0000.$(TARGET).grc: code0000.$(TARGET).grc
  106.  
  107. pref0000.$(TARGET).grc: code0000.$(TARGET).grc
  108.  
  109. rloc0000.$(TARGET).grc: code0000.$(TARGET).grc
  110.  
  111. EOFILE
  112. print F <<EOFILE;
  113. bin.res: \$(TARGET).rcp \$(TARGET).$iconext
  114. EOFILE
  115. print F <<'EOFILE';
  116.     $(PILRC) $(TARGET).rcp .
  117.     touch bin.res
  118.  
  119. $(TARGET): $(OBJS)
  120.     $(CC) $(CFLAGS) $(OBJS) -o $(TARGET) $(LIBS)
  121.     ! $(NM) -u $(TARGET) | grep .
  122.  
  123. send: $(TARGET).prc
  124.     $(PILOTXFER) -i $(TARGET).prc
  125.  
  126. depend:
  127.     makedepend -Y -I. *.c
  128.  
  129. clean:
  130.     -rm -f *.[oa] $(TARGET) *.bin bin.res *.grc Makefile.bak
  131.  
  132. veryclean: clean
  133.     -rm -f $(TARGET).prc pilot.ram pilot.scratch
  134.  
  135. EOFILE
  136. close(F);
  137.  
  138. open(F, ">${prcname}.c") or die "Cannot write ${prcname}.c: $!\n";
  139. print F <<EOFILE;
  140. /* Main code for $appname */
  141.  
  142. #include <Pilot.h>
  143. #include "callback.h"
  144.  
  145. #include "${prcname}Rsc.h"
  146.  
  147. static Boolean MainFormHandleEvent (EventPtr e)
  148. {
  149.     Boolean handled = false;
  150.     FormPtr frm;
  151.     
  152.     CALLBACK_PROLOGUE
  153.  
  154.     switch (e->eType) {
  155.     case frmOpenEvent:
  156.     frm = FrmGetActiveForm();
  157.     FrmDrawForm(frm);
  158.     handled = true;
  159.     break;
  160.  
  161.     case menuEvent:
  162.     MenuEraseStatus(NULL);
  163.  
  164.     switch(e->data.menu.itemID) {
  165.     }
  166.  
  167.         handled = true;
  168.     break;
  169.  
  170.     case ctlSelectEvent:
  171.     switch(e->data.ctlSelect.controlID) {
  172.     }
  173.     break;
  174.  
  175.     default:
  176.         break;
  177.     }
  178.  
  179.     CALLBACK_EPILOGUE
  180.  
  181.     return handled;
  182. }
  183.  
  184. static Boolean ApplicationHandleEvent(EventPtr e)
  185. {
  186.     FormPtr frm;
  187.     Word    formId;
  188.     Boolean handled = false;
  189.  
  190.     if (e->eType == frmLoadEvent) {
  191.     formId = e->data.frmLoad.formID;
  192.     frm = FrmInitForm(formId);
  193.     FrmSetActiveForm(frm);
  194.  
  195.     switch(formId) {
  196.     case MainForm:
  197.         FrmSetEventHandler(frm, MainFormHandleEvent);
  198.         break;
  199.     }
  200.     handled = true;
  201.     }
  202.  
  203.     return handled;
  204. }
  205.  
  206. /* Get preferences, open (or create) app database */
  207. static Word StartApplication(void)
  208. {
  209.     FrmGotoForm(MainForm);
  210.     return 0;
  211. }
  212.  
  213. /* Save preferences, close forms, close app database */
  214. static void StopApplication(void)
  215. {
  216.     FrmSaveAllForms();
  217.     FrmCloseAllForms();
  218. }
  219.  
  220. /* The main event loop */
  221. static void EventLoop(void)
  222. {
  223.     Word err;
  224.     EventType e;
  225.  
  226.     do {
  227.     EvtGetEvent(&e, evtWaitForever);
  228.     if (! SysHandleEvent (&e))
  229.         if (! MenuHandleEvent (NULL, &e, &err))
  230.         if (! ApplicationHandleEvent (&e))
  231.             FrmDispatchEvent (&e);
  232.     } while (e.eType != appStopEvent);
  233. }
  234.  
  235. /* Main entry point; it is unlikely you will need to change this except to
  236.    handle other launch command codes */
  237. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  238. {
  239.     Word err;
  240.  
  241.     if (cmd == sysAppLaunchCmdNormalLaunch) {
  242.  
  243.     err = StartApplication();
  244.     if (err) return err;
  245.  
  246.     EventLoop();
  247.     StopApplication();
  248.  
  249.     } else {
  250.     return sysErrParamErr;
  251.     }
  252.  
  253.     return 0;
  254. }
  255. EOFILE
  256. close(F);
  257.  
  258. open(F, ">${prcname}Rsc.h") or die "Cannot write ${prcname}Rsc.h: $!\n";
  259. print F "#define MainForm 1000\n";
  260. close(F);
  261.  
  262. open(F, ">${prcname}.rcp") or die "Cannot write ${prcname}.rcp: $!\n";
  263. print F <<EOFILE;
  264. #include "${prcname}Rsc.h"
  265.  
  266. FORM MainForm 1 1 158 158
  267. BEGIN
  268.     TITLE "$appname"
  269. END
  270.  
  271. APPLICATIONICONNAME 1000 "$iconname"
  272. ICON "$prcname.$iconext"
  273.  
  274. VERSION 1 "0.0.1"
  275. EOFILE
  276. close(F);
  277.  
  278. open(F, ">${prcname}.${iconext}") or
  279.     die "Cannot write ${prcname}.${iconext}: $!\n";
  280. if ($iconext eq 'pbitm') {
  281.     print F <<EOB;
  282. ------------#######-------------
  283. ----------###########-----------
  284. ---------#############----------
  285. --------###############---------
  286. -------#################--------
  287. ------###################-------
  288. ------###################-------
  289. -----#####################------
  290. -----#####################------
  291. -----#####################------
  292. -----#####################------
  293. -----#####################------
  294. -----#####################------
  295. -----#####################------
  296. ------###################-------
  297. ------###################-------
  298. -------#################--------
  299. --------###############---------
  300. ---------#############----------
  301. ----------###########-----------
  302. ------------#######-------------
  303. --------------------------------
  304. --------------------------------
  305. --------------------------------
  306. --------------------------------
  307. --------------------------------
  308. --------------------------------
  309. --------------------------------
  310. --------------------------------
  311. --------------------------------
  312. --------------------------------
  313. --------------------------------
  314. EOB
  315. } elsif ($iconext eq 'pbm') {
  316.     print F "P4\n32 32\n";
  317.     my ($bitmap) = <<EOB;
  318. 000fe000003ff800007ffc0000fffe00
  319. 01ffff0003ffff8003ffff8007ffffc0
  320. 07ffffc007ffffc007ffffc007ffffc0
  321. 07ffffc007ffffc003ffff8003ffff80
  322. 01ffff0000fffe00007ffc00003ff800
  323. 000fe000000000000000000000000000
  324. 00000000000000000000000000000000
  325. 00000000000000000000000000000000
  326. EOB
  327.     $bitmap =~ tr/0-9a-f//cd;
  328.     print F pack('H*', $bitmap);
  329. } else {
  330.     die "Unknown icon type $iconext\n";
  331. }
  332. close(F);
  333.  
  334. open(F, ">callback.h") or
  335.     die "Cannot write callback.h: $!\n";
  336.  
  337. print F <<'EOFILE';
  338. #ifndef __CALLBACK_H__
  339. #define __CALLBACK_H__
  340.  
  341. /* This is a workaround for a bug in the current version of gcc:
  342.  
  343.    gcc assumes that no one will touch %a4 after it is set up in crt0.o.
  344.    This isn't true if a function is called as a callback by something
  345.    that wasn't compiled by gcc (like FrmCloseAllForms()).  It may also
  346.    not be true if it is used as a callback by something in a different
  347.    shared library.
  348.  
  349.    We really want a function attribute "callback" which will insert this
  350.    progloue and epilogoue automatically.
  351.    
  352.       - Ian */
  353.  
  354. register void *reg_a4 asm("%a4");
  355.  
  356. #define CALLBACK_PROLOGUE \
  357.     void *save_a4 = reg_a4; asm("move.l %%a5,%%a4; sub.l #edata,%%a4" : :);
  358.  
  359. #define CALLBACK_EPILOGUE reg_a4 = save_a4;
  360.  
  361. #endif
  362. EOFILE
  363. close(F);
  364.  
  365. print "Done.\n";
  366.