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