STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
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
/**
 * \defgroup ctk CTK graphical user interface
 *
 * The Contiki Toolkit (CTK) provides the graphical user interface for
 * the Contiki system.
 *
 * @{
 */
 
/**
 * \file
 * The Contiki Toolkit CTK, the Contiki GUI.
 * \author Adam Dunkels <adam@dunkels.com>
 */
 
/*
 * Copyright (c) 2002-2003, Adam Dunkels.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 *
 */
 
#include <string.h>
 
#include "contiki.h"
 
#include "ctk/ctk.h"
#include "ctk/ctk-draw.h"
#include "ctk/ctk-mouse.h"
 
static unsigned char height, width;
 
static unsigned char mode;
 
#if CTK_CONF_WINDOWS
static struct ctk_window desktop_window;
static struct ctk_window *windows;
static struct ctk_window *dialog;
#else /* CTK_CONF_WINDOWS */
static struct ctk_window *window;
#endif /* CTK_CONF_WINDOWS */
 
#if CTK_CONF_MENUS
static struct ctk_menus menus;
static struct ctk_menu *lastmenu;
static struct ctk_menu desktopmenu;
static unsigned char maxnitems;
#endif /* CTK_CONF_MENUS */
 
#ifndef NULL
#define NULL (void *)0
#endif /* NULL */
 
#define REDRAW_NONE         0
#define REDRAW_ALL          1
#define REDRAW_FOCUS        2
#define REDRAW_WIDGETS      4
#define REDRAW_MENUS        8
#define REDRAW_MENUPART     16
 
#define MAX_REDRAWWIDGETS 4
static unsigned char redraw;
static struct ctk_widget *redraw_widgets[MAX_REDRAWWIDGETS];
static unsigned char redraw_widgetptr;
 
#if CTK_CONF_ICONS
static unsigned char iconx, icony;
#define ICONX_START  (width - 6)
#define ICONY_START  (height - 6 - CTK_CONF_MENUS)
#define ICONX_DELTA  -16
#define ICONY_DELTA  -5
#define ICONY_MAX    height
#endif /* CTK_CONF_ICONS */
 
#ifndef ctk_arch_keyavail
unsigned char ctk_arch_keyavail(void);
#endif /* ctk_arch_keyavail */
 
#ifndef ctk_arch_getkey
ctk_arch_key_t ctk_arch_getkey(void);
#endif /* ctk_arch_getkey */
 
#ifndef ctk_arch_isprint
unsigned char ctk_arch_isprint(ctk_arch_key_t key);
#endif /* ctk_arch_isprint */
 
PROCESS(ctk_process, "CTK Contiki GUI");
 
/**
 * \defgroup ctkevents CTK events
 * @{
 */
process_event_t
 
  /**
   * Emitted for every key being pressed.
   *
   * The key is passed as signal data.*/
  ctk_signal_keypress,
  
  /** Emitted when a widget is activated (pressed). A pointer to the
      widget is passed as signal data. */
  ctk_signal_widget_activate,
  
  /** Same as ctk_signal_widget_activate. */
  ctk_signal_button_activate,
 
  /** Emitted when a widget is selected. A pointer to the widget is
      passed as signal data. */
  ctk_signal_widget_select,
  
  /** Same as ctk_signal_widget_select. */
  ctk_signal_button_hover,
 
  /** Emitted when a hyperlink is activated. The signal is broadcast
      to all listeners. */
  ctk_signal_hyperlink_activate,
 
  /** Same as ctk_signal_widget_select. */
  ctk_signal_hyperlink_hover;
 
  /** Emitted when a menu item is activated. The number of the menu
      item is passed as signal data. */
process_event_t ctk_signal_menu_activate;
 
  /** Emitted when a window is closed. A pointer to the window is
      passed as signal data. */
process_event_t ctk_signal_window_close;
 
#if CTK_CONF_MOUSE_SUPPORT
  /** Emitted when the mouse pointer is moved. A NULL pointer is
      passed as signal data and it is up to the listening process to
      check the position of the mouse using the CTK mouse API.*/
process_event_t ctk_signal_pointer_move,
  /** Emitted when a mouse button is pressed. The button is passed as
      signal data to the listening process. */
  ctk_signal_pointer_button;
#endif /* CTK_CONF_MOUSE_SUPPORT */
 
#if CTK_CONF_SCREENSAVER
/** Emitted when the user has been idle long enough for the
    screensaver to start. */
process_event_t ctk_signal_screensaver_stop,
  /** Emitted when the user presses a key or moves the mouse when the
      screensaver is active. */
  ctk_signal_screensaver_start;
#endif /* CTK_CONF_SCREENSAVER */
 
/** @} */
 
#if CTK_CONF_MOUSE_SUPPORT
unsigned short mouse_x, mouse_y, mouse_button;
#endif /* CTK_CONF_MOUSE_SUPPORT */
 
#if CTK_CONF_SCREENSAVER
static unsigned short screensaver_timer = 0;
unsigned short ctk_screensaver_timeout = (5*60);
static struct timer timer;
#endif /* CTK_CONF_SCREENSAVER */
 
static void CC_FASTCALL
textentry_input(ctk_arch_key_t c,
        CC_REGISTER_ARG struct ctk_textentry *t);
 
#if CTK_CONF_MENUS
/*---------------------------------------------------------------------------*/
/**
 * \internal Creates the Desktop menu.
 *
 * Creates the leftmost menu, "Desktop". Since the desktop menu
 * contains the list of all open windows, this function will be called
 * whenever a window is opened or closed.
 */
/*---------------------------------------------------------------------------*/
static void
make_desktopmenu(void)
{
  struct ctk_window *w;
  
  desktopmenu.nitems = 0;
  
  if(windows == NULL) {
    ctk_menuitem_add(&desktopmenu, "(No windows)");
  } else {
    for(w = windows; w != NULL; w = w->next) {
      ctk_menuitem_add(&desktopmenu, w->title);
    }
  }
}
#endif /* CTK_CONF_MENUS */
/*---------------------------------------------------------------------------*/
#if CTK_CONF_ICONS
static void
arrange_icons(void)
{
  struct ctk_widget *icon;
 
  iconx = ICONX_START;
  icony = ICONY_START;
  
  for(icon = desktop_window.active; icon != NULL; icon = icon->next) {
    
    icon->x = iconx;
    icon->y = icony;
    
    icony += ICONY_DELTA;
    if(icony >= ICONY_MAX) {
      icony = ICONY_START;
      iconx += ICONX_DELTA;
    }
  }
}
#endif /* CTK_CONF_ICONS */
/*---------------------------------------------------------------------------*/
void
ctk_restore(void)
{
  ctk_draw_init();
 
  height = ctk_draw_height();
  width = ctk_draw_width();
 
#if CTK_CONF_ICONS
  arrange_icons();
#endif /* CTK_CONF_ICONS */
 
  redraw = REDRAW_ALL;
}
/*---------------------------------------------------------------------------*/
 
/**
 * \addtogroup ctkappfunc
 * @{
 */
 
/*---------------------------------------------------------------------------*/
/**
 * Sets the current CTK mode.
 *
 * The CTK mode can be either CTK_MODE_NORMAL, CTK_MODE_SCREENSAVER or
 * CTK_MODE_EXTERNAL. CTK_MODE_NORMAL is the normal mode, in which
 * keypresses and mouse pointer movements are processed and the screen
 * is redrawn. In CTK_MODE_SCREENSAVER, no screen redraws are
 * performed and the first key press or pointer movement will cause
 * the ctk_signal_screensaver_stop to be emitted. In the
 * CTK_MODE_EXTERNAL mode, key presses and pointer movements are
 * ignored and no screen redraws are made.
 *
 * \param m The mode.
 */
/*---------------------------------------------------------------------------*/
void
ctk_mode_set(unsigned char m) {
  mode = m;
}
/*---------------------------------------------------------------------------*/
/**
 * Retrieves the current CTK mode.
 *
 * \return The current CTK mode.
 */
/*---------------------------------------------------------------------------*/
unsigned char
ctk_mode_get(void) {
  return mode;
}
/*---------------------------------------------------------------------------*/
/**
 * Add an icon to the desktop.
 *
 * \param icon The icon to be added.
 *
 * \param p The process that owns the icon.
 */
/*---------------------------------------------------------------------------*/
void
ctk_icon_add(CC_REGISTER_ARG struct ctk_widget *icon, struct process *p)
{
#if CTK_CONF_ICONS
  icon->widget.icon.owner = p;
  ctk_widget_add(&desktop_window, icon);
  arrange_icons();
#endif /* CTK_CONF_ICONS */
}
#if CTK_CONF_WINDOWS
/*---------------------------------------------------------------------------*/
/**
 * Open a dialog box.
 *
 * \param d The dialog to be opened.
 */
/*---------------------------------------------------------------------------*/
void
ctk_dialog_open(struct ctk_window *d)
{
  dialog = d;
  redraw |= REDRAW_FOCUS;
}
/*---------------------------------------------------------------------------*/
/**
 * Close the dialog box, if one is open.
 *
 */
/*---------------------------------------------------------------------------*/
void
ctk_dialog_close(void)
{
  dialog = NULL;
  redraw |= REDRAW_ALL;
}
#endif /* CTK_CONF_WINDOWS */
/*---------------------------------------------------------------------------*/
/**
 * Open a window, or bring window to front if already open.
 *
 * \param w The window to be opened.
 */
/*---------------------------------------------------------------------------*/
void
ctk_window_open(CC_REGISTER_ARG struct ctk_window *w)
{
#if CTK_CONF_WINDOWS
  struct ctk_window *w2;
  
  /* Check if already open. */
  for(w2 = windows; w2 != w && w2 != NULL; w2 = w2->next);
  if(w2 == NULL) {
   /* Not open, so we add it at the head of the list of open
       windows. */
    w->next = windows;
    if(windows != NULL) {
      windows->prev = w;
    }
    windows = w;
    w->prev = NULL;
  } else {
    /* Window already open, so we move it to the front of the windows
       list. */
    if(w != windows) {
      if(w->next != NULL) {
    w->next->prev = w->prev;
      }
      if(w->prev != NULL) {
    w->prev->next = w->next;
      }
      w->next = windows;
      windows->prev = w;
      windows = w;
      w->prev = NULL;
    }
  }
#else /* CTK_CONF_WINDOWS */
  window = w;
#endif /* CTK_CONF_WINDOWS */
 
#if CTK_CONF_MENUS
  /* Recreate the Desktop menu's window entries.*/
  make_desktopmenu();
#endif /* CTK_CONF_MENUS */
 
  redraw |= REDRAW_ALL;
}
/*---------------------------------------------------------------------------*/
/**
 * Close a window if it is open.
 *
 * If the window is not open, this function does nothing.
 *
 * \param w The window to be closed.
 */
/*---------------------------------------------------------------------------*/
void
ctk_window_close(struct ctk_window *w)
{
#if CTK_CONF_WINDOWCLOSE
  static struct ctk_window *w2;
 
  if(w == NULL) {
    return;
  }
  
  /* Check if the window to be closed is the first window on the list. */
  if(w == windows) {
    windows = w->next;
    if(windows != NULL) {
      windows->prev = NULL;
    }
    w->next = w->prev = NULL;
  } else {
    /* Otherwise we step through the list until we find the window
       before the one to be closed. We then redirect its ->next
       pointer and its ->next->prev. */
    for(w2 = windows; w2 != NULL && w2->next != w; w2 = w2->next);
 
    if(w2 == NULL) {
      /* The window wasn't open, so there is nothing more for us to do. */
      return;
    }
 
    if(w->next != NULL) {
      w->next->prev = w->prev;
    }
    w2->next = w->next;
    
    w->next = w->prev = NULL;
  }
  
#if CTK_CONF_MENUS
  /* Recreate the Desktop menu's window entries.*/
  make_desktopmenu();
#endif /* CTK_CONF_MENUS */
  redraw |= REDRAW_ALL;
#endif /* CTK_CONF_WINDOWCLOSE */
}
#if CTK_CONF_WINDOWS
/*---------------------------------------------------------------------------*/
/**
 * \internal Create the move and close buttons on the window titlebar.
 */
/*---------------------------------------------------------------------------*/
static void
make_windowbuttons(CC_REGISTER_ARG struct ctk_window *window)
{
  unsigned char placement;
 
  if(ctk_draw_windowtitle_height >= 2) {
    placement = -1 - ctk_draw_windowtitle_height/2;
  } else {
    placement = -1;
  }
#if CTK_CONF_WINDOWMOVE
  CTK_BUTTON_NEW(&window->titlebutton, 0, placement,
         window->titlelen, window->title);
#else
  CTK_LABEL_NEW(&window->titlebutton, 0, placement,
        window->titlelen, 1, window->title);
#endif /* CTK_CONF_WINDOWMOVE */
  CTK_WIDGET_ADD(window, &window->titlebutton);
 
#if CTK_CONF_WINDOWCLOSE
  CTK_BUTTON_NEW(&window->closebutton, window->w - 3, placement,
         1, "x");
#else
  CTK_LABEL_NEW(&window->closebutton, window->w - 4, placement,
        3, 1, "   ");
#endif /* CTK_CONF_WINDOWCLOSE */
  CTK_WIDGET_ADD(window, &window->closebutton);
}
#endif /* CTK_CONF_WINDOWS */
/*---------------------------------------------------------------------------*/
/**
 * Remove all widgets from a window.
 *
 * \param w The window to be cleared.
 */
/*---------------------------------------------------------------------------*/
void
ctk_window_clear(struct ctk_window *w)
{
  w->active = w->inactive = w->focused = NULL;
  
#if CTK_CONF_WINDOWS
  make_windowbuttons(w);
#endif /* CTK_CONF_WINDOWS */
}
/*---------------------------------------------------------------------------*/
/**
 * Add a menu to the menu bar.
 *
 * \param menu The menu to be added.
 *
 * \note Do not call this function multiple times for the same menu,
 * as no check is made to see if the menu already is in the menu bar.
 */
/*---------------------------------------------------------------------------*/
void
ctk_menu_add(struct ctk_menu *menu)
{
#if CTK_CONF_MENUS
  struct ctk_menu *m;
 
  if(lastmenu == NULL) {
    lastmenu = menu;
  }
    
  for(m = menus.menus; m->next != NULL; m = m->next) {
    if(m == menu) {
      return;
    }
  }
  m->next = menu;
  menu->next = NULL;
 
  redraw |= REDRAW_MENUPART;
#endif /* CTK_CONF_MENUS */
}
/*---------------------------------------------------------------------------*/
/**
 * Remove a menu from the menu bar.
 *
 * \param menu The menu to be removed.
 */
/*---------------------------------------------------------------------------*/
void
ctk_menu_remove(struct ctk_menu *menu)
{
#if CTK_CONF_MENUS
  struct ctk_menu *m;
 
  for(m = menus.menus; m->next != NULL; m = m->next) {
    if(m->next == menu) {
      m->next = menu->next;
      if(menu == lastmenu) {
    lastmenu = NULL;
      }
      redraw |= REDRAW_MENUPART;
      return;
    }
  }
#endif /* CTK_CONF_MENUS */
}
/*---------------------------------------------------------------------------*/
/**
 * \internal Redraws everything on the screen within the clip
 * interval.
 *
 * \param clipy1 The upper bound of the clip interval
 * \param clipy2 The lower bound of the clip interval
 */
/*---------------------------------------------------------------------------*/
static void CC_FASTCALL
do_redraw_all(unsigned char clipy1, unsigned char clipy2)
{
#if CTK_CONF_WINDOWS
  static struct ctk_widget *widget;
  struct ctk_window *w;
  unsigned char focus;
#endif /* CTK_CONF_WINDOWS */
 
  if(mode != CTK_MODE_NORMAL && mode != CTK_MODE_WINDOWMOVE) {
    return;
  }
  
  ctk_draw_clear(clipy1, clipy2);
 
#if CTK_CONF_WINDOWS  
  /* Draw widgets in root window */
  for(widget = desktop_window.active;
      widget != NULL; widget = widget->next) {
    ctk_draw_widget(widget, windows != NULL? 0: CTK_FOCUS_WINDOW, clipy1, clipy2);
  }
 
  /* Draw windows */
  if(windows != NULL) {
    /* Find the last window.*/
    for(w = windows; w->next != NULL; w = w->next);
 
    /* Draw the windows from back to front. */
    for(; w != windows; w = w->prev) {
      ctk_draw_clear_window(w, 0, clipy1, clipy2);
      ctk_draw_window(w, 0, clipy1, clipy2, 1);
    }
 
    /* Draw focused window */
    focus = mode == CTK_MODE_WINDOWMOVE?
        CTK_FOCUS_WIDGET|CTK_FOCUS_WINDOW:
        CTK_FOCUS_WINDOW;
    ctk_draw_clear_window(windows, focus, clipy1, clipy2);
    ctk_draw_window(windows, focus, clipy1, clipy2, 1);
  }
 
  /* Draw dialog (if any) */
  if(dialog != NULL) {
    ctk_draw_dialog(dialog);
  }
#else /* CTK_CONF_WINDOWS */
  if(window != NULL) {
    ctk_draw_clear_window(window, CTK_FOCUS_WINDOW, clipy1, clipy2);
    ctk_draw_window(window, CTK_FOCUS_WINDOW, clipy1, clipy2, 0);
  }
#endif /* CTK_CONF_WINDOWS */
 
#if CTK_CONF_MENUS
  ctk_draw_menus(&menus);
#endif /* CTK_CONF_MENUS */
}
#if CTK_CONF_WINDOWS
/*---------------------------------------------------------------------------*/
/**
 * Redraw the entire desktop.
 *
 * \param d The desktop to be redrawn.
 *
 * \note Currently the parameter d is not used, but must be set to
 * NULL.
 *
 */
/*---------------------------------------------------------------------------*/
void
ctk_desktop_redraw(struct ctk_desktop *d)
{
  if(PROCESS_CURRENT() == &ctk_process) {
    if(mode == CTK_MODE_NORMAL || mode == CTK_MODE_WINDOWMOVE) {
      do_redraw_all(CTK_CONF_MENUS, height);
    }
  } else {
    height = ctk_draw_height();
    width = ctk_draw_width();
    
    redraw |= REDRAW_ALL;
  }
}
#endif /* CTK_CONF_WINDOWS */
/*---------------------------------------------------------------------------*/
/**
 * Redraw a window.
 *
 * This function redraws the window, but only if it is the foremost
 * one on the desktop.
 *
 * \param w The window to be redrawn.
 */
/*---------------------------------------------------------------------------*/
void
ctk_window_redraw(struct ctk_window *w)
{
  /* Only redraw the window if it is a dialog or if it is the foremost
     window. */
  if(mode != CTK_MODE_NORMAL) {
    return;
  }
  
#if CTK_CONF_WINDOWS
  if(w == dialog) {
    ctk_draw_dialog(w);
  } else if(dialog == NULL &&
#if CTK_CONF_MENUS
        menus.open == NULL &&
#endif /* CTK_CONF_MENUS */
        windows == w)
#endif /* CTK_CONF_WINDOWS */
  {
    ctk_draw_window(w, CTK_FOCUS_WINDOW, 0, height, 0);
  }
}
/*---------------------------------------------------------------------------*/
/**
 * \internal Creates a new window.
 *
 * \param window The window to be created.
 * \param w The width of the window.
 * \param h The height of the window.
 * \param title The title of the window.
 */
/*---------------------------------------------------------------------------*/
static void
window_new(CC_REGISTER_ARG struct ctk_window *window,
       unsigned char w, unsigned char h, char *title)
{
#if CTK_CONF_WINDOWS
  if(w >= width - 2) {
    window->x = 0;
  } else {
    window->x = (width - w - 2) / 2;
  }
  if(h >= height - 2 - ctk_draw_windowtitle_height) {
    window->y = 0;
  } else {
    window->y = (height - h - 2 - ctk_draw_windowtitle_height) / 2;
  }
#endif /* CTK_CONF_WINDOWS */
 
  window->w = w;
  window->h = h;
  window->title = title;
  if(title != NULL) {
    window->titlelen = (unsigned char)strlen(title);
  } else {
    window->titlelen = 0;
  }
  window->next = window->prev = NULL;
  window->owner = PROCESS_CURRENT();
  window->active = window->inactive = window->focused = NULL;
}
/*---------------------------------------------------------------------------*/
/**
 * Create a new window.
 *
 * Creates a new window. The memory for the window structure must
 * already be allocated by the caller, and is usually done with a
 * static declaration.
 *
 * This function sets up the internal structure of the ctk_window
 * struct and creates the move and close buttons, but it does not open
 * the window. The window must be explicitly opened by calling the
 * ctk_window_open() function.
 *
 * \param window The window to be created.
 * \param w The width of the new window.
 * \param h The height of the new window.
 * \param title The title of the new window.
 */
/*---------------------------------------------------------------------------*/
void
ctk_window_new(struct ctk_window *window,
           unsigned char w, unsigned char h, char *title)
{
  window_new(window, w, h, title);
 
#if CTK_CONF_WINDOWS
  make_windowbuttons(window);
#endif /* CTK_CONF_WINDOWS */
}
#if CTK_CONF_WINDOWS
/*---------------------------------------------------------------------------*/
/**
 * Creates a new dialog.
 *
 * This function only sets up the internal structure of the ctk_window
 * struct but does not open the dialog. The dialog must be explicitly
 * opened by calling the ctk_dialog_open() function.
 *
 * \param dialog The dialog to be created.
 * \param w The width of the dialog.
 * \param h The height of the dialog.
 */
/*---------------------------------------------------------------------------*/
void
ctk_dialog_new(CC_REGISTER_ARG struct ctk_window *dialog,
           unsigned char w, unsigned char h)
{
  window_new(dialog, w, h, NULL);
}
#endif /* CTK_CONF_WINDOWS */
/*---------------------------------------------------------------------------*/
/**
 * Creates a new menu.
 *
 * This function sets up the internal structure of the menu, but does
 * not add it to the menubar. Use the function ctk_menu_add() for that
 * purpose.
 *
 * \param menu The menu to be created.
 * \param title The title of the menu.
 */
/*---------------------------------------------------------------------------*/
void
ctk_menu_new(CC_REGISTER_ARG struct ctk_menu *menu, char *title)
{
#if CTK_CONF_MENUS
  menu->next = NULL;
  menu->title = title;
  menu->titlelen = (unsigned char)strlen(title);
  menu->active = 0;
  menu->nitems = 0;
#endif /* CTK_CONF_MENUS */
}
/*---------------------------------------------------------------------------*/
/**
 * Adds a menu item to a menu.
 *
 * In CTK, each menu item is identified by a number which is unique
 * within each menu. When a menu item is selected, a
 * ctk_menuitem_activated signal is emitted and the menu item number
 * is passed as signal data with the signal.
 *
 * \param menu The menu to which the menu item should be added.
 * \param name The name of the menu item.
 * \return The number of the menu item.
 */
/*---------------------------------------------------------------------------*/
unsigned char
ctk_menuitem_add(CC_REGISTER_ARG struct ctk_menu *menu, char *name)
{
#if CTK_CONF_MENUS
  if(menu->nitems == CTK_MAXMENUITEMS) {
    return 0;
  }
  menu->items[menu->nitems].title = name;
  menu->items[menu->nitems].titlelen = (unsigned char)strlen(name);
  return menu->nitems++;
#else
  return 0;
#endif /* CTK_CONF_MENUS */
}
/*---------------------------------------------------------------------------*/
/**
 * \internal Adds a widget to the list of widgets that should be
 * redrawn.
 *
 * \param w The widget that should be redrawn.
 */
/*---------------------------------------------------------------------------*/
static void CC_FASTCALL
add_redrawwidget(struct ctk_widget *w)
{
  static unsigned char i;
  
  if(redraw_widgetptr == MAX_REDRAWWIDGETS) {
    redraw |= REDRAW_FOCUS;
  } else {
    redraw |= REDRAW_WIDGETS;
    /* Check if it is in the queue already. If so, we don't add it
       again. */
    for(i = 0; i < redraw_widgetptr; ++i) {
      if(redraw_widgets[i] == w) {
    return;
      }
    }
    redraw_widgets[redraw_widgetptr++] = w;
  }
}
/*---------------------------------------------------------------------------*/
/**
 * \internal Checks if a widget redrawn and adds it to the list of
 * widgets to be redrawn.
 *
 * A widget can be redrawn only if the current CTK mode is
 * CTK_MODE_NORMAL, if no menu is open, and the widget is in the
 * foremost window.
 *
 * \param widget The widget that should be redrawn.
 */
/*---------------------------------------------------------------------------*/
static void
widget_redraw(struct ctk_widget *widget)
{
  struct ctk_window *window;
 
  if(mode != CTK_MODE_NORMAL || widget == NULL) {
    return;
  }
 
  /* Only redraw widgets that are in the foremost window. If we would
     allow redrawing widgets in non-focused windows, we would have to
     redraw all the windows that cover the non-focused window as well,
     which would lead to flickering.
 
     Also, we avoid drawing any widgets when the menus are active.
    */
    
#if CTK_CONF_MENUS
  if(menus.open == NULL)
#endif /* CTK_CONF_MENUS */
  {
    window = widget->window;
#if CTK_CONF_WINDOWS
    if(window == dialog) {
      ctk_draw_widget(widget, CTK_FOCUS_DIALOG, 0, height);
    } else if(dialog == NULL &&
          (window == windows ||
           window == &desktop_window))
#endif /* CTK_CONF_WINDOWS */
    {
      ctk_draw_widget(widget, CTK_FOCUS_WINDOW, 0, height);
    }
  }
}
/*---------------------------------------------------------------------------*/
/**
 * Redraws a widget.
 *
 * This function will set a flag which causes the widget to be redrawn
 * next time the CTK process is scheduled.
 *
 * \param widget The widget that is to be redrawn.
 *
 * \note This function should usually not be called directly since it
 * requires typecasting of the widget parameter. The wrapper macro
 * CTK_WIDGET_REDRAW() does the required typecast and should be used
 * instead.
 */
/*---------------------------------------------------------------------------*/
void
ctk_widget_redraw(struct ctk_widget *widget)
{
  if(mode != CTK_MODE_NORMAL || widget == NULL) {
    return;
  }
 
  /* Since this function isn't called by CTK itself, we only queue the
     redraw request. */
  add_redrawwidget(widget);
}
/*---------------------------------------------------------------------------*/
/**
 * Adds a widget to a window.
 *
 * This function adds a widget to a window. The order of which the
 * widgets are added is important, as it sets the order to which
 * widgets are cycled with the widget selection keys.
 *
 * \param window The window to which the widhet should be added.
 * \param widget The widget to be added.
 */
/*---------------------------------------------------------------------------*/
void CC_FASTCALL
ctk_widget_add(CC_REGISTER_ARG struct ctk_window *window,
           CC_REGISTER_ARG struct ctk_widget *widget)
{
  if(widget->type == CTK_WIDGET_LABEL ||
     widget->type == CTK_WIDGET_SEPARATOR) {
    widget->next = window->inactive;
    window->inactive = widget;
    widget->window = window;
  } else {
    widget->next = window->active;
    window->active = widget;
    widget->window = window;
  }
}
/*---------------------------------------------------------------------------*/
/**
 * Gets the width of the desktop.
 *
 * \param d The desktop.
 * \return The width of the desktop, in characters.
 *
 * \note The d parameter is currently unused and must be set to NULL.
 */
/*---------------------------------------------------------------------------*/
unsigned char
ctk_desktop_width(struct ctk_desktop *d)
{
  return ctk_draw_width();
}
/*---------------------------------------------------------------------------*/
/**
 * Gets the height of the desktop.
 *
 * \param d The desktop.
 * \return The height of the desktop, in characters.
 *
 * \note The d parameter is currently unused and must be set to NULL.
 */
/*---------------------------------------------------------------------------*/
unsigned char
ctk_desktop_height(struct ctk_desktop *d)
{
  return ctk_draw_height();
}
/*---------------------------------------------------------------------------*/
/**
 * \internal Selects a widget in the window of the widget.
 *
 * \param focus The widget to be focused.
 */
/*---------------------------------------------------------------------------*/
static void CC_FASTCALL
select_widget(struct ctk_widget *focus)
{
  struct ctk_window *window;
 
  window = focus->window;
  
  if(focus != window->focused) {
    window->focused = focus;
    /* The operation changed the focus, so we emit a "hover" signal
       for those widgets that support it. */
    
    if(window->focused->type == CTK_WIDGET_HYPERLINK) {
      process_post(window->owner, ctk_signal_hyperlink_hover, window->focused);
    } else if(window->focused->type == CTK_WIDGET_BUTTON) {
      process_post(window->owner, ctk_signal_button_hover, window->focused);
    }
    
    add_redrawwidget(window->focused);
 
    process_post(focus->window->owner, ctk_signal_widget_select, focus);
  }
}
/*---------------------------------------------------------------------------*/
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
static void CC_FASTCALL
switch_focus_widget(unsigned char direction)
{
#if CTK_CONF_WINDOWS
  register struct ctk_window *window;
#endif /* CTK_CONF_WINDOWS */
  register struct ctk_widget *focus;
  struct ctk_widget *widget;
  
#if CTK_CONF_WINDOWS
  if(dialog != NULL) {
    window = dialog;
  } else {
    window = windows;
  }
 
  /* If there are no windows open, we move focus around between the
     icons on the root window instead. */
  if(window == NULL) {
    window = &desktop_window;
  }
#else /* CTK_CONF_WINDOWS */
  if(window == NULL) {
    return;
  }
#endif /* CTK_CONF_WINDOWS */
 
  focus = window->focused;
  if(focus == NULL) {
    focus = window->active;
    if(focus == NULL) {
      return;
    }
  }
  add_redrawwidget(focus);
  
  if((direction & 1) == 0) {
    /* Move focus "up" */
    focus = focus->next;
  } else {
    /* Move focus "down" */
    for(widget = window->active;
    widget != NULL; widget = widget->next) {
    if(widget->next == focus) {
      break;
    }
    }
    focus = widget;
    if(focus == NULL) {
      if(window->active != NULL) {
    for(focus = window->active;
        focus->next != NULL; focus = focus->next);
      }
    }
  }
  if(focus == NULL) {
    focus = window->active;
  }
 
  select_widget(focus);
}
/*---------------------------------------------------------------------------*/
#if CTK_CONF_MENUS
static void
switch_open_menu(unsigned char rightleft)
{
  struct ctk_menu *menu;
  
  if(rightleft == 0) {
    /* Move right */
    for(menu = menus.menus; menu != NULL; menu = menu->next) {
      if(menu->next == menus.open) {
    break;
      }
    }
    lastmenu = menus.open;
    menus.open = menu;
    if(menus.open == NULL) {
      for(menu = menus.menus;
      menu->next != NULL; menu = menu->next);
      menus.open = menu;
    }
  } else {
    /* Move to left */
    lastmenu = menus.open;
    menus.open = menus.open->next;
    if(menus.open == NULL) {
      menus.open = menus.menus;
    }
  }
 
  menus.open->active = 0;
}
/*---------------------------------------------------------------------------*/
static void
switch_menu_item(unsigned char updown)
{
  register struct ctk_menu *m;
 
  m = menus.open;
  
  if(updown == 0) {
    /* Move up */
    if(m->active == 0) {
      m->active = m->nitems - 1;
    } else {
      --m->active;
      if(m->items[m->active].title[0] == '-') {
    --m->active;
      }
    }
  } else {
    /* Move down */
    if(m->active >= m->nitems - 1) {
      m->active = 0;
    } else {
      ++m->active;
      if(m->items[m->active].title[0] == '-') {
    ++m->active;
      }
    }
  }
}
#endif /* CTK_CONF_MENUS */
/*---------------------------------------------------------------------------*/
static unsigned char CC_FASTCALL
activate(CC_REGISTER_ARG struct ctk_widget *w)
{
  if(w->type == CTK_WIDGET_BUTTON) {
#if CTK_CONF_WINDOWCLOSE
    if(w == (struct ctk_widget *)&windows->closebutton) {
      process_post(w->window->owner, ctk_signal_window_close, windows);
      ctk_window_close(windows);
      return REDRAW_ALL;
    } else
#endif /* CTK_CONF_WINDOWCLOSE */
#if CTK_CONF_WINDOWMOVE
    if(w == (struct ctk_widget *)&windows->titlebutton) {
      mode = CTK_MODE_WINDOWMOVE;
      return REDRAW_ALL;
    } else
#endif /* CTK_CONF_WINDOWMOVE */
    {
      process_post(w->window->owner, ctk_signal_widget_activate, w);
    }
#if CTK_CONF_ICONS
  } else if(w->type == CTK_WIDGET_ICON) {
    if(w->widget.icon.owner != PROCESS_NONE) {
      process_post(w->widget.icon.owner, ctk_signal_widget_activate, w);
    } else {
      process_post(w->window->owner, ctk_signal_widget_activate, w);
    }
#endif /* CTK_CONF_ICONS */
  } else if(w->type == CTK_WIDGET_HYPERLINK) {
    process_post(PROCESS_BROADCAST, ctk_signal_hyperlink_activate, w);
  } else if(w->type == CTK_WIDGET_TEXTENTRY) {
    if(w->widget.textentry.state == CTK_TEXTENTRY_NORMAL) {
      w->widget.textentry.state = CTK_TEXTENTRY_EDIT;
      textentry_input(0, (struct ctk_textentry *)w);
    } else {
      w->widget.textentry.state = CTK_TEXTENTRY_NORMAL;
      process_post(w->window->owner, ctk_signal_widget_activate, w);
    }
    add_redrawwidget(w);
    return REDRAW_WIDGETS;
  } else {
    process_post(w->window->owner, ctk_signal_widget_activate, w);
  }
  return REDRAW_NONE;
}
/*---------------------------------------------------------------------------*/
#ifdef SDCC
/* Dummy function that we define to keep sdcc happy - with sdcc,
   function pointers cannot be NULL. ctk_textentry_input is typedef'd
   in ctk/ctk.h, hence the strange-looking function signature. */
unsigned char
ctk_textentry_input_null(ctk_arch_key_t c, struct ctk_textentry *t)
{
  return 0;
}
#endif /* SDCC */
/*---------------------------------------------------------------------------*/
static void CC_FASTCALL
textentry_input(ctk_arch_key_t c, CC_REGISTER_ARG struct ctk_textentry *t)
{
  register char *cptr, *cptr2;
  static unsigned char len, txpos, typos, tlen;
 
  if(t->input != NULL && t->input(c, t)) {
    return;
  }
 
  txpos = t->xpos;
  typos = t->ypos;
  tlen = t->len;
 
  cptr = &t->text[txpos + typos * (tlen + 1)];
      
  switch(c) {
  case CH_CURS_LEFT:
    if(txpos > 0) {
      --txpos;
    }
    break;
    
  case CH_CURS_RIGHT:
    if(txpos < tlen - 1 && *cptr != 0) {
      ++txpos;
    }
    break;
 
  case CH_CURS_UP:
    txpos = 0;
    break;
    
  case 0:
  case CH_CURS_DOWN:
    txpos = (unsigned char)strlen(t->text);
    if(txpos == tlen) {
      --txpos;
    }
    break;
    
  case CH_ENTER:
    activate((struct ctk_widget *)t);
    switch_focus_widget(DOWN);
    break;
    
  case CTK_CONF_WIDGETDOWN_KEY:
    t->state = CTK_TEXTENTRY_NORMAL;
    switch_focus_widget(DOWN);
    break;
  case CTK_CONF_WIDGETUP_KEY:
    t->state = CTK_TEXTENTRY_NORMAL;
    switch_focus_widget(UP);
    break;
    
  default:
    len = tlen - txpos;
    if(c == CH_DEL) {
      if(len == 1 && *cptr != 0) {
    *cptr = 0;
      } else {
        if(txpos > 0) {
      --txpos;
      strcpy(cptr - 1, cptr);
    }
      }
    } else {
      if(ctk_arch_isprint(c)) {
    if(len > 1) {
      cptr2 = cptr + len - 1;
      while(cptr2 > cptr) {
        *cptr2 = *(cptr2 - 1);
        --cptr2;
      }
      ++txpos;
    }
    *cptr = c;
      }
    }
    break;
  }
 
  t->xpos = txpos;
  t->ypos = typos;
}
/*---------------------------------------------------------------------------*/
#if CTK_CONF_MENUS
static unsigned char
activate_menu(void)
{
  struct ctk_window *w;
  
  lastmenu = menus.open;
  if(menus.open == &desktopmenu) {
    for(w = windows; w != NULL; w = w->next) {
      if(w->title == desktopmenu.items[desktopmenu.active].title) {
    ctk_window_open(w);
    menus.open = NULL;
    return REDRAW_ALL;
      }
    }
  } else {
    process_post(PROCESS_BROADCAST, ctk_signal_menu_activate, menus.open);
  }
  menus.open = NULL;
  return REDRAW_MENUPART;
}
/*---------------------------------------------------------------------------*/
static unsigned char
menus_input(ctk_arch_key_t c)
{
  if(menus.open->nitems > maxnitems) {
    maxnitems = menus.open->nitems;
  }
  
  switch(c) {
  case CH_CURS_RIGHT:
    switch_open_menu(1);
    return REDRAW_MENUPART;
 
  case CH_CURS_DOWN:
    switch_menu_item(1);
    return REDRAW_MENUS;
 
  case CH_CURS_LEFT:
    switch_open_menu(0);
    return REDRAW_MENUPART;
 
  case CH_CURS_UP:
    switch_menu_item(0);
    return REDRAW_MENUS;
    
  case CH_ENTER:
    return activate_menu();
 
  case CTK_CONF_MENU_KEY:
    lastmenu = menus.open;
    menus.open = NULL;
    return REDRAW_MENUPART;
  }
 
  return REDRAW_NONE;
}
#endif /* CTK_CONF_MENUS */
/*---------------------------------------------------------------------------*/
#if CTK_CONF_SCREENSAVER
static void
handle_timer(void)
{
  if(mode == CTK_MODE_NORMAL) {
    ++screensaver_timer;
    if(screensaver_timer >= ctk_screensaver_timeout) {
      process_post(PROCESS_BROADCAST, ctk_signal_screensaver_start, NULL);
#ifdef CTK_SCREENSAVER_INIT
      CTK_SCREENSAVER_INIT();
#endif /* CTK_SCREENSAVER_INIT */
 
      screensaver_timer = 0;
    }
  }
}
#endif /* CTK_CONF_SCREENSAVER */
/*---------------------------------------------------------------------------*/
static void
unfocus_widget(CC_REGISTER_ARG struct ctk_widget *w)
{
  if(w != NULL) {
    redraw |= REDRAW_WIDGETS;
    add_redrawwidget(w);
    if(CTK_WIDGET_TYPE(w) == CTK_WIDGET_TEXTENTRY) {
      ((struct ctk_textentry *)w)->state =
    CTK_TEXTENTRY_NORMAL;
    }
    w->window->focused = NULL;
  }
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ctk_process, ev, data)
{
  static ctk_arch_key_t c;
  static unsigned char i;
#if CTK_CONF_WINDOWS
  register struct ctk_window *window;
#endif /* CTK_CONF_WINDOWS */
  register struct ctk_widget *widget;
  register struct ctk_widget **widgetptr;
#if CTK_CONF_MOUSE_SUPPORT
  static unsigned char mxc, myc, mouse_button_changed, mouse_moved,
    mouse_clicked;
#if CTK_CONF_MENUS
  static unsigned char menux;
  register struct ctk_menu *menu;
#endif /* CTK_CONF_MENUS */
#endif /* CTK_CONF_MOUSE_SUPPORT */
  
  PROCESS_BEGIN();
  
#if CTK_CONF_MENUS
  ctk_menu_new(&desktopmenu, "Desktop");
  make_desktopmenu();
  menus.menus = menus.desktopmenu = &desktopmenu;
#endif /* CTK_CONF_MENUS */
 
#if CTK_CONF_MOUSE_SUPPORT
  ctk_mouse_init();
  ctk_mouse_show();
#endif /* CTK_CONF_MOUSE_SUPPORT */
  
  ctk_restore();
 
#if CTK_CONF_WINDOWS
  desktop_window.owner = &ctk_process;
#endif /* CTK_CONF_WINDOWS */
 
  ctk_signal_keypress = process_alloc_event();
  
  ctk_signal_button_activate =
    ctk_signal_widget_activate = process_alloc_event();
  
  ctk_signal_button_hover =
    ctk_signal_hyperlink_hover =
    ctk_signal_widget_select = process_alloc_event();
  
  ctk_signal_hyperlink_activate = process_alloc_event();
 
  ctk_signal_menu_activate = process_alloc_event();
 
  ctk_signal_window_close = process_alloc_event();
 
#if CTK_CONF_MOUSE_SUPPORT
  ctk_signal_pointer_move = process_alloc_event();
  ctk_signal_pointer_button = process_alloc_event();
#endif /* CTK_CONF_MOUSE_SUPPORT */
 
#if CTK_CONF_SCREENSAVER
  ctk_signal_screensaver_start = process_alloc_event();
  ctk_signal_screensaver_stop = process_alloc_event();
#endif /* CTK_CONF_SCREENSAVER */
 
  mode = CTK_MODE_NORMAL;
 
#if CTK_CONF_ICONS
  iconx = ICONX_START;
  icony = ICONY_START;
#endif /* CTK_CONF_ICONS */
 
#if CTK_CONF_SCREENSAVER
  timer_set(&timer, CLOCK_SECOND);
#endif /* CTK_CONF_SCREENSAVER */
  
  while(1) {
    process_poll(&ctk_process);
    PROCESS_WAIT_EVENT();
    
#if CTK_CONF_SCREENSAVER
    if(timer_expired(&timer)) {
      timer_reset(&timer);
      handle_timer();
    }
#endif /* CTK_CONF_SCREENSAVER */
 
#if CTK_CONF_MENUS
    if(menus.open != NULL) {
      maxnitems = menus.open->nitems;
    } else {
      maxnitems = 0;
    }
#endif /* CTK_CONF_MENUS */
 
#if CTK_CONF_MOUSE_SUPPORT
    mouse_button_changed = mouse_moved = mouse_clicked = 0;
 
    /* See if there is any change in the buttons. */
    if(ctk_mouse_button() != mouse_button) {
      mouse_button = ctk_mouse_button();
      mouse_button_changed = 1;
      if(mouse_button == 0) {
    mouse_clicked = 1;
      }
    }
  
    /* Check if the mouse pointer has moved. */
    if(ctk_mouse_x() != mouse_x ||
       ctk_mouse_y() != mouse_y) {
      mouse_x = ctk_mouse_x();
      mouse_y = ctk_mouse_y();
      mouse_moved = 1;
    }
 
    mxc = ctk_mouse_xtoc(mouse_x);
    myc = ctk_mouse_ytoc(mouse_y);
#endif /* CTK_CONF_MOUSE_SUPPORT */
 
#if CTK_CONF_SCREENSAVER
    if(mode == CTK_MODE_SCREENSAVER) {
      if(ctk_arch_keyavail()
#if CTK_CONF_MOUSE_SUPPORT
     || mouse_moved || mouse_button_changed
#endif /* CTK_CONF_MOUSE_SUPPORT */
     ) {
    process_post(PROCESS_BROADCAST, ctk_signal_screensaver_stop, NULL);
    mode = CTK_MODE_NORMAL;
      }
    } else
#endif /* CTK_CONF_SCREENSAVER */
      if(mode == CTK_MODE_NORMAL) {
#if CTK_CONF_MOUSE_SUPPORT
    /* If there is any change in the mouse conditions, find out in
       which window the mouse pointer currently is in order to send
       the correct signals, or bring a window to focus. */
    if(mouse_moved || mouse_button_changed) {
      ctk_mouse_show();
#if CTK_CONF_SCREENSAVER
      screensaver_timer = 0;
#endif /* CTK_CONF_SCREENSAVER */
      
#if CTK_CONF_MENUS
      if(myc == 0) {
        /* Here we should do whatever needs to be done when the mouse
           moves around and clicks in the menubar. */
        if(mouse_clicked) {
          static unsigned char titlelen;
      
          /* Find out which menu that the mouse pointer is in. Start
         with the ->next menu after the desktop menu. We assume
         that the menus start one character from the left screen
         side and that the desktop menu is farthest to the
         right. */
          menux = 1;
          for(menu = menus.menus->next;
          menu != NULL; menu = menu->next) {
        titlelen = menu->titlelen;
        if(mxc >= menux && mxc <= menux + titlelen) {
          break;
        }
        menux += titlelen;
          }
      
          /* Also check desktop menu. */
          if(mxc >= width - 7 &&
         mxc <= width - 1) {
        menu = &desktopmenu;
          }
      
          menus.open = menu;
          redraw |= REDRAW_MENUPART;
        }
      } else {
        --myc;
 
        if(menus.open != NULL) {
          static unsigned char nitems;
      
          /* Do whatever needs to be done when a menu is open. */
 
          /* First check if the mouse pointer is in the currently open
         menu. */
          if(menus.open == &desktopmenu) {
        menux = width - CTK_CONF_MENUWIDTH;
          } else {
        menux = 1;
        for(menu = menus.menus->next; menu != menus.open;
            menu = menu->next) {
          menux += menu->titlelen;
        }
          }
 
          nitems = menus.open->nitems;
          /* Find out which of the menu items the mouse is pointing
         to. */
          if(mxc >= menux && mxc <= menux + CTK_CONF_MENUWIDTH) {
        if(myc <= nitems) {
          menus.open->active = myc;
        } else {
          menus.open->active = nitems - 1;
        }
          }
      
          if(mouse_clicked) {
        if(mxc >= menux && mxc <= menux + CTK_CONF_MENUWIDTH &&
           myc <= nitems) {
          redraw |= activate_menu();
        } else {
          lastmenu = menus.open;
          menus.open = NULL;
          redraw |= REDRAW_MENUPART;
        }
          } else {
        redraw |= REDRAW_MENUS;
          }
        } else {
#endif /* CTK_CONF_MENUS */
 
#if CTK_CONF_WINDOWS
          /* Walk through the windows from top to bottom to see in
         which window the mouse pointer is. */
          if(dialog != NULL) {
        window = dialog;
          } else {
        for(window = windows; window != NULL;
            window = window->next) {
          
          /* Check if the mouse is within the window. */
          if(mxc >= window->x &&
             mxc <= window->x + window->w +
             2 * ctk_draw_windowborder_width &&
             myc >= window->y &&
             myc <= window->y + window->h +
             ctk_draw_windowtitle_height +
             ctk_draw_windowborder_height) {
            break;
          }
        }
          }
 
          /* If we didn't find any window, and there are no windows
         open, the mouse pointer will definately be within the
         background desktop window. */
          if(window == NULL) {
        window = &desktop_window;
          }
 
          /* If the mouse pointer moves around outside of the
         currently focused window (or dialog), we should not have
         any focused widgets in the focused window so we make sure
         that there are none. */
          if(windows != NULL &&
         window != windows &&
         windows->focused != NULL){
        unfocus_widget(windows->focused);
          }
#endif /* CTK_CONF_WINDOWS */
 
          if(window != NULL) {
#if CTK_CONF_WINDOWS
        /* If the mouse was clicked outside of the current window,
           we bring the clicked window to front. */
        if(dialog == NULL &&
           window != &desktop_window &&
           window != windows &&
           mouse_clicked) {
          /* Bring window to front. */
          ctk_window_open(window);
          redraw |= REDRAW_ALL;
        } else {
 
          /* Find out which widget currently is under the mouse
             pointer and give it focus, unless it already has
             focus. */
          mxc = mxc - window->x - ctk_draw_windowborder_width;
          myc = myc - window->y - ctk_draw_windowtitle_height;
#endif /* CTK_CONF_WINDOWS */
        
          /* See if the mouse pointer is on a widget. If so, it
             should be selected and, if the button is clicked,
             activated. */
          for(widget = window->active; widget != NULL;
              widget = widget->next) {
        
            if(mxc >= widget->x &&
               mxc <= widget->x + widget->w + 1 &&
               myc >= widget->y &&
               myc <= widget->y + widget->h - 1) {
              break;
            }
          }
        
          /* if the mouse is moved in the focused window, we emit
             a ctk_signal_pointer_move signal to the owner of the
             window. */
          if(mouse_moved
#if CTK_CONF_WINDOWS
             && (window != &desktop_window || windows == NULL)
#endif /* CTK_CONF_WINDOWS */
              ) {
 
            process_post(window->owner, ctk_signal_pointer_move, NULL);
 
            /* If there was a focused widget that is not below the
               mouse pointer, we remove focus from the widget and
               redraw it. */
            if(window->focused != NULL &&
               widget != window->focused) {
              unfocus_widget(window->focused);
            }
            redraw |= REDRAW_WIDGETS;
            if(widget != NULL) {
              select_widget(widget);
            }
          }
        
          if(mouse_button_changed) {
            process_post(window->owner, ctk_signal_pointer_button,
                 (process_data_t)(size_t)mouse_button);
            if(mouse_clicked && widget != NULL) {
              select_widget(widget);
              redraw |= activate(widget);
            }
          }
#if CTK_CONF_WINDOWS
        }
#endif /* CTK_CONF_WINDOWS */
          }
#if CTK_CONF_MENUS
        }
      }
#endif /* CTK_CONF_MENUS */
    }
#endif /* CTK_CONF_MOUSE_SUPPORT */
    
    while(ctk_arch_keyavail()) {
 
      ctk_mouse_hide();
      
#if CTK_CONF_SCREENSAVER
      screensaver_timer = 0;
#endif /* CTK_CONF_SCREENSAVER */
      
      c = ctk_arch_getkey();
      
#if CTK_CONF_WINDOWS
      if(dialog != NULL) {
        window = dialog;
      } else if(windows != NULL) {
        window = windows;
      } else {
        window = &desktop_window;
      }
#else /* CTK_CONF_WINDOWS */
      if(window == NULL) {
        continue;
      }
#endif /* CTK_CONF_WINDOWS */
 
          /* Allow to exit the process owning the foreground window by
             pressing ctrl-c. This is especially useful if there's no
             closebutton on the window frames (or no windows at all).
          */
          if(c == 3) {
            process_post(window->owner, PROCESS_EVENT_EXIT, NULL);
          }
 
      widget = window->focused;
      
      if(widget != NULL &&
         widget->type == CTK_WIDGET_TEXTENTRY &&
         widget->widget.textentry.state == CTK_TEXTENTRY_EDIT) {
        textentry_input(c, (struct ctk_textentry *)widget);
        add_redrawwidget(widget);
#if CTK_CONF_MENUS
      } else if(menus.open != NULL) {
        redraw |= menus_input(c);
#endif /* CTK_CONF_MENUS */
      } else {
        switch(c) {
        case CTK_CONF_WIDGETDOWN_KEY:
          switch_focus_widget(DOWN);
          break;
        case CTK_CONF_WIDGETUP_KEY:
          switch_focus_widget(UP);
          break;
#if CTK_CONF_MENUS
        case CTK_CONF_MENU_KEY:
          if(dialog == NULL) {
        if(lastmenu == NULL) {
          menus.open = menus.menus;
        } else {
          menus.open = lastmenu;
        }
        menus.open->active = 0;
        redraw |= REDRAW_MENUS;
          }
          break;
#endif /* CTK_CONF_MENUS */
#if CTK_CONF_WINDOWS
        case CTK_CONF_WINDOWSWITCH_KEY:
          if(windows != NULL) {
        for(window = windows; window->next != NULL;
            window = window->next);
        ctk_window_open(window);
          }
          break;
#endif /* CTK_CONF_WINDOWS */
        default:
 
          if(c == CH_ENTER &&
         widget != NULL) {
        redraw |= activate(widget);
          } else {
        if(widget != NULL &&
           widget->type == CTK_WIDGET_TEXTENTRY) {
          if(widget->widget.textentry.state == CTK_TEXTENTRY_NORMAL) {
            widget->widget.textentry.state = CTK_TEXTENTRY_EDIT;
            textentry_input(0, (struct ctk_textentry *)widget);
          }
          textentry_input(c, (struct ctk_textentry *)widget);
          add_redrawwidget(widget);
        } else {
          unfocus_widget(window->focused);
          process_post_synch(window->owner, ctk_signal_keypress,
                     (process_data_t)(size_t)c);
        }
          }
          break;
        }
      }
 
#if 0
      if(redraw & REDRAW_WIDGETS) {
        widgetptr = redraw_widgets;
        for(i = 0; i < MAX_REDRAWWIDGETS; ++i) {
          widget_redraw(*widgetptr);
          *widgetptr = NULL;
          ++widgetptr;
        }
        redraw &= ~REDRAW_WIDGETS;
        redraw_widgetptr = 0;
      }
#endif /* 0 */
    }
#if CTK_CONF_WINDOWMOVE
      } else if(mode == CTK_MODE_WINDOWMOVE) {
 
    redraw = 0;
 
    window = windows;
 
#if CTK_CONF_MOUSE_SUPPORT
 
    /* If the mouse has moved, we move the window as well. */
    if(mouse_moved) {
 
      if(window->w + mxc + 2 >= width) {
        window->x = width - 2 - window->w;
      } else {
        window->x = mxc;
      }
 
      if(window->h + myc + ctk_draw_windowtitle_height +
         ctk_draw_windowborder_height >= height) {
        window->y = height - window->h -
          ctk_draw_windowtitle_height - ctk_draw_windowborder_height;
      } else {
        window->y = myc;
      }
#if CTK_CONF_MENUS
      if(window->y > 0) {
        --window->y;
      }
#endif /* CTK_CONF_MENUS */
 
      redraw = REDRAW_ALL;
    }
    
    /* Check if the mouse has been clicked, and stop moving the window
       if so. */
    if(mouse_button_changed &&
       mouse_button == 0) {
      mode = CTK_MODE_NORMAL;
      redraw = REDRAW_ALL;
    }
#endif /* CTK_CONF_MOUSE_SUPPORT */
    
    while(mode == CTK_MODE_WINDOWMOVE && ctk_arch_keyavail()) {
    
#if CTK_CONF_SCREENSAVER
      screensaver_timer = 0;
#endif /* CTK_CONF_SCREENSAVER */
      
      c = ctk_arch_getkey();
      
      switch(c) {
      case CH_CURS_RIGHT:
        ++window->x;
        if(window->x + window->w + 1 >= width) {
          --window->x;
        }
        redraw = REDRAW_ALL;
        break;
      case CH_CURS_LEFT:
        if(window->x > 0) {
          --window->x;
        }
        redraw = REDRAW_ALL;
        break;
      case CH_CURS_DOWN:
        ++window->y;
        if(window->y + window->h + 1 + CTK_CONF_MENUS >= height) {
          --window->y;
        }
        redraw = REDRAW_ALL;
        break;
      case CH_CURS_UP:
        if(window->y > 0) {
          --window->y;
        }
        redraw = REDRAW_ALL;
        break;
      default:
        mode = CTK_MODE_NORMAL;
        redraw = REDRAW_ALL;
        break;
      }
    }
#endif /* CTK_CONF_WINDOWMOVE */
      }
 
    if(redraw & REDRAW_ALL) {
      do_redraw_all(CTK_CONF_MENUS, height);
#if CTK_CONF_MENUS
    } else if(redraw & REDRAW_MENUPART) {
      do_redraw_all(CTK_CONF_MENUS, maxnitems + 1);
    } else if(redraw & REDRAW_MENUS) {
      ctk_draw_menus(&menus);
#endif /* CTK_CONF_MENUS */
    } else if(redraw & REDRAW_FOCUS) {
#if CTK_CONF_WINDOWS
      if(dialog != NULL) {
    ctk_window_redraw(dialog);
      } else if(windows != NULL) {
    ctk_window_redraw(windows);
      } else {
    ctk_window_redraw(&desktop_window);
      }
#else /* CTK_CONF_WINDOWS */
      if(window != NULL) {
    ctk_window_redraw(window);
      }
#endif /* CTK_CONF_WINDOWS */
    } else if(redraw & REDRAW_WIDGETS) {
      widgetptr = redraw_widgets;
      for(i = 0; i < MAX_REDRAWWIDGETS; ++i) {
    widget_redraw(*widgetptr);
    *widgetptr = NULL;
    ++widgetptr;
      }
    }
    redraw = 0;
    redraw_widgetptr = 0;
  }
  
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/** @} */
/** @} */