summaryrefslogtreecommitdiff
path: root/frontends/amiga/plugin_hack.c
blob: 5d7ec19c14dfc2d6f80759c4e350eb8dc4699ce6 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright 2011 Chris Young <chris@unsatisfactorysoftware.co.uk>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Plugin=>external program handler (implementation)
*/

#include "amiga/os3support.h"

#include <stdlib.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/utility.h>

#include "utils/log.h"
#include "utils/messages.h"
#include "netsurf/plotters.h"
#include "netsurf/content.h"
#include "amiga/filetype.h"
#include "amiga/plugin_hack.h"
#include "content/content_protected.h"
#include "content/llcache.h"


typedef struct amiga_plugin_hack_content {
	struct content base;
} amiga_plugin_hack_content;

static nserror amiga_plugin_hack_create(const content_handler *handler,
		lwc_string *imime_type, const struct http_parameter *params,
		llcache_handle *llcache, const char *fallback_charset,
		bool quirks, struct content **c);
static bool amiga_plugin_hack_convert(struct content *c);
static void amiga_plugin_hack_reformat(struct content *c, int width, int height);
static void amiga_plugin_hack_destroy(struct content *c);
static bool amiga_plugin_hack_redraw(struct content *c,
		struct content_redraw_data *data, const struct rect *clip,
		const struct redraw_context *ctx);
static void amiga_plugin_hack_open(struct content *c, struct browser_window *bw,
		struct content *page, struct object_params *params);
static void amiga_plugin_hack_close(struct content *c);
static nserror amiga_plugin_hack_clone(const struct content *old, struct content **newc);
static content_type amiga_plugin_hack_content_type(void);

static const content_handler amiga_plugin_hack_content_handler = {
	.create = amiga_plugin_hack_create,
	.data_complete = amiga_plugin_hack_convert,
	.reformat = amiga_plugin_hack_reformat,
	.destroy = amiga_plugin_hack_destroy,
	.redraw = amiga_plugin_hack_redraw,
	.open = amiga_plugin_hack_open,
	.close = amiga_plugin_hack_close,
	.clone = amiga_plugin_hack_clone,
	.type = amiga_plugin_hack_content_type,
	.no_share = false,
};

nserror amiga_plugin_hack_init(void)
{
	struct Node *node = NULL;
	lwc_string *type;
	nserror error;

	do {
		node = ami_mime_has_cmd(&type, node);

		if(node)
		{
			NSLOG(netsurf, INFO, "plugin_hack registered %s",
			      lwc_string_data(type));

			error = content_factory_register_handler(
				lwc_string_data(type), 
				&amiga_plugin_hack_content_handler);

			if (error != NSERROR_OK)
				return error;
		}

	}while (node != NULL);

	return NSERROR_OK;
}

nserror amiga_plugin_hack_create(const content_handler *handler,
		lwc_string *imime_type, const struct http_parameter *params,
		llcache_handle *llcache, const char *fallback_charset,
		bool quirks, struct content **c)
{
	amiga_plugin_hack_content *plugin;
	nserror error;

	plugin = calloc(1, sizeof(amiga_plugin_hack_content));
	if (plugin == NULL)
		return NSERROR_NOMEM;

	error = content__init(&plugin->base, handler, imime_type, params,
			llcache, fallback_charset, quirks);
	if (error != NSERROR_OK) {
		free(plugin);
		return error;
	}

	*c = (struct content *) plugin;

	return NSERROR_OK;
}

bool amiga_plugin_hack_convert(struct content *c)
{
	NSLOG(netsurf, INFO, "amiga_plugin_hack_convert");

	content_set_ready(c);
	content_set_done(c);

	content_set_status(c, "");

	return true;
}

void amiga_plugin_hack_destroy(struct content *c)
{
	amiga_plugin_hack_content *plugin = (amiga_plugin_hack_content *) c;

	NSLOG(netsurf, INFO, "amiga_plugin_hack_destroy %p", plugin);

	return;
}

bool amiga_plugin_hack_redraw(struct content *c,
		struct content_redraw_data *data, const struct rect *clip,
		const struct redraw_context *ctx)
{
	plot_style_t pstyle = {
		.fill_type = PLOT_OP_TYPE_SOLID,
		.fill_colour = 0xffffff,
		.stroke_colour = 0x000000,
		.stroke_width = plot_style_int_to_fixed(1),
	};
	struct rect rect;
	nserror res;

	NSLOG(netsurf, INFO, "amiga_plugin_hack_redraw");

	rect.x0 = data->x;
	rect.y0 = data->y;
	rect.x1 = data->x + data->width;
	rect.y1 = data->y + data->height;

	ctx->plot->rectangle(ctx, &pstyle, &rect);

	res = ctx->plot->text(ctx,
			      plot_style_font,
			      data->x, data->y+20,
			      lwc_string_data(content__get_mime_type(c)),
			      lwc_string_length(content__get_mime_type(c)));
	if (res != NSERROR_OK) {
		return false;
	}
	return true;
}

/**
 * Handle a window containing a CONTENT_PLUGIN being opened.
 *
 * \param  c       content that has been opened
 * \param  bw      browser window containing the content
 * \param  page    content of type CONTENT_HTML containing c, or 0 if not an
 *                 object within a page
 * \param  params  object parameters, or 0 if not an object
 */
void amiga_plugin_hack_open(struct content *c, struct browser_window *bw,
	struct content *page, struct object_params *params)
{
	NSLOG(netsurf, INFO, "amiga_plugin_hack_open %s",
	      nsurl_access(content_get_url(c)));

	if(c)
	{
		/* TODO: Do we need valid dimensions at this point? */
		c->width = 0;
		c->height = 0;
	}

	return;
}

void amiga_plugin_hack_close(struct content *c)
{
	NSLOG(netsurf, INFO, "amiga_plugin_hack_close");
	return;
}

void amiga_plugin_hack_reformat(struct content *c, int width, int height)
{
	NSLOG(netsurf, INFO, "amiga_plugin_hack_reformat");

	c->width = width;
	c->height = height;

	return;
}

nserror amiga_plugin_hack_clone(const struct content *old, struct content **newc)
{
	amiga_plugin_hack_content *plugin;
	nserror error;

	NSLOG(netsurf, INFO, "amiga_plugin_hack_clone");

	plugin = calloc(1, sizeof(amiga_plugin_hack_content));
	if (plugin == NULL)
		return NSERROR_NOMEM;

	error = content__clone(old, &plugin->base);
	if (error != NSERROR_OK) {
		content_destroy(&plugin->base);
		return error;
	}

	/* We "clone" the old content by replaying conversion */
	if (old->status == CONTENT_STATUS_READY || 
			old->status == CONTENT_STATUS_DONE) {
		if (amiga_plugin_hack_convert(&plugin->base) == false) {
			content_destroy(&plugin->base);
			return NSERROR_CLONE_FAILED;
		}
	}

	*newc = (struct content *) plugin;

	return NSERROR_OK;
}

content_type amiga_plugin_hack_content_type(void)
{
	return CONTENT_PLUGIN;
}

void amiga_plugin_hack_execute(struct hlcache_handle *c)
{
	lwc_string *plugincmd;
	char *full_cmd;
	BPTR in, out;

	if(c == NULL) return;

	plugincmd = ami_mime_content_to_cmd(c);
	if(plugincmd == NULL) return;

	full_cmd = ASPrintf("%s %s", lwc_string_data(plugincmd), nsurl_access(hlcache_handle_get_url(c)));

	if(full_cmd)
	{
#ifdef __amigaos4__
		NSLOG(netsurf, INFO, "Attempting to execute %s", full_cmd);

		in = Open("NIL:", MODE_OLDFILE);
		out = Open("NIL:", MODE_NEWFILE);

		SystemTags(full_cmd,
				SYS_Input, in,
				SYS_Output, out,
				SYS_Error, out,
				SYS_Asynch, TRUE,
				NP_Name, "NetSurf External Process",
				TAG_DONE);

		FreeVec(full_cmd);
#endif
	}
}