summaryrefslogtreecommitdiff
path: root/protohnd.h
blob: fce702497df1b90f17125dcc02391b6f8aafbeb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

/*
 * 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 <https://www.gnu.org/licenses/>.
 */

#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