📄 e32module.cpp
字号:
extern "C" {
static void
ao_cg_dealloc(Ao_callgate_object *op)
{
if ((TUint)RThread().Id() == op->ob_tid) {
delete op->ob_data;
op->ob_data = NULL;
}
Ao_callgate_call_item* p = op->ob_args.next;
while (p) {
Ao_callgate_call_item* tmp = p->next;
Py_XDECREF(p->args);
Py_XDECREF(p->kwargs);
PyMem_Free(p);
p = tmp;
}
PyObject_Del(op);
}
static const PyTypeObject c_Ao_callgate_type = {
PyObject_HEAD_INIT(NULL)
0,
"e32.Ao_callgate",
sizeof(Ao_callgate_object),
0,
/* methods */
(destructor)ao_cg_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as _number*/
0, /* tp_as _sequence*/
0, /* tp_as _mapping*/
0, /* tp_hash */
ao_cg_call, /* tp_call */
};
} /* extern "C" */
/*
*
* Implementation of e32._as_level
*
*/
class CMyAccessor : public CActiveScheduler
{
public:
TInt MyLevel() {return Level();}
};
extern "C" PyObject *
e32__as_level(PyObject* /*self*/, PyObject* /*args*/)
{
TInt l =
STATIC_CAST(CMyAccessor*, CActiveScheduler::Current())->MyLevel();
return Py_BuildValue("i", l);
}
/*
* This does not work on target (!?)
*
* extern "C" double
* e32_clock()
* {
* TTimeIntervalMicroSeconds cpu_time(0.0);
*
* RThread().GetCpuTime(cpu_time);
*
* return ((cpu_time.Int64().GetTReal()) / (1000.0 * 1000.0));
* }
*/
extern "C" double
e32_clock()
{
TInt period = 1000*1000;
HAL::Get(HALData::ESystemTickPeriod, period);
return (((double)(((double)User::TickCount())*
(((double)period)/1000.0)))/1000.0);
}
extern "C" double
e32_UTC_offset()
{
#ifndef EKA2
TLocale loc;
return TInt64(loc.UniversalTimeOffset().Int()).GetTReal();
#else
TLocale loc;
return TReal64(loc.UniversalTimeOffset().Int());
#endif /*EKA2*/
}
extern "C" int
e32_daylight_saving_on()
{
#ifndef EKA2
TLocale loc;
return loc.QueryHomeHasDaylightSavingOn();
#else
RTz tzServer;
CTzId *timeZoneID;
TBool daylightSaving;
User::LeaveIfError(tzServer.Connect());
CleanupClosePushL(tzServer);
timeZoneID = tzServer.GetTimeZoneIdL();
CleanupStack::PushL(timeZoneID);
daylightSaving = tzServer.IsDaylightSavingOnL(*timeZoneID);
CleanupStack::PopAndDestroy(timeZoneID);
CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
return daylightSaving;
#endif
}
extern "C" int
e32_daylight_saving()
{
#ifndef EKA2
TLocale loc;
TDaylightSavingZone dlSZ;
dlSZ = loc.HomeDaylightSavingZone();
if (dlSZ == EDstNone) {
return 0;
}
else {
return 1;
}
#else // EKA2
RTz tzServer;
CTzId *timeZoneID;
User::LeaveIfError(tzServer.Connect());
CleanupClosePushL(tzServer);
timeZoneID = tzServer.GetTimeZoneIdL();
CleanupStack::PushL(timeZoneID);
_LIT(KWinterTime,"20070106:105000.00"); // UTC time to convert (06 January 2007 10:50 hrs).
TTime winterTime(KWinterTime); // Create the time as a TTime object
TTimeIntervalMonths interval(6);
TTime summerTime(winterTime+interval); // Summertime = 6 months from wintertime
tzServer.ConvertToLocalTime(winterTime, *timeZoneID);
tzServer.ConvertToLocalTime(summerTime, *timeZoneID);
CleanupStack::PopAndDestroy(timeZoneID);
CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
TTime compTime(summerTime-interval);
if (winterTime == compTime) {
return 0;
}
else {
return 1;
}
#endif // EKA2
}
extern "C" int
e32_check_stack()
{
#ifndef EKA2
TInt heap_sz, stack_sz;
if (RThread().GetRamSizes(heap_sz, stack_sz) == KErrNone) {
if (((TUint)&heap_sz-(TUint)((RThread().Heap())->Base()-stack_sz)) > 0x800)
return 0;
}
else
return 0;
return -1;
#else
TThreadStackInfo stackInfo;
if (RThread().StackInfo(stackInfo) == KErrNone) {
/* Obtain an approximation of the stack pointer by getting
* the address of a stack-allocated variable, and check if there is
* enough space between that and the stack limit.
* 0x800 is an arbitrary "large enough" number. */
if (((TUint)&stackInfo-(TUint)stackInfo.iLimit) > 0x800)
return 0;
else
return -1;
} else {
/* Querying the stack failed - this basically shouldn't happen in any sane situation.
* The documentation says "KErrGeneral, if the thread doesn't have a user
* mode stack, or it has terminated."
*/
return -1;
}
#endif /*EKA2*/
}
/*
*
* Implementation of e32.is_ui_thread
*
*/
TBool is_main_thread()
{
return ((PYTHON_GLOBALS->main_thread == 0) ||
(PYTHON_GLOBALS->main_thread == PyThread_get_thread_ident()));
}
extern "C" PyObject *
e32_is_ui_thread(PyObject* /*self*/, PyObject* /*args*/)
{
PyObject* rval = (is_main_thread() ? Py_True : Py_False);
Py_INCREF(rval);
return rval;
}
/*
*
* Implementation of e32.in_emulator
*
*/
extern "C" PyObject *
e32_in_emulator(PyObject* /*self*/, PyObject* /*args*/)
{
#ifdef __WINS__
PyObject* rval = Py_True;
#else
PyObject* rval = Py_False;
#endif
Py_INCREF(rval);
return rval;
}
/*
*
* Implementation of e32.set_home_time
*
*/
#ifdef EKA2
void
CheckWriteCapL(){
RThread thread;
if (!thread.HasCapability(ECapabilityWriteDeviceData,
__PLATSEC_DIAGNOSTIC_STRING(
"Process does not have required capability: WriteDeviceData"))){
User::Leave(KErrPermissionDenied);
}
}
#endif
extern "C" PyObject *
e32_set_home_time(PyObject* /*self*/, PyObject* args)
{
TReal time = 0;
if (!PyArg_ParseTuple(args, "d", &time)){
return NULL;
}
TTime conv_time;
pythonRealAsTTime(time, conv_time);
TInt error = KErrNone;
#ifdef EKA2
TRAP(error,CheckWriteCapL());
if (error != KErrNone){
return SPyErr_SetFromSymbianOSErr(error);
}
#endif
error = User::SetHomeTime(conv_time);
RETURN_ERROR_OR_PYNONE(error);
}
/*
*
* Implementation of e32.reset_inactivity
*
*/
extern "C" PyObject *
e32_reset_inactivity(PyObject* /*self*/, PyObject* /*args*/)
{
User::ResetInactivityTime();
Py_INCREF(Py_None);
return Py_None;
}
/*
*
* Implementation of e32.inactivity
*
*/
extern "C" PyObject *
e32_inactivity(PyObject* /*self*/, PyObject* /*args*/)
{
return Py_BuildValue("i", User::InactivityTime().Int());
}
#ifdef USE_GLOBAL_DATA_HACK
extern "C" PyObject *
e32_globcnt(PyObject* /*self*/, PyObject* /*args*/)
{
PyObject* rval = Py_BuildValue("i", *(PYTHON_GLOBALS->globptr));
return rval;
}
#endif
#ifdef WITH_DLC
extern "C" PyObject *
e32_dlc_stats(PyObject* /*self*/, PyObject* /*args*/)
{
int allocated_blocks, free_blocks;
dlc_stats(&allocated_blocks, &free_blocks);
return Py_BuildValue("ii", allocated_blocks, free_blocks);
}
#endif
/*
*
* Implementation of e32._uidcrc_app
*
*/
class TMyCheckedAppUid : public TCheckedUid
{
public:
TMyCheckedAppUid(TUint uid):
TCheckedUid(TUidType(TUid::Uid(KDynamicLibraryUidValue),
TUid::Uid(KAppUidValue16),
TUid::Uid(uid))) {;}
TUint CheckSum() {return Check();}
};
extern "C" PyObject *
e32_uidcrc_app(PyObject* /*self*/, PyObject *args)
{
TUint uid;
if (!PyArg_ParseTuple(args, "i", &uid))
return NULL;
TMyCheckedAppUid chk(uid);
return Py_BuildValue("l", chk.CheckSum());
}
static int call_stdo(const char *buf, int n)
{
PyObject* args = Py_BuildValue("(N)",
PyUnicode_Decode(buf, n, NULL, NULL));
PyObject* rval =
PyEval_CallObject(SPyGetGlobalString("e32_stdo"), args);
Py_XDECREF(args);
Py_XDECREF(rval);
return n;
}
static int print_stdo(const char *buf, int n)
{
RFs rfs;
if (rfs.Connect() == KErrNone) {
RFile f;
SPy_Python_globals* pg = PYTHON_GLOBALS;
TPtrC fn((TUint16*)pg->stdo_buf, pg->stdo_buf_len);
TInt error = f.Open(rfs, fn, EFileWrite);
if (error == KErrNotFound)
error = f.Create(rfs, fn, EFileWrite);
TInt dummy;
if ((error == KErrNone) && (f.Seek(ESeekEnd, dummy) == KErrNone))
f.Write(TPtrC8((const TUint8*)buf, n));
f.Close();
rfs.Close();
}
return n;
}
extern "C" PyObject *
e32_stdo(PyObject* /*self*/, PyObject* args)
{
PyObject* c;
if (!PyArg_ParseTuple(args, "O", &c))
return NULL;
SPy_Python_globals* pg = PYTHON_GLOBALS;
if (PyCallable_Check(c)) {
if (SPyAddGlobalString("e32_stdo", c))
return NULL;
((CSPyInterpreter*)pg->interpreter)->iStdO = &call_stdo;
}
else if (PyUnicode_Check(c)) {
TPtr buf((TUint16*)pg->stdo_buf, 0x100);
buf.Copy(PyUnicode_AsUnicode(c), PyUnicode_GetSize(c));
pg->stdo_buf_len = PyUnicode_GetSize(c);
((CSPyInterpreter*)pg->interpreter)->iStdO = &print_stdo;
}
else if (c == Py_None) {
((CSPyInterpreter*)pg->interpreter)->iStdO = NULL;
}
else {
PyErr_SetString(PyExc_TypeError, "callable or unicode expected");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
#ifndef EKA2
extern "C" PyObject *
e32_mem_info(PyObject* /*self*/)
{
TInt error;
TInt heap_sz, stack_sz, current_heap_usage,
max_stack_usage, current_pyheap_usage;
error = RThread().GetRamSizes(heap_sz, stack_sz);
if (error == KErrNone) {
(RThread().Heap())->AllocSize(current_heap_usage);
((CSPyInterpreter*)PYTHON_GLOBALS->interpreter)->
iPyheap->AllocSize(current_pyheap_usage);
}
if (error == KErrNone) {
TUint8* pbase = (RThread().Heap())->Base();
TUint8* pstacklast = pbase - stack_sz;
while (*pstacklast == 0x29)
pstacklast++;
max_stack_usage = pbase - pstacklast;
}
if (error != KErrNone)
return SPyErr_SetFromSymbianOSErr(error);
else
return Py_BuildValue("(iiiii)", heap_sz, stack_sz,
current_heap_usage, max_stack_usage,
current_pyheap_usage);
}
#endif /*EKA2*/
extern "C" PyObject *
e32_strerror(PyObject* /*self*/, PyObject* args)
{
int errcode;
if (!PyArg_ParseTuple(args, "i", &errcode))
return NULL;
return SPyErr_SymbianOSErrAsString(errcode);
}
extern "C" {
static const PyMethodDef e32_methods[] = {
{"Ao_lock", (PyCFunction)new_e32_ao_object, METH_NOARGS, NULL},
{"Ao_timer", (PyCFunction)new_e32_ao_timer_object, METH_NOARGS, NULL},
{"ao_yield", (PyCFunction)e32_ao_yield, METH_NOARGS, NULL},
{"ao_sleep", (PyCFunction)e32_ao_sleep, METH_VARARGS, NULL},
{"ao_callgate", (PyCFunction)e32_ao_callgate, METH_VARARGS, NULL},
{"_as_level", (PyCFunction)e32__as_level, METH_NOARGS, NULL},
{"start_server", (PyCFunction)e32_start_server, METH_VARARGS, NULL},
{"start_exe", (PyCFunction)e32_start_exe, METH_VARARGS, NULL},
{"drive_list", (PyCFunction)e32_drive_list, METH_NOARGS, NULL},
{"file_copy", (PyCFunction)e32_file_copy, METH_VARARGS, NULL},
{"is_ui_thread", (PyCFunction)e32_is_ui_thread, METH_NOARGS, NULL},
{"in_emulator", (PyCFunction)e32_in_emulator, METH_NOARGS, NULL},
{"set_home_time", (PyCFunction)e32_set_home_time, METH_VARARGS, NULL},
{"reset_inactivity", (PyCFunction)e32_reset_inactivity, METH_NOARGS, NULL},
{"inactivity", (PyCFunction)e32_inactivity, METH_NOARGS, NULL},
{"_uidcrc_app", (PyCFunction)e32_uidcrc_app, METH_VARARGS, NULL},
{"_stdo", (PyCFunction)e32_stdo, METH_VARARGS, NULL},
#ifndef EKA2
{"_mem_info", (PyCFunction)e32_mem_info, METH_NOARGS, NULL},
#endif /*EKA2*/
{"strerror", (PyCFunction)e32_strerror, METH_VARARGS, NULL},
#ifdef USE_GLOBAL_DATA_HACK
{"_globcnt", (PyCFunction)e32_globcnt, METH_NOARGS, NULL},
#endif
#ifdef WITH_DLC
{"_dlc_stats", (PyCFunction)e32_dlc_stats, METH_NOARGS, NULL},
#endif
{NULL, NULL} /* sentinel */
};
DL_EXPORT(void) inite32(void)
{
PyObject *m, *d;
Ao_lock_type = c_Ao_lock_type;
Ao_lock_type.ob_type = &PyType_Type;
Ao_timer_type = c_Ao_timer_type;
Ao_timer_type.ob_type = &PyType_Type;
Ao_callgate_type = c_Ao_callgate_type;
Ao_callgate_type.ob_type = &PyType_Type;
m = Py_InitModule("e32", (PyMethodDef*)e32_methods);
d = PyModule_GetDict(m);
PyDict_SetItemString(d,"pys60_version_info",
Py_BuildValue("(iiisi)", PYS60_VERSION_MAJOR, PYS60_VERSION_MINOR, PYS60_VERSION_MICRO,
PYS60_VERSION_TAG, PYS60_VERSION_SERIAL));
PyDict_SetItemString(d,"pys60_version", Py_BuildValue("s", PYS60_VERSION_STRING));
PyDict_SetItemString(d,"s60_version_info", Py_BuildValue("(ii)",
#if SERIES60_VERSION==12
1, 2
#endif
#if SERIES60_VERSION==20
2, 0
#endif
#if SERIES60_VERSION==26
2, 6
#endif
#if SERIES60_VERSION==28
2, 8
#endif
#if SERIES60_VERSION==30
3, 0
#endif
));
}
} /* extern "C" */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -