rem: BOOL           rc             = FALSE; BOOL           quit           = FALSE;
add: BOOL           rc                    = FALSE; BOOL           quit                  = FALSE; BOOL           messed_with_registers = FALSE;
context: HandleGUI( Object*         window,           struct Gadget** gadgets,           struct PUHData* pd ){  BOOL           rc             = FALSE;  BOOL           quit           = FALSE;  struct Window* win_ptr        = NULL;  ULONG          window_signals = 0;  ULONG          audio_mode     = 0;  ULONG          frequency      = 0;  void*          chip           = NULL;    struct Custom* custom         = (struct Custom*) 0xdff000;  struct LogData log_data;  struct Hook log_hook =  {    { NULL, NULL },    (HOOKFUNC) LogToList,    NULL,    &log_data  };  chip = AllocVec( pooh11_sblen, MEMF_CHIP );  if( chip == NULL )  {    LogPUH( pd, "Failed to alloc chip memory." );    return FALSE;  }  CopyMem( pooh11_sb, chip, pooh11_sblen );  GetAttr( WINDOW_SigMask, window, &window_signals );  GetAttr( WINDOW_Window,  window, (ULONG*) &win_ptr );  log_data.m_Gadget = gadgets[ GAD_MESSAGES ];  log_data.m_Window = win_ptr;  SetPUHLogger( &log_hook, pd );  while( ! quit )  {    ULONG mask;        mask = Wait( window_signals | SIGBREAKF_CTRL_C );        if( mask & SIGBREAKF_CTRL_C )    {      quit = TRUE;      rc   = TRUE;      break;    }    if( mask & window_signals )    {      ULONG input_flags = 0;      UWORD code        = 0;            while( ( input_flags = DoMethod( window, WM_HANDLEINPUT, &code ) )              != WMHI_LASTMSG )      {        switch( input_flags & WMHI_CLASSMASK)        {          case WMHI_CLOSEWINDOW:            quit = TRUE;            rc   = TRUE;            break;          case WMHI_ICONIFY:            DoMethod( window, WM_ICONIFY );            GetAttr( WINDOW_Window,  window, (ULONG*) &win_ptr );            log_data.m_Window = win_ptr;            break;                      case WMHI_UNICONIFY:            DoMethod( window, WM_OPEN );            GetAttr( WINDOW_Window,  window, (ULONG*) &win_ptr );            log_data.m_Window = win_ptr;            break;          case WMHI_GADGETUP:          {            switch( input_flags & RL_GADGETMASK )            {              case GAD_MODE_SELECT:              {                struct AHIAudioModeRequester* req = NULL;                                struct TagItem                filter_tags[] =                {                  { AHIDB_Realtime,    TRUE },                  { AHIDB_MaxChannels, 4    },                  { TAG_DONE,          0    }                };                struct Hook filter_hook =                {                  { NULL, NULL },                  (HOOKFUNC) FilterFunc,                  NULL,                  NULL                };                req = AHI_AllocAudioRequest(                     AHIR_Window,         (ULONG) win_ptr,                    AHIR_SleepWindow,    TRUE,                    AHIR_InitialAudioID, audio_mode,                    AHIR_InitialMixFreq, frequency,                    AHIR_DoMixFreq,      TRUE,                    AHIR_DoDefaultMode,  TRUE,                    AHIR_FilterFunc,     (ULONG) &filter_hook,                    AHIR_FilterTags,     (ULONG) filter_tags,                    TAG_DONE );                if( req == NULL )                {                  quit = TRUE;                  rc   = FALSE;                }                else                {                  if( AHI_AudioRequest( req, TAG_DONE ) )                  {                    char buffer[ 256 ];                    audio_mode = req->ahiam_AudioID;                    frequency  = req->ahiam_MixFreq;                                        if( AHI_GetAudioAttrs( audio_mode, NULL,                                           AHIDB_BufferLen, 255,                                           AHIDB_Name,      (ULONG) buffer,                                           TAG_DONE ) )                    {                      RefreshSetGadgetAttrs( gadgets[ GAD_MODE_INFO ], win_ptr, NULL,                          STRINGA_TextVal, (ULONG) buffer,                          TAG_DONE );                      RefreshSetGadgetAttrs( gadgets[ GAD_INSTALL ], win_ptr, NULL,                          GA_Disabled, FALSE,                          TAG_DONE );                    }                  }                  AHI_FreeAudioRequest( req );                }                break;              }              case GAD_INSTALL:              {                ULONG flags      = 0;                ULONG patch_rom  = 0;                ULONG patch_apps = 0;                ULONG toggle_led = 0;                GetAttr( GA_Selected, gadgets[ GAD_PATCH_ROM  ], &patch_rom );                GetAttr( GA_Selected, gadgets[ GAD_PATCH_APPS ], &patch_apps );                GetAttr( GA_Selected, gadgets[ GAD_TOGGLE_LED ], &toggle_led );                ClearList( &log_data );                if( patch_rom )                {                  flags |= PUHF_PATCH_ROM;                }                if( patch_apps )                {                  flags |= PUHF_PATCH_APPS;                }                if( toggle_led )                {                  flags |= PUHF_TOGGLE_LED;                }                                if( ! InstallPUH( flags,                          audio_mode, frequency,                          pd ) )                {                  LogPUH( pd, "Unable to install PUH." );                }                else                {                  RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_ROM ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_APPS ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_TOGGLE_LED ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_MODE_SELECT ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_INSTALL ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_UNINSTALL ], win_ptr, NULL,                      GA_Disabled, FALSE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_ACTIVATE ], win_ptr, NULL,                      GA_Disabled, FALSE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_DEACTIVATE ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                }                break;              }              case GAD_UNINSTALL:              {                ULONG patch_rom = 0;                ClearList( &log_data );                UninstallPUH( pd );                                RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_ROM ], win_ptr, NULL,                    GA_Disabled, FALSE,                    TAG_DONE );                GetAttr( GA_Selected, gadgets[ GAD_PATCH_ROM  ], &patch_rom );                                if( patch_rom )                {                  RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_APPS ], win_ptr, NULL,                      GA_Disabled, FALSE,                      TAG_DONE );                }                RefreshSetGadgetAttrs( gadgets[ GAD_TOGGLE_LED ], win_ptr, NULL,                    GA_Disabled, FALSE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_MODE_SELECT ], win_ptr, NULL,                    GA_Disabled, FALSE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_INSTALL ], win_ptr, NULL,                    GA_Disabled, FALSE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_UNINSTALL ], win_ptr, NULL,                    GA_Disabled, TRUE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_ACTIVATE ], win_ptr, NULL,                    GA_Disabled, TRUE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_DEACTIVATE ], win_ptr, NULL,                    GA_Disabled, TRUE,                    TAG_DONE );                break;              }              case GAD_ACTIVATE:              {                if( ! ActivatePUH( pd ) )                {                  LogPUH( pd, "Unable to activate PUH." );                }                else                {                  LogPUH( pd, "Activated PUH." );                  RefreshSetGadgetAttrs( gadgets[ GAD_ACTIVATE ], win_ptr, NULL,                      GA_Disabled, TRUE,                      TAG_DONE );                  RefreshSetGadgetAttrs( gadgets[ GAD_DEACTIVATE ], win_ptr, NULL,                      GA_Disabled, FALSE,                      TAG_DONE );                }                break;              }              case GAD_DEACTIVATE:              {                DeactivatePUH( pd );                LogPUH( pd, "Deactivated PUH." );                RefreshSetGadgetAttrs( gadgets[ GAD_ACTIVATE ], win_ptr, NULL,                    GA_Disabled, FALSE,                    TAG_DONE );                RefreshSetGadgetAttrs( gadgets[ GAD_DEACTIVATE ], win_ptr, NULL,                    GA_Disabled, TRUE,                    TAG_DONE );                break;              }                            case GAD_TEST:              {                WriteWord( &custom->dmacon, DMAF_AUD0 );                Delay( 1 );  // The infamous DMA-wait! ;-)                WriteLong( &custom->aud[ 0 ].ac_ptr, (ULONG) chip );                WriteWord( &custom->aud[ 0 ].ac_len, pooh11_sblen / 2);                WriteWord( &custom->aud[ 0 ].ac_per, 161 );                WriteWord( &custom->aud[ 0 ].ac_vol, 64 );                WriteWord( &custom->dmacon, DMAF_SETCLR | DMAF_AUD0 );                WriteWord( &custom->aud[ 0 ].ac_len, 1 );                break;              }                            case GAD_PATCH_ROM:                if( code )                {                  RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_APPS ], win_ptr, NULL,                                         GA_Disabled, FALSE,                                         TAG_DONE );                }                else                {                  RefreshSetGadgetAttrs( gadgets[ GAD_PATCH_APPS ], win_ptr, NULL,                                         GA_Disabled, TRUE,                                         GA_Selected, FALSE,                                         TAG_DONE );                }                break;            }                        break;          }          default:            break;        }      }    }  }    SetPUHLogger( NULL, pd );  WriteWord( &custom->dmacon, DMAF_AUD0 );  WriteWord( &custom->aud[ 0 ].ac_vol, 0 );  FreeVec( chip );  return rc;}