AmiTimeKeeper/notify.c

141 lines
3.7 KiB
C

/*-
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <dos/dos.h>
#include <dos/notify.h>
#include <assert.h>
#include "config.h"
#include "mem.h"
#include "logmod.h"
#define MODULENAME "Notify"
struct NotifyRequestNode
{
long Registered;
struct NotifyRequest *Request;
struct NotifyRequestNode *Next;
};
static struct NotifyRequestNode *Last = NULL;
static void AddRequest(struct NotifyRequest *request)
{
struct NotifyRequestNode *node = AllocStructSafe(struct NotifyRequestNode);
if (node == NULL)
{
return;
}
// Init node
node->Registered = DOSFALSE;
node->Request = request;
node->Next = NULL;
// Add to list
Forbid();
if (Last == NULL)
{
Last = node;
}
else
{
node->Next = Last;
Last = node;
}
Permit();
}
void ActivateNotifyPort(struct MsgPort *port)
{
assert(port != NULL);
struct NotifyRequestNode *current = Last;
while (current != NULL)
{
if (current->Registered != DOSTRUE)
{
current->Request->nr_stuff.nr_Msg.nr_Port = port;
current->Registered = StartNotify(current->Request);
if (current->Registered == DOSTRUE)
{
LogInfo("Watching for changes in %s", current->Request->nr_Name);
}
else
{
LogWarn("Could not get notifications for %s", current->Request->nr_Name);
}
}
current = current->Next;
}
}
void WatchFile(char *file, long type)
{
struct NotifyRequest *request;
request = AllocStructSafe(struct NotifyRequest);
if (request == NULL)
{
return;
}
request->nr_Name = (UBYTE *)StrDupSafe(file);
if (request->nr_Name == NULL)
{
return;
}
request->nr_Flags = NRF_SEND_MESSAGE | NRF_WAIT_REPLY;
request->nr_UserData = type;
AddRequest(request);
}
void CleanupNotifications(void)
{
volatile struct NotifyRequestNode *current, *next;
current = Last;
while (current != NULL)
{
if (current->Registered == DOSTRUE)
{
LogTrace("Removing notification for %s", current->Request->nr_Name);
EndNotify(current->Request);
}
FreeMemSafe((void *)current->Request->nr_Name);
FreeMemSafe((void *)current->Request);
next = current->Next;
FreeMemSafe((void *)current);
current = next;
}
Last = NULL;
}