plug-in-message.c 23.5 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

Elliot Lee's avatar
Elliot Lee committed
19
#include "config.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
20

Elliot Lee's avatar
Elliot Lee committed
21
#include <string.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
22

23
#include <glib-object.h>
24

25
26
27
#include "libgimpbase/gimpbase.h"
#include "libgimpbase/gimpprotocol.h"
#include "libgimpbase/gimpwire.h"
28

Michael Natterer's avatar
Michael Natterer committed
29
#include "plug-in-types.h"
30

Michael Natterer's avatar
Michael Natterer committed
31
32
33
#include "base/tile.h"
#include "base/tile-manager.h"

34
#include "core/gimp.h"
35
#include "core/gimpdrawable.h"
36

Michael Natterer's avatar
Michael Natterer committed
37
#include "plug-in.h"
38
39
40
#include "plug-ins.h"
#include "plug-in-def.h"
#include "plug-in-params.h"
41
#include "plug-in-proc.h"
42
#include "plug-in-shm.h"
43

44

45
typedef struct _PlugInBlocked PlugInBlocked;
Elliot Lee's avatar
Elliot Lee committed
46
47
48
49

struct _PlugInBlocked
{
  PlugIn *plug_in;
50
  gchar  *proc_name;
Elliot Lee's avatar
Elliot Lee committed
51
52
53
};


54
55
56
57
58
59
60
61
/*  local function prototypes  */

static void plug_in_handle_quit             (PlugIn          *plug_in);
static void plug_in_handle_tile_req         (PlugIn          *plug_in,
                                             GPTileReq       *tile_req);
static void plug_in_handle_proc_run         (PlugIn          *plug_in,
                                             GPProcRun       *proc_run);
static void plug_in_handle_proc_return_priv (PlugIn          *plug_in,
Michael Natterer's avatar
Michael Natterer committed
62
63
                                             GPProcReturn    *proc_return,
                                             gboolean         temp_proc);
64
65
66
67
68
69
70
71
72
73
static void plug_in_handle_proc_return      (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
static void plug_in_handle_temp_proc_return (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
static void plug_in_handle_proc_install     (PlugIn          *plug_in,
                                             GPProcInstall   *proc_install);
static void plug_in_handle_proc_uninstall   (PlugIn          *plug_in,
                                             GPProcUninstall *proc_uninstall);
static void plug_in_handle_extension_ack    (PlugIn          *plug_in);
static void plug_in_handle_has_init         (PlugIn          *plug_in);
Tor Lillqvist's avatar
Tor Lillqvist committed
74

75

76
/*  private variables  */
77

78
static GSList *blocked_plug_ins = NULL;
79

80

81
/*  public functions  */
Elliot Lee's avatar
Elliot Lee committed
82
83

void
84
85
plug_in_handle_message (PlugIn      *plug_in,
                        WireMessage *msg)
Elliot Lee's avatar
Elliot Lee committed
86
87
88
89
{
  switch (msg->type)
    {
    case GP_QUIT:
90
      plug_in_handle_quit (plug_in);
Elliot Lee's avatar
Elliot Lee committed
91
      break;
Michael Natterer's avatar
Michael Natterer committed
92

Elliot Lee's avatar
Elliot Lee committed
93
    case GP_CONFIG:
Michael Natterer's avatar
Michael Natterer committed
94
95
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a CONFIG message (should not happen)",
96
97
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
98
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
99
      break;
Michael Natterer's avatar
Michael Natterer committed
100

Elliot Lee's avatar
Elliot Lee committed
101
    case GP_TILE_REQ:
102
      plug_in_handle_tile_req (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
103
      break;
Michael Natterer's avatar
Michael Natterer committed
104

Elliot Lee's avatar
Elliot Lee committed
105
    case GP_TILE_ACK:
Michael Natterer's avatar
Michael Natterer committed
106
107
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TILE_ACK message (should not happen)",
108
109
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
110
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
111
      break;
Michael Natterer's avatar
Michael Natterer committed
112

Elliot Lee's avatar
Elliot Lee committed
113
    case GP_TILE_DATA:
Michael Natterer's avatar
Michael Natterer committed
114
115
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TILE_DATA message (should not happen)",
116
117
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
118
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
119
      break;
Michael Natterer's avatar
Michael Natterer committed
120

Elliot Lee's avatar
Elliot Lee committed
121
    case GP_PROC_RUN:
122
      plug_in_handle_proc_run (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
123
      break;
Michael Natterer's avatar
Michael Natterer committed
124

Elliot Lee's avatar
Elliot Lee committed
125
    case GP_PROC_RETURN:
126
      plug_in_handle_proc_return (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
127
      break;
Michael Natterer's avatar
Michael Natterer committed
128

Elliot Lee's avatar
Elliot Lee committed
129
    case GP_TEMP_PROC_RUN:
Michael Natterer's avatar
Michael Natterer committed
130
131
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TEMP_PROC_RUN message (should not happen)",
132
133
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
134
      plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
135
      break;
Michael Natterer's avatar
Michael Natterer committed
136

Elliot Lee's avatar
Elliot Lee committed
137
    case GP_TEMP_PROC_RETURN:
138
      plug_in_handle_temp_proc_return (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
139
      break;
Michael Natterer's avatar
Michael Natterer committed
140

Elliot Lee's avatar
Elliot Lee committed
141
    case GP_PROC_INSTALL:
142
      plug_in_handle_proc_install (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
143
      break;
Michael Natterer's avatar
Michael Natterer committed
144

Elliot Lee's avatar
Elliot Lee committed
145
    case GP_PROC_UNINSTALL:
146
      plug_in_handle_proc_uninstall (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
147
      break;
Michael Natterer's avatar
Michael Natterer committed
148

Elliot Lee's avatar
Elliot Lee committed
149
    case GP_EXTENSION_ACK:
Michael Natterer's avatar
Michael Natterer committed
150
      plug_in_handle_extension_ack (plug_in);
Elliot Lee's avatar
Elliot Lee committed
151
      break;
Michael Natterer's avatar
Michael Natterer committed
152

153
    case GP_HAS_INIT:
154
      plug_in_handle_has_init (plug_in);
Michael Natterer's avatar
Michael Natterer committed
155
      break;
Elliot Lee's avatar
Elliot Lee committed
156
157
158
    }
}

159
160
161

/*  private functions  */

Elliot Lee's avatar
Elliot Lee committed
162
static void
163
plug_in_handle_quit (PlugIn *plug_in)
Elliot Lee's avatar
Elliot Lee committed
164
{
165
  plug_in_close (plug_in, FALSE);
Elliot Lee's avatar
Elliot Lee committed
166
167
168
}

static void
169
170
plug_in_handle_tile_req (PlugIn    *plug_in,
                         GPTileReq *tile_req)
Elliot Lee's avatar
Elliot Lee committed
171
{
Michael Natterer's avatar
Michael Natterer committed
172
173
174
175
176
177
178
  GPTileData    tile_data;
  GPTileData   *tile_info;
  WireMessage   msg;
  GimpDrawable *drawable;
  TileManager  *tm;
  Tile         *tile;
  gint          shm_ID;
179
180

  shm_ID = plug_in_shm_get_ID (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
181
182
183

  if (tile_req->drawable_ID == -1)
    {
Michael Natterer's avatar
Michael Natterer committed
184
185
      /*  this branch communicates with libgimp/gimptile.c:gimp_tile_put()  */

Elliot Lee's avatar
Elliot Lee committed
186
      tile_data.drawable_ID = -1;
187
188
189
190
191
192
193
      tile_data.tile_num    = 0;
      tile_data.shadow      = 0;
      tile_data.bpp         = 0;
      tile_data.width       = 0;
      tile_data.height      = 0;
      tile_data.use_shm     = (shm_ID == -1) ? FALSE : TRUE;
      tile_data.data        = NULL;
Elliot Lee's avatar
Elliot Lee committed
194

195
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
196
	{
197
	  g_warning ("plug_in_handle_tile_req: ERROR");
198
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
199
200
201
	  return;
	}

202
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
203
	{
204
	  g_warning ("plug_in_handle_tile_req: ERROR");
205
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
206
207
208
209
210
	  return;
	}

      if (msg.type != GP_TILE_DATA)
	{
211
	  g_warning ("expected tile data and received: %d", msg.type);
212
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
213
214
215
216
217
	  return;
	}

      tile_info = msg.data;

Michael Natterer's avatar
Michael Natterer committed
218
219
      drawable = (GimpDrawable *) gimp_item_get_by_ID (plug_in->gimp,
                                                       tile_info->drawable_ID);
Elliot Lee's avatar
Elliot Lee committed
220

Michael Natterer's avatar
Michael Natterer committed
221
      if (! drawable)
Elliot Lee's avatar
Elliot Lee committed
222
	{
Michael Natterer's avatar
Michael Natterer committed
223
224
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid drawable (killing)",
225
226
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
227
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
228
229
230
	  return;
	}

Michael Natterer's avatar
Michael Natterer committed
231
232
233
234
235
      if (tile_info->shadow)
	tm = gimp_drawable_shadow (drawable);
      else
	tm = gimp_drawable_data (drawable);

236
      tile = tile_manager_get (tm, tile_info->tile_num, TRUE, TRUE);
Michael Natterer's avatar
Michael Natterer committed
237
238

      if (! tile)
Elliot Lee's avatar
Elliot Lee committed
239
	{
Michael Natterer's avatar
Michael Natterer committed
240
241
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid tile (killing)",
242
243
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
244
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
245
246
247
248
	  return;
	}

      if (tile_data.use_shm)
249
250
251
	memcpy (tile_data_pointer (tile, 0, 0),
                plug_in_shm_get_addr (plug_in->gimp),
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
252
      else
253
254
255
	memcpy (tile_data_pointer (tile, 0, 0),
                tile_info->data,
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
256

257
      tile_release (tile, TRUE);
Elliot Lee's avatar
Elliot Lee committed
258
259

      wire_destroy (&msg);
260
      if (! gp_tile_ack_write (plug_in->my_write, plug_in))
Elliot Lee's avatar
Elliot Lee committed
261
	{
262
	  g_warning ("plug_in_handle_tile_req: ERROR");
263
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
264
265
266
267
268
	  return;
	}
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
269
270
      /*  this branch communicates with libgimp/gimptile.c:gimp_tile_get()  */

Michael Natterer's avatar
Michael Natterer committed
271
272
      drawable = (GimpDrawable *) gimp_item_get_by_ID (plug_in->gimp,
                                                       tile_req->drawable_ID);
Elliot Lee's avatar
Elliot Lee committed
273

Michael Natterer's avatar
Michael Natterer committed
274
      if (! drawable)
Elliot Lee's avatar
Elliot Lee committed
275
	{
Michael Natterer's avatar
Michael Natterer committed
276
277
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid drawable (killing)",
278
279
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
280
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
281
282
283
	  return;
	}

Michael Natterer's avatar
Michael Natterer committed
284
285
286
287
288
      if (tile_req->shadow)
	tm = gimp_drawable_shadow (drawable);
      else
	tm = gimp_drawable_data (drawable);

289
      tile = tile_manager_get (tm, tile_req->tile_num, TRUE, FALSE);
Michael Natterer's avatar
Michael Natterer committed
290

291
      if (! tile)
Elliot Lee's avatar
Elliot Lee committed
292
	{
Michael Natterer's avatar
Michael Natterer committed
293
294
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid tile (killing)",
295
296
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
297
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
298
299
300
301
	  return;
	}

      tile_data.drawable_ID = tile_req->drawable_ID;
302
303
304
305
306
307
      tile_data.tile_num    = tile_req->tile_num;
      tile_data.shadow      = tile_req->shadow;
      tile_data.bpp         = tile_bpp (tile);
      tile_data.width       = tile_ewidth (tile);
      tile_data.height      = tile_eheight (tile);
      tile_data.use_shm     = (shm_ID == -1) ? FALSE : TRUE;
Elliot Lee's avatar
Elliot Lee committed
308
309

      if (tile_data.use_shm)
310
311
312
	memcpy (plug_in_shm_get_addr (plug_in->gimp),
                tile_data_pointer (tile, 0, 0),
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
313
      else
scott's avatar
scott committed
314
	tile_data.data = tile_data_pointer (tile, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
315

316
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
317
	{
318
	  g_message ("plug_in_handle_tile_req: ERROR");
319
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
320
321
322
	  return;
	}

323
      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
324

325
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
326
	{
327
	  g_message ("plug_in_handle_tile_req: ERROR");
328
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
329
330
331
332
333
	  return;
	}

      if (msg.type != GP_TILE_ACK)
	{
334
	  g_warning ("expected tile ack and received: %d", msg.type);
335
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
336
337
338
339
340
341
342
343
	  return;
	}

      wire_destroy (&msg);
    }
}

static void
344
plug_in_handle_proc_run (PlugIn    *plug_in,
345
                         GPProcRun *proc_run)
Elliot Lee's avatar
Elliot Lee committed
346
{
347
348
349
350
  const gchar *proc_name = NULL;
  ProcRecord  *proc_rec;
  Argument    *args;
  Argument    *return_vals;
Elliot Lee's avatar
Elliot Lee committed
351

352
  proc_rec = procedural_db_lookup (plug_in->gimp, proc_run->name);
Elliot Lee's avatar
Elliot Lee committed
353

354
355
356
357
358
359
  if (! proc_rec)
    {
      proc_name = g_hash_table_lookup (plug_in->gimp->procedural_compat_ht,
                                       proc_run->name);

      if (proc_name)
360
361
362
363
364
        {
          proc_rec = procedural_db_lookup (plug_in->gimp, proc_name);

          if (plug_in->gimp->pdb_compat_mode == GIMP_PDB_COMPAT_WARN)
            {
Michael Natterer's avatar
Michael Natterer committed
365
              g_message ("WARNING: Plug-In \"%s\"\n(%s)\n\n"
366
367
                         "called deprecated procedure '%s'.\n"
                         "It should call '%s' instead!",
368
369
                         gimp_filename_to_utf8 (plug_in->name),
			 gimp_filename_to_utf8 (plug_in->prog),
370
371
372
                         proc_run->name, proc_name);
            }
        }
373
374
375
376
377
378
379
    }

  if (! proc_name)
    proc_name = proc_run->name;

  args = plug_in_params_to_args (proc_run->params, proc_run->nparams, FALSE);

380
  plug_in_push (plug_in->gimp, plug_in);
381
382
383
384

  /*  Execute the procedure even if procedural_db_lookup() returned NULL,
   *  procedural_db_execute() will return appropriate error return_vals.
   */
Michael Natterer's avatar
Michael Natterer committed
385
  return_vals = procedural_db_execute (plug_in->gimp, plug_in->context,
386
                                       plug_in->progress,
387
                                       proc_name, args);
388

389
  plug_in_pop (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
390
391
392

  if (return_vals)
    {
393
394
      GPProcReturn proc_return;

Michael Natterer's avatar
Michael Natterer committed
395
396
397
      /*  Return the name we got called with, *not* proc_name, since
       *  proc_name may have been remapped by gimp->procedural_compat_ht
       */
Elliot Lee's avatar
Elliot Lee committed
398
399
400
401
402
      proc_return.name = proc_run->name;

      if (proc_rec)
	{
	  proc_return.nparams = proc_rec->num_values + 1;
403
404
405
	  proc_return.params  = plug_in_args_to_params (return_vals,
                                                        proc_return.nparams,
                                                        FALSE);
Elliot Lee's avatar
Elliot Lee committed
406
407
408
409
	}
      else
	{
	  proc_return.nparams = 1;
410
	  proc_return.params  = plug_in_args_to_params (return_vals, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
411
412
	}

413
      if (! gp_proc_return_write (plug_in->my_write, &proc_return, plug_in))
Elliot Lee's avatar
Elliot Lee committed
414
	{
415
	  g_warning ("plug_in_handle_proc_run: ERROR");
416
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
417
418
419
420
	  return;
	}

      plug_in_args_destroy (args, proc_run->nparams, FALSE);
421
422
423
424
425
426

      if (proc_rec)
        plug_in_args_destroy (return_vals, proc_rec->num_values + 1, TRUE);
      else
        plug_in_args_destroy (return_vals, 1, TRUE);

Elliot Lee's avatar
Elliot Lee committed
427
428
429
430
      plug_in_params_destroy (proc_return.params, proc_return.nparams, FALSE);
    }
  else
    {
431
432
      PlugInBlocked *blocked;

433
434
      blocked = g_new0 (PlugInBlocked, 1);

435
      blocked->plug_in   = plug_in;
Elliot Lee's avatar
Elliot Lee committed
436
      blocked->proc_name = g_strdup (proc_run->name);
437

Elliot Lee's avatar
Elliot Lee committed
438
439
440
441
442
      blocked_plug_ins = g_slist_prepend (blocked_plug_ins, blocked);
    }
}

static void
443
plug_in_handle_proc_return_priv (PlugIn       *plug_in,
Michael Natterer's avatar
Michael Natterer committed
444
445
                                 GPProcReturn *proc_return,
                                 gboolean      temp_proc)
Elliot Lee's avatar
Elliot Lee committed
446
{
Michael Natterer's avatar
Michael Natterer committed
447
448
449
450
451
452
453
454
  PlugInProcFrame *proc_frame;

  if (temp_proc)
    proc_frame = plug_in->temp_proc_frames->data;
  else
    proc_frame = &plug_in->main_proc_frame;

  if (proc_frame->main_loop)
Elliot Lee's avatar
Elliot Lee committed
455
    {
Michael Natterer's avatar
Michael Natterer committed
456
457
458
459
      proc_frame->return_vals = plug_in_params_to_args (proc_return->params,
                                                        proc_return->nparams,
                                                        TRUE);
      proc_frame->n_return_vals = proc_return->nparams;
Elliot Lee's avatar
Elliot Lee committed
460
461
462
    }
  else
    {
463
464
465
      GSList *list;

      for (list = blocked_plug_ins; list; list = g_slist_next (list))
Elliot Lee's avatar
Elliot Lee committed
466
	{
467
468
469
          PlugInBlocked *blocked;

	  blocked = (PlugInBlocked *) list->data;
Elliot Lee's avatar
Elliot Lee committed
470

471
	  if (blocked->proc_name && proc_return->name &&
Sven Neumann's avatar
Sven Neumann committed
472
	      strcmp (blocked->proc_name, proc_return->name) == 0)
Elliot Lee's avatar
Elliot Lee committed
473
	    {
474
	      if (! gp_proc_return_write (blocked->plug_in->my_write,
475
476
                                          proc_return,
                                          blocked->plug_in))
Elliot Lee's avatar
Elliot Lee committed
477
		{
478
		  g_message ("plug_in_handle_proc_run: ERROR");
479
		  plug_in_close (blocked->plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
480
481
		  return;
		}
482

Elliot Lee's avatar
Elliot Lee committed
483
484
485
486
487
488
489
490
491
	      blocked_plug_ins = g_slist_remove (blocked_plug_ins, blocked);
	      g_free (blocked->proc_name);
	      g_free (blocked);
	      break;
	    }
	}
    }
}

492
493
494
495
static void
plug_in_handle_proc_return (PlugIn       *plug_in,
                            GPProcReturn *proc_return)
{
Michael Natterer's avatar
Michael Natterer committed
496
  plug_in_handle_proc_return_priv (plug_in, proc_return, FALSE);
497

Michael Natterer's avatar
Michael Natterer committed
498
499
  if (plug_in->main_proc_frame.main_loop)
    g_main_loop_quit (plug_in->main_proc_frame.main_loop);
500
501
502
503
504
505
506
507

  plug_in_close (plug_in, FALSE);
}

static void
plug_in_handle_temp_proc_return (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
{
Michael Natterer's avatar
Michael Natterer committed
508
  if (plug_in->temp_proc_frames)
509
    {
Michael Natterer's avatar
Michael Natterer committed
510
      plug_in_handle_proc_return_priv (plug_in, proc_return, TRUE);
511
512
513
514

      plug_in_main_loop_quit (plug_in);
    }
  else
515
    {
Michael Natterer's avatar
Michael Natterer committed
516
517
518
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TEMP_PROC_RETURN message while not running "
                 "a temp proc (should not happen)",
519
520
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
521
522
523
524
      plug_in_close (plug_in, TRUE);
    }
}

Elliot Lee's avatar
Elliot Lee committed
525
static void
526
plug_in_handle_proc_install (PlugIn        *plug_in,
527
                             GPProcInstall *proc_install)
Elliot Lee's avatar
Elliot Lee committed
528
{
529
  PlugInDef     *plug_in_def = NULL;
530
531
532
533
534
  PlugInProcDef *proc_def    = NULL;
  ProcRecord    *proc        = NULL;
  GSList        *tmp         = NULL;
  gchar         *prog        = NULL;
  gboolean       valid_utf8  = FALSE;
535
  gint           i;
Elliot Lee's avatar
Elliot Lee committed
536

537
  /*  Argument checking
Elliot Lee's avatar
Elliot Lee committed
538
539
540
   *   --only sanity check arguments when the procedure requests a menu path
   */

541
  if (proc_install->menu_path && proc_install->menu_path[0] == '<')
Elliot Lee's avatar
Elliot Lee committed
542
    {
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
      GError *error = NULL;

      if (! plug_in_param_defs_check (plug_in->name,
                                      plug_in->prog,
                                      proc_install->name,
                                      proc_install->menu_path,
                                      proc_install->params,
                                      proc_install->nparams,
                                      proc_install->return_vals,
                                      proc_install->nreturn_vals,
                                      &error))
        {
          g_message (error->message);
          g_clear_error (&error);

          return;
        }
Elliot Lee's avatar
Elliot Lee committed
560
561
    }

562
  /*  Sanity check for array arguments  */
Elliot Lee's avatar
Elliot Lee committed
563

564
  for (i = 1; i < proc_install->nparams; i++)
Elliot Lee's avatar
Elliot Lee committed
565
    {
566
      if ((proc_install->params[i].type == GIMP_PDB_INT32ARRAY ||
567
	   proc_install->params[i].type == GIMP_PDB_INT8ARRAY  ||
568
	   proc_install->params[i].type == GIMP_PDB_FLOATARRAY ||
569
	   proc_install->params[i].type == GIMP_PDB_STRINGARRAY)
570
          &&
571
	  proc_install->params[i-1].type != GIMP_PDB_INT32)
Elliot Lee's avatar
Elliot Lee committed
572
	{
Michael Natterer's avatar
Michael Natterer committed
573
574
575
576
	  g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "attempted to install procedure \"%s\" "
                     "which fails to comply with the array parameter "
                     "passing standard.  Argument %d is noncompliant.",
577
578
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
579
                     proc_install->name, i);
Elliot Lee's avatar
Elliot Lee committed
580
581
582
	  return;
	}
    }
583
584
585

  /*  Sanity check strings for UTF-8 validity  */

586
  if ((proc_install->menu_path == NULL ||
587
588
       g_utf8_validate (proc_install->menu_path, -1, NULL)) &&
      (g_utf8_validate (proc_install->name, -1, NULL))      &&
589
      (proc_install->blurb == NULL ||
590
       g_utf8_validate (proc_install->blurb, -1, NULL))     &&
591
      (proc_install->help == NULL ||
592
593
594
       g_utf8_validate (proc_install->help, -1, NULL))      &&
      (proc_install->author == NULL ||
       g_utf8_validate (proc_install->author, -1, NULL))    &&
595
      (proc_install->copyright == NULL ||
596
597
598
599
       g_utf8_validate (proc_install->copyright, -1, NULL)) &&
      (proc_install->date == NULL ||
       g_utf8_validate (proc_install->date, -1, NULL)))
    {
600
      valid_utf8 = TRUE;
601

602
      for (i = 0; i < proc_install->nparams && valid_utf8; i++)
603
604
        {
          if (! (g_utf8_validate (proc_install->params[i].name, -1, NULL) &&
605
                 (proc_install->params[i].description == NULL ||
606
                  g_utf8_validate (proc_install->params[i].description, -1, NULL))))
607
            valid_utf8 = FALSE;
608
        }
609

610
      for (i = 0; i < proc_install->nreturn_vals && valid_utf8; i++)
611
612
613
614
        {
          if (! (g_utf8_validate (proc_install->return_vals[i].name, -1, NULL) &&
                 (proc_install->return_vals[i].description == NULL ||
                  g_utf8_validate (proc_install->return_vals[i].description, -1, NULL))))
615
            valid_utf8 = FALSE;
616
617
        }
    }
618

619
  if (! valid_utf8)
620
    {
Michael Natterer's avatar
Michael Natterer committed
621
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
622
                 "attempted to install a procedure with invalid UTF-8 strings.",
623
624
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
625
626
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
627

628
629
  /*  Initialization  */

Elliot Lee's avatar
Elliot Lee committed
630
631
  switch (proc_install->type)
    {
632
633
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
634
635
      plug_in_def = plug_in->plug_in_def;
      prog        = plug_in_def->prog;
Elliot Lee's avatar
Elliot Lee committed
636
637
638
639

      tmp = plug_in_def->proc_defs;
      break;

640
    case GIMP_TEMPORARY:
641
642
      plug_in_def = NULL;
      prog        = "none";
Elliot Lee's avatar
Elliot Lee committed
643

644
      tmp = plug_in->temp_proc_defs;
Elliot Lee's avatar
Elliot Lee committed
645
646
647
648
649
650
651
652
653
654
      break;
    }

  while (tmp)
    {
      proc_def = tmp->data;
      tmp = tmp->next;

      if (strcmp (proc_def->db_info.name, proc_install->name) == 0)
	{
655
          switch (proc_install->type)
656
            {
657
658
            case GIMP_PLUGIN:
            case GIMP_EXTENSION:
659
660
661
              plug_in_def->proc_defs = g_slist_remove (plug_in_def->proc_defs,
                                                       proc_def);
              plug_in_proc_def_free (proc_def);
662
663
664
665
666
667
668
              break;

            case GIMP_TEMPORARY:
              plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                        proc_def);
              plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
              break;
669
            }
670

Elliot Lee's avatar
Elliot Lee committed
671
672
673
674
	  break;
	}
    }

675
  proc_def = plug_in_proc_def_new ();
Elliot Lee's avatar
Elliot Lee committed
676

677
  if (proc_install->menu_path)
678
679
680
681
682
683
684
685
    {
      if (proc_install->menu_path[0] == '<')
        proc_def->menu_paths =
          g_list_append (proc_def->menu_paths,
                         g_strdup (proc_install->menu_path));
      else
        proc_def->menu_label = g_strdup (proc_install->menu_path);
    }
686

687
  proc_def->prog            = g_strdup (prog);
688
689
690
691
  proc_def->extensions      = NULL;
  proc_def->prefixes        = NULL;
  proc_def->magics          = NULL;
  proc_def->image_types     = g_strdup (proc_install->image_types);
692
  proc_def->image_types_val = plug_ins_image_types_parse (proc_def->image_types);
GMT 1999 Andy Thomas's avatar
GMT 1999 Andy Thomas committed
693
  /* Install temp one use todays time */
694
  proc_def->mtime           = time (NULL);
Elliot Lee's avatar
Elliot Lee committed
695

696
697
698
  /* Remember if this proc was installed while initing a plug-in */
  proc_def->installed_during_init = plug_in->init;

699
700
  /*  The procedural database procedure  */

701
702
  proc = &proc_def->db_info;

703
704
705
706
  proc->name      = g_strdup (proc_install->name);
  proc->blurb     = g_strdup (proc_install->blurb);
  proc->help      = g_strdup (proc_install->help);
  proc->author    = g_strdup (proc_install->author);
Elliot Lee's avatar
Elliot Lee committed
707
  proc->copyright = g_strdup (proc_install->copyright);
708
  proc->date      = g_strdup (proc_install->date);
Elliot Lee's avatar
Elliot Lee committed
709
710
  proc->proc_type = proc_install->type;

711
  proc->num_args   = proc_install->nparams;
Elliot Lee's avatar
Elliot Lee committed
712
713
  proc->num_values = proc_install->nreturn_vals;

714
715
  proc->args   = g_new0 (ProcArg, proc->num_args);
  proc->values = g_new0 (ProcArg, proc->num_values);
Elliot Lee's avatar
Elliot Lee committed
716
717
718

  for (i = 0; i < proc->num_args; i++)
    {
719
720
      proc->args[i].arg_type    = proc_install->params[i].type;
      proc->args[i].name        = g_strdup (proc_install->params[i].name);
Elliot Lee's avatar
Elliot Lee committed
721
722
723
724
725
      proc->args[i].description = g_strdup (proc_install->params[i].description);
    }

  for (i = 0; i < proc->num_values; i++)
    {
726
727
      proc->values[i].arg_type    = proc_install->return_vals[i].type;
      proc->values[i].name        = g_strdup (proc_install->return_vals[i].name);
Elliot Lee's avatar
Elliot Lee committed
728
729
730
731
732
      proc->values[i].description = g_strdup (proc_install->return_vals[i].description);
    }

  switch (proc_install->type)
    {
733
734
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
735
736
      plug_in_def->proc_defs = g_slist_prepend (plug_in_def->proc_defs,
                                                proc_def);
Elliot Lee's avatar
Elliot Lee committed
737
738
      break;

739
    case GIMP_TEMPORARY:
740
741
      plug_in->temp_proc_defs = g_slist_prepend (plug_in->temp_proc_defs,
                                                 proc_def);
Elliot Lee's avatar
Elliot Lee committed
742

743
      proc->exec_method.temporary.plug_in = plug_in;
Elliot Lee's avatar
Elliot Lee committed
744

745
      plug_ins_temp_proc_def_add (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
746
747
748
749
750
      break;
    }
}

static void
751
plug_in_handle_proc_uninstall (PlugIn          *plug_in,
752
                               GPProcUninstall *proc_uninstall)
Elliot Lee's avatar
Elliot Lee committed
753
{
754
  GSList *tmp;
Elliot Lee's avatar
Elliot Lee committed
755

Michael Natterer's avatar
Michael Natterer committed
756
  for (tmp = plug_in->temp_proc_defs; tmp; tmp = g_slist_next (tmp))
Elliot Lee's avatar
Elliot Lee committed
757
    {
758
759
      PlugInProcDef *proc_def;

Elliot Lee's avatar
Elliot Lee committed
760
761
      proc_def = tmp->data;

Michael Natterer's avatar
Michael Natterer committed
762
      if (! strcmp (proc_def->db_info.name, proc_uninstall->name))
Elliot Lee's avatar
Elliot Lee committed
763
	{
764
765
	  plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                    proc_def);
766
	  plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
767
768
769
770
	  break;
	}
    }
}
771

Michael Natterer's avatar
Michael Natterer committed
772
773
774
static void
plug_in_handle_extension_ack (PlugIn *plug_in)
{
775
  if (plug_in->ext_main_loop)
776
    {
777
      g_main_loop_quit (plug_in->ext_main_loop);
778
779
780
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
781
782
783
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent an EXTENSION_ACK message while not being started "
                 "as extension (should not happen)",
784
785
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
786
787
      plug_in_close (plug_in, TRUE);
    }
Michael Natterer's avatar
Michael Natterer committed
788
789
}

790
static void
791
plug_in_handle_has_init (PlugIn *plug_in)
792
{
793
  if (plug_in->query)
794
795
796
797
798
    {
      plug_in_def_set_has_init (plug_in->plug_in_def, TRUE);
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
799
800
801
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent an HAS_INIT message while not in query() "
                 "(should not happen)",
802
803
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
804
805
      plug_in_close (plug_in, TRUE);
    }
806
}