aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-vt.h
blob: 93e05ded28e15332ab78c19682a2e1a0a3aab83c (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
 * mds — A micro-display server
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 <http://www.gnu.org/licenses/>.
 */
#ifndef MDS_MDS_VT_H
#define MDS_MDS_VT_H


#include "mds-base.h"

#include <sys/stat.h>
#include <linux/vt.h>



/**
 * Perform a VT switch requested by the OS kernel
 * 
 * @param   leave_foreground  Whether the display is leaving the foreground
 * @return                    Zero on success, -1 on error
 */
int switch_vt(int leave_foreground);


/**
 * Handle the received message
 * 
 * @return  Zero on success, -1 on error
 */
int handle_message(void);

/**
 * Handle a received `Command: get-vt` message
 * 
 * @param   client   The value of the header `Client ID` in the received message
 * @param   message  The value of the header `Message ID` in the received message
 * @return           Zero on success, -1 on error
 */
int handle_get_vt(const char* client, const char* message);

/**
 * Handle a received `Command: configure-vt` message
 * 
 * @param   client     The value of the header `Client ID` in the received message
 * @param   message    The value of the header `Message ID` in the received message
 * @param   graphical  The value of the header `Graphical` in the received message
 * @param   exclusive  The value of the header `Exclusive` in the received message
 * @return             Zero on success, -1 on error
 */
int handle_configure_vt(const char* client, const char* message, const char* graphical, const char* exclusive);


/**
 * This function is called when the kernel wants
 * to switch foreground virtual terminal
 * 
 * @param  signo  The received signal number
 */
void received_switch_vt(int signo);


/**
 * Send a full message even if interrupted
 * 
 * @param   message  The message to send
 * @param   length   The length of the message
 * @return           Non-zero on success
 */
int full_send(const char* message, size_t length);


/**
 * Get the index of the next available virtual terminal
 * 
 * @return  -1 on error, 0 if the terminals are exhausted, otherwise the next terminal
 */
int vt_get_next_available(void);


/**
 * Get the currently active virtual terminal
 * 
 * @return  -1 on error, otherwise the current terminal
 */
int vt_get_active(void);


/**
 * Change currently active virtual terminal and wait for it to complete the switch
 * 
 * @param   vt  The index of the terminal
 * @return      Zero on success, -1 on error
 */
int vt_set_active(int vt);


/**
 * Open a virtual terminal
 * 
 * @param   vt        The index of the terminal
 * @param   old_stat  Output parameter for the old file stat for the terminal
 * @return            The file descriptor for the terminal, -1 on error
 */
int vt_open(int vt, struct stat* restrict old_stat);


/**
 * Close a virtual terminal
 * 
 * @param  vt        The index of the terminal
 * @param  old_stat  The old file stat for the terminal
 */
void vt_close(int fd, struct stat* restrict old_stat);



/**
 * Block or stop blocking other programs to open the a terminal
 * 
 * @param   fd:int         File descriptor for the terminal
 * @param   exclusive:int  Whether to block other programs for using the terminal
 * @return  :int           Zero on success, -1 on error
 */
#define vt_set_exclusive(fd, exclusive)  \
  (ioctl(fd, (exclusive) ? TIOCEXCL : TIOCNXCL))


/**
 * Configure a terminal to be set to either graphical mode or text mode
 * 
 * @param   fd:int         File descriptor for the terminal
 * @param   graphical:int  Whether to use graphical mode
 * @return  :int           Zero on success, -1 on error
 */
#define vt_set_graphical(fd, graphical)  \
  (ioctl(fd, KDSETMODE, (graphical) ? KD_GRAPHICS : KD_TEXT))


/**
 * Construct a virtual terminal mode that can be used in `vt_get_set_mode`
 * 
 * @param  vt_switch_control  Whether we want to be able to block and delay VT switches
 * @param  vt_leave_signal    The signal that should be send to us we a process is trying
 *                            to switch terminal to another terminal
 * @param  vt_enter_signal    The signal that should be send to us we a process is trying
 *                            to switch terminal to our terminal
 * @param  mode               Output parameter
 */
void vt_construct_mode(int vt_switch_control, int vt_leave_signal,
		       int vt_enter_signal, struct vt_mode* restrict mode);


/**
 * Set or get the mode for a virtual terminal
 * 
 * @param   fd:int                File descriptor for the terminal
 * @param   set:int               Whether to set the mode
 * @param   mode:struct vt_mode*  Input or outpur parameter for the mode
 * @return  :int                  Zero on success, -1 on error
 */
#define vt_get_set_mode(fd, set, mode)  \
  (ioctl(fd, set ? VT_SETMODE : VT_GETMODE, mode))


/**
 * Block or temporarily block virtual terminal switch
 * 
 * @param   fd:int  File descriptor for our terminal
 * @return  :int    Zero on success, -1 on error
 */
#define vt_stop_switch(fd)  \
  (ioctl(fd, VT_RELDISP, 0))


/**
 * Allow a temporarily block virtual terminal switch to continue
 * 
 * @param   fd:int  File descriptor for our terminal
 * @return  :int    Zero on success, -1 on error
 */
#define vt_continue_switch(fd)  \
  (ioctl(fd, VT_RELDISP, 1))


/**
 * Accept a virtual terminal switch
 * 
 * @param   fd:int  File descriptor for our terminal
 * @return  :int    Zero on success, -1 on error
 */
#define vt_accept_switch(fd)  \
  (ioctl(fd, VT_RELDISP, VT_ACKACQ))


#endif