all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob e0800b2245e6eb1ea2c5e6a5dbc48d20e1a49593 36051 bytes (raw)
name: src/haiku_support.cc 	 # note: path name is non-authoritative(*)

   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
 
/* Haiku window system support.  Hey, Emacs, this is -*- C++ -*-
   Copyright (C) 2021 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include <Application.h>
#include <AppFileInfo.h>
#include <GraphicsDefs.h>
#include <InterfaceDefs.h>
#include <Bitmap.h>
#include <Window.h>
#include <DirectWindow.h>
#include <View.h>
#include <Screen.h>
#include <Cursor.h>
#include <UnicodeChar.h>
#include <ScrollBar.h>
#include <Region.h>
#include <Menu.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <MenuBar.h>
#include <Beep.h>
#include <Alert.h>
#include <Button.h>
#include <Path.h>
#include <PathFinder.h>
#include <WindowScreen.h>
#include <Messenger.h>
#include <fs_attr.h>

#include <cmath>
#include <cstring>
#include <pthread.h>
#include <stdint.h>

#include "haiku_support.h"

#define SCROLL_BAR_UPDATE 3000

static color_space dpy_color_space = B_NO_COLOR_SPACE;
static key_map *key_map = NULL;
static char *key_chars = NULL;

extern "C"
{
  extern void emacs_abort (void);
  pthread_t app_thread_id;
}

static void
map_key (char *chars, int32 offset, uint32_t *c)
{
  int size = chars[offset++];
  switch (size)
    {
    case 0:
      break;

    case 1:
      *c = chars[offset];
      break;

    default:
      {
        char str[5];
        int i = (size <= 4) ? size : 4;
        strncpy (str, &(chars[offset]), i);
        str[i] = '0';
	*c = BUnicodeChar::FromUTF8 ((char *) &str);
	break;
      }
    }
}

static void
map_shift (uint32_t kc, uint32_t *ch)
{
  if (!key_map)
    get_key_map (&key_map, &key_chars);
  if (!key_map)
    return;
  if (kc >= 128)
    return;

  int32_t m = key_map->shift_map[kc];
  map_key (key_chars, m, ch);
}

class Emacs : public BApplication
{
public:
  Emacs () : BApplication ("application/x-vnd.GNU-emacs")
  {
  }
};

class EmacsWindow : public BDirectWindow
{
public:
  struct child_frame
  {
    struct child_frame *next;
    int xoff, yoff;
    EmacsWindow *window;
  } *subset_windows = NULL;

  EmacsWindow *parent = NULL;

  EmacsWindow () : BDirectWindow (BRect (0, 0, 0, 0), "", B_TITLED_WINDOW_LOOK,
				  B_NORMAL_WINDOW_FEEL, B_NO_SERVER_SIDE_WINDOW_MODIFIERS)
  {

  }

  ~EmacsWindow ()
  {
    struct child_frame *next;
    for (struct child_frame *f = subset_windows; f; f = next)
      {
	f->window->Unparent ();
	next = f->next;
	delete f;
      }

    if (this->parent)
      UnparentAndUnlink ();
  }

  void
  UpwardsSubset (EmacsWindow *w)
  {
    for (; w; w = w->parent)
      AddToSubset (w);
  }

  void
  UpwardsSubsetChildren (EmacsWindow *w)
  {
    UpwardsSubset (w);
    for (struct child_frame *f = subset_windows; f;
	 f = f->next)
      f->window->UpwardsSubsetChildren (w);
  }

  void
  UpwardsUnSubset (EmacsWindow *w)
  {
    for (; w; w = w->parent)
      RemoveFromSubset (w);
  }

  void
  UpwardsUnSubsetChildren (EmacsWindow *w)
  {
    UpwardsUnSubset (w);
    for (struct child_frame *f = subset_windows; f;
	 f = f->next)
      f->window->UpwardsUnSubsetChildren (w);
  }

  void
  Unparent (void)
  {
    this->SetFeel (B_NORMAL_WINDOW_FEEL);
    UpwardsUnSubsetChildren (parent);
    this->RemoveFromSubset (this);
    this->parent = NULL;
  }

  void
  UnparentAndUnlink (void)
  {
    this->parent->UnlinkChild (this);
    this->Unparent ();
  }

  void
  UnlinkChild (EmacsWindow *window)
  {
    struct child_frame *last = NULL;
    struct child_frame *tem = subset_windows;

    for (; tem; last = tem, tem = tem->next)
      {
	if (tem->window == window)
	  {
	    if (last)
	      last->next = tem->next;
	    if (tem == subset_windows)
	      subset_windows = NULL;
	    delete tem;
	    return;
	  }
      }

    emacs_abort ();
  }

  void
  ParentTo (EmacsWindow *window)
  {
    if (this->parent)
      UnparentAndUnlink ();

    this->parent = window;
    this->SetFeel (B_FLOATING_SUBSET_WINDOW_FEEL);
    this->AddToSubset (this);
    if (!IsHidden () && this->parent)
      UpwardsSubsetChildren (parent);
    this->Sync ();
    window->LinkChild (this);
  }

  void
  LinkChild (EmacsWindow *window)
  {
    struct child_frame *f = new struct child_frame;

    for (struct child_frame *f = subset_windows; f;
	 f = f->next)
      {
	if (window == f->window)
	  emacs_abort ();
      }

    f->window = window;
    f->next = subset_windows;
    f->xoff = -1;
    f->yoff = -1;

    subset_windows = f;
  }

  void
  DoMove (struct child_frame *f)
  {
    BRect frame = this->Frame ();
    f->window->MoveTo (frame.left + f->xoff,
		       frame.top + f->yoff);
    this->Sync ();
  }

  void
  DoUpdateWorkspace (struct child_frame *f)
  {
    f->window->SetWorkspaces (this->Workspaces ());
  }

  void
  MoveChild (EmacsWindow *window, int xoff, int yoff,
	     int weak_p)
  {
    for (struct child_frame *f = subset_windows; f;
	 f = f->next)
      {
	if (window == f->window)
	  {
	    f->xoff = xoff;
	    f->yoff = yoff;
	    if (!weak_p)
	      DoMove (f);
	    return;
	  }
      }

    emacs_abort ();
  }

  void
  WindowActivated (bool activated)
  {
    struct haiku_activation_event rq;
    rq.window = this;
    rq.activated_p = activated;

    haiku_write (ACTIVATION, &rq);
  }

  void
  MessageReceived (BMessage *msg)
  {
    if (msg->GetPointer ("menuptr"))
      {
	struct haiku_menu_bar_select_event rq;
	rq.window = this;
	rq.ptr = (void *) msg->GetPointer ("menuptr");
	haiku_write (MENU_BAR_SELECT_EVENT, &rq);
      }
    BDirectWindow::MessageReceived (msg);
  }

  void
  DispatchMessage (BMessage *msg, BHandler *handler)
  {
    if (msg->what == B_KEY_DOWN || msg->what == B_KEY_UP)
      {
	struct haiku_key_event rq;
	rq.window = this;

	int32_t code = msg->GetInt32 ("raw_char", 0);

	rq.modifiers = 0;
	uint32_t mods = modifiers ();

	if (mods & B_SHIFT_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_SHIFT;

	if (mods & B_CONTROL_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_CTRL;

	if (mods & B_COMMAND_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_ALT;

	rq.mb_char = code;
	rq.kc = msg->GetInt32 ("key", -1);
	rq.unraw_mb_char =
	  BUnicodeChar::FromUTF8 (msg->GetString ("bytes"));

	if ((mods & B_SHIFT_KEY) && rq.kc >= 0)
	  map_shift (rq.kc, &rq.unraw_mb_char);

	haiku_write (msg->what == B_KEY_DOWN ? KEY_DOWN : KEY_UP, &rq);
      }
    else if (msg->what == B_MOUSE_WHEEL_CHANGED)
      {
	struct haiku_wheel_move_event rq;
	rq.window = this;
	rq.modifiers = 0;

	uint32_t mods = modifiers ();

	if (mods & B_SHIFT_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_SHIFT;

	if (mods & B_CONTROL_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_CTRL;

	if (mods & B_COMMAND_KEY)
	  rq.modifiers |= HAIKU_MODIFIER_ALT;

	float dx, dy;
	if (msg->FindFloat ("be:wheel_delta_x", &dx) == B_OK &&
	    msg->FindFloat ("be:wheel_delta_y", &dy) == B_OK)
	  {
	    rq.delta_x = dx;
	    rq.delta_y = dy;

	    haiku_write (WHEEL_MOVE_EVENT, &rq);
	  };
      }
    else
      BDirectWindow::DispatchMessage (msg, handler);
  }

  void
  MenusBeginning ()
  {
    struct haiku_menu_bar_state_event rq;
    rq.window = this;

    haiku_write (MENU_BAR_OPEN, &rq);
  }

  void
  MenusEnded ()
  {
    struct haiku_menu_bar_state_event rq;
    rq.window = this;

    haiku_write (MENU_BAR_CLOSE, &rq);
  }

  void
  FrameResized (float newWidth, float newHeight)
  {
    struct haiku_resize_event rq;
    rq.window = this;
    rq.px_heightf = newHeight;
    rq.px_widthf = newWidth;

    haiku_write (FRAME_RESIZED, &rq);
    BDirectWindow::FrameResized (newWidth, newHeight);
  }

  void
  FrameMoved (BPoint newPosition)
  {
    struct haiku_move_event rq;
    rq.window = this;
    rq.x = std::lrint (newPosition.x);
    rq.y = std::lrint (newPosition.y);

    haiku_write (MOVE_EVENT, &rq);

    for (struct child_frame *f = subset_windows;
	 f; f = f->next)
      DoMove (f);
    BDirectWindow::FrameMoved (newPosition);
  }

  void
  WorkspacesChanged (uint32_t old, uint32_t n)
  {
    for (struct child_frame *f = subset_windows;
	 f; f = f->next)
      DoUpdateWorkspace (f);
  }

  void
  EmacsMoveTo (int x, int y)
  {
    if (!this->parent)
      this->MoveTo (x, y);
    else
      this->parent->MoveChild (this, x, y, 0);
  }

  bool
  QuitRequested ()
  {
    struct haiku_quit_requested_event rq;
    rq.window = this;
    haiku_write (QUIT_REQUESTED, &rq);
    return false;
  }

  void
  Minimize (bool minimized_p)
  {
    BDirectWindow::Minimize (minimized_p);
    struct haiku_iconification_event rq;
    rq.window = this;
    rq.iconified_p = !parent && minimized_p;

    haiku_write (ICONIFICATION, &rq);
  }

  void
  EmacsHide (void)
  {
    if (this->IsHidden ())
      return;
    Hide ();
    if (this->parent)
      UpwardsUnSubsetChildren (this->parent);
  }

  void
  EmacsShow (void)
  {
    if (!this->IsHidden ())
      return;
    Show ();
    if (this->parent)
      UpwardsSubsetChildren (this->parent);
  }
};

class EmacsMenuBar : BMenuBar
{
public:
  EmacsMenuBar () : BMenuBar (BRect (0, 0, 0, 0), NULL)
  {
  }

  void
  FrameResized (float newWidth, float newHeight)
  {
    struct haiku_menu_bar_resize_event rq;
    rq.window = this->Window ();
    rq.height = std::lrint (newHeight);
    rq.width = std::lrint (newWidth);

    haiku_write (MENU_BAR_RESIZE, &rq);
    BMenuBar::FrameResized (newWidth, newHeight);
  }
};

class EmacsView : public BView
{
public:
  uint32_t previous_buttons = 0;
  int looper_locked_count = 0;
  BRegion sb_region;

  BView *offscreen_draw_view = NULL;
  BBitmap *offscreen_draw_bitmap_1 = NULL;
  BBitmap *offscreen_blit_bitmap_1 = NULL;

  BScreen *screen = NULL;
  color_space cspace;

  EmacsView () : BView (BRect (0, 0, 0, 0), "Emacs Content",
			B_FOLLOW_NONE, B_WILL_DRAW | B_INPUT_METHOD_AWARE)
  {

  }

  ~EmacsView ()
  {
    TearDownDoubleBuffering ();
    delete screen;
  }

  void
  AttachedToWindow (void)
  {
    screen = new BScreen (Window ());
    cspace = screen->ColorSpace ();
  }

  void
  TearDownDoubleBuffering (void)
  {
    if (offscreen_draw_view)
      {
	if (!offscreen_draw_view->LockLooper ())
	  emacs_abort ();
	offscreen_draw_view->RemoveSelf ();
	delete offscreen_draw_view;
	offscreen_draw_view = NULL;
	delete offscreen_draw_bitmap_1;
	offscreen_draw_bitmap_1 = NULL;
	delete offscreen_blit_bitmap_1;
	offscreen_blit_bitmap_1 = NULL;
      }
   }

  void
  AfterResize (float newWidth, float newHeight)
  {
    if (offscreen_draw_view)
      {
	if (!offscreen_draw_view->LockLooper ())
	  emacs_abort ();
	offscreen_draw_view->RemoveSelf ();
	delete offscreen_blit_bitmap_1;
	offscreen_blit_bitmap_1 = new BBitmap (Frame (), cspace, 1);
	delete offscreen_draw_bitmap_1;
	offscreen_draw_bitmap_1 = new BBitmap (Frame (), cspace, 1);
	offscreen_draw_view->MoveTo (Frame ().left, Frame ().top);
	offscreen_draw_view->ResizeTo (Frame ().Width (), Frame ().Height ());
	offscreen_draw_bitmap_1->AddChild (offscreen_draw_view);

	if (looper_locked_count)
	  offscreen_draw_view->LockLooper ();
      }
  }

  void
  Draw (BRect expose_bounds)
  {
    struct haiku_expose_event rq;
    if (!offscreen_draw_view)
      {
	if (sb_region.Contains (std::lrint (expose_bounds.left),
				std::lrint (expose_bounds.top)) &&
	    sb_region.Contains (std::lrint (expose_bounds.right),
				std::lrint (expose_bounds.top)) &&
	    sb_region.Contains (std::lrint (expose_bounds.left),
				std::lrint (expose_bounds.bottom)) &&
	    sb_region.Contains (std::lrint (expose_bounds.right),
				std::lrint (expose_bounds.bottom)))
	  return;

	rq.x = std::floor (expose_bounds.left);
	rq.y = std::floor (expose_bounds.top);
	rq.width = std::ceil (expose_bounds.right - expose_bounds.left + 1);
	rq.height = std::ceil (expose_bounds.bottom - expose_bounds.top + 1);
	if (!rq.width)
	  rq.width = 1;
	if (!rq.height)
	  rq.height = 1;
	rq.window = this->Window ();

	haiku_write (FRAME_EXPOSED, &rq);
      }
    else
      {
	DrawBitmap (offscreen_blit_bitmap_1, expose_bounds, expose_bounds);
      }
  }

  void
  FlipBuffers (void)
  {
    if (!LockLooper ())
      emacs_abort ();
    if (!offscreen_draw_view)
      emacs_abort ();

    offscreen_draw_view->RemoveSelf ();
    BBitmap *b = offscreen_blit_bitmap_1;
    offscreen_blit_bitmap_1 = offscreen_draw_bitmap_1;
    offscreen_draw_bitmap_1 = b;
    b->ImportBits (offscreen_blit_bitmap_1);
    offscreen_draw_bitmap_1->AddChild (offscreen_draw_view);

    if (looper_locked_count)
      {
	if (!offscreen_draw_view->LockLooper ())
	  emacs_abort ();
      }

    Invalidate ();
    UnlockLooper ();
    return;
  }

  void
  SetUpDoubleBuffering (void)
  {
    if (!LockLooper ())
      emacs_abort ();
    if (offscreen_draw_view)
      emacs_abort ();

    offscreen_draw_view = new BView (Frame (), NULL, B_FOLLOW_NONE, B_WILL_DRAW);
    offscreen_blit_bitmap_1 = new BBitmap (Frame (), cspace, 1);
    offscreen_draw_bitmap_1 = new BBitmap (Frame (), cspace, 1);
    offscreen_draw_bitmap_1->AddChild (offscreen_draw_view);

    if (looper_locked_count)
      offscreen_draw_view->LockLooper ();

    UnlockLooper ();
    Invalidate ();
  }

  void
  MouseMoved (BPoint point, uint32 transit, const BMessage *msg)
  {
    struct haiku_mouse_motion_event rq;

    rq.just_exited_p = transit == B_EXITED_VIEW;
    rq.x = point.x;
    rq.y = point.y;
    rq.be_code = transit;
    rq.window = this->Window ();

    haiku_write (MOUSE_MOTION, &rq);
  }

  void
  MouseDown (BPoint point)
  {
    struct haiku_button_event rq;
    uint32_t buttons;

    this->GetMouse (&point, &buttons, false);

    rq.window = this->Window ();
    rq.btn_no = 0;

    if (!(previous_buttons & B_PRIMARY_MOUSE_BUTTON) &&
	(buttons & B_PRIMARY_MOUSE_BUTTON))
      rq.btn_no = 0;
    else if (!(previous_buttons & B_SECONDARY_MOUSE_BUTTON) &&
	     (buttons & B_SECONDARY_MOUSE_BUTTON))
      rq.btn_no = 2;
    else if (!(previous_buttons & B_TERTIARY_MOUSE_BUTTON) &&
	     (buttons & B_TERTIARY_MOUSE_BUTTON))
      rq.btn_no = 1;
    previous_buttons = buttons;

    rq.x = point.x;
    rq.y = point.y;

    uint32_t mods = modifiers ();

    rq.modifiers = 0;
    if (mods & B_SHIFT_KEY)
      rq.modifiers |= HAIKU_MODIFIER_SHIFT;

    if (mods & B_CONTROL_KEY)
      rq.modifiers |= HAIKU_MODIFIER_CTRL;

    if (mods & B_COMMAND_KEY)
      rq.modifiers |= HAIKU_MODIFIER_ALT;

    haiku_write (BUTTON_DOWN, &rq);
  }

  void
  MouseUp (BPoint point)
  {
    struct haiku_button_event rq;
    uint32_t buttons;

    this->GetMouse (&point, &buttons, false);

    rq.window = this->Window ();
    rq.btn_no = 0;

    if ((previous_buttons & B_PRIMARY_MOUSE_BUTTON) &&
	!(buttons & B_PRIMARY_MOUSE_BUTTON))
      rq.btn_no = 0;
    else if ((previous_buttons & B_SECONDARY_MOUSE_BUTTON) &&
	     !(buttons & B_SECONDARY_MOUSE_BUTTON))
      rq.btn_no = 2;
    else if ((previous_buttons & B_TERTIARY_MOUSE_BUTTON) &&
	     !(buttons & B_TERTIARY_MOUSE_BUTTON))
      rq.btn_no = 1;
    previous_buttons = buttons;

    rq.x = point.x;
    rq.y = point.y;

    uint32_t mods = modifiers ();

    rq.modifiers = 0;
    if (mods & B_SHIFT_KEY)
      rq.modifiers |= HAIKU_MODIFIER_SHIFT;

    if (mods & B_CONTROL_KEY)
      rq.modifiers |= HAIKU_MODIFIER_CTRL;

    if (mods & B_COMMAND_KEY)
      rq.modifiers |= HAIKU_MODIFIER_ALT;

    haiku_write (BUTTON_UP, &rq);
  }
};

class EmacsScrollBar : BScrollBar
{
public:
  void *scroll_bar;

  EmacsScrollBar (int x, int y, int x1, int y1, bool horizontal_p) :
    BScrollBar (BRect (x, y, x1, y1), NULL, NULL, 0, 0, horizontal_p ?
		B_HORIZONTAL : B_VERTICAL)
  {
    BView *vw = (BView *) this;
    vw->SetFlags (vw->Flags () & ~B_FRAME_EVENTS);
    vw->SetResizingMode (B_FOLLOW_NONE);
  }

  void
  MessageReceived (BMessage *msg)
  {
    if (msg->what == SCROLL_BAR_UPDATE)
      {
	this->SetRange (0, msg->GetInt32 ("emacs:range", 0));
	this->SetValue (msg->GetInt32 ("emacs:units", 0));
      }

    BScrollBar::MessageReceived (msg);
  }

  void
  ValueChanged (float new_value)
  {
    struct haiku_scroll_bar_value_event rq;
    rq.scroll_bar = scroll_bar;
    rq.position = new_value;

    haiku_write (SCROLL_BAR_VALUE_EVENT, &rq);
  }

  void
  MouseDown (BPoint pt)
  {
    struct haiku_scroll_bar_drag_event rq;
    rq.dragging_p = 1;
    rq.scroll_bar = scroll_bar;

    haiku_write (SCROLL_BAR_DRAG_EVENT, &rq);
    BScrollBar::MouseDown (pt);
  }

  void
  MouseUp (BPoint pt)
  {
    struct haiku_scroll_bar_drag_event rq;
    rq.dragging_p = 0;
    rq.scroll_bar = scroll_bar;

    haiku_write (SCROLL_BAR_DRAG_EVENT, &rq);
    BScrollBar::MouseUp (pt);
  }
};

static void *
start_running_application (void *data)
{
  haiku_io_init_in_app_thread ();

  ((Emacs *) data)->Lock ();
  ((Emacs *) data)->Run ();
  ((Emacs *) data)->Unlock ();
  return NULL;
}

static uint8_t
reverse_fringe_byte (uint8_t b)
{
  b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
  b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
  b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
  return b;
}

void *
BBitmap_data (void *bitmap)
{
  return ((BBitmap *) bitmap)->Bits ();
}

int
BBitmap_convert (void *_bitmap, void **new_bitmap)
{
  BBitmap *bitmap = (BBitmap *) _bitmap;
  if (bitmap->ColorSpace () != B_RGBA32)
    return 0;
  BRect bounds = bitmap->Bounds ();
  BBitmap *bmp = new (std::nothrow) BBitmap (bounds, B_RGBA32);
  if (!bmp)
    return 0;
  if (bmp->ImportBits (bitmap) != B_OK)
    {
      delete bmp;
      return 0;
    }
  **(BBitmap **) new_bitmap = bmp;
  return 1;
}

void
BBitmap_free (void *bitmap)
{
  delete (BBitmap *) bitmap;
}

void *
BBitmap_new (int width, int height, int mono_p)
{
  BBitmap *bn = new (std::nothrow) BBitmap (BRect (0, 0, width - 1, height - 1),
					    mono_p ? B_GRAY1 : B_RGB32);

  return bn;
}

void
BBitmap_dimensions (void *bitmap, int *left, int *top,
		    int *right, int *bottom,
		    int32_t *bytes_per_row, int *mono_p)
{
  BRect rect = ((BBitmap *) bitmap)->Bounds ();
  *left = rect.left;
  *top = rect.top;
  *right = rect.right;
  *bottom = rect.bottom;

  *bytes_per_row = ((BBitmap *) bitmap)->BytesPerRow ();
  *mono_p = (((BBitmap *) bitmap)->ColorSpace () == B_GRAY1);
}

void *
BApplication_setup (void)
{
  if (be_app)
    return be_app;
  Emacs *app;

  app = new Emacs;
  if (pthread_create (&app_thread_id, NULL, start_running_application, app))
    {
      perror ("pthread_create");
      emacs_abort ();
    }

  return app;
}

void *
BWindow_new (void *_view)
{
  BWindow *window = new (std::nothrow) EmacsWindow;
  BView **v = (BView **) _view;
  if (!window)
    {
      *v = NULL;
      return window;
    }

  BView *vw = new (std::nothrow) EmacsView;
  if (!vw)
    {
      *v = NULL;
      window->Lock ();
      window->Quit ();
      return NULL;
    }
  window->AddChild (vw);
  *v = vw;
  return window;
}

void
BWindow_quit (void *window)
{
  ((BWindow *) window)->Lock ();
  ((BWindow *) window)->Quit ();
}

void
BWindow_set_offset (void *window, int x, int y)
{
  BWindow *wn = (BWindow *) window;
  EmacsWindow *w = dynamic_cast<EmacsWindow *> (wn);
  if (w)
    {
      if (!w->LockLooper ())
	emacs_abort ();
      w->EmacsMoveTo (x, y);
      w->UnlockLooper ();
    }
  else
    wn->MoveTo (x, y);
}

void
BWindow_iconify (void *window)
{
  if (((BWindow *) window)->IsHidden ())
    BWindow_set_visible (window, true);
  ((BWindow *) window)->Minimize (true);
}

void
BWindow_set_visible (void *window, int visible_p)
{
  EmacsWindow *win = (EmacsWindow *) window;
  if (visible_p)
    {
      if (win->IsMinimized ())
	win->Minimize (false);
      win->EmacsShow ();
    }
  else if (!win->IsHidden ())
    {
      if (win->IsMinimized ())
	win->Minimize (false);
      win->EmacsHide ();
    }
  win->Sync ();
}

void
BWindow_retitle (void *window, const char *title)
{
  ((BWindow *) window)->SetTitle (title);
}

void
BWindow_resize (void *window, int width, int height)
{
  ((BWindow *) window)->ResizeTo (width, height);
}

void
BWindow_activate (void *window)
{
  ((BWindow *) window)->Activate ();
}

void
BScreen_px_dim (int *width, int *height)
{
  BScreen screen;
  BRect frame = screen.Frame ();

  *width = frame.right - frame.left;
  *height = frame.bottom - frame.top;
}

void
BView_resize_to (void *view, int width, int height)
{
  EmacsView *vw = (EmacsView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->ResizeTo (width, height);
  vw->AfterResize (width, height);
  vw->UnlockLooper ();
}

void *
BCursor_create_default (void)
{
  return new BCursor (B_CURSOR_ID_SYSTEM_DEFAULT);
}

void *
BCursor_from_id (enum haiku_cursor cursor)
{
  return new BCursor ((enum BCursorID) cursor);
}

void *
BCursor_create_i_beam (void)
{
  return new BCursor (B_CURSOR_ID_I_BEAM);
}

void *
BCursor_create_progress_cursor (void)
{
  return new BCursor (B_CURSOR_ID_PROGRESS);
}

void *
BCursor_create_grab (void)
{
  return new BCursor (B_CURSOR_ID_GRAB);
}

void
BCursor_delete (void *cursor)
{
  delete (BCursor *) cursor;
}

void
BView_set_view_cursor (void *view, void *cursor)
{
  if (!((BView *) view)->LockLooper ())
    emacs_abort ();
  ((BView *) view)->SetViewCursor ((BCursor *) cursor);
  ((BView *) view)->UnlockLooper ();
}

void
BWindow_Flush (void *window)
{
  ((BWindow *) window)->Flush ();
}

void
BMapKey (uint32_t kc, int *non_ascii_p, unsigned *code)
{
  *code = 0;

  switch (kc)
    {
    default:
      *non_ascii_p = 0;
      if (kc < 0xe && kc > 0x1)
	{
	  *code = XK_F1 + kc - 2;
	  *non_ascii_p = 1;
	}
      return;
    case 0x1e:
      *code = XK_BackSpace;
      break;
    case 0x32:
    case 0x5b:
    case 0x47:
      *code = XK_Return;
      break;
    case 0x61:
      *code = XK_Left;
      break;
    case 0x63:
      *code = XK_Right;
      break;
    case 0x57:
      *code = XK_Up;
      break;
    case 0x62:
      *code = XK_Down;
      break;
    case 0x64:
      *code = XK_Insert;
      break;
    case 0x65:
      *code = XK_Delete;
      break;
    case 0x37:
      *code = XK_Home;
      break;
    case 0x58:
      *code = XK_End;
      break;
    case 0x39:
      *code = XK_Page_Up;
      break;
    case 0x5a:
      *code = XK_Page_Down;
      break;
    case 0x1:
      *code = XK_Escape;
      break;
    case 0x68:
      *code = XK_Menu;
      break;
    }
  *non_ascii_p = 1;
}

void *
BScrollBar_make_for_view (void *view, int horizontal_p,
			  int x, int y, int x1, int y1,
			  void *scroll_bar_ptr)
{
  EmacsScrollBar *sb = new EmacsScrollBar (x, y, x1, y1, horizontal_p);
  sb->scroll_bar = scroll_bar_ptr;

  BView *vw = (BView *) view;
  BView *sv = (BView *) sb;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->AddChild ((BView *) sb);
  sv->WindowActivated (vw->Window ()->IsActive ());
  vw->UnlockLooper ();
  return sb;
}

void
BScrollBar_delete (void *sb)
{
  BView *view = (BView *) sb;
  BView *pr = view->Parent ();

  if (!pr->LockLooper ())
    emacs_abort ();
  pr->RemoveChild (view);
  pr->UnlockLooper ();

  delete (EmacsScrollBar *) sb;
}

void
BView_move_frame (void *view, int x, int y, int x1, int y1)
{
  BView *vw = (BView *) view;

  if (!vw->LockLooper ())
    emacs_abort ();
  vw->MoveTo (x, y);
  vw->ResizeTo (x1 - x, y1 - y);
  vw->UnlockLooper ();
}

void
BView_scroll_bar_update (void *sb, int portion, int whole, int position)
{
  BScrollBar *bar = (BScrollBar *) sb;
  BMessage msg = BMessage (SCROLL_BAR_UPDATE);
  BMessenger mr = BMessenger (bar);
  msg.AddInt32 ("emacs:range", whole);
  msg.AddInt32 ("emacs:units", position);

  mr.SendMessage (&msg);
}

int
BScrollBar_default_size (int horizontal_p)
{
  return horizontal_p ? B_H_SCROLL_BAR_HEIGHT : B_V_SCROLL_BAR_WIDTH;
}

void
BView_hide (void *view)
{
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->Hide ();
  vw->UnlockLooper ();
}

void
BView_show (void *view)
{
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->Show ();
  vw->UnlockLooper ();
}

void
BView_invalidate (void *view)
{
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->Invalidate ();
  vw->UnlockLooper ();
}

void
BView_draw_lock (void *view)
{
  EmacsView *vw = (EmacsView *) view;
  if (vw->looper_locked_count)
    {
      vw->looper_locked_count++;
      return;
    }
  BView *v = (BView *) find_appropriate_view_for_draw (vw);
  if (!v->LockLooper ())
    emacs_abort ();

  if (v != vw && !vw->LockLooper ())
    emacs_abort ();
  vw->looper_locked_count++;
}

void
BView_draw_unlock (void *view)
{
  EmacsView *vw = (EmacsView *) view;
  if (--vw->looper_locked_count)
    return;

  BView *v = (BView *) find_appropriate_view_for_draw (view);
  v->UnlockLooper ();
  if (v != vw)
    vw->UnlockLooper ();
}

void
BWindow_center_on_screen (void *window)
{
  BWindow *w = (BWindow *) window;
  w->CenterOnScreen ();
}

void
BView_mouse_down (void *view, int x, int y)
{
  BView *vw = (BView *) view;
  if (vw->LockLooper ())
    {
      vw->MouseDown (BPoint (x, y));
      vw->UnlockLooper ();
    }
}

void
BView_mouse_up (void *view, int x, int y)
{
  BView *vw = (BView *) view;
  if (vw->LockLooper ())
    {
      vw->MouseUp (BPoint (x, y));
      vw->UnlockLooper ();
    }
}

void
BView_mouse_moved (void *view, int x, int y, uint32_t transit)
{
  BView *vw = (BView *) view;
  if (vw->LockLooper ())
    {
      vw->MouseMoved (BPoint (x, y), transit, NULL);
      vw->UnlockLooper ();
    }
}

void
BBitmap_import_mono_bits (void *bitmap, void *bits, int wd, int h)
{
  BBitmap *bmp = (BBitmap *) bitmap;
  unsigned char *data = (unsigned char *) bmp->Bits ();
  unsigned short *bts = (unsigned short *) bits;

  for (int i = 0; i < (h * (wd / 8)); i++)
    {
      *((unsigned short *) data) = reverse_fringe_byte (~bts[i]);
      data += bmp->BytesPerRow ();
    }
}

void
BView_publish_scroll_bar (void *view, int x, int y, int width, int height)
{
  EmacsView *vw = (EmacsView *) view;
  if (vw->LockLooper ())
    {
      vw->sb_region.Include (BRect (x, y, x - 1 + width,
				    y - 1 + height));
      vw->UnlockLooper ();
    }
}

void
BView_forget_scroll_bar (void *view, int x, int y, int width, int height)
{
  EmacsView *vw = (EmacsView *) view;
  if (vw->LockLooper ())
    {
      vw->sb_region.Exclude (BRect (x, y, x - 1 + width,
				    y - 1 + height));
      vw->UnlockLooper ();
    }
}

int
BFont_family_count (void)
{
  return count_font_families ();
}

void
BView_get_mouse (void *view, int *x, int *y)
{
  BPoint l;
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->GetMouse (&l, NULL, 1);
  vw->UnlockLooper ();

  *x = std::lrint (l.x);
  *y = std::lrint (l.y);
}

void
BView_convert_to_screen (void *view, int *x, int *y)
{
  BPoint l = BPoint (*x, *y);
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->ConvertToScreen (&l);
  vw->UnlockLooper ();

  *x = std::lrint (l.x);
  *y = std::lrint (l.y);
}

void
BView_convert_from_screen (void *view, int *x, int *y)
{
  BPoint l = BPoint (*x, *y);
  BView *vw = (BView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->ConvertFromScreen (&l);
  vw->UnlockLooper ();

  *x = std::lrint (l.x);
  *y = std::lrint (l.y);
}

void
BWindow_change_decoration (void *window, int decorate_p)
{
  BWindow *w = (BWindow *) window;
  if (!w->LockLooper ())
    emacs_abort ();
  if (decorate_p)
    w->SetLook (B_TITLED_WINDOW_LOOK);
  else
    w->SetLook (B_NO_BORDER_WINDOW_LOOK);
  w->UnlockLooper ();
}

void
BWindow_set_tooltip_decoration (void *window)
{
  BWindow *w = (BWindow *) window;
  if (!w->LockLooper ())
    emacs_abort ();
  w->SetLook (B_BORDERED_WINDOW_LOOK);
  w->SetFeel (B_FLOATING_APP_WINDOW_FEEL);
  w->UnlockLooper ();
}

void
BWindow_set_avoid_focus (void *window, int avoid_focus_p)
{
  BWindow *w = (BWindow *) window;
  if (!w->LockLooper ())
    emacs_abort ();

  if (!avoid_focus_p)
    w->SetFlags (w->Flags () & ~B_AVOID_FOCUS);
  else
    w->SetFlags (B_AVOID_FOCUS);
  w->Sync ();
  w->UnlockLooper ();
}

void
BView_emacs_delete (void *view)
{
  EmacsView *vw = (EmacsView *) view;
  if (!vw->LockLooper ())
    emacs_abort ();
  vw->RemoveSelf ();
  delete vw;
}

uint32_t
haiku_current_workspace (void)
{
  return current_workspace ();
}

uint32_t
BWindow_workspaces (void *window)
{
  return ((BWindow *) window)->Workspaces ();
}

void *
BPopUpMenu_new (const char *name)
{
  BPopUpMenu *menu = new BPopUpMenu (name, false);
  return menu;
}

void
BMenu_add_item (void *menu, const char *label, void *ptr, bool enabled_p,
		bool marked_p)
{
  BMenu *m = (BMenu *) menu;
  BMessage *msg;
  if (ptr)
    msg = new BMessage ();
  BMenuItem *it = new BMenuItem (label, ptr ? msg : NULL);
  it->SetTarget (m->Window ());
  it->SetEnabled (enabled_p);
  it->SetMarked (marked_p);
  if (ptr)
    msg->AddPointer ("menuptr", ptr);
  m->AddItem (it);
}

void
BMenu_add_separator (void *menu)
{
  BMenu *m = (BMenu *) menu;

  m->AddSeparatorItem ();
}

void *
BMenu_new_submenu (void *menu, const char *label, bool enabled_p)
{
  BMenu *m = (BMenu *) menu;
  BMenu *mn = new BMenu (label, B_ITEMS_IN_COLUMN);
  mn->SetRadioMode (0);
  BMenuItem *i = new BMenuItem (mn);
  i->SetEnabled (enabled_p);
  m->AddItem (i);
  return mn;
}

void *
BMenu_new_menu_bar_submenu (void *menu, const char *label)
{
  BMenu *m = (BMenu *) menu;
  BMenu *mn = new BMenu (label, B_ITEMS_IN_COLUMN);
  mn->SetRadioMode (0);
  BMenuItem *i = new BMenuItem (mn);
  i->SetEnabled (1);
  m->AddItem (i);
  return mn;
}

void *
BMenu_run (void *menu, int x, int y)
{
  BPopUpMenu *mn = (BPopUpMenu *) menu;
  BMenuItem *it = mn->Go (BPoint (x, y));
  if (it)
    {
      BMessage *mg = it->Message ();
      if (mg)
	return (void *) mg->GetPointer ("menuptr");
      else
	return NULL;
    }
  return NULL;
}

void
BPopUpMenu_delete (void *menu)
{
  delete (BPopUpMenu *) menu;
}

void *
BMenuBar_new (void *view)
{
  BView *vw = (BView *) view;
  EmacsMenuBar *bar = new EmacsMenuBar ();

  if (!vw->LockLooper ())
    emacs_abort ();
  vw->AddChild ((BView *) bar);
  vw->UnlockLooper ();

  return bar;
}

void
BMenuBar_delete (void *menubar)
{
  BView *vw = (BView *) menubar;
  BView *p = vw->Parent ();
  if (!p->LockLooper ())
    emacs_abort ();
  vw->RemoveSelf ();
  p->UnlockLooper ();
  delete vw;
}

void
BMenu_delete_all (void *menu)
{
  BMenu *mn = (BMenu *) menu;
  mn->RemoveItems (0, mn->CountItems (), true);
}

void
BMenu_delete_from (void *menu, int start, int count)
{
  BMenu *mn = (BMenu *) menu;
  mn->RemoveItems (start, count, true);
}

int
BMenu_count_items (void *menu)
{
  return ((BMenu *) menu)->CountItems ();
}

void *
BMenu_item_at (void *menu, int idx)
{
  return ((BMenu *) menu)->ItemAt (idx);
}

void
BMenu_item_set_label (void *item, const char *label)
{
  ((BMenuItem *) item)->SetLabel (label);
}

void *
BMenu_item_get_menu (void *item)
{
  return ((BMenuItem *) item)->Submenu ();
}

void
haiku_ring_bell (void)
{
  beep ();
}

void *
BAlert_new (const char *text, enum haiku_alert_type type)
{
  return new BAlert (NULL, text, NULL, NULL, NULL, B_WIDTH_AS_USUAL,
		     (enum alert_type) type);
}

void *
BAlert_add_button (void *alert, const char *text)
{
  BAlert *al = (BAlert *) alert;
  al->AddButton (text);
  return al->ButtonAt (al->CountButtons () - 1);
}

int32_t
BAlert_go (void *alert)
{
  return ((BAlert *) alert)->Go ();
}

void
BButton_set_enabled (void *button, int enabled_p)
{
  ((BButton *) button)->SetEnabled (enabled_p);
}

void
BView_set_tooltip (void *view, const char *tooltip)
{
  ((BView *) view)->SetToolTip (tooltip);
}

void
BAlert_delete (void *alert)
{
  delete (BAlert *) alert;
}

void
BScreen_res (double *rrsx, double *rrsy)
{
  BScreen s (B_MAIN_SCREEN_ID);
  if (!s.IsValid ())
    emacs_abort ();
  monitor_info i;

  if (s.GetMonitorInfo (&i) == B_OK)
    {
      *rrsx = (double) i.width / (double) 2.54;
      *rrsy = (double) i.height / (double) 2.54;
    }
  else
    {
      *rrsx = 72.27;
      *rrsy = 72.27;
    }
}

void
EmacsWindow_parent_to (void *window, void *other_window)
{
  EmacsWindow *w = (EmacsWindow *) window;
  if (!w->LockLooper ())
    emacs_abort ();
  w->ParentTo ((EmacsWindow *) other_window);
  w->UnlockLooper ();
}

void
EmacsWindow_unparent (void *window)
{
  EmacsWindow *w = (EmacsWindow *) window;
  if (!w->LockLooper ())
    emacs_abort ();
  w->UnparentAndUnlink ();
  w->UnlockLooper ();
}

void
be_get_version_string (char *version, int len)
{
  std::strncpy (version, "Unknown Haiku release", len - 1);
  BPath path;
  if (find_directory (B_BEOS_LIB_DIRECTORY, &path) == B_OK)
    {
      path.Append ("libbe.so");

      BAppFileInfo appFileInfo;
      version_info versionInfo;
      BFile file;
      if (file.SetTo (path.Path (), B_READ_ONLY) == B_OK
          && appFileInfo.SetTo (&file) == B_OK
          && appFileInfo.GetVersionInfo (&versionInfo,
                                         B_APP_VERSION_KIND) == B_OK
          && versionInfo.short_info[0] != '\0')
	std::strncpy (version, versionInfo.short_info, len - 1);
    }
}

int
be_get_display_planes (void)
{
  color_space space = dpy_color_space;
  if (space == B_NO_COLOR_SPACE)
    {
      BScreen screen; /* This is actually a very slow operation. */
      space = dpy_color_space = screen.ColorSpace ();
    }

  if (space == B_RGB32 || space == B_RGB24)
    return 24;
  if (space == B_RGB16)
    return 16;
  if (space == B_RGB15)
    return 15;
  if (space == B_CMAP8)
    return 8;

  emacs_abort (); /* https://www.haiku-os.org/docs/api/classBScreen.html
		     says a valid screen can't be anything else. */
  return -1;
}

int
be_get_display_color_cells (void)
{
  color_space space = dpy_color_space;
  if (space == B_NO_COLOR_SPACE)
    {
      BScreen screen;
      space = dpy_color_space = screen.ColorSpace ();
    }

  if (space == B_RGB32 || space == B_RGB24)
    return 1677216;
  if (space == B_RGB16)
    return 65536;
  if (space == B_RGB15)
    return 32768;
  if (space == B_CMAP8)
    return 256;

  emacs_abort ();
  return -1;
}

void
be_warp_pointer (int x, int y)
{
  /* We're not supposed to use the following function without a
     BWindowScreen object, but in Haiku nothing actually prevents us
     from doing so. */

  set_mouse_position (x, y);
}

void
EmacsWindow_move_weak_child (void *window, void *child, int xoff, int yoff)
{
  EmacsWindow *w = (EmacsWindow *) window;
  EmacsWindow *c = (EmacsWindow *) child;

  if (!w->LockLooper ())
    emacs_abort ();
  w->MoveChild (c, xoff, yoff, 1);
  w->UnlockLooper ();
}

void *
find_appropriate_view_for_draw (void *vw)
{
  BView *v = (BView *) vw;
  EmacsView *ev = dynamic_cast<EmacsView *>(v);
  if (!ev)
    return v;

  return ev->offscreen_draw_view ? ev->offscreen_draw_view : vw;
}

void
EmacsView_set_up_double_buffering (void *vw)
{
  EmacsView *view = (EmacsView *) vw;
  if (!view->LockLooper ())
    emacs_abort ();
  if (view->offscreen_draw_view)
    {
      view->UnlockLooper ();
      return;
    }
  view->SetUpDoubleBuffering ();
  view->UnlockLooper ();
}

void
EmacsView_flip_and_blit (void *vw)
{
  EmacsView *view = (EmacsView *) vw;
  if (!view->offscreen_draw_view)
    return;
  if (!view->LockLooper ())
    emacs_abort ();
  view->FlipBuffers ();
  view->UnlockLooper ();
}

void
EmacsView_disable_double_buffering (void *vw)
{
  EmacsView *view = (EmacsView *) vw;
  if (!view->LockLooper ())
    emacs_abort ();
  view->TearDownDoubleBuffering ();
  view->UnlockLooper ();
}

int
EmacsView_double_buffered_p (void *vw)
{
  EmacsView *view = (EmacsView *) vw;
  if (!view->LockLooper ())
    emacs_abort ();
  int db_p = !!view->offscreen_draw_view;
  view->UnlockLooper ();
  return db_p;
}

debug log:

solving e0800b2245 ...
found e0800b2245 in https://yhetil.org/emacs/87zgssfuz9.fsf@yahoo.com/

applying [1/1] https://yhetil.org/emacs/87zgssfuz9.fsf@yahoo.com/
diff --git a/src/haiku_support.cc b/src/haiku_support.cc
new file mode 100644
index 0000000000..e0800b2245

Checking patch src/haiku_support.cc...
Applied patch src/haiku_support.cc cleanly.

index at:
100644 e0800b2245e6eb1ea2c5e6a5dbc48d20e1a49593	src/haiku_support.cc

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.