summaryrefslogtreecommitdiff
path: root/plugins/fuse.c
blob: 281adc42bcae267b742ea943dcc2442fc130d887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
/*
 * Copyright © 2007 David Reveman
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <sys/mount.h>
#include <fuse.h>
#include <fuse_lowlevel.h>

#include <compiz-core.h>

static CompMetadata fuseMetadata;

#define FUSE_INODE_TYPE_ROOT        (1 << 0)
#define FUSE_INODE_TYPE_PLUGIN      (1 << 1)
#define FUSE_INODE_TYPE_SCREEN      (1 << 2)
#define FUSE_INODE_TYPE_DISPLAY     (1 << 3)
#define FUSE_INODE_TYPE_OPTION      (1 << 4)
#define FUSE_INODE_TYPE_TYPE        (1 << 5)
#define FUSE_INODE_TYPE_VALUE       (1 << 6)
#define FUSE_INODE_TYPE_ITEM_COUNT  (1 << 7)
#define FUSE_INODE_TYPE_ITEM_TYPE   (1 << 8)
#define FUSE_INODE_TYPE_ITEMS       (1 << 9)
#define FUSE_INODE_TYPE_ITEM_VALUE  (1 << 10)
#define FUSE_INODE_TYPE_MIN         (1 << 11)
#define FUSE_INODE_TYPE_MAX         (1 << 12)
#define FUSE_INODE_TYPE_PRECISION   (1 << 13)

#define DIR_MASK (FUSE_INODE_TYPE_ROOT    | \
		  FUSE_INODE_TYPE_PLUGIN  | \
		  FUSE_INODE_TYPE_SCREEN  | \
		  FUSE_INODE_TYPE_DISPLAY | \
		  FUSE_INODE_TYPE_OPTION  | \
		  FUSE_INODE_TYPE_ITEMS)

#define CONST_DIR_MASK (FUSE_INODE_TYPE_PLUGIN  | \
			FUSE_INODE_TYPE_SCREEN  | \
			FUSE_INODE_TYPE_DISPLAY | \
			FUSE_INODE_TYPE_OPTION)

#define WRITE_MASK (FUSE_INODE_TYPE_VALUE | \
		    FUSE_INODE_TYPE_ITEM_VALUE)

#define FUSE_INODE_FLAG_TRUNC (1 << 0)

typedef struct _FuseInode {
    struct _FuseInode *parent;
    struct _FuseInode *child;
    struct _FuseInode *sibling;

    int	       type;
    int	       flags;
    fuse_ino_t ino;
    char       *name;
} FuseInode;

typedef struct _FuseWriteBuffer {
    char *data;
    int  size;
    Bool dirty;
} FuseWriteBuffer;

static int displayPrivateIndex;

#define FUSE_DISPLAY_OPTION_MOUNT_POINT 0
#define FUSE_DISPLAY_OPTION_NUM		1

typedef struct _FuseDisplay {
    CompOption opt[FUSE_DISPLAY_OPTION_NUM];

    struct fuse_session *session;
    struct fuse_chan    *channel;
    char		*mountPoint;
    CompWatchFdHandle   watchFdHandle;
    char		*buffer;
} FuseDisplay;

#define GET_FUSE_DISPLAY(d)				     \
    ((FuseDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define FUSE_DISPLAY(d)			   \
    FuseDisplay *fd = GET_FUSE_DISPLAY (d)

#define NUM_OPTIONS(d) (sizeof ((d)->opt) / sizeof (CompOption))

static fuse_ino_t nextIno = 1;
static FuseInode  *inodes = NULL;

static FuseInode *
fuseAddInode (FuseInode	 *parent,
	      int	 type,
	      const char *name)
{
    FuseInode *inode;

    inode = malloc (sizeof (FuseInode));
    if (!inode)
	return NULL;

    inode->parent  = parent;
    inode->sibling = NULL;
    inode->child   = NULL;
    inode->type    = type;
    inode->flags   = 0;
    inode->ino     = nextIno++;
    inode->name	   = strdup (name);

    if (parent)
    {
	if (parent->child)
	    inode->sibling = parent->child;

	parent->child = inode;
    }

    return inode;
}

static void
fuseRemoveInode (FuseInode *parent,
		 FuseInode *inode)
{
    while (inode->child)
	fuseRemoveInode (inode, inode->child);

    if (parent)
    {
	FuseInode *s, *prev = NULL;

	for (s = parent->child; s; s = s->sibling)
	{
	    if (s == inode)
		break;

	    prev = s;
	}

	if (prev)
	    prev->sibling = inode->sibling;
	else
	    parent->child = NULL;
    }

    if (inode->name)
	free (inode->name);

    free (inode);
}

static FuseInode *
fuseFindInode (FuseInode  *inode,
	       fuse_ino_t ino,
	       int	  mask)
{
    if (inode->ino != ino)
    {
	FuseInode *c = inode->child;

	inode = NULL;
	while (c)
	{
	    inode = fuseFindInode (c, ino, ~0);
	    if (inode)
		break;

	    c = c->sibling;
	}
    }

    if (inode && (inode->type & mask))
	return inode;

    return NULL;
}

static FuseInode *
fuseLookupChild (FuseInode  *inode,
		 const char *name)
{
    FuseInode *c;

    for (c = inode->child; c; c = c->sibling)
	if (strcmp (c->name, name) == 0)
	    return c;

    return NULL;
}

static CompOption *
fuseGetDisplayOptionsFromInode (CompDisplay *d,
				FuseInode   *inode,
				int	    *nOption)
{
    CompOption *option = NULL;

    if (inode->type & FUSE_INODE_TYPE_PLUGIN)
    {
	CompPlugin *p;

	p = findActivePlugin (inode->name);
	if (p && p->vTable->getDisplayOptions)
	    option = (*p->vTable->getDisplayOptions) (p, d, nOption);
    }

    return option;
}

static CompOption *
fuseGetScreenOptionsFromInode (CompScreen *s,
			       FuseInode  *inode,
			       int	  *nOption)
{
    CompOption *option = NULL;

    if (inode->type & FUSE_INODE_TYPE_PLUGIN)
    {
	CompPlugin *p;

	p = findActivePlugin (inode->name);
	if (p && p->vTable->getScreenOptions)
	    option = (*p->vTable->getScreenOptions) (p, s, nOption);
    }

    return option;
}

static CompOption *
fuseGetOptionsFromInode (CompDisplay *d,
			 FuseInode   *inode,
			 int	     *nOption)
{
    CompOption *option = NULL;

    if (inode->type & FUSE_INODE_TYPE_SCREEN)
    {
	CompScreen *s;
	int	   screenNum = -1;

	sscanf (inode->name, "screen%d", &screenNum);

	for (s = d->screens; s; s = s->next)
	    if (s->screenNum == screenNum)
		break;

	if (s)
	    option = fuseGetScreenOptionsFromInode (s, inode->parent, nOption);
    }
    else if (inode->type & FUSE_INODE_TYPE_DISPLAY)
    {
	option = fuseGetDisplayOptionsFromInode (d, inode->parent, nOption);
    }

    return option;
}

static CompOption *
fuseGetOptionFromInode (CompDisplay *d,
			FuseInode   *inode)
{
    if (inode->type & (FUSE_INODE_TYPE_OPTION |
		       FUSE_INODE_TYPE_ITEMS))
    {
	CompOption *option;
	int	   nOption;

	if (inode->type & FUSE_INODE_TYPE_ITEMS)
	    inode = inode->parent;

	option = fuseGetOptionsFromInode (d, inode->parent, &nOption);
	if (option)
	{
	    while (nOption--)
	    {
		if (strcmp (inode->name, option->name) == 0)
		    return option;

		option++;
	    }
	}
    }

    return NULL;
}

static char *
fuseGetStringFromInode (CompDisplay *d,
			FuseInode   *inode)
{
    CompOption *option;
    char       str[256];

    if (!inode->parent)
	return NULL;

    option = fuseGetOptionFromInode (d, inode->parent);
    if (!option)
	return NULL;

    if (inode->flags & FUSE_INODE_FLAG_TRUNC)
	return strdup ("");

    if (inode->type & FUSE_INODE_TYPE_TYPE)
    {
	return strdup (optionTypeToString (option->type));
    }
    else if (inode->type & (FUSE_INODE_TYPE_VALUE | FUSE_INODE_TYPE_ITEM_VALUE))
    {
	CompOptionValue *value = NULL;
	CompOptionType  type;

	if (inode->type & FUSE_INODE_TYPE_ITEM_VALUE)
	{
	    int i;

	    if (sscanf (inode->name, "value%d", &i))
	    {
		if (i < option->value.list.nValue)
		{
		    value = &option->value.list.value[i];
		    type  = option->value.list.type;
		}
	    }
	}
	else
	{
	    value = &option->value;
	    type  = option->type;
	}

	if (value)
	{
	    switch (type) {
	    case CompOptionTypeBool:
		return strdup (value->b ? "true" : "false");
	    case CompOptionTypeInt:
		snprintf (str, 256, "%d", value->i);
		return strdup (str);
	    case CompOptionTypeFloat:
		snprintf (str, 256, "%f", value->f);
		return strdup (str);
	    case CompOptionTypeString:
		return strdup (value->s);
	    case CompOptionTypeColor:
		return colorToString (value->c);
	    case CompOptionTypeKey:
		return keyActionToString (d, &value->action);
	    case CompOptionTypeButton:
		return buttonActionToString (d, &value->action);
	    case CompOptionTypeEdge:
		return edgeMaskToString (value->action.edgeMask);
	    case CompOptionTypeBell:
		return strdup (value->action.bell ? "true" : "false");
	    case CompOptionTypeMatch:
		return matchToString (&value->match);
	    default:
		break;
	    }
	}
    }
    else if (inode->type & FUSE_INODE_TYPE_MIN)
    {
	if (option->type == CompOptionTypeInt)
	    snprintf (str, 256, "%d", option->rest.i.min);
	else
	    snprintf (str, 256, "%f", option->rest.f.min);

	return strdup (str);
    }
    else if (inode->type & FUSE_INODE_TYPE_MAX)
    {
	if (option->type == CompOptionTypeInt)
	    snprintf (str, 256, "%d", option->rest.i.max);
	else
	    snprintf (str, 256, "%f", option->rest.f.max);

	return strdup (str);
    }
    else if (inode->type & FUSE_INODE_TYPE_PRECISION)
    {
	snprintf (str, 256, "%f", option->rest.f.precision);
	return strdup (str);
    }
    else if (inode->type & FUSE_INODE_TYPE_ITEM_COUNT)
    {
	snprintf (str, 256, "%d", option->value.list.nValue);
	return strdup (str);
    }
    else if (inode->type & FUSE_INODE_TYPE_ITEM_TYPE)
    {
	return strdup (optionTypeToString (option->value.list.type));
    }

    return NULL;
}

static void
fuseUpdateInode (CompDisplay *d,
		 FuseInode   *inode)
{
    CompScreen *s;
    CompPlugin *p;
    CompOption *option;
    char       str[256];

    if (inode->type & FUSE_INODE_TYPE_ROOT)
    {
	FuseInode *c;

	for (c = inode->child; c; c = c->sibling)
	{
	    if (!findActivePlugin (c->name))
		fuseRemoveInode (inode, c);
	}

	for (p = getPlugins (); p; p = p->next)
	    if (!fuseLookupChild (inode, p->vTable->name))
		fuseAddInode (inode, FUSE_INODE_TYPE_PLUGIN,
			      p->vTable->name);
    }
    else if (inode->type & FUSE_INODE_TYPE_PLUGIN)
    {
	int n;

	if (fuseGetDisplayOptionsFromInode (d, inode, &n))
	    fuseAddInode (inode, FUSE_INODE_TYPE_DISPLAY, "allscreen");

	for (s = d->screens; s; s = s->next)
	{
	    if (fuseGetScreenOptionsFromInode (s, inode, &n))
	    {
		sprintf (str, "screen%d", s->screenNum);
		fuseAddInode (inode, FUSE_INODE_TYPE_SCREEN, str);
	    }
	}
    }
    else if (inode->type & (FUSE_INODE_TYPE_DISPLAY | FUSE_INODE_TYPE_SCREEN))
    {
	int nOption;

	option = fuseGetOptionsFromInode (d, inode, &nOption);
	if (option)
	{
	    while (nOption--)
	    {
		fuseAddInode (inode, FUSE_INODE_TYPE_OPTION, option->name);

		option++;
	    }
	}
    }
    else if (inode->type & FUSE_INODE_TYPE_OPTION)
    {
	option = fuseGetOptionFromInode (d, inode);
	if (option)
	{
	    fuseAddInode (inode, FUSE_INODE_TYPE_TYPE, "type");

	    switch (option->type) {
	    case CompOptionTypeFloat:
		fuseAddInode (inode, FUSE_INODE_TYPE_PRECISION, "precision");
		/* fall-through */
	    case CompOptionTypeInt:
		fuseAddInode (inode, FUSE_INODE_TYPE_MIN, "min");
		fuseAddInode (inode, FUSE_INODE_TYPE_MAX, "max");
		/* fall-through */
	    case CompOptionTypeBool:
	    case CompOptionTypeString:
	    case CompOptionTypeColor:
	    case CompOptionTypeKey:
	    case CompOptionTypeButton:
	    case CompOptionTypeEdge:
	    case CompOptionTypeBell:
	    case CompOptionTypeMatch:
		fuseAddInode (inode, FUSE_INODE_TYPE_VALUE, "value");
		break;
	    case CompOptionTypeList:
		fuseAddInode (inode, FUSE_INODE_TYPE_ITEMS, "items");
		fuseAddInode (inode, FUSE_INODE_TYPE_ITEM_COUNT,
			      "number_of_items");
		fuseAddInode (inode, FUSE_INODE_TYPE_ITEM_TYPE, "item_type");
	    default:
		break;
	    }
	}
    }
    else if (inode->type & FUSE_INODE_TYPE_ITEMS)
    {
	option = fuseGetOptionFromInode (d, inode->parent);
	if (option && option->type == CompOptionTypeList)
	{
	    FuseInode *c, *next;
	    int	      i, nValue = option->value.list.nValue;

	    for (i = 0; i < option->value.list.nValue; i++)
	    {
		sprintf (str, "value%d", i);
		if (!fuseLookupChild (inode, str))
		    fuseAddInode (inode, FUSE_INODE_TYPE_ITEM_VALUE, str);
	    }

	    for (c = inode->child; c; c = next)
	    {
		next = c->sibling;

		if (sscanf (c->name, "value%d", &i) == 0 || i >= nValue)
		    fuseRemoveInode (inode, c);
	    }
	}
    }
}

static void
fuseInodeStat (CompDisplay *d,
	       FuseInode   *inode,
	       struct stat *stbuf)
{
    stbuf->st_ino = inode->ino;

    if (inode->type & DIR_MASK)
    {
	stbuf->st_mode  = S_IFDIR | 0755;
	stbuf->st_nlink = 2;
    }
    else
    {
	char *str;

	if (inode->type & WRITE_MASK)
	    stbuf->st_mode = S_IFREG | 0666;
	else
	    stbuf->st_mode = S_IFREG | 0444;

	stbuf->st_nlink = 1;
	stbuf->st_size  = 0;

	str = fuseGetStringFromInode (d, inode);
	if (str)
	{
	    stbuf->st_size = strlen (str);
	    free (str);
	}
    }
}

static Bool
fuseInitValue (CompOptionValue *value,
	       CompOptionType  type,
	       CompOptionValue *src)
{
    switch (type) {
    case CompOptionTypeBool:
	value->b = src->b;
	break;
    case CompOptionTypeInt:
	value->i = src->i;
	break;
    case CompOptionTypeFloat:
	value->f = src->f;
	break;
    case CompOptionTypeString:
	value->s = strdup (src->s);
	break;
    case CompOptionTypeColor:
	memcpy (value->c, src->c, sizeof (*src->c));
	break;
    case CompOptionTypeKey:
    case CompOptionTypeButton:
    case CompOptionTypeEdge:
    case CompOptionTypeBell:
	value->action = src->action;
	break;
    case CompOptionTypeMatch:
	matchInit (&value->match);
	matchCopy (&value->match, &src->match);
	break;
    default:
	return FALSE;
    }

    return TRUE;
}

static Bool
fuseInitValueFromString (CompDisplay	 *display,
			 CompOptionValue *value,
			 CompOptionType  type,
			 char		 *str)
{
    switch (type) {
    case CompOptionTypeBool:
	value->b = strcmp (str, "true") ? FALSE : TRUE;
	break;
    case CompOptionTypeInt:
	value->i = atoi (str);
	break;
    case CompOptionTypeFloat:
	value->f = strtod (str, NULL);
	break;
    case CompOptionTypeString:
	value->s = strdup (str);
	break;
    case CompOptionTypeColor:
	if (!stringToColor (str, value->c))
	    return FALSE;
	break;
    case CompOptionTypeKey:
	stringToKeyAction (display, str, &value->action);
	break;
    case CompOptionTypeButton:
	stringToButtonAction (display, str, &value->action);
	break;
    case CompOptionTypeEdge:
	value->action.edgeMask = stringToEdgeMask (str);
	break;
    case CompOptionTypeBell:
	value->action.bell = strcmp (str, "true") ? FALSE : TRUE;
	break;
    case CompOptionTypeMatch:
	matchInit (&value->match);
	matchAddFromString (&value->match, str);
	break;
    default:
	return FALSE;
    }

    return TRUE;
}

static void
fuseSetInodeOptionUsingString (CompDisplay *d,
			       FuseInode   *inode,
			       char	   *str)
{
    CompOption *option;

    option = fuseGetOptionFromInode (d, inode->parent);
    if (option)
    {
	CompOptionValue value;
	CompScreen	*s = NULL;
	FuseInode	*objectInode;

	if (inode->type & FUSE_INODE_TYPE_VALUE)
	{
	    if (!fuseInitValueFromString (d, &value, option->type, str))
		return;

	    objectInode = inode->parent->parent;
	}
	else if (inode->type & FUSE_INODE_TYPE_ITEM_VALUE)
	{
	    int i, item, nValue = option->value.list.nValue;

	    if (!sscanf (inode->name, "value%d", &item))
		return;

	    if (item >= nValue)
		return;

	    value.list.value = malloc (sizeof (CompOptionValue) * nValue);
	    if (!value.list.value)
		return;

	    value.list.type   = option->value.list.type;
	    value.list.nValue = 0;

	    for (i = 0; i < nValue; i++)
	    {
		if (i == item)
		{
		    if (!fuseInitValueFromString (d,
						  &value.list.value[i],
						  value.list.type,
						  str))
			break;
		}
		else
		{
		    if (!fuseInitValue (&value.list.value[i],
					value.list.type,
					&option->value.list.value[i]))
			break;
		}

		value.list.nValue++;
	    }

	    /* failed */
	    if (value.list.nValue < nValue)
	    {
		compFiniOptionValue (&value, option->type);
		return;
	    }

	    objectInode = inode->parent->parent->parent;
	}
	else
	{
	    return;
	}

	if (objectInode->type & FUSE_INODE_TYPE_SCREEN)
	{
	    int screenNum = -1;

	    sscanf (objectInode->name, "screen%d", &screenNum);

	    for (s = d->screens; s; s = s->next)
		if (s->screenNum == screenNum)
		    break;
	}

	if (s)
	{
	    (*s->setScreenOptionForPlugin) (s,
					    objectInode->parent->name,
					    option->name,
					    &value);
	}
	else
	{
	    (*d->setDisplayOptionForPlugin) (d,
					     objectInode->parent->name,
					     option->name,
					     &value);
	}

	compFiniOptionValue (&value, option->type);
    }
}

static void
compiz_getattr (fuse_req_t	      req,
		fuse_ino_t	      ino,
		struct fuse_file_info *fi)
{
    CompDisplay	*d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode   *inode;

    inode = fuseFindInode (inodes, ino, ~0);
    if (inode)
    {
	struct stat stbuf;

	memset (&stbuf, 0, sizeof (stbuf));

	fuseInodeStat (d, inode, &stbuf);

	fuse_reply_attr (req, &stbuf, 1.0);
    }
    else
    {
	fuse_reply_err (req, ENOENT);
    }
}

static void
compiz_setattr (fuse_req_t	      req,
		fuse_ino_t	      ino,
		struct stat	      *attr,
		int		      to_set,
		struct fuse_file_info *fi)
{
    CompDisplay *d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode   *inode;

    inode = fuseFindInode (inodes, ino, WRITE_MASK);
    if (inode)
    {
	struct stat stbuf;

	if ((to_set & FUSE_SET_ATTR_SIZE) != FUSE_SET_ATTR_SIZE)
	{
	    fuse_reply_err (req, EACCES);
	    return;
	}

	if (attr->st_size != 0)
	{
	    fuse_reply_err (req, EACCES);
	    return;
	}

	inode->flags |= FUSE_INODE_FLAG_TRUNC;

	memset (&stbuf, 0, sizeof (stbuf));

	fuseInodeStat (d, inode, &stbuf);

	fuse_reply_attr (req, &stbuf, 1.0);
    }
    else
    {
	fuse_reply_err (req, ENOENT);
    }
}

static void
compiz_lookup (fuse_req_t req,
	       fuse_ino_t parent,
	       const char *name)
{
    CompDisplay		    *d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode		    *inode;
    struct fuse_entry_param e;

    inode = fuseFindInode (inodes, parent, DIR_MASK);
    if (!inode)
    {
	fuse_reply_err (req, ENOENT);
	return;
    }

    if (!inode->child || !(inode->type & CONST_DIR_MASK))
	fuseUpdateInode (d, inode);

    inode = fuseLookupChild (inode, name);
    if (!inode)
    {
	fuse_reply_err (req, ENOENT);
	return;
    }

    memset (&e, 0, sizeof (e));

    e.attr_timeout  = 1.0;
    e.entry_timeout = 1.0;
    e.ino	    = inode->ino;

    fuseInodeStat (d, inode, &e.attr);

    fuse_reply_entry (req, &e);
}

struct dirbuf {
    char   *p;
    size_t size;
};

static void
dirbuf_add (fuse_req_t	  req,
	    struct dirbuf *b,
	    const char	  *name,
	    fuse_ino_t	  ino)
{
    struct stat stbuf;
    size_t	oldSize = b->size;

    b->size += fuse_add_direntry (req, NULL, 0, name, NULL, 0);
    b->p     = (char *) realloc (b->p, b->size);

    memset (&stbuf, 0, sizeof (stbuf));

    stbuf.st_ino = ino;

    fuse_add_direntry (req, b->p + oldSize, b->size - oldSize, name, &stbuf,
		       b->size);
}

static int
reply_buf_limited (fuse_req_t req,
		   const char *buf,
		   size_t     bufsize,
		   off_t      off,
		   size_t     maxsize)
{
    if (off < bufsize)
	return fuse_reply_buf (req, buf + off, MIN (bufsize - off, maxsize));
    else
	return fuse_reply_buf (req, NULL, 0);
}

static void
compiz_readdir (fuse_req_t	      req,
		fuse_ino_t	      ino,
		size_t		      size,
		off_t		      off,
		struct fuse_file_info *fi)
{
    CompDisplay   *d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode	  *inode, *c;
    struct dirbuf b;

    inode = fuseFindInode (inodes, ino, DIR_MASK);
    if (!inode)
    {
	fuse_reply_err (req, ENOTDIR);
	return;
    }

    memset (&b, 0, sizeof (b));

    dirbuf_add (req, &b, ".", ino);
    dirbuf_add (req, &b, "..", inode->parent ? inode->parent->ino : ino);

    if (!inode->child || !(inode->type & CONST_DIR_MASK))
	fuseUpdateInode (d, inode);

    for (c = inode->child; c; c = c->sibling)
	dirbuf_add (req, &b, c->name, c->ino);

    reply_buf_limited (req, b.p, b.size, off, size);

    free (b.p);
}

static void
compiz_open (fuse_req_t		   req,
	     fuse_ino_t		   ino,
	     struct fuse_file_info *fi)
{
    CompDisplay *d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode   *inode;

    inode = fuseFindInode (inodes, ino, ~0);
    if (!inode)
    {
	fuse_reply_err (req, ENOENT);
	return;
    }

    fi->fh = 0;

    if (inode->type & DIR_MASK)
    {
	fuse_reply_err (req, EISDIR);
    }
    else if (inode->type & WRITE_MASK)
    {
	if ((fi->flags & 3) != O_RDONLY)
	{
	    char *data;

	    if (fi->flags & O_TRUNC)
		data = strdup ("");
	    else
		data = fuseGetStringFromInode (d, inode);

	    if (data)
	    {
		FuseWriteBuffer *wb;

		wb = malloc (sizeof (FuseWriteBuffer));
		if (wb)
		{
		    wb->data  = data;
		    wb->size  = strlen (wb->data);
		    wb->dirty = TRUE;

		    fi->fh = (unsigned long) wb;
		}
		else
		{
		    free (data);
		}
	    }
	}

	fuse_reply_open (req, fi);
    }
    else if ((fi->flags & 3) != O_RDONLY)
    {
	fuse_reply_err (req, EACCES);
    }
    else
    {
	fuse_reply_open (req, fi);
    }
}

static void
compiz_read (fuse_req_t		   req,
	     fuse_ino_t		   ino,
	     size_t		   size,
	     off_t		   off,
	     struct fuse_file_info *fi)
{
    CompDisplay *d = (CompDisplay *) fuse_req_userdata (req);
    FuseInode   *inode;
    char	*str = NULL;

    inode = fuseFindInode (inodes, ino, ~0);
    if (inode)
	str = fuseGetStringFromInode (d, inode);

    if (str)
    {
	reply_buf_limited (req, str, strlen (str), off, size);
	free (str);
    }
    else
    {
	reply_buf_limited (req, NULL, 0, off, size);
    }
}

static void
compiz_write (fuse_req_t	    req,
	      fuse_ino_t	    ino,
	      const char	    *buf,
	      size_t		    size,
	      off_t		    off,
	      struct fuse_file_info *fi)
{
    FuseInode *inode;

    inode = fuseFindInode (inodes, ino, WRITE_MASK);
    if (inode && fi->fh)
    {
	FuseWriteBuffer *wb = (FuseWriteBuffer *) (uintptr_t) fi->fh;

	if (off + size > wb->size)
	{
	    char *data;

	    data = realloc (wb->data, off + size + 1);
	    if (!data)
	    {
		fuse_reply_err (req, ENOBUFS);
		return;
	    }

	    data[off + size] = '\0';

	    wb->data = data;
	    wb->size = off + size;
	}

	memcpy (wb->data + off, buf, size);

	wb->dirty = TRUE;

	fuse_reply_write (req, size);
    }
    else
    {
	fuse_reply_err (req, ENOENT);
    }
}

static void
compiz_release (fuse_req_t	      req,
		fuse_ino_t	      ino,
		struct fuse_file_info *fi)
{
    CompDisplay *d = (CompDisplay *) fuse_req_userdata (req);

    if (fi->fh)
    {
	FuseWriteBuffer *wb = (FuseWriteBuffer *) (uintptr_t) fi->fh;
	FuseInode	*inode;

	inode = fuseFindInode (inodes, ino, WRITE_MASK);
	if (inode && wb->dirty)
	{
	    fuseSetInodeOptionUsingString (d, inode, wb->data);

	    inode->flags &= ~FUSE_INODE_FLAG_TRUNC;
	}

	free (wb->data);
	free (wb);
    }

    fuse_reply_err (req, 0);
}

static void
compiz_fsync (fuse_req_t	    req,
	      fuse_ino_t	    ino,
	      int		    datasync,
	      struct fuse_file_info *fi)
{
    CompDisplay *d = (CompDisplay *) fuse_req_userdata (req);

    if (fi->fh)
    {
	FuseWriteBuffer *wb = (FuseWriteBuffer *) (uintptr_t) fi->fh;
	FuseInode	*inode;

	inode = fuseFindInode (inodes, ino, WRITE_MASK);
	if (inode && wb->dirty)
	{
	    fuseSetInodeOptionUsingString (d, inode, wb->data);

	    inode->flags &= ~FUSE_INODE_FLAG_TRUNC;

	    wb->dirty = FALSE;
	}
    }

    fuse_reply_err (req, 0);
}

static struct fuse_lowlevel_ops compiz_ll_oper = {
    .lookup  = compiz_lookup,
    .getattr = compiz_getattr,
    .setattr = compiz_setattr,
    .readdir = compiz_readdir,
    .open    = compiz_open,
    .read    = compiz_read,
    .write   = compiz_write,
    .release = compiz_release,
    .fsync   = compiz_fsync
};

static void
fuseUnmount (CompDisplay *d)
{
    FUSE_DISPLAY (d);

    if (fd->watchFdHandle)
    {
	compRemoveWatchFd (fd->watchFdHandle);
	fd->watchFdHandle = 0;
    }

    if (fd->mountPoint)
    {
	/* unmount will destroy the channel */
	fuse_unmount (fd->mountPoint, fd->channel);
	free (fd->mountPoint);
	fd->mountPoint = NULL;
	fd->channel = NULL;
    }

    if (fd->buffer)
    {
	free (fd->buffer);
	fd->buffer = NULL;
    }
}

static Bool
fuseProcessMessages (void *data)
{
    CompDisplay	     *d = (CompDisplay *) data;
    struct fuse_chan *channel;
    size_t	     bufferSize;
    int		     res = 0;

    FUSE_DISPLAY (d);

    channel    = fuse_session_next_chan (fd->session, NULL);
    bufferSize = fuse_chan_bufsize (channel);

    if (fuse_session_exited (fd->session))
	return FALSE;

    for (;;)
    {
	struct fuse_chan *tmpch = channel;

	res = fuse_chan_recv (&tmpch, fd->buffer, bufferSize);
	if (res == -EINTR)
	    continue;

	if (res > 0)
	    fuse_session_process (fd->session, fd->buffer, res, tmpch);

	break;
    }

    return TRUE;
}

static void
fuseMount (CompDisplay *d)
{
    char	     *mountPoint;
    struct fuse_args args = FUSE_ARGS_INIT (0, NULL);

    FUSE_DISPLAY (d);

    mountPoint = strdup (fd->opt[FUSE_DISPLAY_OPTION_MOUNT_POINT].value.s);
    if (!mountPoint)
	return;

    fuse_opt_add_arg (&args, "");
    fuse_opt_add_arg (&args, "-o");
    fuse_opt_add_arg (&args, "allow_root");

    fd->channel = fuse_mount (mountPoint, &args);
    if (!fd->channel)
    {
	fuse_opt_free_args (&args);
	free (mountPoint);
	return;
    }

    fuse_opt_free_args (&args);

    fd->buffer = malloc (fuse_chan_bufsize (fd->channel));
    if (!fd->buffer)
    {
	fuse_unmount (mountPoint, fd->channel);
	free (mountPoint);
	fd->channel = NULL;
	return;
    }

    fd->mountPoint = mountPoint;

    fuse_session_add_chan (fd->session, fd->channel);

    fd->watchFdHandle = compAddWatchFd (fuse_chan_fd (fd->channel),
					POLLIN | POLLPRI | POLLHUP | POLLERR,
					fuseProcessMessages,
					d);
}

static CompOption *
fuseGetDisplayOptions (CompPlugin  *plugin,
		       CompDisplay *display,
		       int	   *count)
{
    FUSE_DISPLAY (display);

    *count = NUM_OPTIONS (fd);
    return fd->opt;
}

static Bool
fuseSetDisplayOption (CompPlugin      *plugin,
		      CompDisplay     *display,
		      const char      *name,
		      CompOptionValue *value)
{
    CompOption *o;
    int	       index;

    FUSE_DISPLAY (display);

    o = compFindOption (fd->opt, NUM_OPTIONS (fd), name, &index);
    if (!o)
	return FALSE;

    switch (index) {
    case FUSE_DISPLAY_OPTION_MOUNT_POINT:
	if (compSetStringOption (o, value))
	{
	    fuseUnmount (display);
	    fuseMount (display);
	    return TRUE;
	}
    default:
	break;
    }

    return FALSE;
}

static const CompMetadataOptionInfo fuseDisplayOptionInfo[] = {
    { "mount_point", "string", 0, 0, 0 }
};

static Bool
fuseInitDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    FuseDisplay	     *fd;
    struct sigaction sa;

    if (!checkPluginABI ("core", ABIVERSION))
	return FALSE;

    memset (&sa, 0, sizeof (struct sigaction));

    sa.sa_handler = SIG_IGN;
    sigemptyset (&sa.sa_mask);
    sa.sa_flags = 0;

    if (sigaction (SIGPIPE, &sa, NULL) == -1)
	return FALSE;

    fd = malloc (sizeof (FuseDisplay));
    if (!fd)
	return FALSE;

    if (!compInitDisplayOptionsFromMetadata (d,
					     &fuseMetadata,
					     fuseDisplayOptionInfo,
					     fd->opt,
					     FUSE_DISPLAY_OPTION_NUM))
    {
	free (fd);
	return FALSE;
    }

    fd->session = fuse_lowlevel_new (NULL,
				     &compiz_ll_oper, sizeof (compiz_ll_oper),
				     (void *) d);
    if (!fd->session)
    {
	compFiniDisplayOptions (d, fd->opt, FUSE_DISPLAY_OPTION_NUM);
	free (fd);
	return FALSE;
    }

    fd->watchFdHandle = 0;
    fd->channel	      = NULL;
    fd->buffer	      = NULL;
    fd->mountPoint    = NULL;

    d->privates[displayPrivateIndex].ptr = fd;

    fuseMount (d);

    return TRUE;
}

static void
fuseFiniDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    FUSE_DISPLAY (d);

    fuseUnmount (d);

    fuse_session_destroy (fd->session);

    compFiniDisplayOptions (d, fd->opt, FUSE_DISPLAY_OPTION_NUM);

    free (fd);
}

static Bool
fuseInit (CompPlugin *p)
{
    if (!compInitPluginMetadataFromInfo (&fuseMetadata,
					 p->vTable->name,
					 fuseDisplayOptionInfo,
					 FUSE_DISPLAY_OPTION_NUM,
					 0, 0))
	return FALSE;

    inodes = fuseAddInode (NULL, FUSE_INODE_TYPE_ROOT, ".");
    if (!inodes)
    {
	compFiniMetadata (&fuseMetadata);
	return FALSE;
    }

    displayPrivateIndex = allocateDisplayPrivateIndex ();
    if (displayPrivateIndex < 0)
    {
	fuseRemoveInode (NULL, inodes);
	compFiniMetadata (&fuseMetadata);
	return FALSE;
    }

    compAddMetadataFromFile (&fuseMetadata, p->vTable->name);

    return TRUE;
}

static void
fuseFini (CompPlugin *p)
{
    fuseRemoveInode (NULL, inodes);
    freeDisplayPrivateIndex (displayPrivateIndex);
    compFiniMetadata (&fuseMetadata);
}

static CompMetadata *
fuseGetMetadata (CompPlugin *plugin)
{
    return &fuseMetadata;
}

CompPluginVTable fuseVTable = {
    "fs",
    fuseGetMetadata,
    fuseInit,
    fuseFini,
    fuseInitDisplay,
    fuseFiniDisplay,
    0, /* InitScreen */
    0, /* FiniScreen */
    0, /* InitWindow */
    0, /* FiniWindow */
    fuseGetDisplayOptions,
    fuseSetDisplayOption,
    0, /* GetScreenOptions */
    0  /* SetScreenOption */
};

CompPluginVTable *
getCompPluginInfo (void)
{
    return &fuseVTable;
}