~ubuntu-branches/ubuntu/trusty/gkrellkam/trusty

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
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
/*
  gkrellkam2 -- image watcher plugin for GKrellM
  Copyright (C) 2001-2002 paul cannon

  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
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      02111-1307, USA.

  To contact the author try:
  paul cannon <pik@debian.org>

  A note on style here:

    I use (gchar *) for a string's type when the array is dynamically
    allocated with the g_* functions, and will eventually need to be
    g_free'd. Strings of type (char *) are static memory.
*/

#if !defined(WIN32)
# include <gkrellm2/gkrellm.h>
# include <libgen.h>
# include <unistd.h>
#else
# include <src/gkrellm.h>
# include <src/win32-plugin.h>
#endif

#include <errno.h>
#include <stdlib.h>
#include <time.h>

#define PLUGIN_VER "2.0.0"

#define PLUGIN_NAME "GKrellKam"
#define PLUGIN_DESC "GKrellM Image Watcher plugin"
#define PLUGIN_URL "http://gkrellkam.sf.net/"
#define PLUGIN_STYLE PLUGIN_NAME
#define PLUGIN_KEYWORD PLUGIN_NAME

#define TEMPTEMPLATE "/tmp/krellkam"

#define DEBUGGING 0

static gchar *kkam_info_text[] = 
{
"<b>" PLUGIN_NAME " " PLUGIN_VER "\n\n",

"GKrellKam is a plugin that can watch a number of image files,\n",
"and display them sized to fit in panels on your gkrellm.\n\n",

"You can ", "<b>left-click", " on an image panel to display the\n",
"original, unsized image, or you can ", "<b>middle-click", " on it\n",
"to get an immediate update.\n\n",

"Scrolling the mouse wheel up over any of the panels increases\n",
"the number of visible panels, and scrolling it down decreases\n",
"the number. This is so you can hide panels you don't always want\n",
"open."

"You can also ", "<b>right-click", " on a panel to open the\n",
"configuration window.",

"\n\n",
"<b>-- EASY START --\n\n",

"Just put the address of a webcam image in the Image Source\n",
"box on the configuration tab for one of the panels. The\n",
"address will probably start with \"http://\" and end in \".jpg\",\n",
"\".png\", or \".gif\".\n\n",

"<b>-- CONFIGURATION TABS --\n\n",

"<b>Global Options\n\n",

"<i>Path to image viewer program\n",
"When you left-click on a GKrellKam panel, it will start your\n",
"favorite image viewer and display the original, unsized image.\n",
"Put the name of the image viewer program in this box. To use\n",
"the included GKrellKam internal viewer, just leave this field\n",
"blank. Try clicking on the internal viewer window to get the\n",
"menu.\n\n",

"<i>Popup errors\n",
"When something goes wrong with an image download or the parsing\n",
"of a list, GKrellKam lets you know. When Popup errors is checked,\n",
"the message will appear as a popup window with an \"OK\" button.\n",
"When you turn this option off, error messages will be reported\n",
"in the tooltip for the image in question. They are not very\n",
"visible this way, but perhaps they are less annoying too.\n\n",

"<i>Number of panels\n",
"This lets you adjust the number of visible GKrellKam panels\n",
"between 0 and 5.\n\n",

"<b>Panel Config Tabs\n\n",

"<i>Default number of seconds per update\n",
"After an image has been successfully loaded, GKrellKam will\n",
"wait this many seconds before updating the image. Updating\n",
"might involve reloading the same image, or getting the next\n",
"image in a list. If you middle-click on a panel, it will do\n",
"the update immediately. This setting can be overridden by\n",
"specific items in lists.\n\n",

"<i>Height of viewer, in pixels\n",
"You can adjust this for each panel to give your pictures a\n",
"nice-looking aspect.\n\n",

"<i>Border size\n",
"Adds a border space around the image to match better with\n",
"your GKrellM theme.\n\n",

"<i>Maintain aspect ratio\n",
"When checked, images are not sized to fit perfectly in this\n",
"panel. They maintain their aspect, and theme background is\n",
"shown on either the sides or the top and bottom.\n\n",

"<i>Select list images at random\n",
"When this is checked, and your image source is a list, then\n",
"images will be taken from the list at random rather than\n",
"cycled through one by one.\n\n",

"<i>Image Source\n",
"Each panel has a \"source\" associated with it. A source can be\n",
"a local picture, the web address of a picture, a list of\n",
"pictures, or a script to call to get a new picture. To give a\n",
"local picture file, list of files, or a script, enter the\n",
"_full_ filename in the \"Image Source\" box. To watch a webcam\n",
"or other online picture, or use an online list, just put its\n",
"address (beginning with http:// or ftp://) in the \"Image Source\n",
"box. Lists should end in \"-list\" or \".list\". You'll need GNU\n",
"wget installed to be able to get files from the internet.\n",
"Special case: when this field begins with \"-x\" followed by a\n",
"space and some more text, the remaining text is assumed to be a\n",
"script or other system commmand, and the whole path does not\n",
"need to be specified.\n\n",

"<i>Reread source button\n",
"When your image source is a list, GKrellKam will only read in\n",
"that list once. If it is changed, and you want GKrellKam to\n",
"follow the changes, you can hit the \"Reread source\" button.\n",
"This will restart the list. If the source is a script, it will\n",
"be re-executed. Other sources will be reread as well.\n\n",

"See the included example list, the ",
"<b>gkrellkam-list(5)",
" manpage,\nand ",
"<b>" PLUGIN_URL,
" online for more info."
};

static gchar *kkam_about_text = _(
  PLUGIN_NAME " " PLUGIN_VER
  "\n" PLUGIN_DESC
  "\n\nCopyright (C) 2001 paul cannon\n"
  "paul@cannon.cs.usu.edu\n"
  "space software lab/utah state university\n\n"
  PLUGIN_NAME " comes with ABSOLUTELY NO WARRANTY;\n"
  "see the file COPYING for details.\n\n"
  PLUGIN_URL );

static const char *default_source[] = {
  "http://aggies.usu.edu/webcam/fullsize.jpg",
  "",
  "",
  "",
  ""
};

#define wget_opts "--cache=off"
#define BUFLEN 256
#define MIN_NUMPANELS 0
#define MAX_NUMPANELS 5
#define MAX_DEPTH 64
#define MAX_SECONDS 604800 /* one week */

typedef enum {
  SOURCE_URL,
  SOURCE_FILE,
  SOURCE_SCRIPT,
  SOURCE_LIST,
  SOURCE_LISTURL
} SourceEnum;

/* source information structure- panels each have a GList of these */

typedef struct
{
  gchar *img_name;
  gchar *tooltip;
  SourceEnum type;
  int seconds;
  int next_dl;
  gchar *tfile;
  int tlife;
} KKamSource;

/* items that each panel needs */

typedef struct
{
  GkrellmPanel *panel;
  GkrellmDecal *decal;
  GdkPixmap *pixmap;
  FILE *cmd_pipe;
  int count;
  int height;
  int boundary;
  int default_period;
  gboolean maintain_aspect;
  gboolean random;
  gint visible;

  GtkWidget *period_spinner;
  GtkWidget *boundary_spinner;
  GtkWidget *height_spinner;
  GtkWidget *aspect_box;
  GtkWidget *random_box;
  GtkWidget *sourcebox;
  GdkPixbuf *pixbuf;

  FILE *listurl_pipe;
  gchar *listurl_file;

  gchar *source;
  GList *sources;
} KKamPanel;

typedef struct
{
  GtkWidget *window;
  GtkWidget *menu;
  GtkWidget *pmap;
  GtkWidget *fdialog;
  GdkPixbuf *pixbuf;
} ViewerInfo;

static GkrellmMonitor *monitor;

static GkrellmTicks *pGK;

static int created = 0;
static int numpanels = 0;
static int newnumpanels = 1;
static KKamPanel *panels = NULL;

static GkrellmStyle *img_style = NULL;
static gint style_id;

static char *viewer_prog = NULL;
static int popup_errors = 0;

static GtkWidget *viewerbox;
static GtkWidget *numpanel_spinner = NULL;
static GtkWidget *popup_errors_box = NULL;
static GtkWidget *tabs = NULL;
static GtkWidget *kkam_vbox = NULL;
static GtkWidget *filebox = NULL;

static GtkTooltips *tooltipobj;

static void change_num_panels ();
static void create_sources_list (KKamPanel *p);
static void kkam_read_list (KKamPanel *p, char *listname, int depth);

static KKamSource empty_source = { "", "No config",
                                   SOURCE_FILE,
                                   0, 0, NULL, 0 };

static char *IMG_EXTENSIONS[] = {
  ".jpg",
  ".jpeg",
  ".png",
  ".bmp",
  ".xpm",
  ".pbm",
  ".ppm",
  ".tif",
  ".tiff",
  ".gif"
};

/*
  report_error ()

  if popup errors are on, brings up a message window.
  if not, sets the tooltip text to a warning.
*/
static void report_error (KKamPanel *p, char *fmt, ...)
{
  va_list ap;
  char *str;
 
  va_start (ap, fmt);
  str = g_strdup_vprintf (fmt, ap);
  va_end (ap);

  if (popup_errors)
  {
    GtkWidget *label, *vbox, *dialog;

    dialog = gtk_dialog_new_with_buttons(_("GKrellKam warning:"),
             NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
             GTK_STOCK_OK, GTK_RESPONSE_NONE,
             NULL);
    g_signal_connect_swapped(GTK_OBJECT(dialog), "response",
             G_CALLBACK(gtk_widget_destroy), GTK_OBJECT(dialog));

    vbox = gtk_vbox_new (FALSE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
             FALSE, FALSE, 0);
  
    label = gtk_label_new (_("GKrellKam warning:"));
    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
    label = gtk_label_new (str);
    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

    gtk_widget_show_all (dialog);
  }
  else
  {
    if (p && tooltipobj && p->panel && p->panel->drawing_area)
      gtk_tooltips_set_tip (tooltipobj, p->panel->drawing_area, str, NULL);
  }
}

/*
  kkam_add_menu_item ()

  Used to add a simple item to a menu more easily
*/
static void kkam_add_menu_item (GtkWidget *menu, char *label,
                                GtkSignalFunc cb, gpointer d)
{
  GtkWidget *mi;

  mi = gtk_menu_item_new_with_label (label);
  g_signal_connect_swapped (G_OBJECT (mi), "activate", G_CALLBACK(cb), d);
  gtk_widget_show (mi);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
}

/*
  kkam_iv_destroy ()

  The delete_event callback for the internal viewer
*/
static void kkam_iv_destroy (ViewerInfo *vi)
{
  if (vi->fdialog)
    gtk_widget_destroy (vi->fdialog);
  g_object_unref (G_OBJECT(vi->pixbuf));
  gtk_widget_destroy (vi->menu);
  gtk_widget_destroy (vi->window);
  g_free (vi);
}

/*
  kkam_iv_donesave ()

  Turns off the viewer's "Save as" file dialog and sets the pointer
  to NULL  
*/
static void kkam_iv_donesave (ViewerInfo *vi)
{
  gtk_widget_destroy (vi->fdialog);
  vi->fdialog = NULL;
}

/*
  kkam_iv_dosave ()

  Called when user hits ok on the "Save as" file dialog. Gets the
  filename, closes the file dialog, and saves the image appropriately.
  gdk_pixbuf_save() supports only "png" or "jpeg" in Gtk+-2.0.
*/
static void kkam_iv_dosave (ViewerInfo *vi)
{
  gchar *fname;
  char  *type = NULL, *s;

  fname = g_strdup (
       gtk_file_selection_get_filename (GTK_FILE_SELECTION (vi->fdialog)));
  kkam_iv_donesave (vi);
  if ((s = strstr(fname, ".png")) != NULL)
    type = "png";
  else if ((s = strstr(fname, ".jpg")) != NULL)
	type = "jpeg";
  else if ((s = strstr(fname, ".jpeg")) != NULL)
	type = "jpeg";
  if (!type)
    {
    report_error(NULL, "Saved images must be .jpg or .png only.\n", NULL);
    return;
	}

  gdk_pixbuf_save(vi->pixbuf, fname, type, NULL, NULL);
  g_free (fname);
}

/*
  kkam_iv_saveas ()

  Opens the "Save as" file dialog for an image displayed on the
  internal viewer.
*/
static void kkam_iv_saveas (ViewerInfo *vi)
{
  if (vi->fdialog)
  {
    gdk_window_raise (vi->fdialog->window);
    return;
  }

  vi->fdialog = gtk_file_selection_new (_("Save As:"));

  g_signal_connect_swapped (
           G_OBJECT (GTK_FILE_SELECTION (vi->fdialog)->ok_button),
           "clicked",
           G_CALLBACK (kkam_iv_dosave), (gpointer)vi);
  g_signal_connect_swapped (
           G_OBJECT (GTK_FILE_SELECTION (vi->fdialog)->cancel_button),
           "clicked",
           G_CALLBACK (kkam_iv_donesave), (gpointer)vi);
  gtk_widget_show (vi->fdialog);
}

/*
  kkam_iv_makemenu ()

  Creates the popup menu seen when left- or right- clicking on the
  internal viewer
*/
static void kkam_iv_makemenu (ViewerInfo *vi)
{
  vi->menu = gtk_menu_new ();
  kkam_add_menu_item (vi->menu, _("Close"),
                      GTK_SIGNAL_FUNC (kkam_iv_destroy), (gpointer)vi);
  kkam_add_menu_item (vi->menu, _("Save As.."),
                      GTK_SIGNAL_FUNC (kkam_iv_saveas), (gpointer)vi);
}

/*
  kkam_iv_popup ()

  Shows the popup menu after user left- or right- clicks on the
  internal viewer
*/
static gint kkam_iv_popup (ViewerInfo *vi, GdkEventButton *ev)
{
  if (ev->button == 1 || ev->button == 3)
    gtk_menu_popup (GTK_MENU (vi->menu), NULL, NULL, NULL, NULL,
                    ev->button, ev->time);
  return FALSE;
}

/*
  kkam_iv_resize ()

  Rerenders the image when a user resizes the internal viewer window
*/
static gint kkam_iv_resize (ViewerInfo *vi, GdkEventConfigure *ev)
{
  GdkPixmap *pix = NULL;
  GdkBitmap *bit = NULL;

  gkrellm_scale_pixbuf_to_pixmap(vi->pixbuf, &pix, &bit,
                      ev->width, ev->height);

  gtk_image_set_from_pixmap (GTK_IMAGE (vi->pmap), pix, bit);
  g_object_unref(G_OBJECT(pix));
  if (bit)
    g_object_unref(G_OBJECT(bit));

  return TRUE;
}

/*
  kkam_internal_viewer ()

  Opens a very simple full-size image viewer window for the image in
  question. Users can specify an external viewer if they don't like it :)
*/
static void kkam_internal_viewer (char *filename)
{
  GtkWidget *ebox;
  GdkPixmap *pix = NULL;
  GdkBitmap *bit = NULL;
  ViewerInfo *vi;

  vi = g_new0 (ViewerInfo, 1);
  if ((vi->pixbuf = gdk_pixbuf_new_from_file (filename, NULL)) == NULL)
  {
    g_free (vi);
    return;
  }

  vi->fdialog = NULL;
  kkam_iv_makemenu (vi);
  vi->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (vi->window), filename);
  g_signal_connect_swapped (G_OBJECT (vi->window), "delete_event",
                             G_CALLBACK (kkam_iv_destroy),
                             (gpointer)vi);
  g_signal_connect_swapped (G_OBJECT (vi->window), "configure_event",
                             G_CALLBACK (kkam_iv_resize),
                             (gpointer)vi);
//  gtk_window_set_policy (GTK_WINDOW (vi->window), TRUE, TRUE, FALSE);
  gtk_window_set_wmclass (GTK_WINDOW (vi->window), "KKamViewer", "GKrellm");

  gkrellm_scale_pixbuf_to_pixmap(vi->pixbuf, &pix, &bit,
                      gdk_pixbuf_get_width(vi->pixbuf),
                      gdk_pixbuf_get_height(vi->pixbuf));

  vi->pmap = gtk_image_new_from_pixmap(pix, bit);
  g_object_unref(G_OBJECT(pix));
  if (bit)
    g_object_unref(G_OBJECT(bit));

  ebox = gtk_event_box_new ();
  gtk_container_add (GTK_CONTAINER (ebox), vi->pmap);
  gtk_container_add (GTK_CONTAINER (vi->window), ebox);
  gtk_widget_set_events (ebox, GDK_BUTTON_PRESS_MASK);
  g_signal_connect_swapped (G_OBJECT (ebox), "button_press_event",
                             G_CALLBACK (kkam_iv_popup), (gpointer)vi);
  gtk_widget_show_all (vi->window);
}

/*
  validnum ()

  returns TRUE if the given number is a valid index
  into the panels array
*/
static gboolean validnum (int num)
{
  return (panels && num >= 0 && num < MAX_NUMPANELS);
}

/*
  activenum ()

  returns TRUE if the given number is an active index
  into the panels array
*/
static gboolean activenum (int num)
{
  return (panels && num >= 0 && num < numpanels);
}

/*
  panel_cursource ()

  returns a pointer to the source definition of the current image for
  the current panel, or &empty_source if none.
*/
static KKamSource *panel_cursource (KKamPanel *p)
{
  if (p->sources == NULL)
    return &empty_source;
  return (KKamSource *)(p->sources->data);
}

/*
  get_period ()

  returns the time that should elapse before the current image is
  changed. If the current source's seconds member is 0, use the
  default_period.
*/
static int get_period (KKamPanel *p)
{
  int per;
  
  if ((per = panel_cursource (p)->seconds) == 0)
    return p->default_period;
  return per;
}

/*
  tfile_release ()

  If a kkamsource has been loaded before, and a copy of the image
  is still attached to it, then unattach that copy. If it is a
  temporary file, then unlink it as well.
*/
static void tfile_release (KKamSource *ks)
{
  if (ks == NULL || ks->tfile == NULL)
    return;

  if (ks->type == SOURCE_URL)
    unlink (ks->tfile);
  g_free (ks->tfile);
  ks->tfile = NULL;
  ks->next_dl = 0;
}

/*
  draw_pixbuf ()
  
  renders the current image into the panel at the right size.
  aspect-scaling patch from Benjamin Johnson (benj@visi.com)- thanks
*/
static void draw_pixbuf (KKamPanel *p)
{
  int pan_x, pan_y;       /* panel x and y sizes */
  int scale_x, scale_y;   /* size of scaled image */
  int loc_x, loc_y;       /* location for scaled image */
  int image_w, image_h;

  if (p->pixbuf == NULL)
    return;

  pan_x = gkrellm_chart_width () - 2 * p->boundary;
  pan_y = p->height - 2 * p->boundary;

  image_w = gdk_pixbuf_get_width(p->pixbuf);
  image_h = gdk_pixbuf_get_height(p->pixbuf);

  if (p->maintain_aspect)
  {
    /* determine sizing here - maintain aspect ratio */

    if (pan_x >= image_w && pan_y >= image_h)
    {
      /* the image is smaller then the panel. do no sizing, just center. */

      loc_x = (pan_x - image_w) / 2 + p->boundary;
      loc_y = (pan_y - image_h) / 2 + p->boundary;

      scale_x = 0; /* scale of 0 defaults to use image size */
      scale_y = 0;
    }
    else if ((double) image_w / (double)pan_x >
             (double) image_h / (double)pan_y)
    {
      /* scale to width (image is wider compared to panel size) */

      scale_x = pan_x;
      scale_y = image_h * pan_x / image_w;

      loc_x = p->boundary;
      loc_y = (pan_y - scale_y) / 2 + p->boundary;
    }
    else
    {
      /* scale to height (image is taller compared to panel size) */

      scale_x = image_w * pan_y / image_h;
      scale_y = pan_y;

      loc_x = (pan_x - scale_x) / 2 + p->boundary;
      loc_y = p->boundary;
    }
  }
  else
  {
    /* Scale to size of panel */

    scale_x = pan_x;
    scale_y = pan_y;

    loc_x = p->boundary;
    loc_y = p->boundary;
  }
  
  gkrellm_destroy_decal (p->decal);
  gkrellm_scale_pixbuf_to_pixmap (p->pixbuf, &(p->pixmap), NULL,
                            scale_x, scale_y);

  p->decal = gkrellm_create_decal_pixmap (p->panel, p->pixmap,
                                          NULL, 1, img_style, loc_x, loc_y);
  gkrellm_draw_decal_pixmap (p->panel, p->decal, 0);
  gkrellm_draw_panel_layers (p->panel);
}

/*
  start_img_dl ()

  Open a pipe and spawn wget.
*/
static void start_img_dl (KKamPanel *p)
{
  gchar *wget_str;
  char tmpfile[] = TEMPTEMPLATE "XXXXXX";
  int tmpfd;

  if (p->cmd_pipe) /* already open */
    return;

  tmpfd = mkstemp (tmpfile); /* this will create the file, perm 0600 */
  if (tmpfd == -1)
  {
    report_error (p, _("Couldn't create temporary file for download: %s"),
                  strerror (errno));
    return;
  }
  close (tmpfd);

  wget_str = g_strdup_printf ("wget -q %s -O %s \"%s\"",
                              wget_opts, tmpfile,
                              panel_cursource (p)->img_name);

  p->cmd_pipe = popen (wget_str, "r");
  g_free (wget_str);
  if (p->cmd_pipe == NULL)
  {
    unlink (tmpfile);
    report_error (p, _("Couldn't start wget: %s"), strerror (errno));
    return;
  }
  
  panel_cursource (p)->tfile = g_strdup (tmpfile);
  fcntl (fileno (p->cmd_pipe), F_SETFL, O_NONBLOCK);
}

/*
  start_script_dl ()

  open a pipe for a user-defined script
*/
static void start_script_dl (KKamPanel *p)
{
  char *scriptname;

  if (p->cmd_pipe) /* already open */
    return;

  scriptname = panel_cursource (p)->img_name;
  if (!strncmp (scriptname, "-x", 2))
    scriptname += 2;

  p->cmd_pipe = popen (scriptname, "r");
  if (p->cmd_pipe == NULL)
  {
    report_error (p, _("Couldn't start script \"%s\": %s\n"),
                  panel_cursource (p)->img_name, strerror (errno));
    return;
  }
  fcntl (fileno (p->cmd_pipe), F_SETFL, O_NONBLOCK);
}

/*
  load_image_file ()

  If the file in p's cursource exists, loads it into p's pixbuf
  member. Calls draw_pixbuf. Also, sets image tooltip.

  Returns -1 on file missing, and 1 otherwise (not necessarily
    success, but probably)
*/
static int load_image_file (KKamPanel *p)
{
  struct stat img_st;
  KKamSource *ks;

  ks = panel_cursource (p);

  /* make sure file is really there. when the imlib stuff
     fails it's really loud */
  if (ks->tfile == NULL || stat (ks->tfile, &img_st) == -1)
  {
    ks->next_dl = 0;
    return -1;
  }

  if (p->pixbuf)
    g_object_unref (G_OBJECT(p->pixbuf));
  p->pixbuf = gdk_pixbuf_new_from_file (ks->tfile, NULL);
  draw_pixbuf (p);

  if (ks->tooltip)
    gtk_tooltips_set_tip (tooltipobj, p->panel->drawing_area,
                          ks->tooltip, NULL);
  else
    gtk_tooltips_set_tip (tooltipobj, p->panel->drawing_area,
                          ks->img_name, NULL);

  return 1;
}

/*
  cmd_results ()

  Checks if the command pipe is dead yet. If so, checks its output
  status and returns 1 if the command was successful.

  On error, returns -1. On not yet ready, returns 0.
*/
static int cmd_results (KKamPanel *p)
{
  int code, len;
  char buf[BUFLEN];
  KKamSource *ks;

  ks = panel_cursource (p);

  if (fread (buf, sizeof (char), 1, p->cmd_pipe) < 1)
  {
    /* if we get EAGAIN, wait some more */
    if (ferror (p->cmd_pipe) && errno == EAGAIN)
      return 0;

    /* if we reach here the pipe is dead- the command has finished. */
    code = pclose (p->cmd_pipe);
    p->cmd_pipe = NULL;

    /* pclose will return a -1 on a wait4 error. If that happens,
       we have no way to know whether wget succeeded. Just try */
    if (ks->type == SOURCE_URL && code <= 0)
    {
      ks->next_dl = time (NULL) + ks->tlife;
      load_image_file (p);
      return 1;
    }

    report_error (p, _("Error: wget gave bad code or script died. code %d"),
                  code);
    return -1;
  }
  
  len = fread (&buf[1], sizeof (char), BUFLEN - 2, p->cmd_pipe);
  buf[len + 1] = '\0';
  g_strstrip (buf);

  pclose (p->cmd_pipe);
  p->cmd_pipe = NULL;

  if (ks->type == SOURCE_SCRIPT)
  {
    ks->tfile = g_strdup (buf);
    ks->next_dl = time (NULL) + ks->tlife;
    load_image_file (p);
    return 1;
  }
  else
  {
    /* if we get here with wget, then wget said something. This is generally
       not good, since we passed -q. We'll have to wait for it to die */
    
    report_error (p, _("wget said: \"%s\""), buf);
    return -1;
  }
}

/*
  rotate_sources ()

  moves the current source item to the end of the list, unless
  p is set for random.
*/
static void rotate_sources (KKamPanel *p)
{
  GList *link;
  int times, i, len;

  if (p == NULL || p->sources == NULL ||
        (len = g_list_length (p->sources)) == 1)
    return;

  times = p->random ? (rand () % (len - 1) + 1) : 1;
  for (i = 0; i < times; i++)
  {
    link = p->sources;
    p->sources = g_list_remove_link (p->sources, link);
    p->sources = g_list_concat (p->sources, link);
  }
}

/*
  update_image ()

  determines what the current image source is, and updates or
  starts an update as appropriate
*/
static void update_image (KKamPanel *p)
{
  KKamSource *ks;
  
  p->count = get_period (p);

  ks = panel_cursource (p);
  if (ks->img_name == NULL || ks->img_name[0] == '\0')
    return;
 
  if (ks->next_dl > time (NULL))
    load_image_file (p);
  else
  {
    tfile_release (ks);

    switch (ks->type)
    {
    case SOURCE_SCRIPT:
      start_script_dl (p);
      break;
    case SOURCE_URL:
      start_img_dl (p);
      break;
    case SOURCE_FILE:
      ks->tfile = g_strdup (ks->img_name);
      ks->next_dl = 0; /* next_dl is meaningless for SOURCE_FILE */
      load_image_file (p);
      break;
    default:
      report_error (p, _("Invalid type %d found in sources list!"), ks->type);
    }
  }
}

/*
  listurl_results ()

  when the listurl download is finished, reads the list and deletes it.
  Returns 0 if still waiting, 1 on success or error.
*/
static int listurl_results (KKamPanel *p)
{
  int code;
  char c;
  KKamSource *ks;

  ks = panel_cursource (p);

  if (fread (&c, sizeof (char), 1, p->listurl_pipe) < 1)
  {
    /* if we get EAGAIN, wait some more */
    if (ferror (p->listurl_pipe) && errno == EAGAIN)
      return 0;

    /* if we reach here the pipe is dead- the command has finished. */
    code = pclose (p->listurl_pipe);
    p->listurl_pipe = NULL;
  }
  else
    code = 256;

  /* pclose will return a -1 on a wait4 error. If that happens,
     we have no way to know whether wget succeeded. Just try */
  if (code <= 0)
  {
    kkam_read_list (p, p->listurl_file, 0);
    update_image (p);
  }
  else
    report_error (p, _("Error: wget listurl download died. code %d"), code);

  unlink (p->listurl_file);
  g_free (p->listurl_file);
  p->listurl_file = NULL;

  return 1;
}

/*
  kkam_update_plugin ()

  callback from gkrellm. Counts seconds until next update, and when
  count is reached, starts the update process.
*/
static void kkam_update_plugin ()
{
  int i;

  if (pGK->second_tick)
    for (i = 0; i < numpanels; i++)
    {
      if (panels[i].listurl_pipe)
        listurl_results (&panels[i]);
      else if (panels[i].cmd_pipe)
        cmd_results (&panels[i]);
      else if (--panels[i].count <= 0)
      {
        rotate_sources (&panels[i]);
        update_image (&panels[i]);
      }
    }
}

void showsource (KKamSource *s)
{
  fprintf (stderr, "name %s, type %d, seconds %d, tooltip %s\n",
                   s->img_name, s->type, s->seconds, s->tooltip);
}

static gint
wheel_callback(GtkWidget *widget, GdkEventScroll *ev)
{
  if (ev->direction == GDK_SCROLL_UP)
  {
    newnumpanels = MIN (numpanels + 1, MAX_NUMPANELS);
    change_num_panels ();
  }
  else if (ev->direction == GDK_SCROLL_DOWN)
  {
    newnumpanels = MAX (numpanels - 1, MIN_NUMPANELS);
    change_num_panels ();
  }
  return TRUE;
}

/*
  click_callback ()

  Process user's mouse-clicks on the panels. Open viewers, refresh images,
  increase/decrease number of panels, etc.
*/
static gint click_callback (GtkWidget *widget, GdkEventButton *ev, gpointer gw)
{
  gchar *cmd;
  int which;
  KKamSource *ks;
 
  which = GPOINTER_TO_INT (gw);
  if (!activenum (which))
    return FALSE;
  
  ks = panel_cursource (&panels[which]);

  switch (ev->button)
  {
  case 1: /* view image */
    if (ks->tfile)
    {
      if (viewer_prog == NULL || viewer_prog[0] == '\0')
        kkam_internal_viewer (ks->tfile);
      else
      {
        cmd = g_strdup_printf ("%s '%s' &", viewer_prog, ks->tfile);
        system (cmd);
        g_free (cmd);
      }
    }
    break;
  case 2: /* immediate update */
    panels[which].count = 0;
    ks->next_dl = 0;
    break;

  case 3:
    gkrellm_open_config_window (monitor);
    break;

  }
  return FALSE;
}

static gint panel_expose_event (GtkWidget *widget,
                                GdkEventExpose *ev,
                                gpointer gw)
{
  int which;

  which = GPOINTER_TO_INT (gw);
  if (!activenum (which))
    return FALSE;

  gdk_draw_drawable (widget->window,
       widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
       panels[which].panel->pixmap,
       ev->area.x, ev->area.y, ev->area.x, ev->area.y,
       ev->area.width, ev->area.height);

  return FALSE;
}

static void cb_aspect_box (KKamPanel *p)
{
  p->maintain_aspect = GTK_TOGGLE_BUTTON (p->aspect_box)->active;
  gkrellm_config_modified ();
  draw_pixbuf (p);
}

static void cb_boundary_spinner (gpointer w, KKamPanel *p)
{
  p->boundary = gtk_spin_button_get_value_as_int
                                 (GTK_SPIN_BUTTON (p->boundary_spinner));
  gkrellm_config_modified ();
  draw_pixbuf (p);
}

static void cb_height_spinner (gpointer w, KKamPanel *p)
{
  int newheight;

  newheight = gtk_spin_button_get_value_as_int (
                                   GTK_SPIN_BUTTON (p->height_spinner));
  
  if (newheight != p->height)
  {
    gkrellm_panel_configure_add_height (p->panel, newheight - p->height);
    p->height = newheight;
    gkrellm_panel_create (kkam_vbox, monitor, p->panel);

    gkrellm_config_modified ();                            
    draw_pixbuf (p);
  }
}

/*
  src_set ()

  sets the image source from the file selection dialog
*/
static void src_set (KKamPanel *p)
{
  g_free (p->source);
  p->source = g_strdup (gtk_file_selection_get_filename (
                                            GTK_FILE_SELECTION (filebox)));
  gkrellm_config_modified ();
  gtk_entry_set_text (GTK_ENTRY (p->sourcebox), p->source);
  gtk_widget_destroy (GTK_WIDGET (filebox));
  create_sources_list (p);
  p->count = get_period (p);
  update_image (p);
}

/*
  srcbrowse ()

  brings up a file browser window to select a source
*/
static void srcbrowse (KKamPanel *p)
{
  filebox = gtk_file_selection_new (_("Select Image Source"));
  g_signal_connect_swapped (
                     G_OBJECT (GTK_FILE_SELECTION (filebox)->ok_button),
                     "clicked", G_CALLBACK (src_set), (gpointer)p);
  g_signal_connect_swapped (
                     G_OBJECT (GTK_FILE_SELECTION (filebox)->cancel_button),
                     "clicked",
                     G_CALLBACK (gtk_widget_destroy),
                     (gpointer)filebox);

  gtk_widget_show (filebox);
}

/*
  srcreread ()

  rereads the source of a panel- this is useful if, for example,
  one changes a list and wants the new list used
*/
static void srcreread (KKamPanel *p)
{
  g_free (p->source);
  p->source = gtk_editable_get_chars (GTK_EDITABLE (p->sourcebox), 0, -1);
  create_sources_list (p);
  p->count = get_period (p);
  update_image (p);
}

/*
  create_configpanel_tab ()

  Creates a GtkVbox for the configuration of one of the panels
*/
static GtkWidget *create_configpanel_tab (int i)
{
  GtkWidget *vbox, *hbox, *button, *sourcelabel;

  vbox = gtk_vbox_new (FALSE, 0);

  gkrellm_gtk_spin_button (vbox, &panels[i].period_spinner,
                       (gfloat) panels[i].default_period,
                       1.0, (gfloat)MAX_SECONDS, 1.0, 10.0, 0, 0, NULL, NULL,
                       FALSE, _("Default number of seconds per update"));

  gkrellm_gtk_spin_button (vbox, &panels[i].height_spinner,
                       (gfloat) panels[i].height,
                       10.0, 100.0, 1.0, 5.0, 0, 0, cb_height_spinner, &panels[i],
                       FALSE, _("Height of viewer, in pixels"));

  hbox = gtk_hbox_new (FALSE, 0);
  gkrellm_gtk_spin_button (hbox, &panels[i].boundary_spinner,
                       (gfloat) panels[i].boundary,
                       0.0, 20.0, 1.0, 1.0, 0, 0, cb_boundary_spinner, &panels[i],
                       FALSE, _("Border size"));
  
  gkrellm_gtk_check_button (hbox, &panels[i].aspect_box,
                        panels[i].maintain_aspect,
                        TRUE, 0, _("Maintain aspect ratio"));
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  
  g_signal_connect_swapped (G_OBJECT (panels[i].aspect_box), "toggled",
                        G_CALLBACK (cb_aspect_box), (gpointer)&panels[i]);

  gkrellm_gtk_check_button (vbox, &panels[i].random_box,
                        panels[i].random,
                        TRUE, 0, _("Select list images at random"));

  hbox = gtk_hbox_new (FALSE, 0);
  sourcelabel = gtk_label_new (_("Image source:  "));
  panels[i].sourcebox = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (panels[i].sourcebox), panels[i].source);
  button = gtk_button_new_with_label (_("Browse.."));
  g_signal_connect_swapped (G_OBJECT (button), "clicked",
                             G_CALLBACK (srcbrowse), (gpointer)&panels[i]);
  gtk_box_pack_start (GTK_BOX (hbox), sourcelabel, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), panels[i].sourcebox, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);

  hbox = gtk_hbox_new (FALSE, 5);
  button = gtk_button_new_with_label (_("Reread source"));
  g_signal_connect_swapped (G_OBJECT (button), "clicked",
                             G_CALLBACK (srcreread), (gpointer)&panels[i]);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);

  gtk_widget_show_all (vbox);

  return vbox;
}

/*
  insert_configpanel_tab ()

  Creates a new configuration tab and inserts it into the config box
*/
static void insert_configpanel_tab (int i)
{
  GtkWidget *label, *configpanel;
  gchar *labeltxt;
  
  if (!GTK_IS_OBJECT (tabs))
    return;

  configpanel = create_configpanel_tab (i);
  
  labeltxt = g_strdup_printf (_("Panel #%i"), i + 1);
  label = gtk_label_new (labeltxt);
  g_free (labeltxt);
  
  gtk_notebook_insert_page (GTK_NOTEBOOK (tabs), configpanel, label, i + 1);
}

/*
  remove_configpanel_tab ()

  Removes a configuration tab from the config box
*/
static void remove_configpanel_tab (int i)
{
  if (!GTK_IS_OBJECT (tabs))
    return;

  gtk_notebook_remove_page (GTK_NOTEBOOK (tabs), i + 1);
}

/*
  change_num_panels ()

  called to move the number in `newnumpanels' into
  `numpanels' with all associated changes to the monitor
*/
static void change_num_panels ()
{
  int i;
  
  if (numpanels == newnumpanels)
    return;

  if (created)
  {
    for (i = numpanels - 1; i >= newnumpanels; i--)
    {
      remove_configpanel_tab (i);
      if (panels[i].cmd_pipe)
      {
        pclose (panels[i].cmd_pipe);
        panels[i].cmd_pipe = NULL;
      }
    }

    for (i = 0; i < MAX_NUMPANELS; i++)
    {
      gkrellm_panel_enable_visibility (panels[i].panel, i < newnumpanels,
                                       &(panels[i].visible));
    }

    for (i = numpanels; i < newnumpanels; i++)
    {
      insert_configpanel_tab (i);
      update_image (&panels[i]);
    }
  }

  numpanels = newnumpanels;
  gkrellm_config_modified ();
}

static void kkam_create_plugin (GtkWidget *vbox, gint first_create)
{
  int i;

  kkam_vbox = vbox;
  
  if (first_create)
  {
    change_num_panels ();
    created = 1;

    for (i = 0; i < MAX_NUMPANELS; i++)
      panels[i].panel = gkrellm_panel_new0 ();

    tooltipobj = gtk_tooltips_new ();

    srand (time (NULL)); /* randomize from timer */
  }

  img_style = gkrellm_meter_style (style_id);

  for (i = 0; i < MAX_NUMPANELS; i++)
  {
    gkrellm_panel_configure_add_height (panels[i].panel, panels[i].height);
    gkrellm_panel_create (vbox, monitor, panels[i].panel);
    gkrellm_panel_keep_lists (panels[i].panel, TRUE);

    panels[i].visible = TRUE;
    if (i >= numpanels)
    {
      gkrellm_panel_enable_visibility (panels[i].panel, FALSE,
                                       &(panels[i].visible));
    }
  }

  if (first_create)
  {
    for (i = 0; i < MAX_NUMPANELS; i++)
    {
      g_signal_connect (G_OBJECT (panels[i].panel->drawing_area),
            "expose_event", G_CALLBACK (panel_expose_event),
            GINT_TO_POINTER (i));
      g_signal_connect (G_OBJECT (panels[i].panel->drawing_area),
            "button_press_event", G_CALLBACK (click_callback),
            GINT_TO_POINTER (i));
      g_signal_connect (G_OBJECT (panels[i].panel->drawing_area),
            "scroll_event", G_CALLBACK (wheel_callback), NULL);
            
      gkrellm_draw_panel_layers (panels[i].panel);
      if (i < numpanels)
        update_image (&panels[i]);
    }
  }
  else
  {
    for (i = 0; i < numpanels; i++)
      if (panels[i].decal && panels[i].decal->pixmap)
      {
        gkrellm_draw_decal_pixmap (panels[i].panel, panels[i].decal, 0);
        gkrellm_draw_panel_layers (panels[i].panel);
      }
  }
}

static KKamSource *kkam_source_new ()
{
  return g_new0 (KKamSource, 1);
}

static void kkam_source_free (KKamSource *ks)
{
  if (ks->tfile)
    tfile_release (ks);
  g_free (ks->img_name);
  g_free (ks->tooltip);
  g_free (ks);
}

static void destroy_sources_list (KKamPanel *p)
{
  g_list_foreach (p->sources, (GFunc)kkam_source_free, NULL);
  g_list_free (p->sources);
  p->sources = NULL;
}

/*
  addto_sources_list ()

  Adds a component to the sources list for panel p. The name
  and type are copied from name and type. The new component
  is returned, in case it needs to be modified further.
*/
static KKamSource *addto_sources_list (KKamPanel *p, char *name, SourceEnum type)
{
  KKamSource *ks;

  ks = kkam_source_new ();
  ks->type = type;
  ks->img_name = g_strdup (name);
  ks->tfile = NULL;
  ks->next_dl = 0;
  p->sources = g_list_append (p->sources, (gpointer)ks);

  return ks;
}

/*
  endswith ()

  returns TRUE if the end of str matches endstr
*/
static gboolean endswith (char *str, char *endstr)
{
  int len, lenend;

  if ((len = strlen (str)) < (lenend = strlen (endstr)))
    return FALSE;
  return !strcmp (&str[len - lenend], endstr);
}

/*
  source_type_of ()

  determines what type of source is represented by the given
  string, according to these rules, checked in order:

  - A source beginning with 'http:' or 'ftp:' and ending in '.list'
     is a SOURCE_LISTURL.
  - A source beginning with 'http:' or 'ftp:' is a SOURCE_URL.
  - A source that begins with '-x' is a SOURCE_SCRIPT.
  - A source with an extension in IMG_EXTENSIONS is a SOURCE_FILE.
  - A source that matches a filename that is executable by the user is
     a SOURCE_SCRIPT.
  - A source ending in [.-]list is a SOURCE_LIST.
  - If none of the above apply, and the source is a filename, the
     file is opened. If it 'looks' like a GKrellKam list configuration
     file, it is a SOURCE_LIST.
  - Otherwise, it is a SOURCE_FILE.

FIXME: spaghetti!
*/
static SourceEnum source_type_of (char *def)
{
  int i, p, len;
  FILE *test;
  gchar **words;
  unsigned char buf[BUFLEN];
  
  words = g_strsplit (def, " ", 2);
  if (!words || !words[0]) /* wish I didn't need this */
    return SOURCE_FILE;

  if (!strncmp (words[0], "http:", 5) || !strncmp (words[0], "ftp:", 4))
  {
    if (endswith (words[0], ".list") || endswith (words[0], "-list"))
    {
      g_strfreev (words);
      return SOURCE_LISTURL;
    }

    g_strfreev (words);
    return SOURCE_URL;
  }
  if (!strcmp (words[0], "-x"))
  {
    g_strfreev (words);
    return SOURCE_SCRIPT;
  }
  for (i = 0; i < (sizeof (IMG_EXTENSIONS) / sizeof (IMG_EXTENSIONS[0])); i++)
  {
    if (endswith (words[0], IMG_EXTENSIONS[i]))
    {
      g_strfreev (words);
      return SOURCE_FILE;
    }
  }
  if (access (words[0], X_OK) == 0)
  {
    g_strfreev (words);
    return SOURCE_SCRIPT;
  }
  if (endswith (words[0], ".list") || endswith (words[0], "-list"))
  {
    g_strfreev (words);
    return SOURCE_LIST;
  }
  
  if ((test = fopen (words[0], "r")) != NULL)
  {
    len = fread (buf, sizeof (buf[0]), BUFLEN, test);
    for (p = 0; p < len; p++)
      if (!isgraph (buf[p]) && !isspace (buf[p]))
      {
        fclose (test);
        g_strfreev (words);
        return SOURCE_FILE;
      }
    g_strfreev (words);
    fclose (test);
    return SOURCE_LIST;
  }

  g_strfreev (words);
  return SOURCE_FILE;
}

/*
  nextword ()

  convenience function for the list reader; returns a pointer in s
  to the beginning of the next parameter (non-space following a colon)
*/
char *nextword (char *s)
{
  char *ret;

  for (ret = s; *ret != ':'; ret++) ;
  ret++;
  for ( ; isspace (*ret); ret++) ;
  return ret;
}

/*
  kkam_read_list ()

  reads a GKrellKam list configuration file and loads its
  data into the panel's source GList
*/
static void kkam_read_list (KKamPanel *p, char *listname, int depth)
{
  KKamSource *ks = NULL;
  SourceEnum typ;
  FILE *listfile;
  char buf[BUFLEN];
  int thislist_error = 0;

  if (depth > MAX_DEPTH)
  {
    report_error (p, _("Maximum recursion depth exceeded reading list %s; "
                  "perhaps a list is trying to load itself?"), listname);
    return;
  }

  if ((listfile = fopen (listname, "r")) == NULL)
    return;
  
  while (fgets (buf, BUFLEN, listfile))
  {
    g_strchomp (buf);

    switch (buf[0])
    {
    case '\0':
    case '#':
      ks = NULL;
      break;

    case '\t':
      if (ks != NULL)
      {
        if (!strncmp (&buf[1], "tooltip:", 8))
          ks->tooltip = g_strdup (nextword (&buf[1]));
        else if (!strncmp (&buf[1], "seconds:", 8))
          ks->seconds = CLAMP (atoi (nextword (&buf[1])), 1, MAX_SECONDS);
        else if (!strncmp (&buf[1], "refresh:", 8))
          ks->tlife = CLAMP (atoi (nextword (&buf[1])), 1, MAX_SECONDS);
      }
      else if (thislist_error == 0) /* only show one error per file */
      {
        thislist_error = 1;
        report_error (p, _("In list %s, property line \"%s\" isn't associated"
                           " with any source!"), listname, &buf[1]);
      }
      break;
      
    default:
      if (!strncmp (buf, "image:", 6))
        ks = addto_sources_list (p, nextword (buf), SOURCE_FILE);
      else if (!strncmp (buf, "script:", 7))
        ks = addto_sources_list (p, nextword (buf), SOURCE_SCRIPT);
      else if (!strncmp (buf, "url:", 4))
        ks = addto_sources_list (p, nextword (buf), SOURCE_URL);
      else if (!strncmp (buf, "list:", 5))
      {
        kkam_read_list (p, nextword (buf), depth + 1);
        ks = NULL;
      }
      else
      {
        typ = source_type_of (buf);
        if (typ == SOURCE_LIST)
        {
          kkam_read_list (p, buf, depth + 1);
          ks = NULL;
        }
        else
          ks = addto_sources_list (p, buf, typ);
      }
    }
  }
}

static void kkam_read_listurl (KKamPanel *p, char *source)
{
  gchar *wget_str;
  char tmpfile[] = TEMPTEMPLATE "-urllistXXXXXX";
  int tmpfd;

  if (p->listurl_pipe) /* already open */
    return;

  tmpfd = mkstemp (tmpfile); /* this will create the file, perm 0600 */
  if (tmpfd == -1)
  {
    report_error (p, _("Couldn't create temporary file for list download: %s"),
                  strerror (errno));
    return;
  }
  close (tmpfd);

  wget_str = g_strdup_printf ("wget -q %s -O %s \"%s\"",
                              wget_opts, tmpfile, source);

  p->listurl_pipe = popen (wget_str, "r");
  g_free (wget_str);
  if (p->listurl_pipe == NULL)
  {
    unlink (tmpfile);
    report_error (p, _("Couldn't start wget for list download: %s"),
                  strerror (errno));
    return;
  }
  
  p->listurl_file = g_strdup (tmpfile);
  fcntl (fileno (p->listurl_pipe), F_SETFL, O_NONBLOCK);

  gtk_tooltips_set_tip (tooltipobj, p->panel->drawing_area,
                        _("Downloading list.."), NULL);
}

/*
  create_sources_list ()

  Takes the 'source' of a panel- which can be a web address,
  local image, script, list, etc.. and translates that into
  a GList of sources. For most source types, obviously, the
  sources GList will only have one entry.
*/
static void create_sources_list (KKamPanel *p)
{
  SourceEnum s;

  if (p->sources)
    destroy_sources_list (p);

  if (p->source && p->source[0])
  {
    switch (s = source_type_of (p->source))
    {
    case SOURCE_URL:
    case SOURCE_FILE:
    case SOURCE_SCRIPT:
      addto_sources_list (p, p->source, s);
      break;
    case SOURCE_LIST:
      kkam_read_list (p, p->source, 0);
      break;
    case SOURCE_LISTURL:
      kkam_read_listurl (p, p->source);
      break;
    }
  }
}

/*
  update_source_config ()

  Translates a 0.2.2[bcd]-style config into an appropriate source item
*/
static void update_source_config (KKamPanel *p, char *val)
{
  SourceEnum t;
  gchar **words;
  gchar *scr;
  int i;

  g_strdelimit (val, " \t\n", '\n');
  words = g_strsplit (val, "\n", 0);
  
  for (i = 0; words[i]; i++)
  {
    if (!strcmp (words[i], "-l") || !strcmp (words[i], "--list"))
    {
      g_free (words[i]);
      words[i] = g_strdup ("");
    }
    else if (!strcmp (words[i], "-x") || !strcmp (words[i], "--execute"))
    {
      g_free (words[i]);
      words[i] = g_strdup ("-x");
      scr = g_strjoinv (" ", &words[i]);
      addto_sources_list (p, scr, SOURCE_SCRIPT);
      g_free (p->source);
      p->source = scr;
      break;
    }
    else if (!strcmp (words[i], "-r") || !strcmp (words[i], "--random"))
      p->random = TRUE;
    else
    {
      t = source_type_of (words[i]);
      g_free (p->source);
      p->source = g_strdup (words[i]);
      if (t == SOURCE_LIST)
        kkam_read_list (p, words[i], 0);
      else
        addto_sources_list (p, words[i], source_type_of (val));
    }
  }
  g_strfreev (words);
}

/*
  update_script_config ()

  Translates a pre-0.2.2-style config into an appropriate source item
*/
static void update_script_config (KKamPanel *p, char *val)
{
  gchar *chopmeup;
  char *firstword, *rest;

  chopmeup = g_strdup_printf ("%s\n \n", g_strstrip (val));
  firstword = strtok (chopmeup, " \n");
  if (!firstword)
    return;
  rest = strtok (NULL, "\n");
  if (!rest)
    return;
  g_strstrip (rest);
  
  /* If the old update_script item was using krellkam_load, parse
     its parameters. If it was using a different script, put it with
     the appropriate type into the source list */

  if (!strcmp (basename (firstword), "krellkam_load"))
    update_source_config (p, rest);
  else
  {
    g_free (p->source);
    p->source = g_strdup_printf ("-x %s", val);
    addto_sources_list (p, val, SOURCE_SCRIPT);
  }
  g_free (chopmeup);
}

static void kkam_save_config (FILE *f)
{
  int i;

  if (viewer_prog && viewer_prog[0])
    fprintf (f, "%s viewer_prog %s\n", PLUGIN_KEYWORD, viewer_prog);

  fprintf (f, "%s popup_errors %d\n", PLUGIN_KEYWORD, popup_errors);
  fprintf (f, "%s numpanels %d\n", PLUGIN_KEYWORD, numpanels);

  for (i = 0; i < MAX_NUMPANELS; i++)
  {
    fprintf (f, "%s %d sourcedef %s\n",
             PLUGIN_KEYWORD, i + 1, panels[i].source);
    fprintf (f, "%s %d options %d.%d.%d.%d.%d\n",
             PLUGIN_KEYWORD, i + 1,
             panels[i].height,
             panels[i].default_period,
             panels[i].boundary,
             panels[i].maintain_aspect,
             panels[i].random);
  }
}

static void kkam_load_config (gchar *arg)
{
  gchar *config_item, *value;
  int which;

  config_item = strtok (arg, " \n");
  if (!config_item)
    return;
  which = atoi (config_item);
  if (which)
  {
    config_item = strtok (NULL, " \n");
    if (!config_item)
      return;
    which--;
  }
  value = strtok (NULL, "\n");
  if (value == NULL)
    value = "";

  if (!strcmp (config_item, "options"))
  {
    if (validnum (which))
    {
      sscanf (value, "%d.%d.%d.%d.%d",
              &(panels[which].height),
              &(panels[which].default_period),
              &(panels[which].boundary),
              &(panels[which].maintain_aspect),
              &(panels[which].random));
      
      panels[which].height = CLAMP (panels[which].height, 10, 100);
      panels[which].default_period =
                           CLAMP (panels[which].default_period, 1, MAX_SECONDS);
      panels[which].boundary = CLAMP (panels[which].boundary, 0, 20);
      panels[which].maintain_aspect =
                          (gboolean)CLAMP (panels[which].maintain_aspect, 0, 1);
      panels[which].random = (gboolean)CLAMP (panels[which].random, 0, 1);
    }
  }
  else if (!strcmp (config_item, "sourcedef"))
  {
    if (validnum (which))
    {
      g_free (panels[which].source);
      panels[which].source = g_strstrip (g_strdup (value));
      create_sources_list (&panels[which]);
    }
  }
  else if (!strcmp (config_item, "viewer_prog"))
  {
    g_free (viewer_prog);
    viewer_prog = g_strdup (value);
  }
  else if (!strcmp (config_item, "popup_errors"))
  {
    popup_errors = atoi (value);
  }
  else if (!strcmp (config_item, "numpanels"))
  {
    newnumpanels = CLAMP (atoi (value), MIN_NUMPANELS, MAX_NUMPANELS);
    change_num_panels ();
  }
  /* backwards compatibility */
  else if (!strcmp (config_item, "img_height"))
  {
    if (validnum (which))
      panels[which].height = CLAMP (atoi (value), 10, 100);
  }
  else if (!strcmp (config_item, "period"))
  {
    if (validnum (which))
      panels[which].default_period = CLAMP (atoi (value), 1, MAX_SECONDS);
  }
  else if (!strcmp (config_item, "maintain_aspect"))
  {
    if (validnum (which))
      panels[which].maintain_aspect = (gboolean)CLAMP (atoi (value), 0, 1);
  }
  else if (!strcmp (config_item, "boundary"))
  {
    if (validnum (which))
      panels[which].boundary = CLAMP (atoi (value), 0, 20);
  }
  else if (!strcmp (config_item, "update_period"))
  {
    /* this is in minutes */
    if (validnum (which))
      panels[which].default_period = MAX (atoi (value) * 60, 1);
  }
  else if (!strcmp (config_item, "update_script"))
  {
    /* update old config item */
    if (validnum (which))
      update_script_config (&panels[which], value);
  }
  else if (!strcmp (config_item, "source"))
  {
    /* backwards compat for 0.2.2[bcd] */
    if (validnum (which))
      update_source_config (&panels[which], value);
  }
}

static void cb_numpanel_spinner ()
{
  newnumpanels = gtk_spin_button_get_value_as_int
                                       (GTK_SPIN_BUTTON (numpanel_spinner));
  change_num_panels ();
}

static void kkam_create_tab (GtkWidget *tab_vbox)
{
  GtkWidget *vbox, *tablabel;
  GtkWidget *text;
  GtkWidget *hbox, *configpanel, *about;
  GtkAdjustment *numadj;
  gchar *tabname;
  int i;

  if (tabs)
    g_object_unref (G_OBJECT (tabs));
  
  tabs = gtk_notebook_new ();  
  gtk_notebook_set_tab_pos (GTK_NOTEBOOK (tabs), GTK_POS_TOP);
  gtk_box_pack_start (GTK_BOX (tab_vbox), tabs, TRUE, TRUE, 0);
  g_object_ref (G_OBJECT (tabs));

  /* main options tab */
  vbox = gkrellm_gtk_framed_notebook_page (tabs, _("Options"));
  
  hbox = gtk_hbox_new (FALSE, 0);
  viewerbox = gtk_entry_new ();
  if (viewer_prog)
    gtk_entry_set_text (GTK_ENTRY (viewerbox), viewer_prog);

  gtk_box_pack_start (GTK_BOX (hbox),
                      gtk_label_new (_("Path to image viewer program:")),
                      FALSE, FALSE, 10);
  gtk_box_pack_start (GTK_BOX (hbox), viewerbox, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);
  
  hbox = gtk_hbox_new (FALSE, 0);
  popup_errors_box = gtk_check_button_new_with_label (_("Popup errors"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (popup_errors_box),
                                popup_errors);
  gtk_box_pack_start (GTK_BOX (hbox), popup_errors_box, FALSE, FALSE, 10);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);

  numadj = (GtkAdjustment *) gtk_adjustment_new ((gfloat) numpanels,
                             (gfloat) MIN_NUMPANELS,
                             (gfloat) MAX_NUMPANELS,
                             1.0, 1.0, 0);
  numpanel_spinner = gtk_spin_button_new (numadj, 1.0, 0);
  g_signal_connect (G_OBJECT (numpanel_spinner), "changed",
                      G_CALLBACK (cb_numpanel_spinner), NULL);
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), numpanel_spinner, FALSE, FALSE, 10);
  gtk_box_pack_start (GTK_BOX (hbox),
                      gtk_label_new (_("Number of panels")),
                      FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, FALSE, 0);
  
  /* individual panel options tabs */
  for (i = 0; i < MAX_NUMPANELS; i++)
  {
    configpanel = create_configpanel_tab (i);

    tabname = g_strdup_printf (_("Panel #%d"), i + 1);
    tablabel = gtk_label_new (tabname);
    g_free (tabname);
    
    if (i < numpanels)
      gtk_notebook_append_page (GTK_NOTEBOOK (tabs), configpanel, tablabel);
  }

  /* Info tab */
  vbox = gkrellm_gtk_framed_notebook_page (tabs, _("Info"));

  text = gkrellm_gtk_scrolled_text_view(vbox, NULL,
                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  for (i = 0; i < sizeof(kkam_info_text) / sizeof(gchar *); ++i)
     gkrellm_gtk_text_view_append(text, _(kkam_info_text[i]));

  /* About tab */
  vbox = gkrellm_gtk_framed_notebook_page (tabs, _("About"));
  about = gtk_label_new (kkam_about_text);
  gtk_box_pack_start (GTK_BOX (vbox), about, TRUE, TRUE, 0);
}

static void kkam_apply_config ()
{
  int i, diff;
  gchar *newsource;

  for (i = 0; i < numpanels; i++)
  {
    newsource = gtk_editable_get_chars
                         (GTK_EDITABLE (panels[i].sourcebox), 0, -1);
    diff = strcmp (newsource, panels[i].source);
    g_free (panels[i].source);
    panels[i].source = newsource;
    if (diff)
      create_sources_list (&panels[i]);

    panels[i].default_period = gtk_spin_button_get_value_as_int
                               (GTK_SPIN_BUTTON (panels[i].period_spinner));
    panels[i].maintain_aspect = GTK_TOGGLE_BUTTON (panels[i].aspect_box)->active;
    panels[i].random = GTK_TOGGLE_BUTTON (panels[i].random_box)->active;
    panels[i].boundary = gtk_spin_button_get_value_as_int
                               (GTK_SPIN_BUTTON (panels[i].boundary_spinner));
  }

  newnumpanels = gtk_spin_button_get_value_as_int
                                 (GTK_SPIN_BUTTON (numpanel_spinner));
  change_num_panels ();

  if (viewer_prog)
    g_free (viewer_prog);
  viewer_prog = g_strdup
                   (gtk_editable_get_chars (GTK_EDITABLE (viewerbox), 0, -1));

  popup_errors = gtk_toggle_button_get_active
                                       (GTK_TOGGLE_BUTTON (popup_errors_box));
}

void kkam_cleanup ()
{
  int i;

  for (i = 0; i < MAX_NUMPANELS; i++)
    destroy_sources_list (&panels[i]);
}

static GkrellmMonitor kam_mon  =
{
  PLUGIN_NAME,         /* Name, for config tab.                    */
  0,                   /* Id,  0 if a plugin                       */
  kkam_create_plugin,  /* The create_plugin() function             */
  kkam_update_plugin,  /* The update_plugin() function             */
  kkam_create_tab,     /* The create_plugin_tab() config function  */
  kkam_apply_config,   /* The apply_plugin_config() function       */

  kkam_save_config,    /* The save_plugin_config() function        */
  kkam_load_config,    /* The load_plugin_config() function        */
  PLUGIN_KEYWORD,      /* config keyword                           */

  NULL,                /* Undefined 2                              */
  NULL,                /* Undefined 1                              */
  NULL,                /* private                                  */

  MON_CPU,             /* Insert plugin before this monitor.       */
  NULL,                /* Handle if a plugin, filled in by GKrellM */
  NULL                 /* path if a plugin, filled in by GKrellM   */
};


#if defined(WIN32)
    __declspec(dllexport) GkrellmMonitor *
    gkrellm_init_plugin(win32_plugin_callbacks* calls)
#else
    GkrellmMonitor *
    gkrellm_init_plugin()
#endif
{
  int i;
  
  pGK = gkrellm_ticks();
  style_id = gkrellm_add_meter_style (&kam_mon, PLUGIN_STYLE);
  panels = g_new0 (KKamPanel, MAX_NUMPANELS);
  
  /* the g_new0 initialized everything to 0- pretty convenient for
     almost everything.. */

  for (i = 0; i < MAX_NUMPANELS; i++)
  {
    panels[i].height = 50;
    panels[i].source = g_strdup (default_source[i]);
    panels[i].default_period = 60;
  }

  /* gkrellm now hooks INT and QUIT and exits nicely. This will work. */

  g_atexit (kkam_cleanup);
 
  return (monitor = &kam_mon);
}