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
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.
   */
Michael Natterer's avatar
Michael Natterer committed
397
  return_vals = procedural_db_execute (plug_in->gimp, plug_in->context,
398
                                       proc_name, args);
399

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

  if (return_vals)
    {
404
405
      GPProcReturn proc_return;

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

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

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

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

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

444
445
      blocked = g_new0 (PlugInBlocked, 1);

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

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

static void
454
455
plug_in_handle_proc_return_priv (PlugIn       *plug_in,
                                 GPProcReturn *proc_return)
Elliot Lee's avatar
Elliot Lee committed
456
{
457
  if (plug_in->recurse_main_loop || plug_in->temp_main_loops)
Elliot Lee's avatar
Elliot Lee committed
458
    {
459
460
461
462
      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
463
464
465
    }
  else
    {
466
467
468
      GSList *list;

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

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

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

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

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

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

  plug_in_close (plug_in, FALSE);
}

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

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

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

542
  /*  Argument checking
Elliot Lee's avatar
Elliot Lee committed
543
544
545
   *   --only sanity check arguments when the procedure requests a menu path
   */

546
  if (proc_install->menu_path && proc_install->menu_path[0] == '<')
Elliot Lee's avatar
Elliot Lee committed
547
    {
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
      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
565
566
    }

567
  /*  Sanity check for array arguments  */
Elliot Lee's avatar
Elliot Lee committed
568

569
  for (i = 1; i < proc_install->nparams; i++)
Elliot Lee's avatar
Elliot Lee committed
570
    {
571
      if ((proc_install->params[i].type == GIMP_PDB_INT32ARRAY ||
572
	   proc_install->params[i].type == GIMP_PDB_INT8ARRAY  ||
573
	   proc_install->params[i].type == GIMP_PDB_FLOATARRAY ||
574
	   proc_install->params[i].type == GIMP_PDB_STRINGARRAY)
575
          &&
576
	  proc_install->params[i-1].type != GIMP_PDB_INT32)
Elliot Lee's avatar
Elliot Lee committed
577
	{
Michael Natterer's avatar
Michael Natterer committed
578
579
580
581
	  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.",
582
583
                     gimp_filename_to_utf8 (plug_in->name),
		     gimp_filename_to_utf8 (plug_in->prog),
Michael Natterer's avatar
Michael Natterer committed
584
                     proc_install->name, i);
Elliot Lee's avatar
Elliot Lee committed
585
586
587
	  return;
	}
    }
588
589
590

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

591
  if ((proc_install->menu_path == NULL ||
592
593
       g_utf8_validate (proc_install->menu_path, -1, NULL)) &&
      (g_utf8_validate (proc_install->name, -1, NULL))      &&
594
      (proc_install->blurb == NULL ||
595
       g_utf8_validate (proc_install->blurb, -1, NULL))     &&
596
      (proc_install->help == NULL ||
597
598
599
       g_utf8_validate (proc_install->help, -1, NULL))      &&
      (proc_install->author == NULL ||
       g_utf8_validate (proc_install->author, -1, NULL))    &&
600
      (proc_install->copyright == NULL ||
601
602
603
604
       g_utf8_validate (proc_install->copyright, -1, NULL)) &&
      (proc_install->date == NULL ||
       g_utf8_validate (proc_install->date, -1, NULL)))
    {
605
      valid_utf8 = TRUE;
606

607
      for (i = 0; i < proc_install->nparams && valid_utf8; i++)
608
609
        {
          if (! (g_utf8_validate (proc_install->params[i].name, -1, NULL) &&
610
                 (proc_install->params[i].description == NULL ||
611
                  g_utf8_validate (proc_install->params[i].description, -1, NULL))))
612
            valid_utf8 = FALSE;
613
        }
614

615
      for (i = 0; i < proc_install->nreturn_vals && valid_utf8; i++)
616
617
618
619
        {
          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))))
620
            valid_utf8 = FALSE;
621
622
        }
    }
623

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

633
634
  /*  Initialization  */

Elliot Lee's avatar
Elliot Lee committed
635
636
  switch (proc_install->type)
    {
637
638
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
639
640
      plug_in_def = plug_in->plug_in_def;
      prog        = plug_in_def->prog;
Elliot Lee's avatar
Elliot Lee committed
641
642
643
644

      tmp = plug_in_def->proc_defs;
      break;

645
    case GIMP_TEMPORARY:
646
647
      plug_in_def = NULL;
      prog        = "none";
Elliot Lee's avatar
Elliot Lee committed
648

649
      tmp = plug_in->temp_proc_defs;
Elliot Lee's avatar
Elliot Lee committed
650
651
652
653
654
655
656
657
658
659
      break;
    }

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

      if (strcmp (proc_def->db_info.name, proc_install->name) == 0)
	{
660
          switch (proc_install->type)
661
            {
662
663
            case GIMP_PLUGIN:
            case GIMP_EXTENSION:
664
665
666
              plug_in_def->proc_defs = g_slist_remove (plug_in_def->proc_defs,
                                                       proc_def);
              plug_in_proc_def_free (proc_def);
667
668
669
670
671
672
673
              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;
674
            }
675

Elliot Lee's avatar
Elliot Lee committed
676
677
678
679
	  break;
	}
    }

680
  proc_def = plug_in_proc_def_new ();
Elliot Lee's avatar
Elliot Lee committed
681

682
  if (proc_install->menu_path)
683
684
685
686
687
688
689
690
    {
      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);
    }
691

692
  proc_def->prog            = g_strdup (prog);
693
694
695
696
  proc_def->extensions      = NULL;
  proc_def->prefixes        = NULL;
  proc_def->magics          = NULL;
  proc_def->image_types     = g_strdup (proc_install->image_types);
697
  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
698
  /* Install temp one use todays time */
699
  proc_def->mtime           = time (NULL);
Elliot Lee's avatar
Elliot Lee committed
700

701
702
703
  /* Remember if this proc was installed while initing a plug-in */
  proc_def->installed_during_init = plug_in->init;

704
705
  /*  The procedural database procedure  */

706
707
  proc = &proc_def->db_info;

708
709
710
711
  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
712
  proc->copyright = g_strdup (proc_install->copyright);
713
  proc->date      = g_strdup (proc_install->date);
Elliot Lee's avatar
Elliot Lee committed
714
715
  proc->proc_type = proc_install->type;

716
  proc->num_args   = proc_install->nparams;
Elliot Lee's avatar
Elliot Lee committed
717
718
  proc->num_values = proc_install->nreturn_vals;

719
720
  proc->args   = g_new0 (ProcArg, proc->num_args);
  proc->values = g_new0 (ProcArg, proc->num_values);
Elliot Lee's avatar
Elliot Lee committed
721
722
723

  for (i = 0; i < proc->num_args; i++)
    {
724
725
      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
726
727
728
729
730
      proc->args[i].description = g_strdup (proc_install->params[i].description);
    }

  for (i = 0; i < proc->num_values; i++)
    {
731
732
      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
733
734
735
736
737
      proc->values[i].description = g_strdup (proc_install->return_vals[i].description);
    }

  switch (proc_install->type)
    {
738
739
    case GIMP_PLUGIN:
    case GIMP_EXTENSION:
740
741
      plug_in_def->proc_defs = g_slist_prepend (plug_in_def->proc_defs,
                                                proc_def);
Elliot Lee's avatar
Elliot Lee committed
742
743
      break;

744
    case GIMP_TEMPORARY:
745
746
      plug_in->temp_proc_defs = g_slist_prepend (plug_in->temp_proc_defs,
                                                 proc_def);
Elliot Lee's avatar
Elliot Lee committed
747

748
      proc->exec_method.temporary.plug_in = plug_in;
Elliot Lee's avatar
Elliot Lee committed
749

750
      plug_ins_temp_proc_def_add (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
751
752
753
754
755
      break;
    }
}

static void
756
plug_in_handle_proc_uninstall (PlugIn          *plug_in,
757
                               GPProcUninstall *proc_uninstall)
Elliot Lee's avatar
Elliot Lee committed
758
{
759
  GSList *tmp;
Elliot Lee's avatar
Elliot Lee committed
760

Michael Natterer's avatar
Michael Natterer committed
761
  for (tmp = plug_in->temp_proc_defs; tmp; tmp = g_slist_next (tmp))
Elliot Lee's avatar
Elliot Lee committed
762
    {
763
764
      PlugInProcDef *proc_def;

Elliot Lee's avatar
Elliot Lee committed
765
766
      proc_def = tmp->data;

Michael Natterer's avatar
Michael Natterer committed
767
      if (! strcmp (proc_def->db_info.name, proc_uninstall->name))
Elliot Lee's avatar
Elliot Lee committed
768
	{
769
770
	  plug_in->temp_proc_defs = g_slist_remove (plug_in->temp_proc_defs,
                                                    proc_def);
771
	  plug_ins_temp_proc_def_remove (plug_in->gimp, proc_def);
Elliot Lee's avatar
Elliot Lee committed
772
773
774
775
	  break;
	}
    }
}
776

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

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