Symbian3/PDK/Source/GUID-5329C067-60B8-4E0E-A2B3-9423B75E189D.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 11 Mar 2010 18:02:22 +0000
changeset 3 46218c8b8afa
parent 1 25a17d01db0c
child 5 f345bda72bc4
permissions -rw-r--r--
week 10 bug fix submission (SF PDK version): Bug 1892, Bug 1897, Bug 1319. Also 3 or 4 documents were found to contain code blocks with SFL, which has been fixed. Partial fix for broken links, links to Forum Nokia, and the 'Symbian platform' terminology issues.

<?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>