2 * Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
11 #include <KernelExport.h>
12 #include <tty/tty_module.h>
14 #include <condition_variable.h>
15 #include <fs/select_sync_pool.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)
33 class Request
: public DoublyLinkedListLinkImpl
<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
);
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
);
75 typedef DoublyLinkedList
<Request
> RequestList
;
77 RequestList fRequests
;
84 void Enqueue(tty_cookie
*cookie
, RequestQueue
*queue1
,
85 RequestQueue
*queue2
= NULL
);
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
; }
101 ConditionVariable
* fConditionVariable
;
104 RequestQueue
* fRequestQueues
[2];
105 Request fRequests
[2];
110 struct tty_cookie
: DoublyLinkedListLinkImpl
<tty_cookie
> {
112 struct tty
*other_tty
;
115 sem_id blocking_semaphore
;
119 typedef DoublyLinkedList
<tty_cookie
> TTYCookieList
;
121 struct tty_settings
{
124 struct termios termios
;
125 struct winsize window_size
;
129 int32 ref_count
; // referenced by cookies
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
;
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
,
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
,
159 extern status_t
tty_control(tty_cookie
*cookie
, uint32 op
, void *buffer
,
161 extern status_t
tty_select(tty_cookie
*cookie
, uint8 event
, uint32 ref
,
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 */