Compare commits

...

15 Commits

Author SHA1 Message Date
Xavier Del Campo Romero b4c08b585c net.h: Add missing extern "C" 2022-09-23 04:39:36 +02:00
Xavier Del Campo Romero e6ae88aa87 input.c: do not fail on input_render
Implementation is still incomplete, and should not crash the
application.
2022-09-23 04:38:33 +02:00
Xavier Del Campo Romero eed4687120 join_menu.c: Update serial/IPv4 GUI menus 2022-09-23 04:38:33 +02:00
Xavier Del Campo Romero 6f5058c584 ps1/net.c: return 0 on net_init
Even if the implementation is not finished yet, this will avoid the
program from crashing on startup.
2022-09-23 04:38:33 +02:00
Xavier Del Campo Romero 8435b3f5fd net: Implement net_available 2022-09-23 04:38:33 +02:00
Xavier Del Campo Romero b108afb6e7 net: Implement net_serial_devices
This function provides a platform-specific list of serial devices that
can be accessed.
2022-09-23 04:38:20 +02:00
Xavier Del Campo Romero 26692e8cb9 keyboard: Add KEYBOARD_KEY_SLASH 2022-09-23 04:20:57 +02:00
Xavier Del Campo Romero f8c74babda label.c: Set empty text by default
This will avoid crashing the application on rendering due to
uninitialized labels.
2022-09-23 04:20:13 +02:00
Xavier Del Campo Romero 9e308d97d9 gui.c: Remove unneeded condition
Even if neither hcentered or vcentered are used, GUI element position
must be determined by that of its parents.
2022-09-23 04:18:20 +02:00
Xavier Del Campo Romero 68ccd085fc gui.c: Fix minor const-correctness issues 2022-09-23 04:17:44 +02:00
Xavier Del Campo Romero d989dc6f49 gui: Allow elements to be hidden
When a GUI element is hidden, no rendering or updating is done to it or
its children. This can be useful to define a complex GUI tree structure
that changes under specific conditions, without redefining it.
2022-09-23 04:15:55 +02:00
Xavier Del Campo Romero d0089e7ddf gfx: Provide functions for fullscreen handling 2022-09-23 04:12:56 +02:00
Xavier Del Campo Romero 9d3e754f36 Implement checkbox GUI element 2022-09-23 04:12:07 +02:00
Xavier Del Campo Romero 467f09e952 net: Group events into struct 2022-09-21 18:27:58 +02:00
Xavier Del Campo Romero 85d4af0266 gui: fix wrong logic on update/render/deinit
The older implementation caused some elements to be
update/rendered/deinitialized more than once per call. For example,
considering the following tree of GUI elements:

A
    B
    C
        D
    E

The older implementation would update/render/deinitialize C and E more
than once, as shown below:

Update A
Update B
Update C
Update D
Update E
Update C

This was because a GUI element was acting on its siblings, but the
siblings would also act on theirs, causing the extra calls.

OTOH, as an additional improvement, no nested call is now required for
siblings.
2022-09-21 18:14:44 +02:00
27 changed files with 405 additions and 73 deletions

View File

@ -208,6 +208,14 @@ sprite(NAME line_edit_right
CY 491
TRANSPARENT FALSE)
sprite(NAME checkbox
X 376
Y 132
BPP 4
CX 368
CY 490
TRANSPARENT TRUE)
sound(NAME acknowledge_01)
sound(NAME acknowledge_02)
sound(NAME selected_01)
@ -218,6 +226,7 @@ container(NAME rts
btn_left
btn_mid
btn_right
checkbox
worker_n
worker_ne
worker_e

View File

@ -57,6 +57,8 @@ line_edit_mid.bmp:
line_edit_mid_24.bmp:
line_edit_right.bmp:
line_edit_right_24.bmp:
checkbox_24.bmp:
checkbox.bmp:
Derived works from ui_sheet.png
font.bmp:

BIN
res/checkbox.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 B

BIN
res/checkbox_24.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -7,6 +7,7 @@
#include <gui/button.h>
#include <gui/line_edit.h>
#include <gui/rounded_rect.h>
#include <gui/checkbox.h>
#include <resource.h>
#include <terrain.h>
#include <unit.h>
@ -273,6 +274,15 @@ static const struct container c[] =
{
.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_RIGHT]
}
},
{
.path = "checkbox",
.type = CONTAINER_TYPE_SPRITE,
.data =
{
.sprite = &gui_checkbox_sprite
}
}
};

View File

@ -13,6 +13,8 @@ extern "C"
int gfx_init(void);
int gfx_draw(void);
int gfx_toggle_fullscreen(void);
bool gfx_toggle_fullscreen_available(void);
bool gfx_fullscreen(void);
void sprite_sort(struct sprite *s);
int sprite_clone(const struct sprite *src, struct sprite *dst);
void rect_init(struct rect *r);

View File

@ -38,6 +38,16 @@ int gfx_toggle_fullscreen(void)
return -1;
}
bool gfx_toggle_fullscreen_available(void)
{
return false;
}
bool gfx_fullscreen(void)
{
return true;
}
bool gfx_inside_drawenv(const short x, const short y, const short w,
const short h)
{

View File

@ -163,6 +163,16 @@ int gfx_toggle_fullscreen(void)
return 0;
}
bool gfx_toggle_fullscreen_available(void)
{
return true;
}
bool gfx_fullscreen(void)
{
return fullscreen;
}
int gfx_draw(void)
{
enum {FPS = 50, REFRESH_MS = 1000 / FPS};

View File

@ -2,6 +2,7 @@ add_library(gui
"src/bar.c"
"src/button.c"
"src/button_type1.c"
"src/checkbox.c"
"src/container.c"
"src/gui.c"
"src/label.c"

View File

@ -24,7 +24,7 @@ struct gui_common
} *cb;
short x, y, xoff, yoff;
bool hcentered, vcentered;
bool hidden, hcentered, vcentered;
struct gui_common *parent, *child, *sibling;
};

View File

@ -0,0 +1,34 @@
#ifndef GUI_CHECKBOX_H
#define GUI_CHECKBOX_H
#include <gui.h>
#include <gfx.h>
#include <util.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct gui_checkbox
{
struct gui_common common;
bool active;
void *arg;
void (*on_pressed)(bool active, void *arg);
};
void gui_checkbox_init(struct gui_checkbox *c);
extern struct sprite gui_checkbox_sprite;
UTIL_STATIC_ASSERT(!offsetof(struct gui_checkbox, common),
"unexpected offset for struct gui_checkbox");
#ifdef __cplusplus
}
#endif
#endif /* GUI_CHECKBOX_H */

71
src/gui/src/checkbox.c Normal file
View File

@ -0,0 +1,71 @@
#include <gui.h>
#include <gui/checkbox.h>
#include <gui_private.h>
#include <gfx.h>
#include <stdbool.h>
struct sprite gui_checkbox_sprite;
static int render(const struct gui_common *const g)
{
const struct gui_checkbox *const c = (const struct gui_checkbox *)g;
sprite_get_or_ret(s, -1);
if (sprite_clone(&gui_checkbox_sprite, s))
return -1;
gui_coords(g, &s->x, &s->y);
s->w /= 2;
if (c->active)
s->u += s->w;
sprite_sort(s);
return 0;
}
static void get_dim(const struct gui_common *const g,
short *const w, short *const h)
{
*w = gui_checkbox_sprite.w / 2;
*h = gui_checkbox_sprite.h;
}
static int update(struct gui_common *const g,
const union peripheral *const p, const struct camera *const cam,
struct input *const in)
{
struct gui_checkbox *const c = (struct gui_checkbox *)g;
short w, h;
get_dim(g, &w, &h);
if (gui_pressed(g, in, p, cam, w, h))
{
c->active ^= true;
if (c->on_pressed)
c->on_pressed(c->active, c->arg);
}
return 0;
}
void gui_checkbox_init(struct gui_checkbox *const c)
{
static const struct gui_common_cb cb =
{
.get_dim = get_dim,
.update = update,
.render = render
};
*c = (const struct gui_checkbox)
{
.common =
{
.cb = &cb
}
};
}

View File

@ -7,7 +7,7 @@
static void add_child(struct gui_common *const parent,
struct gui_common *const child)
{
if (!child->cb || !child->cb->get_dim)
if (child->hidden || !child->cb || !child->cb->get_dim)
return;
struct gui_container *const c = (struct gui_container *)parent;
@ -62,11 +62,14 @@ static int update(struct gui_common *const g,
struct gui_common *const child = c->common.child;
if (child->cb->get_dim)
add_child(&c->common, child);
if (child && child->cb)
{
if (child->cb->get_dim)
add_child(&c->common, child);
for (struct gui_common *s = child->sibling; s; s = s->sibling)
add_child(&c->common, s);
for (struct gui_common *s = child->sibling; s; s = s->sibling)
add_child(&c->common, s);
}
return 0;
}

View File

@ -6,14 +6,15 @@
static void get_centered(const struct gui_common *const g,
short *const x, short *const y)
{
if (g->cb && g->cb->get_dim)
if (!g->hidden && g->cb && g->cb->get_dim)
{
short w, h, pw = 0, ph = 0;
const struct gui_common *p = g->parent;
if (g->parent)
if (p)
{
if (g->parent->cb && g->parent->cb->get_dim)
g->parent->cb->get_dim(g->parent, &pw, &ph);
if (!p->hidden && p->cb && p->cb->get_dim)
p->cb->get_dim(p, &pw, &ph);
}
else
{
@ -34,6 +35,9 @@ static void get_centered(const struct gui_common *const g,
void gui_coords(const struct gui_common *const g, short *const x,
short *const y)
{
if (g->hidden)
return;
*x = g->x + g->xoff;
*y = g->y + g->yoff;
@ -43,9 +47,7 @@ void gui_coords(const struct gui_common *const g, short *const x,
{
short px = p->x + p->xoff, py = p->y + p->yoff;
if (p->hcentered || p->vcentered)
gui_coords(p, &px, &py);
gui_coords(p, &px, &py);
*x += px;
*y += py;
}
@ -77,8 +79,11 @@ static bool check_collision(const struct gui_common *const g,
bool gui_pressed(const struct gui_common *const g,
const struct input *const in,
const union peripheral *const p,
const struct camera *cam, const short w, const short h)
const struct camera *const cam, const short w, const short h)
{
if (g->hidden)
return false;
bool check = false;
switch (p->common.type)
@ -103,8 +108,11 @@ bool gui_pressed(const struct gui_common *const g,
bool gui_released(const struct gui_common *const g,
const union peripheral *const p,
const struct camera *cam, const short w, const short h)
const struct camera *const cam, const short w, const short h)
{
if (g->hidden)
return false;
bool check = false;
switch (p->common.type)
@ -155,14 +163,14 @@ void gui_add_child(struct gui_common *const p,
int gui_update(struct gui_common *const g, const union peripheral *const p,
const struct camera *const c, struct input *const in)
{
if (g->child && gui_update(g->child, p, c, in))
return -1;
if (g->cb && g->cb->update && g->cb->update(g, p, c, in))
if (!g->hidden
&& ((g->child && gui_update(g->child, p, c, in))
|| (g->cb && g->cb->update && g->cb->update(g, p, c, in))))
return -1;
for (struct gui_common *s = g->sibling; s; s = s->sibling)
if (gui_update(s, p, c, in))
if (!s->hidden && ((s->child && gui_update(s->child, p, c, in))
|| (s->cb && s->cb->update && s->cb->update(s, p, c, in))))
return -1;
return 0;
@ -170,14 +178,13 @@ int gui_update(struct gui_common *const g, const union peripheral *const p,
int gui_render(const struct gui_common *const g)
{
if (g->cb && g->cb->render && g->cb->render(g))
return -1;
if (g->child && gui_render(g->child))
if (!g->hidden && ((g->cb && g->cb->render && g->cb->render(g))
|| (g->child && gui_render(g->child))))
return -1;
for (struct gui_common *s = g->sibling; s; s = s->sibling)
if (gui_render(s))
if (!s->hidden && ((s->cb && s->cb->render && s->cb->render(s))
|| (s->child && gui_render(s->child))))
return -1;
return 0;
@ -192,5 +199,11 @@ void gui_deinit(struct gui_common *const g, struct input *const in)
gui_deinit(g->child, in);
for (struct gui_common *s = g->sibling; s; s = s->sibling)
gui_deinit(s, in);
{
if (s->cb && s->cb->deinit)
s->cb->deinit(s, in);
if (s->child)
gui_deinit(s->child, in);
}
}

View File

@ -34,6 +34,8 @@ void gui_label_init(struct gui_label *const l)
.common =
{
.cb = &cb
}
},
.text = ""
};
}

View File

@ -100,7 +100,7 @@ int input_render(const struct input *const in, const union peripheral *const p)
switch (p->common.type)
{
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
return 0;
break;
case PERIPHERAL_TYPE_TOUCH:
/* Fall through. */
@ -108,7 +108,7 @@ int input_render(const struct input *const in, const union peripheral *const p)
break;
}
return -1;
return 0;
}
bool input_keyboard_justpressed(const struct input *const in,

View File

@ -58,7 +58,8 @@ extern "C"
X(KEYBOARD_KEY_BACKSPACE) \
X(KEYBOARD_KEY_SPACE) \
X(KEYBOARD_KEY_MINUS) \
X(KEYBOARD_KEY_DOT)
X(KEYBOARD_KEY_DOT) \
X(KEYBOARD_KEY_SLASH)
enum keyboard_key
{

View File

@ -54,6 +54,8 @@ static void key_event(const SDL_KeyboardEvent *const ev,
{.key = KEYBOARD_KEY_F11, .sdl_key = SDLK_F11},
{.key = KEYBOARD_KEY_SPACE, .sdl_key = SDLK_SPACE},
{.key = KEYBOARD_KEY_MINUS, .sdl_key = SDLK_MINUS},
{.key = KEYBOARD_KEY_SLASH, .sdl_key = SDLK_SLASH},
{.key = KEYBOARD_KEY_SLASH, .sdl_key = SDLK_KP_DIVIDE},
{.key = KEYBOARD_KEY_A, .sdl_key = SDLK_a},
{.key = KEYBOARD_KEY_B, .sdl_key = SDLK_b},
{.key = KEYBOARD_KEY_C, .sdl_key = SDLK_c},

View File

@ -68,7 +68,8 @@ char keyboard_to_char(const struct keyboard *const k,
{
{.key = KEYBOARD_KEY_DOT, .ch = '.'},
{.key = KEYBOARD_KEY_SPACE, .ch = ' '},
{.key = KEYBOARD_KEY_MINUS, .ch = '-'}
{.key = KEYBOARD_KEY_MINUS, .ch = '-'},
{.key = KEYBOARD_KEY_SLASH, .ch = '/'}
};
for (size_t i = 0; i < sizeof map / sizeof *map; i++)

View File

@ -1,13 +1,14 @@
#include <menu.h>
#include <menu_private.h>
#include <game.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <gui/checkbox.h>
#include <gui/label.h>
#include <gui/line_edit.h>
#include <gui/rounded_rect.h>
#include <net.h>
#include <net/serial.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
@ -16,8 +17,8 @@
struct join_menu
{
struct gui_container cnt, bcnt;
struct gui_label type_label, address, port, connecting;
struct gui_button type_btn, back, connect;
struct gui_label type_label, address_device, port, connecting, speed;
struct gui_button type_btn, back, connect, device_btn, speed_btn;
struct gui_line_edit address_le, port_le;
enum
@ -30,7 +31,8 @@ struct join_menu
} state;
struct net_socket *socket;
size_t domain_i;
size_t domain_i, speed_i, device_i, n_devices;
const char *const *devices;
};
static const enum net_domain domains[] =
@ -39,22 +41,22 @@ static const enum net_domain domains[] =
NET_DOMAIN_SERIAL
};
static const char *const speeds[] =
{
"19200", "38400", "57600", "115200"
};
static void on_connected(void *const arg)
{
struct join_menu *const m = arg;
if (m->state == IDLE)
{
m->state = CONNECT;
m->connecting.text = "Connecting...";
}
m->state = CONNECTED;
}
static void on_disconnected(void *const arg)
{
struct join_menu *const m = arg;
m->connecting.text = "Failed to connect";
m->state = CONNECT_FAILED;
m->socket = NULL;
}
@ -68,9 +70,12 @@ static int on_connect(struct join_menu *const m)
.common =
{
.domain = d,
.on_connected = on_connected,
.on_disconnected = on_disconnected,
.arg = m
.ev =
{
.connected = on_connected,
.disconnected = on_disconnected,
.arg = m
}
}
};
@ -80,10 +85,13 @@ static int on_connect(struct join_menu *const m)
{
errno = 0;
const unsigned long port = strtoul(m->port_le.text, NULL, 0);
const unsigned long port = strtoul(m->port_le.text, NULL, 10);
if (errno)
{
m->connecting.text = "Invalid port";
return -1;
}
c.ipv4.addr = m->address_le.text;
c.ipv4.port = port;
@ -97,6 +105,7 @@ static int on_connect(struct join_menu *const m)
if (!(m->socket = net_connect(&c)))
{
fprintf(stderr, "%s: net_connect failed\n", __func__);
on_disconnected(m);
return -1;
}
@ -120,17 +129,20 @@ static int update(struct menu_common *const c, void *const arg)
{
case CONNECT:
if (on_connect(m))
return -1;
{
m->state = CONNECT_FAILED;
break;
}
m->state = CONNECTING;
if (!m->connecting.common.parent)
gui_add_child(&m->cnt.common, &m->connecting.common);
m->connecting.text = "Connecting...";
m->connecting.common.hidden = false;
break;
case CONNECT_FAILED:
net_close(m->socket);
m->connecting.text = "Failed to connect";
m->connecting.common.hidden = false;
m->socket = NULL;
m->state = IDLE;
break;
@ -153,21 +165,80 @@ static int render(const struct menu_common *const c, void *const arg)
return 0;
}
static int update_domain(struct join_menu *const m)
{
switch (domains[m->domain_i])
{
case NET_DOMAIN_IPV4:
m->port.common.hidden = false;
m->port_le.common.hidden = false;
m->address_le.common.hidden = false;
m->device_btn.common.hidden = true;
m->speed.common.hidden = true;
m->speed_btn.common.hidden = true;
m->address_device.text = "Address:";
break;
case NET_DOMAIN_SERIAL:
m->port.common.hidden = true;
m->port_le.common.hidden = true;
m->address_le.common.hidden = true;
m->device_btn.common.hidden = false;
m->speed.common.hidden = false;
m->speed_btn.common.hidden = false;
m->address_device.text = "Device:";
m->speed_btn.u.type1.label.text = speeds[m->speed_i = 0];
m->devices = net_serial_devices(&m->n_devices);
if (!m->devices)
return -1;
m->device_btn.u.type1.label.text = m->devices[m->device_i = 0];
break;
}
return 0;
}
static void on_type_pressed(void *const arg)
{
struct join_menu *const m = arg;
if (++m->domain_i >= sizeof domains / sizeof *domains)
m->domain_i = 0;
do
if (++m->domain_i >= sizeof domains / sizeof *domains)
m->domain_i = 0;
while (!net_available(domains[m->domain_i]));
m->type_btn.u.type1.label.text = net_domain_str(domains[m->domain_i]);
update_domain(m);
}
static void on_device_pressed(void *const arg)
{
struct join_menu *const m = arg;
if (++m->device_i >= m->n_devices)
m->device_i = 0;
m->device_btn.u.type1.label.text = m->devices[m->device_i];
}
static void on_speed_pressed(void *const arg)
{
struct join_menu *const m = arg;
if (++m->speed_i >= sizeof speeds / sizeof *speeds)
m->speed_i = 0;
m->speed_btn.u.type1.label.text = speeds[m->speed_i];
}
static void on_connect_pressed(void *const arg)
{
struct join_menu *const m = arg;
m->state = CONNECT;
if (m->state == IDLE)
m->state = CONNECT;
}
int menu_join(struct menu_common *const c)
@ -188,18 +259,28 @@ int menu_join(struct menu_common *const c)
m.type_label.text = "Type:";
gui_add_child(&m.cnt.common, &m.type_label.common);
while (!net_available(domains[m.domain_i]))
if (++m.domain_i >= sizeof domains / sizeof *domains)
return -1;
gui_button_init(&m.type_btn, GUI_BUTTON_TYPE_1);
m.type_btn.arg = &m;
m.type_btn.u.type1.w = 140;
m.type_btn.common.hcentered = true;
m.type_btn.on_pressed = on_type_pressed;
m.type_btn.u.type1.label.text = net_domain_str(*domains);
m.type_btn.u.type1.label.text = net_domain_str(domains[m.domain_i]);
gui_add_child(&m.cnt.common, &m.type_btn.common);
gui_label_init(&m.address);
m.address.common.hcentered = true;
m.address.text = "Address:";
gui_add_child(&m.cnt.common, &m.address.common);
gui_label_init(&m.address_device);
m.address_device.common.hcentered = true;
gui_add_child(&m.cnt.common, &m.address_device.common);
gui_button_init(&m.device_btn, GUI_BUTTON_TYPE_1);
m.device_btn.common.hcentered = true;
m.device_btn.on_pressed = on_device_pressed;
m.device_btn.arg = &m;
m.device_btn.u.type1.w = 140;
gui_add_child(&m.cnt.common, &m.device_btn.common);
char address[sizeof "255.255.255.255"];
gui_line_edit_init(&m.address_le, address, sizeof address);
@ -218,8 +299,22 @@ int menu_join(struct menu_common *const c)
m.port_le.common.hcentered = true;
gui_add_child(&m.cnt.common, &m.port_le.common);
gui_label_init(&m.speed);
m.speed.font = FONT;
m.speed.text = "Baud rate:";
m.speed.common.hcentered = true;
gui_add_child(&m.cnt.common, &m.speed.common);
gui_button_init(&m.speed_btn, GUI_BUTTON_TYPE_1);
m.speed_btn.arg = &m;
m.speed_btn.on_pressed = on_speed_pressed;
m.speed_btn.u.type1.w = 140;
gui_add_child(&m.cnt.common, &m.speed_btn.common);
gui_label_init(&m.connecting);
m.connecting.common.hcentered = true;
m.connecting.common.hidden = true;
gui_add_child(&m.cnt.common, &m.connecting.common);
gui_container_init(&m.bcnt);
m.bcnt.common.hcentered = true;
@ -243,6 +338,9 @@ int menu_join(struct menu_common *const c)
m.back.common.vcentered = true;
gui_add_child(&m.bcnt.common, &m.back.common);
if (update_domain(&m))
return -1;
while (!back && !c->p.common.exit && !connect)
{
if (menu_update(c, update, render, &m))

View File

@ -1,9 +1,15 @@
#ifndef NET_H
#define NET_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
enum net_domain
{
NET_DOMAIN_IPV4,
@ -15,9 +21,13 @@ union net_connect
struct net_connect_common
{
enum net_domain domain;
void (*on_connected)(void *arg);
void (*on_disconnected)(void *arg);
void *arg;
struct net_connect_ev
{
void (*connected)(void *arg);
void (*disconnected)(void *arg);
void *arg;
} ev;
} common;
struct net_connect_ipv4
@ -67,7 +77,7 @@ struct net_socket;
int net_init(void);
void net_deinit(void);
int net_update(struct net_socket *s);
bool net_available(enum net_domain d);
struct net_socket *net_server(const union net_server *srv);
struct net_socket *net_connect(const union net_connect *c);
int net_read(struct net_socket *s, void *buf, size_t n);
@ -75,4 +85,8 @@ int net_write(struct net_socket *s, const void *buf, size_t n);
int net_close(struct net_socket *s);
const char *net_domain_str(enum net_domain d);
#ifdef __cplusplus
}
#endif
#endif /* NET_H */

17
src/net/inc/net/serial.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef NET_SERIAL_H
#define NET_SERIAL_H
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
const char *const *net_serial_devices(size_t *n);
#ifdef __cplusplus
}
#endif
#endif /* NET_SERIAL_H */

View File

@ -9,9 +9,7 @@ struct net_socket_domain
{
ENetHost *host;
ENetPeer *peers;
void (*on_connected)(void *arg);
void (*on_disconnected)(void *arg);
void *arg;
struct net_connect_ev ev;
};
enum
@ -27,9 +25,7 @@ struct net_socket_domain *net_connect_ipv4(const union net_connect *const c)
if (!s)
goto failure;
s->on_connected = c->common.on_connected;
s->on_disconnected = c->common.on_disconnected;
s->arg = c->common.arg;
s->ev = c->common.ev;
s->host = enet_host_create(NULL, 1, MAX_CHANNELS, 0, 0);
if (!s->host)
@ -96,14 +92,16 @@ int net_update_ipv4(struct net_socket_domain *const s)
switch (ev.type)
{
case ENET_EVENT_TYPE_CONNECT:
if (s->on_connected)
s->on_connected(s->arg);
if (s->ev.connected)
s->ev.connected(s->ev.arg);
break;
case ENET_EVENT_TYPE_DISCONNECT:
if (s->on_disconnected)
s->on_disconnected(s->arg);
if (s->ev.disconnected)
s->ev.disconnected(s->ev.arg);
break;
case ENET_EVENT_TYPE_RECEIVE:
break;

View File

@ -109,3 +109,8 @@ void net_deinit(void)
net_deinit_ipv4();
net_deinit_serial();
}
bool net_available(const enum net_domain d)
{
return true;
}

View File

@ -1,4 +1,5 @@
#include <net.h>
#include <net/serial.h>
#include <net_private.h>
#include <stddef.h>
@ -42,3 +43,8 @@ int net_init_serial(void)
void net_deinit_serial(void)
{
}
const char *const *net_serial_devices(size_t *const n)
{
return NULL;
}

View File

@ -1,5 +1,7 @@
#include <net.h>
#include <net/serial.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
int net_read(struct net_socket *const h, void *const buf, const size_t n)
@ -34,9 +36,25 @@ struct net_socket *net_server(const union net_server *const c)
int net_init(void)
{
return -1;
return 0;
}
void net_deinit(void)
{
}
bool net_available(const enum net_domain d)
{
return d == NET_DOMAIN_SERIAL;
}
const char *const *net_serial_devices(size_t *const n)
{
static const char *const dev[] =
{
"PS1 port"
};
*n = sizeof dev / sizeof *dev;
return dev;
}

View File

@ -42,3 +42,8 @@ int net_init_serial(void)
void net_deinit_serial(void)
{
}
const char *const *net_serial_devices(size_t *const n)
{
return NULL;
}