-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgimp-dbus.c
1827 lines (1604 loc) · 58.3 KB
/
gimp-dbus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* gimp-dbus
* A GIMP plugin that serves PDB calls (and other related calls) over
* DBus.
*
* Copyright (c) 2012-13 Alexandra Greenberg, Mark Lewis, Evan Manuella,
* Samuel Rebelsky, Hart Russell, Mani Tiwaree, and Christine Tran
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published
* by the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
*/
// +----------+--------------------------------------------------------
// | Manifest |
// +----------+
// +---------+---------------------------------------------------------
// | Headers |
// +---------+
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "tile-stream.h"
// +-----------+-------------------------------------------------------
// | Constants |
// +-----------+
/**
* The "about" message.
*/
#define GIMP_DBUS_ABOUT "Glimmer Labs' Gimp D-Bus plugin version 0.0.8"
/**
* The service name that we use for gimp-dbus.
*/
#define GIMP_DBUS_SERVICE "edu.grinnell.cs.glimmer.GimpDBus"
/**
* The standard object that we use for gimp-dbus.
*/
#define GIMP_DBUS_APPLICATION_OBJECT "/edu/grinnell/cs/glimmer/gimp"
/**
* The PDB interface.
*/
#define GIMP_DBUS_INTERFACE_PDB "edu.grinnell.cs.glimmer.pdb"
/**
* The additional interface.
*/
#define GIMP_DBUS_INTERFACE_ADDITIONAL "edu.grinnell.cs.glimmer.gimpplus"
/**
* Where we put this service in the menu.
*/
#define GIMP_DBUS_MENU "<Toolbox>/MediaScript/"
// +--------+----------------------------------------------------------
// | Macros |
// +--------+
/**
* (Optionally) Print a log message.
*
* Plugins can be hard to debug, so we sometimes print log messages to
* see what's happening. LOG works like printf when the DEBUG flag is
* set and is a noop when the flag is not set.
*
* To turn on log messages, compile with the DEBUG flag set.
*/
#ifdef DEBUG
#define LOG(...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
#define BEGIN(FUN) fprintf (stderr, "BEGIN[%s]\n", FUN);
#define END(FUN) fprintf (stderr, "END[%s]\n", FUN);
#else
#define LOG(...) do { } while (0)
#define BEGIN(FUN) do { } while (0)
#define END(FUN) do { } while (0)
#endif
/**
* Signal an error having to do with an argument.
*/
#define SIGNAL_ARGUMENT_ERROR(INVOCATION, MESSAGE, ...) \
g_dbus_method_invocation_return_error ( \
INVOCATION, \
G_IO_ERROR, \
G_IO_ERROR_INVALID_ARGUMENT, \
MESSAGE, \
##__VA_ARGS__)
/**
* Signal a more general error. (Unfortunately, I don't know what other
* parameters to use.
*/
#define SIGNAL_ERROR(INVOCATION, MESSAGE, ...) \
g_dbus_method_invocation_return_error ( \
INVOCATION, \
G_IO_ERROR, \
G_IO_ERROR_INVALID_ARGUMENT, \
MESSAGE, \
##__VA_ARGS__)
// +-------+-----------------------------------------------------------
// | Types |
// +-------+
/**
* A simple way to store both the procedure names and a count of the
* procedure names.
*/
struct gimpnames {
gchar **procnames;
gint nprocs;
};
/**
* A simple dbus message handler.
*/
typedef void (*SimpleMessageHandler)(const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters);
/**
* An entry in a table of message handlers. We terminate the table
* with an entry whose name is NULL.
*/
struct HandlerEntry
{
char *name;
SimpleMessageHandler handler;
};
typedef struct HandlerEntry HandlerEntry;
/**
* An entry in a table of GIMP run handlers. We terminate the table
* with an entry whose name is NULL.
*/
struct RunTableEntry
{
char *name;
GimpRunProc proc;
};
typedef struct RunTableEntry RunTableEntry;
// +-----------------+------------------------------------------------
// | Predeclarations |
// +-----------------+
/**
* Handle a call to method_name on connection, using the
* specified parameters. Send the result of the call to
* invocation.
*/
int gimp_dbus_handle_pdb_method_call (GDBusConnection *connection,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation);
/**
* GIMP plugin query.
*/
static void query (void);
/**
* GIMP plugin run.
*/
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
/**
* DBus method call handler for PDB methods.
*/
static void pdb_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data);
/**
* DBus get-propery handler for PDB properties.
*/
static GVariant *pdb_handle_get_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data);
/**
* DBus set-property handler for PDB properties.
*/
static gboolean pdb_handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data);
/**
* DBus method call handler for alternate methods.
*/
static void alt_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data);
/**
* DBus get-propery handler for alternate properties.
*/
static GVariant *alt_handle_get_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data);
/**
* DBus set-property handler for alternate properties.
*/
static gboolean alt_handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data);
/**
* Build information on one argument to a method. Returns NULL if
* it is unable to build.
*/
static GDBusArgInfo *
g_dbus_arg_new (gchar *name,
const gchar *signature,
GDBusAnnotationInfo **annotations);
// +---------+--------------------------------------------------------
// | Globals |
// +---------+
/**
* The XML to describe the additional services that we provide.
*/
static const gchar alt_introspection_xml[] =
"<node>"
" <interface name='" GIMP_DBUS_INTERFACE_ADDITIONAL "'>"
" <method name='ggimp_about'>"
" <arg type='s' name='result' direction='out'/>"
" </method>"
" <method name='ggimp_quit'>"
" </method>"
" <method name='ggimp_rgb_red'>"
" <arg type='i' name='color' direction='in'/>"
" <arg type='i' name='red' direction='out'/>"
" </method>"
" <method name='tile_stream_advance'>"
" <arg type='i' name='stream' direction='in'/>"
" <arg type='i' name='continues' direction='out'/>"
" </method>"
" <method name='tile_stream_close'>"
" <arg type='i' name='stream' direction='in'/>"
" </method>"
" <method name='tile_stream_get'>"
" <arg type='i' name='stream' direction='in'/>"
" <arg type='i' name='size' direction='out'/>"
" <arg type='ay' name='data' direction='out'/>"
" <arg type='i' name='bpp' direction='out'/>"
" <arg type='i' name='rowstride' direction='out'/>"
" <arg type='i' name='x' direction='out'/>"
" <arg type='i' name='y' direction='out'/>"
" <arg type='i' name='width' direction='out'/>"
" <arg type='i' name='height' direction='out'/>"
" </method>"
" <method name='tile_stream_is_valid'>"
" <arg type='i' name='stream' direction='in'/>"
" <arg type='i' name='valid' direction='out'/>"
" </method>"
" <method name='tile_stream_new'>"
" <arg type='i' name='image' direction='in'/>"
" <arg type='i' name='drawable' direction='in'/>"
" <arg type='i' name='stream' direction='out'/>"
" </method>"
" <method name='tile_update'>"
" <arg type='i' name='stream' direction='in'/>"
" <arg type='i' name='size' direction='in'/>"
" <arg type='ay' name='data' direction='in'/>"
" <arg type='i' name='success' direction='out'/>"
" </method>"
" </interface>"
"</node>";
static const GDBusNodeInfo *alt_introspection_data = NULL;
/**
* The GDBusNodeInfo on the PDB to be published to the dbus.
*/
static GDBusNodeInfo *pdbnode = NULL;
/**
* Information on the registration id for the PDB interface.
*/
static guint pdb_registration_id;
/**
* Information on the registration id for the alternate interface.
*/
static guint alt_registration_id;
/**
* The standard DBus handlers for PDB.
*/
static const GDBusInterfaceVTable pdb_interface_vtable =
{
pdb_handle_method_call,
pdb_handle_get_property,
pdb_handle_set_property
};
/**
* The standard DBus handlers for alternate functions.
*/
static const GDBusInterfaceVTable alt_interface_vtable =
{
alt_handle_method_call,
alt_handle_get_property,
alt_handle_set_property
};
/**
* The event loop.
*/
GMainLoop *loop = NULL;
// +----------------------------+--------------------------------------
// | Support for Error Checking |
// +----------------------------+
/*
Sample code for error checking. (This code may not be necessary if
DBus does the checking for us.
int nparams = g_variant_n_children (parameters);
// Check that we have one parameter and that it's an integer.
if (nparams != 1)
{
report_invalid_paramcount (invocation, "FUN", 1, nparams);
return;
}
GVariant *child = g_variant_get_child_value (parameters, 0);
if (g_variant_get_type (child) != != G_VARIANT_TYPE_INT32)
{
report_invalid_parameter (invocation, "FUN", 0, "integer",
child);
}
*/
/**
* Return an error about a particular parameter.
*/
static void
report_invalid_parameter (GDBusMethodInvocation *invocation,
const gchar *method_name,
int paramnum,
const gchar *paramtype,
GVariant *param)
{
SIGNAL_ARGUMENT_ERROR (
invocation,
"%s expects %s for parameter %d, received %s",
method_name, paramtype, paramnum, g_variant_get_type_string);
} // report_invalid_parameter
/**
* Return an error about the number of parameters.
*/
static void
report_invalid_paramcount (GDBusMethodInvocation *invocation,
const gchar *method_name,
int expected,
int actual)
{
if (expected == 1)
{
SIGNAL_ARGUMENT_ERROR (invocation,
"%s expects 1 parameter, received %d",
method_name, actual);
} // if we expected one parameter
else
{
SIGNAL_ARGUMENT_ERROR (invocation,
"%s expects %d parameters, received %d",
method_name, expected, actual);
} // if we expected multiple parameters
} // report_invalid_paramcount
/**
* Make sure that a tile stream sent to a handler is valid. If not,
* return an error (which should stop the handler).
*/
static int
handler_validate_tile_stream (int stream, GDBusMethodInvocation *invocation)
{
if (! tile_stream_is_valid (stream))
{
LOG ("tile-stream-get: Invalid tile stream: %d", stream);
SIGNAL_ERROR (invocation, "could not create stream");
return 0;
} // if tile stream is invalid
return 1;
} // handler_validate_tile_stream
// +---------------------------------+---------------------------------
// | Methods for Alternate Interface |
// +---------------------------------+
void
ggimp_dbus_handle_about (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
GVariant *result = g_variant_new ("(s)", GIMP_DBUS_ABOUT);
g_dbus_method_invocation_return_value (invocation, result);
} // ggimp_dbus_handle_about
void
ggimp_dbus_handle_default (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
SIGNAL_ARGUMENT_ERROR (invocation, "Invalid method: '%s'", method_name);
} // ggimp_dbus_handle_default
void
ggimp_dbus_handle_quit (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
g_main_loop_quit (loop);
} // ggimp_dbus_handle_quit
void
ggimp_dbus_handle_rgb_red (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameter
GVariant *param = g_variant_get_child_value (parameters, 0);
// Grab the integer
int color = g_variant_get_int32 (param);
// Extract the red component
int red = (color >> 16) & 255;
// Convert it back to a GVariant
GVariant *result = g_variant_new ("(i)", red);
// And return it
g_dbus_method_invocation_return_value (invocation, result);
} // gimp_gbus_handle_rgb_red
void
ggimp_dbus_handle_tile_stream_advance (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int stream =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
// Validate
if (! handler_validate_tile_stream (stream, invocation))
return;
// Advance and return
GVariant *result = g_variant_new ("(i)", tile_stream_advance (stream));
g_dbus_method_invocation_return_value (invocation, result);
} // ggimp_dbus_handle_tile_stream_advance
void
ggimp_dbus_handle_tile_stream_close (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int stream =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
// Validate
if (! handler_validate_tile_stream (stream, invocation))
return;
// Close and return
tile_stream_close (stream);
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
} // ggimp_dbus_handle_tile_stream_close
void
ggimp_dbus_handle_tile_stream_get (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int stream =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
// Validate
if (! handler_validate_tile_stream (stream, invocation))
return;
// Get the region
GimpPixelRgn *rgn = tile_stream_get (stream);
if (rgn == NULL)
{
LOG ("tile-stream-get: Failed to get tile.\n");
SIGNAL_ERROR (invocation, "could not get tile");
return;
} // if the region is null
// Grab the bytes
int size = rgn->rowstride * rgn->h;
GVariant *bytes = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
rgn->data,
size,
sizeof (guint8));
// Build the return value
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
g_variant_builder_add_value (&builder, g_variant_new_int32 (size));
g_variant_builder_add_value (&builder, bytes);
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->bpp));
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->rowstride));
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->x));
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->y));
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->w));
g_variant_builder_add_value (&builder, g_variant_new_int32 (rgn->h));
// And we're done
g_dbus_method_invocation_return_value (invocation,
g_variant_builder_end (&builder));
} // ggimp_dbus_handle_tile_stream_get
void
ggimp_dbus_handle_tile_stream_is_valid (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int stream =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
// Do the computation and return
GVariant *result = g_variant_new ("(i)", tile_stream_is_valid (stream));
g_dbus_method_invocation_return_value (invocation, result);
} // ggimp_dbus_handle_tile_stream_is_valid
void
ggimp_dbus_handle_tile_stream_new (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int image =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
int drawable =
g_variant_get_int32 (g_variant_get_child_value (parameters, 1));
// Validate the parameters
if (! gimp_image_is_valid (image))
{
LOG ("tile_stream_new: invalid image %d\n", image);
report_invalid_parameter (invocation,
"tile_stream_new",
1,
"image",
g_variant_get_child_value (parameters, 0));
return;
} // if it's an invalid image
if (! gimp_drawable_is_valid (drawable))
{
LOG ("tile_stream_new: invalid drawable %d\n", drawable);
report_invalid_parameter (invocation,
"tile_stream_new",
2,
"drawable",
g_variant_get_child_value (parameters, 1));
return;
} // if it's an invalid drawable
if (gimp_drawable_get_image (drawable) != image)
{
LOG ("tile_stream_new: drawable %d does not match image %d\n",
drawable, image);
SIGNAL_ARGUMENT_ERROR (invocation, "drawable does not match image");
return;
} // if the items don't match.
// Build the tile stream
int stream = drawable_new_tile_stream (image, drawable);
if (! tile_stream_is_valid (stream))
{
SIGNAL_ERROR (invocation, "could not create stream");
return;
} // if tile stream is invalid
// Convert it back to a GVariant
GVariant *result = g_variant_new ("(i)", stream);
// And return it
g_dbus_method_invocation_return_value (invocation, result);
} // ggimp_dbus_handle_tile_stream_new
void
ggimp_dbus_handle_tile_update (const gchar *method_name,
GDBusMethodInvocation *invocation,
GVariant *parameters)
{
// Grab the parameters
int stream =
g_variant_get_int32 (g_variant_get_child_value (parameters, 0));
int size =
g_variant_get_int32 (g_variant_get_child_value (parameters, 1));
gsize realsize;
GVariant *wrapped_data = g_variant_get_child_value (parameters, 2);
guint8 *data = (guint8 *) g_variant_get_fixed_array (wrapped_data,
&realsize,
sizeof (guint8));
if (size > realsize)
{
SIGNAL_ARGUMENT_ERROR (invocation, "size > number of bytes");
return;
}
// Validate
if (! handler_validate_tile_stream (stream, invocation))
return;
// Call the underlying function
int result = tile_update (stream, size, data);
// And return
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(i)", result));
} // ggimp_dbus_handle_tile_update
// +------------------------+------------------------------------------
// | Standard DBus Handlers |
// +------------------------+
static void
alt_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
static HandlerEntry alt_handlers[] =
{
{ "ggimp_about", ggimp_dbus_handle_about },
{ "ggimp_quit", ggimp_dbus_handle_quit },
{ "ggimp_rgb_red", ggimp_dbus_handle_rgb_red },
{ "tile_stream_advance", ggimp_dbus_handle_tile_stream_advance },
{ "tile_stream_close", ggimp_dbus_handle_tile_stream_close },
{ "tile_stream_get", ggimp_dbus_handle_tile_stream_get },
{ "tile_stream_is_valid", ggimp_dbus_handle_tile_stream_is_valid },
{ "tile_stream_new", ggimp_dbus_handle_tile_stream_new },
{ "tile_update", ggimp_dbus_handle_tile_update },
{ NULL, ggimp_dbus_handle_default }
};
int i;
// Loop through the list of handlers, looking for one that matches.
for (i = 0; alt_handlers[i].name != NULL; i++)
{
if (g_strcmp0 (method_name, alt_handlers[i].name) == 0)
{
(*(alt_handlers[i].handler)) (method_name, invocation, parameters);
return;
} // if the name matches
} // for each handler
// If we've gotten this far, nothing has matched. Give up.
ggimp_dbus_handle_default (method_name, invocation, parameters);
} // alt_handle_method_call
static GVariant *
alt_handle_get_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// NULL signals failure
return NULL;
} // alt_handle_get_property
static gboolean
alt_handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// false signals failure.
return FALSE;
}// alt_handle_set_property
static void
pdb_handle_method_call (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
gimp_dbus_handle_pdb_method_call (connection,
method_name,
parameters,
invocation);
} // pdb_handle_method_call
static GVariant *
pdb_handle_get_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// NULL signals failure
return NULL;
} // pdb_handle_get_property
static gboolean
pdb_handle_set_property (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *property_name,
GVariant *value,
GError **error,
gpointer user_data)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"No property %s found in interface %s.",
property_name,
interface_name);
// false signals failure.
return FALSE;
}// pdb_handle_set_property
/**
* What to do when the bus is acquired.
*/
static void
on_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
pdb_registration_id =
g_dbus_connection_register_object (connection,
GIMP_DBUS_APPLICATION_OBJECT,
pdbnode->interfaces[0],
&pdb_interface_vtable,
NULL, /* user_data */
NULL, /* user_data_free_func */
NULL); /* GERROR */
alt_introspection_data =
g_dbus_node_info_new_for_xml (alt_introspection_xml, NULL);
if (alt_introspection_data == NULL)
{
// Hack!
fprintf (stderr, "Could not create alt_introspection_data\n");
exit (1);
}
alt_registration_id =
g_dbus_connection_register_object (connection,
GIMP_DBUS_APPLICATION_OBJECT,
alt_introspection_data->interfaces[0],
&alt_interface_vtable,
NULL, /* user_data */
NULL, /* user_data_free_func */
NULL); /* GERROR */
} // on_bus_acquired
/**
* What to do when the service name is acquired.
*/
static void
on_name_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
// Right now, we do nothing. But the standard in D-Bus is to have
// a callback, so it's in place in case we decide to do something
// in the future.
} // on_name_acquired
/**
* Handling the loss of a name on the bus.
*/
static void
on_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
LOG ("Lost name %s", name);
g_main_loop_quit (loop);
} // on_name_lost
// +-----------------+-------------------------------------------------
// | General Helpers |
// +-----------------+
/**
* Replace one character by another.
*/
gchar *
strrep (gchar *str, gchar target, gchar replacement)
{
gchar *tmp = str;
while ((tmp = strchr (tmp, target)) != NULL)
*tmp = replacement;
return str;
} // strrep
// +-----------------+-------------------------------------------------
// | Type Conversion |
// +-----------------+
/**
* Convert a GimpPDBArtType (type information for a GIMP parameter) to
* a GVariant signature (type information for GVariants).
*/
const GVariantType *
gimp_dbus_pdb_type_to_signature (GimpPDBArgType type)
{
const GVariantType *result;
switch (type)
{
case GIMP_PDB_COLOR:
result = G_VARIANT_TYPE_INT32;
break;
case GIMP_PDB_INT32:
result = G_VARIANT_TYPE_INT32;
break;
case GIMP_PDB_INT16:
result = G_VARIANT_TYPE_INT16;
break;
case GIMP_PDB_INT8:
result = G_VARIANT_TYPE_BYTE;
break;
case GIMP_PDB_FLOAT:
result = G_VARIANT_TYPE_DOUBLE;
break;
case GIMP_PDB_STRING:
result = G_VARIANT_TYPE_STRING;
break;
case GIMP_PDB_STRINGARRAY:
result = G_VARIANT_TYPE_STRING_ARRAY;
break;
case GIMP_PDB_INT32ARRAY:
result = ((const GVariantType *) "ai");
break;
case GIMP_PDB_INT16ARRAY:
result = ((const GVariantType *) "an");
break;
case GIMP_PDB_INT8ARRAY:
result = ((const GVariantType *) "ay");
break;
case GIMP_PDB_FLOATARRAY:
result = ((const GVariantType *) "ad");
break;
case GIMP_PDB_DISPLAY:
result = G_VARIANT_TYPE_INT32;
break;
case GIMP_PDB_IMAGE:
result = G_VARIANT_TYPE_INT32;
break;
case GIMP_PDB_LAYER:
result = G_VARIANT_TYPE_INT32;
break;
// We should have covered everything. To be safe, we have
// a default type of 32-bit integers. TODO: Pick a better
// default type.
default:
result = G_VARIANT_TYPE_INT32;
break;
} // switch
return result;
} // gimp_dbus_pdb_param_to_signature
/**
* Convert a GVariant to a GimpParam and make param point to it.
* Returns success/failure.
*/
gboolean
gimp_dbus_g_variant_to_gimp_param (GVariant *parameter,
GimpParamDef *paramdef,
GimpParam *param)
{
gsize size;
unsigned long nchildren;
int arraycounter = 0;
char* tempc;
gint32 temp32;
gint16 temp16;
gdouble tempd;
gchar** sarray = NULL;
gint32* array32 = NULL;
gint16* array16 = NULL;
gdouble* arrayd = NULL;
guchar r, g, b; //int rgb
GimpRGB tempRGB;
// Make sure that types match. FIX TO HANDLE MULTIPLE REPRESENTATIONS
// OF COLORS.
const gchar *paramtype =
(const gchar *) gimp_dbus_pdb_type_to_signature (paramdef->type);
if (! g_variant_type_equal (paramtype,
g_variant_get_type (parameter)))
{
LOG ("you're not converting types correctly");
return FALSE;
} // g_variant_type_equal
param->type = paramdef->type;
switch (paramdef->type)
{
// Special case: Colors. Need to convert from whatever
// type we received to a gimp_rgb. Right now, we only
// handle integers. (Will need fixing above to handle