plug-in-message.c 25.9 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
46
47
#define ENABLE_TEMP_RETURN 1


48
typedef struct _PlugInBlocked PlugInBlocked;
Elliot Lee's avatar
Elliot Lee committed
49
50
51
52

struct _PlugInBlocked
{
  PlugIn *plug_in;
53
  gchar  *proc_name;
Elliot Lee's avatar
Elliot Lee committed
54
55
56
};


57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*  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,
                                             GPProcReturn    *proc_return);
static void plug_in_handle_proc_return      (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
#ifdef ENABLE_TEMP_RETURN
static void plug_in_handle_temp_proc_return (PlugIn          *plug_in,
                                             GPProcReturn    *proc_return);
Tor Lillqvist's avatar
Tor Lillqvist committed
71
#endif
72
73
74
75
76
77
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
78

79

80
/*  private variables  */
81

82
static GSList *blocked_plug_ins = NULL;
83

84

85
/*  public functions  */
Elliot Lee's avatar
Elliot Lee committed
86
87

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

Elliot Lee's avatar
Elliot Lee committed
97
    case GP_CONFIG:
Michael Natterer's avatar
Michael Natterer committed
98
99
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a CONFIG message (should not happen)",
100
101
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
102
      plug_in_close (plug_in, TRUE);
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_REQ:
106
      plug_in_handle_tile_req (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
107
      break;
Michael Natterer's avatar
Michael Natterer committed
108

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

Elliot Lee's avatar
Elliot Lee committed
117
    case GP_TILE_DATA:
Michael Natterer's avatar
Michael Natterer committed
118
119
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TILE_DATA message (should not happen)",
120
121
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
122
      plug_in_close (plug_in, TRUE);
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_RUN:
126
      plug_in_handle_proc_run (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_PROC_RETURN:
130
      plug_in_handle_proc_return (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
131
      break;
Michael Natterer's avatar
Michael Natterer committed
132

Elliot Lee's avatar
Elliot Lee committed
133
    case GP_TEMP_PROC_RUN:
Michael Natterer's avatar
Michael Natterer committed
134
135
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TEMP_PROC_RUN message (should not happen)",
136
137
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
138
      plug_in_close (plug_in, TRUE);
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_TEMP_PROC_RETURN:
142
143
144
#ifdef ENABLE_TEMP_RETURN
      plug_in_handle_temp_proc_return (plug_in, msg->data);
#else
Michael Natterer's avatar
Michael Natterer committed
145
146
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TEMP_PROC_RETURN message (should not happen)",
147
148
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
149
150
      plug_in_close (plug_in, TRUE);
#endif
Elliot Lee's avatar
Elliot Lee committed
151
      break;
Michael Natterer's avatar
Michael Natterer committed
152

Elliot Lee's avatar
Elliot Lee committed
153
    case GP_PROC_INSTALL:
154
      plug_in_handle_proc_install (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
155
      break;
Michael Natterer's avatar
Michael Natterer committed
156

Elliot Lee's avatar
Elliot Lee committed
157
    case GP_PROC_UNINSTALL:
158
      plug_in_handle_proc_uninstall (plug_in, msg->data);
Elliot Lee's avatar
Elliot Lee committed
159
      break;
Michael Natterer's avatar
Michael Natterer committed
160

Elliot Lee's avatar
Elliot Lee committed
161
    case GP_EXTENSION_ACK:
Michael Natterer's avatar
Michael Natterer committed
162
      plug_in_handle_extension_ack (plug_in);
Elliot Lee's avatar
Elliot Lee committed
163
      break;
Michael Natterer's avatar
Michael Natterer committed
164

165
    case GP_HAS_INIT:
166
      plug_in_handle_has_init (plug_in);
Michael Natterer's avatar
Michael Natterer committed
167
      break;
Elliot Lee's avatar
Elliot Lee committed
168
169
170
    }
}

171
172
173

/*  private functions  */

Elliot Lee's avatar
Elliot Lee committed
174
static void
175
plug_in_handle_quit (PlugIn *plug_in)
Elliot Lee's avatar
Elliot Lee committed
176
{
177
  plug_in_close (plug_in, FALSE);
Elliot Lee's avatar
Elliot Lee committed
178
179
180
}

static void
181
182
plug_in_handle_tile_req (PlugIn    *plug_in,
                         GPTileReq *tile_req)
Elliot Lee's avatar
Elliot Lee committed
183
{
Michael Natterer's avatar
Michael Natterer committed
184
185
186
187
188
189
190
  GPTileData    tile_data;
  GPTileData   *tile_info;
  WireMessage   msg;
  GimpDrawable *drawable;
  TileManager  *tm;
  Tile         *tile;
  gint          shm_ID;
191
192

  shm_ID = plug_in_shm_get_ID (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
193
194
195

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

Elliot Lee's avatar
Elliot Lee committed
198
      tile_data.drawable_ID = -1;
199
200
201
202
203
204
205
      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
206

207
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
208
	{
209
	  g_warning ("plug_in_handle_tile_req: ERROR");
210
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
211
212
213
	  return;
	}

214
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
215
	{
216
	  g_warning ("plug_in_handle_tile_req: ERROR");
217
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
218
219
220
221
222
	  return;
	}

      if (msg.type != GP_TILE_DATA)
	{
223
	  g_warning ("expected tile data and received: %d", msg.type);
224
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
225
226
227
228
229
	  return;
	}

      tile_info = msg.data;

Michael Natterer's avatar
Michael Natterer committed
230
231
      drawable = (GimpDrawable *) gimp_item_get_by_ID (plug_in->gimp,
                                                       tile_info->drawable_ID);
Elliot Lee's avatar
Elliot Lee committed
232

Michael Natterer's avatar
Michael Natterer committed
233
      if (! drawable)
Elliot Lee's avatar
Elliot Lee committed
234
	{
Michael Natterer's avatar
Michael Natterer committed
235
236
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid drawable (killing)",
237
238
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
239
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
240
241
242
	  return;
	}

Michael Natterer's avatar
Michael Natterer committed
243
244
245
246
247
      if (tile_info->shadow)
	tm = gimp_drawable_shadow (drawable);
      else
	tm = gimp_drawable_data (drawable);

248
      tile = tile_manager_get (tm, tile_info->tile_num, TRUE, TRUE);
Michael Natterer's avatar
Michael Natterer committed
249
250

      if (! tile)
Elliot Lee's avatar
Elliot Lee committed
251
	{
Michael Natterer's avatar
Michael Natterer committed
252
253
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid tile (killing)",
254
255
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
256
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
257
258
259
260
	  return;
	}

      if (tile_data.use_shm)
261
262
263
	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
264
      else
265
266
267
	memcpy (tile_data_pointer (tile, 0, 0),
                tile_info->data,
                tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
268

269
      tile_release (tile, TRUE);
Elliot Lee's avatar
Elliot Lee committed
270
271

      wire_destroy (&msg);
272
      if (! gp_tile_ack_write (plug_in->my_write, plug_in))
Elliot Lee's avatar
Elliot Lee committed
273
	{
274
	  g_warning ("plug_in_handle_tile_req: ERROR");
275
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
276
277
278
279
280
	  return;
	}
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
281
282
      /*  this branch communicates with libgimp/gimptile.c:gimp_tile_get()  */

Michael Natterer's avatar
Michael Natterer committed
283
284
      drawable = (GimpDrawable *) gimp_item_get_by_ID (plug_in->gimp,
                                                       tile_req->drawable_ID);
Elliot Lee's avatar
Elliot Lee committed
285

Michael Natterer's avatar
Michael Natterer committed
286
      if (! drawable)
Elliot Lee's avatar
Elliot Lee committed
287
	{
Michael Natterer's avatar
Michael Natterer committed
288
289
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid drawable (killing)",
290
291
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
292
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
293
294
295
	  return;
	}

Michael Natterer's avatar
Michael Natterer committed
296
297
298
299
300
      if (tile_req->shadow)
	tm = gimp_drawable_shadow (drawable);
      else
	tm = gimp_drawable_data (drawable);

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

303
      if (! tile)
Elliot Lee's avatar
Elliot Lee committed
304
	{
Michael Natterer's avatar
Michael Natterer committed
305
306
          g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "requested invalid tile (killing)",
307
308
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog));
309
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
310
311
312
313
	  return;
	}

      tile_data.drawable_ID = tile_req->drawable_ID;
314
315
316
317
318
319
      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
320
321

      if (tile_data.use_shm)
322
323
324
	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
325
      else
scott's avatar
scott committed
326
	tile_data.data = tile_data_pointer (tile, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
327

328
      if (! gp_tile_data_write (plug_in->my_write, &tile_data, plug_in))
Elliot Lee's avatar
Elliot Lee committed
329
	{
330
	  g_message ("plug_in_handle_tile_req: ERROR");
331
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
332
333
334
	  return;
	}

335
      tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
336

337
      if (! wire_read_msg (plug_in->my_read, &msg, plug_in))
Elliot Lee's avatar
Elliot Lee committed
338
	{
339
	  g_message ("plug_in_handle_tile_req: ERROR");
340
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
341
342
343
344
345
	  return;
	}

      if (msg.type != GP_TILE_ACK)
	{
346
	  g_warning ("expected tile ack and received: %d", msg.type);
347
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
348
349
350
351
352
353
354
355
	  return;
	}

      wire_destroy (&msg);
    }
}

static void
356
plug_in_handle_proc_run (PlugIn    *plug_in,
357
                         GPProcRun *proc_run)
Elliot Lee's avatar
Elliot Lee committed
358
{
359
360
361
362
  const gchar *proc_name = NULL;
  ProcRecord  *proc_rec;
  Argument    *args;
  Argument    *return_vals;
Elliot Lee's avatar
Elliot Lee committed
363

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

366
367
368
369
370
371
  if (! proc_rec)
    {
      proc_name = g_hash_table_lookup (plug_in->gimp->procedural_compat_ht,
                                       proc_run->name);

      if (proc_name)
372
373
374
375
376
        {
          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
377
              g_message ("WARNING: Plug-In \"%s\"\n(%s)\n\n"
378
379
                         "called deprecated procedure '%s'.\n"
                         "It should call '%s' instead!",
380
381
                         gimp_filename_to_utf8 (plug_in->name),
			 gimp_filename_to_utf8 (plug_in->prog),
382
383
384
                         proc_run->name, proc_name);
            }
        }
385
386
387
388
389
390
391
    }

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

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

392
  plug_in_push (plug_in->gimp, plug_in);
393
394
395
396

  /*  Execute the procedure even if procedural_db_lookup() returned NULL,
   *  procedural_db_execute() will return appropriate error return_vals.
   */
397
398
399
  return_vals = procedural_db_execute (plug_in->gimp,
                                       gimp_get_user_context (plug_in->gimp),
                                       proc_name, args);
400

401
  plug_in_pop (plug_in->gimp);
Elliot Lee's avatar
Elliot Lee committed
402
403
404

  if (return_vals)
    {
405
406
      GPProcReturn proc_return;

Michael Natterer's avatar
Michael Natterer committed
407
408
409
      /*  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
410
411
412
413
414
      proc_return.name = proc_run->name;

      if (proc_rec)
	{
	  proc_return.nparams = proc_rec->num_values + 1;
415
416
417
	  proc_return.params  = plug_in_args_to_params (return_vals,
                                                        proc_return.nparams,
                                                        FALSE);
Elliot Lee's avatar
Elliot Lee committed
418
419
420
421
	}
      else
	{
	  proc_return.nparams = 1;
422
	  proc_return.params  = plug_in_args_to_params (return_vals, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
423
424
	}

425
      if (! gp_proc_return_write (plug_in->my_write, &proc_return, plug_in))
Elliot Lee's avatar
Elliot Lee committed
426
	{
427
	  g_warning ("plug_in_handle_proc_run: ERROR");
428
	  plug_in_close (plug_in, TRUE);
Elliot Lee's avatar
Elliot Lee committed
429
430
431
432
	  return;
	}

      plug_in_args_destroy (args, proc_run->nparams, FALSE);
433
434
435
436
437
438

      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
439
440
441
442
      plug_in_params_destroy (proc_return.params, proc_return.nparams, FALSE);
    }
  else
    {
443
444
      PlugInBlocked *blocked;

445
446
      blocked = g_new0 (PlugInBlocked, 1);

447
      blocked->plug_in   = plug_in;
Elliot Lee's avatar
Elliot Lee committed
448
      blocked->proc_name = g_strdup (proc_run->name);
449

Elliot Lee's avatar
Elliot Lee committed
450
451
452
453
454
      blocked_plug_ins = g_slist_prepend (blocked_plug_ins, blocked);
    }
}

static void
455
456
plug_in_handle_proc_return_priv (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
Elliot Lee's avatar
Elliot Lee committed
457
{
458
  if (plug_in->recurse_main_loop || plug_in->temp_main_loops)
Elliot Lee's avatar
Elliot Lee committed
459
    {
460
461
462
463
      plug_in->return_vals = plug_in_params_to_args (proc_return->params,
                                                     proc_return->nparams,
                                                     TRUE);
      plug_in->n_return_vals = proc_return->nparams;
Elliot Lee's avatar
Elliot Lee committed
464
465
466
    }
  else
    {
467
468
469
      GSList *list;

      for (list = blocked_plug_ins; list; list = g_slist_next (list))
Elliot Lee's avatar
Elliot Lee committed
470
	{
471
472
473
          PlugInBlocked *blocked;

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

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

Elliot Lee's avatar
Elliot Lee committed
487
488
489
490
491
492
493
494
495
	      blocked_plug_ins = g_slist_remove (blocked_plug_ins, blocked);
	      g_free (blocked->proc_name);
	      g_free (blocked);
	      break;
	    }
	}
    }
}

496
497
498
499
500
501
static void
plug_in_handle_proc_return (PlugIn       *plug_in,
                            GPProcReturn *proc_return)
{
  plug_in_handle_proc_return_priv (plug_in, proc_return);

502
503
  if (plug_in->recurse_main_loop)
    g_main_loop_quit (plug_in->recurse_main_loop);
504
505
506
507
508
509
510
511
512

  plug_in_close (plug_in, FALSE);
}

#ifdef ENABLE_TEMP_RETURN
static void
plug_in_handle_temp_proc_return (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
{
513
  if (plug_in->temp_main_loops)
514
515
516
517
518
519
    {
      plug_in_handle_proc_return_priv (plug_in, proc_return);

      plug_in_main_loop_quit (plug_in);
    }
  else
520
    {
Michael Natterer's avatar
Michael Natterer committed
521
522
523
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent a TEMP_PROC_RETURN message while not running "
                 "a temp proc (should not happen)",
524
525
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
526
527
528
529
530
      plug_in_close (plug_in, TRUE);
    }
}
#endif

Elliot Lee's avatar
Elliot Lee committed
531
static void
532
plug_in_handle_proc_install (PlugIn        *plug_in,
533
                             GPProcInstall *proc_install)
Elliot Lee's avatar
Elliot Lee committed
534
{
535
  PlugInDef     *plug_in_def = NULL;
536
537
538
539
540
  PlugInProcDef *proc_def    = NULL;
  ProcRecord    *proc        = NULL;
  GSList        *tmp         = NULL;
  gchar         *prog        = NULL;
  gboolean       valid_utf8  = FALSE;
541
  gint           i;
Elliot Lee's avatar
Elliot Lee committed
542

543
  /*  Argument checking
Elliot Lee's avatar
Elliot Lee committed
544
545
546
547
548
549
550
551
   *   --only sanity check arguments when the procedure requests a menu path
   */

  if (proc_install->menu_path)
    {
      if (strncmp (proc_install->menu_path, "<Toolbox>", 9) == 0)
	{
	  if ((proc_install->nparams < 1) ||
552
	      (proc_install->params[0].type != GIMP_PDB_INT32))
Elliot Lee's avatar
Elliot Lee committed
553
	    {
Michael Natterer's avatar
Michael Natterer committed
554
	      g_message ("Plug-In \"%s\"\n(%s)\n\n"
555
			 "attempted to install <Toolbox> procedure \"%s\" "
556
557
558
			 "which does not take the standard <Toolbox> Plug-In "
                         "args.\n"
                         "(INT32)",
559
560
                         gimp_filename_to_utf8 (plug_in->name),
			 gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
561
                         proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
562
563
564
565
566
567
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Image>", 7) == 0)
	{
	  if ((proc_install->nparams < 3) ||
568
569
570
	      (proc_install->params[0].type != GIMP_PDB_INT32) ||
	      (proc_install->params[1].type != GIMP_PDB_IMAGE) ||
	      (proc_install->params[2].type != GIMP_PDB_DRAWABLE))
Elliot Lee's avatar
Elliot Lee committed
571
	    {
Michael Natterer's avatar
Michael Natterer committed
572
573
574
	      g_message ("Plug-In \"%s\"\n(%s)\n\n"
                         "attempted to install <Image> procedure \"%s\" "
                         "which does not take the standard <Image> Plug-In "
575
576
                         "args.\n"
                         "(INT32, IMAGE, DRAWABLE)",
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);
Elliot Lee's avatar
Elliot Lee committed
580
581
582
583
584
585
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Load>", 6) == 0)
	{
	  if ((proc_install->nparams < 3) ||
586
587
588
	      (proc_install->params[0].type != GIMP_PDB_INT32) ||
	      (proc_install->params[1].type != GIMP_PDB_STRING) ||
	      (proc_install->params[2].type != GIMP_PDB_STRING))
Elliot Lee's avatar
Elliot Lee committed
589
	    {
Michael Natterer's avatar
Michael Natterer committed
590
591
592
	      g_message ("Plug-In \"%s\"\n(%s)\n\n"
                         "attempted to install <Load> procedure \"%s\" "
                         "which does not take the standard <Load> Plug-In "
593
594
                         "args.\n"
                         "(INT32, STRING, STRING)",
595
596
                         gimp_filename_to_utf8 (plug_in->name),
			 gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
597
                         proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
598
599
600
601
602
603
	      return;
	    }
	}
      else if (strncmp (proc_install->menu_path, "<Save>", 6) == 0)
	{
	  if ((proc_install->nparams < 5) ||
604
605
	      (proc_install->params[0].type != GIMP_PDB_INT32)    ||
	      (proc_install->params[1].type != GIMP_PDB_IMAGE)    ||
606
	      (proc_install->params[2].type != GIMP_PDB_DRAWABLE) ||
607
	      (proc_install->params[3].type != GIMP_PDB_STRING)   ||
608
	      (proc_install->params[4].type != GIMP_PDB_STRING))
Elliot Lee's avatar
Elliot Lee committed
609
	    {
Michael Natterer's avatar
Michael Natterer committed
610
611
612
	      g_message ("Plug-In \"%s\"\n(%s)\n\n"
                         "attempted to install <Save> procedure \"%s\" "
                         "which does not take the standard <Save> Plug-In "
613
614
                         "args.\n"
                         "(INT32, IMAGE, DRAWABLE, STRING, STRING)",
615
616
                         gimp_filename_to_utf8 (plug_in->name),
			 gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
617
                         proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
618
619
620
621
622
	      return;
	    }
	}
      else
	{
Michael Natterer's avatar
Michael Natterer committed
623
624
625
626
627
	  g_message ("Plug-In \"%s\"\n(%s)\n\n"
                     "attempted to install procedure \"%s\" "
                     "in an invalid menu location.\n"
                     "Use either \"<Toolbox>\", \"<Image>\", "
                     "\"<Load>\", or \"<Save>\".",
628
629
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
630
                     proc_install->name);
Elliot Lee's avatar
Elliot Lee committed
631
632
633
634
	  return;
	}
    }

635
  /*  Sanity check for array arguments  */
Elliot Lee's avatar
Elliot Lee committed
636

637
  for (i = 1; i < proc_install->nparams; i++)
Elliot Lee's avatar
Elliot Lee committed
638
    {
639
      if ((proc_install->params[i].type == GIMP_PDB_INT32ARRAY ||
640
	   proc_install->params[i].type == GIMP_PDB_INT8ARRAY  ||
641
	   proc_install->params[i].type == GIMP_PDB_FLOATARRAY ||
642
	   proc_install->params[i].type == GIMP_PDB_STRINGARRAY)
643
          &&
644
	  proc_install->params[i-1].type != GIMP_PDB_INT32)
Elliot Lee's avatar
Elliot Lee committed
645
	{
Michael Natterer's avatar
Michael Natterer committed
646
647
648
649
	  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.",
650
651
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
652
                     proc_install->name, i);
Elliot Lee's avatar
Elliot Lee committed
653
654
655
	  return;
	}
    }
656
657
658

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

659
  if ((proc_install->menu_path == NULL ||
660
661
       g_utf8_validate (proc_install->menu_path, -1, NULL)) &&
      (g_utf8_validate (proc_install->name, -1, NULL))      &&
662
      (proc_install->blurb == NULL ||
663
       g_utf8_validate (proc_install->blurb, -1, NULL))     &&
664
      (proc_install->help == NULL ||
665
666
667
       g_utf8_validate (proc_install->help, -1, NULL))      &&
      (proc_install->author == NULL ||
       g_utf8_validate (proc_install->author, -1, NULL))    &&
668
      (proc_install->copyright == NULL ||
669
670
671
672
       g_utf8_validate (proc_install->copyright, -1, NULL)) &&
      (proc_install->date == NULL ||
       g_utf8_validate (proc_install->date, -1, NULL)))
    {
673
      valid_utf8 = TRUE;
674

675
      for (i = 0; i < proc_install->nparams && valid_utf8; i++)
676
677
        {
          if (! (g_utf8_validate (proc_install->params[i].name, -1, NULL) &&
678
                 (proc_install->params[i].description == NULL ||
679
                  g_utf8_validate (proc_install->params[i].description, -1, NULL))))
680
            valid_utf8 = FALSE;
681
        }
682

683
      for (i = 0; i < proc_install->nreturn_vals && valid_utf8; i++)
684
685
686
687
        {
          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))))
688
            valid_utf8 = FALSE;
689
690
        }
    }
691

692
  if (! valid_utf8)
693
    {
Michael Natterer's avatar
Michael Natterer committed
694
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
695
                 "attempted to install a procedure with invalid UTF-8 strings.",
696
697
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
698
699
      return;
    }
Elliot Lee's avatar
Elliot Lee committed
700

701
702
  /*  Initialization  */

Elliot Lee's avatar
Elliot Lee committed
703
704
  switch (proc_install->type)
    {
705
706
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
707
708
      plug_in_def = plug_in->plug_in_def;
      prog        = plug_in_def->prog;
Elliot Lee's avatar
Elliot Lee committed
709
710
711
712

      tmp = plug_in_def->proc_defs;
      break;

713
    case GIMP_TEMPORARY:
714
715
      plug_in_def = NULL;
      prog        = "none";
Elliot Lee's avatar
Elliot Lee committed
716

717
      tmp = plug_in->temp_proc_defs;
Elliot Lee's avatar
Elliot Lee committed
718
719
720
721
722
723
724
725
726
727
      break;
    }

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

      if (strcmp (proc_def->db_info.name, proc_install->name) == 0)
	{
728
          switch (proc_install->type)
729
            {
730
731
            case GIMP_PLUGIN:
            case GIMP_EXTENSION:
732
733
734
              plug_in_def->proc_defs = g_slist_remove (plug_in_def->proc_defs,
                                                       proc_def);
              plug_in_proc_def_free (proc_def);
735
736
737
738
739
740
741
              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;
742
            }
743

Elliot Lee's avatar
Elliot Lee committed
744
745
746
747
	  break;
	}
    }

748
  proc_def = plug_in_proc_def_new ();
Elliot Lee's avatar
Elliot Lee committed
749

750
  proc_def->prog            = g_strdup (prog);
751
752
753
754
755
756
  proc_def->menu_path       = g_strdup (proc_install->menu_path);
  proc_def->accelerator     = NULL;
  proc_def->extensions      = NULL;
  proc_def->prefixes        = NULL;
  proc_def->magics          = NULL;
  proc_def->image_types     = g_strdup (proc_install->image_types);
757
  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
758
  /* Install temp one use todays time */
759
  proc_def->mtime           = time (NULL);
Elliot Lee's avatar
Elliot Lee committed
760

761
762
  /*  The procedural database procedure  */

763
764
  proc = &proc_def->db_info;

765
766
767
768
  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
769
  proc->copyright = g_strdup (proc_install->copyright);
770
  proc->date      = g_strdup (proc_install->date);
Elliot Lee's avatar
Elliot Lee committed
771
772
  proc->proc_type = proc_install->type;

773
  proc->num_args   = proc_install->nparams;
Elliot Lee's avatar
Elliot Lee committed
774
775
  proc->num_values = proc_install->nreturn_vals;

776
777
  proc->args   = g_new0 (ProcArg, proc->num_args);
  proc->values = g_new0 (ProcArg, proc->num_values);
Elliot Lee's avatar
Elliot Lee committed
778
779
780

  for (i = 0; i < proc->num_args; i++)
    {
781
782
      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
783
784
785
786
787
      proc->args[i].description = g_strdup (proc_install->params[i].description);
    }

  for (i = 0; i < proc->num_values; i++)
    {
788
789
      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
790
791
792
793
794
      proc->values[i].description = g_strdup (proc_install->return_vals[i].description);
    }

  switch (proc_install->type)
    {
795
796
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
797
798
      plug_in_def->proc_defs = g_slist_prepend (plug_in_def->proc_defs,
                                                proc_def);
Elliot Lee's avatar
Elliot Lee committed
799
800
      break;

801
    case GIMP_TEMPORARY:
802
803
      plug_in->temp_proc_defs = g_slist_prepend (plug_in->temp_proc_defs,
                                                 proc_def);
Elliot Lee's avatar
Elliot Lee committed
804

805
      proc->exec_method.temporary.plug_in = plug_in;
Elliot Lee's avatar
Elliot Lee committed
806

807
      plug_ins_temp_proc_def_add (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
808
809
810
811
812
      break;
    }
}

static void
813
plug_in_handle_proc_uninstall (PlugIn          *plug_in,
814
                               GPProcUninstall *proc_uninstall)
Elliot Lee's avatar
Elliot Lee committed
815
{
816
  GSList *tmp;
Elliot Lee's avatar
Elliot Lee committed
817

Michael Natterer's avatar
Michael Natterer committed
818
  for (tmp = plug_in->temp_proc_defs; tmp; tmp = g_slist_next (tmp))
Elliot Lee's avatar
Elliot Lee committed
819
    {
820
821
      PlugInProcDef *proc_def;

Elliot Lee's avatar
Elliot Lee committed
822
823
      proc_def = tmp->data;

Michael Natterer's avatar
Michael Natterer committed
824
      if (! strcmp (proc_def->db_info.name, proc_uninstall->name))
Elliot Lee's avatar
Elliot Lee committed
825
	{
826
827
	  plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                    proc_def);
828
	  plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
829
830
831
832
	  break;
	}
    }
}
833

Michael Natterer's avatar
Michael Natterer committed
834
835
836
static void
plug_in_handle_extension_ack (PlugIn *plug_in)
{
837
  if (plug_in->ext_main_loop)
838
    {
839
      g_main_loop_quit (plug_in->ext_main_loop);
840
841
842
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
843
844
845
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent an EXTENSION_ACK message while not being started "
                 "as extension (should not happen)",
846
847
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
848
849
      plug_in_close (plug_in, TRUE);
    }
Michael Natterer's avatar
Michael Natterer committed
850
851
}

852
static void
853
plug_in_handle_has_init (PlugIn *plug_in)
854
{
855
  if (plug_in->query)
856
857
858
859
860
    {
      plug_in_def_set_has_init (plug_in->plug_in_def, TRUE);
    }
  else
    {
Michael Natterer's avatar
Michael Natterer committed
861
862
863
      g_message ("Plug-In \"%s\"\n(%s)\n\n"
		 "sent an HAS_INIT message while not in query() "
                 "(should not happen)",
864
865
                 gimp_filename_to_utf8 (plug_in->name),
		 gimp_filename_to_utf8 (plug_in->prog));
866
867
      plug_in_close (plug_in, TRUE);
    }
868
}