/***************************************************************************** Program: OS4Reboot File: main.c Function: Main (complete program listing in this file) Description: Utility to reboot the system Copyright: Copyright (C) 2004-5 Dreamland Fantasy Studios All rights reserved Author: Francis G. Loch (fgl) Environment: VBCC 0.8i, SDK 51.15, GoldED Studio AIX Service Pack 27, Micro A1-C, PowerPC 750 GX @ 800MHz, 256Mb RAM, AmigaOS 4.0 (Developer Pre-release Update 3) Notes: None Revisions: 0.7.2 (fgl) 25/08/2005 Fixed problem with error requester sometimes being displayed when reboot is too slow during a hard reboot. 0.7.1 (fgl) 02/02/2005 Minor update. Optimised the code a little. 0.7 (fgl) 09/01/2005 Added BOOTMODE icon tooltype allowing an immediate hard or soft reboot. 0.6 (fgl) 09/01/2005 Added DELAY tooltype. 0.5 (fgl) 04/01/2005 Can now "hard" reset the system via OS4's standard reboot command. 0.4 (fgl) 02/01/2005 Now runs as a commodity. 0.3 (fgl) 31/12/2004 Added a CLI interface with optional delay. 0.2 (fgl) 21/11/2004 Can now be activated via the Workbench's Tool menu. 0.1 (fgl) 19/11/2004 1st release. Known bugs: None *****************************************************************************/ /* /// "definitions" */ #define PROGRAMNAME "OS4Reboot" #define VERSION 0 #define REVISION 7 #define MAINTENANCE 2 #define VERSIONSTRING "0.7.2" #define DESCRIPTION "Reboot utility for AmigaOS 4" char ver[] = "$VER: OS4Reboot 0.7.2 (25.08.2005) Copyright © 2004-5 Dreamland Fantasy Studios"; /* use classic libray syntax under OS4 */ #ifdef __amigaos4__ #define __USE_INLINE__ #endif /* /// */ /* /// "includes" */ #include #include #include #include #include #include #include #include /* /// */ /*--------------------------------------------------------------------------*/ /* Constants and macros */ /*--------------------------------------------------------------------------*/ struct Library *CxBase = NULL; struct CommoditiesIFace *ICommodities = NULL; struct Library *IconBase = NULL; struct IconIFace *IIcon = NULL; CONST_STRPTR bootmode; uint32 delay=0; struct NewBroker newbroker = { NB_VERSION, PROGRAMNAME, "OS4Reboot 0.7.2", DESCRIPTION, NBU_UNIQUE | NBU_NOTIFY, 0, 0, 0, 0 }; BOOL quit = FALSE; /*--------------------------------------------------------------------------*/ /* Hard Reboot */ /*--------------------------------------------------------------------------*/ void HardReboot(void) { if (Execute("run >nil: reboot", 0, 0)) { // Delay present to prevent requester being displayed before hard reboot Delay(250) Object *requester; uint32 result = 0; requester = NewObject(REQUESTER_GetClass(), NULL, REQ_Type, REQTYPE_INFO, REQ_Image, REQIMAGE_ERROR, REQ_TitleText, PROGRAMNAME, REQ_BodyText, "ERROR: Unable to perform hard reboot!\n\nCheck that the file 'reboot' is present in the C: drawer.", REQ_GadgetText, "Okay", TAG_DONE); if (requester) { result = IDoMethod(requester, RM_OPENREQ, NULL, NULL, NULL); DisposeObject(requester); } } } /*--------------------------------------------------------------------------*/ /* Reboot requester */ /*--------------------------------------------------------------------------*/ void request_reboot() { Object *requester; uint32 result = 0; if (Stricmp(bootmode, "SOFT") == 0) { Delay(delay * 50); ColdReboot(); } if (Stricmp(bootmode, "HARD") == 0) { Delay(delay * 50); HardReboot(); } requester = NewObject(REQUESTER_GetClass(), NULL, REQ_Type, REQTYPE_INFO, REQ_Image, REQIMAGE_WARNING, REQ_TitleText, PROGRAMNAME, REQ_BodyText, "WARNING: Reboot system?\n\nThis will cause all unsaved data to be lost!", REQ_GadgetText, "Soft Reboot|Hard Reboot|Cancel", TAG_DONE); if (requester) { result = IDoMethod(requester, RM_OPENREQ, NULL, NULL, NULL); DisposeObject(requester); if (result == 1) { Delay(delay * 50); ColdReboot(); } if (result == 2) { Delay(delay * 50); HardReboot(); } } } /*--------------------------------------------------------------------------*/ /* Main program */ /*--------------------------------------------------------------------------*/ int main(int32 argc, CONST_STRPTR *argv) { if (Cli()) { // OS4Reboot from Shell int duration; if (argc > 1) { if (Stricmp(argv[1], "?") == 0) { Printf("OS4Reboot 0.7.2 - Copyright © 2004-5 Dreamland Fantasy Studios\n\n"); Printf("Usage from CLI: OS4Reboot [delay]\n\n"); } else { // Try DOS function instead of atoi() duration = atoi(argv[1]) * 50; Delay(duration); ColdReboot(); } } else { ColdReboot(); } } else { // OS4Reboot from Workbench BOOL quit = FALSE; BOOL enabled = TRUE; struct MsgPort *appport, *cxport; struct AppMenuItem *appitem; struct AppMessage *appmsg; CONST_STRPTR *ttypes; CxObj *broker; CxMsg *msg; uint32 appsig, cxsig, sigs; uint32 msgid, msgtype; if (appport = CreateMsgPort()) { appitem = AddAppMenuItemA(0L, 0L, "Reboot system", appport, NULL); } if ( (CxBase = OpenLibrary ("commodities.library",0)) && (ICommodities = (struct CommoditiesIFace *) GetInterface (CxBase,"main",1,NULL)) && (IconBase = OpenLibrary ("icon.library",0)) && (IIcon = (struct IconIFace *) GetInterface (IconBase,"main",1,NULL)) ) { ttypes = (CONST_STRPTR *) ArgArrayInit(argc, argv); delay = ArgInt(ttypes, "DELAY", 0); bootmode = (STRPTR) ArgString(ttypes, "BOOTMODE", 0); if (cxport = CreateMsgPort()) { newbroker.nb_Port = cxport; newbroker.nb_Pri = ArgInt(ttypes, "CX_PRIORITY", 0); if (broker = CxBroker(&newbroker, NULL)) { ActivateCxObj(broker, 1L); } else { quit = TRUE; } } } appsig = 1L << appport->mp_SigBit; cxsig = 1L << cxport->mp_SigBit; while (quit == FALSE) { sigs = Wait(appsig | cxsig | SIGBREAKF_CTRL_C); if (sigs & SIGBREAKF_CTRL_C) { quit = TRUE; } if (sigs & appsig) { while (appmsg = (struct AppMessage *) GetMsg(appport)) { if (enabled) { request_reboot(); } ReplyMsg((struct Message *) appmsg); } } if (sigs & cxsig) { while (msg = (CxMsg *) GetMsg (cxport)) { msgid = CxMsgID (msg); msgtype = CxMsgType (msg); ReplyMsg ((struct Message *) msg); switch (msgtype) { case CXM_COMMAND: switch (msgid) { case CXCMD_DISABLE: ActivateCxObj(broker, 0L); enabled = FALSE; break; case CXCMD_ENABLE: ActivateCxObj(broker, 1L); enabled = TRUE; break; case CXCMD_KILL: quit = TRUE; break; case CXCMD_UNIQUE: request_reboot(); break; } break; } } } } if (appitem) RemoveAppMenuItem(appitem); if (appport) DeleteMsgPort(appport); if (cxport) DeleteMsgPort(cxport); if (broker) DeleteCxObj(broker); ArgArrayDone(); if (IIcon) DropInterface ((struct Interface *)IIcon); if (IconBase) CloseLibrary (IconBase); if (ICommodities) DropInterface ((struct Interface *)ICommodities); if (CxBase) CloseLibrary (CxBase); } return 0; }