forked from e16/e16
1
0
Fork 0

Add registration of file descriptors to be handled in main event loop.

SVN revision: 32943
This commit is contained in:
Kim Woelders 2007-12-02 18:39:17 +00:00
parent 4a7eb693c7
commit 32ee6600de
4 changed files with 163 additions and 114 deletions

View File

@ -25,7 +25,6 @@
#include "aclass.h"
#include "emodule.h"
#include "events.h"
#include "session.h"
#include "timers.h"
#include "tooltips.h"
#include "xwin.h"
@ -53,6 +52,10 @@
static const char *EventName(unsigned int type);
#endif
/*
* Server extension handling
*/
typedef struct
{
int event_base, error_base;
@ -199,6 +202,41 @@ ExtQuery(const EServerExt * ext)
ext->init(available);
}
/*
* File descriptor handling
*/
struct _EventFdDesc
{
const char *name;
int fd;
void (*handler) (void);
};
static int nfds = 0;
static EventFdDesc *pfds = NULL;
EventFdDesc *
EventFdRegister(int fd, EventFdHandler * handler)
{
nfds++;
pfds = EREALLOC(EventFdDesc, pfds, nfds);
pfds[nfds - 1].fd = fd;
pfds[nfds - 1].handler = handler;
return pfds + (nfds - 1);
}
void
EventFdUnregister(EventFdDesc * efd)
{
efd->fd = -1;
}
/*
* Event handling
*/
#define DOUBLE_CLICK_TIME 250 /* Milliseconds */
void
@ -219,6 +257,8 @@ EventsInit(void)
ExtData[XEXT_COMPOSITE].minor >= 2))
Mode.server.extensions |= 1 << XEXT_CM_ALL;
#endif
EventFdRegister(ConnectionNumber(disp), NULL);
}
static void
@ -647,8 +687,7 @@ EventsMain(void)
struct timeval tval;
double time1, time2, dt;
int count, pfetch;
int fdsize;
int xfd, smfd;
int fdsize, fd, i;
time1 = GetTime();
@ -691,12 +730,17 @@ EventsMain(void)
continue;
FD_ZERO(&fdset);
xfd = ConnectionNumber(disp);
FD_SET(xfd, &fdset);
smfd = GetSMfd();
if (smfd >= 0)
FD_SET(smfd, &fdset);
fdsize = MAX(xfd, smfd) + 1;
fdsize = -1;
for (i = 0; i < nfds; i++)
{
fd = pfds[i].fd;
if (fd < 0)
continue;
if (fdsize < fd)
fdsize = fd;
FD_SET(fd, &fdset);
}
fdsize++;
if (time2 > 0.)
{
@ -711,9 +755,8 @@ EventsMain(void)
if (EDebug(EDBUG_TYPE_EVENTS))
Eprintf
("EventsMain - count=%d xfd=%d:%d smfd=%d:%d dt=%lf time2=%lf\n",
count, xfd, FD_ISSET(xfd, &fdset), smfd,
(smfd >= 0) ? FD_ISSET(smfd, &fdset) : 0, dt, time2);
("EventsMain - count=%d xfd=%d:%d dt=%lf time2=%lf\n",
count, pfds[0].fd, FD_ISSET(pfds[0].fd, &fdset), dt, time2);
if (count == 0)
{
@ -722,11 +765,16 @@ EventsMain(void)
}
else if (count > 0)
{
if ((smfd >= 0) && (FD_ISSET(smfd, &fdset)))
/* Excluding X fd */
for (i = 1; i < nfds; i++)
{
if (EDebug(EDBUG_TYPE_EVENTS))
Eprintf("EventsMain - ICE\n");
ProcessICEMSGS();
fd = pfds[i].fd;
if ((fd >= 0) && (FD_ISSET(fd, &fdset)))
{
if (EDebug(EDBUG_TYPE_EVENTS))
Eprintf("Event fd %d\n", i);
pfds[i].handler();
}
}
}
}

View File

@ -39,4 +39,9 @@ void EventsInit(void);
void EventsMain(void);
void EventShow(const XEvent * ev);
typedef struct _EventFdDesc EventFdDesc;
typedef void (EventFdHandler) (void);
EventFdDesc *EventFdRegister(int fd, EventFdHandler * handler);
void EventFdUnregister(EventFdDesc * efd);
#endif /* _EVENTS_H_ */

View File

@ -25,6 +25,7 @@
#include "dialog.h"
#include "e16-ecore_hints.h"
#include "emodule.h"
#include "events.h"
#include "ewins.h"
#include "hints.h"
#include "session.h"
@ -54,8 +55,6 @@ static Window new_init_win_ext = None;
#endif
#endif
static int sm_fd = -1;
/* True if we are saving state for a doExit("restart") */
static int restarting = False;
@ -71,6 +70,8 @@ static int restarting = False;
static char *sm_client_id = NULL;
static SmcConn sm_conn = NULL;
static EventFdDesc *sm_efd = NULL;
static void
set_save_props(SmcConn smc_conn, int master_flag)
{
@ -321,24 +322,49 @@ ice_io_error_handler(IceConn connection __UNUSED__)
* exit(1) instead of closing the losing connection. */
}
#endif /* USE_SM */
void
SessionInit(void)
static void
ice_exit(void)
{
SmcCloseConnection(sm_conn, 0, NULL);
sm_conn = NULL;
EventFdUnregister(sm_efd);
}
static void
ice_msgs_process(void)
{
IceProcessMessagesStatus status;
status = IceProcessMessages(ice_conn, NULL, NULL);
if (status == IceProcessMessagesIOError)
{
/* Less of the hope.... E survives */
DialogAlert(_("ERROR!\n" "\n"
"Lost the Session Manager that was there?\n"
"Here here session manager... come here... want a bone?\n"
"Oh come now! Stop sulking! Bugger. Oh well. "
"Will continue without\n" "a session manager.\n" "\n"
"I'll survive somehow.\n" "\n" "\n" "... I hope.\n"));
ice_exit();
}
}
static void
ice_init(void)
{
#if USE_SM
static SmPointer context;
SmcCallbacks callbacks;
#endif
char error_string_ret[4096];
char *client_id;
char style[2];
SmPropValue styleVal;
SmProp styleProp;
SmProp *props[1];
int sm_fd;
if (Mode.wm.window)
if (!getenv("SESSION_MANAGER"))
return;
#if USE_SM
#if 0 /* Unused */
atom_sm_client_id = XInternAtom(disp, "SM_CLIENT_ID", False);
#endif
IceSetIOErrorHandler(ice_io_error_handler);
callbacks.save_yourself.callback = callback_save_yourself;
@ -349,54 +375,64 @@ SessionInit(void)
callbacks.save_yourself.client_data = callbacks.die.client_data =
callbacks.save_complete.client_data =
callbacks.shutdown_cancelled.client_data = (SmPointer) NULL;
if (getenv("SESSION_MANAGER"))
{
char error_string_ret[4096] = "";
char *client_id = NULL;
if (sm_client_id)
client_id = Estrdup(sm_client_id);
sm_conn =
SmcOpenConnection(NULL, &context, SmProtoMajor, SmProtoMinor,
SmcSaveYourselfProcMask | SmcDieProcMask |
SmcSaveCompleteProcMask |
SmcShutdownCancelledProcMask, &callbacks,
client_id, &sm_client_id, 4096, error_string_ret);
if (client_id)
Efree(client_id);
client_id = Estrdup(sm_client_id);
if (error_string_ret[0])
Eprintf("While connecting to session manager: %s.",
error_string_ret);
}
if (sm_conn)
{
char style[2];
SmPropValue styleVal;
SmProp styleProp;
SmProp *props[1];
error_string_ret[0] = '\0';
style[0] = SmRestartIfRunning;
style[1] = 0;
sm_conn =
SmcOpenConnection(NULL, &context, SmProtoMajor, SmProtoMinor,
SmcSaveYourselfProcMask | SmcDieProcMask |
SmcSaveCompleteProcMask |
SmcShutdownCancelledProcMask, &callbacks,
client_id, &sm_client_id, 4096, error_string_ret);
if (client_id)
Efree(client_id);
styleVal.length = 1;
styleVal.value = style;
if (error_string_ret[0])
Eprintf("While connecting to session manager: %s.", error_string_ret);
styleProp.name = (char *)SmRestartStyleHint;
styleProp.type = (char *)SmCARD8;
styleProp.num_vals = 1;
styleProp.vals = &styleVal;
if (!sm_conn)
return;
props[0] = &styleProp;
style[0] = SmRestartIfRunning;
style[1] = 0;
styleVal.length = 1;
styleVal.value = style;
styleProp.name = (char *)SmRestartStyleHint;
styleProp.type = (char *)SmCARD8;
styleProp.num_vals = 1;
styleProp.vals = &styleVal;
props[0] = &styleProp;
ice_conn = SmcGetIceConnection(sm_conn);
sm_fd = IceConnectionNumber(ice_conn);
/* Just in case we are a copy of E created by a doExit("restart") */
SmcSetProperties(sm_conn, 1, props);
fcntl(sm_fd, F_SETFD, fcntl(sm_fd, F_GETFD, 0) | FD_CLOEXEC);
sm_efd = EventFdRegister(sm_fd, ice_msgs_process);
}
ice_conn = SmcGetIceConnection(sm_conn);
sm_fd = IceConnectionNumber(ice_conn);
/* Just in case we are a copy of E created by a doExit("restart") */
SmcSetProperties(sm_conn, 1, props);
fcntl(sm_fd, F_SETFD, fcntl(sm_fd, F_GETFD, 0) | FD_CLOEXEC);
}
#endif /* USE_SM */
void
SessionInit(void)
{
if (Mode.wm.window)
return;
#if 0 /* Unused */
atom_sm_client_id = XInternAtom(disp, "SM_CLIENT_ID", False);
#endif
#if USE_SM
ice_init();
#endif
if (!Conf.session.script)
Conf.session.script = Estrdup("$EROOT/scripts/session.sh");
if (!Conf.session.cmd_reboot)
@ -405,37 +441,6 @@ SessionInit(void)
Conf.session.cmd_halt = Estrdup("poweroff");
}
void
ProcessICEMSGS(void)
{
#if USE_SM
IceProcessMessagesStatus status;
if (sm_fd < 0)
return;
status = IceProcessMessages(ice_conn, NULL, NULL);
if (status == IceProcessMessagesIOError)
{
/* Less of the hope.... E survives */
DialogAlert(_("ERROR!\n" "\n"
"Lost the Session Manager that was there?\n"
"Here here session manager... come here... want a bone?\n"
"Oh come now! Stop sulking! Bugger. Oh well. "
"Will continue without\n" "a session manager.\n" "\n"
"I'll survive somehow.\n" "\n" "\n" "... I hope.\n"));
SmcCloseConnection(sm_conn, 0, NULL);
sm_conn = NULL;
sm_fd = -1;
}
#endif /* USE_SM */
}
int
GetSMfd(void)
{
return sm_fd;
}
void
SessionGetInfo(EWin * ewin)
{
@ -465,21 +470,17 @@ SetSMID(const char *smid)
#endif /* USE_SM */
}
void
static void
SessionSave(int shutdown)
{
if (EDebug(EDBUG_TYPE_SESSION))
Eprintf("SessionSave(%d)\n", shutdown);
/* dont' need anymore */
/* autosave(); */
Real_SaveSnapInfo(0, NULL);
#if USE_SM
if (shutdown && sm_conn)
{
SmcCloseConnection(sm_conn, 0, NULL);
sm_conn = NULL;
sm_fd = -1;
}
ice_exit();
#endif /* USE_SM */
}
@ -504,9 +505,7 @@ doSMExit(int mode, const char *params)
restarting = True;
if (!params)
SessionSave(1);
Real_SaveSnapInfo(0, NULL);
SessionSave(1);
if (mode != EEXIT_THEME && mode != EEXIT_RESTART)
SessionHelper(ESESSION_STOP);

View File

@ -38,11 +38,8 @@
#define ESESSION_STOP 2
void SessionInit(void);
void SessionSave(int shutdown);
void SessionExit(int mode, const char *params);
void SessionHelper(int when);
void ProcessICEMSGS(void);
int GetSMfd(void);
void SetSMID(const char *smid);
void SettingsSession(void);