Files
w3m/js/w3m_events.h
2025-08-16 19:43:11 -04:00

143 lines
4.8 KiB
C

/*
* w3m Event System Header
*
* This file defines the event handling system for JavaScript integration
* in w3m. It provides event listener registration, event dispatching,
* and integration with w3m's existing input handling system.
*/
#ifndef W3M_EVENTS_H
#define W3M_EVENTS_H
#ifdef USE_JAVASCRIPT
#include "quickjs/quickjs.h"
#include "w3m_javascript.h"
#include "w3m_dom.h"
/* Forward declarations */
struct _Buffer;
struct _anchor;
struct form_list;
/* Event Types */
typedef enum {
W3M_EVENT_CLICK,
W3M_EVENT_SUBMIT,
W3M_EVENT_LOAD,
W3M_EVENT_UNLOAD,
W3M_EVENT_FOCUS,
W3M_EVENT_BLUR,
W3M_EVENT_CHANGE,
W3M_EVENT_KEYPRESS,
W3M_EVENT_KEYDOWN,
W3M_EVENT_KEYUP,
W3M_EVENT_MOUSEOVER,
W3M_EVENT_MOUSEOUT,
W3M_EVENT_TYPE_COUNT
} W3MEventType;
/* Event Listener Structure */
typedef struct W3MEventListener {
W3MEventType type;
JSValue callback; /* JavaScript callback function */
W3MElement *target; /* Target element */
int use_capture; /* Capture phase flag */
struct W3MEventListener *next; /* Linked list */
} W3MEventListener;
/* Event Object Structure */
typedef struct W3MEvent {
W3MEventType type;
const char *type_string; /* "click", "submit", etc. */
W3MElement *target; /* Event target */
W3MElement *currentTarget; /* Current target during propagation */
/* Event state */
int bubbles;
int cancelable;
int defaultPrevented;
int propagationStopped;
/* Event data */
union {
struct {
int button; /* Mouse button */
int clientX, clientY; /* Mouse coordinates */
} mouse;
struct {
int keyCode; /* Key code */
int charCode; /* Character code */
int ctrlKey, altKey, shiftKey;
} keyboard;
struct {
char *data; /* Generic data */
} generic;
} data;
/* JavaScript object reference */
JSValue js_event;
} W3MEvent;
/* Event System State */
typedef struct {
W3MEventListener *listeners[W3M_EVENT_TYPE_COUNT];
W3MEvent **event_queue;
int queue_size;
int queue_capacity;
int processing_events;
} W3MEventSystem;
/* Event System Management */
W3MEventSystem *w3m_events_create_system(void);
void w3m_events_destroy_system(W3MEventSystem *system);
void w3m_events_process_queue(W3MEventSystem *system, W3MJSContext *ctx);
/* Event Listener Management */
void w3m_events_add_listener(W3MEventSystem *system, W3MElement *target,
const char *type, JSValue callback, int use_capture);
void w3m_events_remove_listener(W3MEventSystem *system, W3MElement *target,
const char *type, JSValue callback, int use_capture);
int w3m_events_has_listener(W3MEventSystem *system, W3MElement *target, const char *type);
/* Event Creation and Dispatch */
W3MEvent *w3m_events_create_event(W3MEventType type, W3MElement *target);
void w3m_events_destroy_event(W3MEvent *event);
int w3m_events_dispatch_event(W3MEventSystem *system, W3MJSContext *ctx, W3MEvent *event);
/* Event Type Utilities */
W3MEventType w3m_events_string_to_type(const char *type_string);
const char *w3m_events_type_to_string(W3MEventType type);
/* Integration with w3m Input System */
int w3m_events_handle_click(Buffer *buf, Anchor *anchor);
int w3m_events_handle_form_submit(Buffer *buf, FormList *form);
int w3m_events_handle_key_press(Buffer *buf, int key);
void w3m_events_handle_page_load(Buffer *buf);
void w3m_events_handle_page_unload(Buffer *buf);
/* JavaScript Event API Functions */
JSValue js_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_dispatchEvent(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
/* Event Object JavaScript API */
JSValue js_event_preventDefault(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_event_stopPropagation(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_event_get_target(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_event_get_type(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
/* Event Creation from JavaScript */
JSValue js_createEvent(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
JSValue js_Event_constructor(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
/* Helper Functions */
void w3m_events_bind_to_js(W3MJSContext *ctx, W3MEventSystem *system);
JSValue w3m_events_event_to_js(W3MJSContext *ctx, W3MEvent *event);
W3MEvent *w3m_events_js_to_event(W3MJSContext *ctx, JSValue val);
#endif /* USE_JAVASCRIPT */
#endif /* W3M_EVENTS_H */