/* * secde -- experimental lightweight Wayland/X11 server * Copyright (C) 2019 Samuel Lidén Borell * * This program 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, either version 3 of the License, or * (at your option) any later version. * * This program 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 . */ #ifndef PROTOHND_H #define PROTOHND_H /** Bitmask for the type of a file descriptor */ typedef enum { FDF_SOCKET = 0x1, /* uses recv/send */ FDF_SERVER = 0x2, /* supports accept */ FDF_READABLE = 0x4 /* supports read/recv and send/write */ } FdFlags; #define FDTYPE_SERVERSOCK (FDF_SOCKET | FDF_SERVER) #define FDTYPE_CLIENTSOCK (FDF_SOCKET | FDF_READABLE) #define FDTYPE_CHARDEV (FDF_READABLE) /** * Type of protocol. Some use sockets with clients * also, others use character devices. */ typedef enum { FDP_UNSPEC = 0, /* used for client fds in protohnd_fdadded */ FDP_X11, /* socket */ FDP_PULSE, /* socket */ FDP_WAYLAND, /* socket */ FDP_KMS, /* character device */ FDP_FBDEV, /* character device */ FDP_EVDEV, /* character device */ FDP_ALSA, /* character device */ FDP_V4L2, /* character device */ FDP_INOTIFY /* character device */ } FdProtocol; typedef struct FdInfo FdInfo; /** Called when the server has begun to start */ void protohnd_initing(void); /** * Called when the server has been initialized. * Devices and sockets may be added even after initialization. */ void protohnd_inited(void); /** * Returns the FdInfo pointer for a given file descriptor, or * NULL if not found. */ FdInfo *protohnd_getfdinfo(int fd); /** Returns the file descriptor from the given FdInfo pointer */ int protohnd_getfd(struct FdInfo *info); /** Returns the FdFlags mask for a given FdInfo pointer */ FdFlags protohnd_getfdflags(struct FdInfo *info); /** * Called when some type of socket (client or server) or file * descriptor (like a character device) has been added. * * \param server If the new fd is a client, then this is the server * \param fd New file descriptor to add * \param flags Flags for file descriptor. * \param protocol Protocol of file descriptor. FDP_UNSPEC for clients. * \return FdInfo pointer, or NULL if the fd should be closed */ /* TODO it would be nice to have a "user" parameter here, that was returned and then passed into all places where the serversock is passed */ struct FdInfo *protohnd_fdadded(struct FdInfo *server, int fd, FdFlags flags, FdProtocol protocol /*, cliantaddr, clientaddrsize*/); /** * May be called when a client has closed a connection. * There might still be data to read. * There is no guarantee that this function will be called. */ void protohnd_willclose(struct FdInfo *info); /** * Called when a file descriptor has been completely closed and no more * data remains to be read. */ void protohnd_closed(struct FdInfo *info); /** Called when data has been received */ void protohnd_received(struct FdInfo *info, const unsigned char *buff, int numrecv); /** * Called to notify when the fd is ready or no longer * ready to send data. * * \param info FdInfo pointer * \param state 1 if ready, 0 if not ready */ void protohnd_writeready(struct FdInfo *info, int state); /** * Called when data can be written. Returns the FdInfo structure of * the file descriptor to write to, or NULL if there is nothing to * write. * * The buff and length arguments are initialized to point to a static buffer * and the length of it, but may be changed to point to a longer buffer. * * Set clientmore if there is more data to write to the returned fd. * * Writes may or may not succeed. The protohnd_written function will be * called with the write status, and with the same FdInfo pointer. */ struct FdInfo *protohnd_getwrite(unsigned char **buff, int *length, int *clientmore); /** * Called when data has been written, or when a write has failed. * Result is set to: 0 for success, 1 for error, and -1 if one should try again. */ void protohnd_written(struct FdInfo *info, int numwritten, int remaining, int result); #endif