Symbian3/SDK/Source/GUID-5329C067-60B8-4E0E-A2B3-9423B75E189D.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-5329C067-60B8-4E0E-A2B3-9423B75E189D" xml:lang="en"><title>Getting
GLib events in Symbian UI applications</title><shortdesc>Symbian UI applications run an event loop that is based on <apiname>CActiveScheduler</apiname>.
The active scheduler is started by the UI framework immediately after the
UI has been created (that is after <codeph>ConstructL()</codeph> of <apiname>CEikAppUi</apiname>-derived
class).</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>To receive events from GLib event sources, an active object can be added
to the active scheduler especially for this purpose. The active object runs
one iteration of the glib event loop every time it gets scheduled.</p>
<note>The scheduling logic in the sample code provided below is based on a
simple timer; more complex scheduling logic can be implemented depending on
the requirement of the application.</note>
<codeblock xml:space="preserve">/********* GlibEventHandler.h *********************/

class CGlibEventHandler: public CActive
{
public:
	static CGlibEventHandler* NewL();
	~CGlibEventHandler();
       void RunL();
       void DoCancel();
       void Start();
       void Stop();
private:
       CGlibEventHandler();
       void ConstructL();	
       RTimer iTimer;
};

/********* GlibEventHandler.h *********************/


/********* GlibEventHandler.cpp *******************/

CGlibEventHandler* CGlibEventHandler::NewL()
{
       CGlibEventHandler* self = new(ELeave) CGlibEventHandler();
       CleanupStack::PushL(self);
       self-&gt;ConstructL();
       CleanupStack::Pop();
       return self;
}

CGlibEventHandler::CGlibEventHandler():CActive(EPriorityStandard)
{
}
	
void CGlibEventHandler::ConstructL()
{
	User::LeaveIfError(iTimer.CreateLocal());
	CActiveScheduler::Add(this);	
}

CGlibEventHandler::~CGlibEventHandler()
{
	Cancel();
	iTimer.Close();		
}

void CGlibEventHandler::Start()
{
	iTimer.After(iStatus, TTimeIntervalMicroSeconds32(1000));
	SetActive();	
}
	
void CGlibEventHandler::Stop()
{
	Cancel();
}
	
void CGlibEventHandler::RunL()
{
	g_main_context_iteration(NULL, FALSE);
	iTimer.After(iStatus, TTimeIntervalMicroSeconds32(1000));
	SetActive();	
}
	
void CGlibEventHandler::DoCancel()
{
	iTimer.Cancel();	
}

/********* GlibEventHandler.cpp *******************/
</codeblock>
<p>The following is done in the application:</p>
<codeblock xml:space="preserve">GMainLoop* mainloop = g_main_loop_new(NULL, FALSE);
CGlibEventHandler* glibeventhandler = NULL;
glibeventhandler = CGlibEventHandler::NewL();
glibeventhandler-&gt;Start();
</codeblock>
</conbody></concept>