btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / src / add-ons / kernel / generic / tty / tty_private.h
blob97bbe680363b3c0fdf3888c03150e4824106b8c8
1 /*
2 * Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef TTY_PRIVATE_H
6 #define TTY_PRIVATE_H
8 #include <termios.h>
10 #include <Drivers.h>
11 #include <KernelExport.h>
12 #include <tty/tty_module.h>
14 #include <condition_variable.h>
15 #include <fs/select_sync_pool.h>
16 #include <lock.h>
17 #include <util/DoublyLinkedList.h>
19 #include "line_buffer.h"
22 #define TTY_BUFFER_SIZE 4096
24 // The only nice Rico'ism :)
25 #define CTRL(c) ((c) - 0100)
28 class RequestOwner;
29 class Semaphore;
30 struct tty;
31 struct tty_cookie;
33 class Request : public DoublyLinkedListLinkImpl<Request> {
34 public:
35 Request();
37 void Init(RequestOwner *owner, tty_cookie *cookie, size_t bytesNeeded);
39 tty_cookie *TTYCookie() const { return fCookie; }
41 void Notify(size_t bytesAvailable);
42 void NotifyError(status_t error);
44 bool WasNotified() const { return fNotified; }
45 bool HasError() const { return fError; }
47 void Dump(const char* prefix);
49 private:
50 RequestOwner *fOwner;
51 tty_cookie *fCookie;
52 size_t fBytesNeeded;
53 bool fNotified;
54 bool fError;
57 class RequestQueue {
58 public:
59 RequestQueue();
60 ~RequestQueue() {}
62 void Add(Request *request);
63 void Remove(Request *request);
65 Request *First() const { return fRequests.First(); }
66 bool IsEmpty() const { return fRequests.IsEmpty(); }
68 void NotifyFirst(size_t bytesAvailable);
69 void NotifyError(status_t error);
70 void NotifyError(tty_cookie *cookie, status_t error);
72 void Dump(const char* prefix);
74 private:
75 typedef DoublyLinkedList<Request> RequestList;
77 RequestList fRequests;
80 class RequestOwner {
81 public:
82 RequestOwner();
84 void Enqueue(tty_cookie *cookie, RequestQueue *queue1,
85 RequestQueue *queue2 = NULL);
86 void Dequeue();
88 void SetBytesNeeded(size_t bytesNeeded);
89 size_t BytesNeeded() const { return fBytesNeeded; }
91 status_t Wait(bool interruptable, bigtime_t timeout = B_INFINITE_TIMEOUT);
93 bool IsFirstInQueues();
95 void Notify(Request *request);
96 void NotifyError(Request *request, status_t error);
98 status_t Error() const { return fError; }
100 private:
101 ConditionVariable* fConditionVariable;
102 tty_cookie* fCookie;
103 status_t fError;
104 RequestQueue* fRequestQueues[2];
105 Request fRequests[2];
106 size_t fBytesNeeded;
110 struct tty_cookie : DoublyLinkedListLinkImpl<tty_cookie> {
111 struct tty *tty;
112 struct tty *other_tty;
113 uint32 open_mode;
114 int32 thread_count;
115 sem_id blocking_semaphore;
116 bool closed;
119 typedef DoublyLinkedList<tty_cookie> TTYCookieList;
121 struct tty_settings {
122 pid_t pgrp_id;
123 pid_t session_id;
124 struct termios termios;
125 struct winsize window_size;
128 struct tty {
129 int32 ref_count; // referenced by cookies
130 int32 open_count;
131 struct mutex lock;
132 tty_settings settings;
133 select_sync_pool* select_pool;
134 RequestQueue reader_queue;
135 RequestQueue writer_queue;
136 TTYCookieList cookies;
137 line_buffer input_buffer;
138 tty_service_func service_func;
139 uint32 pending_eof;
140 bool is_master;
141 uint8 hardware_bits;
145 extern struct mutex gTTYCookieLock;
146 extern struct recursive_lock gTTYRequestLock;
148 extern struct tty *tty_create(tty_service_func func, bool isMaster);
149 extern void tty_destroy(struct tty *tty);
151 extern tty_cookie *tty_create_cookie(struct tty *tty, struct tty *otherTTY,
152 uint32 openMode);
153 extern void tty_destroy_cookie(tty_cookie *cookie);
154 extern void tty_close_cookie(tty_cookie *cookie);
156 extern status_t tty_read(tty_cookie *cookie, void *buffer, size_t *_length);
157 extern status_t tty_write(tty_cookie *sourceCookie, const void *buffer,
158 size_t *_length);
159 extern status_t tty_control(tty_cookie *cookie, uint32 op, void *buffer,
160 size_t length);
161 extern status_t tty_select(tty_cookie *cookie, uint8 event, uint32 ref,
162 selectsync *sync);
163 extern status_t tty_deselect(tty_cookie *cookie, uint8 event, selectsync *sync);
165 extern status_t tty_input_lock(tty_cookie* cookie, bool lock);
166 extern status_t tty_hardware_signal(tty_cookie* cookie, int signal, bool);
168 #endif /* TTY_PRIVATE_H */