aboutsummaryrefslogtreecommitdiff
path: root/cyclone/shadow/maxmode.c
blob: 9cd1055770202db72571a5283bd31d65f8fe430b (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
/* Copyright (c) 2004-2005 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <stdio.h>
#include <string.h>
#include "m_pd.h"
#include "common/loud.h"
#include "common/fitter.h"
#include "common/port.h"
#include "hammer/file.h"
#include "unstable/fragile.h"
#include "unstable/loader.h"
#include "../build_counter"

typedef struct _maxmode
{
    t_object       x_ob;
    t_symbol      *x_dir;
    t_symbol      *x_canvasdir;
    t_hammerfile  *x_filehandle;
} t_maxmode;

static t_class *maxmode_class;
static int maxmode_dummiesndx;
static int maxmode_lastndx;
static t_pd *maxmode_dproxy = 0;
static int maxmode_withbanner = 0;

static void maxmode_readhook(t_pd *z, t_symbol *fn, int ac, t_atom *av)
{
    import_max(fn->s_name, "");
}

static void maxmode_doimport(t_maxmode *x, t_symbol *fn, t_symbol *dir)
{
    if (!dir || dir == &s_)
	dir = x->x_dir;
    if (fn && fn != &s_)
	import_max(fn->s_name, (dir && dir != &s_) ? dir->s_name : "");
    else
	hammerpanel_open(x->x_filehandle, dir);
}

static void maxmode_click(t_maxmode *x, t_floatarg xpos, t_floatarg ypos,
			  t_floatarg shift, t_floatarg ctrl, t_floatarg alt)
{
    maxmode_doimport(x, 0, 0);
}

static void maxmode_import(t_maxmode *x, t_symbol *fn)
{
    maxmode_doimport(x, fn, 0);
}

static void maxmode_cd(t_maxmode *x, t_symbol *dir)
{
    /* LATER hammerfile interface for relative jumps, etc. */
    x->x_dir = (dir && dir != &s_ ? dir : x->x_canvasdir);
}

static void maxmode_pwd(t_maxmode *x)
{
    outlet_symbol(((t_object *)x)->ob_outlet, x->x_dir);
}

static void maxmode_bang(t_maxmode *x)
{
    if (maxmode_dproxy)
	pd_bang(maxmode_dproxy);
    else
	post("no replacement abstractions");
    if (maxmode_lastndx > maxmode_dummiesndx)
	fragile_class_printnames("dummies are: ",
				 maxmode_dummiesndx, maxmode_lastndx);
    else
	post("no dummies");
}

static void maxmode_free(t_maxmode *x)
{
    hammerfile_free(x->x_filehandle);
}

static void *maxmode_new(t_symbol *s, int ac, t_atom *av)
{
    t_maxmode *x = (t_maxmode *)pd_new(maxmode_class);
    if (maxmode_withbanner && !ac)
    {
	post("this is maxmode %s, %s %s build",
	     CYCLONE_VERSION, loud_ordinal(CYCLONE_BUILD), CYCLONE_RELEASE);
	loud_warning(0, "maxmode",
 "creating maxmode object without loading cyclone components");
	maxmode_withbanner = 0;
    }
    if (!fittermax_get())
    {
	int selective = 0;
	if (ac)
	{
	    while (ac--) if (av->a_type == A_SYMBOL)
	    {
		/* FIXME register into fitter for per-patch, selective
		   compatibility control */
		av++;
	    }
	}
	if (!selective)
	    fittermax_set();
    }
    x->x_filehandle = hammerfile_new((t_pd *)x, 0, maxmode_readhook, 0, 0);
    x->x_canvasdir = canvas_getdir(x->x_filehandle->f_canvas);
    x->x_dir = x->x_canvasdir;
    outlet_new((t_object *)x, &s_symbol);
    return (x);
}

void maxmode_setup(void)
{
    int dresult = LOADER_OK;
    if (zgetfn(&pd_objectmaker, gensym("maxmode")))
    {
	loud_error(0, "maxmode is already loaded");
	return;
    }
    maxmode_class = class_new(gensym("maxmode"),
			      (t_newmethod)maxmode_new,
			      (t_method)maxmode_free,
			      sizeof(t_maxmode), 0, A_GIMME, 0);
    class_addbang(maxmode_class, maxmode_bang);
    class_addmethod(maxmode_class, (t_method)maxmode_cd,
		    gensym("cd"), A_DEFSYM, 0);
    class_addmethod(maxmode_class, (t_method)maxmode_pwd,
		    gensym("pwd"), 0);
    class_addmethod(maxmode_class, (t_method)maxmode_import,
		    gensym("import"), A_DEFSYM, 0);
    class_addmethod(maxmode_class, (t_method)maxmode_click,
		    gensym("click"),
		    A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
    hammerfile_setup(maxmode_class, 0);

    if (canvas_getcurrent())
    {
	fitter_setup(0, 0, 0);
	if (!zgetfn(&pd_objectmaker, gensym("cyclone")))
	    /* cycloneless maxmode -- banner is posted by the oldest
	       maxmode object with no creation arguments */
	    maxmode_withbanner = 1;
    }
    else
    {
	fittermax_set();
	if (zgetfn(&pd_objectmaker, gensym("cyclone")))
	    loud_warning(0, "maxmode", "cyclone is already loaded");
	else
	{
	    if (unstable_load_lib("", "cyclone") == LOADER_NOFILE)
		loud_error(0, "cyclone library is missing");
	    else if (!zgetfn(&pd_objectmaker, gensym("cyclone")))
		loud_error(0, "miXed/Pd version mismatch");
	}
    }
    maxmode_dummiesndx = fragile_class_count();
    if (zgetfn(&pd_objectmaker, gensym("dummies")))
	loud_warning(0, "maxmode", "dummies are already loaded");
    else
	dresult = unstable_load_lib("", "dummies");
    maxmode_lastndx = fragile_class_count() - 1;
    if (dresult == LOADER_NOFILE)
	loud_warning(0, "maxmode", "dummies not found");
    else
    {
	t_symbol *s = gensym("_cc.dummies");
	if (s->s_thing && !s->s_next
	    && !strcmp(class_getname(*s->s_thing), "_cc.dummies"))
	    maxmode_dproxy = s->s_thing;
	else
	    loudbug_bug("maxmode_setup");  /* FIXME */
    }
}