home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 563.lha / CenterScreen_v1.07 / CenterScreen.c < prev    next >
C/C++ Source or Header  |  1991-08-25  |  14KB  |  471 lines

  1. /*
  2.  *  CenterScreen.c
  3.  *
  4.  *  Commodity
  5.  *
  6.  *  Author: Stefan Sticht
  7.  *
  8.  *  Copyright: source is public domain, no copyright
  9.  *
  10.  *  Version history:
  11.  *
  12.  *  V1.01   initial release
  13.  *  V1.02   knows how to center lores and superhires screens
  14.  *  V1.03   recompiled with main.c V1.02
  15.  *  V1.04   completly rewritten; shared commodity code thrown away; smaller, uses less CPU time
  16.  *  V1.05   some really minor changes
  17.  *  V1.06   recompiled with changed (for 68040 compatiblity) cback.o
  18.  *  V1.07   only cosmetic changes (Usage string)
  19.  */
  20.  
  21. #define VERSION "V1.07"
  22.  
  23. /********************************************************************
  24.  *                             interfacing                          *
  25.  ********************************************************************/
  26.  
  27. /*
  28.  *  include files
  29.  */
  30.  
  31. #include <stdarg.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <graphics/gfxbase.h>
  35. #include <intuition/intuitionbase.h>
  36. #include <libraries/commodities.h>
  37.  
  38. #include <clib/alib_protos.h>
  39. #include <clib/commodities_protos.h>
  40. #include <pragmas/commodities_pragmas.h>
  41. #include <clib/dos_protos.h>
  42. #include <pragmas/dos_pragmas.h>
  43. #include <clib/exec_protos.h>
  44. #include <pragmas/exec_pragmas.h>
  45. #include <clib/intuition_protos.h>
  46. #include <pragmas/intuition_pragmas.h>
  47.  
  48. #ifdef DEBUG
  49. #define printf KPrintF
  50. #include <clib/dlib_protos.h>
  51. #endif
  52.  
  53. /*
  54.  *  prototypes
  55.  */
  56. long request(char *title, char *gadgets, char *text, ...);
  57. struct Library *myopenlibrary(char *name, unsigned long version);
  58. void processmessages(void);
  59.  
  60. /*
  61.  *  global data defined in other moduls
  62.  *
  63.  *  libraries opened by startup code; basepointers needed by function pragmas
  64.  */
  65. extern struct Library *DOSBase;
  66. extern struct Library *SysBase;
  67.  
  68. /*
  69.  *  Disable SAS/C CTRL/C handling
  70.  */
  71. void chkabort(void) {}
  72.  
  73. /********************************************************************
  74.  *                             global data                          *
  75.  ********************************************************************/
  76.  
  77. /*
  78.  *  definition of all messages (multi language support not completed yet)
  79.  */
  80. #ifdef GERMAN
  81.  
  82. #define RETRY_GADGETS           "Wiederholen|Abbrechen"
  83. #define RESUME_GADGETS          "Weiter"
  84. #define MSG_LIBRARY_OPENERR     "Die %s (V%ld+) kann nicht geöffnet werden!"
  85. #define COM_NAME                "ZentriereSchirm"
  86. #define COM_DESCR               "Zentriert Bildschirm mit "
  87. #define TT_ACTION               "AKTION"
  88. #define NO                      "NEIN"
  89. #define YES                     "JA"
  90.  
  91. #else
  92.  
  93. #define RETRY_GADGETS           "Retry|Cancel"
  94. #define RESUME_GADGETS          "Resume"
  95. #define MSG_LIBRARY_OPENERR     "%s (V%ld+) can't be opened!"
  96. #define COM_NAME                "CenterScreen"
  97. #define COM_DESCR               "Center frontmost screen on "
  98. #define TT_ACTION               "ACTION"
  99. #define YES                     "YES"
  100. #define NO                      "NO"
  101.  
  102. #endif
  103.  
  104. #define COM_TITLE           COM_NAME " " VERSION
  105. #define CX_PRIORITY         "CX_PRIORITY"
  106. #define DEF_CX_PRIORITY     0
  107.  
  108. #define DEF_TT_ACTION       "lcommand c"
  109.  
  110. /*
  111.  *  data for cback.o
  112.  */
  113. long _stack = 2048l;
  114. char *_procname = COM_NAME;
  115. long _priority = 0l;
  116. long _BackGroundIO = 1;
  117. extern BPTR _Backstdout;
  118.  
  119. /*
  120.  *  library base pointers
  121.  */
  122. struct GfxBase *GfxBase;
  123. struct IntuitionBase *IntuitionBase;
  124. struct Library *CxBase;
  125. struct Library *IconBase;
  126.  
  127. /*
  128.  *  message port
  129.  */
  130. struct MsgPort *cxport = NULL;
  131.  
  132. /*
  133.  *  signal flag
  134.  */
  135. unsigned long cxsigflag = 0l;
  136.  
  137. /*
  138.  *  programtitle and version for Version command
  139.  */
  140. char versionstring[] ="\0$VER: " COM_NAME " " VERSION;
  141.  
  142. /*
  143.  *  helpstring
  144.  */
  145. #ifdef GERMAN
  146. char helpstring[] = "\033[1m" COM_NAME "\033[0m " VERSION " (Public Domain) von Stefan Sticht\n"\
  147.                     "Aufruf: " COM_NAME " [" CX_PRIORITY "=<Zahl>] [" TT_ACTION "=<Zeichenkette>]\n";
  148. #else
  149. char helpstring[] = "\033[1m" COM_NAME "\033[0m " VERSION " (Public Domain) by Stefan Sticht\n"
  150.                     "Usage: " COM_NAME " [" CX_PRIORITY "=<number>] [" TT_ACTION "=<string>]\n";
  151. #endif
  152.  
  153. /*
  154.  *  the tooltypearray
  155.  */
  156. char **tooltypes;
  157.  
  158. /*
  159.  *  our broker
  160.  */
  161. CxObj *broker = NULL;
  162.  
  163. char descr[CBD_DESCRLEN + 1] = COM_DESCR;
  164.  
  165. struct NewBroker newbroker = {
  166.     NB_VERSION,                         /* BYTE nb_Version               */
  167.     COM_NAME,                           /* BYTE *nb_Name                 */
  168.     COM_TITLE,                          /* BYTE *nb_Title                */
  169.     descr,                              /* BYTE *nb_Descr                */
  170.     NBU_NOTIFY | NBU_UNIQUE,            /* SHORT nb_Unique               */
  171.     0,                                  /* SHORT nb_Flags                */
  172.     0,                                  /* BYTE nb_Pri                   */
  173.     NULL,                               /* struct MsgPort nb_Port        */
  174.     0                                   /* WORD nb_ReservedChannel       */
  175. };
  176.  
  177. #define CENTERSCREEN 1
  178.  
  179. /********************************************************************
  180.  *                             functions                            *
  181.  ********************************************************************/
  182.  
  183. #define pos(x) ((x > 0) ? x : 0)
  184.  
  185. /*
  186.  *  request(): a glue routine to EasyRequest as simple as printf plus
  187.  *             titlestring, gadgettexts
  188.  *
  189.  *  Input: char *title:         pointer to the title of the requester
  190.  *         char *gadgets:       pointer to gadgettext
  191.  *         char *text:          text displayed in requester
  192.  *
  193.  *  Result: same as EasyrequestArgs()
  194.  *
  195.  * !!! for more info see EasyRequestArgs() in Autodocs/intuition.doc !!!
  196.  */
  197. long request(char *title, char *gadgets, char *text, ...)
  198. {
  199.     /*
  200.      *  structure textreq only needed in this function, so hide it here
  201.      *  must be static, in order to be initialized only once
  202.      */
  203.     static struct EasyStruct textreq = {
  204.         sizeof (struct EasyStruct), /* ULONG es_StructSize      */
  205.         0l,                         /* ULONG es_Flags           */
  206.         NULL,                       /* UBYTE *es_Title          */
  207.         NULL,                       /* UBYTE *es_TextFormat     */
  208.         NULL,                       /* UBYTE *es_GadgetFormat   */
  209.         };
  210.     va_list ap;
  211.     long rc;
  212.  
  213.     /*
  214.      *  get start of variable arguments
  215.      */
  216.     va_start(ap, text);
  217.  
  218.     /*
  219.      *  update textreq
  220.      */
  221.     textreq.es_Title = (UBYTE *)title;
  222.     textreq.es_TextFormat = (UBYTE *)text;
  223.     textreq.es_GadgetFormat = (UBYTE *)gadgets;
  224.  
  225.     /*
  226.      *  win may be NULL
  227.      */
  228.     rc = EasyRequestArgs(NULL, &textreq, NULL, ap);
  229.  
  230.     va_end(ap);
  231.  
  232.     return(rc);
  233. }
  234.  
  235. /*
  236.  *  myopenlibrary(): same as OpenLibrary(), but opens a retry-requester
  237.  *                   if OpenLibrary() fails, to give the user a chance to
  238.  *                   copy the library to libs: and retry
  239.  *                   requires request(), see above
  240.  */
  241. struct Library *myopenlibrary(char *name, unsigned long version)
  242. {
  243.     static char errortext[] = MSG_LIBRARY_OPENERR;
  244.     struct Library *libptr;
  245.     long ok = TRUE;
  246.  
  247.     do {
  248.         if (!(libptr = OpenLibrary((UBYTE *)name, version))) {
  249.             if (IntuitionBase) ok = request(COM_NAME ":", RETRY_GADGETS, errortext, name, version);
  250.             else ok = FALSE;
  251.             }
  252.         } while (!libptr && ok);
  253.  
  254.     return(libptr);
  255. }
  256.  
  257. void main(int argc, char *argv[])
  258. {
  259.     CxObj *centerfilter;
  260.     char *centeraction;
  261.     struct Message *msg;
  262.     unsigned long len;
  263.  
  264.     if ((argc > 1) && (*argv[1] == '?')) {
  265.         /*
  266.          *  display help string
  267.          */
  268.         if (_Backstdout) {
  269.             Write(_Backstdout, helpstring, sizeof(helpstring) - 1l);
  270.             Close(_Backstdout);
  271.             }
  272.         return;
  273.         }
  274.     else if (argc && _Backstdout) Close(_Backstdout);
  275.  
  276.     /*
  277.      *  open required libraries first
  278.      */
  279.     if (IntuitionBase = (struct IntuitionBase *)myopenlibrary("intuition.library", 37l)) {
  280.  
  281.         if (CxBase = myopenlibrary("commodities.library", 37l)) {
  282.  
  283.             if (IconBase = myopenlibrary("icon.library", 37l)) {
  284.  
  285.                 if (GfxBase = (struct GfxBase *)myopenlibrary("graphics.library", 37l)) {
  286.  
  287.                     /*
  288.                      * create tooltypes array (requires icon.library open!!!)
  289.                      */
  290.                     tooltypes = (char **)ArgArrayInit(argc, argv);
  291.  
  292.                     /*
  293.                      *  create our message port
  294.                      */
  295.                     if (cxport = CreateMsgPort()) {
  296.  
  297.                         cxsigflag = 1l << cxport->mp_SigBit;
  298.                         /*
  299.                          * set up some broker data
  300.                          */
  301.                         newbroker.nb_Pri = ArgInt(tooltypes, CX_PRIORITY, DEF_CX_PRIORITY);
  302.                         newbroker.nb_Port = cxport;
  303.  
  304.                         if ((centeraction = ArgString(tooltypes, TT_ACTION, DEF_TT_ACTION)) &&
  305.                             *centeraction) {
  306.  
  307.                             len = strlen(descr);
  308.                             strncpy(descr + len, centeraction, sizeof(descr) - len - 2l);
  309.  
  310.                             if (broker = CxBroker(&newbroker, NULL)) {
  311.  
  312.                                 if (centerfilter = HotKey(centeraction, cxport, CENTERSCREEN)) {
  313.  
  314.                                     AttachCxObj(broker, centerfilter);
  315.  
  316.                                     if (!CxObjError(centerfilter)) {
  317.                                         /*
  318.                                          *  activate our commodity
  319.                                          */
  320.                                         ActivateCxObj(broker, 1l);
  321.                                         /*
  322.                                          *  now watch our numerous ports
  323.                                          */
  324.                                         processmessages();
  325.  
  326.                                         } /* if !CxObjError() */
  327.  
  328.                                     } /* if centerfilter */
  329.  
  330.                                 DeleteCxObjAll(broker);
  331.  
  332.                                 } /* if broker */
  333.  
  334.                             #ifdef DEBUG
  335.                             else printf("main(): CxBroker() failed!\n");
  336.                             #endif
  337.  
  338.                             } /* if centeraction */
  339.  
  340.                         /*
  341.                          *  delete our message port after replying all pending messages
  342.                          */
  343.                         while (msg = GetMsg(cxport)) ReplyMsg(msg);
  344.                         DeleteMsgPort(cxport);
  345.                         } /* if cxport */
  346.  
  347.                     #ifdef DEBUG
  348.                     else printf("main(): CraeteMsgPort() failed!\n");
  349.                     #endif
  350.  
  351.                     ArgArrayDone();
  352.  
  353.                     CloseLibrary((struct Library *)GfxBase);
  354.                     } /* if GfxBase */
  355.  
  356.                 CloseLibrary(IconBase);
  357.                 } /* if IconBase */
  358.  
  359.             CloseLibrary(CxBase);
  360.             } /* if CxBase */
  361.  
  362.     CloseLibrary((struct Library *)IntuitionBase);
  363.     } /* if IntuitionBase */
  364.  
  365. } /* main() */
  366.  
  367. void processmessages(void)
  368. {
  369.     struct Message *msg;
  370.     struct Screen *scr;
  371.     unsigned long lock;
  372.     unsigned long msgid;
  373.     unsigned long msgtype;
  374.     unsigned long sigreceived;
  375.     unsigned short leftedge;
  376.     unsigned short normalwidth; 
  377.     unsigned short width;
  378.     unsigned short quit = FALSE;
  379.  
  380.     while (!quit) {
  381.  
  382.         sigreceived = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  383.  
  384.         #ifdef DEBUG
  385.         printf("processmessages(): signal received\n");
  386.         #endif
  387.  
  388.         if (sigreceived & SIGBREAKF_CTRL_C) quit = TRUE;
  389.  
  390.         if (sigreceived & cxsigflag) {
  391.  
  392.             while (msg = (struct Message *)GetMsg(cxport)) {
  393.  
  394.                 msgid = CxMsgID((CxMsg *)msg);
  395.                 msgtype = CxMsgType((CxMsg *)msg);
  396.  
  397.                 ReplyMsg(msg);
  398.  
  399.                 switch (msgtype) {
  400.  
  401.                     case CXM_IEVENT:
  402.                         switch (msgid) {
  403.  
  404.                             case CENTERSCREEN:
  405.                                 normalwidth = GfxBase->NormalDisplayColumns;
  406.                                 lock = LockIBase(0l);
  407.                                 if (scr = IntuitionBase->FirstScreen) {
  408.  
  409.                                     width = scr->Width;
  410.  
  411.                                     if (!(scr->ViewPort.Modes & HIRES)) {
  412.                                         /*
  413.                                          *  lores screen (I hope so): half normalwidth
  414.                                          */
  415.                                         normalwidth >>= 1;
  416.                                         }
  417.  
  418.                                     else if (scr->ViewPort.Modes & SUPERHIRES) {
  419.                                         /*
  420.                                          *  superhires screen: double normalwidth
  421.                                          */
  422.                                         normalwidth <<= 1;
  423.                                         }
  424.  
  425.                                     if (scr && (width < normalwidth)) {
  426.                                         leftedge = pos((normalwidth - width) >> 1);
  427.                                         UnlockIBase(lock);
  428.                                         MoveScreen(scr, (long)(leftedge - scr->LeftEdge), 0l);
  429.                                         }
  430.                                     else UnlockIBase(lock);
  431.  
  432.                                     } /* if (scr = IntuitionBase->FirstScreen) */
  433.  
  434.                                 else UnlockIBase(lock);
  435.  
  436.                                 break;
  437.  
  438.                             } /* switch msgid */
  439.  
  440.                         break;
  441.  
  442.                     case CXM_COMMAND:
  443.                         switch (msgid) {
  444.  
  445.                             case CXCMD_UNIQUE:
  446.                             case CXCMD_KILL:
  447.                                 quit = TRUE;
  448.                                 break;
  449.  
  450.                             case CXCMD_DISABLE:
  451.                                 ActivateCxObj(broker, 0l);
  452.                                 break;
  453.  
  454.                             case CXCMD_ENABLE:
  455.                                 ActivateCxObj(broker, 1l);
  456.                                 break;
  457.  
  458.                             }
  459.                         break;
  460.  
  461.                     } /* switch msgtype */
  462.  
  463.                 } /* while CxMsg */
  464.  
  465.             } /* if (sigreceived & cxsigflag) */
  466.  
  467.         } /* while !quit */
  468.  
  469.     ActivateCxObj(broker, 0l);
  470. }
  471.