summaryrefslogtreecommitdiff
path: root/data/uicaboodle.py/uicaboodle.m
blob: aa219ec52f871a914db374b4ed0a500d06aa2ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <Python.h>
#include "pyobjc-api.h"
#import <UIKit/UIKit.h>

#define _trace() \
    fprintf(stderr, "_trace():%s(%d):%s\n", __FILE__, __LINE__, __FUNCTION__)

#define _assert(test) \
    do if (!(test)) { \
        fprintf(stderr, "_assert(%s):%s(%d):%s\n", #test, __FILE__, __LINE__, __FUNCTION__); \
        exit(-1); \
    } while (false)

static PyObject*  objc_UIApplicationMain(
    PyObject *self __attribute__((__unused__)),
    PyObject *args
) {
    int argc;
    char **argv;
    size_t i;
    int res = -1;

    PyObject *arga;
    PyObject *_class;

    _assert(PyObjC_API != NULL);
    _assert(args != NULL);

    /* XXX: correctly parse __OBJC2__ condition */
    if (!PyArg_ParseTuple(args, "OO", &arga, &_class)) {
	PyErr_SetString(PyExc_TypeError, "Invalid Arguments");
        return NULL;
    }

    _assert(arga != NULL);
    _assert(_class != NULL);

    argc = PySequence_Size(arga);
    argv = calloc(argc + 1, sizeof(char *));

    if (argv == NULL) {
	PyErr_SetString(PyExc_MemoryError, "Out of memory");
	return NULL;
    }

    for (i = 0; i != argc; ++i) {
        PyObject *arg = PySequence_GetItem(arga, i);
        if (arg == NULL) {
            PyErr_SetString(PyExc_TypeError, "UIApplicationMain: null argument");
            goto error;
        }

        if (!PyString_Check(arg)) {
            PyErr_SetString(PyExc_TypeError, "UIApplicationMain: non-string argument");
            goto error;
        }

        argv[i] = strdup(PyString_AsString(arg));
        if (argv[i] == NULL) {
            PyErr_SetString(PyExc_MemoryError, "Out of memory");
            goto error;
        }
    }

    PyObjC_DURING
#ifdef __OBJC2__
        res = UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:class_getName(PyObjC_API->cls_get_class(_class))]);
#else
        res = UIApplicationMain(argc, argv, PyObjC_API->cls_get_class(_class));
#endif
    PyObjC_HANDLER
        PyObjCErr_FromObjC(localException);
    PyObjC_ENDHANDLER

  error:
    for (i = 0; i != argc; ++i)
        free(argv[i]);
    free(argv);

    if (res == -1 && PyErr_Occurred())
        return NULL;
    return PyInt_FromLong(res);
}

static PyMethodDef mod_methods[] = {{
    "UIApplicationMain",
    /*(PyCFunction)*/ objc_UIApplicationMain,
    METH_VARARGS|METH_KEYWORDS,
#ifdef __OBJC2__
    "int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);"
#else
    "int UIApplicationMain(int argc, char *argv[], Class _class);"
#endif
}, {
    NULL, NULL, 0, NULL
}};

void init_uicaboodle(void) {
    PyObject *module = Py_InitModule4("_uicaboodle", mod_methods, "", NULL, PYTHON_API_VERSION);
    PyObjC_ImportAPI(module);
}