home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / timeserv / alarmthd.c next >
C/C++ Source or Header  |  1999-05-11  |  10KB  |  337 lines

  1. /*static char *SCCSID = "@(#)alarmthd.c    6.8 92/02/18";*/
  2.  
  3. /*=========================================================================\
  4.  *                                                                         *
  5.  *       FILE:                                                             *
  6.  *                                                                         *
  7.  *       DESCRIPTION:    Presentation Manager Alarm Clock Application      *
  8.  *                                                                         *
  9.  *                                                                         *
  10.  *                                                                         *
  11.  *      Copyright 1992  IBM Corp.                                          *
  12.  *                                                                         *
  13.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is        *
  14.  *      sample code created by IBM Corporation. This sample code is not    *
  15.  *      part of any standard or IBM product and is provided to you solely  *
  16.  *      for  the purpose of assisting you in the development of your       *
  17.  *      applications.  The code is provided "AS IS", without               *
  18.  *      warranty of any kind.  IBM shall not be liable for any damages     *
  19.  *      arising out of your use of the sample code, even if they have been *
  20.  *      advised of the possibility of   such damages.                      *
  21.  *                                                                         *
  22.  *-------------------------------------------------------------------------*
  23.  *
  24.  *
  25.  *
  26.  *
  27.  *
  28.  *
  29.  *
  30.  *
  31.  *
  32.  *
  33.  *
  34.  *--------------------------------------------------------------
  35.  *
  36.  *  This source file contains the following functions:
  37.  *
  38.  *
  39.  *
  40.  *
  41.  *
  42. \*==============================================================*/
  43.  
  44. /*--------------------------------------------------------------*\
  45.  *  Include files, macros, defined constants, and externs
  46. \*--------------------------------------------------------------*/
  47. #define INCL_PM
  48. #define INCL_WINMENUS
  49. #define INCL_GPITRANSFORMS
  50. #define INCL_WINHELP
  51. #define INCL_WINWINDOWMGR
  52. #define INCL_WINMESSAGEMGR
  53. #define INCL_DOSSEMAPHORES
  54. #define INCL_DOSDATETIME
  55. #define INCL_DOSQUEUES
  56. #define INCL_DOSPROCESS
  57. #define INCL_WINDIALOGS
  58.  
  59. #include <os2.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include "clock.h"
  63. #include "dialogs.h"
  64. #include "clkdata.h"
  65. #include "alarmthd.h"
  66. #include "res.h"
  67.  
  68.  
  69. /*--------------------------------------------------------------*\
  70.  *  Global variables  and definitions for this file
  71. \*--------------------------------------------------------------*/
  72.  
  73. #define HOURS_24 ( (LONG) 1000L * 24L * 60L * 60L)
  74.  
  75.  
  76. /*--------------------------------------------------------------*\
  77.  *  Entry point declarations
  78. \*--------------------------------------------------------------*/
  79.  
  80. APIRET AlarmInit(VOID);
  81. /****************************************************************\
  82.  *
  83.  *--------------------------------------------------------------
  84.  *
  85.  *  Name:AlarmThread()
  86.  *
  87.  *  Purpose:
  88.  *
  89.  *
  90.  *
  91.  *  Usage:
  92.  *
  93.  *  Method:
  94.  *          -
  95.  *
  96.  *          -
  97.  *          -
  98.  *
  99.  *          -
  100.  *          -
  101.  *
  102.  *  Returns:
  103.  *          1 - if sucessful execution completed
  104.  *          0 - if error
  105. \****************************************************************/
  106. VOID AlarmThread(PVOID pvMessage)
  107.  
  108. {
  109.      ULONG ulTimerValue,ulPostCount;
  110.      SHORT usi;
  111.  
  112.  
  113.      /*
  114.       *create a event semaphore
  115.       *for this timer
  116.       */
  117.      if( DosCreateEventSem(NULL,
  118.                            &TimerResources.hTimerDev,
  119.                            DC_SEM_SHARED,
  120.                            FALSE) )
  121.      {
  122.           /*
  123.            *generate error
  124.            *message
  125.            *back to the console
  126.            */
  127.      }
  128.  
  129.      /*
  130.       *create our queue
  131.       *and  semaphore
  132.       */
  133.       if(AlarmInit() )
  134.       {
  135.           /*
  136.            *send a message to our
  137.            *main thread,unable to start
  138.            */
  139.  
  140.           MessageBox(hwndFrame, IDMSG_CANNOTLOADSTRING,
  141.                      MB_OK | MB_ERROR, FALSE);
  142.           _endthread();
  143.       }
  144.       ulTimerValue = TimerResources.ulTimerValue;
  145.  
  146.       /*
  147.        *stay in our
  148.        *loop until a abort message is received
  149.        *from our main
  150.        *thread
  151.        */
  152.  
  153.       do
  154.       {
  155.  
  156.           /*
  157.            *sit here and
  158.            *wait for the semaphore to post,
  159.            *from one of two conditions
  160.            * 1)either the timer expired
  161.            * 2)or the pm thread cleared it for
  162.            * a exit or the timer value has changed
  163.            */
  164.            if( DosWaitEventSem((HEV)TimerResources.hTimerDev,
  165.                                SEM_INDEFINITE_WAIT) )
  166.            {
  167.                /*
  168.                 *error occurred
  169.                 *notify the pm thread
  170.                 */
  171.                 break;
  172.  
  173.            }
  174.            if(fEndThread)
  175.            {
  176.                continue;
  177.            }
  178.  
  179.            /*
  180.             *reset the post count
  181.             */
  182.            DosResetEventSem( (HEV) TimerResources.hTimerDev,
  183.                                &ulPostCount);
  184.            /*
  185.             *did the timer value get
  186.             *changed
  187.             */
  188.             if(TimerResources.ulTimerValue != ulTimerValue)
  189.             {
  190.                      ulTimerValue = TimerResources.ulTimerValue;
  191.                      /*
  192.                       *non async timer
  193.                       *needs to be stopped
  194.                       */
  195.                      if(cp.sTimerType == TIMER_CONT )
  196.                      {
  197.                          DosStopTimer(TimerResources.hTimer);
  198.                      }
  199.                      /*
  200.                       *set the alarm for the initial time
  201.                       *delay till the first
  202.                       *alarm goes
  203.                       *off
  204.                       */
  205.  
  206.                      if(AlarmInit() )
  207.                      {
  208.                          /*
  209.                           *generate error message
  210.                           */
  211.                           MessageBox(hwndFrame, IDMSG_CANNOTLOADSTRING,
  212.                                      MB_OK | MB_ERROR, FALSE);
  213.                           break;
  214.                      }
  215.  
  216.             }
  217.             /*
  218.              *the timer
  219.              *expired
  220.              */
  221.             else
  222.             {
  223.                 /*
  224.                  *is the alarm set
  225.                  */
  226.                 if(cp.alarm.usMode & AM_ACTIVE )
  227.                 {
  228.                     /*
  229.                      *send a message
  230.                      *that a alarm has gone off
  231.                      */
  232.                      if(cp.alarm.usMode & AM_AUDIO)
  233.                      {
  234.                         for (usi = 0; usi < 20; usi++)
  235.                         {
  236.                              DosBeep(300 + usi * 100, 50);
  237.                         }
  238.                      }
  239.                      /*
  240.                       *message box
  241.                       */
  242.                      if(cp.alarm.usMode & AM_MSGBOX)
  243.                      {
  244.                            WinPostMsg( hwndFrame,
  245.                                        WM_COMMAND,
  246.                                        MPFROM2SHORT(IDM_ALARM_EXPIRED,TRUE),
  247.                                            MPVOID );
  248.                      }
  249.                      /*
  250.                       *the alarm has expired,check and make sure
  251.                       *its on a 24 hour boundary
  252.                       */
  253.                      if(ulTimerValue != HOURS_24 )
  254.                      {
  255.                          ulTimerValue = TimerResources.ulTimerValue =
  256.                                    HOURS_24;
  257.                          /*
  258.                           *non async timer
  259.                           *needs to be stopped
  260.                           */
  261.                          if(cp.sTimerType == TIMER_CONT )
  262.                          {
  263.                              DosStopTimer(TimerResources.hTimer);
  264.                          }
  265.                          if(AlarmInit() )
  266.                          {
  267.                              /*
  268.                               *generate error message
  269.                               */
  270.                               break;
  271.                          }
  272.                      }
  273.                  }
  274.             }
  275.      }while(!fEndThread);
  276.  
  277.  
  278.      _endthread();
  279.  
  280.  
  281. }
  282.  
  283. /****************************************************************\
  284.  *
  285.  *--------------------------------------------------------------
  286.  *
  287.  *  Name:AlarmInit();
  288.  *
  289.  *  Purpose:
  290.  *
  291.  *
  292.  *
  293.  *  Usage:
  294.  *
  295.  *  Method:
  296.  *          -
  297.  *
  298.  *          -
  299.  *          -
  300.  *
  301.  *          -
  302.  *          -
  303.  *
  304.  *  Returns:
  305.  *          NO_ERROR   (0) No Error occurred
  306.  *          Non - Zero the api return value for the called function
  307. \****************************************************************/
  308. APIRET AlarmInit(VOID)
  309. {
  310.      APIRET ApiReturn = NO_ERROR;
  311.  
  312.  
  313.      switch(cp.sTimerType)
  314.      {
  315.      case TIMER_CONT:
  316.           if( ApiReturn = DosStartTimer(TimerResources.ulTimerValue,
  317.                                         (HSEM)TimerResources.hTimerDev,
  318.                                         &TimerResources.hTimer) )
  319.           {
  320.                return(ApiReturn);
  321.           }
  322.           break;
  323.      case TIMER_ASYNC:
  324.           if( ApiReturn = DosAsyncTimer(TimerResources.ulTimerValue,
  325.                                         (HSEM)TimerResources.hTimerDev,
  326.                                         &TimerResources.hTimer) )
  327.           {
  328.                return(ApiReturn);
  329.           }
  330.           break;
  331.      }
  332.      return(ApiReturn);
  333. }
  334. /*--------------------------------------------------------------*\
  335.  *  End of file : alarmthd.c
  336. \*--------------------------------------------------------------*/
  337.