aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/main.h
blob: 621c9eff006031bf7ef10a58c4841a1fa22e7de6 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* 

py/pyext - python script object for PD and MaxMSP

Copyright (c)2002-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#ifndef __MAIN_H
#define __MAIN_H

#include "pyprefix.h"
#include "pysymbol.h"

#if FLEXT_OS == FLEXT_LINUX || FLEXT_OS == FLEXT_IRIX
#include <unistd.h>
#endif

#define PY__VERSION "0.2.0pre"


#define PYEXT_MODULE "pyext" // name for module
#define PYEXT_CLASS "_class"  // name for base class

#define PY_STOP_WAIT 1000  // ms
#define PY_STOP_TICK 10  // ms


class Fifo
{
protected:
    struct FifoEl {
        PyObject *fun;
        PyObject *args;
        FifoEl *nxt;
    };
public:
    Fifo(): head(NULL),tail(NULL) {}
    ~Fifo();

    bool Push(PyObject *f,PyObject *a);
    bool Pop(PyObject *&f,PyObject *&a);
    
protected:
    FifoEl *head,*tail;
};


class py:
	public flext_base
{
	FLEXT_HEADER_S(py,flext_base,Setup)

public:
	py();
	~py();
	static void lib_setup();

	static PyObject *MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,int inlet = -1,bool withself = false);
	static AtomList *GetPyArgs(PyObject *pValue,PyObject **self = NULL);

protected:

    void m__dir(PyObject *obj);
	void m__doc(PyObject *obj);

    void m_dir() { m__dir(module); }
    void mg_dir(AtomList &lst) { m__dir(module); }
    void m_doc() { m__doc(dict); }

	PyObject *module,*dict; // inherited user class module and associated dictionary

	static const char *py_doc;

    void GetDir(PyObject *obj,AtomList &lst);

	void GetModulePath(const char *mod,char *dir,int len);
	void AddToPath(const char *dir);
	void SetArgs(int argc,const t_atom *argv);
	void ImportModule(const char *name);
	void UnimportModule();
	void ReloadModule();

	void Register(const char *reg);
	void Unregister(const char *reg);
	void Reregister(const char *reg);
	virtual void Reload() = 0;

    void Respond(bool b);

	static bool IsAnything(const t_symbol *s) { return s && s != sym_float && s != sym_int && s != sym_symbol && s != sym_list && s != sym_pointer; }

	enum retval { nothing,atom,sequ };

	// --- module stuff -----

	static PyObject *module_obj,*module_dict;
	static PyMethodDef func_tbl[];

	static PyObject *py__doc__(PyObject *,PyObject *args);
	static PyObject *py_send(PyObject *,PyObject *args);
#ifdef FLEXT_THREADS
	static PyObject *py_priority(PyObject *,PyObject *args);
#endif

	static PyObject *py_samplerate(PyObject *,PyObject *args);
	static PyObject *py_blocksize(PyObject *,PyObject *args);

#if FLEXT_SYS == FLEXT_SYS_PD
	static PyObject *py_getvalue(PyObject *,PyObject *args);
	static PyObject *py_setvalue(PyObject *,PyObject *args);
#endif

	// ----thread stuff ------------

	virtual void m_stop(int argc,const t_atom *argv);

	bool shouldexit,respond;
	int thrcount;
	int stoptick;
    Timer stoptmr;
    int detach;

	void tick(void *);
    
    bool gencall(PyObject *fun,PyObject *args);
    virtual bool callpy(PyObject *fun,PyObject *args) = 0;

#if FLEXT_SYS == FLEXT_SYS_MAX
    static short patcher_myvol(t_patcher *x);
#endif

    static void collect();

private:

	void work_wrapper(void *data); 

#ifdef FLEXT_THREADS
    bool qucall(PyObject *fun,PyObject *args);
    void threadworker();
    Fifo qufifo;
    ThrCond qucond;

    static PyThreadState *FindThreadState();
    static void FreeThreadState();

	FLEXT_THREAD_X(work_wrapper)
#else
	FLEXT_CALLBACK_X(work_wrapper)
#endif

public:

#ifdef FLEXT_THREADS
	ThrMutex mutex;
	inline void Lock() { mutex.Unlock(); }
	inline void Unlock() { mutex.Unlock(); }

	inline PyThreadState *PyLock() 
    { 
        PyEval_AcquireLock();
	    return PyThreadState_Swap(FindThreadState());
    }

	inline void PyUnlock(PyThreadState *st) 
    {
        PyThreadState_Swap(st);
        PyEval_ReleaseLock();
    }
#else
	inline void Lock() {}
	inline void Unlock() {}

	inline PyThreadState *PyLock() { return NULL; }
	inline void PyUnlock(PyThreadState *st) {}
#endif

	static PyObject* StdOut_Write(PyObject* Self, PyObject* Args);

protected:

    virtual void m_click();

	static void Setup(t_classid c);

	// callbacks
	FLEXT_ATTRVAR_I(detach)
	FLEXT_ATTRVAR_B(respond)
	FLEXT_CALLBACK_V(m_stop)
	FLEXT_CALLBACK(m_dir)
	FLEXT_CALLGET_V(mg_dir)
	FLEXT_CALLBACK(m_doc)
    FLEXT_CALLBACK_T(tick)

#ifdef FLEXT_THREADS
    FLEXT_THREAD(threadworker)
#endif
};

#endif