Commit ac5b9bc4ee560e6280044803aee5f651d2e39048

DS 2022-09-14T18:28:35

Add support for X11 primary selection (#6132) X11 has a so-called primary selection, which you can use by marking text and middle-clicking elsewhere to copy the marked text. There are 3 new API functions in `SDL_clipboard.h`, which work exactly like their clipboard equivalents. ## Test Instructions * Run the tests (just a copy of the clipboard tests): `$ ./test/testautomation --filter Clipboard` * Build and run this small application: <details> ```C #include <SDL.h> #include <unistd.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <time.h> void print_error(const char *where) { const char *errstr = SDL_GetError(); if (errstr == NULL || errstr[0] == '\0') return; fprintf(stderr, "SDL Error after '%s': %s\n", where, errstr); SDL_ClearError(); } int main() { char text_buf[256]; srand(time(NULL)); SDL_Init(SDL_INIT_VIDEO); print_error("SDL_INIT()"); SDL_Window *window = SDL_CreateWindow("Primary Selection Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 400, 400, SDL_WINDOW_SHOWN); print_error("SDL_CreateWindow()"); SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); print_error("SDL_CreateRenderer()"); bool quit = false; unsigned int do_render = 0; while (!quit) { SDL_Event event; while (SDL_PollEvent(&event)) { print_error("SDL_PollEvent()"); switch (event.type) { case SDL_QUIT: { quit = true; break; } case SDL_KEYDOWN: { switch (event.key.keysym.sym) { case SDLK_ESCAPE: case SDLK_q: quit = true; break; case SDLK_c: snprintf(text_buf, sizeof(text_buf), "foo%d", rand()); SDL_SetClipboardText(text_buf); print_error("SDL_SetClipboardText()"); printf("clipboard: set_to=\"%s\"\n", text_buf); break; case SDLK_v: { printf("clipboard: has=%d, ", SDL_HasClipboardText()); print_error("SDL_HasClipboardText()"); char *text = SDL_GetClipboardText(); print_error("SDL_GetClipboardText()"); printf("text=\"%s\"\n", text); SDL_free(text); break; } case SDLK_d: snprintf(text_buf, sizeof(text_buf), "bar%d", rand()); SDL_SetPrimarySelectionText(text_buf); print_error("SDL_SetPrimarySelectionText()"); printf("primselec: set_to=\"%s\"\n", text_buf); break; case SDLK_f: { printf("primselec: has=%d, ", SDL_HasPrimarySelectionText()); print_error("SDL_HasPrimarySelectionText()"); char *text = SDL_GetPrimarySelectionText(); print_error("SDL_GetPrimarySelectionText()"); printf("text=\"%s\"\n", text); SDL_free(text); break; } default: break; } break; } default: { break; }} } // create less noise with WAYLAND_DEBUG=1 if (do_render == 0) { SDL_RenderPresent(renderer); print_error("SDL_RenderPresent()"); } do_render += 1; usleep(12000); } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); print_error("quit"); return 0; } ``` </details> * Use c,v,d,f to get and set the clipboard and primary selection. * Mark text and middle-click also in other applications. * For wayland under x: * `$ mutter --wayland --no-x11 --nested` * `$ XDG_SESSION_TYPE=wayland SDL_VIDEODRIVER=wayland ./<path_to_test_appl_binary>`

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
diff --git a/include/SDL_clipboard.h b/include/SDL_clipboard.h
index 9351363..fbe5771 100644
--- a/include/SDL_clipboard.h
+++ b/include/SDL_clipboard.h
@@ -82,6 +82,52 @@ extern DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
  */
 extern DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText(void);
 
+/**
+ * Put UTF-8 text into the primary selection.
+ *
+ * \param text the text to store in the primary selection
+ * \returns 0 on success or a negative error code on failure; call
+ *          SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 2.25.0.
+ *
+ * \sa SDL_GetPrimarySelectionText
+ * \sa SDL_HasPrimarySelectionText
+ */
+extern DECLSPEC int SDLCALL SDL_SetPrimarySelectionText(const char *text);
+
+/**
+ * Get UTF-8 text from the primary selection, which must be freed with SDL_free().
+ *
+ * This functions returns empty string if there was not enough memory left for
+ * a copy of the primary selection's content.
+ *
+ * \returns the primary selection text on success or an empty string on failure;
+ *          call SDL_GetError() for more information. Caller must call SDL_free()
+ *          on the returned pointer when done with it (even if there was an
+ *          error).
+ *
+ * \since This function is available since SDL 2.25.0.
+ *
+ * \sa SDL_HasPrimarySelectionText
+ * \sa SDL_SetPrimarySelectionText
+ */
+extern DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void);
+
+/**
+ * Query whether the primary selection exists and contains a non-empty text
+ * string.
+ *
+ * \returns SDL_TRUE if the primary selection has text, or SDL_FALSE if it does
+ *          not.
+ *
+ * \since This function is available since SDL 2.25.0.
+ *
+ * \sa SDL_GetPrimarySelectionText
+ * \sa SDL_SetPrimarySelectionText
+ */
+extern DECLSPEC SDL_bool SDLCALL SDL_HasPrimarySelectionText(void);
+
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
diff --git a/include/SDL_events.h b/include/SDL_events.h
index c0fc9bb..1836fc0 100644
--- a/include/SDL_events.h
+++ b/include/SDL_events.h
@@ -143,7 +143,7 @@ typedef enum
     SDL_MULTIGESTURE,
 
     /* Clipboard events */
-    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
+    SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard or primary selection changed */
 
     /* Drag and drop events */
     SDL_DROPFILE        = 0x1000, /**< The system requests a file open */
diff --git a/src/dynapi/SDL2.exports b/src/dynapi/SDL2.exports
index d25b8f7..d9e3545 100644
--- a/src/dynapi/SDL2.exports
+++ b/src/dynapi/SDL2.exports
@@ -860,3 +860,6 @@
 ++'_SDL_crc16'.'SDL2.dll'.'SDL_crc16'
 ++'_SDL_GetWindowSizeInPixels'.'SDL2.dll'.'SDL_GetWindowSizeInPixels'
 ++'_SDL_GetJoystickGUIDInfo'.'SDL2.dll'.'SDL_GetJoystickGUIDInfo'
+++'_SDL_SetPrimarySelectionText'.'SDL2.dll'.'SDL_SetPrimarySelectionText'
+++'_SDL_GetPrimarySelectionText'.'SDL2.dll'.'SDL_GetPrimarySelectionText'
+++'_SDL_HasPrimarySelectionText'.'SDL2.dll'.'SDL_HasPrimarySelectionText'
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 4b68f1a..2e982b8 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -886,3 +886,6 @@
 #define SDL_crc16 SDL_crc16_REAL
 #define SDL_GetWindowSizeInPixels SDL_GetWindowSizeInPixels_REAL
 #define SDL_GetJoystickGUIDInfo SDL_GetJoystickGUIDInfo_REAL
+#define SDL_SetPrimarySelectionText SDL_SetPrimarySelectionText_REAL
+#define SDL_GetPrimarySelectionText SDL_GetPrimarySelectionText_REAL
+#define SDL_HasPrimarySelectionText SDL_HasPrimarySelectionText_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index b8ecadb..b40efff 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -969,3 +969,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_ResetHint,(const char *a),(a),return)
 SDL_DYNAPI_PROC(Uint16,SDL_crc16,(Uint16 a, const void *b, size_t c),(a,b,c),return)
 SDL_DYNAPI_PROC(void,SDL_GetWindowSizeInPixels,(SDL_Window *a, int *b, int *c),(a,b,c),)
 SDL_DYNAPI_PROC(void,SDL_GetJoystickGUIDInfo,(SDL_JoystickGUID a, Uint16 *b, Uint16 *c, Uint16 *d, Uint16 *e),(a,b,c,d,e),)
+SDL_DYNAPI_PROC(int,SDL_SetPrimarySelectionText,(const char *a),(a),return)
+SDL_DYNAPI_PROC(char*,SDL_GetPrimarySelectionText,(void),(),return)
+SDL_DYNAPI_PROC(SDL_bool,SDL_HasPrimarySelectionText,(void),(),return)
diff --git a/src/video/SDL_clipboard.c b/src/video/SDL_clipboard.c
index c3669af..bee570d 100644
--- a/src/video/SDL_clipboard.c
+++ b/src/video/SDL_clipboard.c
@@ -45,6 +45,27 @@ SDL_SetClipboardText(const char *text)
     }
 }
 
+int
+SDL_SetPrimarySelectionText(const char *text)
+{
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+
+    if (!_this) {
+        return SDL_SetError("Video subsystem must be initialized to set primary selection text");
+    }
+
+    if (!text) {
+        text = "";
+    }
+    if (_this->SetPrimarySelectionText) {
+        return _this->SetPrimarySelectionText(_this, text);
+    } else {
+        SDL_free(_this->primary_selection_text);
+        _this->primary_selection_text = SDL_strdup(text);
+        return 0;
+    }
+}
+
 char *
 SDL_GetClipboardText(void)
 {
@@ -66,6 +87,27 @@ SDL_GetClipboardText(void)
     }
 }
 
+char *
+SDL_GetPrimarySelectionText(void)
+{
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+
+    if (!_this) {
+        SDL_SetError("Video subsystem must be initialized to get primary selection text");
+        return SDL_strdup("");
+    }
+
+    if (_this->GetPrimarySelectionText) {
+        return _this->GetPrimarySelectionText(_this);
+    } else {
+        const char *text = _this->primary_selection_text;
+        if (!text) {
+            text = "";
+        }
+        return SDL_strdup(text);
+    }
+}
+
 SDL_bool
 SDL_HasClipboardText(void)
 {
@@ -87,4 +129,26 @@ SDL_HasClipboardText(void)
     }
 }
 
+SDL_bool
+SDL_HasPrimarySelectionText(void)
+{
+    SDL_VideoDevice *_this = SDL_GetVideoDevice();
+
+    if (!_this) {
+        SDL_SetError("Video subsystem must be initialized to check primary selection text");
+        return SDL_FALSE;
+    }
+
+    if (_this->HasPrimarySelectionText) {
+        return _this->HasPrimarySelectionText(_this);
+    } else {
+        if (_this->primary_selection_text && _this->primary_selection_text[0] != '\0') {
+            return SDL_TRUE;
+        } else {
+            return SDL_FALSE;
+        }
+    }
+    return SDL_FALSE;
+}
+
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index d3f0d83..4f602d3 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -328,6 +328,9 @@ struct SDL_VideoDevice
     int (*SetClipboardText) (_THIS, const char *text);
     char * (*GetClipboardText) (_THIS);
     SDL_bool (*HasClipboardText) (_THIS);
+    int (*SetPrimarySelectionText) (_THIS, const char *text);
+    char * (*GetPrimarySelectionText) (_THIS);
+    SDL_bool (*HasPrimarySelectionText) (_THIS);
 
     /* MessageBox */
     int (*ShowMessageBox) (_THIS, const SDL_MessageBoxData *messageboxdata, int *buttonid);
@@ -353,6 +356,7 @@ struct SDL_VideoDevice
     Uint8 window_magic;
     Uint32 next_object_id;
     char *clipboard_text;
+    char *primary_selection_text;
     SDL_bool setting_display_mode;
     Uint32 quirk_flags;
 
@@ -422,11 +426,11 @@ struct SDL_VideoDevice
     /* Data private to this driver */
     void *driverdata;
     struct SDL_GLDriverData *gl_data;
-    
+
 #if SDL_VIDEO_OPENGL_EGL
     struct SDL_EGL_VideoData *egl_data;
 #endif
-    
+
 #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
     struct SDL_PrivateGLESData *gles_data;
 #endif
diff --git a/src/video/wayland/SDL_waylandclipboard.c b/src/video/wayland/SDL_waylandclipboard.c
index ede016c..ff19639 100644
--- a/src/video/wayland/SDL_waylandclipboard.c
+++ b/src/video/wayland/SDL_waylandclipboard.c
@@ -58,6 +58,39 @@ Wayland_SetClipboardText(_THIS, const char *text)
     return status;
 }
 
+int
+Wayland_SetPrimarySelectionText(_THIS, const char *text)
+{
+    SDL_VideoData *video_data = NULL;
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
+
+    int status = 0;
+
+    if (_this == NULL || _this->driverdata == NULL) {
+        status = SDL_SetError("Video driver uninitialized");
+    } else {
+        video_data = _this->driverdata;
+        if (video_data->input != NULL && video_data->input->primary_selection_device != NULL) {
+            primary_selection_device = video_data->input->primary_selection_device;
+            if (text[0] != '\0') {
+                SDL_WaylandPrimarySelectionSource* source = Wayland_primary_selection_source_create(_this);
+                Wayland_primary_selection_source_add_data(source, TEXT_MIME, text,
+                                                          SDL_strlen(text));
+
+                status = Wayland_primary_selection_device_set_selection(primary_selection_device,
+                                                                        source);
+                if (status != 0) {
+                    Wayland_primary_selection_source_destroy(source);
+                }
+            } else {
+                status = Wayland_primary_selection_device_clear_selection(primary_selection_device);
+            }
+        }
+    }
+
+    return status;
+}
+
 char *
 Wayland_GetClipboardText(_THIS)
 {
@@ -65,8 +98,6 @@ Wayland_GetClipboardText(_THIS)
     SDL_WaylandDataDevice *data_device = NULL;
 
     char *text = NULL;
-
-    void *buffer = NULL;
     size_t length = 0;
 
     if (_this == NULL || _this->driverdata == NULL) {
@@ -75,19 +106,50 @@ Wayland_GetClipboardText(_THIS)
         video_data = _this->driverdata;
         if (video_data->input != NULL && video_data->input->data_device != NULL) {
             data_device = video_data->input->data_device;
-            if (data_device->selection_offer != NULL) {
-                buffer = Wayland_data_offer_receive(data_device->selection_offer,
+            /* Prefer own selection, if not canceled */
+            if (Wayland_data_source_has_mime(
+                    data_device->selection_source, TEXT_MIME)) {
+                text = Wayland_data_source_get_data(data_device->selection_source,
                                                     &length, TEXT_MIME, SDL_TRUE);
-                if (length > 0) {
-                    text = (char*) buffer;
-                }
+            } else if (Wayland_data_offer_has_mime(
+                    data_device->selection_offer, TEXT_MIME)) {
+                text = Wayland_data_offer_receive(data_device->selection_offer,
+                                                  &length, TEXT_MIME, SDL_TRUE);
             }
-            if (length == 0 && data_device->selection_source != NULL) {
-                buffer = Wayland_data_source_get_data(data_device->selection_source,
-                                                      &length, TEXT_MIME, SDL_TRUE);
-                if (length > 0) {
-                    text = (char*) buffer;
-                }
+        }
+    }
+
+    if (text == NULL) {
+        text = SDL_strdup("");
+    }
+
+    return text;
+}
+
+char *
+Wayland_GetPrimarySelectionText(_THIS)
+{
+    SDL_VideoData *video_data = NULL;
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
+
+    char *text = NULL;
+    size_t length = 0;
+
+    if (_this == NULL || _this->driverdata == NULL) {
+        SDL_SetError("Video driver uninitialized");
+    } else {
+        video_data = _this->driverdata;
+        if (video_data->input != NULL && video_data->input->primary_selection_device != NULL) {
+            primary_selection_device = video_data->input->primary_selection_device;
+            /* Prefer own selection, if not canceled */
+            if (Wayland_primary_selection_source_has_mime(
+                    primary_selection_device->selection_source, TEXT_MIME)) {
+                text = Wayland_primary_selection_source_get_data(primary_selection_device->selection_source,
+                                                                 &length, TEXT_MIME, SDL_TRUE);
+            } else if (Wayland_primary_selection_offer_has_mime(
+                    primary_selection_device->selection_offer, TEXT_MIME)) {
+                text = Wayland_primary_selection_offer_receive(primary_selection_device->selection_offer,
+                                                               &length, TEXT_MIME, SDL_TRUE);
             }
         }
     }
@@ -112,13 +174,32 @@ Wayland_HasClipboardText(_THIS)
         video_data = _this->driverdata;
         if (video_data->input != NULL && video_data->input->data_device != NULL) {
             data_device = video_data->input->data_device;
-            if (Wayland_data_offer_has_mime(
-                    data_device->selection_offer, TEXT_MIME)) {
-                result = SDL_TRUE;
-            } else if (Wayland_data_source_has_mime(
-                    data_device->selection_source, TEXT_MIME)) {
-                result = SDL_TRUE;
-            }
+            result = result ||
+                     Wayland_data_source_has_mime(data_device->selection_source, TEXT_MIME) ||
+                     Wayland_data_offer_has_mime(data_device->selection_offer, TEXT_MIME);
+        }
+    }
+    return result;
+}
+
+SDL_bool
+Wayland_HasPrimarySelectionText(_THIS)
+{
+    SDL_VideoData *video_data = NULL;
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
+
+    SDL_bool result = SDL_FALSE;
+    if (_this == NULL || _this->driverdata == NULL) {
+        SDL_SetError("Video driver uninitialized");
+    } else {
+        video_data = _this->driverdata;
+        if (video_data->input != NULL && video_data->input->primary_selection_device != NULL) {
+            primary_selection_device = video_data->input->primary_selection_device;
+            result = result ||
+                     Wayland_primary_selection_source_has_mime(
+                         primary_selection_device->selection_source, TEXT_MIME) ||
+                     Wayland_primary_selection_offer_has_mime(
+                         primary_selection_device->selection_offer, TEXT_MIME);
         }
     }
     return result;
diff --git a/src/video/wayland/SDL_waylandclipboard.h b/src/video/wayland/SDL_waylandclipboard.h
index b30e160..b0e443d 100644
--- a/src/video/wayland/SDL_waylandclipboard.h
+++ b/src/video/wayland/SDL_waylandclipboard.h
@@ -26,6 +26,9 @@
 extern int Wayland_SetClipboardText(_THIS, const char *text);
 extern char *Wayland_GetClipboardText(_THIS);
 extern SDL_bool Wayland_HasClipboardText(_THIS);
+extern int Wayland_SetPrimarySelectionText(_THIS, const char *text);
+extern char *Wayland_GetPrimarySelectionText(_THIS);
+extern SDL_bool Wayland_HasPrimarySelectionText(_THIS);
 
 #endif /* SDL_waylandclipboard_h_ */
 
diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c
index e61ba45..63e9d48 100644
--- a/src/video/wayland/SDL_waylanddatamanager.c
+++ b/src/video/wayland/SDL_waylanddatamanager.c
@@ -33,11 +33,12 @@
 
 #include "SDL_waylandvideo.h"
 #include "SDL_waylanddatamanager.h"
+#include "primary-selection-unstable-v1-client-protocol.h"
 
 /* FIXME: This is arbitrary, but we want this to be less than a frame because
  * any longer can potentially spin an infinite loop of PumpEvents (!)
  */
-#define PIPE_MS_TIMEOUT 10
+#define PIPE_MS_TIMEOUT 14
 
 static ssize_t
 write_pipe(int fd, const void* buffer, size_t total_length, size_t *pos)
@@ -53,7 +54,7 @@ write_pipe(int fd, const void* buffer, size_t total_length, size_t *pos)
     ready = SDL_IOReady(fd, SDL_IOR_WRITE, PIPE_MS_TIMEOUT);
 
     sigemptyset(&sig_set);
-    sigaddset(&sig_set, SIGPIPE);  
+    sigaddset(&sig_set, SIGPIPE);
 
 #if SDL_THREADS_DISABLED
     sigprocmask(SIG_BLOCK, &sig_set, &old_sig_set);
@@ -97,7 +98,7 @@ read_pipe(int fd, void** buffer, size_t* total_length, SDL_bool null_terminate)
     size_t pos = 0;
 
     ready = SDL_IOReady(fd, SDL_IOR_READ, PIPE_MS_TIMEOUT);
-  
+
     if (ready == 0) {
         bytes_read = SDL_SetError("Pipe timeout");
     } else if (ready < 0) {
@@ -120,8 +121,8 @@ read_pipe(int fd, void** buffer, size_t* total_length, SDL_bool null_terminate)
             output_buffer = SDL_malloc(new_buffer_length);
         } else {
             output_buffer = SDL_realloc(*buffer, new_buffer_length);
-        }           
-        
+        }
+
         if (output_buffer == NULL) {
             bytes_read = SDL_OutOfMemory();
         } else {
@@ -130,7 +131,7 @@ read_pipe(int fd, void** buffer, size_t* total_length, SDL_bool null_terminate)
             if (null_terminate == SDL_TRUE) {
                 SDL_memset((Uint8*)output_buffer + (new_buffer_length - 1), 0, 1);
             }
-            
+
             *buffer = output_buffer;
         }
     }
@@ -160,28 +161,28 @@ Wayland_convert_mime_type(const char *mime_type)
             break;
         }
     }
-    
+
     return found;
 }
 
 static SDL_MimeDataList*
-mime_data_list_find(struct wl_list* list, 
+mime_data_list_find(struct wl_list* list,
                     const char* mime_type)
 {
     SDL_MimeDataList *found = NULL;
 
     SDL_MimeDataList *mime_list = NULL;
-    wl_list_for_each(mime_list, list, link) { 
+    wl_list_for_each(mime_list, list, link) {
         if (SDL_strcmp(mime_list->mime_type, mime_type) == 0) {
             found = mime_list;
             break;
         }
-    }    
+    }
     return found;
 }
 
 static int
-mime_data_list_add(struct wl_list* list, 
+mime_data_list_add(struct wl_list* list,
                    const char* mime_type,
                    const void* buffer, size_t length)
 {
@@ -216,7 +217,7 @@ mime_data_list_add(struct wl_list* list,
             }
         }
     }
-    
+
     if (mime_data != NULL && buffer != NULL && length > 0) {
         if (mime_data->data != NULL) {
             SDL_free(mime_data->data);
@@ -233,31 +234,25 @@ mime_data_list_add(struct wl_list* list,
 static void
 mime_data_list_free(struct wl_list *list)
 {
-    SDL_MimeDataList *mime_data = NULL; 
+    SDL_MimeDataList *mime_data = NULL;
     SDL_MimeDataList *next = NULL;
 
     wl_list_for_each_safe(mime_data, next, list, link) {
         if (mime_data->data != NULL) {
             SDL_free(mime_data->data);
-        }        
+        }
         if (mime_data->mime_type != NULL) {
             SDL_free(mime_data->mime_type);
         }
-        SDL_free(mime_data);       
-    } 
+        SDL_free(mime_data);
+    }
 }
 
-ssize_t 
-Wayland_data_source_send(SDL_WaylandDataSource *source,  
-                         const char *mime_type, int fd)
+static ssize_t
+Wayland_source_send(SDL_MimeDataList *mime_data, const char *mime_type, int fd)
 {
     size_t written_bytes = 0;
     ssize_t status = 0;
-    SDL_MimeDataList *mime_data = NULL;
- 
-    mime_type = Wayland_convert_mime_type(mime_type);
-    mime_data = mime_data_list_find(&source->mimes,
-                                                      mime_type);
 
     if (mime_data == NULL || mime_data->data == NULL) {
         status = SDL_SetError("Invalid mime type");
@@ -271,15 +266,49 @@ Wayland_data_source_send(SDL_WaylandDataSource *source,
     return status;
 }
 
+ssize_t
+Wayland_data_source_send(SDL_WaylandDataSource *source,
+                         const char *mime_type, int fd)
+{
+    SDL_MimeDataList *mime_data = NULL;
+
+    mime_type = Wayland_convert_mime_type(mime_type);
+    mime_data = mime_data_list_find(&source->mimes,
+                                                      mime_type);
+
+    return Wayland_source_send(mime_data, mime_type, fd);
+}
+
+ssize_t
+Wayland_primary_selection_source_send(SDL_WaylandPrimarySelectionSource *source,
+                                      const char *mime_type, int fd)
+{
+    SDL_MimeDataList *mime_data = NULL;
+
+    mime_type = Wayland_convert_mime_type(mime_type);
+    mime_data = mime_data_list_find(&source->mimes,
+                                                      mime_type);
+
+    return Wayland_source_send(mime_data, mime_type, fd);
+}
+
 int Wayland_data_source_add_data(SDL_WaylandDataSource *source,
                                  const char *mime_type,
                                  const void *buffer,
-                                 size_t length) 
+                                 size_t length)
+{
+    return mime_data_list_add(&source->mimes, mime_type, buffer, length);
+}
+
+int Wayland_primary_selection_source_add_data(SDL_WaylandPrimarySelectionSource *source,
+                                              const char *mime_type,
+                                              const void *buffer,
+                                              size_t length)
 {
     return mime_data_list_add(&source->mimes, mime_type, buffer, length);
 }
 
-SDL_bool 
+SDL_bool
 Wayland_data_source_has_mime(SDL_WaylandDataSource *source,
                              const char *mime_type)
 {
@@ -291,7 +320,47 @@ Wayland_data_source_has_mime(SDL_WaylandDataSource *source,
     return found;
 }
 
-void* 
+SDL_bool
+Wayland_primary_selection_source_has_mime(SDL_WaylandPrimarySelectionSource *source,
+                                          const char *mime_type)
+{
+    SDL_bool found = SDL_FALSE;
+
+    if (source != NULL) {
+        found = mime_data_list_find(&source->mimes, mime_type) != NULL;
+    }
+    return found;
+}
+
+static void*
+Wayland_source_get_data(SDL_MimeDataList *mime_data,
+                        size_t *length,
+                        SDL_bool null_terminate)
+{
+    void *buffer = NULL;
+
+    if (mime_data != NULL && mime_data->length > 0) {
+        size_t buffer_length = mime_data->length;
+
+        if (null_terminate == SDL_TRUE) {
+            ++buffer_length;
+        }
+        buffer = SDL_malloc(buffer_length);
+        if (buffer == NULL) {
+            *length = SDL_OutOfMemory();
+        } else {
+            *length = mime_data->length;
+            SDL_memcpy(buffer, mime_data->data, mime_data->length);
+            if (null_terminate) {
+                *((Uint8 *)buffer + mime_data->length) = 0;
+            }
+        }
+    }
+
+    return buffer;
+}
+
+void*
 Wayland_data_source_get_data(SDL_WaylandDataSource *source,
                              size_t *length, const char* mime_type,
                              SDL_bool null_terminate)
@@ -304,23 +373,26 @@ Wayland_data_source_get_data(SDL_WaylandDataSource *source,
         SDL_SetError("Invalid data source");
     } else {
         mime_data = mime_data_list_find(&source->mimes, mime_type);
-        if (mime_data != NULL && mime_data->length > 0) {
-            size_t buffer_length = mime_data->length;
+        buffer = Wayland_source_get_data(mime_data, length, null_terminate);
+    }
 
-            if (null_terminate == SDL_TRUE) {
-                ++buffer_length;
-            }
-            buffer = SDL_malloc(buffer_length);
-            if (buffer == NULL) {
-                *length = SDL_OutOfMemory();
-            } else {
-                *length = mime_data->length;
-                SDL_memcpy(buffer, mime_data->data, mime_data->length);
-                if (null_terminate) {
-                    *((Uint8 *)buffer + mime_data->length) = 0;
-                }
-            }
-        }
+    return buffer;
+}
+
+void*
+Wayland_primary_selection_source_get_data(SDL_WaylandPrimarySelectionSource *source,
+                                          size_t *length, const char* mime_type,
+                                          SDL_bool null_terminate)
+{
+    SDL_MimeDataList *mime_data = NULL;
+    void *buffer = NULL;
+    *length = 0;
+
+    if (source == NULL) {
+        SDL_SetError("Invalid primary selection source");
+    } else {
+        mime_data = mime_data_list_find(&source->mimes, mime_type);
+        buffer = Wayland_source_get_data(mime_data, length, null_terminate);
     }
 
     return buffer;
@@ -340,13 +412,27 @@ Wayland_data_source_destroy(SDL_WaylandDataSource *source)
     }
 }
 
-void* 
+void
+Wayland_primary_selection_source_destroy(SDL_WaylandPrimarySelectionSource *source)
+{
+    if (source != NULL) {
+        SDL_WaylandPrimarySelectionDevice *primary_selection_device = (SDL_WaylandPrimarySelectionDevice *) source->primary_selection_device;
+        if (primary_selection_device && (primary_selection_device->selection_source == source)) {
+            primary_selection_device->selection_source = NULL;
+        }
+        zwp_primary_selection_source_v1_destroy(source->source);
+        mime_data_list_free(&source->mimes);
+        SDL_free(source);
+    }
+}
+
+void*
 Wayland_data_offer_receive(SDL_WaylandDataOffer *offer,
                            size_t *length, const char* mime_type,
                            SDL_bool null_terminate)
 {
     SDL_WaylandDataDevice *data_device = NULL;
- 
+
     int pipefd[2];
     void *buffer = NULL;
     *length = 0;
@@ -364,22 +450,59 @@ Wayland_data_offer_receive(SDL_WaylandDataOffer *offer,
         WAYLAND_wl_display_flush(data_device->video_data->display);
 
         close(pipefd[1]);
-        
+
         while (read_pipe(pipefd[0], &buffer, length, null_terminate) > 0);
         close(pipefd[0]);
     }
     return buffer;
 }
 
-int 
+void*
+Wayland_primary_selection_offer_receive(SDL_WaylandPrimarySelectionOffer *offer,
+                                        size_t *length, const char* mime_type,
+                                        SDL_bool null_terminate)
+{
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
+
+    int pipefd[2];
+    void *buffer = NULL;
+    *length = 0;
+
+    if (offer == NULL) {
+        SDL_SetError("Invalid data offer");
+    } else if ((primary_selection_device = offer->primary_selection_device) == NULL) {
+        SDL_SetError("Primary selection device not initialized");
+    } else if (pipe2(pipefd, O_CLOEXEC|O_NONBLOCK) == -1) {
+        SDL_SetError("Could not read pipe");
+    } else {
+        zwp_primary_selection_offer_v1_receive(offer->offer, mime_type, pipefd[1]);
+
+        /* TODO: Needs pump and flush? */
+        WAYLAND_wl_display_flush(primary_selection_device->video_data->display);
+
+        close(pipefd[1]);
+
+        while (read_pipe(pipefd[0], &buffer, length, null_terminate) > 0);
+        close(pipefd[0]);
+    }
+    return buffer;
+}
+
+int
 Wayland_data_offer_add_mime(SDL_WaylandDataOffer *offer,
                             const char* mime_type)
 {
     return mime_data_list_add(&offer->mimes, mime_type, NULL, 0);
 }
 
+int
+Wayland_primary_selection_offer_add_mime(SDL_WaylandPrimarySelectionOffer *offer,
+                                         const char* mime_type)
+{
+    return mime_data_list_add(&offer->mimes, mime_type, NULL, 0);
+}
 
-SDL_bool 
+SDL_bool
 Wayland_data_offer_has_mime(SDL_WaylandDataOffer *offer,
                             const char *mime_type)
 {
@@ -391,6 +514,18 @@ Wayland_data_offer_has_mime(SDL_WaylandDataOffer *offer,
     return found;
 }
 
+SDL_bool
+Wayland_primary_selection_offer_has_mime(SDL_WaylandPrimarySelectionOffer *offer,
+                                         const char *mime_type)
+{
+    SDL_bool found = SDL_FALSE;
+
+    if (offer != NULL) {
+        found = mime_data_list_find(&offer->mimes, mime_type) != NULL;
+    }
+    return found;
+}
+
 void
 Wayland_data_offer_destroy(SDL_WaylandDataOffer *offer)
 {
@@ -401,6 +536,16 @@ Wayland_data_offer_destroy(SDL_WaylandDataOffer *offer)
     }
 }
 
+void
+Wayland_primary_selection_offer_destroy(SDL_WaylandPrimarySelectionOffer *offer)
+{
+    if (offer != NULL) {
+        zwp_primary_selection_offer_v1_destroy(offer->offer);
+        mime_data_list_free(&offer->mimes);
+        SDL_free(offer);
+    }
+}
+
 int
 Wayland_data_device_clear_selection(SDL_WaylandDataDevice *data_device)
 {
@@ -417,6 +562,22 @@ Wayland_data_device_clear_selection(SDL_WaylandDataDevice *data_device)
 }
 
 int
+Wayland_primary_selection_device_clear_selection(SDL_WaylandPrimarySelectionDevice *primary_selection_device)
+{
+    int status = 0;
+
+    if (primary_selection_device == NULL || primary_selection_device->primary_selection_device == NULL) {
+        status = SDL_SetError("Invalid Primary Selection Device");
+    } else if (primary_selection_device->selection_source != NULL) {
+        zwp_primary_selection_device_v1_set_selection(primary_selection_device->primary_selection_device,
+                                                      NULL, 0);
+        Wayland_primary_selection_source_destroy(primary_selection_device->selection_source);
+        primary_selection_device->selection_source = NULL;
+    }
+    return status;
+}
+
+int
 Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
                                   SDL_WaylandDataSource *source)
 {
@@ -433,7 +594,7 @@ Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
 
         wl_list_for_each(mime_data, &(source->mimes), link) {
             wl_data_source_offer(source->source,
-                                 mime_data->mime_type); 
+                                 mime_data->mime_type);
 
             /* TODO - Improve system for multiple mime types to same data */
             for (index = 0; index < MIME_LIST_SIZE; ++index) {
@@ -443,9 +604,9 @@ Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
                }
             }
             /* */
- 
+
             ++num_offers;
-        } 
+        }
 
         if (num_offers == 0) {
             Wayland_data_device_clear_selection(data_device);
@@ -455,7 +616,7 @@ Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
             if (data_device->selection_serial != 0) {
                 wl_data_device_set_selection(data_device->data_device,
                                              source->source,
-                                             data_device->selection_serial); 
+                                             data_device->selection_serial);
             }
             if (data_device->selection_source != NULL) {
                 Wayland_data_source_destroy(data_device->selection_source);
@@ -469,6 +630,58 @@ Wayland_data_device_set_selection(SDL_WaylandDataDevice *data_device,
 }
 
 int
+Wayland_primary_selection_device_set_selection(SDL_WaylandPrimarySelectionDevice *primary_selection_device,
+                                              SDL_WaylandPrimarySelectionSource *source)
+{
+    int status = 0;
+    size_t num_offers = 0;
+    size_t index = 0;
+
+    if (primary_selection_device == NULL) {
+        status = SDL_SetError("Invalid Primary Selection Device");
+    } else if (source == NULL) {
+        status = SDL_SetError("Invalid source");
+    } else {
+        SDL_MimeDataList *mime_data = NULL;
+
+        wl_list_for_each(mime_data, &(source->mimes), link) {
+            zwp_primary_selection_source_v1_offer(source->source,
+                                                  mime_data->mime_type);
+
+            /* TODO - Improve system for multiple mime types to same data */
+            for (index = 0; index < MIME_LIST_SIZE; ++index) {
+                if (SDL_strcmp(mime_conversion_list[index][1], mime_data->mime_type) == 0) {
+                zwp_primary_selection_source_v1_offer(source->source,
+                                                      mime_conversion_list[index][0]);
+               }
+            }
+            /* */
+
+            ++num_offers;
+        }
+
+        if (num_offers == 0) {
+            Wayland_primary_selection_device_clear_selection(primary_selection_device);
+            status = SDL_SetError("No mime data");
+        } else {
+            /* Only set if there is a valid serial if not set it later */
+            if (primary_selection_device->selection_serial != 0) {
+                zwp_primary_selection_device_v1_set_selection(primary_selection_device->primary_selection_device,
+                                                              source->source,
+                                                              primary_selection_device->selection_serial);
+            }
+            if (primary_selection_device->selection_source != NULL) {
+                Wayland_primary_selection_source_destroy(primary_selection_device->selection_source);
+            }
+            primary_selection_device->selection_source = source;
+            source->primary_selection_device = primary_selection_device;
+        }
+    }
+
+    return status;
+}
+
+int
 Wayland_data_device_set_serial(SDL_WaylandDataDevice *data_device,
                                uint32_t serial)
 {
@@ -481,13 +694,35 @@ Wayland_data_device_set_serial(SDL_WaylandDataDevice *data_device,
             && data_device->selection_source != NULL) {
             wl_data_device_set_selection(data_device->data_device,
                                          data_device->selection_source->source,
-                                         serial); 
+                                         data_device->selection_serial);
         }
 
         data_device->selection_serial = serial;
     }
 
-    return status; 
+    return status;
+}
+
+int
+Wayland_primary_selection_device_set_serial(SDL_WaylandPrimarySelectionDevice *primary_selection_device,
+                                            uint32_t serial)
+{
+    int status = -1;
+    if (primary_selection_device != NULL) {
+        status = 0;
+
+        /* If there was no serial and there is a pending selection set it now. */
+        if (primary_selection_device->selection_serial == 0
+            && primary_selection_device->selection_source != NULL) {
+            zwp_primary_selection_device_v1_set_selection(primary_selection_device->primary_selection_device,
+                                                          primary_selection_device->selection_source->source,
+                                                          primary_selection_device->selection_serial);
+        }
+
+        primary_selection_device->selection_serial = serial;
+    }
+
+    return status;
 }
 
 #endif /* SDL_VIDEO_DRIVER_WAYLAND */
diff --git a/src/video/wayland/SDL_waylanddatamanager.h b/src/video/wayland/SDL_waylanddatamanager.h
index 0030a39..3e1aa03 100644
--- a/src/video/wayland/SDL_waylanddatamanager.h
+++ b/src/video/wayland/SDL_waylanddatamanager.h
@@ -44,12 +44,24 @@ typedef struct {
 } SDL_WaylandDataSource;
 
 typedef struct {
+    struct zwp_primary_selection_source_v1 *source;
+    struct wl_list mimes;
+    void *primary_selection_device;
+} SDL_WaylandPrimarySelectionSource;
+
+typedef struct {
     struct wl_data_offer *offer;
     struct wl_list mimes;
     void *data_device;
 } SDL_WaylandDataOffer;
 
 typedef struct {
+    struct zwp_primary_selection_offer_v1 *offer;
+    struct wl_list mimes;
+    void *primary_selection_device;
+} SDL_WaylandPrimarySelectionOffer;
+
+typedef struct {
     struct wl_data_device *data_device;
     SDL_VideoData *video_data;
 
@@ -58,46 +70,83 @@ typedef struct {
     SDL_WaylandDataOffer *drag_offer;
     SDL_WaylandDataOffer *selection_offer;
 
-    /* Clipboard */
+    /* Clipboard and Primary Selection */
     uint32_t selection_serial;
     SDL_WaylandDataSource *selection_source;
 } SDL_WaylandDataDevice;
 
+typedef struct {
+    struct zwp_primary_selection_device_v1 *primary_selection_device;
+    SDL_VideoData *video_data;
+
+    uint32_t selection_serial;
+    SDL_WaylandPrimarySelectionSource *selection_source;
+    SDL_WaylandPrimarySelectionOffer *selection_offer;
+} SDL_WaylandPrimarySelectionDevice;
+
 extern const char* Wayland_convert_mime_type(const char *mime_type);
 
-/* Wayland Data Source - (Sending) */
+/* Wayland Data Source / Primary Selection Source - (Sending) */
 extern SDL_WaylandDataSource* Wayland_data_source_create(_THIS);
-extern ssize_t Wayland_data_source_send(SDL_WaylandDataSource *source, 
+extern SDL_WaylandPrimarySelectionSource* Wayland_primary_selection_source_create(_THIS);
+extern ssize_t Wayland_data_source_send(SDL_WaylandDataSource *source,
                                         const char *mime_type, int fd);
+extern ssize_t Wayland_primary_selection_source_send(SDL_WaylandPrimarySelectionSource *source,
+                                                     const char *mime_type, int fd);
 extern int Wayland_data_source_add_data(SDL_WaylandDataSource *source,
-                                        const char *mime_type, 
-                                        const void *buffer, 
+                                        const char *mime_type,
+                                        const void *buffer,
                                         size_t length);
+extern int Wayland_primary_selection_source_add_data(SDL_WaylandPrimarySelectionSource *source,
+                                                     const char *mime_type,
+                                                     const void *buffer,
+                                                     size_t length);
 extern SDL_bool Wayland_data_source_has_mime(SDL_WaylandDataSource *source,
                                              const char *mime_type);
+extern SDL_bool Wayland_primary_selection_source_has_mime(SDL_WaylandPrimarySelectionSource *source,
+                                                          const char *mime_type);
 extern void* Wayland_data_source_get_data(SDL_WaylandDataSource *source,
                                           size_t *length,
                                           const char *mime_type,
                                           SDL_bool null_terminate);
+extern void* Wayland_primary_selection_source_get_data(SDL_WaylandPrimarySelectionSource *source,
+                                                       size_t *length,
+                                                       const char *mime_type,
+                                                       SDL_bool null_terminate);
 extern void Wayland_data_source_destroy(SDL_WaylandDataSource *source);
+extern void Wayland_primary_selection_source_destroy(SDL_WaylandPrimarySelectionSource *source);
 
-/* Wayland Data Offer - (Receiving) */
+/* Wayland Data / Primary Selection Offer - (Receiving) */
 extern void* Wayland_data_offer_receive(SDL_WaylandDataOffer *offer,
                                         size_t *length,
                                         const char *mime_type,
                                         SDL_bool null_terminate);
+extern void* Wayland_primary_selection_offer_receive(SDL_WaylandPrimarySelectionOffer *offer,
+                                                     size_t *length,
+                                                     const char *mime_type,
+                                                     SDL_bool null_terminate);
 extern SDL_bool Wayland_data_offer_has_mime(SDL_WaylandDataOffer *offer,
                                             const char *mime_type);
+extern SDL_bool Wayland_primary_selection_offer_has_mime(SDL_WaylandPrimarySelectionOffer *offer,
+                                                         const char *mime_type);
 extern int Wayland_data_offer_add_mime(SDL_WaylandDataOffer *offer,
                                        const char *mime_type);
+extern int Wayland_primary_selection_offer_add_mime(SDL_WaylandPrimarySelectionOffer *offer,
+                                                    const char *mime_type);
 extern void Wayland_data_offer_destroy(SDL_WaylandDataOffer *offer);
+extern void Wayland_primary_selection_offer_destroy(SDL_WaylandPrimarySelectionOffer *offer);
 
-/* Clipboard */
+/* Clipboard / Primary Selection */
 extern int Wayland_data_device_clear_selection(SDL_WaylandDataDevice *device);
+extern int Wayland_primary_selection_device_clear_selection(SDL_WaylandPrimarySelectionDevice *device);
 extern int Wayland_data_device_set_selection(SDL_WaylandDataDevice *device,
                                              SDL_WaylandDataSource *source);
+extern int Wayland_primary_selection_device_set_selection(SDL_WaylandPrimarySelectionDevice *device,
+                                             SDL_WaylandPrimarySelectionSource *source);
 extern int Wayland_data_device_set_serial(SDL_WaylandDataDevice *device,
                                           uint32_t serial);
+extern int Wayland_primary_selection_device_set_serial(SDL_WaylandPrimarySelectionDevice *device,
+                                                       uint32_t serial);
 #endif /* SDL_waylanddatamanager_h_ */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 5c3ac20..02c48bc 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -42,6 +42,7 @@
 #include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
 #include "text-input-unstable-v3-client-protocol.h"
 #include "tablet-unstable-v2-client-protocol.h"
+#include "primary-selection-unstable-v1-client-protocol.h"
 
 #ifdef HAVE_LIBDECOR_H
 #include <libdecor.h>
@@ -587,6 +588,7 @@ pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
         }
 
         Wayland_data_device_set_serial(input->data_device, serial);
+        Wayland_primary_selection_device_set_serial(input->primary_selection_device, serial);
 
         SDL_SendMouseButton(window->sdlwindow, 0,
                             state ? SDL_PRESSED : SDL_RELEASED, sdl_button);
@@ -1106,6 +1108,7 @@ keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
     if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
         if (has_text && !(SDL_GetModState() & KMOD_CTRL)) {
             Wayland_data_device_set_serial(input->data_device, serial);
+            Wayland_primary_selection_device_set_serial(input->primary_selection_device, serial);
             if (!handled_by_ime) {
                 SDL_SendKeyboardText(text);
             }
@@ -1319,6 +1322,25 @@ static const struct wl_data_source_listener data_source_listener = {
     data_source_handle_action,             // Version 3
 };
 
+static void
+primary_selection_source_send(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1,
+		                      const char *mime_type, int32_t fd)
+{
+    Wayland_primary_selection_source_send((SDL_WaylandPrimarySelectionSource *)data,
+                                          mime_type, fd);
+}
+
+static void
+primary_selection_source_cancelled(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1)
+{
+    Wayland_primary_selection_source_destroy(data);
+}
+
+static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
+    primary_selection_source_send,
+    primary_selection_source_cancelled,
+};
+
 SDL_WaylandDataSource*
 Wayland_data_source_create(_THIS)
 {
@@ -1355,6 +1377,41 @@ Wayland_data_source_create(_THIS)
     return data_source;
 }
 
+SDL_WaylandPrimarySelectionSource*
+Wayland_primary_selection_source_create(_THIS)
+{
+    SDL_WaylandPrimarySelectionSource *primary_selection_source = NULL;
+    SDL_VideoData *driver_data = NULL;
+    struct zwp_primary_selection_source_v1 *id = NULL;
+
+    if (_this == NULL || _this->driverdata == NULL) {
+        SDL_SetError("Video driver uninitialized");
+    } else {
+        driver_data = _this->driverdata;
+
+        if (driver_data->primary_selection_device_manager != NULL) {
+            id = zwp_primary_selection_device_manager_v1_create_source(
+                     driver_data->primary_selection_device_manager);
+        }
+
+        if (id == NULL) {
+            SDL_SetError("Wayland unable to create primary selection source");
+        } else {
+            primary_selection_source = SDL_calloc(1, sizeof *primary_selection_source);
+            if (primary_selection_source == NULL) {
+                SDL_OutOfMemory();
+                zwp_primary_selection_source_v1_destroy(id);
+            } else {
+                WAYLAND_wl_list_init(&(primary_selection_source->mimes));
+                primary_selection_source->source = id;
+                zwp_primary_selection_source_v1_add_listener(id, &primary_selection_source_listener,
+                        primary_selection_source);
+            }
+        }
+    }
+    return primary_selection_source;
+}
+
 static void
 data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
                         const char *mime_type)
@@ -1382,6 +1439,18 @@ static const struct wl_data_offer_listener data_offer_listener = {
 };
 
 static void
+primary_selection_offer_handle_offer(void *data, struct zwp_primary_selection_offer_v1 *zwp_primary_selection_offer_v1,
+                                     const char *mime_type)
+{
+    SDL_WaylandPrimarySelectionOffer *offer = data;
+    Wayland_primary_selection_offer_add_mime(offer, mime_type);
+}
+
+static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
+    primary_selection_offer_handle_offer,
+};
+
+static void
 data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
                               struct wl_data_offer *id)
 {
@@ -1621,6 +1690,48 @@ static const struct wl_data_device_listener data_device_listener = {
 };
 
 static void
+primary_selection_device_handle_offer(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
+                                      struct zwp_primary_selection_offer_v1 *id)
+{
+    SDL_WaylandPrimarySelectionOffer *primary_selection_offer = NULL;
+
+    primary_selection_offer = SDL_calloc(1, sizeof *primary_selection_offer);
+    if (primary_selection_offer == NULL) {
+        SDL_OutOfMemory();
+    } else {
+        primary_selection_offer->offer = id;
+        primary_selection_offer->primary_selection_device = data;
+        WAYLAND_wl_list_init(&(primary_selection_offer->mimes));
+        zwp_primary_selection_offer_v1_set_user_data(id, primary_selection_offer);
+        zwp_primary_selection_offer_v1_add_listener(id, &primary_selection_offer_listener, primary_selection_offer);
+    }
+}
+
+static void
+primary_selection_device_handle_selection(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
+                                          struct zwp_primary_selection_offer_v1 *id)
+{
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = data;
+    SDL_WaylandPrimarySelectionOffer *offer = NULL;
+
+    if (id != NULL) {
+        offer = zwp_primary_selection_offer_v1_get_user_data(id);
+    }
+
+    if (primary_selection_device->selection_offer != offer) {
+        Wayland_primary_selection_offer_destroy(primary_selection_device->selection_offer);
+        primary_selection_device->selection_offer = offer;
+    }
+
+    SDL_SendClipboardUpdate();
+}
+
+static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
+    primary_selection_device_handle_offer,
+    primary_selection_device_handle_selection
+};
+
+static void
 text_input_enter(void *data,
                  struct zwp_text_input_v3 *zwp_text_input_v3,
                  struct wl_surface *surface)
@@ -1667,9 +1778,9 @@ text_input_preedit_string(void *data,
             do {
                 const int sz = (int)SDL_utf8strlcpy(buf, text+i, sizeof(buf));
                 const int chars = (int)SDL_utf8strlen(buf);
-    
+
                 SDL_SendEditingText(buf, cursor, chars);
-    
+
                 i += sz;
                 cursor += chars;
             } while (i < text_bytes);
@@ -1754,6 +1865,32 @@ Wayland_create_data_device(SDL_VideoData *d)
 }
 
 static void
+Wayland_create_primary_selection_device(SDL_VideoData *d)
+{
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
+
+    primary_selection_device = SDL_calloc(1, sizeof *primary_selection_device);
+    if (primary_selection_device == NULL) {
+        return;
+    }
+
+    primary_selection_device->primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(
+        d->primary_selection_device_manager, d->input->seat
+    );
+    primary_selection_device->video_data = d;
+
+    if (primary_selection_device->primary_selection_device == NULL) {
+        SDL_free(primary_selection_device);
+    } else {
+        zwp_primary_selection_device_v1_set_user_data(primary_selection_device->primary_selection_device,
+                                                      primary_selection_device);
+        zwp_primary_selection_device_v1_add_listener(primary_selection_device->primary_selection_device,
+                                                     &primary_selection_device_listener, primary_selection_device);
+        d->input->primary_selection_device = primary_selection_device;
+    }
+}
+
+static void
 Wayland_create_text_input(SDL_VideoData *d)
 {
     SDL_WaylandTextInput *text_input = NULL;
@@ -1788,6 +1925,16 @@ Wayland_add_data_device_manager(SDL_VideoData *d, uint32_t id, uint32_t version)
 }
 
 void
+Wayland_add_primary_selection_device_manager(SDL_VideoData *d, uint32_t id, uint32_t version)
+{
+    d->primary_selection_device_manager = wl_registry_bind(d->registry, id, &zwp_primary_selection_device_manager_v1_interface, 1);
+
+    if (d->input != NULL) {
+        Wayland_create_primary_selection_device(d);
+    }
+}
+
+void
 Wayland_add_text_input_manager(SDL_VideoData *d, uint32_t id, uint32_t version)
 {
     d->text_input_manager = wl_registry_bind(d->registry, id, &zwp_text_input_manager_v3_interface, 1);
@@ -2173,6 +2320,9 @@ Wayland_display_add_input(SDL_VideoData *d, uint32_t id, uint32_t version)
     if (d->data_device_manager != NULL) {
         Wayland_create_data_device(d);
     }
+    if (d->primary_selection_device_manager != NULL) {
+        Wayland_create_primary_selection_device(d);
+    }
     if (d->text_input_manager != NULL) {
         Wayland_create_text_input(d);
     }
diff --git a/src/video/wayland/SDL_waylandevents_c.h b/src/video/wayland/SDL_waylandevents_c.h
index 491c385..d70fdee 100644
--- a/src/video/wayland/SDL_waylandevents_c.h
+++ b/src/video/wayland/SDL_waylandevents_c.h
@@ -85,6 +85,7 @@ struct SDL_WaylandInput {
     struct wl_touch *touch;
     struct wl_keyboard *keyboard;
     SDL_WaylandDataDevice *data_device;
+    SDL_WaylandPrimarySelectionDevice *primary_selection_device;
     SDL_WaylandTextInput *text_input;
     struct zwp_relative_pointer_v1 *relative_pointer;
     SDL_WindowData *pointer_focus;
@@ -137,6 +138,7 @@ extern void Wayland_SendWakeupEvent(_THIS, SDL_Window *window);
 extern int Wayland_WaitEventTimeout(_THIS, int timeout);
 
 extern void Wayland_add_data_device_manager(SDL_VideoData *d, uint32_t id, uint32_t version);
+extern void Wayland_add_primary_selection_device_manager(SDL_VideoData *d, uint32_t id, uint32_t version);
 extern void Wayland_add_text_input_manager(SDL_VideoData *d, uint32_t id, uint32_t version);
 
 extern void Wayland_display_add_input(SDL_VideoData *d, uint32_t id, uint32_t version);
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 113ae6f..97e5efb 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -55,6 +55,8 @@
 #include "tablet-unstable-v2-client-protocol.h"
 #include "xdg-output-unstable-v1-client-protocol.h"
 #include "viewporter-client-protocol.h"
+#include "viewporter-client-protocol.h"
+#include "primary-selection-unstable-v1-client-protocol.h"
 
 #ifdef HAVE_LIBDECOR_H
 #include <libdecor.h>
@@ -265,6 +267,9 @@ Wayland_CreateDevice(void)
     device->SetClipboardText = Wayland_SetClipboardText;
     device->GetClipboardText = Wayland_GetClipboardText;
     device->HasClipboardText = Wayland_HasClipboardText;
+    device->SetPrimarySelectionText = Wayland_SetPrimarySelectionText;
+    device->GetPrimarySelectionText = Wayland_GetPrimarySelectionText;
+    device->HasPrimarySelectionText = Wayland_HasPrimarySelectionText;
     device->StartTextInput = Wayland_StartTextInput;
     device->StopTextInput = Wayland_StopTextInput;
     device->SetTextInputRect = Wayland_SetTextInputRect;
@@ -868,6 +873,8 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
         Wayland_add_text_input_manager(d, id, version);
     } else if (SDL_strcmp(interface, "wl_data_device_manager") == 0) {
         Wayland_add_data_device_manager(d, id, version);
+    } else if (SDL_strcmp(interface, "zwp_primary_selection_device_manager_v1") == 0) {
+        Wayland_add_primary_selection_device_manager(d, id, version);
     } else if (SDL_strcmp(interface, "zxdg_decoration_manager_v1") == 0) {
         d->decoration_manager = wl_registry_bind(d->registry, id, &zxdg_decoration_manager_v1_interface, 1);
     } else if (SDL_strcmp(interface, "zwp_tablet_manager_v2") == 0) {
@@ -908,7 +915,7 @@ static const struct wl_registry_listener registry_listener = {
     display_handle_global,
     display_remove_global
 };
- 
+
 #ifdef HAVE_LIBDECOR_H
 static SDL_bool should_use_libdecor(SDL_VideoData *data, SDL_bool ignore_xdg)
 {
@@ -1107,6 +1114,10 @@ Wayland_VideoQuit(_THIS)
         wp_viewporter_destroy(data->viewporter);
     }
 
+    if (data->primary_selection_device_manager) {
+        zwp_primary_selection_device_manager_v1_destroy(data->primary_selection_device_manager);
+    }
+
     if (data->compositor)
         wl_compositor_destroy(data->compositor);
 
diff --git a/src/video/wayland/SDL_waylandvideo.h b/src/video/wayland/SDL_waylandvideo.h
index 9b2d683..3e7b363 100644
--- a/src/video/wayland/SDL_waylandvideo.h
+++ b/src/video/wayland/SDL_waylandvideo.h
@@ -68,6 +68,7 @@ typedef struct {
     struct zwp_relative_pointer_manager_v1 *relative_pointer_manager;
     struct zwp_pointer_constraints_v1 *pointer_constraints;
     struct wl_data_device_manager *data_device_manager;
+    struct zwp_primary_selection_device_manager_v1 *primary_selection_device_manager;
     struct zxdg_decoration_manager_v1 *decoration_manager;
     struct zwp_keyboard_shortcuts_inhibit_manager_v1 *key_inhibitor_manager;
     struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager;
diff --git a/src/video/x11/SDL_x11clipboard.c b/src/video/x11/SDL_x11clipboard.c
index 37a17f4..fd198bd 100644
--- a/src/video/x11/SDL_x11clipboard.c
+++ b/src/video/x11/SDL_x11clipboard.c
@@ -52,11 +52,11 @@ GetWindow(_THIS)
     return data->clipboard_window;
 }
 
-
-/* We use our own cut-buffer for intermediate storage instead of  
-   XA_CUT_BUFFER0 because their use isn't really defined for holding UTF8. */ 
+/* We use our own cut-buffer for intermediate storage instead of
+   XA_CUT_BUFFER0 because their use isn't really defined for holding UTF8. */
 Atom
-X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType mime_type)
+X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType mime_type,
+        Atom selection_type)
 {
     switch (mime_type) {
         case SDL_X11_CLIPBOARD_MIME_TYPE_STRING:
@@ -65,7 +65,9 @@ X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType
         case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT_PLAIN_UTF8:
         #endif
         case SDL_X11_CLIPBOARD_MIME_TYPE_TEXT:
-            return X11_XInternAtom(display, "SDL_CUTBUFFER", False);
+            return X11_XInternAtom(display, selection_type == XA_PRIMARY ?
+                                   "SDL_CUTBUFFER_PRIMARY_SELECTION" : "SDL_CUTBUFFER",
+                                   False);
         default:
             SDL_SetError("Can't find mime_type.");
             return XA_STRING;
@@ -118,13 +120,11 @@ X11_GetSDLCutBufferClipboardInternalFormat(Display *display, enum ESDLX11Clipboa
     }
 }
 
-
-int
-X11_SetClipboardText(_THIS, const char *text)
+static int
+SetSelectionText(_THIS, const char *text, Atom selection_type)
 {
     Display *display = ((SDL_VideoData *) _this->driverdata)->display;
     Window window;
-    Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
 
     /* Get the SDL window that will own the selection */
     window = GetWindow(_this);
@@ -134,22 +134,19 @@ X11_SetClipboardText(_THIS, const char *text)
 
     /* Save the selection on the root window */
     X11_XChangeProperty(display, DefaultRootWindow(display),
-        X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING), X11_GetSDLCutBufferClipboardInternalFormat(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING), 8, PropModeReplace,
+        X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING, selection_type),
+        X11_GetSDLCutBufferClipboardInternalFormat(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING), 8, PropModeReplace,
         (const unsigned char *)text, SDL_strlen(text));
 
-    if (XA_CLIPBOARD != None &&
-        X11_XGetSelectionOwner(display, XA_CLIPBOARD) != window) {
-        X11_XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
+    if (X11_XGetSelectionOwner(display, selection_type) != window) {
+        X11_XSetSelectionOwner(display, selection_type, window, CurrentTime);
     }
 
-    if (X11_XGetSelectionOwner(display, XA_PRIMARY) != window) {
-        X11_XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime);
-    }
     return 0;
 }
 
-char *
-X11_GetClipboardText(_THIS)
+static char *
+GetSlectionText(_THIS, Atom selection_type)
 {
     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
     Display *display = videodata->display;
@@ -165,18 +162,13 @@ X11_GetClipboardText(_THIS)
     char *text;
     Uint32 waitStart;
     Uint32 waitElapsed;
-    Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
-    if (XA_CLIPBOARD == None) {
-        SDL_SetError("Couldn't access X clipboard");
-        return SDL_strdup("");
-    }
 
     text = NULL;
 
     /* Get the window that holds the selection */
     window = GetWindow(_this);
     format = X11_GetSDLCutBufferClipboardInternalFormat(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING);
-    owner = X11_XGetSelectionOwner(display, XA_CLIPBOARD);
+    owner = X11_XGetSelectionOwner(display, selection_type);
     if (owner == None) {
         /* Fall back to ancient X10 cut-buffers which do not support UTF8 strings*/
         owner = DefaultRootWindow(display);
@@ -184,12 +176,12 @@ X11_GetClipboardText(_THIS)
         format = XA_STRING;
     } else if (owner == window) {
         owner = DefaultRootWindow(display);
-        selection = X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING);
+        selection = X11_GetSDLCutBufferClipboardType(display, SDL_X11_CLIPBOARD_MIME_TYPE_STRING, selection_type);
     } else {
         /* Request that the selection owner copy the data to our window */
         owner = window;
         selection = X11_XInternAtom(display, "SDL_SELECTION", False);
-        X11_XConvertSelection(display, XA_CLIPBOARD, format, selection, owner,
+        X11_XConvertSelection(display, selection_type, format, selection, owner,
             CurrentTime);
 
         /* When using synergy on Linux and when data has been put in the clipboard
@@ -200,13 +192,13 @@ X11_GetClipboardText(_THIS)
         while (videodata->selection_waiting) {
             SDL_PumpEvents();
             waitElapsed = SDL_GetTicks() - waitStart;
-            /* Wait one second for a clipboard response. */
+            /* Wait one second for a selection response. */
             if (waitElapsed > 1000) {
                 videodata->selection_waiting = SDL_FALSE;
-                SDL_SetError("Clipboard timeout");
-                /* We need to set the clipboard text so that next time we won't
+                SDL_SetError("Selection timeout");
+                /* We need to set the selection text so that next time we won't
                    timeout, otherwise we will hang on every call to this function. */
-                X11_SetClipboardText(_this, "");
+                SetSelectionText(_this, "", selection_type);
                 return SDL_strdup("");
             }
         }
@@ -232,6 +224,41 @@ X11_GetClipboardText(_THIS)
     return text;
 }
 
+int
+X11_SetClipboardText(_THIS, const char *text)
+{
+    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
+    Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
+    if (XA_CLIPBOARD == None) {
+        return SDL_SetError("Couldn't access X clipboard");
+    }
+    return SetSelectionText(_this, text, XA_CLIPBOARD);
+}
+
+int
+X11_SetPrimarySelectionText(_THIS, const char *text)
+{
+    return SetSelectionText(_this, text, XA_PRIMARY);
+}
+
+char *
+X11_GetClipboardText(_THIS)
+{
+    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
+    Atom XA_CLIPBOARD = X11_XInternAtom(videodata->display, "CLIPBOARD", 0);
+    if (XA_CLIPBOARD == None) {
+        SDL_SetError("Couldn't access X clipboard");
+        return SDL_strdup("");
+    }
+    return GetSlectionText(_this, XA_CLIPBOARD);
+}
+
+char *
+X11_GetPrimarySelectionText(_THIS)
+{
+    return GetSlectionText(_this, XA_PRIMARY);
+}
+
 SDL_bool
 X11_HasClipboardText(_THIS)
 {
@@ -244,6 +271,18 @@ X11_HasClipboardText(_THIS)
     return result;
 }
 
+SDL_bool
+X11_HasPrimarySelectionText(_THIS)
+{
+    SDL_bool result = SDL_FALSE;
+    char *text = X11_GetPrimarySelectionText(_this);
+    if (text) {
+        result = text[0] != '\0' ? SDL_TRUE : SDL_FALSE;
+        SDL_free(text);
+    }
+    return result;
+}
+
 #endif /* SDL_VIDEO_DRIVER_X11 */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/x11/SDL_x11clipboard.h b/src/video/x11/SDL_x11clipboard.h
index ec98445..ddeb016 100644
--- a/src/video/x11/SDL_x11clipboard.h
+++ b/src/video/x11/SDL_x11clipboard.h
@@ -36,7 +36,10 @@ enum ESDLX11ClipboardMimeType {
 extern int X11_SetClipboardText(_THIS, const char *text);
 extern char *X11_GetClipboardText(_THIS);
 extern SDL_bool X11_HasClipboardText(_THIS);
-extern Atom X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType mime_type);
+extern int X11_SetPrimarySelectionText(_THIS, const char *text);
+extern char *X11_GetPrimarySelectionText(_THIS);
+extern SDL_bool X11_HasPrimarySelectionText(_THIS);
+extern Atom X11_GetSDLCutBufferClipboardType(Display *display, enum ESDLX11ClipboardMimeType mime_type, Atom selection_type);
 extern Atom X11_GetSDLCutBufferClipboardExternalFormat(Display *display, enum ESDLX11ClipboardMimeType mime_type);
 extern Atom X11_GetSDLCutBufferClipboardInternalFormat(Display *display, enum ESDLX11ClipboardMimeType mime_type);
 
diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index fca0d38..5c99a65 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -620,7 +620,7 @@ X11_HandleClipboardEvent(_THIS, const XEvent *xevent)
             int seln_format, mime_formats;
             unsigned long nbytes;
             unsigned long overflow;
-            unsigned char *seln_data;            
+            unsigned char *seln_data;
             Atom supportedFormats[SDL_X11_CLIPBOARD_MIME_TYPE_MAX+1];
             Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
 
@@ -640,11 +640,11 @@ X11_HandleClipboardEvent(_THIS, const XEvent *xevent)
             /* !!! FIXME: We were probably storing this on the root window
                because an SDL window might go away...? but we don't have to do
                this now (or ever, really). */
-            
+
             if (req->target == XA_TARGETS) {
                 supportedFormats[0] = XA_TARGETS;
                 mime_formats = 1;
-                for (i = 0; i < SDL_X11_CLIPBOARD_MIME_TYPE_MAX; ++i) 
+                for (i = 0; i < SDL_X11_CLIPBOARD_MIME_TYPE_MAX; ++i)
                     supportedFormats[mime_formats++] = X11_GetSDLCutBufferClipboardExternalFormat(display, i);
                 X11_XChangeProperty(display, req->requestor, req->property,
                     XA_ATOM, 32, PropModeReplace,
@@ -657,7 +657,7 @@ X11_HandleClipboardEvent(_THIS, const XEvent *xevent)
                     if (X11_GetSDLCutBufferClipboardExternalFormat(display, i) != req->target)
                         continue;
                     if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
-                        X11_GetSDLCutBufferClipboardType(display, i), 0, INT_MAX/4, False, X11_GetSDLCutBufferClipboardInternalFormat(display, i),
+                        X11_GetSDLCutBufferClipboardType(display, i, req->selection), 0, INT_MAX/4, False, X11_GetSDLCutBufferClipboardInternalFormat(display, i),
                         &sevent.xselection.target, &seln_format, &nbytes,
                         &overflow, &seln_data) == Success) {
                             if (seln_format != None) {
@@ -946,7 +946,7 @@ X11_DispatchEvent(_THIS, XEvent *xevent)
             if (xevent->xcrossing.mode != NotifyGrab &&
                 xevent->xcrossing.mode != NotifyUngrab &&
                 xevent->xcrossing.detail != NotifyInferior) {
-                
+
                 /* In order for interaction with the window decorations and menu to work properly
                    on Mutter, we need to ungrab the keyboard when the the mouse leaves. */
                 if (!(data->window->flags & SDL_WINDOW_FULLSCREEN)) {
@@ -1158,7 +1158,7 @@ X11_DispatchEvent(_THIS, XEvent *xevent)
                                         &xevent->xconfigure.x, &xevent->xconfigure.y,
                                         &ChildReturn);
             }
-                
+
             if (xevent->xconfigure.x != data->last_xconfigure.x ||
                 xevent->xconfigure.y != data->last_xconfigure.y) {
                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c
index 92045ca..2e5e190 100644
--- a/src/video/x11/SDL_x11video.c
+++ b/src/video/x11/SDL_x11video.c
@@ -301,6 +301,9 @@ X11_CreateDevice(void)
     device->SetClipboardText = X11_SetClipboardText;
     device->GetClipboardText = X11_GetClipboardText;
     device->HasClipboardText = X11_HasClipboardText;
+    device->SetPrimarySelectionText = X11_SetPrimarySelectionText;
+    device->GetPrimarySelectionText = X11_GetPrimarySelectionText;
+    device->HasPrimarySelectionText = X11_HasPrimarySelectionText;
     device->StartTextInput = X11_StartTextInput;
     device->StopTextInput = X11_StopTextInput;
     device->SetTextInputRect = X11_SetTextInputRect;
diff --git a/test/testautomation_clipboard.c b/test/testautomation_clipboard.c
index 113c7dc..76090d1 100644
--- a/test/testautomation_clipboard.c
+++ b/test/testautomation_clipboard.c
@@ -28,6 +28,21 @@ clipboard_testHasClipboardText(void *arg)
 }
 
 /**
+ * \brief Check call to SDL_HasPrimarySelectionText
+ *
+ * \sa
+ * http://wiki.libsdl.org/SDL_HasPrimarySelectionText
+ */
+int
+clipboard_testHasPrimarySelectionText(void *arg)
+{
+    SDL_HasPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
+
+    return TEST_COMPLETED;
+}
+
+/**
  * \brief Check call to SDL_GetClipboardText
  *
  * \sa
@@ -46,6 +61,24 @@ clipboard_testGetClipboardText(void *arg)
 }
 
 /**
+ * \brief Check call to SDL_GetPrimarySelectionText
+ *
+ * \sa
+ * http://wiki.libsdl.org/SDL_GetPrimarySelectionText
+ */
+int
+clipboard_testGetPrimarySelectionText(void *arg)
+{
+    char *charResult;
+    charResult = SDL_GetPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
+
+    SDL_free(charResult);
+
+    return TEST_COMPLETED;
+}
+
+/**
  * \brief Check call to SDL_SetClipboardText
  * \sa
  * http://wiki.libsdl.org/SDL_SetClipboardText
@@ -71,7 +104,36 @@ clipboard_testSetClipboardText(void *arg)
     SDL_free(textRef);
     SDL_free(text);
 
-   return TEST_COMPLETED;
+    return TEST_COMPLETED;
+}
+
+/**
+ * \brief Check call to SDL_SetPrimarySelectionText
+ * \sa
+ * http://wiki.libsdl.org/SDL_SetPrimarySelectionText
+ */
+int
+clipboard_testSetPrimarySelectionText(void *arg)
+{
+    char *textRef = SDLTest_RandomAsciiString();
+    char *text = SDL_strdup(textRef);
+    int result;
+    result = SDL_SetPrimarySelectionText((const char *)text);
+    SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText succeeded");
+    SDLTest_AssertCheck(
+        result == 0,
+        "Validate SDL_SetPrimarySelectionText result, expected 0, got %i",
+        result);
+    SDLTest_AssertCheck(
+        SDL_strcmp(textRef, text) == 0,
+        "Verify SDL_SetPrimarySelectionText did not modify input string, expected '%s', got '%s'",
+        textRef, text);
+
+    /* Cleanup */
+    SDL_free(textRef);
+    SDL_free(text);
+
+    return TEST_COMPLETED;
 }
 
 /**
@@ -150,7 +212,86 @@ clipboard_testClipboardTextFunctions(void *arg)
     SDL_free(text);
     SDL_free(charResult);
 
-   return TEST_COMPLETED;
+    return TEST_COMPLETED;
+}
+
+/**
+ * \brief End-to-end test of SDL_xyzPrimarySelectionText functions
+ * \sa
+ * http://wiki.libsdl.org/SDL_HasPrimarySelectionText
+ * http://wiki.libsdl.org/SDL_GetPrimarySelectionText
+ * http://wiki.libsdl.org/SDL_SetPrimarySelectionText
+ */
+int
+clipboard_testPrimarySelectionTextFunctions(void *arg)
+{
+    char *textRef = SDLTest_RandomAsciiString();
+    char *text = SDL_strdup(textRef);
+    SDL_bool boolResult;
+    int intResult;
+    char *charResult;
+
+    /* Clear primary selection text state */
+    boolResult = SDL_HasPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
+    if (boolResult == SDL_TRUE) {
+        intResult = SDL_SetPrimarySelectionText((const char *)NULL);
+        SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText(NULL) succeeded");
+        SDLTest_AssertCheck(
+            intResult == 0,
+            "Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
+            intResult);
+        charResult = SDL_GetPrimarySelectionText();
+        SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
+        SDL_free(charResult);
+        boolResult = SDL_HasPrimarySelectionText();
+        SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
+        SDLTest_AssertCheck(
+            boolResult == SDL_FALSE,
+            "Verify SDL_HasPrimarySelectionText returned SDL_FALSE, got %s",
+            (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
+    }
+
+    /* Empty primary selection  */
+    charResult = SDL_GetPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
+    SDLTest_AssertCheck(
+        charResult != NULL,
+        "Verify SDL_GetPrimarySelectionText did not return NULL");
+    SDLTest_AssertCheck(
+        charResult[0] == '\0',
+        "Verify SDL_GetPrimarySelectionText returned string with length 0, got length %i",
+        (int) SDL_strlen(charResult));
+    intResult = SDL_SetPrimarySelectionText((const char *)text);
+    SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText succeeded");
+    SDLTest_AssertCheck(
+        intResult == 0,
+        "Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
+        intResult);
+    SDLTest_AssertCheck(
+        SDL_strcmp(textRef, text) == 0,
+        "Verify SDL_SetPrimarySelectionText did not modify input string, expected '%s', got '%s'",
+        textRef, text);
+    boolResult = SDL_HasPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
+    SDLTest_AssertCheck(
+        boolResult == SDL_TRUE,
+        "Verify SDL_HasPrimarySelectionText returned SDL_TRUE, got %s",
+        (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
+    SDL_free(charResult);
+    charResult = SDL_GetPrimarySelectionText();
+    SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
+    SDLTest_AssertCheck(
+        SDL_strcmp(textRef, charResult) == 0,
+        "Verify SDL_GetPrimarySelectionText returned correct string, expected '%s', got '%s'",
+        textRef, charResult);
+
+    /* Cleanup */
+    SDL_free(textRef);
+    SDL_free(text);
+    SDL_free(charResult);
+
+    return TEST_COMPLETED;
 }
 
 
@@ -161,17 +302,29 @@ static const SDLTest_TestCaseReference clipboardTest1 =
         { (SDLTest_TestCaseFp)clipboard_testHasClipboardText, "clipboard_testHasClipboardText", "Check call to SDL_HasClipboardText", TEST_ENABLED };
 
 static const SDLTest_TestCaseReference clipboardTest2 =
-        { (SDLTest_TestCaseFp)clipboard_testGetClipboardText, "clipboard_testGetClipboardText", "Check call to SDL_GetClipboardText", TEST_ENABLED };
+        { (SDLTest_TestCaseFp)clipboard_testHasPrimarySelectionText, "clipboard_testHasPrimarySelectionText", "Check call to SDL_HasPrimarySelectionText", TEST_ENABLED };
 
 static const SDLTest_TestCaseReference clipboardTest3 =
-        { (SDLTest_TestCaseFp)clipboard_testSetClipboardText, "clipboard_testSetClipboardText", "Check call to SDL_SetClipboardText", TEST_ENABLED };
+        { (SDLTest_TestCaseFp)clipboard_testGetClipboardText, "clipboard_testGetClipboardText", "Check call to SDL_GetClipboardText", TEST_ENABLED };
 
 static const SDLTest_TestCaseReference clipboardTest4 =
+        { (SDLTest_TestCaseFp)clipboard_testGetPrimarySelectionText, "clipboard_testGetPrimarySelectionText", "Check call to SDL_GetPrimarySelectionText", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference clipboardTest5 =
+        { (SDLTest_TestCaseFp)clipboard_testSetClipboardText, "clipboard_testSetClipboardText", "Check call to SDL_SetClipboardText", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference clipboardTest6 =
+        { (SDLTest_TestCaseFp)clipboard_testSetPrimarySelectionText, "clipboard_testSetPrimarySelectionText", "Check call to SDL_SetPrimarySelectionText", TEST_ENABLED };
+
+static const SDLTest_TestCaseReference clipboardTest7 =
         { (SDLTest_TestCaseFp)clipboard_testClipboardTextFunctions, "clipboard_testClipboardTextFunctions", "End-to-end test of SDL_xyzClipboardText functions", TEST_ENABLED };
 
+static const SDLTest_TestCaseReference clipboardTest8 =
+        { (SDLTest_TestCaseFp)clipboard_testPrimarySelectionTextFunctions, "clipboard_testPrimarySelectionTextFunctions", "End-to-end test of SDL_xyzPrimarySelectionText functions", TEST_ENABLED };
+
 /* Sequence of Clipboard test cases */
 static const SDLTest_TestCaseReference *clipboardTests[] =  {
-    &clipboardTest1, &clipboardTest2, &clipboardTest3, &clipboardTest4, NULL
+    &clipboardTest1, &clipboardTest2, &clipboardTest3, &clipboardTest4, &clipboardTest5, &clipboardTest6, &clipboardTest7, &clipboardTest8, NULL
 };
 
 /* Clipboard test suite (global) */
diff --git a/wayland-protocols/primary-selection-unstable-v1.xml b/wayland-protocols/primary-selection-unstable-v1.xml
new file mode 100644
index 0000000..e5a39e3
--- /dev/null
+++ b/wayland-protocols/primary-selection-unstable-v1.xml
@@ -0,0 +1,225 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="wp_primary_selection_unstable_v1">
+  <copyright>
+    Copyright © 2015, 2016 Red Hat
+
+    Permission is hereby granted, free of charge, to any person obtaining a
+    copy of this software and associated documentation files (the "Software"),
+    to deal in the Software without restriction, including without limitation
+    the rights to use, copy, modify, merge, publish, distribute, sublicense,
+    and/or sell copies of the Software, and to permit persons to whom the
+    Software is furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice (including the next
+    paragraph) shall be included in all copies or substantial portions of the
+    Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+    DEALINGS IN THE SOFTWARE.
+  </copyright>
+
+  <description summary="Primary selection protocol">
+    This protocol provides the ability to have a primary selection device to
+    match that of the X server. This primary selection is a shortcut to the
+    common clipboard selection, where text just needs to be selected in order
+    to allow copying it elsewhere. The de facto way to perform this action
+    is the middle mouse button, although it is not limited to this one.
+
+    Clients wishing to honor primary selection should create a primary
+    selection source and set it as the selection through
+    wp_primary_selection_device.set_selection whenever the text selection
+    changes. In order to minimize calls in pointer-driven text selection,
+    it should happen only once after the operation finished. Similarly,
+    a NULL source should be set when text is unselected.
+
+    wp_primary_selection_offer objects are first announced through the
+    wp_primary_selection_device.data_offer event. Immediately after this event,
+    the primary data offer will emit wp_primary_selection_offer.offer events
+    to let know of the mime types being offered.
+
+    When the primary selection changes, the client with the keyboard focus
+    will receive wp_primary_selection_device.selection events. Only the client
+    with the keyboard focus will receive such events with a non-NULL
+    wp_primary_selection_offer. Across keyboard focus changes, previously
+    focused clients will receive wp_primary_selection_device.events with a
+    NULL wp_primary_selection_offer.
+
+    In order to request the primary selection data, the client must pass
+    a recent serial pertaining to the press event that is triggering the
+    operation, if the compositor deems the serial valid and recent, the
+    wp_primary_selection_source.send event will happen in the other end
+    to let the transfer begin. The client owning the primary selection
+    should write the requested data, and close the file descriptor
+    immediately.
+
+    If the primary selection owner client disappeared during the transfer,
+    the client reading the data will receive a
+    wp_primary_selection_device.selection event with a NULL
+    wp_primary_selection_offer, the client should take this as a hint
+    to finish the reads related to the no longer existing offer.
+
+    The primary selection owner should be checking for errors during
+    writes, merely cancelling the ongoing transfer if any happened.
+  </description>
+
+  <interface name="zwp_primary_selection_device_manager_v1" version="1">
+    <description summary="X primary selection emulation">
+      The primary selection device manager is a singleton global object that
+      provides access to the primary selection. It allows to create
+      wp_primary_selection_source objects, as well as retrieving the per-seat
+      wp_primary_selection_device objects.
+    </description>
+
+    <request name="create_source">
+      <description summary="create a new primary selection source">
+        Create a new primary selection source.
+      </description>
+      <arg name="id" type="new_id" interface="zwp_primary_selection_source_v1"/>
+    </request>
+
+    <request name="get_device">
+      <description summary="create a new primary selection device">
+        Create a new data device for a given seat.
+      </description>
+      <arg name="id" type="new_id" interface="zwp_primary_selection_device_v1"/>
+      <arg name="seat" type="object" interface="wl_seat"/>
+    </request>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the primary selection device manager">
+        Destroy the primary selection device manager.
+      </description>
+    </request>
+  </interface>
+
+  <interface name="zwp_primary_selection_device_v1" version="1">
+    <request name="set_selection">
+      <description summary="set the primary selection">
+        Replaces the current selection. The previous owner of the primary
+        selection will receive a wp_primary_selection_source.cancelled event.
+
+        To unset the selection, set the source to NULL.
+      </description>
+      <arg name="source" type="object" interface="zwp_primary_selection_source_v1" allow-null="true"/>
+      <arg name="serial" type="uint" summary="serial of the event that triggered this request"/>
+    </request>
+
+    <event name="data_offer">
+      <description summary="introduce a new wp_primary_selection_offer">
+        Introduces a new wp_primary_selection_offer object that may be used
+        to receive the current primary selection. Immediately following this
+        event, the new wp_primary_selection_offer object will send
+        wp_primary_selection_offer.offer events to describe the offered mime
+        types.
+      </description>
+      <arg name="offer" type="new_id" interface="zwp_primary_selection_offer_v1"/>
+    </event>
+
+    <event name="selection">
+      <description summary="advertise a new primary selection">
+        The wp_primary_selection_device.selection event is sent to notify the
+        client of a new primary selection. This event is sent after the
+        wp_primary_selection.data_offer event introducing this object, and after
+        the offer has announced its mimetypes through
+        wp_primary_selection_offer.offer.
+
+        The data_offer is valid until a new offer or NULL is received
+        or until the client loses keyboard focus. The client must destroy the
+        previous selection data_offer, if any, upon receiving this event.
+      </description>
+      <arg name="id" type="object" interface="zwp_primary_selection_offer_v1" allow-null="true"/>
+    </event>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the primary selection device">
+        Destroy the primary selection device.
+      </description>
+    </request>
+  </interface>
+
+  <interface name="zwp_primary_selection_offer_v1" version="1">
+    <description summary="offer to transfer primary selection contents">
+      A wp_primary_selection_offer represents an offer to transfer the contents
+      of the primary selection clipboard to the client. Similar to
+      wl_data_offer, the offer also describes the mime types that the data can
+      be converted to and provides the mechanisms for transferring the data
+      directly to the client.
+    </description>
+
+    <request name="receive">
+      <description summary="request that the data is transferred">
+        To transfer the contents of the primary selection clipboard, the client
+        issues this request and indicates the mime type that it wants to
+        receive. The transfer happens through the passed file descriptor
+        (typically created with the pipe system call). The source client writes
+        the data in the mime type representation requested and then closes the
+        file descriptor.
+
+        The receiving client reads from the read end of the pipe until EOF and
+        closes its end, at which point the transfer is complete.
+      </description>
+      <arg name="mime_type" type="string"/>
+      <arg name="fd" type="fd"/>
+    </request>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the primary selection offer">
+        Destroy the primary selection offer.
+      </description>
+    </request>
+
+    <event name="offer">
+      <description summary="advertise offered mime type">
+        Sent immediately after creating announcing the
+        wp_primary_selection_offer through
+        wp_primary_selection_device.data_offer. One event is sent per offered
+        mime type.
+      </description>
+      <arg name="mime_type" type="string"/>
+    </event>
+  </interface>
+
+  <interface name="zwp_primary_selection_source_v1" version="1">
+    <description summary="offer to replace the contents of the primary selection">
+      The source side of a wp_primary_selection_offer, it provides a way to
+      describe the offered data and respond to requests to transfer the
+      requested contents of the primary selection clipboard.
+    </description>
+
+    <request name="offer">
+      <description summary="add an offered mime type">
+        This request adds a mime type to the set of mime types advertised to
+        targets. Can be called several times to offer multiple types.
+      </description>
+      <arg name="mime_type" type="string"/>
+    </request>
+
+    <request name="destroy" type="destructor">
+      <description summary="destroy the primary selection source">
+        Destroy the primary selection source.
+      </description>
+    </request>
+
+    <event name="send">
+      <description summary="send the primary selection contents">
+        Request for the current primary selection contents from the client.
+        Send the specified mime type over the passed file descriptor, then
+        close it.
+      </description>
+      <arg name="mime_type" type="string"/>
+      <arg name="fd" type="fd"/>
+    </event>
+
+    <event name="cancelled">
+      <description summary="request for primary selection contents was canceled">
+        This primary selection source is no longer valid. The client should
+        clean up and destroy this primary selection source.
+      </description>
+    </event>
+  </interface>
+</protocol>