File: gui.cc

package info (click to toggle)
bochs 3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,244 kB
  • sloc: cpp: 270,331; ansic: 25,334; sh: 8,371; makefile: 5,512; yacc: 1,485; asm: 395; perl: 359; lex: 318; csh: 3
file content (1693 lines) | stat: -rw-r--r-- 53,328 bytes parent folder | download
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
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2002-2025  The Bochs Project
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA


#include <signal.h>
#include "iodev.h"
#include "virt_timer.h"
#include "keymap.h"
#include "gui/bitmaps/floppya.h"
#include "gui/bitmaps/floppyb.h"
#include "gui/bitmaps/mouse.h"
#include "gui/bitmaps/reset.h"
#include "gui/bitmaps/power.h"
#include "gui/bitmaps/snapshot.h"
#include "gui/bitmaps/copy.h"
#include "gui/bitmaps/paste.h"
#include "gui/bitmaps/configbutton.h"
#include "gui/bitmaps/cdrom1.h"
#if BX_USB_DEBUGGER
  #include "gui/bitmaps/usb.h"
#endif
#include "gui/bitmaps/userbutton.h"
#include "gui/bitmaps/saverestore.h"

#if BX_USE_GUI_CONSOLE
#include "sdl.h"
#endif

#if BX_WITH_MACOS
#  include <Disks.h>
#endif

bx_gui_c *bx_gui = NULL;

#define BX_GUI_THIS bx_gui->
#define LOG_THIS BX_GUI_THIS

// Enable this if you want confirm quit after pressing power button.
//#define BX_GUI_CONFIRM_QUIT

// user button shortcut stuff

#define BX_KEY_UNKNOWN 0x7fffffff
#define N_USER_KEYS 38

typedef struct {
  const char *key;
  Bit32u symbol;
} user_key_t;

static user_key_t user_keys[N_USER_KEYS] =
{
  { "f1",    BX_KEY_F1 },
  { "f2",    BX_KEY_F2 },
  { "f3",    BX_KEY_F3 },
  { "f4",    BX_KEY_F4 },
  { "f5",    BX_KEY_F5 },
  { "f6",    BX_KEY_F6 },
  { "f7",    BX_KEY_F7 },
  { "f8",    BX_KEY_F8 },
  { "f9",    BX_KEY_F9 },
  { "f10",   BX_KEY_F10 },
  { "f11",   BX_KEY_F11 },
  { "f12",   BX_KEY_F12 },
  { "alt",   BX_KEY_ALT_L },
  { "bksl",  BX_KEY_BACKSLASH },
  { "bksp",  BX_KEY_BACKSPACE },
  { "ctrl",  BX_KEY_CTRL_L },
  { "del",   BX_KEY_DELETE },
  { "down",  BX_KEY_DOWN },
  { "end",   BX_KEY_END },
  { "enter", BX_KEY_ENTER },
  { "esc",   BX_KEY_ESC },
  { "home",  BX_KEY_HOME },
  { "ins",   BX_KEY_INSERT },
  { "left",  BX_KEY_LEFT },
  { "menu",  BX_KEY_MENU },
  { "minus", BX_KEY_MINUS },
  { "pgdwn", BX_KEY_PAGE_DOWN },
  { "pgup",  BX_KEY_PAGE_UP },
  { "plus",  BX_KEY_KP_ADD },
  { "right", BX_KEY_RIGHT },
  { "shift", BX_KEY_SHIFT_L },
  { "space", BX_KEY_SPACE },
  { "tab",   BX_KEY_TAB },
  { "up",    BX_KEY_UP },
  { "win",   BX_KEY_WIN_L },
  { "print", BX_KEY_PRINT },
  { "power", BX_KEY_POWER_POWER },
  { "scrlck", BX_KEY_SCRL_LOCK }
};

Bit32u get_user_key(char *key)
{
  int i = 0;

  while (i < N_USER_KEYS) {
    if (!strcmp(key, user_keys[i].key))
      return user_keys[i].symbol;
    i++;
  }
  return BX_KEY_UNKNOWN;
}

// font bitmap helper function

Bit8u reverse_bitorder(Bit8u b)
{
  Bit8u ret = 0;

  for (unsigned i = 0; i < 8; i++) {
    ret |= (b & 0x01) << (7 - i);
    b >>= 1;
  }
  return ret;
}

// common gui implementation

bx_gui_c::bx_gui_c(void): disp_mode(DISP_MODE_SIM)
{
  put("GUI"); // Init in specific_init
  bx_headerbar_entries = 0;
  statusitem_count = 0;
  led_timer_index = BX_NULL_TIMER_HANDLE;
  framebuffer = NULL;
  guest_textmode = 1;
  guest_fwidth = 8;
  guest_fheight = 16;
  guest_xres = 640;
  guest_yres = 480;
  guest_bpp = 8;
  snapshot_mode = 0;
  snapshot_buffer = NULL;
  command_mode.present = 0;
  command_mode.active = 0;
  marker_count = 0;
  memset(palette, 0, sizeof(palette));
  memset(vga_charmap[0], 0, 0x2000);
  memset(vga_charmap[1], 0, 0x2000);
  memset(&gui_opts, 0, sizeof(gui_opts));
}

bx_gui_c::~bx_gui_c()
{
  if (framebuffer != NULL) {
    delete [] framebuffer;
  }
#if BX_USE_GUI_CONSOLE
  if (console.running) {
    console_cleanup();
  }
#endif
}

void bx_gui_c::init(int argc, char **argv, unsigned max_xres, unsigned max_yres,
                    unsigned tilewidth, unsigned tileheight)
{
  BX_GUI_THIS new_gfx_api = 0;
  BX_GUI_THIS host_xres = 640;
  BX_GUI_THIS host_yres = 480;
  BX_GUI_THIS host_bpp = 8;
  BX_GUI_THIS new_text_api = 0;
  memset(&BX_GUI_THIS tm_info, 0, sizeof(bx_vga_tminfo_t));
  BX_GUI_THIS cursor_address = 0;
  BX_GUI_THIS max_xres = max_xres;
  BX_GUI_THIS max_yres = max_yres;
  BX_GUI_THIS x_tilesize = tilewidth;
  BX_GUI_THIS y_tilesize = tileheight;
  BX_GUI_THIS dialog_caps = BX_GUI_DLG_RUNTIME | BX_GUI_DLG_SAVE_RESTORE;
#if BX_USE_GUI_CONSOLE
  BX_GUI_THIS console.present = 0;
  BX_GUI_THIS console.running = 0;
#endif
  BX_GUI_THIS toggle_method = SIM->get_param_enum(BXPN_MOUSE_TOGGLE)->get();
  BX_GUI_THIS toggle_keystate = 0;
  switch (toggle_method) {
    case BX_MOUSE_TOGGLE_CTRL_MB:
      strcpy(mouse_toggle_text, "CTRL + 3rd button");
      break;
    case BX_MOUSE_TOGGLE_CTRL_F10:
      strcpy(mouse_toggle_text, "CTRL + F10");
      break;
    case BX_MOUSE_TOGGLE_CTRL_ALT:
      strcpy(mouse_toggle_text, "CTRL + ALT");
      break;
    case BX_MOUSE_TOGGLE_CTRL_ALT_G:
      strcpy(mouse_toggle_text, "CTRL + ALT + G");
      break;
    case BX_MOUSE_TOGGLE_F12:
      strcpy(mouse_toggle_text, "F12");
      break;
  }

  specific_init(argc, argv, BX_HEADER_BAR_Y);

  // Define some bitmaps to use in the headerbar
  BX_GUI_THIS floppyA_bmap_id = create_bitmap(bx_floppya_bmap,
                          BX_FLOPPYA_BMAP_X, BX_FLOPPYA_BMAP_Y);
  BX_GUI_THIS floppyA_eject_bmap_id = create_bitmap(bx_floppya_eject_bmap,
                          BX_FLOPPYA_BMAP_X, BX_FLOPPYA_BMAP_Y);
  BX_GUI_THIS floppyB_bmap_id = create_bitmap(bx_floppyb_bmap,
                          BX_FLOPPYB_BMAP_X, BX_FLOPPYB_BMAP_Y);
  BX_GUI_THIS floppyB_eject_bmap_id = create_bitmap(bx_floppyb_eject_bmap,
                          BX_FLOPPYB_BMAP_X, BX_FLOPPYB_BMAP_Y);
  BX_GUI_THIS cdrom1_bmap_id = create_bitmap(bx_cdrom1_bmap,
                          BX_CDROM1_BMAP_X, BX_CDROM1_BMAP_Y);
  BX_GUI_THIS cdrom1_eject_bmap_id = create_bitmap(bx_cdrom1_eject_bmap,
                          BX_CDROM1_BMAP_X, BX_CDROM1_BMAP_Y);
  BX_GUI_THIS mouse_bmap_id = create_bitmap(bx_mouse_bmap,
                          BX_MOUSE_BMAP_X, BX_MOUSE_BMAP_Y);
  BX_GUI_THIS nomouse_bmap_id = create_bitmap(bx_nomouse_bmap,
                          BX_MOUSE_BMAP_X, BX_MOUSE_BMAP_Y);

  BX_GUI_THIS power_bmap_id = create_bitmap(bx_power_bmap, BX_POWER_BMAP_X, BX_POWER_BMAP_Y);
  BX_GUI_THIS reset_bmap_id = create_bitmap(bx_reset_bmap, BX_RESET_BMAP_X, BX_RESET_BMAP_Y);
  BX_GUI_THIS snapshot_bmap_id = create_bitmap(bx_snapshot_bmap, BX_SNAPSHOT_BMAP_X, BX_SNAPSHOT_BMAP_Y);
  BX_GUI_THIS copy_bmap_id = create_bitmap(bx_copy_bmap, BX_COPY_BMAP_X, BX_COPY_BMAP_Y);
  BX_GUI_THIS paste_bmap_id = create_bitmap(bx_paste_bmap, BX_PASTE_BMAP_X, BX_PASTE_BMAP_Y);
  BX_GUI_THIS config_bmap_id = create_bitmap(bx_config_bmap, BX_CONFIG_BMAP_X, BX_CONFIG_BMAP_Y);
  BX_GUI_THIS user_bmap_id = create_bitmap(bx_user_bmap, BX_USER_BMAP_X, BX_USER_BMAP_Y);
  BX_GUI_THIS save_restore_bmap_id = create_bitmap(bx_save_restore_bmap,
                          BX_SAVE_RESTORE_BMAP_X, BX_SAVE_RESTORE_BMAP_Y);

#if BX_USB_DEBUGGER
  BX_GUI_THIS usbdbg_bmap_id = create_bitmap(bx_usbdbg_bmap,
                          BX_USB_BMAP_X, BX_USB_BMAP_Y);
  BX_GUI_THIS usbdbg_dis_bmap_id = create_bitmap(bx_usbdbg_dis_bmap,
                          BX_USB_BMAP_X, BX_USB_BMAP_Y);
  BX_GUI_THIS usbdbg_trigger_bmap_id = create_bitmap(bx_usbdbg_trigger_bmap,
                          BX_USB_BMAP_X, BX_USB_BMAP_Y);
#endif

  // Add the initial bitmaps to the headerbar, and enable callback routine, for use
  // when that bitmap is clicked on. The floppy and cdrom devices are not
  // initialized yet. so we just set the bitmaps to ejected for now.

  // Floppy A:
  BX_GUI_THIS floppyA_hbar_id = headerbar_bitmap(BX_GUI_THIS floppyA_eject_bmap_id,
                          BX_GRAVITY_LEFT, floppyA_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS floppyA_hbar_id, "Change floppy A: media");

  // Floppy B:
  BX_GUI_THIS floppyB_hbar_id = headerbar_bitmap(BX_GUI_THIS floppyB_eject_bmap_id,
                          BX_GRAVITY_LEFT, floppyB_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS floppyB_hbar_id, "Change floppy B: media");

  // First CD-ROM
  BX_GUI_THIS cdrom1_hbar_id = headerbar_bitmap(BX_GUI_THIS cdrom1_eject_bmap_id,
                          BX_GRAVITY_LEFT, cdrom1_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS cdrom1_hbar_id, "Change first CDROM media");

  // Mouse button
  if (SIM->get_param_bool(BXPN_MOUSE_ENABLED)->get())
    BX_GUI_THIS mouse_hbar_id = headerbar_bitmap(BX_GUI_THIS mouse_bmap_id,
                          BX_GRAVITY_LEFT, toggle_mouse_enable);
  else
    BX_GUI_THIS mouse_hbar_id = headerbar_bitmap(BX_GUI_THIS nomouse_bmap_id,
                          BX_GRAVITY_LEFT, toggle_mouse_enable);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS mouse_hbar_id, "Enable mouse capture");

#if BX_USB_DEBUGGER
  // USB button
  if (BX_GUI_THIS dialog_caps & BX_GUI_DLG_USB) {
    BX_GUI_THIS usbdbg_hbar_id = headerbar_bitmap(BX_GUI_THIS usbdbg_dis_bmap_id,
                          BX_GRAVITY_LEFT, usb_handler);
    BX_GUI_THIS set_tooltip(BX_GUI_THIS usbdbg_hbar_id, "USB debugger support not enabled");
  }
#endif

  // These are the buttons on the right side.  They are created in order
  // of right to left.

  // Power button
  BX_GUI_THIS power_hbar_id = headerbar_bitmap(BX_GUI_THIS power_bmap_id,
                          BX_GRAVITY_RIGHT, power_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS power_hbar_id, "Turn power off");
  // Save/Restore Button
  BX_GUI_THIS save_restore_hbar_id = headerbar_bitmap(BX_GUI_THIS save_restore_bmap_id,
                          BX_GRAVITY_RIGHT, save_restore_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS save_restore_hbar_id, "Save simulation state");
  // Reset button
  BX_GUI_THIS reset_hbar_id = headerbar_bitmap(BX_GUI_THIS reset_bmap_id,
                          BX_GRAVITY_RIGHT, reset_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS reset_hbar_id, "Reset the system");
  // Configure button
  BX_GUI_THIS config_hbar_id = headerbar_bitmap(BX_GUI_THIS config_bmap_id,
                          BX_GRAVITY_RIGHT, config_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS config_hbar_id, "Runtime config dialog");
  // Snapshot button
  BX_GUI_THIS snapshot_hbar_id = headerbar_bitmap(BX_GUI_THIS snapshot_bmap_id,
                          BX_GRAVITY_RIGHT, snapshot_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS snapshot_hbar_id, "Save snapshot of the Bochs screen");
  // Paste button
  BX_GUI_THIS paste_hbar_id = headerbar_bitmap(BX_GUI_THIS paste_bmap_id,
                          BX_GRAVITY_RIGHT, paste_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS paste_hbar_id, "Paste clipboard text as emulated keystrokes");
  // Copy button
  BX_GUI_THIS copy_hbar_id = headerbar_bitmap(BX_GUI_THIS copy_bmap_id,
                          BX_GRAVITY_RIGHT, copy_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS copy_hbar_id, "Copy text mode screen to the clipboard");
  // User button
  BX_GUI_THIS user_hbar_id = headerbar_bitmap(BX_GUI_THIS user_bmap_id,
                          BX_GRAVITY_RIGHT, userbutton_handler);
  BX_GUI_THIS set_tooltip(BX_GUI_THIS user_hbar_id, "Send keyboard shortcut");

  if (!parse_user_shortcut(SIM->get_param_string(BXPN_USER_SHORTCUT)->getptr())) {
    SIM->get_param_string(BXPN_USER_SHORTCUT)->set("none");
  }

  BX_GUI_THIS charmap_updated = 0;

  if (!BX_GUI_THIS new_gfx_api && (BX_GUI_THIS framebuffer == NULL)) {
    BX_GUI_THIS framebuffer = new Bit8u[max_xres * max_yres * 4];
  }
  show_headerbar();

  // register timer for status bar LEDs
  if (BX_GUI_THIS led_timer_index == BX_NULL_TIMER_HANDLE) {
    BX_GUI_THIS led_timer_index =
      bx_virt_timer.register_timer(this, led_timer_handler, 100000, 1, 1, 1,
                                   "status bar LEDs");
  }
}

void bx_gui_c::cleanup(void)
{
  statusitem_count = 0;
}

void bx_gui_c::update_drive_status_buttons(void)
{
  BX_GUI_THIS floppyA_status = (SIM->get_param_enum(BXPN_FLOPPYA_STATUS)->get() == BX_INSERTED);
  BX_GUI_THIS floppyB_status = (SIM->get_param_enum(BXPN_FLOPPYB_STATUS)->get() == BX_INSERTED);
  bx_param_c *cdrom = SIM->get_first_cdrom();
  if (cdrom != NULL) {
    BX_GUI_THIS cdrom1_status = SIM->get_param_enum("status", cdrom)->get();
  } else {
    BX_GUI_THIS cdrom1_status = BX_EJECTED;
  }
  if (BX_GUI_THIS floppyA_status)
    replace_bitmap(BX_GUI_THIS floppyA_hbar_id, BX_GUI_THIS floppyA_bmap_id);
  else {
#if BX_WITH_MACOS
    // If we are using the Mac floppy driver, eject the disk
    // from the floppy drive.  This doesn't work in MacOS X.
    if (!strcmp(SIM->get_param_string(BXPN_FLOPPYA_PATH)->getptr(), SuperDrive))
      DiskEject(1);
#endif
    replace_bitmap(BX_GUI_THIS floppyA_hbar_id, BX_GUI_THIS floppyA_eject_bmap_id);
  }
  if (BX_GUI_THIS floppyB_status)
    replace_bitmap(BX_GUI_THIS floppyB_hbar_id, BX_GUI_THIS floppyB_bmap_id);
  else {
#if BX_WITH_MACOS
    // If we are using the Mac floppy driver, eject the disk
    // from the floppy drive.  This doesn't work in MacOS X.
    if (!strcmp(SIM->get_param_string(BXPN_FLOPPYB_PATH)->getptr(), SuperDrive))
      DiskEject(1);
#endif
    replace_bitmap(BX_GUI_THIS floppyB_hbar_id, BX_GUI_THIS floppyB_eject_bmap_id);
  }
  if (BX_GUI_THIS cdrom1_status)
    replace_bitmap(BX_GUI_THIS cdrom1_hbar_id, BX_GUI_THIS cdrom1_bmap_id);
  else {
    replace_bitmap(BX_GUI_THIS cdrom1_hbar_id, BX_GUI_THIS cdrom1_eject_bmap_id);
  }
}

void bx_gui_c::floppyA_handler(void)
{
  int ret = 1;

  if (SIM->get_param_enum(BXPN_FLOPPYA_DEVTYPE)->get() == BX_FDD_NONE)
    return; // no primary floppy device present
  if (!BX_GUI_THIS fullscreen_mode &&
      (BX_GUI_THIS dialog_caps & BX_GUI_DLG_FLOPPY)) {
    // instead of just toggling the status, bring up a dialog asking what disk
    // image you want to switch to.
    ret = SIM->ask_param(BXPN_FLOPPYA);
  } else {
    SIM->get_param_enum(BXPN_FLOPPYA_STATUS)->set(!BX_GUI_THIS floppyA_status);
  }
  if (ret > 0) {
    SIM->update_runtime_options();
  }
}

void bx_gui_c::floppyB_handler(void)
{
  int ret = 1;

  if (SIM->get_param_enum(BXPN_FLOPPYB_DEVTYPE)->get() == BX_FDD_NONE)
    return; // no secondary floppy device present
  if (!BX_GUI_THIS fullscreen_mode &&
      (BX_GUI_THIS dialog_caps & BX_GUI_DLG_FLOPPY)) {
    // instead of just toggling the status, bring up a dialog asking what disk
    // image you want to switch to.
    ret = SIM->ask_param(BXPN_FLOPPYB);
  } else {
    SIM->get_param_enum(BXPN_FLOPPYB_STATUS)->set(!BX_GUI_THIS floppyB_status);
  }
  if (ret > 0) {
    SIM->update_runtime_options();
  }
}

void bx_gui_c::cdrom1_handler(void)
{
  int ret = 1;
  bx_param_c *cdrom = SIM->get_first_cdrom();

  if (cdrom == NULL)
    return;  // no cdrom found
  if (BX_GUI_THIS dialog_caps & BX_GUI_DLG_CDROM) {
    // instead of just toggling the status, bring up a dialog asking what disk
    // image you want to switch to.
    // This code handles the first cdrom only. The cdrom drives #2, #3 and
    // #4 are handled in the runtime configuaration.
    ret = SIM->ask_param(cdrom);
  } else {
    SIM->get_param_enum("status", cdrom)->set(!BX_GUI_THIS cdrom1_status);
  }
  if (ret > 0) {
    SIM->update_runtime_options();
  }
}

void bx_gui_c::reset_handler(void)
{
  BX_INFO(("system RESET callback"));
  BX_GUI_THIS statusbar_setall(0);
  bx_pc_system.Reset(BX_RESET_HARDWARE);
}

void bx_gui_c::power_handler(void)
{
#ifdef BX_GUI_CONFIRM_QUIT
  if (!SIM->ask_yes_no("Quit Bochs", "Are you sure ?", 0))
    return;
#endif
  // the user pressed power button, so there's no doubt they want bochs
  // to quit.  Change panics to fatal for the GUI and then do a panic.
  bx_user_quit = 1;
  BX_FATAL(("POWER button turned off."));
  // shouldn't reach this point, but if you do, QUIT!!!
  fprintf (stderr, "Bochs is exiting because you pressed the power button.\n");
  BX_EXIT(1);
}

void bx_gui_c::make_text_snapshot(char **snapshot, Bit32u *length)
{
  Bit8u* raw_snap = NULL;
  char *clean_snap;
  unsigned line_addr, txt_addr, txHeight, txWidth;

  DEV_vga_get_text_snapshot(&raw_snap, &txHeight, &txWidth);
  clean_snap = new char[txHeight*(txWidth+2)+1];
  txt_addr = 0;
  for (unsigned i=0; i<txHeight; i++) {
    line_addr = i * txWidth * 2;
    for (unsigned j=0; j<(txWidth*2); j+=2) {
      if (!raw_snap[line_addr+j])
        raw_snap[line_addr+j] = 0x20;
      clean_snap[txt_addr++] = raw_snap[line_addr+j];
    }
    while ((txt_addr > 0) && (clean_snap[txt_addr-1] == ' ')) txt_addr--;
#ifdef WIN32
    clean_snap[txt_addr++] = 13;
#endif
    clean_snap[txt_addr++] = 10;
  }
  clean_snap[txt_addr] = 0;
  *snapshot = clean_snap;
  *length = txt_addr;
}

Bit32u bx_gui_c::set_snapshot_mode(bool mode)
{
  unsigned pixel_bytes, bufsize;

  BX_GUI_THIS snapshot_mode = mode;
  if (mode) {
    pixel_bytes = ((BX_GUI_THIS guest_bpp + 1) >> 3);
    bufsize = BX_GUI_THIS guest_xres * BX_GUI_THIS guest_yres * pixel_bytes;
    BX_GUI_THIS snapshot_buffer = new Bit8u[bufsize];
    if (BX_GUI_THIS snapshot_buffer != NULL) {
      memset(BX_GUI_THIS snapshot_buffer, 0, bufsize);
      DEV_vga_refresh(1);
      return bufsize;
    }
  } else {
    if (BX_GUI_THIS snapshot_buffer != NULL) {
      delete [] BX_GUI_THIS snapshot_buffer;
      BX_GUI_THIS snapshot_buffer = NULL;
      DEV_vga_redraw_area(0, 0, BX_GUI_THIS guest_xres, BX_GUI_THIS guest_yres);
    }
  }
  return 0;
}

// create a text snapshot and copy to the system clipboard.  On guis that
// we haven't figured out how to support yet, dump to a file instead.
void bx_gui_c::copy_handler(void)
{
  Bit32u len;
  char *text_snapshot;

  if (BX_GUI_THIS guest_textmode) {
    make_text_snapshot(&text_snapshot, &len);
    if (!BX_GUI_THIS set_clipboard_text(text_snapshot, len)) {
      // platform specific code failed, use portable code instead
      FILE *fp = fopen("copy.txt", "w");
      if (fp != NULL) {
        fwrite(text_snapshot, 1, len, fp);
        fclose(fp);
      }
    }
    delete [] text_snapshot;
  } else {
    BX_ERROR(("copy button failed, graphics mode not implemented"));
  }
}

#define BX_SNAPSHOT_TXT 0
#define BX_SNAPSHOT_BMP 1

// create a text snapshot and dump it to a file
void bx_gui_c::snapshot_handler(void)
{
  int fd, i, j, pitch;
  Bit8u *snapshot_ptr = NULL;
  Bit8u *row_buffer, *pixel_ptr, *row_ptr;
  Bit8u bmp_header[54], iBits, b1, b2;
  Bit32u ilen, len, rlen;
  char filename[BX_PATHNAME_LEN], msg[80], *ext;
  Bit8u snap_fmt;

  if (BX_GUI_THIS guest_textmode) {
    strcpy(filename, "snapshot.txt");
    snap_fmt = BX_SNAPSHOT_TXT;
  } else {
    strcpy(filename, "snapshot.bmp");
    snap_fmt = BX_SNAPSHOT_BMP;
  }
  if (!BX_GUI_THIS fullscreen_mode &&
      (BX_GUI_THIS dialog_caps & BX_GUI_DLG_SNAPSHOT)) {
    int ret = SIM->ask_filename(filename, sizeof(filename),
                                "Save snapshot as...", filename,
                                bx_param_string_c::SAVE_FILE_DIALOG);
    if (ret < 0) { // cancelled
      return;
    }
    ext = strrchr(filename, '.');
    if (ext == NULL) {
      SIM->message_box("ERROR", "Unknown snapshot file format");
      return;
    } else {
      ext++;
      if (BX_GUI_THIS guest_textmode && !strcmp(ext, "txt")) {
        snap_fmt = BX_SNAPSHOT_TXT;
      } else if (!strcmp(ext, "bmp")) {
        snap_fmt = BX_SNAPSHOT_BMP;
      } else {
        sprintf(msg, "Unsupported snapshot file format '%s'", ext);
        SIM->message_box("ERROR", msg);
        return;
      }
    }
  }
  if (snap_fmt == BX_SNAPSHOT_BMP) {
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC
#ifdef O_BINARY
              | O_BINARY
#endif
              , S_IRUSR | S_IWUSR
              );
    if (fd < 0) {
      SIM->message_box("ERROR", "snapshot button failed: cannot create BMP file");
      return;
    }
    ilen =  BX_GUI_THIS set_snapshot_mode(1);
    if (ilen > 0) {
      BX_INFO(("GFX snapshot: %u x %u x %u bpp (%u bytes)", BX_GUI_THIS guest_xres,
               BX_GUI_THIS guest_yres, BX_GUI_THIS guest_bpp, ilen));
    } else {
      close(fd);
      SIM->message_box("ERROR", "snapshot button failed: cannot allocate memory");
      return;
    }
    iBits = (BX_GUI_THIS guest_bpp == 8) ? 8 : 24;
    rlen = (BX_GUI_THIS guest_xres * (iBits >> 3) + 3) & ~3;
    len = rlen * BX_GUI_THIS guest_yres + 54;
    if (BX_GUI_THIS guest_bpp == 8) {
      len += (256 * 4);
    }
    memset(bmp_header, 0, 54);
    bmp_header[0] = 0x42;
    bmp_header[1] = 0x4d;
    bmp_header[2] = len & 0xff;
    bmp_header[3] = (len >> 8) & 0xff;
    bmp_header[4] = (len >> 16) & 0xff;
    bmp_header[5] = (len >> 24) & 0xff;
    bmp_header[10] = 54;
    if (BX_GUI_THIS guest_bpp == 8) {
      bmp_header[11] = 4;
    }
    bmp_header[14] = 40;
    bmp_header[18] = BX_GUI_THIS guest_xres & 0xff;
    bmp_header[19] = (BX_GUI_THIS guest_xres >> 8) & 0xff;
    bmp_header[22] = BX_GUI_THIS guest_yres & 0xff;
    bmp_header[23] = (BX_GUI_THIS guest_yres >> 8) & 0xff;
    bmp_header[26] = 1;
    bmp_header[28] = iBits;
    write(fd, bmp_header, 54);
    if (BX_GUI_THIS guest_bpp == 8) {
      write(fd, BX_GUI_THIS palette, 256 * 4);
    }
    pitch = BX_GUI_THIS guest_xres * ((BX_GUI_THIS guest_bpp + 1) >> 3);
    row_buffer = new Bit8u[rlen];
    row_ptr = BX_GUI_THIS snapshot_buffer + ((BX_GUI_THIS guest_yres - 1) * pitch);
    for (i = BX_GUI_THIS guest_yres; i > 0; i--) {
      memset(row_buffer, 0, rlen);
      if ((BX_GUI_THIS guest_bpp == 8) || (BX_GUI_THIS guest_bpp == 24)) {
        memcpy(row_buffer, row_ptr, pitch);
      } else if ((BX_GUI_THIS guest_bpp == 15) || (BX_GUI_THIS guest_bpp == 16)) {
        pixel_ptr = row_ptr;
        for (j = 0; j < (int)(BX_GUI_THIS guest_xres * 3); j+=3) {
          b1 = *(pixel_ptr++);
          b2 = *(pixel_ptr++);
          *(row_buffer+j)   = (b1 << 3);
          if (BX_GUI_THIS guest_bpp == 15) {
            *(row_buffer+j+1) = ((b1 & 0xe0) >> 2) | (b2 << 6);
            *(row_buffer+j+2) = (b2 & 0x7c) << 1;
          } else {
            *(row_buffer+j+1) = ((b1 & 0xe0) >> 3) | (b2 << 5);
            *(row_buffer+j+2) = (b2 & 0xf8);
          }
        }
      } else if (BX_GUI_THIS guest_bpp == 32) {
        pixel_ptr = row_ptr;
        for (j = 0; j < (int)(BX_GUI_THIS guest_xres * 3); j+=3) {
          *(row_buffer+j)   = *(pixel_ptr++);
          *(row_buffer+j+1) = *(pixel_ptr++);
          *(row_buffer+j+2) = *(pixel_ptr++);
          pixel_ptr++;
        }
      }
      write(fd, row_buffer, rlen);
      row_ptr -= pitch;
    }
    delete [] row_buffer;
    close(fd);
    BX_GUI_THIS set_snapshot_mode(0);
  } else {
    make_text_snapshot((char**)&snapshot_ptr, &len);
    FILE *fp = fopen(filename, "wb");
    if (fp != NULL) {
      fwrite(snapshot_ptr, 1, len, fp);
      fclose(fp);
    } else {
      SIM->message_box("ERROR", "snapshot button failed: cannot create text file");
    }
    delete [] snapshot_ptr;
  }
}

// Read ASCII chars from the system clipboard and paste them into bochs.
// Note that paste cannot work with the key mapping tables loaded.
void bx_gui_c::paste_handler(void)
{
  Bit32s nbytes;
  Bit8u *bytes;
  if (!bx_keymap.isKeymapLoaded ()) {
    BX_ERROR(("keyboard_mapping disabled, so paste cannot work"));
    return;
  }
  if (!BX_GUI_THIS get_clipboard_text(&bytes, &nbytes)) {
    BX_ERROR (("paste not implemented on this platform"));
    return;
  }
  BX_INFO (("pasting %d bytes", nbytes));
  DEV_kbd_paste_bytes (bytes, nbytes);
}

void bx_gui_c::config_handler(void)
{
  if (BX_GUI_THIS dialog_caps & BX_GUI_DLG_RUNTIME) {
    if (!SIM->configuration_interface(NULL, CI_RUNTIME_CONFIG)) {
      bx_exit(1);
    }
  }
}

#if BX_USB_DEBUGGER
void bx_gui_c::usb_handler(void)
{
  if (BX_GUI_THIS dialog_caps & BX_GUI_DLG_USB) {
    // Once we set the trigger, don't allow the user to press the button again
    if (SIM->get_param_num(BXPN_USB_DEBUG_START_FRAME)->get() < BX_USB_DEBUG_SOF_TRIGGER)
      SIM->usb_debug_interface(USB_DEBUG_FRAME, 0, 0, 0);
  }
}

void bx_gui_c::set_usbdbg_bitmap(bool trigger)
{
  set_tooltip(BX_GUI_THIS usbdbg_hbar_id, "Trigger the USB debugger");
  replace_bitmap(usbdbg_hbar_id, trigger ? usbdbg_trigger_bmap_id : usbdbg_bmap_id);
}
#endif

void bx_gui_c::toggle_mouse_enable(void)
{
  int old = SIM->get_param_bool(BXPN_MOUSE_ENABLED)->get();
  BX_DEBUG (("toggle mouse_enabled, now %d", !old));
  SIM->get_param_bool(BXPN_MOUSE_ENABLED)->set(!old);
}

bool bx_gui_c::mouse_toggle_check(Bit32u key, bool pressed)
{
  Bit32u newstate;
  bool toggle = 0;

  if (console_running())
    return 0;
  newstate = toggle_keystate;
  if (pressed) {
    newstate |= key;
    if (newstate == toggle_keystate) return 0;
    if ((key == BX_MT_KEY_CTRL) || (key == BX_MT_KEY_ALT)) {
      newstate &= (BX_MT_KEY_CTRL | BX_MT_KEY_ALT);
    }
    switch (toggle_method) {
      case BX_MOUSE_TOGGLE_CTRL_MB:
        toggle = (newstate & BX_GUI_MT_CTRL_MB) == BX_GUI_MT_CTRL_MB;
        if (!toggle) {
          toggle = (newstate & BX_GUI_MT_CTRL_LRB) == BX_GUI_MT_CTRL_LRB;
        }
        break;
      case BX_MOUSE_TOGGLE_CTRL_F10:
        toggle = (newstate & BX_GUI_MT_CTRL_F10) == BX_GUI_MT_CTRL_F10;
        break;
      case BX_MOUSE_TOGGLE_CTRL_ALT:
        toggle = (newstate & BX_GUI_MT_CTRL_ALT_G) == BX_GUI_MT_CTRL_ALT;
        break;
      case BX_MOUSE_TOGGLE_CTRL_ALT_G:
        toggle = (newstate & BX_GUI_MT_CTRL_ALT_G) == BX_GUI_MT_CTRL_ALT_G;
        break;
      case BX_MOUSE_TOGGLE_F12:
        toggle = (newstate == BX_GUI_MT_F12);
        break;
    }
    toggle_keystate = newstate;
  } else {
    toggle_keystate &= ~key;
  }
  return toggle;
}

const char* bx_gui_c::get_toggle_info(void)
{
  return mouse_toggle_text;
}

Bit8u bx_gui_c::get_modifier_keys(void)
{
  if ((keymodstate & BX_MOD_KEY_CAPS) > 0) {
    return ((keymodstate & ~BX_MOD_KEY_CAPS) | BX_MOD_KEY_SHIFT);
  } else {
    return keymodstate;
  }
}

Bit8u bx_gui_c::set_modifier_keys(Bit8u modifier, bool pressed)
{
  Bit8u newstate = keymodstate, changestate = 0;

  if (modifier == BX_MOD_KEY_CAPS) {
    if (pressed) {
      newstate ^= modifier;
    }
  } else {
    if (pressed) {
      newstate |= modifier;
    } else {
      newstate &= ~modifier;
    }
  }
  changestate = keymodstate ^ newstate;
  keymodstate = newstate;
  return changestate;
}

bool bx_gui_c::parse_user_shortcut(const char *val)
{
  char *ptr, shortcut_tmp[512];
  Bit32u symbol, new_shortcut[4];
  int i, len = 0;

  user_shortcut_error = 0;
  if ((strlen(val) == 0) || !strcmp(val, "none")) {
    user_shortcut_len = 0;
    return 1;
  } else {
    len = 0;
    strcpy(shortcut_tmp, val);
    ptr = strtok(shortcut_tmp, "-");
    while (ptr) {
      symbol = get_user_key(ptr);
      if (symbol == BX_KEY_UNKNOWN) {
        BX_ERROR(("Unknown key symbol '%s' ignored", ptr));
        user_shortcut_error = 1;
        return 0;
      }
      if (len < 3) {
        new_shortcut[len++] = symbol;
        ptr = strtok(NULL, "-");
      } else {
        BX_ERROR(("Ignoring extra key symbol '%s'", ptr));
        break;
      }
    }
    for (i = 0; i < len; i++) {
      user_shortcut[i] = new_shortcut[i];
    }
    user_shortcut_len = len;
    return 1;
  }
}

void bx_gui_c::userbutton_handler(void)
{
  int i, ret = 1;

  if (!BX_GUI_THIS fullscreen_mode &&
      (BX_GUI_THIS dialog_caps & BX_GUI_DLG_USER)) {
    ret = SIM->ask_param(BXPN_USER_SHORTCUT);
  }
  if (BX_GUI_THIS user_shortcut_error) {
    SIM->message_box("ERROR", "Ignoring invalid user shortcut");
    return;
  }
  if ((ret > 0) && (BX_GUI_THIS user_shortcut_len > 0)) {
    i = 0;
    while (i < BX_GUI_THIS user_shortcut_len) {
      DEV_kbd_gen_scancode(BX_GUI_THIS user_shortcut[i++]);
    }
    i--;
    while (i >= 0) {
      DEV_kbd_gen_scancode(BX_GUI_THIS user_shortcut[i--] | BX_KEY_RELEASED);
    }
  }
}

void bx_gui_c::save_restore_handler(void)
{
  char sr_path[BX_PATHNAME_LEN];

  if (BX_GUI_THIS dialog_caps & BX_GUI_DLG_SAVE_RESTORE) {
    BX_GUI_THIS set_display_mode(DISP_MODE_CONFIG);
    sr_path[0] = 0;
    int ret = SIM->ask_filename(sr_path, sizeof(sr_path),
                            "Save Bochs state to folder...", "none",
                            bx_param_string_c::SELECT_FOLDER_DLG);
    if ((ret >= 0) && (strcmp(sr_path, "none"))) {
      void *hwnd = SIM->ml_message_box("Please wait.", "Waiting for the 'save state' to finish.\n"
              "Depending on the size of your emulated disk images, this may be a little while.");
      if (SIM->save_state(sr_path)) {
        SIM->ml_message_box_kill(hwnd);
        if (!SIM->ask_yes_no("WARNING",
              "The state of cpu, memory, devices and hard drive images is saved now.\n"
              "It is possible to continue, but when using the restore function in a\n"
              "new Bochs session, all changes after this checkpoint will be lost.\n\n"
              "Do you want to continue?", 0)) {
          power_handler();
        }
      } else {
        SIM->ml_message_box_kill(hwnd);
        SIM->message_box("ERROR", "Failed to save state");
      }
    }
    BX_GUI_THIS set_display_mode(DISP_MODE_SIM);
  }
}

void bx_gui_c::marklog_handler(void)
{
  BX_INFO(("### MARKER #%u", BX_GUI_THIS marker_count++));
}

void bx_gui_c::headerbar_click(int x)
{
  int xorigin;

  for (unsigned i=0; i<bx_headerbar_entries; i++) {
    if (bx_headerbar_entry[i].alignment == BX_GRAVITY_LEFT)
      xorigin = bx_headerbar_entry[i].xorigin;
    else
      xorigin = guest_xres - bx_headerbar_entry[i].xorigin;
    if ((x>=xorigin) && (x<(xorigin+int(bx_headerbar_entry[i].xdim)))) {
      if (console_running() && (i != power_hbar_id))
        return;
      bx_headerbar_entry[i].f();
      return;
    }
  }
}

void bx_gui_c::mouse_enabled_changed(bool val)
{
  // This is only called when SIM->get_init_done is 1.  Note that VAL
  // is the new value of mouse_enabled, which may not match the old
  // value which is still in SIM->get_param_bool(BXPN_MOUSE_ENABLED)->get().
  BX_DEBUG (("replacing the mouse bitmaps"));
  if (val)
    BX_GUI_THIS replace_bitmap(BX_GUI_THIS mouse_hbar_id, BX_GUI_THIS mouse_bmap_id);
  else
    BX_GUI_THIS replace_bitmap(BX_GUI_THIS mouse_hbar_id, BX_GUI_THIS nomouse_bmap_id);
  // give the GUI a chance to respond to the event.  Most guis will hide
  // the native mouse cursor and do something to trap the mouse inside the
  // bochs VGA display window.
  BX_GUI_THIS mouse_enabled_changed_specific (val);
}

void bx_gui_c::init_signal_handlers()
{
#if BX_GUI_SIGHANDLER
  if (bx_gui_sighandler)
  {
    Bit32u mask = bx_gui->get_sighandler_mask ();
    for (Bit32u sig=0; sig<32; sig++)
    {
      if (mask & (1<<sig))
        signal (sig, bx_signal_handler);
    }
  }
#endif
}

void bx_gui_c::set_text_charmap(Bit8u map, Bit8u *fbuffer)
{
  memcpy(& BX_GUI_THIS vga_charmap[map], fbuffer, 0x2000);
  for (unsigned i=0; i<256; i++) BX_GUI_THIS char_changed[map][i] = 1;
  BX_GUI_THIS charmap_updated = 1;
}

void bx_gui_c::beep_on(float frequency)
{
  BX_DEBUG(("GUI Beep ON (frequency=%.2f)", frequency));
}

void bx_gui_c::beep_off()
{
  BX_DEBUG(("GUI Beep OFF"));
}

int bx_gui_c::register_statusitem(const char *text, bool auto_off)
{
  unsigned id = statusitem_count;

  for (unsigned i = 0; i < statusitem_count; i++) {
    if (!statusitem[i].in_use) {
      id = i;
      break;
    }
  }
  if (id == statusitem_count) {
    if (statusitem_count == BX_MAX_STATUSITEMS) {
      return -1;
    } else {
      statusitem_count++;
    }
  }
  statusitem[id].in_use = 1;
  strncpy(statusitem[id].text, text, 8);
  statusitem[id].text[7] = 0;
  statusitem[id].auto_off = auto_off;
  statusitem[id].counter = 0;
  statusitem[id].active = 0;
  statusitem[id].mode = 0;
  statusbar_setitem_specific(id, 0, 0);
  return id;
}

void bx_gui_c::unregister_statusitem(int id)
{
  if ((id >= 0) && (id < (int)statusitem_count)) {
    strcpy(statusitem[id].text, "      ");
    statusbar_setitem_specific(id, 0, 0);
    if (id == (int)(statusitem_count - 1)) {
      statusitem_count--;
    } else {
      statusitem[id].in_use = 0;
    }
  }
}

void bx_gui_c::statusbar_setitem(int element, bool active, bool w)
{
  if (element < 0) {
    for (unsigned i = 0; i < statusitem_count; i++) {
      statusbar_setitem_specific(i, 0, 0);
    }
  } else if ((unsigned)element < statusitem_count) {
    if ((active != statusitem[element].active) ||
        (w != statusitem[element].mode)) {
      statusbar_setitem_specific(element, active, w);
      statusitem[element].active = active;
      statusitem[element].mode = w;
    }
    if (active && statusitem[element].auto_off) {
      statusitem[element].counter = 5;
    }
  }
}

void bx_gui_c::statusbar_setall(bool active)
{
  for (int i=0; i<BX_MAX_STATUSITEMS; i++)
    statusbar_setitem(i, active);
}

void bx_gui_c::led_timer_handler(void *this_ptr)
{
  bx_gui_c *class_ptr = (bx_gui_c *) this_ptr;
  class_ptr->led_timer();
}

void bx_gui_c::led_timer()
{
  for (unsigned i = 0; i < statusitem_count; i++) {
    if (statusitem[i].auto_off) {
      if (statusitem[i].counter > 0) {
        if (!(--statusitem[i].counter)) {
          statusbar_setitem(i, 0);
        }
      }
    }
  }
}

void bx_gui_c::get_capabilities(Bit16u *xres, Bit16u *yres, Bit16u *bpp)
{
  *xres = 1024;
  *yres = 768;
  *bpp = 32;
}

bool bx_gui_c::palette_change_common(Bit8u index, Bit8u red, Bit8u green, Bit8u blue)
{
  BX_GUI_THIS palette[index].red = red;
  BX_GUI_THIS palette[index].green = green;
  BX_GUI_THIS palette[index].blue = blue;
  return palette_change(index, red, green, blue);
}

void bx_gui_c::show_ips(Bit32u ips_count)
{
#if BX_SHOW_IPS
  BX_INFO(("ips = %3.3fM", ips_count / 1000000.0));
#endif
}

Bit8u bx_gui_c::get_mouse_headerbar_id()
{
  return BX_GUI_THIS mouse_hbar_id;
}

#if BX_DEBUGGER && BX_DEBUGGER_GUI
void bx_gui_c::init_debug_dialog(bool global_ini)
{
  extern void InitDebugDialog(bool GlobalIni);
  InitDebugDialog(global_ini);
}

void bx_gui_c::close_debug_dialog()
{
  extern void CloseDebugDialog();
  CloseDebugDialog();
}
#endif

// new graphics API (compatibility code)

bx_svga_tileinfo_t *bx_gui_c::graphics_tile_info(bx_svga_tileinfo_t *info)
{
  BX_GUI_THIS host_pitch = BX_GUI_THIS host_xres * ((BX_GUI_THIS host_bpp + 1) >> 3);

  info->bpp = BX_GUI_THIS host_bpp;
  info->pitch = BX_GUI_THIS host_pitch;
  switch (info->bpp) {
    case 15:
      info->red_shift = 15;
      info->green_shift = 10;
      info->blue_shift = 5;
      info->red_mask = 0x7c00;
      info->green_mask = 0x03e0;
      info->blue_mask = 0x001f;
      break;
    case 16:
      info->red_shift = 16;
      info->green_shift = 11;
      info->blue_shift = 5;
      info->red_mask = 0xf800;
      info->green_mask = 0x07e0;
      info->blue_mask = 0x001f;
      break;
    case 24:
    case 32:
      info->red_shift = 24;
      info->green_shift = 16;
      info->blue_shift = 8;
      info->red_mask = 0xff0000;
      info->green_mask = 0x00ff00;
      info->blue_mask = 0x0000ff;
      break;
  }
  info->is_indexed = (BX_GUI_THIS host_bpp == 8);
#ifdef BX_LITTLE_ENDIAN
  info->is_little_endian = 1;
#else
  info->is_little_endian = 0;
#endif

  return info;
}

Bit8u *bx_gui_c::graphics_tile_get(unsigned x0, unsigned y0,
                            unsigned *w, unsigned *h)
{
  if (x0+BX_GUI_THIS x_tilesize > BX_GUI_THIS host_xres) {
    *w = BX_GUI_THIS host_xres - x0;
  } else {
    *w = BX_GUI_THIS x_tilesize;
  }

  if (y0+BX_GUI_THIS y_tilesize > BX_GUI_THIS host_yres) {
    *h = BX_GUI_THIS host_yres - y0;
  } else {
    *h = BX_GUI_THIS y_tilesize;
  }

  return (Bit8u *)framebuffer + y0 * BX_GUI_THIS host_pitch +
                  x0 * ((BX_GUI_THIS host_bpp + 1) >> 3);
}

void bx_gui_c::graphics_tile_update_in_place(unsigned x0, unsigned y0,
                                        unsigned w, unsigned h)
{
  Bit8u *tile;
  Bit8u *tile_ptr, *fb_ptr;
  Bit16u xc, yc, fb_pitch, tile_pitch;
  Bit8u r, diffx, diffy;

  tile = new Bit8u[BX_GUI_THIS x_tilesize * BX_GUI_THIS y_tilesize * 4];
  diffx = (x0 % BX_GUI_THIS x_tilesize);
  diffy = (y0 % BX_GUI_THIS y_tilesize);
  if (diffx > 0) {
    x0 -= diffx;
    w += diffx;
  }
  if (diffy > 0) {
    y0 -= diffy;
    h += diffy;
  }
  fb_pitch = BX_GUI_THIS host_pitch;
  tile_pitch = BX_GUI_THIS x_tilesize * ((BX_GUI_THIS host_bpp + 1) >> 3);
  for (yc=y0; yc<(y0+h); yc+=BX_GUI_THIS y_tilesize) {
    for (xc=x0; xc<(x0+w); xc+=BX_GUI_THIS x_tilesize) {
      fb_ptr = BX_GUI_THIS framebuffer + (yc * fb_pitch + xc * ((BX_GUI_THIS host_bpp + 1) >> 3));
      tile_ptr = &tile[0];
      for (r=0; r<h; r++) {
        memcpy(tile_ptr, fb_ptr, tile_pitch);
        fb_ptr += fb_pitch;
        tile_ptr += tile_pitch;
      }
      BX_GUI_THIS graphics_tile_update(tile, xc, yc);
    }
  }
  delete [] tile;
}

void bx_gui_c::draw_char_common(Bit8u ch, Bit8u fc, Bit8u bc, Bit16u xc,
                                Bit16u yc, Bit8u fw, Bit8u fh, Bit8u fx,
                                Bit8u fy, bool gfxcharw9, Bit8u cs, Bit8u ce,
                                bool curs, bool font2)
{
  Bit8u *buf, *font_ptr, fontpixels;
  Bit16u font_row, mask;
  bool dwidth;

  buf = BX_GUI_THIS snapshot_buffer + yc * BX_GUI_THIS guest_xres + xc;
  dwidth = (BX_GUI_THIS guest_fwidth > 9);
  if (font2) {
    font_ptr = &vga_charmap[1][(ch << 5) + fy];
  } else {
    font_ptr = &vga_charmap[0][(ch << 5) + fy];
  }
  do {
    font_row = *font_ptr++;
    if (gfxcharw9) {
      font_row = (font_row << 1) | (font_row & 0x01);
    } else {
      font_row <<= 1;
    }
    if (fx > 0) {
      font_row <<= fx;
    }
    fontpixels = fw;
    if (curs && (fy >= cs) && (fy <= ce))
      mask = 0x100;
    else
      mask = 0x00;
    do {
      if ((font_row & 0x100) == mask)
        *buf = bc;
      else
        *buf = fc;
      buf++;
      if (!dwidth || (fontpixels & 1)) font_row <<= 1;
    } while (--fontpixels);
    buf += (BX_GUI_THIS guest_xres - fw);
    fy++;
  } while (--fh);
}

void bx_gui_c::text_update_common(Bit8u *old_text, Bit8u *new_text,
                                  Bit16u cursor_address,
                                  bx_vga_tminfo_t *tm_info)
{
  Bit16u curs, cursor_x, cursor_y, xc, yc, rows, hchars, text_cols;
  Bit16u offset, loffset;
  Bit8u cfheight, cfwidth, cfrow, cfcol, fgcolor, bgcolor;
  Bit8u split_textrow, split_fontrows, x, y;
  Bit8u *new_line, *old_line, *text_base;
  bool cursor_visible, gfxcharw9, split_screen, font2;
  bool forceUpdate = 0, blink_mode = 0, blink_state = 0;

  if (BX_GUI_THIS snapshot_mode || BX_GUI_THIS new_text_api) {
    cursor_visible = ((tm_info->cs_start <= tm_info->cs_end) &&
                      (tm_info->cs_start < BX_GUI_THIS guest_fheight));
    if (BX_GUI_THIS snapshot_mode && (BX_GUI_THIS snapshot_buffer != NULL)) {
      forceUpdate = 1;
    } else if (BX_GUI_THIS new_text_api) {
      blink_mode = (tm_info->blink_flags & BX_TEXT_BLINK_MODE) > 0;
      blink_state = (tm_info->blink_flags & BX_TEXT_BLINK_STATE) > 0;
      if (blink_mode) {
        if (tm_info->blink_flags & BX_TEXT_BLINK_TOGGLE)
          forceUpdate = 1;
      }
      if (!blink_state) cursor_visible = 0;
      if (BX_GUI_THIS charmap_updated) {
        BX_GUI_THIS set_font(tm_info->line_graphics);
        BX_GUI_THIS charmap_updated = 0;
        forceUpdate = 1;
      }
      if ((tm_info->h_panning != BX_GUI_THIS tm_info.h_panning) ||
          (tm_info->v_panning != BX_GUI_THIS tm_info.v_panning) ||
          (tm_info->line_compare != BX_GUI_THIS tm_info.line_compare)) {
        BX_GUI_THIS tm_info.h_panning = tm_info->h_panning;
        BX_GUI_THIS tm_info.v_panning = tm_info->v_panning;
        BX_GUI_THIS tm_info.line_compare = tm_info->line_compare;
        forceUpdate = 1;
      }
      // invalidate character at previous and new cursor location
      if (cursor_address != BX_GUI_THIS cursor_address) {
        old_text[BX_GUI_THIS cursor_address] = ~new_text[BX_GUI_THIS cursor_address];
        BX_GUI_THIS cursor_address = cursor_address;
      }
      if (cursor_address < 0x7fff) {
        old_text[cursor_address] = ~new_text[cursor_address];
      }
    }
    rows = BX_GUI_THIS guest_yres / BX_GUI_THIS guest_fheight;
    if (tm_info->v_panning > 0) rows++;
    text_cols = BX_GUI_THIS guest_xres / BX_GUI_THIS guest_fwidth;
    if (cursor_visible) {
      curs = cursor_address;
    } else {
      curs = 0xffff;
    }
    if (tm_info->line_compare < 0x3ff) {
      split_textrow = (tm_info->line_compare + tm_info->v_panning) / BX_GUI_THIS guest_fheight;
      split_fontrows = ((tm_info->line_compare + tm_info->v_panning) % BX_GUI_THIS guest_fheight) + 1;
    } else {
      split_textrow = 0xff;
      split_fontrows = 0;
    }
    y = 0;
    yc = 0;
    split_screen = 0;
    loffset = tm_info->start_address;
    text_base = new_text;
    new_text += tm_info->start_address;
    old_text += tm_info->start_address;
    do {
      hchars = text_cols;
      if (tm_info->h_panning > 0) hchars++;
      cfheight = BX_GUI_THIS guest_fheight;
      cfrow = 0;
      if (split_screen) {
        if (rows == 1) {
          cfheight = (guest_yres - tm_info->line_compare - 1) % BX_GUI_THIS guest_fheight;
          if (cfheight == 0) cfheight = BX_GUI_THIS guest_fheight;
        }
      } else if (tm_info->v_panning > 0) {
        if (y == 0) {
          cfheight -= tm_info->v_panning;
          cfrow = tm_info->v_panning;
        } else if (rows == 1) {
          cfheight = tm_info->v_panning;
        }
      }
      if (y == split_textrow) {
        cfheight = split_fontrows - cfrow;
      }
      new_line = new_text;
      old_line = old_text;
      offset = loffset;
      x = 0;
      xc = 0;
      do {
        cfwidth = BX_GUI_THIS guest_fwidth;
        cfcol = 0;
        if (tm_info->h_panning > 0) {
          if (x == 0) {
            cfcol = tm_info->h_panning;
            cfwidth -= tm_info->h_panning;
          } else if (hchars == 1) {
            cfwidth = tm_info->h_panning;
          }
        }
        // check if char needs to be updated
        if (forceUpdate || (new_text[0] != old_text[0]) ||
            (new_text[1] != old_text[1])) {
          fgcolor = tm_info->actl_palette[new_text[1] & 0x0f];
          if (blink_mode) {
            bgcolor = tm_info->actl_palette[(new_text[1] >> 4) & 0x07];
            if (!blink_state && (new_text[1] & 0x80))
              fgcolor = bgcolor;
          } else {
            bgcolor = tm_info->actl_palette[(new_text[1] >> 4) & 0x0F];
          }
          font2 = (new_text[1] & 0x08) > 0;
          gfxcharw9 = ((tm_info->line_graphics) && ((new_text[0] & 0xE0) == 0xC0));
          if (BX_GUI_THIS snapshot_mode) {
            BX_GUI_THIS draw_char_common(new_text[0], fgcolor, bgcolor, xc, yc,
                                         cfwidth, cfheight, cfcol, cfrow,
                                         gfxcharw9, tm_info->cs_start,
                                         tm_info->cs_end, (offset == curs), font2);
          } else {
            BX_GUI_THIS draw_char(new_text[0], fgcolor, bgcolor, xc, yc,
                                  cfwidth, cfheight, cfcol, cfrow,
                                  gfxcharw9, tm_info->cs_start,
                                  tm_info->cs_end, (offset == curs), font2);
          }
        }
        new_text += 2;
        old_text += 2;
        offset += 2;
        x++;
        xc += cfwidth;
      } while (--hchars);
      if (y == split_textrow) {
        new_text = text_base;
        forceUpdate = 1;
        loffset = 0;
        rows = ((guest_yres - tm_info->line_compare + BX_GUI_THIS guest_fheight - 2) / BX_GUI_THIS guest_fheight) + 1;
        if (tm_info->split_hpanning) tm_info->h_panning = 0;
        split_screen = 1;
      } else {
        new_text = new_line + tm_info->line_offset;
        old_text = old_line + tm_info->line_offset;
        loffset += tm_info->line_offset;
      }
      y++;
      yc += cfheight;
    } while (--rows);
  } else {
    // workarounds for existing text_update() API
    if (cursor_address >= tm_info->start_address) {
      cursor_x = ((cursor_address - tm_info->start_address) % tm_info->line_offset) / 2;
      cursor_y = ((cursor_address - tm_info->start_address) / tm_info->line_offset);
    } else {
      cursor_x = 0xffff;
      cursor_y = 0xffff;
    }
    if ((tm_info->blink_flags & BX_TEXT_BLINK_STATE) == 0) {
      tm_info->cs_start |= 0x20;
    }
    new_text += tm_info->start_address;
    old_text += tm_info->start_address;
    text_update(old_text, new_text, cursor_x, cursor_y, tm_info);
  }
}

void bx_gui_c::graphics_tile_update_common(Bit8u *tile, unsigned x, unsigned y)
{
  unsigned i, pitch, pixel_bytes, nbytes, tilebytes;
  Bit8u *src, *dst;

  if (BX_GUI_THIS snapshot_mode) {
    if (BX_GUI_THIS snapshot_buffer != NULL) {
      pixel_bytes = ((BX_GUI_THIS guest_bpp + 1) >> 3);
      tilebytes = BX_GUI_THIS x_tilesize * pixel_bytes;
      if ((x + BX_GUI_THIS x_tilesize) <= BX_GUI_THIS guest_xres) {
        nbytes = tilebytes;
      } else {
        nbytes = (BX_GUI_THIS guest_xres - x) * pixel_bytes;
      }
      pitch = BX_GUI_THIS guest_xres * pixel_bytes;
      src = tile;
      dst = BX_GUI_THIS snapshot_buffer + (y * pitch) + x;
      for (i = 0; i < y_tilesize; i++) {
        memcpy(dst, src, nbytes);
        src += tilebytes;
        dst += pitch;
        if (++y >= BX_GUI_THIS guest_yres) break;
      }
    }
  } else {
    graphics_tile_update(tile, x, y);
  }
}

bx_svga_tileinfo_t * bx_gui_c::graphics_tile_info_common(bx_svga_tileinfo_t *info)
{
  if (!info) {
    info = new bx_svga_tileinfo_t;
    if (!info) {
      return NULL;
    }
  }
  info->snapshot_mode = BX_GUI_THIS snapshot_mode;
  if (BX_GUI_THIS snapshot_mode) {
    info->pitch = BX_GUI_THIS guest_xres * ((BX_GUI_THIS guest_bpp + 1) >> 3);
  } else {
    return graphics_tile_info(info);
  }

  return info;
}

#if BX_USE_GUI_CONSOLE

#define BX_CONSOLE_BUFSIZE 4000

void bx_gui_c::console_init(void)
{
  int i;

  console.screen = new Bit8u[BX_CONSOLE_BUFSIZE];
  console.oldscreen = new Bit8u[BX_CONSOLE_BUFSIZE];
  for (i = 0; i < BX_CONSOLE_BUFSIZE; i+=2) {
    console.screen[i] = 0x20;
    console.screen[i+1] = 0x07;
  }
  memset(console.oldscreen, 0xff, BX_CONSOLE_BUFSIZE);
  console.saved_xres = guest_xres;
  console.saved_yres = guest_yres;
  console.saved_bpp = guest_bpp;
  console.saved_fwidth = guest_fwidth;
  console.saved_fheight = guest_fheight;
  memcpy(console.saved_charmap, BX_GUI_THIS vga_charmap[0], 0x2000);
  for (i = 0; i < 256; i++) {
    memcpy(&BX_GUI_THIS vga_charmap[0][0]+i*32, &sdl_font8x16[i], 16);
    BX_GUI_THIS char_changed[0][i] = 1;
  }
  BX_GUI_THIS charmap_updated = 1;
  console.cursor_x = 0;
  console.cursor_y = 0;
  console.cursor_addr = 0;
  memset(&console.tminfo, 0, sizeof(bx_vga_tminfo_t));
  console.tminfo.line_offset = 160;
  console.tminfo.line_compare = 1023;
  console.tminfo.cs_start = 0x2e;
  console.tminfo.cs_end = 0x0f;
  console.tminfo.actl_palette[7] = 0x07;
  memcpy(console.saved_palette, palette, 32);
  palette_change_common(0x00, 0x00, 0x00, 0x00);
  palette_change_common(0x07, 0xa8, 0xa8, 0xa8);
  dimension_update(720, 400, 16, 9, 8);
  console.n_keys = 0;
  console.running = 1;
}

void bx_gui_c::console_cleanup(void)
{
  delete [] console.screen;
  delete [] console.oldscreen;
  palette_change_common(0x00, console.saved_palette[2], console.saved_palette[1],
                        console.saved_palette[0]);
  palette_change_common(0x07, console.saved_palette[30], console.saved_palette[29],
                        console.saved_palette[28]);
  unsigned fheight = (console.saved_fheight);
  unsigned fwidth = (console.saved_fwidth);
  set_text_charmap(0, console.saved_charmap);
  dimension_update(console.saved_xres, console.saved_yres, fheight, fwidth,
                   console.saved_bpp);
  DEV_vga_refresh(1);
  console.running = 0;
}

void bx_gui_c::console_refresh(bool force)
{
  if (force) memset(console.oldscreen, 0xff, BX_CONSOLE_BUFSIZE);
  if (BX_GUI_THIS new_text_api) {
    text_update_common(console.oldscreen, console.screen, console.cursor_addr,
                       &console.tminfo);
  } else {
    text_update(console.oldscreen, console.screen, console.cursor_x,
                console.cursor_y, &console.tminfo);
  }
  flush();
  memcpy(console.oldscreen, console.screen, BX_CONSOLE_BUFSIZE);
}

void bx_gui_c::console_key_enq(Bit8u key)
{
  if (console.n_keys < 16) {
    console.keys[console.n_keys++] = key;
  }
}

int bx_gui_c::bx_printf(const char *s)
{
  unsigned offset;

  if (!console.running) {
    console_init();
  }
  for (unsigned i = 0; i < strlen(s); i++) {
    offset = console.cursor_y * 160 + console.cursor_x * 2;
    if ((s[i] != 0x08) && (s[i] != 0x0a)) {
      console.screen[offset] = s[i];
      console.screen[offset+1] = 0x07;
      console.cursor_x++;
    }
    if ((s[i] == 0x0a) || (console.cursor_x == 80)) {
      console.cursor_x = 0;
      console.cursor_y++;
    }
    if (s[i] == 0x08) {
      if (offset > 0) {
        console.screen[offset-2] = 0x20;
        console.screen[offset-1] = 0x07;
        if (console.cursor_x > 0) {
          console.cursor_x--;
        } else {
          console.cursor_x = 79;
          console.cursor_y--;
        }
      }
    }
    if (console.cursor_y == 25) {
      memmove(console.screen, console.screen+160, BX_CONSOLE_BUFSIZE-160);
      console.cursor_y--;
      offset = console.cursor_y * 160 + console.cursor_x * 2;
      for (int j = 0; j < 160; j+=2) {
        console.screen[offset+j] = 0x20;
        console.screen[offset+j+1] = 0x07;
      }
    }
  }
  console.cursor_addr = console.cursor_y * 160 + console.cursor_x * 2;
  console_refresh(0);
  return (int)strlen(s);
}

char* bx_gui_c::bx_gets(char *s, int size)
{
  char keystr[2];
  int pos = 0, done = 0;
  int cs_counter = 1, cs_visible = 0;

  set_console_edit_mode(1);
  console.tminfo.blink_flags |= BX_TEXT_BLINK_MODE;
  keystr[1] = 0;
  do {
    handle_events();
    while (console.n_keys > 0) {
      if ((console.keys[0] >= 0x20) && (pos < (size-1))) {
        s[pos++] = console.keys[0];
        keystr[0] = console.keys[0];
        bx_printf(keystr);
      } else if (console.keys[0] == 0x0d) {
        s[pos] = 0x00;
        keystr[0] = 0x0a;
        bx_printf(keystr);
        done = 1;
      } else if ((console.keys[0] == 0x08) && (pos > 0)) {
        pos--;
        keystr[0] = 0x08;
        bx_printf(keystr);
      }
      memmove(&console.keys[0], &console.keys[1], 15);
      console.n_keys--;
    }
#if BX_HAVE_USLEEP
    usleep(25000);
#else
    msleep(25);
#endif
    if (--cs_counter == 0) {
      cs_counter = 10;
      cs_visible ^= 1;
      if (cs_visible) {
        console.tminfo.cs_start &= ~0x20;
        console.tminfo.blink_flags |= BX_TEXT_BLINK_STATE;
      } else {
        console.tminfo.cs_start |= 0x20;
        console.tminfo.blink_flags &= ~BX_TEXT_BLINK_STATE;
      }
      console.tminfo.blink_flags |= BX_TEXT_BLINK_TOGGLE;
      console_refresh(0);
    } else {
      console.tminfo.blink_flags &= ~BX_TEXT_BLINK_TOGGLE;
    }
  } while (!done);
  console.tminfo.cs_start |= 0x20;
  console.tminfo.blink_flags &= ~BX_TEXT_BLINK_MODE;
  set_console_edit_mode(0);
  return s;
}
#endif

void bx_gui_c::set_command_mode(bool active)
{
  if (command_mode.present) {
    command_mode.active = active;
  }
}

bool bx_gui_c::parse_common_gui_options(const char *arg, Bit8u flags)
{
  if (!strcmp(arg, "nokeyrepeat") && (flags & BX_GUI_OPT_NOKEYREPEAT)) {
    BX_INFO(("disabled host keyboard repeat"));
    gui_opts.nokeyrepeat = 1;
    return true;
  } else if (!strncmp(arg, "gui_debug", 9)) {
#if BX_DEBUGGER && BX_DEBUGGER_GUI
    gui_opts.enh_dbg_enabled = 1;
    gui_opts.enh_dbg_global_ini = 0;
    if ((strlen(arg) > 9) && (arg[9] == ':')) {
      if (!strcmp(&arg[10], "globalini")) {
        gui_opts.enh_dbg_global_ini = 1;
        BX_INFO(("Debugger gui using global config from BXSHARE path"));
      } else {
        BX_ERROR(("Ignoring unknown setting '%s' for gui debugger", &arg[10]));
      }
    } else if (strlen(arg) > 9) {
      return false;
    }
#else
    SIM->message_box("ERROR", "Bochs debugger not available - ignoring 'gui_debug' option");
#endif
    return true;
#if BX_SHOW_IPS
  } else if (!strcmp(arg, "hideIPS") && (flags & BX_GUI_OPT_HIDE_IPS)) {
    BX_INFO(("hide IPS display in status bar"));
    gui_opts.hide_ips = 1;
    return true;
#endif
  } else if (!strcmp(arg, "cmdmode") && (flags & BX_GUI_OPT_CMDMODE)) {
    BX_INFO(("enabled command mode support"));
    command_mode.present = 1;
    return true;
#if BX_USE_GUI_CONSOLE
  } else if (!strcmp(arg, "no_gui_console") && (flags & BX_GUI_OPT_NO_GUI_CONSOLE)) {
    BX_INFO(("use system console instead of gui console"));
    console.present = 0;
    return true;
#endif
  }
  return false;
}