1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <math.h>

#include <locale.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <ctk/ctk.h>
#include <cdk/cdk.h>
#include <cdk/cdkx.h>
#include <cdk/cdkkeysyms.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/XIproto.h>

#include <gio/gio.h>

#include "cafe-settings-profile.h"
#include "csd_mouse-manager.h"
#include "csd_input-helper.h"

/* Keys with same names for both touchpad and mouse */
#define KEY_LEFT_HANDED                  "left-handed"          /*  a boolean for mouse, an enum for touchpad */
#define KEY_MOTION_ACCELERATION          "motion-acceleration"
#define KEY_MOTION_THRESHOLD             "motion-threshold"
#define KEY_ACCEL_PROFILE                "accel-profile"

/* Mouse settings */
#define CAFE_MOUSE_SCHEMA                "org.cafe.peripherals-mouse"
#define KEY_MOUSE_LOCATE_POINTER         "locate-pointer"
#define KEY_MIDDLE_BUTTON_EMULATION      "middle-button-enabled"

/* Touchpad settings */
#define CAFE_TOUCHPAD_SCHEMA             "org.cafe.peripherals-touchpad"
#define KEY_TOUCHPAD_DISABLE_W_TYPING    "disable-while-typing"
#define KEY_TOUCHPAD_TWO_FINGER_CLICK    "two-finger-click"
#define KEY_TOUCHPAD_THREE_FINGER_CLICK  "three-finger-click"
#define KEY_TOUCHPAD_NATURAL_SCROLL      "natural-scroll"
#define KEY_TOUCHPAD_TAP_TO_CLICK        "tap-to-click"
#define KEY_TOUCHPAD_ONE_FINGER_TAP      "tap-button-one-finger"
#define KEY_TOUCHPAD_TWO_FINGER_TAP      "tap-button-two-finger"
#define KEY_TOUCHPAD_THREE_FINGER_TAP    "tap-button-three-finger"
#define KEY_VERT_EDGE_SCROLL             "vertical-edge-scrolling"
#define KEY_HORIZ_EDGE_SCROLL            "horizontal-edge-scrolling"
#define KEY_VERT_TWO_FINGER_SCROLL       "vertical-two-finger-scrolling"
#define KEY_HORIZ_TWO_FINGER_SCROLL      "horizontal-two-finger-scrolling"
#define KEY_TOUCHPAD_ENABLED             "touchpad-enabled"


#if 0   /* FIXME need to fork (?) mousetweaks for this to work */
#define CAFE_MOUSE_A11Y_SCHEMA           "org.cafe.accessibility-mouse"
#define KEY_MOUSE_A11Y_DWELL_ENABLE      "dwell-enable"
#define KEY_MOUSE_A11Y_DELAY_ENABLE      "delay-enable"
#endif

struct CsdMouseManagerPrivate
{
        GSettings *settings_mouse;
        GSettings *settings_touchpad;

#if 0   /* FIXME need to fork (?) mousetweaks for this to work */
        gboolean mousetweaks_daemon_running;
#endif
        gboolean syndaemon_spawned;
        GPid syndaemon_pid;
        gboolean locate_pointer_spawned;
        GPid locate_pointer_pid;
};

typedef enum {
        TOUCHPAD_HANDEDNESS_RIGHT,
        TOUCHPAD_HANDEDNESS_LEFT,
        TOUCHPAD_HANDEDNESS_MOUSE
} TouchpadHandedness;

typedef enum {
        ACCEL_PROFILE_DEFAULT,
        ACCEL_PROFILE_ADAPTIVE,
        ACCEL_PROFILE_FLAT
} AccelProfile;

static void     csd_mouse_manager_finalize    (GObject              *object);
static void     set_mouse_settings            (CsdMouseManager      *manager);
static void     set_tap_to_click_synaptics    (XDeviceInfo          *device_info,
                                               gboolean              state,
                                               gboolean              left_handed,
                                               gint                  one_finger_tap,
                                               gint                  two_finger_tap,
                                               gint                  three_finger_tap);


G_DEFINE_TYPE_WITH_PRIVATE (CsdMouseManager, csd_mouse_manager, G_TYPE_OBJECT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static gpointer manager_object = NULL;

static void
csd_mouse_manager_class_init (CsdMouseManagerClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = csd_mouse_manager_finalize;
}

static void
configure_button_layout (guchar   *buttons,
                         gint      n_buttons,
                         gboolean  left_handed)
{
        const gint left_button = 1;
        gint right_button;
        gint i;

        /* if the button is higher than 2 (3rd button) then it's
         * probably one direction of a scroll wheel or something else
         * uninteresting
         */
        right_button = MIN (n_buttons, 3);

        /* If we change things we need to make sure we only swap buttons.
         * If we end up with multiple physical buttons assigned to the same
         * logical button the server will complain. This code assumes physical
         * button 0 is the physical left mouse button, and that the physical
         * button other than 0 currently assigned left_button or right_button
         * is the physical right mouse button.
         */

        /* check if the current mapping satisfies the above assumptions */
        if (buttons[left_button - 1] != left_button &&
            buttons[left_button - 1] != right_button)
                /* The current mapping is weird. Swapping buttons is probably not a
                 * good idea.
                 */
                return;

        /* check if we are left_handed and currently not swapped */
        if (left_handed && buttons[left_button - 1] == left_button) {
                /* find the right button */
                for (i = 0; i < n_buttons; i++) {
                        if (buttons[i] == right_button) {
                                buttons[i] = left_button;
                                break;
                        }
                }
                /* swap the buttons */
                buttons[left_button - 1] = right_button;
        }
        /* check if we are not left_handed but are swapped */
        else if (!left_handed && buttons[left_button - 1] == right_button) {
                /* find the right button */
                for (i = 0; i < n_buttons; i++) {
                        if (buttons[i] == left_button) {
                                buttons[i] = right_button;
                                break;
                        }
                }
                /* swap the buttons */
                buttons[left_button - 1] = left_button;
        }
}

static gboolean
xinput_device_has_buttons (XDeviceInfo *device_info)
{
        int i;
        XAnyClassInfo *class_info;

        class_info = device_info->inputclassinfo;
        for (i = 0; i < device_info->num_classes; i++) {
                if (class_info->class == ButtonClass) {
                        XButtonInfo *button_info;

                        button_info = (XButtonInfo *) class_info;
                        if (button_info->num_buttons > 0)
                                return TRUE;
                }

                class_info = (XAnyClassInfo *) (((guchar *) class_info) +
                                                class_info->length);
        }
        return FALSE;
}

static Atom
property_from_name (const char *property_name)
{
        return XInternAtom (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), property_name, True);
}

static void *
get_property (XDevice     *device,
              const gchar *property,
              Atom         type,
              int          format,
              gulong       nitems)
{
        CdkDisplay *display;
        gulong nitems_ret, bytes_after_ret;
        int rc, format_ret;
        Atom property_atom, type_ret;
        guchar *data = NULL;

        property_atom = property_from_name (property);
        if (!property_atom)
                return NULL;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);

        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, property_atom,
                                 0, 10, False, type, &type_ret, &format_ret,
                                 &nitems_ret, &bytes_after_ret, &data);

        cdk_x11_display_error_trap_pop_ignored (display);

        if (rc == Success && type_ret == type && format_ret == format && nitems_ret >= nitems)
                return data;

        if (data)
                XFree (data);

        return NULL;
}

static void
change_property (XDevice     *device,
                 const gchar *property,
                 Atom         type,
                 int          format,
                 void        *data,
                 gulong       nitems)
{
        CdkDisplay *display;
        Atom property_atom;
        guchar *data_ret;

        property_atom = property_from_name (property);
        if (!property_atom)
                return;

        data_ret = get_property (device, property, type, format, nitems);
        if (!data_ret)
                return;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);

        XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device,
                               property_atom, type, format,
                               PropModeReplace, data, nitems);

        cdk_x11_display_error_trap_pop_ignored (display);

        XFree (data_ret);
}

static gboolean
touchpad_has_single_button (XDevice *device)
{
        Atom type, prop;
        CdkDisplay  *display;
        int format;
        unsigned long nitems, bytes_after;
        unsigned char *data;
        gboolean is_single_button = FALSE;
        int rc;

        prop = property_from_name ("Synaptics Capabilities");
        if (!prop)
                return FALSE;

        display = cdk_display_get_default ();
        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 1, False,
                                 XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);
        if (rc == Success && type == XA_INTEGER && format == 8 && nitems >= 3)
                is_single_button = (data[0] == 1 && data[1] == 0 && data[2] == 0);

        if (rc == Success)
                XFree (data);

        cdk_x11_display_error_trap_pop_ignored (display);

        return is_single_button;
}

static gboolean
property_exists_on_device (XDeviceInfo *device_info,
                           const char  *property_name)
{
        XDevice *device;
        int rc;
        Atom type, prop;
        CdkDisplay  *display;
        int format;
        unsigned long nitems, bytes_after;
        unsigned char *data;

        prop = property_from_name (property_name);
        if (!prop)
                return FALSE;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
        if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                return FALSE;

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display),
                                 device, prop, 0, 1, False, XA_INTEGER, &type, &format,
                                 &nitems, &bytes_after, &data);

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        cdk_x11_display_error_trap_pop_ignored (display);

        return rc == Success;
}

static void
property_set_bool (XDeviceInfo *device_info,
                   XDevice     *device,
                   const char  *property_name,
                   int          property_index,
                   gboolean     enabled)
{
        int rc;
        unsigned long nitems, bytes_after;
        unsigned char *data;
        int act_format;
        Atom act_type, property;
        CdkDisplay  *display;

        property = property_from_name (property_name);
        if (!property)
                return;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device,
                                 property, 0, 1, False,
                                 XA_INTEGER, &act_type, &act_format, &nitems,
                                 &bytes_after, &data);

        if (rc == Success && act_type == XA_INTEGER && act_format == 8 && nitems > property_index) {
                data[property_index] = enabled ? 1 : 0;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device,
                                       property, XA_INTEGER, 8,
                                       PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error while setting %s on \"%s\"", property_name, device_info->name);
        }
}

static void
touchpad_set_bool (XDeviceInfo *device_info,
                   const char  *property_name,
                   int          property_index,
                   gboolean     enabled)
{
        XDevice    *device;
        CdkDisplay *display;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        property_set_bool (device_info, device, property_name, property_index, enabled);

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        cdk_x11_display_error_trap_pop_ignored (display);
}

static void
set_left_handed_legacy_driver (CsdMouseManager *manager,
                               XDeviceInfo     *device_info,
                               gboolean         mouse_left_handed,
                               gboolean         touchpad_left_handed)
{
        XDevice *device;
        CdkDisplay *display;
        guchar *buttons;
        gsize buttons_capacity = 16;
        gint n_buttons;
        gboolean left_handed;

        if ((device_info->use == IsXPointer) ||
            (device_info->use == IsXKeyboard) ||
            (g_strcmp0 ("Virtual core XTEST pointer", device_info->name) == 0) ||
            (!xinput_device_has_buttons (device_info)))
                return;

        /* If the device is a touchpad, swap tap buttons
         * around too, otherwise a tap would be a right-click */
        device = device_is_touchpad (device_info);
        display = cdk_display_get_default ();

        if (device != NULL) {
                gboolean tap = g_settings_get_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_TAP_TO_CLICK);
                gboolean single_button = touchpad_has_single_button (device);

                left_handed = touchpad_left_handed;

                if (tap && !single_button) {
                        gint one_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_ONE_FINGER_TAP);
                        gint two_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_TWO_FINGER_TAP);
                        gint three_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_THREE_FINGER_TAP);
                        set_tap_to_click_synaptics (device_info, tap, left_handed, one_finger_tap, two_finger_tap, three_finger_tap);
                }

                XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);

                if (single_button)
                        return;
        } else {
                left_handed = mouse_left_handed;
        }

        cdk_x11_display_error_trap_push (display);

        device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
        if ((cdk_x11_display_error_trap_pop (display) != 0) ||
            (device == NULL))
                return;

        buttons = g_new (guchar, buttons_capacity);

        n_buttons = XGetDeviceButtonMapping (CDK_DISPLAY_XDISPLAY (display), device,
                                             buttons,
                                             buttons_capacity);

        while (n_buttons > buttons_capacity) {
                buttons_capacity = n_buttons;
                buttons = (guchar *) g_realloc (buttons,
                                                buttons_capacity * sizeof (guchar));

                n_buttons = XGetDeviceButtonMapping (CDK_DISPLAY_XDISPLAY (display), device,
                                                     buttons,
                                                     buttons_capacity);
        }

        configure_button_layout (buttons, n_buttons, left_handed);

        XSetDeviceButtonMapping (CDK_DISPLAY_XDISPLAY (display), device, buttons, n_buttons);
        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);

        g_free (buttons);
}

static void
set_left_handed_libinput (XDeviceInfo *device_info,
                          gboolean     mouse_left_handed,
                          gboolean     touchpad_left_handed)
{
        XDevice    *device;
        CdkDisplay *display;
        gboolean   want_lefthanded;

        device = device_is_touchpad (device_info);
        display = cdk_display_get_default ();

        if (device == NULL) {
                cdk_x11_display_error_trap_push (display);
                device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
                if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                        return;

                want_lefthanded = mouse_left_handed;
        } else {
                /* touchpad device is already open after
                 * return from device_is_touchpad function
                 */
                want_lefthanded = touchpad_left_handed;
        }

        property_set_bool (device_info, device, "libinput Left Handed Enabled", 0, want_lefthanded);

        cdk_x11_display_error_trap_push (display);
        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
          cdk_x11_display_error_trap_pop_ignored (display);

}

static void
set_left_handed (CsdMouseManager *manager,
                 XDeviceInfo     *device_info,
                 gboolean         mouse_left_handed,
                 gboolean         touchpad_left_handed)
{
        if (property_exists_on_device (device_info, "libinput Left Handed Enabled"))
                set_left_handed_libinput (device_info, mouse_left_handed, touchpad_left_handed);
        else
                set_left_handed_legacy_driver (manager, device_info, mouse_left_handed, touchpad_left_handed);
}

static void
set_left_handed_all (CsdMouseManager *manager,
                     gboolean         mouse_left_handed,
                     gboolean         touchpad_left_handed)
{
        XDeviceInfo *device_info;
        gint n_devices;
        gint i;

        device_info = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &n_devices);

        for (i = 0; i < n_devices; i++) {
                set_left_handed (manager, &device_info[i], mouse_left_handed, touchpad_left_handed);
        }

        if (device_info != NULL)
                XFreeDeviceList (device_info);
}

static CdkFilterReturn
devicepresence_filter (CdkXEvent *xevent,
                       CdkEvent  *event,
                       gpointer   data)
{
        XEvent *xev = (XEvent *) xevent;
        G_GNUC_UNUSED XEventClass class_presence;
        int xi_presence;

        DevicePresence (cdk_x11_get_default_xdisplay (), xi_presence, class_presence);

        if (xev->type == xi_presence)
        {
                XDevicePresenceNotifyEvent *dpn = (XDevicePresenceNotifyEvent *) xev;
                if (dpn->devchange == DeviceEnabled)
                        set_mouse_settings ((CsdMouseManager *) data);
        }

        return CDK_FILTER_CONTINUE;
}

static void
set_devicepresence_handler (CsdMouseManager *manager)
{
        CdkDisplay    *cdk_display;
        Display       *display;
        XEventClass    class_presence;
        G_GNUC_UNUSED  int xi_presence;

        cdk_display = cdk_display_get_default ();
        display = cdk_x11_get_default_xdisplay ();

        cdk_x11_display_error_trap_push (cdk_display);
        DevicePresence (display, xi_presence, class_presence);
        XSelectExtensionEvent (display,
                               RootWindow (display, DefaultScreen (display)),
                               &class_presence, 1);

        cdk_display_flush (cdk_display);
        if (!cdk_x11_display_error_trap_pop (cdk_display))
                cdk_window_add_filter (NULL, devicepresence_filter, manager);
}

static void
set_motion_legacy_driver (CsdMouseManager *manager,
                          XDeviceInfo     *device_info)
{
        XDevice *device;
        CdkDisplay *display;
        XPtrFeedbackControl feedback;
        XFeedbackState *states, *state;
        gint num_feedbacks, i;
        GSettings *settings;
        gdouble motion_acceleration;
        gint motion_threshold;
        gint numerator, denominator;

        device = device_is_touchpad (device_info);
        display = cdk_display_get_default ();

        if (device != NULL) {
                settings = manager->priv->settings_touchpad;
        } else {
                cdk_x11_display_error_trap_push (display);
                device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
                if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                        return;

                settings = manager->priv->settings_mouse;
        }

        /* Calculate acceleration */
        motion_acceleration = g_settings_get_double (settings, KEY_MOTION_ACCELERATION);
        if (motion_acceleration >= 1.0) {
                /* we want to get the acceleration, with a resolution of 0.5
                 */
                if ((motion_acceleration - floor (motion_acceleration)) < 0.25) {
                        numerator = floor (motion_acceleration);
                        denominator = 1;
                } else if ((motion_acceleration - floor (motion_acceleration)) < 0.5) {
                        numerator = ceil (2.0 * motion_acceleration);
                        denominator = 2;
                } else if ((motion_acceleration - floor (motion_acceleration)) < 0.75) {
                        numerator = floor (2.0 *motion_acceleration);
                        denominator = 2;
                } else {
                        numerator = ceil (motion_acceleration);
                        denominator = 1;
                }
        } else if (motion_acceleration < 1.0 && motion_acceleration > 0) {
                /* This we do to 1/10ths */
                numerator = floor (motion_acceleration * 10) + 1;
                denominator= 10;
        } else {
                numerator = -1;
                denominator = -1;
        }

        /* And threshold */
        motion_threshold = g_settings_get_int (settings, KEY_MOTION_THRESHOLD);

        /* Get the list of feedbacks for the device */
        states = XGetFeedbackControl (CDK_DISPLAY_XDISPLAY (display), device, &num_feedbacks);
        if (states == NULL) {
                XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
                return;
        }

        state = (XFeedbackState *) states;
        for (i = 0; i < num_feedbacks; i++) {
                if (state->class == PtrFeedbackClass) {
                        /* And tell the device */
                        feedback.class      = PtrFeedbackClass;
                        feedback.length     = sizeof (XPtrFeedbackControl);
                        feedback.id         = state->id;
                        feedback.threshold  = motion_threshold;
                        feedback.accelNum   = numerator;
                        feedback.accelDenom = denominator;

                        g_debug ("Setting accel %d/%d, threshold %d for device '%s'",
                                 numerator, denominator, motion_threshold, device_info->name);

                        XChangeFeedbackControl (CDK_DISPLAY_XDISPLAY (display),
                                                device,
                                                DvAccelNum | DvAccelDenom | DvThreshold,
                                                (XFeedbackControl *) &feedback);
                        break;
                }

                state = (XFeedbackState *) ((char *) state + state->length);
        }

        XFreeFeedbackList (states);
        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
}

static void
set_motion_libinput (CsdMouseManager *manager,
                     XDeviceInfo     *device_info)
{
        XDevice *device;
        CdkDisplay *display;
        Atom prop;
        Atom type;
        Atom float_type;
        int format, rc;
        unsigned long nitems, bytes_after;
        GSettings *settings;
        union {
                unsigned char *c;
                long *l;
        } data;
        gfloat accel;
        gfloat motion_acceleration;

        float_type = property_from_name ("FLOAT");
        if (!float_type)
                return;

        prop = property_from_name ("libinput Accel Speed");
        if (!prop) {
                return;
        }

        device = device_is_touchpad (device_info);
        display = cdk_display_get_default ();

        if (device != NULL) {
                settings = manager->priv->settings_touchpad;
        } else {
                cdk_x11_display_error_trap_push (display);
                device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
                if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                        return;

                settings = manager->priv->settings_mouse;
        }

        /* Calculate acceleration */
        motion_acceleration = g_settings_get_double (settings, KEY_MOTION_ACCELERATION);

        /* panel gives us a range of 1.0-10.0, map to libinput's [-1, 1]
         *
         * oldrange = (oldmax - oldmin)
         * newrange = (newmax - newmin)
         *
         * mapped = (value - oldmin) * newrange / oldrange + oldmin
         */

        if (motion_acceleration == -1.0) /* unset */
                accel = 0.0;
        else
                accel = (motion_acceleration - 1.0) * 2.0 / 9.0 - 1;

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display),
                                 device, prop, 0, 1, False, float_type, &type, &format,
                                 &nitems, &bytes_after, &data.c);

        if (rc == Success && type == float_type && format == 32 && nitems >= 1) {
                *(float *) data.l = accel;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display),
                                       device, prop, float_type, 32, PropModeReplace, data.c, nitems);
        }

        if (rc == Success) {
                XFree (data.c);
        }

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error while setting accel speed on \"%s\"", device_info->name);
        }
}

static void
set_motion (CsdMouseManager *manager,
            XDeviceInfo     *device_info)
{
        if (property_exists_on_device (device_info, "libinput Accel Speed"))
                set_motion_libinput (manager, device_info);
        else
                set_motion_legacy_driver (manager, device_info);
}

static void
set_motion_all (CsdMouseManager *manager)
{
        XDeviceInfo *device_info;
        gint n_devices;
        gint i;

        device_info = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &n_devices);

        for (i = 0; i < n_devices; i++) {
                set_motion (manager, &device_info[i]);
        }

        if (device_info != NULL)
                XFreeDeviceList (device_info);
}

static void
set_middle_button_evdev (XDeviceInfo *device_info,
                         gboolean     middle_button)
{
        CdkDisplay *display;
        XDevice *device;
        Atom prop;
        Atom type;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char *data;

        prop = property_from_name ("Evdev Middle Button Emulation");

        if (!prop) /* no evdev devices */
                return;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);

        device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
        if ((cdk_x11_display_error_trap_pop (display) != 0) ||
            (device == NULL))
                return;

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display),
                                 device, prop, 0, 1, False, XA_INTEGER, &type, &format,
                                 &nitems, &bytes_after, &data);

        if (rc == Success && format == 8 && type == XA_INTEGER && nitems == 1) {
                data[0] = middle_button ? 1 : 0;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display),
                                       device, prop, type, format, PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting middle button emulation on \"%s\"", device_info->name);
        }
}

static void
set_middle_button_libinput (XDeviceInfo *device_info,
                            gboolean     middle_button)
{
        XDevice *device;
        CdkDisplay *display;

        /* touchpad devices are excluded as the old code
         * only applies to evdev devices
         */
        device = device_is_touchpad (device_info);
        display = cdk_display_get_default ();

        if (device != NULL) {
                cdk_x11_display_error_trap_push (display);
                XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
                cdk_x11_display_error_trap_pop_ignored (display);
                return;
        }

        cdk_x11_display_error_trap_push (display);
        device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
        if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                return;

        property_set_bool (device_info, device, "libinput Middle Emulation Enabled", 0, middle_button);

        cdk_x11_display_error_trap_push (display);
        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        cdk_x11_display_error_trap_pop_ignored (display);
}

static void
set_middle_button (XDeviceInfo *device_info,
                   gboolean     middle_button)
{
        if (property_from_name ("Evdev Middle Button Emulation"))
                set_middle_button_evdev (device_info, middle_button);

        if (property_from_name ("libinput Middle Emulation Enabled"))
                set_middle_button_libinput (device_info, middle_button);
}

static void
set_middle_button_all (gboolean middle_button)
{
        XDeviceInfo *device_info;
        gint n_devices;
        gint i;

        device_info = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &n_devices);

        for (i = 0; i < n_devices; i++) {
                set_middle_button (&device_info[i], middle_button);
        }

        if (device_info != NULL)
                XFreeDeviceList (device_info);
}

static gboolean
have_program_in_path (const char *name)
{
        gchar *path;
        gboolean result;

        path = g_find_program_in_path (name);
        result = (path != NULL);
        g_free (path);
        return result;
}

static void
set_disable_w_typing_synaptics (CsdMouseManager *manager,
                                gboolean         state)
{
        if (state && touchpad_is_present ()) {
                GError *error = NULL;
                char *args[6];

                if (manager->priv->syndaemon_spawned)
                        return;

                args[0] = "syndaemon";
                args[1] = "-i";
                args[2] = "0.5";
                args[3] = "-K";
                args[4] = "-R";
                args[5] = NULL;

                if (!have_program_in_path (args[0]))
                        return;

                g_spawn_async (g_get_home_dir (), args, NULL,
                                G_SPAWN_SEARCH_PATH, NULL, NULL,
                                &manager->priv->syndaemon_pid, &error);

                manager->priv->syndaemon_spawned = (error == NULL);

                if (error) {
                        g_settings_set_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_DISABLE_W_TYPING, FALSE);
                        g_error_free (error);
                }

        } else if (manager->priv->syndaemon_spawned)
        {
                kill (manager->priv->syndaemon_pid, SIGHUP);
                g_spawn_close_pid (manager->priv->syndaemon_pid);
                manager->priv->syndaemon_spawned = FALSE;
        }
}

static void
set_disable_w_typing_libinput (CsdMouseManager *manager,
                               gboolean         state)
{
        XDeviceInfo *device_info;
        gint n_devices;
        gint i;

        /* This is only called once for synaptics but for libinput
         * we need to loop through the list of devices
         */
        device_info = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &n_devices);

        for (i = 0; i < n_devices; i++) {
                touchpad_set_bool (&device_info[i], "libinput Disable While Typing Enabled", 0, state);
        }

        if (device_info != NULL)
                XFreeDeviceList (device_info);
}

static void
set_disable_w_typing (CsdMouseManager *manager,
                      gboolean         state)
{
        if (property_from_name ("Synaptics Off"))
                set_disable_w_typing_synaptics (manager, state);

        if (property_from_name ("libinput Disable While Typing Enabled"))
                set_disable_w_typing_libinput (manager, state);
}

static void
set_accel_profile_libinput (CsdMouseManager *manager,
                            XDeviceInfo     *device_info)
{
        XDevice *device;
        CdkDisplay *display;
        GSettings *settings;
        guchar *available, *defaults, *values;

        display = cdk_display_get_default ();

        device = device_is_touchpad (device_info);

        if (device != NULL) {
                settings = manager->priv->settings_touchpad;
        } else {
                cdk_x11_display_error_trap_push (display);
                device = XOpenDevice (CDK_DISPLAY_XDISPLAY (display), device_info->id);
                if ((cdk_x11_display_error_trap_pop (display) != 0) || (device == NULL))
                        return;

                settings = manager->priv->settings_mouse;
        }

        available = get_property (device, "libinput Accel Profiles Available", XA_INTEGER, 8, 2);
        if (!available)
                return;
        XFree (available);

        defaults = get_property (device, "libinput Accel Profile Enabled Default", XA_INTEGER, 8, 2);
        if (!defaults)
                return;

        values = get_property (device, "libinput Accel Profile Enabled", XA_INTEGER, 8, 2);
        if (!values) {
                XFree (defaults);
                return;
        }

        /* 2 boolean values (8 bit, 0 or 1), in order "adaptive", "flat". */
        switch (g_settings_get_enum (settings, KEY_ACCEL_PROFILE)) {
                case ACCEL_PROFILE_ADAPTIVE:
                        values[0] = 1; /* enable adaptive */
                        values[1] = 0; /* disable flat */
                        break;
                case ACCEL_PROFILE_FLAT:
                        values[0] = 0; /* disable adaptive */
                        values[1] = 1; /* enable flat */
                        break;
                case ACCEL_PROFILE_DEFAULT:
                default:
                        values[0] = defaults[0];
                        values[1] = defaults[1];
                        break;
        }

        change_property (device, "libinput Accel Profile Enabled", XA_INTEGER, 8, values, 2);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);

        XFree (defaults);
        XFree (values);
}

static void
set_accel_profile (CsdMouseManager *manager,
                   XDeviceInfo     *device_info)
{
        if (property_exists_on_device (device_info, "libinput Accel Profile Enabled"))
                set_accel_profile_libinput (manager, device_info);

        /* TODO: Add acceleration profiles for synaptics/legacy drivers */
}

static void
set_accel_profile_all (CsdMouseManager *manager)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        for (i = 0; i < numdevices; i++) {
                set_accel_profile (manager, &devicelist[i]);
        }

        XFreeDeviceList (devicelist);
}

static gboolean
get_touchpad_handedness (CsdMouseManager *manager,
                         gboolean         mouse_left_handed)
{
        switch (g_settings_get_enum (manager->priv->settings_touchpad, KEY_LEFT_HANDED)) {
        case TOUCHPAD_HANDEDNESS_RIGHT:
                return FALSE;
        case TOUCHPAD_HANDEDNESS_LEFT:
                return TRUE;
        case TOUCHPAD_HANDEDNESS_MOUSE:
                return mouse_left_handed;
        default:
                g_assert_not_reached ();
        }
}

static void
set_tap_to_click_synaptics (XDeviceInfo *device_info,
                            gboolean     state,
                            gboolean     left_handed,
                            gint         one_finger_tap,
                            gint         two_finger_tap,
                            gint         three_finger_tap)
{
        XDevice *device;
        CdkDisplay *display;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char* data;
        Atom prop, type;

        prop = property_from_name ("Synaptics Tap Action");

        if (!prop)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 2,
                                 False, XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);

        if (one_finger_tap > 3 || one_finger_tap < 1)
                one_finger_tap = 1;
        if (two_finger_tap > 3 || two_finger_tap < 1)
                two_finger_tap = 3;
        if (three_finger_tap > 3 || three_finger_tap < 1)
                three_finger_tap = 2;

        if (rc == Success && type == XA_INTEGER && format == 8 && nitems >= 7)
        {
                /* Set RLM mapping for 1/2/3 fingers*/
                data[4] = (state) ? ((left_handed) ? (4-one_finger_tap) : one_finger_tap) : 0;
                data[5] = (state) ? ((left_handed) ? (4-two_finger_tap) : two_finger_tap) : 0;
                data[6] = (state) ? three_finger_tap : 0;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, XA_INTEGER, 8,
                                       PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting tap to click on \"%s\"", device_info->name);
        }
}

static void
set_tap_to_click_libinput (XDeviceInfo *device_info,
                           gboolean     state)
{
        touchpad_set_bool (device_info, "libinput Tapping Enabled", 0, state);
}

static void
set_tap_to_click (XDeviceInfo *device_info,
                  gboolean     state,
                  gboolean     left_handed,
                  gint         one_finger_tap,
                  gint         two_finger_tap,
                  gint         three_finger_tap)
{
        if (property_from_name ("Synaptics Tap Action"))
                set_tap_to_click_synaptics (device_info, state, left_handed,
                                            one_finger_tap, two_finger_tap, three_finger_tap);

        if (property_from_name ("libinput Tapping Enabled"))
                set_tap_to_click_libinput (device_info, state);
}

static void
set_tap_to_click_all (CsdMouseManager *manager)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        gboolean state = g_settings_get_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_TAP_TO_CLICK);
        gboolean left_handed = get_touchpad_handedness (manager, g_settings_get_boolean (manager->priv->settings_mouse, KEY_LEFT_HANDED));
        gint one_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_ONE_FINGER_TAP);
        gint two_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_TWO_FINGER_TAP);
        gint three_finger_tap = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_THREE_FINGER_TAP);

        for (i = 0; i < numdevices; i++) {
                set_tap_to_click (&devicelist[i], state, left_handed, one_finger_tap, two_finger_tap, three_finger_tap);
        }

        XFreeDeviceList (devicelist);
}

static void
set_click_actions_synaptics (XDeviceInfo *device_info,
                             gint         enable_two_finger_click,
                             gint         enable_three_finger_click)
{
        XDevice *device;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char* data;
        Atom prop, type;
        CdkDisplay *display;

        prop = property_from_name ("Synaptics Click Action");
        if (!prop)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        g_debug ("setting click action to click on %s", device_info->name);

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 2,
                                 False, XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);

        if (rc == Success && type == XA_INTEGER && format == 8 && nitems >= 3) {
                data[0] = 1;
                data[1] = enable_two_finger_click;
                data[2] = enable_three_finger_click;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop,
                                       XA_INTEGER, 8, PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting click actions on \"%s\"", device_info->name);
        }
}

static void
set_click_actions_libinput (XDeviceInfo *device_info,
                            gint         enable_two_finger_click,
                            gint         enable_three_finger_click)
{
        XDevice *device;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char *data;
        Atom prop, type;
        gboolean want_clickfinger;
        gboolean want_softwarebuttons;
        CdkDisplay *display;

        prop = property_from_name ("libinput Click Method Enabled");
        if (!prop)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        g_debug ("setting click action to click on %s", device_info->name);

        want_clickfinger = enable_two_finger_click || enable_three_finger_click;
        want_softwarebuttons = !want_clickfinger;

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 2,
                                 False, XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);

        if (rc == Success && type == XA_INTEGER && format == 8 && nitems >= 2) {
                data[0] = want_softwarebuttons;
                data[1] = want_clickfinger;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop,
                                       XA_INTEGER, 8, PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting click actions on \"%s\"", device_info->name);
        }
}

static void
set_click_actions (XDeviceInfo *device_info,
                   gint         enable_two_finger_click,
                   gint         enable_three_finger_click)
{
        if (property_from_name ("Synaptics Click Action"))
                set_click_actions_synaptics (device_info, enable_two_finger_click, enable_three_finger_click);

        if (property_from_name ("libinput Click Method Enabled"))
                set_click_actions_libinput (device_info, enable_two_finger_click, enable_three_finger_click);
}

static void
set_click_actions_all (CsdMouseManager *manager)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        gint enable_two_finger_click = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_TWO_FINGER_CLICK);
        gint enable_three_finger_click = g_settings_get_int (manager->priv->settings_touchpad, KEY_TOUCHPAD_THREE_FINGER_CLICK);

        for (i = 0; i < numdevices; i++) {
                set_click_actions (&devicelist[i], enable_two_finger_click, enable_three_finger_click);
        }

        XFreeDeviceList (devicelist);
}

static void
set_natural_scroll_synaptics (XDeviceInfo *device_info,
                              gboolean     natural_scroll)
{
        XDevice *device;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char* data;
        glong *ptr;
        Atom prop, type;
        CdkDisplay *display;

        prop = property_from_name ("Synaptics Scrolling Distance");
        if (!prop)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        g_debug ("Trying to set %s for \"%s\"", natural_scroll ? "natural (reverse) scroll" : "normal scroll", device_info->name);

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 2,
                                 False, XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);

        if (rc == Success && type == XA_INTEGER && format == 32 && nitems >= 2) {
                ptr = (glong *) data;
                if (natural_scroll) {
                        ptr[0] = -abs(ptr[0]);
                        ptr[1] = -abs(ptr[1]);
                } else {
                        ptr[0] = abs(ptr[0]);
                        ptr[1] = abs(ptr[1]);
                }

                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop,
                                       XA_INTEGER, 32, PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting natural scroll on \"%s\"", device_info->name);
        }
}

static void
set_natural_scroll_libinput (XDeviceInfo *device_info,
                             gboolean     natural_scroll)
{
        g_debug ("Trying to set %s for \"%s\"", natural_scroll ? "natural (reverse) scroll" : "normal scroll", device_info->name);

        touchpad_set_bool (device_info, "libinput Natural Scrolling Enabled", 0, natural_scroll);
}

static void
set_natural_scroll (XDeviceInfo *device_info,
                    gboolean     natural_scroll)
{
        if (property_from_name ("Synaptics Scrolling Distance"))
                set_natural_scroll_synaptics (device_info, natural_scroll);

        if (property_from_name ("libinput Natural Scrolling Enabled"))
                set_natural_scroll_libinput (device_info, natural_scroll);
}

static void
set_natural_scroll_all (CsdMouseManager *manager)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        gboolean natural_scroll = g_settings_get_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_NATURAL_SCROLL);

        for (i = 0; i < numdevices; i++) {
                set_natural_scroll (&devicelist[i], natural_scroll);
        }

        XFreeDeviceList (devicelist);
}

static void
set_scrolling_synaptics (XDeviceInfo *device_info,
                         GSettings   *settings)
{
        touchpad_set_bool (device_info, "Synaptics Edge Scrolling", 0, g_settings_get_boolean (settings, KEY_VERT_EDGE_SCROLL));
        touchpad_set_bool (device_info, "Synaptics Edge Scrolling", 1, g_settings_get_boolean (settings, KEY_HORIZ_EDGE_SCROLL));
        touchpad_set_bool (device_info, "Synaptics Two-Finger Scrolling", 0, g_settings_get_boolean (settings, KEY_VERT_TWO_FINGER_SCROLL));
        touchpad_set_bool (device_info, "Synaptics Two-Finger Scrolling", 1, g_settings_get_boolean (settings, KEY_HORIZ_TWO_FINGER_SCROLL));
}

static void
set_scrolling_libinput (XDeviceInfo *device_info,
                        GSettings   *settings)
{
        XDevice *device;
        int format, rc;
        unsigned long nitems, bytes_after;
        unsigned char *data;
        Atom prop, type;
        gboolean want_edge, want_2fg;
        CdkDisplay *display;
        gboolean want_horiz;

        prop = property_from_name ("libinput Scroll Method Enabled");
        if (!prop)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        want_2fg = g_settings_get_boolean (settings, KEY_VERT_TWO_FINGER_SCROLL);
        want_edge = g_settings_get_boolean (settings, KEY_VERT_EDGE_SCROLL);

        /* libinput only allows for one scroll method at a time.
         * If both are set, pick 2fg scrolling.
         */
        if (want_2fg)
                want_edge = FALSE;

        g_debug ("setting scroll method on %s", device_info->name);

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        rc = XGetDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device, prop, 0, 2,
                                 False, XA_INTEGER, &type, &format, &nitems,
                                 &bytes_after, &data);

        if (rc == Success && type == XA_INTEGER && format == 8 && nitems >= 3) {
                data[0] = want_2fg;
                data[1] = want_edge;
                XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device,
                                       prop, XA_INTEGER, 8, PropModeReplace, data, nitems);
        }

        if (rc == Success)
                XFree (data);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error in setting scroll method on \"%s\"", device_info->name);
        }

        /* Horizontal scrolling is handled by xf86-input-libinput and
         * there's only one bool. Pick the one matching the scroll method
         * we picked above.
         */
        if (want_2fg)
                want_horiz = g_settings_get_boolean (settings, KEY_HORIZ_TWO_FINGER_SCROLL);
        else if (want_edge)
                want_horiz = g_settings_get_boolean (settings, KEY_HORIZ_EDGE_SCROLL);
        else
                return;

        touchpad_set_bool (device_info, "libinput Horizontal Scroll Enabled", 0, want_horiz);
}

static void
set_scrolling (XDeviceInfo *device_info,
               GSettings   *settings)
{
        if (property_from_name ("Synaptics Edge Scrolling"))
                set_scrolling_synaptics (device_info, settings);

        if (property_from_name ("libinput Scroll Method Enabled"))
                set_scrolling_libinput (device_info, settings);
}

static void
set_scrolling_all (GSettings *settings)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        for (i = 0; i < numdevices; i++) {
                set_scrolling (&devicelist[i], settings);
        }

        XFreeDeviceList (devicelist);
}

static void
set_touchpad_enabled (XDeviceInfo *device_info,
                      gboolean     state)
{
        XDevice *device;
        Atom prop_enabled;
        CdkDisplay *display;
        unsigned char data = state;

        prop_enabled = property_from_name ("Device Enabled");
        if (!prop_enabled)
                return;

        device = device_is_touchpad (device_info);
        if (device == NULL) {
                return;
        }

        display = cdk_display_get_default ();

        cdk_x11_display_error_trap_push (display);
        XChangeDeviceProperty (CDK_DISPLAY_XDISPLAY (display), device,
                               prop_enabled, XA_INTEGER, 8,
                               PropModeReplace, &data, 1);

        XCloseDevice (CDK_DISPLAY_XDISPLAY (display), device);
        cdk_display_flush (display);
        if (cdk_x11_display_error_trap_pop (display)) {
                g_warning ("Error %s device \"%s\"",
                           (state) ? "enabling" : "disabling",
                           device_info->name);
        }
}

static void
set_touchpad_enabled_all (gboolean state)
{
        int numdevices, i;
        XDeviceInfo *devicelist = XListInputDevices (CDK_DISPLAY_XDISPLAY (cdk_display_get_default ()), &numdevices);

        if (devicelist == NULL)
                return;

        for (i = 0; i < numdevices; i++) {
                set_touchpad_enabled (&devicelist[i], state);
        }

        XFreeDeviceList (devicelist);
}

static void
set_locate_pointer (CsdMouseManager *manager,
                    gboolean         state)
{
        if (state) {
                GError *error = NULL;
                char *args[2];

                if (manager->priv->locate_pointer_spawned)
                        return;

                args[0] = LIBEXECDIR "/csd_locate-pointer";
                args[1] = NULL;

                g_spawn_async (NULL, args, NULL,
                               0, NULL, NULL,
                               &manager->priv->locate_pointer_pid, &error);

                manager->priv->locate_pointer_spawned = (error == NULL);

                if (error) {
                        g_settings_set_boolean (manager->priv->settings_mouse, KEY_MOUSE_LOCATE_POINTER, FALSE);
                        g_error_free (error);
                }

        }
        else if (manager->priv->locate_pointer_spawned) {
                kill (manager->priv->locate_pointer_pid, SIGHUP);
                g_spawn_close_pid (manager->priv->locate_pointer_pid);
                manager->priv->locate_pointer_spawned = FALSE;
        }
}

#if 0   /* FIXME need to fork (?) mousetweaks for this to work */
static void
set_mousetweaks_daemon (CsdMouseManager *manager,
                        gboolean         dwell_enable,
                        gboolean         delay_enable)
{
        GError *error = NULL;
        gchar *comm;
        gboolean run_daemon = dwell_enable || delay_enable;

        if (run_daemon || manager->priv->mousetweaks_daemon_running)
                comm = g_strdup_printf ("mousetweaks %s",
                                        run_daemon ? "" : "-s");
        else
                return;

        if (run_daemon)
                manager->priv->mousetweaks_daemon_running = TRUE;


        if (! g_spawn_command_line_async (comm, &error)) {
                if (error->code == G_SPAWN_ERROR_NOENT &&
                    (dwell_enable || delay_enable)) {
                        CtkWidget *dialog;

                        GSettings *settings;
                        settings = g_settings_new (CAFE_MOUSE_A11Y_SCHEMA);
                        if (dwell_enable)
                                g_settings_set_boolean (settings,
                                                        CAFE_MOUSE_A11Y_KEY_DWELL_ENABLE,
                                                        FALSE);
                        else if (delay_enable)
                                g_settings_set_boolean (settings,
                                                        CAFE_MOUSE_A11Y_KEY_DELAY_ENABLE,
                                                        FALSE);
                        g_object_unref (settings);

                        dialog = ctk_message_dialog_new (NULL, 0,
                                                         CTK_MESSAGE_WARNING,
                                                         CTK_BUTTONS_OK,
                                                         _("Could not enable mouse accessibility features"));
                        ctk_message_dialog_format_secondary_text (CTK_MESSAGE_DIALOG (dialog),
                                                                  _("Mouse accessibility requires Mousetweaks "
                                                                    "to be installed on your system."));
                        ctk_window_set_title (CTK_WINDOW (dialog),
                                              _("Mouse Preferences"));
                        ctk_window_set_icon_name (CTK_WINDOW (dialog),
                                                  "input-mouse");
                        ctk_dialog_run (CTK_DIALOG (dialog));
                        ctk_widget_destroy (dialog);
                }
                g_error_free (error);
        }
        g_free (comm);
}
#endif  /* set_mousetweaks_daemon */

static void
set_mouse_settings (CsdMouseManager *manager)
{
        gboolean mouse_left_handed = g_settings_get_boolean (manager->priv->settings_mouse, KEY_LEFT_HANDED);
        gboolean touchpad_left_handed = get_touchpad_handedness (manager, mouse_left_handed);
        set_left_handed_all (manager, mouse_left_handed, touchpad_left_handed);

        set_motion_all (manager);
        set_middle_button_all (g_settings_get_boolean (manager->priv->settings_mouse, KEY_MIDDLE_BUTTON_EMULATION));

        set_disable_w_typing (manager, g_settings_get_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_DISABLE_W_TYPING));

        set_tap_to_click_all (manager);
        set_click_actions_all (manager);
        set_scrolling_all (manager->priv->settings_touchpad);
        set_natural_scroll_all (manager);
        set_touchpad_enabled_all (g_settings_get_boolean (manager->priv->settings_touchpad, KEY_TOUCHPAD_ENABLED));
        set_accel_profile_all (manager);
}

static void
mouse_callback (GSettings          *settings,
                const gchar        *key,
                CsdMouseManager    *manager)
{
        if (g_strcmp0 (key, KEY_LEFT_HANDED) == 0) {
                gboolean mouse_left_handed = g_settings_get_boolean (settings, key);
                gboolean touchpad_left_handed = get_touchpad_handedness (manager, mouse_left_handed);
                set_left_handed_all (manager, mouse_left_handed, touchpad_left_handed);
        } else if ((g_strcmp0 (key, KEY_MOTION_ACCELERATION) == 0)
                || (g_strcmp0 (key, KEY_MOTION_THRESHOLD) == 0)) {
                set_motion_all (manager);
        } else if (g_strcmp0 (key, KEY_ACCEL_PROFILE) == 0) {
                set_accel_profile_all (manager);
        } else if (g_strcmp0 (key, KEY_MIDDLE_BUTTON_EMULATION) == 0) {
                set_middle_button_all (g_settings_get_boolean (settings, key));
        } else if (g_strcmp0 (key, KEY_MOUSE_LOCATE_POINTER) == 0) {
                set_locate_pointer (manager, g_settings_get_boolean (settings, key));
#if 0   /* FIXME need to fork (?) mousetweaks for this to work */
        } else if (g_strcmp0 (key, KEY_MOUSE_A11Y_DWELL_ENABLE) == 0) {
                set_mousetweaks_daemon (manager,
                                        g_settings_get_boolean (settings, key),
                                        g_settings_get_boolean (settings, KEY_MOUSE_A11Y_DELAY_ENABLE, NULL));
                }
        } else if (g_strcmp0 (key, KEY_MOUSE_A11Y_DELAY_ENABLE) == 0) {
                set_mousetweaks_daemon (manager,
                                        g_settings_get_boolean (settings, KEY_MOUSE_A11Y_DWELL_ENABLE, NULL),
                                        g_settings_get_boolean (settings, key));
#endif
        }
}

static void
touchpad_callback (GSettings          *settings,
                   const gchar        *key,
                   CsdMouseManager    *manager)
{
        if (g_strcmp0 (key, KEY_TOUCHPAD_DISABLE_W_TYPING) == 0) {
                set_disable_w_typing (manager, g_settings_get_boolean (settings, key));
        } else if (g_strcmp0 (key, KEY_LEFT_HANDED) == 0) {
                gboolean mouse_left_handed = g_settings_get_boolean (manager->priv->settings_mouse, key);
                gboolean touchpad_left_handed = get_touchpad_handedness (manager, mouse_left_handed);
                set_left_handed_all (manager, mouse_left_handed, touchpad_left_handed);
        } else if ((g_strcmp0 (key, KEY_TOUCHPAD_TAP_TO_CLICK) == 0)
                || (g_strcmp0 (key, KEY_TOUCHPAD_ONE_FINGER_TAP) == 0)
                || (g_strcmp0 (key, KEY_TOUCHPAD_TWO_FINGER_TAP) == 0)
                || (g_strcmp0 (key, KEY_TOUCHPAD_THREE_FINGER_TAP) == 0)) {
                set_tap_to_click_all (manager);
        } else if ((g_strcmp0 (key, KEY_TOUCHPAD_TWO_FINGER_CLICK) == 0)
                || (g_strcmp0 (key, KEY_TOUCHPAD_THREE_FINGER_CLICK) == 0)) {
                set_click_actions_all (manager);
        } else if ((g_strcmp0 (key, KEY_VERT_EDGE_SCROLL) == 0)
                || (g_strcmp0 (key, KEY_HORIZ_EDGE_SCROLL) == 0)
                || (g_strcmp0 (key, KEY_VERT_TWO_FINGER_SCROLL) == 0)
                || (g_strcmp0 (key, KEY_HORIZ_TWO_FINGER_SCROLL) == 0)) {
                set_scrolling_all (manager->priv->settings_touchpad);
        } else if (g_strcmp0 (key, KEY_TOUCHPAD_NATURAL_SCROLL) == 0) {
                set_natural_scroll_all (manager);
        } else if (g_strcmp0 (key, KEY_TOUCHPAD_ENABLED) == 0) {
                set_touchpad_enabled_all (g_settings_get_boolean (settings, key));
        } else if ((g_strcmp0 (key, KEY_MOTION_ACCELERATION) == 0)
                || (g_strcmp0 (key, KEY_MOTION_THRESHOLD) == 0)) {
                set_motion_all (manager);
        } else if (g_strcmp0 (key, KEY_ACCEL_PROFILE) == 0) {
                set_accel_profile_all (manager);
        }
}

static void
csd_mouse_manager_init (CsdMouseManager *manager)
{
        manager->priv = csd_mouse_manager_get_instance_private (manager);
}

static gboolean
csd_mouse_manager_idle_cb (CsdMouseManager *manager)
{
        cafe_settings_profile_start (NULL);

        manager->priv->settings_mouse = g_settings_new (CAFE_MOUSE_SCHEMA);
        manager->priv->settings_touchpad = g_settings_new (CAFE_TOUCHPAD_SCHEMA);

        g_signal_connect (manager->priv->settings_mouse, "changed",
                          G_CALLBACK (mouse_callback), manager);
        g_signal_connect (manager->priv->settings_touchpad, "changed",
                          G_CALLBACK (touchpad_callback), manager);

        manager->priv->syndaemon_spawned = FALSE;

        set_devicepresence_handler (manager);

        set_mouse_settings (manager);
        set_locate_pointer (manager, g_settings_get_boolean (manager->priv->settings_mouse, KEY_MOUSE_LOCATE_POINTER));

#if 0   /* FIXME need to fork (?) mousetweaks for this to work */
        set_mousetweaks_daemon (manager,
                                g_settings_get_boolean (manager->priv->settings_mouse_a11y,
                                                        KEY_MOUSE_A11Y_DWELL_ENABLE),
                                g_settings_get_boolean (manager->priv->settings_mouse_a11y,
                                                        KEY_MOUSE_A11Y_DELAY_ENABLE));
#endif

        cafe_settings_profile_end (NULL);

        return FALSE;
}

gboolean
csd_mouse_manager_start (CsdMouseManager *manager,
                         GError         **error)
{
        cafe_settings_profile_start (NULL);

        if (!supports_xinput_devices ()) {
                g_debug ("XInput is not supported, not applying any settings");
                return TRUE;
        }

        g_idle_add ((GSourceFunc) csd_mouse_manager_idle_cb, manager);

        cafe_settings_profile_end (NULL);

        return TRUE;
}

void
csd_mouse_manager_stop (CsdMouseManager *manager)
{
        CsdMouseManagerPrivate *p = manager->priv;

        g_debug ("Stopping mouse manager");

        if (p->settings_mouse != NULL) {
                g_object_unref(p->settings_mouse);
                p->settings_mouse = NULL;
        }

        if (p->settings_touchpad != NULL) {
                g_object_unref(p->settings_touchpad);
                p->settings_touchpad = NULL;
        }

        set_locate_pointer (manager, FALSE);

        cdk_window_remove_filter (NULL, devicepresence_filter, manager);
}

static void
csd_mouse_manager_finalize (GObject *object)
{
        CsdMouseManager *mouse_manager;

        g_return_if_fail (object != NULL);
        g_return_if_fail (CSD_IS_MOUSE_MANAGER (object));

        mouse_manager = CSD_MOUSE_MANAGER (object);

        g_return_if_fail (mouse_manager->priv != NULL);

        G_OBJECT_CLASS (csd_mouse_manager_parent_class)->finalize (object);
}

CsdMouseManager *
csd_mouse_manager_new (void)
{
        if (manager_object != NULL) {
                g_object_ref (manager_object);
        } else {
                manager_object = g_object_new (CSD_TYPE_MOUSE_MANAGER, NULL);
                g_object_add_weak_pointer (manager_object,
                                           (gpointer *) &manager_object);
        }

        return CSD_MOUSE_MANAGER (manager_object);
}