haiku/src/servers/app/MessageLooper.cpp
Axel Dörfler 770c05d6cc The Desktop class now gets its own message processing loop: moved application
creation/deletion (and management) over to that class.
ServerApp now gets a desktop pointer, and no longer uses gDesktop.
Converted private MessageLooper::_MessagePort() to a public method MessagePort()
so that the looper can be addressed from elsewhere without using PostMessage().
Added a real basic message loop to MessageLooper::_MessageLoop().
BApplication now only asks the app_server to get its desktop object which should
now be used for everything that's not in the realm of the application.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13824 a95241bf-73f2-0310-859d-f6bbb57e9c96
2005-07-25 21:08:34 +00:00

151 lines
2.2 KiB
C++

/*
* Copyright 2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, axeld@pinc-software.de
*/
#include "MessageLooper.h"
#include <Autolock.h>
#include <stdio.h>
MessageLooper::MessageLooper(const char* name)
: BLocker(name),
fQuitting(false)
{
}
MessageLooper::~MessageLooper()
{
}
bool
MessageLooper::Run()
{
BAutolock locker(this);
fQuitting = false;
char name[B_OS_NAME_LENGTH];
_GetLooperName(name, sizeof(name));
// Spawn our message-monitoring thread
fThread = spawn_thread(_message_thread, name, B_NORMAL_PRIORITY, this);
if (fThread < B_OK) {
fQuitting = true;
return false;
}
if (resume_thread(fThread) != B_OK) {
fQuitting = true;
kill_thread(fThread);
fThread = -1;
return false;
}
return true;
}
void
MessageLooper::Quit()
{
fQuitting = true;
_PrepareQuit();
if (fThread < B_OK) {
// thread has not been started yet
delete this;
return;
}
if (fThread == find_thread(NULL)) {
// called from our message looper
delete this;
exit_thread(0);
} else {
// called from a different thread
PostMessage(kMsgQuitLooper);
}
}
/*!
\brief Send a message to the looper without any attachments
\param code ID code of the message to post
*/
void
MessageLooper::PostMessage(int32 code)
{
BPrivate::LinkSender link(MessagePort());
link.StartMessage(code);
link.Flush();
}
void
MessageLooper::_PrepareQuit()
{
// to be implemented by subclasses
}
void
MessageLooper::_GetLooperName(char* name, size_t length)
{
strcpy(name, "unnamed looper");
}
void
MessageLooper::_DispatchMessage(int32 code, BPrivate::LinkReceiver &link)
{
}
void
MessageLooper::_MessageLooper()
{
BPrivate::LinkReceiver& receiver = fLink.Receiver();
while (true) {
int32 code;
status_t status = receiver.GetNextMessage(code);
if (status < B_OK) {
// that shouldn't happen, it's our port
printf("Someone deleted our message port!\n");
break;
}
Lock();
if (code == kMsgQuitLooper) {
Quit();
} else
_DispatchMessage(code, receiver);
Unlock();
}
}
/*!
\brief Message-dispatching loop starter
*/
/*static*/
int32
MessageLooper::_message_thread(void* _looper)
{
MessageLooper* looper = (MessageLooper*)_looper;
looper->_MessageLooper();
return 0;
}