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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* $Id$
*/
#include "x_event_source.h"
#include <kiklib/kik_def.h> /* USE_WIN32API */
#ifndef USE_WIN32API
#include <string.h> /* memset/memcpy */
#include <sys/time.h> /* timeval */
#include <unistd.h> /* select */
#include <kiklib/kik_file.h> /* kik_file_set_cloexec */
#endif
#include <kiklib/kik_debug.h>
#include <kiklib/kik_mem.h> /* alloca/kik_alloca_garbage_collect/malloc/free */
#include <kiklib/kik_types.h> /* u_int */
#include <ml_term_manager.h>
#include "x_display.h"
#include "x_screen_manager.h"
#if 0
#define __DEBUG
#endif
/* --- static variables --- */
#ifndef USE_WIN32API
static struct
{
int fd ;
void (*handler)( void) ;
} * additional_fds ;
static u_int num_of_additional_fds ;
#endif
/* --- static functions --- */
#ifdef USE_WIN32API
static VOID CALLBACK
timer_proc(
HWND hwnd,
UINT msg,
UINT timerid,
DWORD time
)
{
x_display_t ** displays ;
u_int num_of_displays ;
int count ;
displays = x_get_opened_displays( &num_of_displays) ;
for( count = 0 ; count < num_of_displays ; count ++)
{
x_display_idling( displays[count]) ;
}
}
#else /* USE_WIN32API */
static void
receive_next_event(void)
{
u_int count ;
ml_term_t ** terms ;
u_int num_of_terms ;
int xfd ;
int ptyfd ;
int maxfd ;
int ret ;
fd_set read_fds ;
struct timeval tval ;
x_display_t ** displays ;
u_int num_of_displays ;
num_of_terms = ml_get_all_terms( &terms) ;
while( 1)
{
/* on Linux tv_usec,tv_sec members are zero cleared after select() */
#if 0
tval.tv_usec = 50000 ; /* 0.05 sec */
#else
tval.tv_usec = 100000 ; /* 0.1 sec */
#endif
tval.tv_sec = 0 ;
maxfd = 0 ;
FD_ZERO( &read_fds) ;
displays = x_get_opened_displays( &num_of_displays) ;
for( count = 0 ; count < num_of_displays ; count ++)
{
x_display_receive_next_event( displays[count]) ;
xfd = x_display_fd( displays[count]) ;
FD_SET( xfd , &read_fds) ;
if( xfd > maxfd)
{
maxfd = xfd ;
}
}
for( count = 0 ; count < num_of_terms ; count ++)
{
ptyfd = ml_term_get_master_fd( terms[count]) ;
FD_SET( ptyfd , &read_fds) ;
if( ptyfd > maxfd)
{
maxfd = ptyfd ;
}
}
for( count = 0 ; count < num_of_additional_fds ; count++)
{
if( additional_fds[count].fd >= 0)
{
FD_SET( additional_fds[count].fd , &read_fds) ;
if( additional_fds[count].fd > maxfd)
{
maxfd = additional_fds[count].fd ;
}
}
}
if( ( ret = select( maxfd + 1 , &read_fds , NULL , NULL , &tval)) != 0)
{
break ;
}
for( count = 0 ; count < num_of_displays ; count ++)
{
x_display_idling( displays[count]) ;
}
for( count = 0 ; count < num_of_additional_fds ; count++)
{
if( additional_fds[count].fd < 0)
{
(*additional_fds[count].handler)() ;
}
}
}
if( ret < 0)
{
/* error happened */
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " error happened in select. ") ;
perror( NULL) ;
#endif
return ;
}
/*
* Processing order should be as follows.
*
* X Window -> PTY -> Socket -> additional_fds
*/
for( count = 0 ; count < num_of_displays ; count ++)
{
if( FD_ISSET( x_display_fd( displays[count]) , &read_fds))
{
x_display_receive_next_event( displays[count]) ;
}
}
for( count = 0 ; count < num_of_terms ; count ++)
{
if( FD_ISSET( ml_term_get_master_fd( terms[count]) , &read_fds))
{
ml_term_parse_vt100_sequence( terms[count]) ;
}
}
for( count = 0 ; count < num_of_additional_fds ; count++)
{
if( additional_fds[count].fd >= 0)
{
if( FD_ISSET( additional_fds[count].fd , &read_fds))
{
(*additional_fds[count].handler)() ;
break ;
}
}
}
return ;
}
#endif
/* --- global functions --- */
int
x_event_source_init(void)
{
#ifdef USE_WIN32API
/* x_window_manager_idling() called in 0.1sec. */
SetTimer( NULL, 0, 100, timer_proc) ;
#endif
return 1 ;
}
int
x_event_source_final(void)
{
#if ! defined(USE_WIN32GUI) && ! defined(DEBUG)
exit(0) ;
#endif
#ifndef USE_WIN32API
free( additional_fds) ;
#endif
return 1 ;
}
int
x_event_source_process(void)
{
#ifdef USE_WIN32API
u_int num_of_displays ;
x_display_t ** displays ;
ml_term_t ** terms ;
u_int num_of_terms ;
u_int count ;
#endif
#ifdef USE_WIN32API
displays = x_get_opened_displays( &num_of_displays) ;
for( count = 0 ; count < num_of_displays ; count++)
{
x_display_receive_next_event( displays[count]) ;
}
#else
receive_next_event() ;
#endif
ml_close_dead_terms() ;
#ifdef USE_WIN32API
/*
* XXX
* If pty is closed after ml_close_dead_terms() ...
*/
num_of_terms = ml_get_all_terms( &terms) ;
for( count = 0 ; count < num_of_terms ; count++)
{
ml_term_parse_vt100_sequence( terms[count]) ;
}
#endif
x_close_dead_screens() ;
if( x_get_all_screens( NULL) == 0)
{
return 0 ;
}
return 1 ;
}
#ifndef USE_WIN32API
/*
* fd >= 0 -> Normal file descriptor. handler is invoked if fd is ready.
* fd < 0 -> Special ID. handler is invoked at interval of 0.1 sec.
*/
int
x_event_source_add_fd(
int fd ,
void (*handler)(void)
)
{
void * p ;
if( ! handler)
{
return 0 ;
}
if( ( p = realloc( additional_fds ,
sizeof(*additional_fds) * (num_of_additional_fds + 1))) == NULL)
{
return 0 ;
}
additional_fds = p ;
additional_fds[num_of_additional_fds].fd = fd ;
additional_fds[num_of_additional_fds++].handler = handler ;
if( fd >= 0)
{
kik_file_set_cloexec( fd) ;
}
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " %d is added to additional fds.\n" , fd) ;
#endif
return 1 ;
}
int
x_event_source_remove_fd(
int fd
)
{
u_int count ;
for( count = 0 ; count < num_of_additional_fds ; count++)
{
if( additional_fds[count].fd == fd)
{
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " Additional fd %d is removed.\n" , fd) ;
#endif
additional_fds[count] = additional_fds[--num_of_additional_fds] ;
return 1 ;
}
}
return 0 ;
}
#endif
|