2006-01-08 18:15:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Richard Wilson <info@tinct.net>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-01-08 18:15:17 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Automated RISC OS message routing (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "oslib/os.h"
|
|
|
|
#include "oslib/wimp.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "riscos/message.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
2006-01-08 18:15:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
struct active_message {
|
|
|
|
unsigned int message_code;
|
|
|
|
int id;
|
2006-01-09 03:12:09 +03:00
|
|
|
void (*callback)(wimp_message *message);
|
2006-01-08 18:15:17 +03:00
|
|
|
struct active_message *next;
|
|
|
|
struct active_message *previous;
|
|
|
|
};
|
|
|
|
struct active_message *current_messages = NULL;
|
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
static struct active_message *ro_message_add(unsigned int message_code,
|
2006-01-09 03:12:09 +03:00
|
|
|
void (*callback)(wimp_message *message));
|
2006-01-08 23:02:26 +03:00
|
|
|
static void ro_message_free(int ref);
|
|
|
|
|
2006-01-08 18:15:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message and registers a return route for a bounce.
|
|
|
|
*
|
|
|
|
* \param event the message event type
|
|
|
|
* \param message the message to register a route back for
|
|
|
|
* \param task the task to send a message to, or 0 for broadcast
|
|
|
|
* \param callback the code to call on a bounce
|
|
|
|
* \return true on success, false otherwise
|
|
|
|
*/
|
|
|
|
bool ro_message_send_message(wimp_event_no event, wimp_message *message,
|
2008-07-27 02:29:15 +04:00
|
|
|
wimp_t task, void (*callback)(wimp_message *message))
|
|
|
|
{
|
2006-01-08 18:15:17 +03:00
|
|
|
os_error *error;
|
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
assert(message);
|
|
|
|
|
2006-01-08 18:15:17 +03:00
|
|
|
/* send a message */
|
|
|
|
error = xwimp_send_message(event, message, task);
|
|
|
|
if (error) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("xwimp_send_message: 0x%x: %s", error->errnum, error->errmess);
|
2006-01-08 18:15:17 +03:00
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
return false;
|
|
|
|
}
|
2006-01-08 23:02:26 +03:00
|
|
|
|
2006-01-08 18:15:17 +03:00
|
|
|
/* register the default bounce handler */
|
|
|
|
if (callback) {
|
|
|
|
assert(event == wimp_USER_MESSAGE_RECORDED);
|
2006-02-16 02:09:55 +03:00
|
|
|
return ro_message_register_handler(message, message->action,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message and registers a return route for a bounce.
|
|
|
|
*
|
|
|
|
* \param event the message event type
|
|
|
|
* \param message the message to register a route back for
|
|
|
|
* \param to_w the window to send the message to
|
|
|
|
* \param to_i the icon
|
|
|
|
* \param callback the code to call on a bounce
|
|
|
|
* \param to_t receives the task handle of the window's creator
|
|
|
|
* \return true on success, false otherwise
|
|
|
|
*/
|
|
|
|
bool ro_message_send_message_to_window(wimp_event_no event, wimp_message *message,
|
|
|
|
wimp_w to_w, wimp_i to_i, void (*callback)(wimp_message *message),
|
2008-07-27 02:29:15 +04:00
|
|
|
wimp_t *to_t)
|
|
|
|
{
|
2006-02-16 02:09:55 +03:00
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
assert(message);
|
|
|
|
|
|
|
|
/* send a message */
|
|
|
|
error = xwimp_send_message_to_window(event, message, to_w, to_i, to_t);
|
|
|
|
if (error) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("xwimp_send_message_to_window: 0x%x: %s", error->errnum, error->errmess);
|
2006-02-16 02:09:55 +03:00
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register the default bounce handler */
|
|
|
|
if (callback) {
|
|
|
|
assert(event == wimp_USER_MESSAGE_RECORDED);
|
2006-01-08 18:15:17 +03:00
|
|
|
return ro_message_register_handler(message, message->action,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a return route for a message.
|
|
|
|
*
|
|
|
|
* This function must be called after wimp_send_message so that a
|
|
|
|
* valid value is present in the my_ref field.
|
|
|
|
*
|
|
|
|
* \param message the message to register a route back for
|
2006-01-08 23:02:26 +03:00
|
|
|
* \param message_code the message action code to route
|
2006-01-08 18:15:17 +03:00
|
|
|
* \param callback the code to call for a matched action
|
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
|
|
|
bool ro_message_register_handler(wimp_message *message,
|
|
|
|
unsigned int message_code,
|
2008-07-27 02:29:15 +04:00
|
|
|
void (*callback)(wimp_message *message))
|
|
|
|
{
|
2006-01-08 18:15:17 +03:00
|
|
|
struct active_message *add;
|
|
|
|
|
|
|
|
assert(message);
|
|
|
|
assert(callback);
|
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
add = ro_message_add(message_code, callback);
|
|
|
|
if (add)
|
|
|
|
add->id = message->my_ref;
|
|
|
|
return (add != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a route for a message code.
|
|
|
|
*
|
|
|
|
* \param message_code the message action code to route
|
|
|
|
* \param callback the code to call for a matched action
|
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
|
|
|
bool ro_message_register_route(unsigned int message_code,
|
2008-07-27 02:29:15 +04:00
|
|
|
void (*callback)(wimp_message *message))
|
|
|
|
{
|
2006-01-08 23:02:26 +03:00
|
|
|
assert(callback);
|
2006-11-27 18:35:18 +03:00
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
return (ro_message_add(message_code, callback) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct active_message *ro_message_add(unsigned int message_code,
|
2008-07-27 02:29:15 +04:00
|
|
|
void (*callback)(wimp_message *message))
|
|
|
|
{
|
2006-01-08 23:02:26 +03:00
|
|
|
struct active_message *add;
|
|
|
|
|
|
|
|
assert(callback);
|
|
|
|
|
2006-01-08 18:15:17 +03:00
|
|
|
add = (struct active_message *)malloc(sizeof(*add));
|
|
|
|
if (!add)
|
2006-01-08 23:02:26 +03:00
|
|
|
return NULL;
|
2006-01-08 18:15:17 +03:00
|
|
|
add->message_code = message_code;
|
2006-01-08 23:02:26 +03:00
|
|
|
add->id = 0;
|
2006-01-08 18:15:17 +03:00
|
|
|
add->callback = callback;
|
|
|
|
add->next = current_messages;
|
|
|
|
add->previous = NULL;
|
|
|
|
current_messages = add;
|
2006-11-27 18:35:18 +03:00
|
|
|
return add;
|
2006-01-08 18:15:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to route a message.
|
|
|
|
*
|
2014-11-13 01:36:11 +03:00
|
|
|
* \param event wimp event
|
|
|
|
* \param message the message to attempt to route
|
2006-01-08 18:15:17 +03:00
|
|
|
* \return true if message was routed, false otherwise
|
|
|
|
*/
|
2008-07-27 02:29:15 +04:00
|
|
|
bool ro_message_handle_message(wimp_event_no event, wimp_message *message)
|
|
|
|
{
|
2006-01-08 18:15:17 +03:00
|
|
|
struct active_message *test;
|
|
|
|
|
|
|
|
assert(message);
|
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
if (event == wimp_USER_MESSAGE_ACKNOWLEDGE) {
|
|
|
|
/* handle message acknowledgement */
|
2014-05-26 14:57:40 +04:00
|
|
|
bool handled = false;
|
|
|
|
int ref = message->my_ref;
|
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
if (ref == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* handle the message */
|
|
|
|
for (test = current_messages; test; test = test->next) {
|
|
|
|
if ((ref == test->id) &&
|
|
|
|
(message->action == test->message_code)) {
|
|
|
|
handled = true;
|
|
|
|
if (test->callback)
|
2006-01-09 03:12:09 +03:00
|
|
|
test->callback(message);
|
2006-01-08 23:02:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-01-08 18:15:17 +03:00
|
|
|
|
2006-01-08 23:02:26 +03:00
|
|
|
/* remove all handlers for this id */
|
|
|
|
ro_message_free(ref);
|
|
|
|
return handled;
|
|
|
|
} else {
|
|
|
|
/* handle simple routing */
|
|
|
|
for (test = current_messages; test; test = test->next) {
|
|
|
|
if ((test->id == 0) &&
|
|
|
|
(message->action == test->message_code)) {
|
2006-01-09 03:12:09 +03:00
|
|
|
test->callback(message);
|
2006-01-08 23:02:26 +03:00
|
|
|
return true;
|
|
|
|
}
|
2006-01-08 18:15:17 +03:00
|
|
|
}
|
|
|
|
}
|
2006-01-08 23:02:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-27 02:29:15 +04:00
|
|
|
void ro_message_free(int ref)
|
|
|
|
{
|
2006-01-08 23:02:26 +03:00
|
|
|
struct active_message *test;
|
|
|
|
struct active_message *next = current_messages;
|
2006-01-08 18:15:17 +03:00
|
|
|
|
|
|
|
while ((test = next)) {
|
2006-01-08 23:02:26 +03:00
|
|
|
next = test->next;
|
2006-01-08 18:15:17 +03:00
|
|
|
if (ref == test->id) {
|
|
|
|
if (test->previous)
|
|
|
|
test->previous->next = test->next;
|
|
|
|
if (test->next)
|
|
|
|
test->next->previous = test->previous;
|
|
|
|
if (current_messages == test)
|
|
|
|
current_messages = test->next;
|
2006-01-08 23:02:26 +03:00
|
|
|
free(test);
|
2006-01-08 18:15:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|