Alex 'AdUser' Z
4 years ago
3 changed files with 78 additions and 1 deletions
@ -0,0 +1,33 @@
|
||||
/* Copyright 2021 Alex 'AdUser' Z (ad_user@runbox.com)
|
||||
* |
||||
* This program is free software; you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License version 2 as |
||||
* published by the Free Software Foundation. |
||||
*/ |
||||
#include "common.h" |
||||
|
||||
static void (*handlers[4])(const char *) = { NULL, NULL, NULL, NULL }; |
||||
|
||||
void |
||||
f2b_event_send(const char *evt) { |
||||
for (short int i = 0; i < 3; i++) { |
||||
if (!handlers[i]) break; /* end of list */ |
||||
handlers[i](evt); |
||||
} |
||||
} |
||||
|
||||
bool |
||||
f2b_event_handler_register(void (*h)(const char *)) { |
||||
for (short int i = 0; i < 3; i++) { |
||||
if (handlers[i] == NULL) { |
||||
handlers[i] = h; |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
void |
||||
f2b_event_handler_flush() { |
||||
memset(handlers, 0x0, sizeof(handlers)); |
||||
} |
@ -0,0 +1,44 @@
|
||||
/* Copyright 2020-2021 Alex 'AdUser' Z (ad_user@runbox.com)
|
||||
* |
||||
* This program is free software; you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License version 2 as |
||||
* published by the Free Software Foundation. |
||||
*/ |
||||
#ifndef F2B_EVENT_H_ |
||||
#define F2B_EVENT_H_ |
||||
|
||||
/**
|
||||
* @file |
||||
* This file contains event-related routines |
||||
*/ |
||||
|
||||
/**
|
||||
* @def EVENT_MAX |
||||
* Maximum text length of event line |
||||
*/ |
||||
#define EVENT_MAX 128 |
||||
|
||||
/**
|
||||
* @brief Send event to all registered handlers |
||||
* @param evt Constant string in specified format |
||||
*/ |
||||
void |
||||
f2b_event_send(const char *evt); |
||||
|
||||
/**
|
||||
* @brief Adds event handler |
||||
* @param h Pointer to function |
||||
* @note For now you can register not more than three handlers. |
||||
* It will be called in order of registration |
||||
* @return true on success, false on error |
||||
*/ |
||||
bool |
||||
f2b_event_handler_register(void (*h)(const char *evt)); |
||||
|
||||
/**
|
||||
* @brief Clean event handlers list |
||||
*/ |
||||
void |
||||
f2b_event_handlers_flush(); |
||||
|
||||
#endif /* F2B_EVENT_H_ */ |
Loading…
Reference in new issue