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
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2007 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup wm
*
* Handle events and notifiers from GHOST input (mouse, keyboard, tablet, ndof).
*
* Also some operator reports utility functions.
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "DNA_listBase.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "MEM_guardedalloc.h"
#include "CLG_log.h"
#include "GHOST_C-api.h"
#include "BLI_blenlib.h"
#include "BLI_dynstr.h"
#include "BLI_math.h"
#include "BLI_timer.h"
#include "BLI_utildefines.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
#include "BKE_global.h"
#include "BKE_idprop.h"
#include "BKE_main.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_screen.h"
#include "BKE_workspace.h"
#include "BKE_sound.h"
#include "BLT_translation.h"
#include "ED_fileselect.h"
#include "ED_info.h"
#include "ED_screen.h"
#include "ED_undo.h"
#include "ED_util.h"
#include "ED_view3d.h"
#include "RNA_access.h"
#include "UI_interface.h"
#include "PIL_time.h"
#include "WM_api.h"
#include "WM_message.h"
#include "WM_toolsystem.h"
#include "WM_types.h"
#include "wm.h"
#include "wm_event_system.h"
#include "wm_event_types.h"
#include "wm_window.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
/**
* When a gizmo is highlighted and uses click/drag events,
* this prevents mouse button press events from being passed through to other key-maps
* which would obscure those events.
*
* This allows gizmos that only use drag to co-exist with tools that use click.
*
* Without tools using press events which would prevent click/drag events getting to the gizmos.
*
* This is not a fool proof solution since since it's possible the gizmo operators would pass
* through these events when called, see: T65479.
*/
#define USE_GIZMO_MOUSE_PRIORITY_HACK
static void wm_notifier_clear(wmNotifier *note);
static int wm_operator_call_internal(bContext *C,
wmOperatorType *ot,
PointerRNA *properties,
ReportList *reports,
const short context,
const bool poll_only,
wmEvent *event);
static bool wm_operator_check_locked_interface(bContext *C, wmOperatorType *ot);
/* -------------------------------------------------------------------- */
/** \name Event Management
* \{ */
wmEvent *wm_event_add_ex(wmWindow *win,
const wmEvent *event_to_add,
const wmEvent *event_to_add_after)
{
wmEvent *event = MEM_mallocN(sizeof(wmEvent), "wmEvent");
*event = *event_to_add;
if (event_to_add_after == NULL) {
BLI_addtail(&win->event_queue, event);
}
else {
/* Note: strictly speaking this breaks const-correctness,
* however we're only changing 'next' member. */
BLI_insertlinkafter(&win->event_queue, (void *)event_to_add_after, event);
}
return event;
}
wmEvent *wm_event_add(wmWindow *win, const wmEvent *event_to_add)
{
return wm_event_add_ex(win, event_to_add, NULL);
}
wmEvent *WM_event_add_simulate(wmWindow *win, const wmEvent *event_to_add)
{
if ((G.f & G_FLAG_EVENT_SIMULATE) == 0) {
BLI_assert(0);
return NULL;
}
wmEvent *event = wm_event_add(win, event_to_add);
/* Logic for setting previous value is documented on the #wmEvent struct,
* see #wm_event_add_ghostevent for the implementation of logic this follows. */
win->eventstate->x = event->x;
win->eventstate->y = event->y;
if (event->type == MOUSEMOVE) {
win->eventstate->prevx = event->prevx = win->eventstate->x;
win->eventstate->prevy = event->prevy = win->eventstate->y;
}
else if (ISMOUSE_BUTTON(event->type) || ISKEYBOARD(event->type)) {
win->eventstate->prevval = event->prevval = win->eventstate->val;
win->eventstate->prevtype = event->prevtype = win->eventstate->type;
win->eventstate->val = event->val;
win->eventstate->type = event->type;
if (event->val == KM_PRESS) {
if (event->is_repeat == false) {
win->eventstate->prevclickx = event->x;
win->eventstate->prevclicky = event->y;
}
}
}
return event;
}
void wm_event_free(wmEvent *event)
{
#ifndef NDEBUG
/* Don't use assert here because it's fairly harmless in most cases,
* more an issue of correctness, something we should avoid in general. */
if (event->is_repeat && !ISKEYBOARD(event->type)) {
printf("%s: 'is_repeat=true' for non-keyboard event, this should not happen.\n", __func__);
WM_event_print(event);
}
#endif
if (event->customdata) {
if (event->customdatafree) {
/* Note: pointer to listbase struct elsewhere. */
if (event->custom == EVT_DATA_DRAGDROP) {
ListBase *lb = event->customdata;
WM_drag_free_list(lb);
}
else {
MEM_freeN(event->customdata);
}
}
}
MEM_freeN(event);
}
static void wm_event_free_last(wmWindow *win)
{
wmEvent *event = BLI_poptail(&win->event_queue);
if (event != NULL) {
wm_event_free(event);
}
}
void wm_event_free_all(wmWindow *win)
{
wmEvent *event;
while ((event = BLI_pophead(&win->event_queue))) {
wm_event_free(event);
}
}
void wm_event_init_from_window(wmWindow *win, wmEvent *event)
{
*event = *(win->eventstate);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Notifiers & Listeners
* \{ */
static bool wm_test_duplicate_notifier(const wmWindowManager *wm, uint type, void *reference)
{
LISTBASE_FOREACH (wmNotifier *, note, &wm->notifier_queue) {
if ((note->category | note->data | note->subtype | note->action) == type &&
note->reference == reference) {
return true;
}
}
return false;
}
void WM_event_add_notifier_ex(wmWindowManager *wm, const wmWindow *win, uint type, void *reference)
{
if (wm_test_duplicate_notifier(wm, type, reference)) {
return;
}
wmNotifier *note = MEM_callocN(sizeof(wmNotifier), "notifier");
BLI_addtail(&wm->notifier_queue, note);
note->window = win;
note->category = type & NOTE_CATEGORY;
note->data = type & NOTE_DATA;
note->subtype = type & NOTE_SUBTYPE;
note->action = type & NOTE_ACTION;
note->reference = reference;
}
/* XXX: in future, which notifiers to send to other windows? */
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
{
WM_event_add_notifier_ex(CTX_wm_manager(C), CTX_wm_window(C), type, reference);
}
void WM_main_add_notifier(unsigned int type, void *reference)
{
Main *bmain = G_MAIN;
wmWindowManager *wm = bmain->wm.first;
if (!wm || wm_test_duplicate_notifier(wm, type, reference)) {
return;
}
wmNotifier *note = MEM_callocN(sizeof(wmNotifier), "notifier");
BLI_addtail(&wm->notifier_queue, note);
note->category = type & NOTE_CATEGORY;
note->data = type & NOTE_DATA;
note->subtype = type & NOTE_SUBTYPE;
note->action = type & NOTE_ACTION;
note->reference = reference;
}
/**
* Clear notifiers by reference, Used so listeners don't act on freed data.
*/
void WM_main_remove_notifier_reference(const void *reference)
{
Main *bmain = G_MAIN;
wmWindowManager *wm = bmain->wm.first;
if (wm) {
LISTBASE_FOREACH_MUTABLE (wmNotifier *, note, &wm->notifier_queue) {
if (note->reference == reference) {
/* Don't remove because this causes problems for #wm_event_do_notifiers
* which may be looping on the data (deleting screens). */
wm_notifier_clear(note);
}
}
/* Remap instead. */
#if 0
if (wm->message_bus) {
WM_msg_id_remove(wm->message_bus, reference);
}
#endif
}
}
void WM_main_remap_editor_id_reference(ID *old_id, ID *new_id)
{
Main *bmain = G_MAIN;
LISTBASE_FOREACH (bScreen *, screen, &bmain->screens) {
LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
LISTBASE_FOREACH (SpaceLink *, sl, &area->spacedata) {
ED_spacedata_id_remap(area, sl, old_id, new_id);
}
}
}
wmWindowManager *wm = bmain->wm.first;
if (wm && wm->message_bus) {
struct wmMsgBus *mbus = wm->message_bus;
if (new_id != NULL) {
WM_msg_id_update(mbus, old_id, new_id);
}
else {
WM_msg_id_remove(mbus, old_id);
}
}
}
static void wm_notifier_clear(wmNotifier *note)
{
/* NULL the entire notifier, only leaving (next, prev) members intact. */
memset(((char *)note) + sizeof(Link), 0, sizeof(*note) - sizeof(Link));
}
void wm_event_do_depsgraph(bContext *C, bool is_after_open_file)
{
wmWindowManager *wm = CTX_wm_manager(C);
/* The whole idea of locked interface is to prevent viewport and whatever
* thread from modifying the same data. Because of this, we can not perform
* dependency graph update.
*/
if (wm->is_interface_locked) {
return;
}
/* Combine datamasks so one window doesn't disable UV's in another T26448. */
CustomData_MeshMasks win_combine_v3d_datamask = {0};
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
const Scene *scene = WM_window_get_active_scene(win);
const bScreen *screen = WM_window_get_active_screen(win);
ED_view3d_screen_datamask(C, scene, screen, &win_combine_v3d_datamask);
}
/* Update all the dependency graphs of visible view layers. */
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
Scene *scene = WM_window_get_active_scene(win);
ViewLayer *view_layer = WM_window_get_active_view_layer(win);
Main *bmain = CTX_data_main(C);
/* Copied to set's in scene_update_tagged_recursive() */
scene->customdata_mask = win_combine_v3d_datamask;
/* XXX, hack so operators can enforce datamasks T26482, gl render */
CustomData_MeshMasks_update(&scene->customdata_mask, &scene->customdata_mask_modal);
/* TODO(sergey): For now all dependency graphs which are evaluated from
* workspace are considered active. This will work all fine with "locked"
* view layer and time across windows. This is to be granted separately,
* and for until then we have to accept ambiguities when object is shared
* across visible view layers and has overrides on it.
*/
Depsgraph *depsgraph = BKE_scene_ensure_depsgraph(bmain, scene, view_layer);
if (is_after_open_file) {
DEG_graph_relations_update(depsgraph);
DEG_graph_on_visible_update(bmain, depsgraph, true);
}
DEG_make_active(depsgraph);
BKE_scene_graph_update_tagged(depsgraph, bmain);
}
}
/**
* Was part of #wm_event_do_notifiers,
* split out so it can be called once before entering the #WM_main loop.
* This ensures operators don't run before the UI and depsgraph are initialized.
*/
void wm_event_do_refresh_wm_and_depsgraph(bContext *C)
{
wmWindowManager *wm = CTX_wm_manager(C);
/* Cached: editor refresh callbacks now, they get context. */
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
const bScreen *screen = WM_window_get_active_screen(win);
CTX_wm_window_set(C, win);
LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
if (area->do_refresh) {
CTX_wm_area_set(C, area);
ED_area_do_refresh(C, area);
}
}
}
wm_event_do_depsgraph(C, false);
CTX_wm_window_set(C, NULL);
}
static void wm_event_execute_timers(bContext *C)
{
wmWindowManager *wm = CTX_wm_manager(C);
if (UNLIKELY(wm == NULL)) {
return;
}
/* Set the first window as context, so that there is some minimal context. This avoids crashes
* when calling code that assumes that there is always a window in the context (which many
* operators do). */
CTX_wm_window_set(C, wm->windows.first);
BLI_timer_execute();
CTX_wm_window_set(C, NULL);
}
/* Called in mainloop. */
void wm_event_do_notifiers(bContext *C)
{
/* Run the timer before assigning 'wm' in the unlikely case a timer loads a file, see T80028. */
wm_event_execute_timers(C);
wmWindowManager *wm = CTX_wm_manager(C);
if (wm == NULL) {
return;
}
/* Disable? - Keep for now since its used for window level notifiers. */
#if 1
/* Cache & catch WM level notifiers, such as frame change, scene/screen set. */
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
Scene *scene = WM_window_get_active_scene(win);
bool do_anim = false;
bool clear_info_stats = false;
CTX_wm_window_set(C, win);
LISTBASE_FOREACH_MUTABLE (wmNotifier *, note, &wm->notifier_queue) {
if (note->category == NC_WM) {
if (ELEM(note->data, ND_FILEREAD, ND_FILESAVE)) {
wm->file_saved = 1;
wm_window_title(wm, win);
}
else if (note->data == ND_DATACHANGED) {
wm_window_title(wm, win);
}
}
if (note->window == win) {
if (note->category == NC_SCREEN) {
if (note->data == ND_WORKSPACE_SET) {
WorkSpace *ref_ws = note->reference;
UI_popup_handlers_remove_all(C, &win->modalhandlers);
WM_window_set_active_workspace(C, win, ref_ws);
if (G.debug & G_DEBUG_EVENTS) {
printf("%s: Workspace set %p\n", __func__, note->reference);
}
}
else if (note->data == ND_WORKSPACE_DELETE) {
WorkSpace *workspace = note->reference;
ED_workspace_delete(
workspace, CTX_data_main(C), C, wm); /* XXX hrms, think this over! */
if (G.debug & G_DEBUG_EVENTS) {
printf("%s: Workspace delete %p\n", __func__, workspace);
}
}
else if (note->data == ND_LAYOUTBROWSE) {
bScreen *ref_screen = BKE_workspace_layout_screen_get(note->reference);
/* free popup handlers only T35434. */
UI_popup_handlers_remove_all(C, &win->modalhandlers);
ED_screen_change(C, ref_screen); /* XXX hrms, think this over! */
if (G.debug & G_DEBUG_EVENTS) {
printf("%s: screen set %p\n", __func__, note->reference);
}
}
else if (note->data == ND_LAYOUTDELETE) {
WorkSpace *workspace = WM_window_get_active_workspace(win);
WorkSpaceLayout *layout = note->reference;
ED_workspace_layout_delete(workspace, layout, C); /* XXX hrms, think this over! */
if (G.debug & G_DEBUG_EVENTS) {
printf("%s: screen delete %p\n", __func__, note->reference);
}
}
}
}
if (note->window == win ||
(note->window == NULL && (note->reference == NULL || note->reference == scene))) {
if (note->category == NC_SCENE) {
if (note->data == ND_FRAME) {
do_anim = true;
}
}
}
if (ELEM(note->category, NC_SCENE, NC_OBJECT, NC_GEOM, NC_WM)) {
clear_info_stats = true;
}
}
if (clear_info_stats) {
/* Only do once since adding notifiers is slow when there are many. */
ViewLayer *view_layer = CTX_data_view_layer(C);
ED_info_stats_clear(view_layer);
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO, NULL);
}
if (do_anim) {
/* XXX, quick frame changes can cause a crash if framechange and rendering
* collide (happens on slow scenes), BKE_scene_graph_update_for_newframe can be called
* twice which can depgraph update the same object at once */
if (G.is_rendering == false) {
/* Depsgraph gets called, might send more notifiers. */
Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
ED_update_for_newframe(CTX_data_main(C), depsgraph);
}
}
}
/* The notifiers are sent without context, to keep it clean. */
wmNotifier *note;
while ((note = BLI_pophead(&wm->notifier_queue))) {
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
Scene *scene = WM_window_get_active_scene(win);
bScreen *screen = WM_window_get_active_screen(win);
WorkSpace *workspace = WM_window_get_active_workspace(win);
/* Filter out notifiers. */
if (note->category == NC_SCREEN && note->reference && note->reference != screen &&
note->reference != workspace && note->reference != WM_window_get_active_layout(win)) {
/* Pass. */
}
else if (note->category == NC_SCENE && note->reference && note->reference != scene) {
/* Pass. */
}
else {
/* XXX context in notifiers? */
CTX_wm_window_set(C, win);
# if 0
printf("notifier win %d screen %s cat %x\n",
win->winid,
win->screen->id.name + 2,
note->category);
# endif
ED_screen_do_listen(C, note);
LISTBASE_FOREACH (ARegion *, region, &screen->regionbase) {
wmRegionListenerParams region_params = {
.window = win,
.area = NULL,
.region = region,
.scene = scene,
.notifier = note,
};
ED_region_do_listen(®ion_params);
}
ED_screen_areas_iter (win, screen, area) {
wmSpaceTypeListenerParams area_params = {
.window = win,
.area = area,
.notifier = note,
.scene = scene,
};
ED_area_do_listen(&area_params);
LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
wmRegionListenerParams region_params = {
.window = win,
.area = area,
.region = region,
.scene = scene,
.notifier = note,
};
ED_region_do_listen(®ion_params);
}
}
}
}
MEM_freeN(note);
}
#endif /* if 1 (postpone disabling for in favor of message-bus), eventually. */
/* Handle message bus. */
{
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
CTX_wm_window_set(C, win);
WM_msgbus_handle(wm->message_bus, C);
}
CTX_wm_window_set(C, NULL);
}
wm_event_do_refresh_wm_and_depsgraph(C);
/* Status bar */
if (wm->winactive) {
wmWindow *win = wm->winactive;
CTX_wm_window_set(C, win);
WM_window_cursor_keymap_status_refresh(C, win);
CTX_wm_window_set(C, NULL);
}
/* Autorun warning */
wm_test_autorun_warning(C);
}
static int wm_event_always_pass(const wmEvent *event)
{
/* Some events we always pass on, to ensure proper communication. */
return ISTIMER(event->type) || (event->type == WINDEACTIVATE);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name UI Handling
* \{ */
static int wm_handler_ui_call(bContext *C,
wmEventHandler_UI *handler,
const wmEvent *event,
int always_pass)
{
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
ARegion *menu = CTX_wm_menu(C);
static bool do_wheel_ui = true;
const bool is_wheel = ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE, MOUSEPAN);
/* UI code doesn't handle return values - it just always returns break.
* to make the DBL_CLICK conversion work, we just don't send this to UI, except mouse clicks. */
if (((handler->head.flag & WM_HANDLER_ACCEPT_DBL_CLICK) == 0) && !ISMOUSE_BUTTON(event->type) &&
(event->val == KM_DBL_CLICK)) {
return WM_HANDLER_CONTINUE;
}
/* UI is quite aggressive with swallowing events, like scroll-wheel. */
/* I realize this is not extremely nice code... when UI gets keymaps it can be maybe smarter. */
if (do_wheel_ui == false) {
if (is_wheel) {
return WM_HANDLER_CONTINUE;
}
if (wm_event_always_pass(event) == 0) {
do_wheel_ui = true;
}
}
/* Don't block file-select events. Those are triggered by a separate file browser window.
* See T75292. */
if (event->type == EVT_FILESELECT) {
return WM_UI_HANDLER_CONTINUE;
}
/* We set context to where UI handler came from. */
if (handler->context.area) {
CTX_wm_area_set(C, handler->context.area);
}
if (handler->context.region) {
CTX_wm_region_set(C, handler->context.region);
}
if (handler->context.menu) {
CTX_wm_menu_set(C, handler->context.menu);
}
int retval = handler->handle_fn(C, event, handler->user_data);
/* putting back screen context */
if ((retval != WM_UI_HANDLER_BREAK) || always_pass) {
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
CTX_wm_menu_set(C, menu);
}
else {
/* This special cases is for areas and regions that get removed. */
CTX_wm_area_set(C, NULL);
CTX_wm_region_set(C, NULL);
CTX_wm_menu_set(C, NULL);
}
if (retval == WM_UI_HANDLER_BREAK) {
return WM_HANDLER_BREAK;
}
/* event not handled in UI, if wheel then we temporarily disable it */
if (is_wheel) {
do_wheel_ui = false;
}
return WM_HANDLER_CONTINUE;
}
void wm_event_handler_ui_cancel_ex(bContext *C,
wmWindow *win,
ARegion *region,
bool reactivate_button)
{
if (!region) {
return;
}
LISTBASE_FOREACH_MUTABLE (wmEventHandler *, handler_base, ®ion->handlers) {
if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
BLI_assert(handler->handle_fn != NULL);
wmEvent event;
wm_event_init_from_window(win, &event);
event.type = EVT_BUT_CANCEL;
event.val = reactivate_button ? 0 : 1;
event.is_repeat = false;
handler->handle_fn(C, &event, handler->user_data);
}
}
}
static void wm_event_handler_ui_cancel(bContext *C)
{
wmWindow *win = CTX_wm_window(C);
ARegion *region = CTX_wm_region(C);
wm_event_handler_ui_cancel_ex(C, win, region, true);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name WM Reports
*
* Access to #wmWindowManager.reports
* \{ */
/**
* Show the report in the info header.
*/
void WM_report_banner_show(void)
{
wmWindowManager *wm = G_MAIN->wm.first;
ReportList *wm_reports = &wm->reports;
/* After adding reports to the global list, reset the report timer. */
WM_event_remove_timer(wm, NULL, wm_reports->reporttimer);
/* Records time since last report was added */
wm_reports->reporttimer = WM_event_add_timer(wm, wm->winactive, TIMERREPORT, 0.05);
ReportTimerInfo *rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
wm_reports->reporttimer->customdata = rti;
}
/**
* Hide all currently displayed banners and abort their timer.
*/
void WM_report_banners_cancel(Main *bmain)
{
wmWindowManager *wm = bmain->wm.first;
BKE_reports_clear(&wm->reports);
WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
}
#ifdef WITH_INPUT_NDOF
void WM_ndof_deadzone_set(float deadzone)
{
GHOST_setNDOFDeadZone(deadzone);
}
#endif
static void wm_add_reports(ReportList *reports)
{
/* If the caller owns them, handle this. */
if (reports->list.first && (reports->flag & RPT_OP_HOLD) == 0) {
wmWindowManager *wm = G_MAIN->wm.first;
/* add reports to the global list, otherwise they are not seen */
BLI_movelisttolist(&wm->reports.list, &reports->list);
WM_report_banner_show();
}
}
void WM_report(ReportType type, const char *message)
{
ReportList reports;
BKE_reports_init(&reports, RPT_STORE);
BKE_report(&reports, type, message);
wm_add_reports(&reports);
BKE_reports_clear(&reports);
}
void WM_reportf(ReportType type, const char *format, ...)
{
va_list args;
DynStr *ds = BLI_dynstr_new();
va_start(args, format);
BLI_dynstr_vappendf(ds, format, args);
va_end(args);
char *str = BLI_dynstr_get_cstring(ds);
WM_report(type, str);
MEM_freeN(str);
BLI_dynstr_free(ds);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Operator Logic
* \{ */
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
{
LISTBASE_FOREACH (wmOperatorTypeMacro *, macro, &ot->macro) {
wmOperatorType *ot_macro = WM_operatortype_find(macro->idname, 0);
if (!WM_operator_poll(C, ot_macro)) {
return false;
}
}
/* Python needs operator type, so we added exception for it. */
if (ot->pyop_poll) {
return ot->pyop_poll(C, ot);
}
if (ot->poll) {
return ot->poll(C);
}
return true;
}
/* sets up the new context and calls 'wm_operator_invoke()' with poll_only */
bool WM_operator_poll_context(bContext *C, wmOperatorType *ot, short context)
{
return wm_operator_call_internal(C, ot, NULL, NULL, context, true, NULL);
}
bool WM_operator_check_ui_empty(wmOperatorType *ot)
{
if (ot->macro.first != NULL) {
/* For macros, check all have exec() we can call. */
LISTBASE_FOREACH (wmOperatorTypeMacro *, macro, &ot->macro) {
wmOperatorType *otm = WM_operatortype_find(macro->idname, 0);
if (otm && !WM_operator_check_ui_empty(otm)) {
return false;
}
}
return true;
}
/* Assume a UI callback will draw something. */
if (ot->ui) {
return false;
}
PointerRNA ptr;
WM_operator_properties_create_ptr(&ptr, ot);
RNA_STRUCT_BEGIN (&ptr, prop) {
int flag = RNA_property_flag(prop);
if (flag & PROP_HIDDEN) {
continue;
}
return false;
}
RNA_STRUCT_END;
return true;
}
/**
* Sets the active region for this space from the context.
*
* \see #BKE_area_find_region_active_win
*/
void WM_operator_region_active_win_set(bContext *C)
{
ScrArea *area = CTX_wm_area(C);
if (area) {
ARegion *region = CTX_wm_region(C);
if (region && region->regiontype == RGN_TYPE_WINDOW) {
area->region_active_win = BLI_findindex(&area->regionbase, region);
}
}
}
/* (caller_owns_reports == true) when called from python. */
static void wm_operator_reports(bContext *C, wmOperator *op, int retval, bool caller_owns_reports)
{
if (G.background == 0 && caller_owns_reports == false) { /* popup */
if (op->reports->list.first) {
/* FIXME, temp setting window, see other call to UI_popup_menu_reports for why. */
wmWindow *win_prev = CTX_wm_window(C);
ScrArea *area_prev = CTX_wm_area(C);
ARegion *region_prev = CTX_wm_region(C);
if (win_prev == NULL) {
CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
}
UI_popup_menu_reports(C, op->reports);
CTX_wm_window_set(C, win_prev);
CTX_wm_area_set(C, area_prev);
CTX_wm_region_set(C, region_prev);
}
}
if (retval & OPERATOR_FINISHED) {
CLOG_STR_INFO_N(WM_LOG_OPERATORS, 1, WM_operator_pystring(C, op, false, true));
if (caller_owns_reports == false) {
BKE_reports_print(op->reports, RPT_DEBUG); /* Print out reports to console. */
}
if (op->type->flag & OPTYPE_REGISTER) {
if (G.background == 0) { /* Ends up printing these in the terminal, gets annoying. */
/* Report the python string representation of the operator. */
char *buf = WM_operator_pystring(C, op, false, true);
BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
MEM_freeN(buf);
}
}
}
/* Refresh Info Editor with reports immediately, even if op returned OPERATOR_CANCELLED. */
if ((retval & OPERATOR_CANCELLED) && !BLI_listbase_is_empty(&op->reports->list)) {
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
}
/* If the caller owns them, handle this. */
wm_add_reports(op->reports);
}
/**
* This function is mainly to check that the rules for freeing
* an operator are kept in sync.
*/
static bool wm_operator_register_check(wmWindowManager *wm, wmOperatorType *ot)
{
/* Check undo flag here since undo operators are also added to the list,
* to support checking if the same operator is run twice. */
return wm && (wm->op_undo_depth == 0) && (ot->flag & (OPTYPE_REGISTER | OPTYPE_UNDO));
}
static void wm_operator_finished(bContext *C, wmOperator *op, const bool repeat, const bool store)
{
wmWindowManager *wm = CTX_wm_manager(C);
enum {
NOP,
SET,
CLEAR,
} hud_status = NOP;
op->customdata = NULL;
if (store) {
WM_operator_last_properties_store(op);
}
/* We don't want to do undo pushes for operators that are being
* called from operators that already do an undo push. Usually
* this will happen for python operators that call C operators. */
if (wm->op_undo_depth == 0) {
if (op->type->flag & OPTYPE_UNDO) {
ED_undo_push_op(C, op);
if (repeat == 0) {
hud_status = CLEAR;
}
}
else if (op->type->flag & OPTYPE_UNDO_GROUPED) {
ED_undo_grouped_push_op(C, op);
if (repeat == 0) {
hud_status = CLEAR;
}
}
}
if (repeat == 0) {
if (G.debug & G_DEBUG_WM) {
char *buf = WM_operator_pystring(C, op, false, true);
BKE_report(CTX_wm_reports(C), RPT_OPERATOR, buf);
MEM_freeN(buf);
}
if (wm_operator_register_check(wm, op->type)) {
/* take ownership of reports (in case python provided own) */
op->reports->flag |= RPT_FREE;
wm_operator_register(C, op);
WM_operator_region_active_win_set(C);
if (WM_operator_last_redo(C) == op) {
/* Show the redo panel. */
hud_status = SET;
}
}
else {
WM_operator_free(op);
}
}
if (hud_status != NOP) {
if (hud_status == SET) {
ScrArea *area = CTX_wm_area(C);
if (area) {
ED_area_type_hud_ensure(C, area);
}
}
else if (hud_status == CLEAR) {
ED_area_type_hud_clear(wm, NULL);
}
else {
BLI_assert(0);
}
}
}
/* If repeat is true, it doesn't register again, nor does it free. */
static int wm_operator_exec(bContext *C, wmOperator *op, const bool repeat, const bool store)
{
wmWindowManager *wm = CTX_wm_manager(C);
int retval = OPERATOR_CANCELLED;
CTX_wm_operator_poll_msg_set(C, NULL);
if (op == NULL || op->type == NULL) {
return retval;
}
if (0 == WM_operator_poll(C, op->type)) {
return retval;
}
if (op->type->exec) {
if (op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
retval = op->type->exec(C, op);
OPERATOR_RETVAL_CHECK(retval);
if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
wm->op_undo_depth--;
}
}
/* XXX(mont29) Disabled the repeat check to address part 2 of T31840.
* Carefully checked all calls to wm_operator_exec and WM_operator_repeat, don't see any reason
* why this was needed, but worth to note it in case something turns bad. */
if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED) /* && repeat == 0 */) {
wm_operator_reports(C, op, retval, false);
}
if (retval & OPERATOR_FINISHED) {
wm_operator_finished(C, op, repeat, store && wm->op_undo_depth == 0);
}
else if (repeat == 0) {
/* warning: modal from exec is bad practice, but avoid crashing. */
if (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED)) {
WM_operator_free(op);
}
}
return retval | OPERATOR_HANDLED;
}
/* Simply calls exec with basic checks. */
static int wm_operator_exec_notest(bContext *C, wmOperator *op)
{
int retval = OPERATOR_CANCELLED;
if (op == NULL || op->type == NULL || op->type->exec == NULL) {
return retval;
}
retval = op->type->exec(C, op);
OPERATOR_RETVAL_CHECK(retval);
return retval;
}
/**
* For running operators with frozen context (modal handlers, menus).
*
* \param store: Store settings for re-use.
*
* \warning do not use this within an operator to call its self! T29537.
*/
int WM_operator_call_ex(bContext *C, wmOperator *op, const bool store)
{
return wm_operator_exec(C, op, false, store);
}
int WM_operator_call(bContext *C, wmOperator *op)
{
return WM_operator_call_ex(C, op, false);
}
/**
* This is intended to be used when an invoke operator wants to call exec on its self
* and is basically like running op->type->exec() directly, no poll checks no freeing,
* since we assume whoever called invoke will take care of that
*/
int WM_operator_call_notest(bContext *C, wmOperator *op)
{
return wm_operator_exec_notest(C, op);
}
/**
* Execute this operator again, put here so it can share above code
*/
int WM_operator_repeat(bContext *C, wmOperator *op)
{
const int op_flag = OP_IS_REPEAT;
op->flag |= op_flag;
const int ret = wm_operator_exec(C, op, true, true);
op->flag &= ~op_flag;
return ret;
}
int WM_operator_repeat_last(bContext *C, wmOperator *op)
{
const int op_flag = OP_IS_REPEAT_LAST;
op->flag |= op_flag;
const int ret = wm_operator_exec(C, op, true, true);
op->flag &= ~op_flag;
return ret;
}
/**
* \return true if #WM_operator_repeat can run.
* Simple check for now but may become more involved.
* To be sure the operator can run call `WM_operator_poll(C, op->type)` also, since this call
* checks if #WM_operator_repeat() can run at all, not that it WILL run at any time.
*/
bool WM_operator_repeat_check(const bContext *UNUSED(C), wmOperator *op)
{
if (op->type->exec != NULL) {
return true;
}
if (op->opm) {
/* for macros, check all have exec() we can call */
LISTBASE_FOREACH (wmOperatorTypeMacro *, macro, &op->opm->type->macro) {
wmOperatorType *otm = WM_operatortype_find(macro->idname, 0);
if (otm && otm->exec == NULL) {
return false;
}
}
return true;
}
return false;
}
bool WM_operator_is_repeat(const bContext *C, const wmOperator *op)
{
/* May be in the operators list or not. */
wmOperator *op_prev;
if (op->prev == NULL && op->next == NULL) {
wmWindowManager *wm = CTX_wm_manager(C);
op_prev = wm->operators.last;
}
else {
op_prev = op->prev;
}
return (op_prev && (op->type == op_prev->type));
}
static wmOperator *wm_operator_create(wmWindowManager *wm,
wmOperatorType *ot,
PointerRNA *properties,
ReportList *reports)
{
/* XXX operatortype names are static still. for debug */
wmOperator *op = MEM_callocN(sizeof(wmOperator), ot->idname);
/* XXX adding new operator could be function, only happens here now */
op->type = ot;
BLI_strncpy(op->idname, ot->idname, OP_MAX_TYPENAME);
/* Initialize properties, either copy or create. */
op->ptr = MEM_callocN(sizeof(PointerRNA), "wmOperatorPtrRNA");
if (properties && properties->data) {
op->properties = IDP_CopyProperty(properties->data);
}
else {
IDPropertyTemplate val = {0};
op->properties = IDP_New(IDP_GROUP, &val, "wmOperatorProperties");
}
RNA_pointer_create(&wm->id, ot->srna, op->properties, op->ptr);
/* Initialize error reports. */
if (reports) {
op->reports = reports; /* Must be initialized already. */
}
else {
op->reports = MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
BKE_reports_init(op->reports, RPT_STORE | RPT_FREE);
}
/* Recursive filling of operator macro list. */
if (ot->macro.first) {
static wmOperator *motherop = NULL;
int root = 0;
/* Ensure all ops are in execution order in 1 list. */
if (motherop == NULL) {
motherop = op;
root = 1;
}
/* If properties exist, it will contain everything needed. */
if (properties) {
wmOperatorTypeMacro *otmacro = ot->macro.first;
RNA_STRUCT_BEGIN (properties, prop) {
if (otmacro == NULL) {
break;
}
/* Skip invalid properties. */
if (STREQ(RNA_property_identifier(prop), otmacro->idname)) {
wmOperatorType *otm = WM_operatortype_find(otmacro->idname, 0);
PointerRNA someptr = RNA_property_pointer_get(properties, prop);
wmOperator *opm = wm_operator_create(wm, otm, &someptr, NULL);
IDP_ReplaceGroupInGroup(opm->properties, otmacro->properties);
BLI_addtail(&motherop->macro, opm);
opm->opm = motherop; /* Pointer to mom, for modal(). */
otmacro = otmacro->next;
}
}
RNA_STRUCT_END;
}
else {
LISTBASE_FOREACH (wmOperatorTypeMacro *, macro, &ot->macro) {
wmOperatorType *otm = WM_operatortype_find(macro->idname, 0);
wmOperator *opm = wm_operator_create(wm, otm, macro->ptr, NULL);
BLI_addtail(&motherop->macro, opm);
opm->opm = motherop; /* Pointer to mom, for modal(). */
}
}
if (root) {
motherop = NULL;
}
}
WM_operator_properties_sanitize(op->ptr, 0);
return op;
}
static void wm_region_mouse_co(bContext *C, wmEvent *event)
{
ARegion *region = CTX_wm_region(C);
if (region) {
/* Compatibility convention. */
event->mval[0] = event->x - region->winrct.xmin;
event->mval[1] = event->y - region->winrct.ymin;
}
else {
/* These values are invalid (avoid odd behavior by relying on old mval values). */
event->mval[0] = -1;
event->mval[1] = -1;
}
}
/**
* Also used for exec when 'event' is NULL.
*/
static int wm_operator_invoke(bContext *C,
wmOperatorType *ot,
wmEvent *event,
PointerRNA *properties,
ReportList *reports,
const bool poll_only,
bool use_last_properties)
{
int retval = OPERATOR_PASS_THROUGH;
/* This is done because complicated setup is done to call this function
* that is better not duplicated. */
if (poll_only) {
return WM_operator_poll(C, ot);
}
if (WM_operator_poll(C, ot)) {
wmWindowManager *wm = CTX_wm_manager(C);
/* If reports == NULL, they'll be initialized. */
wmOperator *op = wm_operator_create(wm, ot, properties, reports);
const bool is_nested_call = (wm->op_undo_depth != 0);
if (event != NULL) {
op->flag |= OP_IS_INVOKE;
}
/* /initialize setting from previous run. */
if (!is_nested_call && use_last_properties) { /* Not called by py script. */
WM_operator_last_properties_init(op);
}
if ((event == NULL) || (event->type != MOUSEMOVE)) {
CLOG_INFO(WM_LOG_HANDLERS,
2,
"handle evt %d win %p op %s",
event ? event->type : 0,
CTX_wm_screen(C)->active_region,
ot->idname);
}
if (op->type->invoke && event) {
wm_region_mouse_co(C, event);
if (op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
retval = op->type->invoke(C, op, event);
OPERATOR_RETVAL_CHECK(retval);
if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
wm->op_undo_depth--;
}
}
else if (op->type->exec) {
if (op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
retval = op->type->exec(C, op);
OPERATOR_RETVAL_CHECK(retval);
if (op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
wm->op_undo_depth--;
}
}
else {
/* Debug, important to leave a while, should never happen. */
CLOG_ERROR(WM_LOG_OPERATORS, "invalid operator call '%s'", op->idname);
}
/* Note, if the report is given as an argument then assume the caller will deal with displaying
* them currently Python only uses this. */
if (!(retval & OPERATOR_HANDLED) && (retval & (OPERATOR_FINISHED | OPERATOR_CANCELLED))) {
/* Only show the report if the report list was not given in the function. */
wm_operator_reports(C, op, retval, (reports != NULL));
}
if (retval & OPERATOR_HANDLED) {
/* Do nothing, wm_operator_exec() has been called somewhere. */
}
else if (retval & OPERATOR_FINISHED) {
const bool store = !is_nested_call && use_last_properties;
wm_operator_finished(C, op, false, store);
}
else if (retval & OPERATOR_RUNNING_MODAL) {
/* Take ownership of reports (in case python provided own). */
op->reports->flag |= RPT_FREE;
/* Grab cursor during blocking modal ops (X11)
* Also check for macro.
*/
if (ot->flag & OPTYPE_BLOCKING || (op->opm && op->opm->type->flag & OPTYPE_BLOCKING)) {
int bounds[4] = {-1, -1, -1, -1};
int wrap = WM_CURSOR_WRAP_NONE;
if (event && (U.uiflag & USER_CONTINUOUS_MOUSE)) {
const wmOperator *op_test = op->opm ? op->opm : op;
const wmOperatorType *ot_test = op_test->type;
if ((ot_test->flag & OPTYPE_GRAB_CURSOR_XY) ||
(op_test->flag & OP_IS_MODAL_GRAB_CURSOR)) {
wrap = WM_CURSOR_WRAP_XY;
}
else if (ot_test->flag & OPTYPE_GRAB_CURSOR_X) {
wrap = WM_CURSOR_WRAP_X;
}
else if (ot_test->flag & OPTYPE_GRAB_CURSOR_Y) {
wrap = WM_CURSOR_WRAP_Y;
}
}
if (wrap) {
const rcti *winrect = NULL;
ARegion *region = CTX_wm_region(C);
ScrArea *area = CTX_wm_area(C);
/* Wrap only in X for header. */
if (region && RGN_TYPE_IS_HEADER_ANY(region->regiontype)) {
wrap = WM_CURSOR_WRAP_X;
}
if (region && region->regiontype == RGN_TYPE_WINDOW &&
BLI_rcti_isect_pt_v(®ion->winrct, &event->x)) {
winrect = ®ion->winrct;
}
else if (area && BLI_rcti_isect_pt_v(&area->totrct, &event->x)) {
winrect = &area->totrct;
}
if (winrect) {
bounds[0] = winrect->xmin;
bounds[1] = winrect->ymax;
bounds[2] = winrect->xmax;
bounds[3] = winrect->ymin;
}
}
WM_cursor_grab_enable(CTX_wm_window(C), wrap, false, bounds);
}
/* Cancel UI handlers, typically tooltips that can hang around
* while dragging the view or worse, that stay there permanently
* after the modal operator has swallowed all events and passed
* none to the UI handler. */
wm_event_handler_ui_cancel(C);
}
else {
WM_operator_free(op);
}
}
return retval;
}
/**
* #WM_operator_name_call is the main accessor function
* This is for python to access since its done the operator lookup
* invokes operator in context.
*/
static int wm_operator_call_internal(bContext *C,
wmOperatorType *ot,
PointerRNA *properties,
ReportList *reports,
const short context,
const bool poll_only,
wmEvent *event)
{
int retval;
CTX_wm_operator_poll_msg_set(C, NULL);
/* Dummy test. */
if (ot) {
wmWindow *window = CTX_wm_window(C);
if (event == NULL) {
switch (context) {
case WM_OP_INVOKE_DEFAULT:
case WM_OP_INVOKE_REGION_WIN:
case WM_OP_INVOKE_REGION_PREVIEW:
case WM_OP_INVOKE_REGION_CHANNELS:
case WM_OP_INVOKE_AREA:
case WM_OP_INVOKE_SCREEN:
/* Window is needed for invoke and cancel operators. */
if (window == NULL) {
if (poll_only) {
CTX_wm_operator_poll_msg_set(C, "Missing 'window' in context");
}
return 0;
}
else {
event = window->eventstate;
}
break;
default:
event = NULL;
break;
}
}
else {
switch (context) {
case WM_OP_EXEC_DEFAULT:
case WM_OP_EXEC_REGION_WIN:
case WM_OP_EXEC_REGION_PREVIEW:
case WM_OP_EXEC_REGION_CHANNELS:
case WM_OP_EXEC_AREA:
case WM_OP_EXEC_SCREEN:
event = NULL;
default:
break;
}
}
switch (context) {
case WM_OP_EXEC_REGION_WIN:
case WM_OP_INVOKE_REGION_WIN:
case WM_OP_EXEC_REGION_CHANNELS:
case WM_OP_INVOKE_REGION_CHANNELS:
case WM_OP_EXEC_REGION_PREVIEW:
case WM_OP_INVOKE_REGION_PREVIEW: {
/* Forces operator to go to the region window/channels/preview, for header menus,
* but we stay in the same region if we are already in one.
*/
ARegion *region = CTX_wm_region(C);
ScrArea *area = CTX_wm_area(C);
int type = RGN_TYPE_WINDOW;
switch (context) {
case WM_OP_EXEC_REGION_CHANNELS:
case WM_OP_INVOKE_REGION_CHANNELS:
type = RGN_TYPE_CHANNELS;
break;
case WM_OP_EXEC_REGION_PREVIEW:
case WM_OP_INVOKE_REGION_PREVIEW:
type = RGN_TYPE_PREVIEW;
break;
case WM_OP_EXEC_REGION_WIN:
case WM_OP_INVOKE_REGION_WIN:
default:
type = RGN_TYPE_WINDOW;
break;
}
if (!(region && region->regiontype == type) && area) {
ARegion *region_other = (type == RGN_TYPE_WINDOW) ?
BKE_area_find_region_active_win(area) :
BKE_area_find_region_type(area, type);
if (region_other) {
CTX_wm_region_set(C, region_other);
}
}
retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only, true);
/* Set region back. */
CTX_wm_region_set(C, region);
return retval;
}
case WM_OP_EXEC_AREA:
case WM_OP_INVOKE_AREA: {
/* Remove region from context. */
ARegion *region = CTX_wm_region(C);
CTX_wm_region_set(C, NULL);
retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only, true);
CTX_wm_region_set(C, region);
return retval;
}
case WM_OP_EXEC_SCREEN:
case WM_OP_INVOKE_SCREEN: {
/* Remove region + area from context. */
ARegion *region = CTX_wm_region(C);
ScrArea *area = CTX_wm_area(C);
CTX_wm_region_set(C, NULL);
CTX_wm_area_set(C, NULL);
retval = wm_operator_invoke(C, ot, event, properties, reports, poll_only, true);
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
return retval;
}
case WM_OP_EXEC_DEFAULT:
case WM_OP_INVOKE_DEFAULT:
return wm_operator_invoke(C, ot, event, properties, reports, poll_only, true);
}
}
return 0;
}
/* Invokes operator in context. */
int WM_operator_name_call_ptr(bContext *C,
wmOperatorType *ot,
short context,
PointerRNA *properties)
{
BLI_assert(ot == WM_operatortype_find(ot->idname, true));
return wm_operator_call_internal(C, ot, properties, NULL, context, false, NULL);
}
int WM_operator_name_call(bContext *C, const char *opstring, short context, PointerRNA *properties)
{
wmOperatorType *ot = WM_operatortype_find(opstring, 0);
if (ot) {
return WM_operator_name_call_ptr(C, ot, context, properties);
}
return 0;
}
int WM_operator_name_call_with_properties(struct bContext *C,
const char *opstring,
short context,
struct IDProperty *properties)
{
PointerRNA props_ptr;
wmOperatorType *ot = WM_operatortype_find(opstring, false);
RNA_pointer_create(NULL, ot->srna, properties, &props_ptr);
return WM_operator_name_call_ptr(C, ot, context, &props_ptr);
}
/**
* Call an existent menu. The menu can be created in C or Python.
*/
void WM_menu_name_call(bContext *C, const char *menu_name, short context)
{
wmOperatorType *ot = WM_operatortype_find("WM_OT_call_menu", false);
PointerRNA ptr;
WM_operator_properties_create_ptr(&ptr, ot);
RNA_string_set(&ptr, "name", menu_name);
WM_operator_name_call_ptr(C, ot, context, &ptr);
WM_operator_properties_free(&ptr);
}
/**
* Similar to #WM_operator_name_call called with #WM_OP_EXEC_DEFAULT context.
*
* - #wmOperatorType is used instead of operator name since python already has the operator type.
* - `poll()` must be called by python before this runs.
* - reports can be passed to this function (so python can report them as exceptions).
*/
int WM_operator_call_py(bContext *C,
wmOperatorType *ot,
short context,
PointerRNA *properties,
ReportList *reports,
const bool is_undo)
{
int retval = OPERATOR_CANCELLED;
#if 0
wmOperator *op;
op = wm_operator_create(wm, ot, properties, reports);
if (op->type->exec) {
if (is_undo && op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
retval = op->type->exec(C, op);
OPERATOR_RETVAL_CHECK(retval);
if (is_undo && op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
wm->op_undo_depth--;
}
}
else {
CLOG_WARN(WM_LOG_OPERATORS,
"\"%s\" operator has no exec function, Python cannot call it",
op->type->name);
}
#endif
/* Not especially nice using undo depth here. It's used so Python never
* triggers undo or stores an operator's last used state.
*
* We could have some more obvious way of doing this like passing a flag.
*/
wmWindowManager *wm = CTX_wm_manager(C);
if (!is_undo && wm) {
wm->op_undo_depth++;
}
retval = wm_operator_call_internal(C, ot, properties, reports, context, false, NULL);
if (!is_undo && wm && (wm == CTX_wm_manager(C))) {
wm->op_undo_depth--;
}
return retval;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Handler Types
*
* General API for different handler types.
* \{ */
/* Future extra customadata free? */
void wm_event_free_handler(wmEventHandler *handler)
{
MEM_freeN(handler);
}
/* Only set context when area/region is part of screen. */
static void wm_handler_op_context(bContext *C, wmEventHandler_Op *handler, const wmEvent *event)
{
wmWindow *win = handler->context.win ? handler->context.win : CTX_wm_window(C);
/* It's probably fine to always use WM_window_get_active_screen() to get the screen. But this
* code has been getting it through context since forever, so play safe and stick to that when
* possible. */
bScreen *screen = handler->context.win ? WM_window_get_active_screen(win) : CTX_wm_screen(C);
if (screen == NULL || handler->op == NULL) {
return;
}
if (handler->context.area == NULL) {
CTX_wm_area_set(C, NULL);
}
else {
ScrArea *area = NULL;
ED_screen_areas_iter (win, screen, area_iter) {
if (area_iter == handler->context.area) {
area = area_iter;
break;
}
}
if (area == NULL) {
/* When changing screen layouts with running modal handlers (like render display), this
* is not an error to print. */
if (handler->op == NULL) {
CLOG_ERROR(WM_LOG_HANDLERS,
"internal error: handler (%s) has invalid area",
handler->op->type->idname);
}
}
else {
ARegion *region;
wmOperator *op = handler->op ? (handler->op->opm ? handler->op->opm : handler->op) : NULL;
CTX_wm_area_set(C, area);
if (op && (op->flag & OP_IS_MODAL_CURSOR_REGION)) {
region = BKE_area_find_region_xy(area, handler->context.region_type, event->x, event->y);
if (region) {
handler->context.region = region;
}
}
else {
region = NULL;
}
if (region == NULL) {
LISTBASE_FOREACH (ARegion *, region_iter, &area->regionbase) {
region = region_iter;
if (region == handler->context.region) {
break;
}
}
}
/* XXX no warning print here, after full-area and back regions are remade. */
if (region) {
CTX_wm_region_set(C, region);
}
}
}
}
/* Called on exit or remove area, only here call cancel callback. */
void WM_event_remove_handlers(bContext *C, ListBase *handlers)
{
wmWindowManager *wm = CTX_wm_manager(C);
/* C is zero on freeing database, modal handlers then already were freed */
wmEventHandler *handler_base;
while ((handler_base = BLI_pophead(handlers))) {
BLI_assert(handler_base->type != 0);
if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
if (handler->op) {
wmWindow *win = CTX_wm_window(C);
if (handler->is_fileselect) {
/* Exit File Browsers referring to this handler/operator. */
LISTBASE_FOREACH (wmWindow *, temp_win, &wm->windows) {
ScrArea *file_area = ED_fileselect_handler_area_find(temp_win, handler->op);
if (!file_area) {
continue;
}
ED_area_exit(C, file_area);
}
}
if (handler->op->type->cancel) {
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
wm_handler_op_context(C, handler, win->eventstate);
if (handler->op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
handler->op->type->cancel(C, handler->op);
if (handler->op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth--;
}
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
}
WM_cursor_grab_disable(win, NULL);
WM_operator_free(handler->op);
}
}
else if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
if (handler->remove_fn) {
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
ARegion *menu = CTX_wm_menu(C);
if (handler->context.area) {
CTX_wm_area_set(C, handler->context.area);
}
if (handler->context.region) {
CTX_wm_region_set(C, handler->context.region);
}
if (handler->context.menu) {
CTX_wm_menu_set(C, handler->context.menu);
}
handler->remove_fn(C, handler->user_data);
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
CTX_wm_menu_set(C, menu);
}
}
wm_event_free_handler(handler_base);
}
}
static bool wm_eventmatch(const wmEvent *winevent, const wmKeyMapItem *kmi)
{
if (kmi->flag & KMI_INACTIVE) {
return false;
}
if (winevent->is_repeat) {
if (kmi->flag & KMI_REPEAT_IGNORE) {
return false;
}
}
const int kmitype = WM_userdef_event_map(kmi->type);
/* The matching rules. */
if (kmitype == KM_TEXTINPUT) {
if (winevent->val == KM_PRESS) { /* Prevent double clicks. */
/* NOT using ISTEXTINPUT anymore because (at least on Windows) some key codes above 255
* could have printable ascii keys - BUG T30479. */
if (ISKEYBOARD(winevent->type) && (winevent->ascii || winevent->utf8_buf[0])) {
return true;
}
}
}
if (kmitype != KM_ANY) {
if (ELEM(kmitype, TABLET_STYLUS, TABLET_ERASER)) {
const wmTabletData *wmtab = &winevent->tablet;
if (winevent->type != LEFTMOUSE) {
/* Tablet events can occur on hover + keypress. */
return false;
}
if ((kmitype == TABLET_STYLUS) && (wmtab->active != EVT_TABLET_STYLUS)) {
return false;
}
if ((kmitype == TABLET_ERASER) && (wmtab->active != EVT_TABLET_ERASER)) {
return false;
}
}
else {
if (winevent->type != kmitype) {
return false;
}
}
}
if (kmi->val != KM_ANY) {
if (winevent->val != kmi->val) {
return false;
}
}
/* Modifiers also check bits, so it allows modifier order.
* Account for rare case of when these keys are used as the 'type' not as modifiers. */
if (kmi->shift != KM_ANY) {
if ((winevent->shift != kmi->shift) && !(winevent->shift & kmi->shift) &&
!ELEM(winevent->type, EVT_LEFTSHIFTKEY, EVT_RIGHTSHIFTKEY)) {
return false;
}
}
if (kmi->ctrl != KM_ANY) {
if (winevent->ctrl != kmi->ctrl && !(winevent->ctrl & kmi->ctrl) &&
!ELEM(winevent->type, EVT_LEFTCTRLKEY, EVT_RIGHTCTRLKEY)) {
return false;
}
}
if (kmi->alt != KM_ANY) {
if (winevent->alt != kmi->alt && !(winevent->alt & kmi->alt) &&
!ELEM(winevent->type, EVT_LEFTALTKEY, EVT_RIGHTALTKEY)) {
return false;
}
}
if (kmi->oskey != KM_ANY) {
if (winevent->oskey != kmi->oskey && !(winevent->oskey & kmi->oskey) &&
(winevent->type != EVT_OSKEY)) {
return false;
}
}
/* Only keymap entry with keymodifier is checked,
* means all keys without modifier get handled too. */
/* That is currently needed to make overlapping events work (when you press A - G fast or so). */
if (kmi->keymodifier) {
if (winevent->keymodifier != kmi->keymodifier) {
return false;
}
}
return true;
}
static wmKeyMapItem *wm_eventmatch_modal_keymap_items(const wmKeyMap *keymap,
wmOperator *op,
const wmEvent *event)
{
LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
/* Should already be handled by #wm_user_modal_keymap_set_items. */
BLI_assert(kmi->propvalue_str[0] == '\0');
if (wm_eventmatch(event, kmi)) {
if ((keymap->poll_modal_item == NULL) || (keymap->poll_modal_item(op, kmi->propvalue))) {
return kmi;
}
}
}
return NULL;
}
struct wmEvent_ModalMapStore {
short prevtype;
short prevval;
bool dbl_click_disabled;
};
/**
* This function prepares events for use with #wmOperatorType.modal by:
*
* - Matching keymap items with the operators modal keymap.
* - Converting double click events into press events,
* allowing them to be restored when the events aren't handled.
*
* This is done since we only want to use double click events to match key-map items,
* allowing modal functions to check for press/release events without having to interpret them.
*/
static void wm_event_modalkeymap_begin(const bContext *C,
wmOperator *op,
wmEvent *event,
struct wmEvent_ModalMapStore *event_backup)
{
BLI_assert(event->type != EVT_MODAL_MAP);
/* Support for modal keymap in macros. */
if (op->opm) {
op = op->opm;
}
event_backup->dbl_click_disabled = false;
if (op->type->modalkeymap) {
wmKeyMap *keymap = WM_keymap_active(CTX_wm_manager(C), op->type->modalkeymap);
wmKeyMapItem *kmi = NULL;
const wmEvent *event_match = NULL;
wmEvent event_no_dbl_click;
if ((kmi = wm_eventmatch_modal_keymap_items(keymap, op, event))) {
event_match = event;
}
else if (event->val == KM_DBL_CLICK) {
event_no_dbl_click = *event;
event_no_dbl_click.val = KM_PRESS;
if ((kmi = wm_eventmatch_modal_keymap_items(keymap, op, &event_no_dbl_click))) {
event_match = &event_no_dbl_click;
}
}
if (event_match != NULL) {
event_backup->prevtype = event->prevtype;
event_backup->prevval = event->prevval;
event->prevtype = event_match->type;
event->prevval = event_match->val;
event->type = EVT_MODAL_MAP;
event->val = kmi->propvalue;
/* Avoid double-click events even in the case of 'EVT_MODAL_MAP',
* since it's possible users configure double-click keymap items
* which would break when modal functions expect press/release. */
if (event->prevtype == KM_DBL_CLICK) {
event->prevtype = KM_PRESS;
event_backup->dbl_click_disabled = true;
}
}
}
if (event->type != EVT_MODAL_MAP) {
/* This bypass just disables support for double-click in modal handlers. */
if (event->val == KM_DBL_CLICK) {
event->val = KM_PRESS;
event_backup->dbl_click_disabled = true;
}
}
}
/**
* Restore changes from #wm_event_modalkeymap_begin
*
* \warning bad hacking event system...
* better restore event type for checking of #KM_CLICK for example.
* Modal maps could use different method (ton).
*/
static void wm_event_modalkeymap_end(wmEvent *event,
const struct wmEvent_ModalMapStore *event_backup)
{
if (event->type == EVT_MODAL_MAP) {
event->type = event->prevtype;
event->val = event->prevval;
event->prevtype = event_backup->prevtype;
event->prevval = event_backup->prevval;
}
if (event_backup->dbl_click_disabled) {
event->val = KM_DBL_CLICK;
}
}
/* Warning: this function removes a modal handler, when finished */
static int wm_handler_operator_call(bContext *C,
ListBase *handlers,
wmEventHandler *handler_base,
wmEvent *event,
PointerRNA *properties,
const char *kmi_idname)
{
int retval = OPERATOR_PASS_THROUGH;
/* Derived, modal or blocking operator. */
if ((handler_base->type == WM_HANDLER_TYPE_OP) &&
(((wmEventHandler_Op *)handler_base)->op != NULL)) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
wmOperator *op = handler->op;
wmOperatorType *ot = op->type;
if (!wm_operator_check_locked_interface(C, ot)) {
/* Interface is locked and operator is not allowed to run,
* nothing to do in this case.
*/
}
else if (ot->modal) {
/* We set context to where modal handler came from. */
wmWindowManager *wm = CTX_wm_manager(C);
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
wm_handler_op_context(C, handler, event);
wm_region_mouse_co(C, event);
struct wmEvent_ModalMapStore event_backup;
wm_event_modalkeymap_begin(C, op, event, &event_backup);
if (ot->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
/* Warning, after this call all context data and 'event' may be freed. see check below. */
retval = ot->modal(C, op, event);
OPERATOR_RETVAL_CHECK(retval);
/* When this is _not_ the case the modal modifier may have loaded
* a new blend file (demo mode does this), so we have to assume
* the event, operator etc have all been freed. - campbell */
if (CTX_wm_manager(C) == wm) {
wm_event_modalkeymap_end(event, &event_backup);
if (ot->flag & OPTYPE_UNDO) {
wm->op_undo_depth--;
}
if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
wm_operator_reports(C, op, retval, false);
if (op->type->modalkeymap) {
wmWindow *win = CTX_wm_window(C);
WM_window_status_area_tag_redraw(win);
}
}
else {
/* Not very common, but modal operators may report before finishing. */
if (!BLI_listbase_is_empty(&op->reports->list)) {
wm_add_reports(op->reports);
}
}
/* Important to run 'wm_operator_finished' before NULLing the context members. */
if (retval & OPERATOR_FINISHED) {
wm_operator_finished(C, op, false, true);
handler->op = NULL;
}
else if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
WM_operator_free(op);
handler->op = NULL;
}
/* Putting back screen context, reval can pass through after modal failures! */
if ((retval & OPERATOR_PASS_THROUGH) || wm_event_always_pass(event)) {
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
}
else {
/* This special cases is for areas and regions that get removed. */
CTX_wm_area_set(C, NULL);
CTX_wm_region_set(C, NULL);
}
/* /update gizmos during modal handlers. */
wm_gizmomaps_handled_modal_update(C, event, handler);
/* Remove modal handler, operator itself should have been canceled and freed. */
if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
WM_cursor_grab_disable(CTX_wm_window(C), NULL);
BLI_remlink(handlers, handler);
wm_event_free_handler(&handler->head);
/* prevent silly errors from operator users */
// retval &= ~OPERATOR_PASS_THROUGH;
}
}
}
else {
CLOG_ERROR(WM_LOG_HANDLERS, "missing modal '%s'", op->idname);
}
}
else {
wmOperatorType *ot = WM_operatortype_find(kmi_idname, 0);
if (ot && wm_operator_check_locked_interface(C, ot)) {
bool use_last_properties = true;
PointerRNA tool_properties = {0};
bToolRef *keymap_tool = NULL;
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
keymap_tool = ((wmEventHandler_Keymap *)handler_base)->keymap_tool;
}
else if (handler_base->type == WM_HANDLER_TYPE_GIZMO) {
wmGizmoMap *gizmo_map = ((wmEventHandler_Gizmo *)handler_base)->gizmo_map;
wmGizmo *gz = wm_gizmomap_highlight_get(gizmo_map);
if (gz && (gz->flag & WM_GIZMO_OPERATOR_TOOL_INIT)) {
keymap_tool = WM_toolsystem_ref_from_context(C);
}
}
const bool is_tool = (keymap_tool != NULL);
const bool use_tool_properties = is_tool;
if (use_tool_properties) {
WM_toolsystem_ref_properties_init_for_keymap(
keymap_tool, &tool_properties, properties, ot);
properties = &tool_properties;
use_last_properties = false;
}
retval = wm_operator_invoke(C, ot, event, properties, NULL, false, use_last_properties);
if (use_tool_properties) {
WM_operator_properties_free(&tool_properties);
}
/* Link gizmo if 'WM_GIZMOGROUPTYPE_TOOL_INIT' is set. */
if (retval & OPERATOR_FINISHED) {
if (is_tool) {
bToolRef_Runtime *tref_rt = keymap_tool->runtime;
if (tref_rt->gizmo_group[0]) {
const char *idname = tref_rt->gizmo_group;
wmGizmoGroupType *gzgt = WM_gizmogrouptype_find(idname, false);
if (gzgt != NULL) {
if ((gzgt->flag & WM_GIZMOGROUPTYPE_TOOL_INIT) != 0) {
ARegion *region = CTX_wm_region(C);
if (region != NULL) {
wmGizmoMapType *gzmap_type = WM_gizmomaptype_ensure(&gzgt->gzmap_params);
WM_gizmo_group_type_ensure_ptr_ex(gzgt, gzmap_type);
wmGizmoGroup *gzgroup = WM_gizmomaptype_group_init_runtime_with_region(
gzmap_type, gzgt, region);
/* We can't rely on drawing to initialize gizmo's since disabling
* overlays/gizmos will prevent pre-drawing setup calls. (see T60905) */
WM_gizmogroup_ensure_init(C, gzgroup);
}
}
}
}
}
}
/* Done linking gizmo. */
}
}
/* Finished and pass through flag as handled. */
/* Finished and pass through flag as handled. */
if (retval == (OPERATOR_FINISHED | OPERATOR_PASS_THROUGH)) {
return WM_HANDLER_HANDLED;
}
/* Modal unhandled, break. */
if (retval == (OPERATOR_PASS_THROUGH | OPERATOR_RUNNING_MODAL)) {
return (WM_HANDLER_BREAK | WM_HANDLER_MODAL);
}
if (retval & OPERATOR_PASS_THROUGH) {
return WM_HANDLER_CONTINUE;
}
return WM_HANDLER_BREAK;
}
/* Fileselect handlers are only in the window queue,
* so it's safe to switch screens or area types. */
static int wm_handler_fileselect_do(bContext *C,
ListBase *handlers,
wmEventHandler_Op *handler,
int val)
{
wmWindowManager *wm = CTX_wm_manager(C);
int action = WM_HANDLER_CONTINUE;
switch (val) {
case EVT_FILESELECT_FULL_OPEN: {
wmWindow *win = CTX_wm_window(C);
ScrArea *area;
if ((area = ED_screen_temp_space_open(C,
IFACE_("Blender File View"),
WM_window_pixels_x(win) / 2,
WM_window_pixels_y(win) / 2,
U.file_space_data.temp_win_sizex * UI_DPI_FAC,
U.file_space_data.temp_win_sizey * UI_DPI_FAC,
SPACE_FILE,
U.filebrowser_display_type,
true))) {
ARegion *region_header = BKE_area_find_region_type(area, RGN_TYPE_HEADER);
BLI_assert(area->spacetype == SPACE_FILE);
region_header->flag |= RGN_FLAG_HIDDEN;
/* Header on bottom, AZone triangle to toggle header looks misplaced at the top. */
region_header->alignment = RGN_ALIGN_BOTTOM;
/* Settings for filebrowser, #sfile is not operator owner but sends events. */
SpaceFile *sfile = (SpaceFile *)area->spacedata.first;
sfile->op = handler->op;
ED_fileselect_set_params_from_userdef(sfile);
}
else {
BKE_report(&wm->reports, RPT_ERROR, "Failed to open window!");
return OPERATOR_CANCELLED;
}
action = WM_HANDLER_BREAK;
break;
}
case EVT_FILESELECT_EXEC:
case EVT_FILESELECT_CANCEL:
case EVT_FILESELECT_EXTERNAL_CANCEL: {
wmWindow *ctx_win = CTX_wm_window(C);
/* Remove link now, for load file case before removing. */
BLI_remlink(handlers, handler);
if (val == EVT_FILESELECT_EXTERNAL_CANCEL) {
/* The window might have been freed already. */
if (BLI_findindex(&wm->windows, handler->context.win) == -1) {
handler->context.win = NULL;
}
}
else {
ScrArea *ctx_area = CTX_wm_area(C);
wmWindow *temp_win = NULL;
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
bScreen *screen = WM_window_get_active_screen(win);
ScrArea *file_area = screen->areabase.first;
if ((file_area->spacetype != SPACE_FILE) || !WM_window_is_temp_screen(win)) {
continue;
}
if (file_area->full) {
/* Users should not be able to maximize/fullscreen an area in a temporary screen. So if
* there's a maximized file browser in a temporary screen, it was likely opened by
* #EVT_FILESELECT_FULL_OPEN. */
continue;
}
int win_size[2];
bool is_maximized;
ED_fileselect_window_params_get(win, win_size, &is_maximized);
ED_fileselect_params_to_userdef(file_area->spacedata.first, win_size, is_maximized);
if (BLI_listbase_is_single(&file_area->spacedata)) {
BLI_assert(ctx_win != win);
wm_window_close(C, wm, win);
CTX_wm_window_set(C, ctx_win); /* #wm_window_close() NULLs. */
/* Some operators expect a drawable context (for EVT_FILESELECT_EXEC). */
wm_window_make_drawable(wm, ctx_win);
/* Ensure correct cursor position, otherwise, popups may close immediately after
* opening (UI_BLOCK_MOVEMOUSE_QUIT). */
wm_get_cursor_position(ctx_win, &ctx_win->eventstate->x, &ctx_win->eventstate->y);
wm->winactive = ctx_win; /* Reports use this... */
if (handler->context.win == win) {
handler->context.win = NULL;
}
}
else if (file_area->full) {
ED_screen_full_prevspace(C, file_area);
}
else {
ED_area_prevspace(C, file_area);
}
temp_win = win;
break;
}
if (!temp_win && ctx_area->full) {
ED_fileselect_params_to_userdef(ctx_area->spacedata.first, NULL, false);
ED_screen_full_prevspace(C, ctx_area);
}
}
wm_handler_op_context(C, handler, ctx_win->eventstate);
ScrArea *handler_area = CTX_wm_area(C);
/* Make sure new context area is ready, the operator callback may operate on it. */
if (handler_area) {
ED_area_do_refresh(C, handler_area);
}
/* Needed for #UI_popup_menu_reports. */
if (val == EVT_FILESELECT_EXEC) {
int retval;
if (handler->op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
retval = handler->op->type->exec(C, handler->op);
/* XXX check this carefully, CTX_wm_manager(C) == wm is a bit hackish */
if (handler->op->type->flag & OPTYPE_UNDO && CTX_wm_manager(C) == wm) {
wm->op_undo_depth--;
}
/* XXX check this carefully, CTX_wm_manager(C) == wm is a bit hackish */
if (CTX_wm_manager(C) == wm && wm->op_undo_depth == 0) {
if (handler->op->type->flag & OPTYPE_UNDO) {
ED_undo_push_op(C, handler->op);
}
else if (handler->op->type->flag & OPTYPE_UNDO_GROUPED) {
ED_undo_grouped_push_op(C, handler->op);
}
}
if (handler->op->reports->list.first) {
/* FIXME, temp setting window, this is really bad!
* only have because lib linking errors need to be seen by users :(
* it can be removed without breaking anything but then no linking errors - campbell */
wmWindow *win_prev = CTX_wm_window(C);
ScrArea *area_prev = CTX_wm_area(C);
ARegion *region_prev = CTX_wm_region(C);
if (win_prev == NULL) {
CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
}
BKE_report_print_level_set(handler->op->reports, RPT_WARNING);
UI_popup_menu_reports(C, handler->op->reports);
/* XXX - copied from 'wm_operator_finished()' */
/* add reports to the global list, otherwise they are not seen */
BLI_movelisttolist(&CTX_wm_reports(C)->list, &handler->op->reports->list);
/* More hacks, since we meddle with reports, banner display doesn't happen automaticM */
WM_report_banner_show();
CTX_wm_window_set(C, win_prev);
CTX_wm_area_set(C, area_prev);
CTX_wm_region_set(C, region_prev);
}
/* For WM_operator_pystring only, custom report handling is done above. */
wm_operator_reports(C, handler->op, retval, true);
if (retval & OPERATOR_FINISHED) {
WM_operator_last_properties_store(handler->op);
}
if (retval & (OPERATOR_CANCELLED | OPERATOR_FINISHED)) {
WM_operator_free(handler->op);
}
}
else {
if (handler->op->type->cancel) {
if (handler->op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth++;
}
handler->op->type->cancel(C, handler->op);
if (handler->op->type->flag & OPTYPE_UNDO) {
wm->op_undo_depth--;
}
}
WM_operator_free(handler->op);
}
CTX_wm_area_set(C, NULL);
wm_event_free_handler(&handler->head);
action = WM_HANDLER_BREAK;
break;
}
}
return action;
}
static int wm_handler_fileselect_call(bContext *C,
ListBase *handlers,
wmEventHandler_Op *handler,
const wmEvent *event)
{
int action = WM_HANDLER_CONTINUE;
if (event->type != EVT_FILESELECT) {
return action;
}
if (handler->op != (wmOperator *)event->customdata) {
return action;
}
return wm_handler_fileselect_do(C, handlers, handler, event->val);
}
static int wm_action_not_handled(int action)
{
return action == WM_HANDLER_CONTINUE || action == (WM_HANDLER_BREAK | WM_HANDLER_MODAL);
}
#define PRINT \
if (do_debug_handler) \
printf
static int wm_handlers_do_keymap_with_keymap_handler(
/* From 'wm_handlers_do_intern'. */
bContext *C,
wmEvent *event,
ListBase *handlers,
wmEventHandler_Keymap *handler,
/* Additional. */
wmKeyMap *keymap,
const bool do_debug_handler)
{
int action = WM_HANDLER_CONTINUE;
if (keymap == NULL) {
/* Only callback is allowed to have NULL keymaps. */
BLI_assert(handler->dynamic.keymap_fn);
}
else {
PRINT("%s: checking '%s' ...", __func__, keymap->idname);
if (WM_keymap_poll(C, keymap)) {
PRINT("pass\n");
LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
if (wm_eventmatch(event, kmi)) {
struct wmEventHandler_KeymapPost keymap_post = handler->post;
PRINT("%s: item matched '%s'\n", __func__, kmi->idname);
action |= wm_handler_operator_call(
C, handlers, &handler->head, event, kmi->ptr, kmi->idname);
if (action & WM_HANDLER_BREAK) {
/* Not always_pass here, it denotes removed handler_base. */
CLOG_INFO(WM_LOG_HANDLERS, 2, "handled! '%s'", kmi->idname);
if (keymap_post.post_fn != NULL) {
keymap_post.post_fn(keymap, kmi, keymap_post.user_data);
}
break;
}
if (action & WM_HANDLER_HANDLED) {
CLOG_INFO(WM_LOG_HANDLERS, 2, "handled - and pass on! '%s'", kmi->idname);
}
else {
CLOG_INFO(WM_LOG_HANDLERS, 2, "un-handled '%s'", kmi->idname);
}
}
}
}
else {
PRINT("fail\n");
}
}
return action;
}
static int wm_handlers_do_keymap_with_gizmo_handler(
/* From 'wm_handlers_do_intern' */
bContext *C,
wmEvent *event,
ListBase *handlers,
wmEventHandler_Gizmo *handler,
/* Additional. */
wmGizmoGroup *gzgroup,
wmKeyMap *keymap,
const bool do_debug_handler,
bool *r_keymap_poll)
{
int action = WM_HANDLER_CONTINUE;
bool keymap_poll = false;
PRINT("%s: checking '%s' ...", __func__, keymap->idname);
if (WM_keymap_poll(C, keymap)) {
keymap_poll = true;
PRINT("pass\n");
LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
if (wm_eventmatch(event, kmi)) {
PRINT("%s: item matched '%s'\n", __func__, kmi->idname);
CTX_wm_gizmo_group_set(C, gzgroup);
/* handler->op is called later, we want keymap op to be triggered here. */
action |= wm_handler_operator_call(
C, handlers, &handler->head, event, kmi->ptr, kmi->idname);
CTX_wm_gizmo_group_set(C, NULL);
if (action & WM_HANDLER_BREAK) {
if (G.debug & (G_DEBUG_EVENTS | G_DEBUG_HANDLERS)) {
printf("%s: handled - and pass on! '%s'\n", __func__, kmi->idname);
}
break;
}
if (action & WM_HANDLER_HANDLED) {
if (G.debug & (G_DEBUG_EVENTS | G_DEBUG_HANDLERS)) {
printf("%s: handled - and pass on! '%s'\n", __func__, kmi->idname);
}
}
else {
PRINT("%s: un-handled '%s'\n", __func__, kmi->idname);
}
}
}
}
else {
PRINT("fail\n");
}
if (r_keymap_poll) {
*r_keymap_poll = keymap_poll;
}
return action;
}
static int wm_handlers_do_gizmo_handler(bContext *C,
wmWindowManager *wm,
wmEventHandler_Gizmo *handler,
wmEvent *event,
ListBase *handlers,
const bool do_debug_handler)
{
int action = WM_HANDLER_CONTINUE;
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
wmGizmoMap *gzmap = handler->gizmo_map;
BLI_assert(gzmap != NULL);
wmGizmo *gz = wm_gizmomap_highlight_get(gzmap);
/* Needed so UI blocks over gizmos don't let events fall through to the gizmos,
* noticeable for the node editor - where dragging on a node should move it, see: T73212.
* note we still allow for starting the gizmo drag outside, then travel 'inside' the node. */
if (region->type->clip_gizmo_events_by_ui) {
if (UI_region_block_find_mouse_over(region, &event->x, true)) {
if (gz != NULL && event->type != EVT_GIZMO_UPDATE) {
WM_tooltip_clear(C, CTX_wm_window(C));
wm_gizmomap_highlight_set(gzmap, C, NULL, 0);
}
return action;
}
}
if (region->gizmo_map != handler->gizmo_map) {
WM_gizmomap_tag_refresh(handler->gizmo_map);
}
wm_gizmomap_handler_context_gizmo(C, handler);
wm_region_mouse_co(C, event);
/* Drag events use the previous click location to highlight the gizmos,
* Get the highlight again in case the user dragged off the gizmo. */
const bool is_event_drag = ISTWEAK(event->type) || (event->val == KM_CLICK_DRAG);
const bool is_event_modifier = ISKEYMODIFIER(event->type);
bool handle_highlight = false;
bool handle_keymap = false;
/* Handle gizmo highlighting. */
if (!wm_gizmomap_modal_get(gzmap) &&
((event->type == MOUSEMOVE) || is_event_modifier || is_event_drag)) {
handle_highlight = true;
if (is_event_modifier || is_event_drag) {
handle_keymap = true;
}
}
else {
handle_keymap = true;
}
if (handle_highlight) {
struct {
wmGizmo *gz;
int part;
} prev = {
.gz = gz,
.part = gz ? gz->highlight_part : 0,
};
int part = -1;
gz = wm_gizmomap_highlight_find(gzmap, C, event, &part);
/* If no gizmos are/were active, don't clear tool-tips. */
if (gz || prev.gz) {
if ((prev.gz != gz) || (prev.part != part)) {
WM_tooltip_clear(C, CTX_wm_window(C));
}
}
if (wm_gizmomap_highlight_set(gzmap, C, gz, part)) {
if (gz != NULL) {
if ((U.flag & USER_TOOLTIPS) && (gz->flag & WM_GIZMO_NO_TOOLTIP) == 0) {
WM_tooltip_timer_init(C, CTX_wm_window(C), area, region, WM_gizmomap_tooltip_init);
}
}
}
}
/* Don't use from now on. */
bool is_event_handle_all = gz && (gz->flag & WM_GIZMO_EVENT_HANDLE_ALL);
if (handle_keymap) {
/* Handle highlight gizmo. */
if ((gz != NULL) && (gz->flag & WM_GIZMO_HIDDEN_KEYMAP) == 0) {
bool keymap_poll = false;
wmGizmoGroup *gzgroup = gz->parent_gzgroup;
wmKeyMap *keymap = WM_keymap_active(wm, gz->keymap ? gz->keymap : gzgroup->type->keymap);
action |= wm_handlers_do_keymap_with_gizmo_handler(
C, event, handlers, handler, gzgroup, keymap, do_debug_handler, &keymap_poll);
#ifdef USE_GIZMO_MOUSE_PRIORITY_HACK
if (((action & WM_HANDLER_BREAK) == 0) && !is_event_handle_all && keymap_poll) {
if ((event->val == KM_PRESS) && ELEM(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE)) {
wmEvent event_test_click = *event;
event_test_click.val = KM_CLICK;
wmEvent event_test_click_drag = *event;
event_test_click_drag.val = KM_CLICK_DRAG;
wmEvent event_test_tweak = *event;
event_test_tweak.type = EVT_TWEAK_L + (event->type - LEFTMOUSE);
event_test_tweak.val = KM_ANY;
LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
if ((kmi->flag & KMI_INACTIVE) == 0) {
if (wm_eventmatch(&event_test_click, kmi) ||
wm_eventmatch(&event_test_click_drag, kmi) ||
wm_eventmatch(&event_test_tweak, kmi)) {
wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
if (WM_operator_poll_context(C, ot, WM_OP_INVOKE_DEFAULT)) {
is_event_handle_all = true;
break;
}
}
}
}
}
}
#endif /* USE_GIZMO_MOUSE_PRIORITY_HACK */
}
/* Don't use from now on. */
gz = NULL;
/* Fallback to selected gizmo (when un-handled). */
if ((action & WM_HANDLER_BREAK) == 0) {
if (WM_gizmomap_is_any_selected(gzmap)) {
const ListBase *groups = WM_gizmomap_group_list(gzmap);
LISTBASE_FOREACH (wmGizmoGroup *, gzgroup, groups) {
if (wm_gizmogroup_is_any_selected(gzgroup)) {
wmKeyMap *keymap = WM_keymap_active(wm, gzgroup->type->keymap);
action |= wm_handlers_do_keymap_with_gizmo_handler(
C, event, handlers, handler, gzgroup, keymap, do_debug_handler, NULL);
if (action & WM_HANDLER_BREAK) {
break;
}
}
}
}
}
}
if (is_event_handle_all) {
if (action == WM_HANDLER_CONTINUE) {
action |= WM_HANDLER_BREAK | WM_HANDLER_MODAL;
}
}
/* restore the area */
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
return action;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Handle Single Event (All Handler Types)
* \{ */
static int wm_handlers_do_intern(bContext *C, wmEvent *event, ListBase *handlers)
{
const bool do_debug_handler =
(G.debug & G_DEBUG_HANDLERS) &&
/* Comment this out to flood the console! (if you really want to test). */
!ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE);
wmWindowManager *wm = CTX_wm_manager(C);
int action = WM_HANDLER_CONTINUE;
int always_pass;
if (handlers == NULL) {
return action;
}
/* Modal handlers can get removed in this loop, we keep the loop this way.
*
* Note: check 'handlers->first' because in rare cases the handlers can be cleared
* by the event that's called, for eg:
*
* Calling a python script which changes the area.type, see T32232. */
for (wmEventHandler *handler_base = handlers->first, *handler_base_next;
handler_base && handlers->first;
handler_base = handler_base_next) {
handler_base_next = handler_base->next;
/* During this loop, UI handlers for nested menus can tag multiple handlers free. */
if (handler_base->flag & WM_HANDLER_DO_FREE) {
/* Pass. */
}
else if (handler_base->poll == NULL || handler_base->poll(CTX_wm_region(C), event)) {
/* In advance to avoid access to freed event on window close. */
always_pass = wm_event_always_pass(event);
/* Modal+blocking handler_base. */
if (handler_base->flag & WM_HANDLER_BLOCKING) {
action |= WM_HANDLER_BREAK;
}
/* Handle all types here. */
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
wmKeyMap *keymap = WM_event_get_keymap_from_handler(wm, handler);
action |= wm_handlers_do_keymap_with_keymap_handler(
C, event, handlers, handler, keymap, do_debug_handler);
/* Clear the tool-tip whenever a key binding is handled, without this tool-tips
* are kept when a modal operators starts (annoying but otherwise harmless). */
if (action & WM_HANDLER_BREAK) {
/* Window may be gone after file read. */
if (CTX_wm_window(C) != NULL) {
WM_tooltip_clear(C, CTX_wm_window(C));
}
}
}
else if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
BLI_assert(handler->handle_fn != NULL);
if (!wm->is_interface_locked) {
action |= wm_handler_ui_call(C, handler, event, always_pass);
}
}
else if (handler_base->type == WM_HANDLER_TYPE_DROPBOX) {
wmEventHandler_Dropbox *handler = (wmEventHandler_Dropbox *)handler_base;
if (!wm->is_interface_locked && event->type == EVT_DROP) {
LISTBASE_FOREACH (wmDropBox *, drop, handler->dropboxes) {
/* Other drop custom types allowed. */
if (event->custom == EVT_DATA_DRAGDROP) {
ListBase *lb = (ListBase *)event->customdata;
LISTBASE_FOREACH (wmDrag *, drag, lb) {
const char *tooltip = NULL;
if (drop->poll(C, drag, event, &tooltip)) {
/* Optionally copy drag information to operator properties. Don't call it if the
* operator fails anyway, it might do more than just set properties (e.g.
* typically import an asset). */
if (drop->copy && WM_operator_poll_context(C, drop->ot, drop->opcontext)) {
drop->copy(drag, drop);
}
/* Pass single matched wmDrag onto the operator. */
BLI_remlink(lb, drag);
ListBase single_lb = {drag, drag};
event->customdata = &single_lb;
int op_retval = wm_operator_call_internal(
C, drop->ot, drop->ptr, NULL, drop->opcontext, false, event);
OPERATOR_RETVAL_CHECK(op_retval);
if ((op_retval & OPERATOR_CANCELLED) && drop->cancel) {
drop->cancel(CTX_data_main(C), drag, drop);
}
action |= WM_HANDLER_BREAK;
/* Free the drags. */
WM_drag_free_list(lb);
WM_drag_free_list(&single_lb);
event->customdata = NULL;
event->custom = 0;
/* XXX fileread case. */
if (CTX_wm_window(C) == NULL) {
return action;
}
/* Escape from drag loop, got freed. */
break;
}
}
}
}
}
}
else if (handler_base->type == WM_HANDLER_TYPE_GIZMO) {
wmEventHandler_Gizmo *handler = (wmEventHandler_Gizmo *)handler_base;
action |= wm_handlers_do_gizmo_handler(C, wm, handler, event, handlers, do_debug_handler);
}
else if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
if (handler->is_fileselect) {
if (!wm->is_interface_locked) {
/* Screen context changes here. */
action |= wm_handler_fileselect_call(C, handlers, handler, event);
}
}
else {
action |= wm_handler_operator_call(C, handlers, handler_base, event, NULL, NULL);
}
}
else {
/* Unreachable (handle all types above). */
BLI_assert(0);
}
if (action & WM_HANDLER_BREAK) {
if (always_pass) {
action &= ~WM_HANDLER_BREAK;
}
else {
break;
}
}
}
/* XXX fileread case, if the wm is freed then the handler's
* will have been too so the code below need not run. */
if (CTX_wm_window(C) == NULL) {
return action;
}
/* XXX code this for all modal ops, and ensure free only happens here. */
/* Modal UI handler can be tagged to be freed. */
if (BLI_findindex(handlers, handler_base) !=
-1) { /* Could be freed already by regular modal ops. */
if (handler_base->flag & WM_HANDLER_DO_FREE) {
BLI_remlink(handlers, handler_base);
wm_event_free_handler(handler_base);
}
}
}
if (action == (WM_HANDLER_BREAK | WM_HANDLER_MODAL)) {
wm_cursor_arrow_move(CTX_wm_window(C), event);
}
return action;
}
#undef PRINT
/* This calls handlers twice - to solve (double-)click events. */
static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers)
{
int action = wm_handlers_do_intern(C, event, handlers);
/* Fileread case. */
if (CTX_wm_window(C) == NULL) {
return action;
}
if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
/* Test for CLICK_DRAG events. */
if (wm_action_not_handled(action)) {
wmWindow *win = CTX_wm_window(C);
if (win->event_queue_check_drag) {
if (WM_event_drag_test(event, &event->prevclickx)) {
int x = event->x;
int y = event->y;
short val = event->val;
short type = event->type;
event->x = event->prevclickx;
event->y = event->prevclicky;
event->val = KM_CLICK_DRAG;
event->type = event->prevtype;
CLOG_INFO(WM_LOG_HANDLERS, 1, "handling PRESS_DRAG");
action |= wm_handlers_do_intern(C, event, handlers);
event->val = val;
event->type = type;
event->x = x;
event->y = y;
win->event_queue_check_click = false;
if (!wm_action_not_handled(action)) {
/* Only disable when handled as other handlers may use this drag event. */
win->event_queue_check_drag = false;
}
}
}
}
else {
wmWindow *win = CTX_wm_window(C);
if (win) {
win->event_queue_check_drag = false;
}
}
}
else if (ISMOUSE_BUTTON(event->type) || ISKEYBOARD(event->type)) {
/* All events that don't set wmEvent.prevtype must be ignored. */
/* Test for CLICK events. */
if (wm_action_not_handled(action)) {
wmWindow *win = CTX_wm_window(C);
/* eventstate stores if previous event was a KM_PRESS, in case that
* wasn't handled, the KM_RELEASE will become a KM_CLICK */
if (win != NULL) {
if (event->val == KM_PRESS) {
if (event->prevval != KM_PRESS) {
win->event_queue_check_click = true;
win->event_queue_check_drag = true;
}
}
else if (event->val == KM_RELEASE) {
win->event_queue_check_drag = false;
}
}
if (win && event->prevtype == event->type) {
if (event->val == KM_RELEASE) {
if (event->prevval == KM_PRESS) {
if (win->event_queue_check_click == true) {
if (WM_event_drag_test(event, &event->prevclickx)) {
win->event_queue_check_click = false;
win->event_queue_check_drag = false;
}
else {
/* Position is where the actual click happens, for more
* accurate selecting in case the mouse drifts a little. */
int x = event->x;
int y = event->y;
event->x = event->prevclickx;
event->y = event->prevclicky;
event->val = KM_CLICK;
CLOG_INFO(WM_LOG_HANDLERS, 1, "handling CLICK");
action |= wm_handlers_do_intern(C, event, handlers);
event->val = KM_RELEASE;
event->x = x;
event->y = y;
}
}
}
}
else if (event->val == KM_DBL_CLICK) {
/* The underlying event is a press, so try and handle this. */
event->val = KM_PRESS;
action |= wm_handlers_do_intern(C, event, handlers);
/* revert value if not handled */
if (wm_action_not_handled(action)) {
event->val = KM_DBL_CLICK;
}
}
}
}
else {
wmWindow *win = CTX_wm_window(C);
if (win) {
win->event_queue_check_click = false;
win->event_queue_check_drag = false;
}
}
}
else if (ISMOUSE_WHEEL(event->type) || ISMOUSE_GESTURE(event->type)) {
/* Modifiers which can trigger click event's,
* however we don't want this if the mouse wheel has been used, see T74607. */
if (wm_action_not_handled(action)) {
/* pass */
}
else {
wmWindow *win = CTX_wm_window(C);
if (win) {
if (ISKEYMODIFIER(event->prevtype)) {
win->event_queue_check_click = false;
}
}
}
}
return action;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Event Queue Utilities
*
* Utilities used by #wm_event_do_handlers.
* \{ */
static bool wm_event_inside_rect(const wmEvent *event, const rcti *rect)
{
if (wm_event_always_pass(event)) {
return true;
}
if (BLI_rcti_isect_pt_v(rect, &event->x)) {
return true;
}
return false;
}
static bool wm_event_inside_region(const wmEvent *event, const ARegion *region)
{
if (wm_event_always_pass(event)) {
return true;
}
return ED_region_contains_xy(region, &event->x);
}
static ScrArea *area_event_inside(bContext *C, const int xy[2])
{
wmWindow *win = CTX_wm_window(C);
bScreen *screen = CTX_wm_screen(C);
if (screen) {
ED_screen_areas_iter (win, screen, area) {
if (BLI_rcti_isect_pt_v(&area->totrct, xy)) {
return area;
}
}
}
return NULL;
}
static ARegion *region_event_inside(bContext *C, const int xy[2])
{
bScreen *screen = CTX_wm_screen(C);
ScrArea *area = CTX_wm_area(C);
if (screen && area) {
LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
if (BLI_rcti_isect_pt_v(®ion->winrct, xy)) {
return region;
}
}
}
return NULL;
}
static void wm_paintcursor_tag(bContext *C, wmPaintCursor *pc, ARegion *region)
{
if (region) {
for (; pc; pc = pc->next) {
if (pc->poll == NULL || pc->poll(C)) {
wmWindow *win = CTX_wm_window(C);
WM_paint_cursor_tag_redraw(win, region);
}
}
}
}
/* Called on mousemove, check updates for paintcursors. */
/* Context was set on active area and region. */
static void wm_paintcursor_test(bContext *C, const wmEvent *event)
{
wmWindowManager *wm = CTX_wm_manager(C);
if (wm->paintcursors.first) {
ARegion *region = CTX_wm_region(C);
if (region) {
wm_paintcursor_tag(C, wm->paintcursors.first, region);
}
/* If previous position was not in current region, we have to set a temp new context. */
if (region == NULL || !BLI_rcti_isect_pt_v(®ion->winrct, &event->prevx)) {
ScrArea *area = CTX_wm_area(C);
CTX_wm_area_set(C, area_event_inside(C, &event->prevx));
CTX_wm_region_set(C, region_event_inside(C, &event->prevx));
wm_paintcursor_tag(C, wm->paintcursors.first, CTX_wm_region(C));
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
}
}
}
static void wm_event_drag_and_drop_test(wmWindowManager *wm, wmWindow *win, wmEvent *event)
{
bScreen *screen = WM_window_get_active_screen(win);
if (BLI_listbase_is_empty(&wm->drags)) {
return;
}
if (event->type == MOUSEMOVE || ISKEYMODIFIER(event->type)) {
screen->do_draw_drag = true;
}
else if (event->type == EVT_ESCKEY) {
WM_drag_free_list(&wm->drags);
screen->do_draw_drag = true;
}
else if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
event->type = EVT_DROP;
/* Vreate customdata, first free existing. */
if (event->customdata) {
if (event->customdatafree) {
MEM_freeN(event->customdata);
}
}
event->custom = EVT_DATA_DRAGDROP;
event->customdata = &wm->drags;
event->customdatafree = 1;
/* Vlear drop icon. */
screen->do_draw_drag = true;
/* restore cursor (disabled, see wm_dragdrop.c) */
// WM_cursor_modal_restore(win);
}
}
/* Filter out all events of the pie that spawned the last pie unless it's a release event. */
static bool wm_event_pie_filter(wmWindow *win, const wmEvent *event)
{
if (win->pie_event_type_lock && win->pie_event_type_lock == event->type) {
if (event->val == KM_RELEASE) {
win->pie_event_type_lock = EVENT_NONE;
return false;
}
return true;
}
return false;
}
/**
* Account for the special case when events are being handled and a file is loaded.
* In this case event handling exits early, however when "Load UI" is disabled
* the even will still be in #wmWindow.event_queue.
*
* Without this it's possible to continuously handle the same event, see: T76484.
*/
static void wm_event_free_and_remove_from_queue_if_valid(wmEvent *event)
{
LISTBASE_FOREACH (wmWindowManager *, wm, &G_MAIN->wm) {
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
if (BLI_remlink_safe(&win->event_queue, event)) {
wm_event_free(event);
return;
}
}
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Main Event Queue (Every Window)
*
* Handle events for all windows, run from the #WM_main event loop.
* \{ */
/* Called in main loop. */
/* Goes over entire hierarchy: events -> window -> screen -> area -> region. */
void wm_event_do_handlers(bContext *C)
{
wmWindowManager *wm = CTX_wm_manager(C);
BLI_assert(ED_undo_is_state_valid(C));
/* Update key configuration before handling events. */
WM_keyconfig_update(wm);
WM_gizmoconfig_update(CTX_data_main(C));
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
bScreen *screen = WM_window_get_active_screen(win);
/* Some safety checks - these should always be set! */
BLI_assert(WM_window_get_active_scene(win));
BLI_assert(WM_window_get_active_screen(win));
BLI_assert(WM_window_get_active_workspace(win));
if (screen == NULL) {
wm_event_free_all(win);
}
else {
Scene *scene = WM_window_get_active_scene(win);
ViewLayer *view_layer = WM_window_get_active_view_layer(win);
Depsgraph *depsgraph = BKE_scene_get_depsgraph(scene, view_layer);
Scene *scene_eval = (depsgraph != NULL) ? DEG_get_evaluated_scene(depsgraph) : NULL;
if (scene_eval != NULL) {
const int is_playing_sound = BKE_sound_scene_playing(scene_eval);
if (scene_eval->id.recalc & ID_RECALC_AUDIO_SEEK) {
/* Ignore seek here, the audio will be updated to the scene frame after jump during next
* dependency graph update. */
}
else if (is_playing_sound != -1) {
bool is_playing_screen;
is_playing_screen = (ED_screen_animation_playing(wm) != NULL);
if (((is_playing_sound == 1) && (is_playing_screen == 0)) ||
((is_playing_sound == 0) && (is_playing_screen == 1))) {
wmWindow *win_ctx = CTX_wm_window(C);
bScreen *screen_stx = CTX_wm_screen(C);
Scene *scene_ctx = CTX_data_scene(C);
CTX_wm_window_set(C, win);
CTX_wm_screen_set(C, screen);
CTX_data_scene_set(C, scene);
ED_screen_animation_play(C, -1, 1);
CTX_data_scene_set(C, scene_ctx);
CTX_wm_screen_set(C, screen_stx);
CTX_wm_window_set(C, win_ctx);
}
if (is_playing_sound == 0) {
const double time = BKE_sound_sync_scene(scene_eval);
if (isfinite(time)) {
int ncfra = round(time * FPS);
if (ncfra != scene->r.cfra) {
scene->r.cfra = ncfra;
ED_update_for_newframe(CTX_data_main(C), depsgraph);
WM_event_add_notifier(C, NC_WINDOW, NULL);
}
}
}
}
}
}
wmEvent *event;
while ((event = win->event_queue.first)) {
int action = WM_HANDLER_CONTINUE;
/* Active screen might change during handlers, update pointer. */
screen = WM_window_get_active_screen(win);
if (G.debug & (G_DEBUG_HANDLERS | G_DEBUG_EVENTS) &&
!ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
printf("\n%s: Handling event\n", __func__);
WM_event_print(event);
}
/* Take care of pie event filter. */
if (wm_event_pie_filter(win, event)) {
if (!ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
CLOG_INFO(WM_LOG_HANDLERS, 1, "event filtered due to pie button pressed");
}
BLI_remlink(&win->event_queue, event);
wm_event_free(event);
continue;
}
CTX_wm_window_set(C, win);
/* Clear tool-tip on mouse move. */
if (screen->tool_tip && screen->tool_tip->exit_on_event) {
if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
if (len_manhattan_v2v2_int(screen->tool_tip->event_xy, &event->x) > U.move_threshold) {
WM_tooltip_clear(C, win);
}
}
}
/* We let modal handlers get active area/region, also wm_paintcursor_test needs it. */
CTX_wm_area_set(C, area_event_inside(C, &event->x));
CTX_wm_region_set(C, region_event_inside(C, &event->x));
/* MVC demands to not draw in event handlers...
* but we need to leave it for ogl selecting etc. */
wm_window_make_drawable(wm, win);
wm_region_mouse_co(C, event);
/* First we do priority handlers, modal + some limited keymaps. */
action |= wm_handlers_do(C, event, &win->modalhandlers);
/* Fileread case. */
if (CTX_wm_window(C) == NULL) {
wm_event_free_and_remove_from_queue_if_valid(event);
return;
}
/* Check for a tooltip. */
if (screen == WM_window_get_active_screen(win)) {
if (screen->tool_tip && screen->tool_tip->timer) {
if ((event->type == TIMER) && (event->customdata == screen->tool_tip->timer)) {
WM_tooltip_init(C, win);
}
}
}
/* Check dragging, creates new event or frees, adds draw tag. */
wm_event_drag_and_drop_test(wm, win, event);
/* Builtin tweak, if action is break it removes tweak. */
wm_tweakevent_test(C, event, action);
if ((action & WM_HANDLER_BREAK) == 0) {
/* Note: setting subwin active should be done here, after modal handlers have been done */
if (event->type == MOUSEMOVE) {
/* State variables in screen, cursors.
* Also used in wm_draw.c, fails for modal handlers though. */
ED_screen_set_active_region(C, win, &event->x);
/* For regions having custom cursors. */
wm_paintcursor_test(C, event);
}
#ifdef WITH_INPUT_NDOF
else if (event->type == NDOF_MOTION) {
win->addmousemove = true;
}
#endif
ED_screen_areas_iter (win, screen, area) {
/* After restoring a screen from SCREENMAXIMIZED we have to wait
* with the screen handling till the region coordinates are updated. */
if (screen->skip_handling == true) {
/* Restore for the next iteration of wm_event_do_handlers. */
screen->skip_handling = false;
break;
}
/* Update azones if needed - done here because it needs to be independent from redraws.
*/
if (area->flag & AREA_FLAG_ACTIONZONES_UPDATE) {
ED_area_azones_update(area, &event->x);
}
if (wm_event_inside_rect(event, &area->totrct)) {
CTX_wm_area_set(C, area);
if ((action & WM_HANDLER_BREAK) == 0) {
LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
if (wm_event_inside_region(event, region)) {
CTX_wm_region_set(C, region);
/* Call even on non mouse events, since the */
wm_region_mouse_co(C, event);
if (!BLI_listbase_is_empty(&wm->drags)) {
/* Does polls for drop regions and checks #uiButs. */
/* Need to be here to make sure region context is true. */
if (ELEM(event->type, MOUSEMOVE, EVT_DROP) || ISKEYMODIFIER(event->type)) {
wm_drags_check_ops(C, event);
}
}
action |= wm_handlers_do(C, event, ®ion->handlers);
/* Fileread case (python), T29489. */
if (CTX_wm_window(C) == NULL) {
wm_event_free_and_remove_from_queue_if_valid(event);
return;
}
if (action & WM_HANDLER_BREAK) {
break;
}
}
}
}
CTX_wm_region_set(C, NULL);
if ((action & WM_HANDLER_BREAK) == 0) {
wm_region_mouse_co(C, event); /* Only invalidates event->mval in this case. */
action |= wm_handlers_do(C, event, &area->handlers);
}
CTX_wm_area_set(C, NULL);
/* NOTE: do not escape on WM_HANDLER_BREAK,
* mousemove needs handled for previous area. */
}
}
if ((action & WM_HANDLER_BREAK) == 0) {
/* Also some non-modal handlers need active area/region. */
CTX_wm_area_set(C, area_event_inside(C, &event->x));
CTX_wm_region_set(C, region_event_inside(C, &event->x));
wm_region_mouse_co(C, event);
action |= wm_handlers_do(C, event, &win->handlers);
/* Fileread case. */
if (CTX_wm_window(C) == NULL) {
wm_event_free_and_remove_from_queue_if_valid(event);
return;
}
}
}
/* If press was handled, we don't want to do click. This way
* press in tool keymap can override click in editor keymap.*/
if (ISMOUSE_BUTTON(event->type) && event->val == KM_PRESS &&
!wm_action_not_handled(action)) {
win->event_queue_check_click = false;
}
/* Update previous mouse position for following events to use. */
win->eventstate->prevx = event->x;
win->eventstate->prevy = event->y;
/* Unlink and free here, blender-quit then frees all. */
BLI_remlink(&win->event_queue, event);
wm_event_free(event);
}
/* Only add mouse-move when the event queue was read entirely. */
if (win->addmousemove && win->eventstate) {
wmEvent tevent = *(win->eventstate);
// printf("adding MOUSEMOVE %d %d\n", tevent.x, tevent.y);
tevent.type = MOUSEMOVE;
tevent.prevx = tevent.x;
tevent.prevy = tevent.y;
tevent.is_repeat = false;
wm_event_add(win, &tevent);
win->addmousemove = 0;
}
CTX_wm_window_set(C, NULL);
}
/* Update key configuration after handling events. */
WM_keyconfig_update(wm);
WM_gizmoconfig_update(CTX_data_main(C));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name File Selector Handling
* \{ */
void WM_event_fileselect_event(wmWindowManager *wm, void *ophandle, int eventval)
{
/* Add to all windows! */
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
wmEvent event = *win->eventstate;
event.type = EVT_FILESELECT;
event.val = eventval;
event.customdata = ophandle; /* Only as void pointer type check. */
wm_event_add(win, &event);
}
}
/* Operator is supposed to have a filled "path" property. */
/* Optional property: filetype (XXX enum?) */
/**
* The idea here is to keep a handler alive on window queue, owning the operator.
* The file window can send event to make it execute, thus ensuring
* executing happens outside of lower level queues, with UI refreshed.
* Should also allow multiwin solutions.
*/
void WM_event_add_fileselect(bContext *C, wmOperator *op)
{
wmWindowManager *wm = CTX_wm_manager(C);
wmWindow *win = CTX_wm_window(C);
const bool is_temp_screen = WM_window_is_temp_screen(win);
/* Close any popups, like when opening a file browser from the splash. */
UI_popup_handlers_remove_all(C, &win->modalhandlers);
if (!is_temp_screen) {
/* Only allow 1 file selector open per window. */
LISTBASE_FOREACH_MUTABLE (wmEventHandler *, handler_base, &win->modalhandlers) {
if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
if (handler->is_fileselect == false) {
continue;
}
ScrArea *file_area = ED_fileselect_handler_area_find(win, handler->op);
if (file_area) {
CTX_wm_area_set(C, file_area);
wm_handler_fileselect_do(C, &win->modalhandlers, handler, EVT_FILESELECT_CANCEL);
}
/* If not found we stop the handler without changing the screen. */
else {
wm_handler_fileselect_do(
C, &win->modalhandlers, handler, EVT_FILESELECT_EXTERNAL_CANCEL);
}
}
}
}
wmEventHandler_Op *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_OP;
handler->is_fileselect = true;
handler->op = op;
handler->context.win = CTX_wm_window(C);
handler->context.area = CTX_wm_area(C);
handler->context.region = CTX_wm_region(C);
BLI_addhead(&win->modalhandlers, handler);
/* Check props once before invoking if check is available
* ensures initial properties are valid. */
if (op->type->check) {
op->type->check(C, op); /* Ignore return value. */
}
WM_event_fileselect_event(wm, op, EVT_FILESELECT_FULL_OPEN);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Modal Operator Handling
* \{ */
#if 0
/* lets not expose struct outside wm? */
static void WM_event_set_handler_flag(wmEventHandler *handler, int flag)
{
handler->flag = flag;
}
#endif
wmEventHandler_Op *WM_event_add_modal_handler(bContext *C, wmOperator *op)
{
wmEventHandler_Op *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_OP;
wmWindow *win = CTX_wm_window(C);
/* Operator was part of macro. */
if (op->opm) {
/* Give the mother macro to the handler. */
handler->op = op->opm;
/* Mother macro opm becomes the macro element. */
handler->op->opm = op;
}
else {
handler->op = op;
}
handler->context.area = CTX_wm_area(C); /* Means frozen screen context for modal handlers! */
handler->context.region = CTX_wm_region(C);
handler->context.region_type = handler->context.region ? handler->context.region->regiontype :
-1;
BLI_addhead(&win->modalhandlers, handler);
if (op->type->modalkeymap) {
WM_window_status_area_tag_redraw(win);
}
return handler;
}
/**
* Modal handlers store a pointer to an area which might be freed while the handler runs.
* Use this function to NULL all handler pointers to \a old_area.
*/
void WM_event_modal_handler_area_replace(wmWindow *win, const ScrArea *old_area, ScrArea *new_area)
{
LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
/* Fileselect handler is quite special...
* it needs to keep old area stored in handler, so don't change it. */
if ((handler->context.area == old_area) && (handler->is_fileselect == false)) {
handler->context.area = new_area;
}
}
}
}
/**
* Modal handlers store a pointer to a region which might be freed while the handler runs.
* Use this function to NULL all handler pointers to \a old_region.
*/
void WM_event_modal_handler_region_replace(wmWindow *win,
const ARegion *old_region,
ARegion *new_region)
{
LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
/* Fileselect handler is quite special...
* it needs to keep old region stored in handler, so don't change it. */
if ((handler->context.region == old_region) && (handler->is_fileselect == false)) {
handler->context.region = new_region;
handler->context.region_type = new_region ? new_region->regiontype : RGN_TYPE_WINDOW;
}
}
}
}
wmEventHandler_Keymap *WM_event_add_keymap_handler(ListBase *handlers, wmKeyMap *keymap)
{
if (!keymap) {
CLOG_WARN(WM_LOG_HANDLERS, "called with NULL keymap");
return NULL;
}
/* Only allow same keymap once. */
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
if (handler->keymap == keymap) {
return handler;
}
}
}
wmEventHandler_Keymap *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_KEYMAP;
BLI_addtail(handlers, handler);
handler->keymap = keymap;
return handler;
}
/**
* Implements fallback tool when enabled by:
* #SCE_WORKSPACE_TOOL_FALLBACK, #WM_GIZMOGROUPTYPE_TOOL_FALLBACK_KEYMAP.
*
* This runs before #WM_event_get_keymap_from_toolsystem,
* allowing both the fallback-tool and active-tool to be activated
* providing the key-map is configured so the keys don't conflict.
* For example one mouse button can run the active-tool, another button for the fallback-tool.
* See T72567.
*
* Follow #wmEventHandler_KeymapDynamicFn signature.
*/
wmKeyMap *WM_event_get_keymap_from_toolsystem_fallback(wmWindowManager *wm,
wmEventHandler_Keymap *handler)
{
ScrArea *area = handler->dynamic.user_data;
handler->keymap_tool = NULL;
bToolRef_Runtime *tref_rt = area->runtime.tool ? area->runtime.tool->runtime : NULL;
if (tref_rt && tref_rt->keymap_fallback[0]) {
const char *keymap_id = NULL;
/* Support for the gizmo owning the tool keymap. */
if (tref_rt->gizmo_group[0] != '\0' && tref_rt->keymap_fallback[0] != '\n') {
wmGizmoMap *gzmap = NULL;
wmGizmoGroup *gzgroup = NULL;
LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
if (region->gizmo_map != NULL) {
gzmap = region->gizmo_map;
gzgroup = WM_gizmomap_group_find(gzmap, tref_rt->gizmo_group);
if (gzgroup != NULL) {
break;
}
}
}
if (gzgroup != NULL) {
if (gzgroup->type->flag & WM_GIZMOGROUPTYPE_TOOL_FALLBACK_KEYMAP) {
/* If all are hidden, don't override. */
if (gzgroup->use_fallback_keymap) {
wmGizmo *highlight = wm_gizmomap_highlight_get(gzmap);
if (highlight == NULL) {
keymap_id = tref_rt->keymap_fallback;
}
}
}
}
}
if (keymap_id && keymap_id[0]) {
wmKeyMap *km = WM_keymap_list_find_spaceid_or_empty(
&wm->userconf->keymaps, keymap_id, area->spacetype, RGN_TYPE_WINDOW);
/* We shouldn't use keymaps from unrelated spaces. */
if (km != NULL) {
handler->keymap_tool = area->runtime.tool;
return km;
}
printf(
"Keymap: '%s' not found for tool '%s'\n", tref_rt->keymap, area->runtime.tool->idname);
}
}
return NULL;
}
wmKeyMap *WM_event_get_keymap_from_toolsystem(wmWindowManager *wm, wmEventHandler_Keymap *handler)
{
ScrArea *area = handler->dynamic.user_data;
handler->keymap_tool = NULL;
bToolRef_Runtime *tref_rt = area->runtime.tool ? area->runtime.tool->runtime : NULL;
if (tref_rt && tref_rt->keymap[0]) {
const char *keymap_id = tref_rt->keymap;
{
wmKeyMap *km = WM_keymap_list_find_spaceid_or_empty(
&wm->userconf->keymaps, keymap_id, area->spacetype, RGN_TYPE_WINDOW);
/* We shouldn't use keymaps from unrelated spaces. */
if (km != NULL) {
handler->keymap_tool = area->runtime.tool;
return km;
}
printf(
"Keymap: '%s' not found for tool '%s'\n", tref_rt->keymap, area->runtime.tool->idname);
}
}
return NULL;
}
struct wmEventHandler_Keymap *WM_event_add_keymap_handler_dynamic(
ListBase *handlers, wmEventHandler_KeymapDynamicFn *keymap_fn, void *user_data)
{
if (!keymap_fn) {
CLOG_WARN(WM_LOG_HANDLERS, "called with NULL keymap_fn");
return NULL;
}
/* Only allow same keymap once. */
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
if (handler->dynamic.keymap_fn == keymap_fn) {
/* Maximizing the view needs to update the area. */
handler->dynamic.user_data = user_data;
return handler;
}
}
}
wmEventHandler_Keymap *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_KEYMAP;
BLI_addtail(handlers, handler);
handler->dynamic.keymap_fn = keymap_fn;
handler->dynamic.user_data = user_data;
return handler;
}
/* Priorities not implemented yet, for time being just insert in begin of list. */
wmEventHandler_Keymap *WM_event_add_keymap_handler_priority(ListBase *handlers,
wmKeyMap *keymap,
int UNUSED(priority))
{
WM_event_remove_keymap_handler(handlers, keymap);
wmEventHandler_Keymap *handler = MEM_callocN(sizeof(*handler), "event keymap handler");
handler->head.type = WM_HANDLER_TYPE_KEYMAP;
BLI_addhead(handlers, handler);
handler->keymap = keymap;
return handler;
}
static bool event_or_prev_in_rect(const wmEvent *event, const rcti *rect)
{
if (BLI_rcti_isect_pt(rect, event->x, event->y)) {
return true;
}
if (event->type == MOUSEMOVE && BLI_rcti_isect_pt(rect, event->prevx, event->prevy)) {
return true;
}
return false;
}
static bool handler_region_v2d_mask_test(const ARegion *region, const wmEvent *event)
{
rcti rect = region->v2d.mask;
BLI_rcti_translate(&rect, region->winrct.xmin, region->winrct.ymin);
return event_or_prev_in_rect(event, &rect);
}
wmEventHandler_Keymap *WM_event_add_keymap_handler_poll(ListBase *handlers,
wmKeyMap *keymap,
EventHandlerPoll poll)
{
wmEventHandler_Keymap *handler = WM_event_add_keymap_handler(handlers, keymap);
if (handler == NULL) {
return NULL;
}
handler->head.poll = poll;
return handler;
}
wmEventHandler_Keymap *WM_event_add_keymap_handler_v2d_mask(ListBase *handlers, wmKeyMap *keymap)
{
return WM_event_add_keymap_handler_poll(handlers, keymap, handler_region_v2d_mask_test);
}
void WM_event_remove_keymap_handler(ListBase *handlers, wmKeyMap *keymap)
{
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
if (handler->keymap == keymap) {
BLI_remlink(handlers, handler);
wm_event_free_handler(&handler->head);
break;
}
}
}
}
void WM_event_set_keymap_handler_post_callback(wmEventHandler_Keymap *handler,
void(keymap_tag)(wmKeyMap *keymap,
wmKeyMapItem *kmi,
void *user_data),
void *user_data)
{
handler->post.post_fn = keymap_tag;
handler->post.user_data = user_data;
}
wmEventHandler_UI *WM_event_add_ui_handler(const bContext *C,
ListBase *handlers,
wmUIHandlerFunc handle_fn,
wmUIHandlerRemoveFunc remove_fn,
void *user_data,
const char flag)
{
wmEventHandler_UI *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_UI;
handler->handle_fn = handle_fn;
handler->remove_fn = remove_fn;
handler->user_data = user_data;
if (C) {
handler->context.area = CTX_wm_area(C);
handler->context.region = CTX_wm_region(C);
handler->context.menu = CTX_wm_menu(C);
}
else {
handler->context.area = NULL;
handler->context.region = NULL;
handler->context.menu = NULL;
}
BLI_assert((flag & WM_HANDLER_DO_FREE) == 0);
handler->head.flag = flag;
BLI_addhead(handlers, handler);
return handler;
}
/* Set "postpone" for win->modalhandlers, this is in a running for () loop in wm_handlers_do(). */
void WM_event_remove_ui_handler(ListBase *handlers,
wmUIHandlerFunc handle_fn,
wmUIHandlerRemoveFunc remove_fn,
void *user_data,
const bool postpone)
{
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
if ((handler->handle_fn == handle_fn) && (handler->remove_fn == remove_fn) &&
(handler->user_data == user_data)) {
/* Handlers will be freed in wm_handlers_do(). */
if (postpone) {
handler->head.flag |= WM_HANDLER_DO_FREE;
}
else {
BLI_remlink(handlers, handler);
wm_event_free_handler(&handler->head);
}
break;
}
}
}
}
void WM_event_free_ui_handler_all(bContext *C,
ListBase *handlers,
wmUIHandlerFunc handle_fn,
wmUIHandlerRemoveFunc remove_fn)
{
LISTBASE_FOREACH_MUTABLE (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
if ((handler->handle_fn == handle_fn) && (handler->remove_fn == remove_fn)) {
remove_fn(C, handler->user_data);
BLI_remlink(handlers, handler);
wm_event_free_handler(&handler->head);
}
}
}
}
wmEventHandler_Dropbox *WM_event_add_dropbox_handler(ListBase *handlers, ListBase *dropboxes)
{
/* Only allow same dropbox once. */
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_DROPBOX) {
wmEventHandler_Dropbox *handler = (wmEventHandler_Dropbox *)handler_base;
if (handler->dropboxes == dropboxes) {
return handler;
}
}
}
wmEventHandler_Dropbox *handler = MEM_callocN(sizeof(*handler), __func__);
handler->head.type = WM_HANDLER_TYPE_DROPBOX;
/* Dropbox stored static, no free or copy. */
handler->dropboxes = dropboxes;
BLI_addhead(handlers, handler);
return handler;
}
/* XXX solution works, still better check the real cause (ton) */
void WM_event_remove_area_handler(ListBase *handlers, void *area)
{
LISTBASE_FOREACH_MUTABLE (wmEventHandler *, handler_base, handlers) {
if (handler_base->type == WM_HANDLER_TYPE_UI) {
wmEventHandler_UI *handler = (wmEventHandler_UI *)handler_base;
if (handler->context.area == area) {
BLI_remlink(handlers, handler);
wm_event_free_handler(handler_base);
}
}
}
}
#if 0
static void WM_event_remove_handler(ListBase *handlers, wmEventHandler *handler)
{
BLI_remlink(handlers, handler);
wm_event_free_handler(handler);
}
#endif
void WM_event_add_mousemove(wmWindow *win)
{
win->addmousemove = 1;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Ghost Event Conversion
* \{ */
static int convert_key(GHOST_TKey key)
{
if (key >= GHOST_kKeyA && key <= GHOST_kKeyZ) {
return (EVT_AKEY + ((int)key - GHOST_kKeyA));
}
if (key >= GHOST_kKey0 && key <= GHOST_kKey9) {
return (EVT_ZEROKEY + ((int)key - GHOST_kKey0));
}
if (key >= GHOST_kKeyNumpad0 && key <= GHOST_kKeyNumpad9) {
return (EVT_PAD0 + ((int)key - GHOST_kKeyNumpad0));
}
if (key >= GHOST_kKeyF1 && key <= GHOST_kKeyF24) {
return (EVT_F1KEY + ((int)key - GHOST_kKeyF1));
}
switch (key) {
case GHOST_kKeyBackSpace:
return EVT_BACKSPACEKEY;
case GHOST_kKeyTab:
return EVT_TABKEY;
case GHOST_kKeyLinefeed:
return EVT_LINEFEEDKEY;
case GHOST_kKeyClear:
return 0;
case GHOST_kKeyEnter:
return EVT_RETKEY;
case GHOST_kKeyEsc:
return EVT_ESCKEY;
case GHOST_kKeySpace:
return EVT_SPACEKEY;
case GHOST_kKeyQuote:
return EVT_QUOTEKEY;
case GHOST_kKeyComma:
return EVT_COMMAKEY;
case GHOST_kKeyMinus:
return EVT_MINUSKEY;
case GHOST_kKeyPlus:
return EVT_PLUSKEY;
case GHOST_kKeyPeriod:
return EVT_PERIODKEY;
case GHOST_kKeySlash:
return EVT_SLASHKEY;
case GHOST_kKeySemicolon:
return EVT_SEMICOLONKEY;
case GHOST_kKeyEqual:
return EVT_EQUALKEY;
case GHOST_kKeyLeftBracket:
return EVT_LEFTBRACKETKEY;
case GHOST_kKeyRightBracket:
return EVT_RIGHTBRACKETKEY;
case GHOST_kKeyBackslash:
return EVT_BACKSLASHKEY;
case GHOST_kKeyAccentGrave:
return EVT_ACCENTGRAVEKEY;
case GHOST_kKeyLeftShift:
return EVT_LEFTSHIFTKEY;
case GHOST_kKeyRightShift:
return EVT_RIGHTSHIFTKEY;
case GHOST_kKeyLeftControl:
return EVT_LEFTCTRLKEY;
case GHOST_kKeyRightControl:
return EVT_RIGHTCTRLKEY;
case GHOST_kKeyOS:
return EVT_OSKEY;
case GHOST_kKeyLeftAlt:
return EVT_LEFTALTKEY;
case GHOST_kKeyRightAlt:
return EVT_RIGHTALTKEY;
case GHOST_kKeyApp:
return EVT_APPKEY;
case GHOST_kKeyCapsLock:
return EVT_CAPSLOCKKEY;
case GHOST_kKeyNumLock:
return 0;
case GHOST_kKeyScrollLock:
return 0;
case GHOST_kKeyLeftArrow:
return EVT_LEFTARROWKEY;
case GHOST_kKeyRightArrow:
return EVT_RIGHTARROWKEY;
case GHOST_kKeyUpArrow:
return EVT_UPARROWKEY;
case GHOST_kKeyDownArrow:
return EVT_DOWNARROWKEY;
case GHOST_kKeyPrintScreen:
return 0;
case GHOST_kKeyPause:
return EVT_PAUSEKEY;
case GHOST_kKeyInsert:
return EVT_INSERTKEY;
case GHOST_kKeyDelete:
return EVT_DELKEY;
case GHOST_kKeyHome:
return EVT_HOMEKEY;
case GHOST_kKeyEnd:
return EVT_ENDKEY;
case GHOST_kKeyUpPage:
return EVT_PAGEUPKEY;
case GHOST_kKeyDownPage:
return EVT_PAGEDOWNKEY;
case GHOST_kKeyNumpadPeriod:
return EVT_PADPERIOD;
case GHOST_kKeyNumpadEnter:
return EVT_PADENTER;
case GHOST_kKeyNumpadPlus:
return EVT_PADPLUSKEY;
case GHOST_kKeyNumpadMinus:
return EVT_PADMINUS;
case GHOST_kKeyNumpadAsterisk:
return EVT_PADASTERKEY;
case GHOST_kKeyNumpadSlash:
return EVT_PADSLASHKEY;
case GHOST_kKeyGrLess:
return EVT_GRLESSKEY;
case GHOST_kKeyMediaPlay:
return EVT_MEDIAPLAY;
case GHOST_kKeyMediaStop:
return EVT_MEDIASTOP;
case GHOST_kKeyMediaFirst:
return EVT_MEDIAFIRST;
case GHOST_kKeyMediaLast:
return EVT_MEDIALAST;
default:
return EVT_UNKNOWNKEY; /* GHOST_kKeyUnknown */
}
}
static void wm_eventemulation(wmEvent *event, bool test_only)
{
/* Store last middle-mouse event value to make emulation work
* when modifier keys are released first.
* This really should be in a data structure somewhere. */
static int emulating_event = EVENT_NONE;
/* Middle-mouse emulation. */
if (U.flag & USER_TWOBUTTONMOUSE) {
if (event->type == LEFTMOUSE) {
short *mod = (
#if !defined(WIN32)
(U.mouse_emulate_3_button_modifier == USER_EMU_MMB_MOD_OSKEY) ? &event->oskey :
&event->alt
#else
/* Disable for WIN32 for now because it accesses the start menu. */
&event->alt
#endif
);
if (event->val == KM_PRESS) {
if (*mod) {
*mod = 0;
event->type = MIDDLEMOUSE;
if (!test_only) {
emulating_event = MIDDLEMOUSE;
}
}
}
else if (event->val == KM_RELEASE) {
/* Only send middle-mouse release if emulated. */
if (emulating_event == MIDDLEMOUSE) {
event->type = MIDDLEMOUSE;
*mod = 0;
}
if (!test_only) {
emulating_event = EVENT_NONE;
}
}
}
}
/* Numpad emulation. */
if (U.flag & USER_NONUMPAD) {
switch (event->type) {
case EVT_ZEROKEY:
event->type = EVT_PAD0;
break;
case EVT_ONEKEY:
event->type = EVT_PAD1;
break;
case EVT_TWOKEY:
event->type = EVT_PAD2;
break;
case EVT_THREEKEY:
event->type = EVT_PAD3;
break;
case EVT_FOURKEY:
event->type = EVT_PAD4;
break;
case EVT_FIVEKEY:
event->type = EVT_PAD5;
break;
case EVT_SIXKEY:
event->type = EVT_PAD6;
break;
case EVT_SEVENKEY:
event->type = EVT_PAD7;
break;
case EVT_EIGHTKEY:
event->type = EVT_PAD8;
break;
case EVT_NINEKEY:
event->type = EVT_PAD9;
break;
case EVT_MINUSKEY:
event->type = EVT_PADMINUS;
break;
case EVT_EQUALKEY:
event->type = EVT_PADPLUSKEY;
break;
case EVT_BACKSLASHKEY:
event->type = EVT_PADSLASHKEY;
break;
}
}
}
static const wmTabletData wm_event_tablet_data_default = {
.active = EVT_TABLET_NONE,
.pressure = 1.0f,
.x_tilt = 0.0f,
.y_tilt = 0.0f,
.is_motion_absolute = false,
};
void WM_event_tablet_data_default_set(wmTabletData *tablet_data)
{
*tablet_data = wm_event_tablet_data_default;
}
void wm_tablet_data_from_ghost(const GHOST_TabletData *tablet_data, wmTabletData *wmtab)
{
if ((tablet_data != NULL) && tablet_data->Active != GHOST_kTabletModeNone) {
wmtab->active = (int)tablet_data->Active;
wmtab->pressure = wm_pressure_curve(tablet_data->Pressure);
wmtab->x_tilt = tablet_data->Xtilt;
wmtab->y_tilt = tablet_data->Ytilt;
/* We could have a preference to support relative tablet motion (we can't detect that). */
wmtab->is_motion_absolute = true;
// printf("%s: using tablet %.5f\n", __func__, wmtab->pressure);
}
else {
*wmtab = wm_event_tablet_data_default;
// printf("%s: not using tablet\n", __func__);
}
}
#ifdef WITH_INPUT_NDOF
/* Adds customdata to event. */
static void attach_ndof_data(wmEvent *event, const GHOST_TEventNDOFMotionData *ghost)
{
wmNDOFMotionData *data = MEM_mallocN(sizeof(wmNDOFMotionData), "customdata NDOF");
const float ts = U.ndof_sensitivity;
const float rs = U.ndof_orbit_sensitivity;
mul_v3_v3fl(data->tvec, &ghost->tx, ts);
mul_v3_v3fl(data->rvec, &ghost->rx, rs);
if (U.ndof_flag & NDOF_PAN_YZ_SWAP_AXIS) {
float t;
t = data->tvec[1];
data->tvec[1] = -data->tvec[2];
data->tvec[2] = t;
}
data->dt = ghost->dt;
data->progress = (wmProgress)ghost->progress;
event->custom = EVT_DATA_NDOF_MOTION;
event->customdata = data;
event->customdatafree = 1;
}
#endif /* WITH_INPUT_NDOF */
/* Imperfect but probably usable... draw/enable drags to other windows. */
static wmWindow *wm_event_cursor_other_windows(wmWindowManager *wm, wmWindow *win, wmEvent *event)
{
int mval[2] = {event->x, event->y};
if (wm->windows.first == wm->windows.last) {
return NULL;
}
/* In order to use window size and mouse position (pixels), we have to use a WM function. */
/* check if outside, include top window bar... */
if (mval[0] < 0 || mval[1] < 0 || mval[0] > WM_window_pixels_x(win) ||
mval[1] > WM_window_pixels_y(win) + 30) {
/* Let's skip windows having modal handlers now */
/* potential XXX ugly... I wouldn't have added a modalhandlers list
* (introduced in rev 23331, ton). */
LISTBASE_FOREACH (wmEventHandler *, handler, &win->modalhandlers) {
if (ELEM(handler->type, WM_HANDLER_TYPE_UI, WM_HANDLER_TYPE_OP)) {
return NULL;
}
}
wmWindow *win_other;
if (WM_window_find_under_cursor(wm, win, win, mval, &win_other, mval)) {
event->x = mval[0];
event->y = mval[1];
return win_other;
}
}
return NULL;
}
static bool wm_event_is_double_click(const wmEvent *event)
{
if ((event->type == event->prevtype) && (event->prevval == KM_RELEASE) &&
(event->val == KM_PRESS)) {
if (ISMOUSE(event->type) && WM_event_drag_test(event, &event->prevclickx)) {
/* Pass. */
}
else {
if ((PIL_check_seconds_timer() - event->prevclicktime) * 1000 < U.dbl_click_time) {
return true;
}
}
}
return false;
}
/**
* Copy the current state to the previous event state.
*/
static void wm_event_prev_values_set(wmEvent *event, wmEvent *event_state)
{
event->prevval = event_state->prevval = event_state->val;
event->prevtype = event_state->prevtype = event_state->type;
}
static void wm_event_prev_click_set(wmEvent *event, wmEvent *event_state)
{
event->prevclicktime = event_state->prevclicktime = PIL_check_seconds_timer();
event->prevclickx = event_state->prevclickx = event_state->x;
event->prevclicky = event_state->prevclicky = event_state->y;
}
static wmEvent *wm_event_add_mousemove(wmWindow *win, const wmEvent *event)
{
wmEvent *event_last = win->event_queue.last;
/* Some painting operators want accurate mouse events, they can
* handle in between mouse move moves, others can happily ignore
* them for better performance. */
if (event_last && event_last->type == MOUSEMOVE) {
event_last->type = INBETWEEN_MOUSEMOVE;
}
wmEvent *event_new = wm_event_add(win, event);
if (event_last == NULL) {
event_last = win->eventstate;
}
copy_v2_v2_int(&event_new->prevx, &event_last->x);
return event_new;
}
static wmEvent *wm_event_add_trackpad(wmWindow *win, const wmEvent *event, int deltax, int deltay)
{
/* Ignore in between trackpad events for performance, we only need high accuracy
* for painting with mouse moves, for navigation using the accumulated value is ok. */
wmEvent *event_last = win->event_queue.last;
if (event_last && event_last->type == event->type) {
deltax += event_last->x - event_last->prevx;
deltay += event_last->y - event_last->prevy;
wm_event_free_last(win);
}
/* Set prevx/prevy, the delta is computed from this in operators. */
wmEvent *event_new = wm_event_add(win, event);
event_new->prevx = event_new->x - deltax;
event_new->prevy = event_new->y - deltay;
return event_new;
}
/**
* Windows store own event queues #wmWindow.event_queue (no #bContext here).
*/
void wm_event_add_ghostevent(wmWindowManager *wm, wmWindow *win, int type, void *customdata)
{
if (UNLIKELY(G.f & G_FLAG_EVENT_SIMULATE)) {
return;
}
/**
* Having both, \a event and \a event_state, can be highly confusing to work with,
* but is necessary for our current event system, so let's clear things up a bit:
*
* - Data added to event only will be handled immediately,
* but will not be copied to the next event.
* - Data added to \a event_state only stays,
* but is handled with the next event -> execution delay.
* - Data added to event and \a event_state stays and is handled immediately.
*/
wmEvent event, *event_state = win->eventstate;
/* Initialize and copy state (only mouse x y and modifiers). */
event = *event_state;
event.is_repeat = false;
/**
* Always support accessing the last key press/release. This is set from `win->eventstate`,
* so it will always be a valid event type to store in the previous state.
*
* Note that these values are intentionally _not_ set in the `win->eventstate`,
* as copying these values only makes sense when `win->eventstate->{val/type}` would be
* written to (which only happens for some kinds of events).
* If this was done it could leave `win->eventstate` previous and current value
* set to the same key press/release state which doesn't make sense.
*/
event.prevtype = event.type;
event.prevval = event.val;
/* Ensure the event state is correct, any deviation from this may cause bugs. */
#ifndef NDEBUG
if ((event_state->type || event_state->val) && /* Ignore cleared event state. */
!(ISMOUSE_BUTTON(event_state->type) || ISKEYBOARD(event_state->type))) {
CLOG_WARN(WM_LOG_HANDLERS,
"Non-keyboard/mouse button found in 'win->eventstate->type = %d'",
event_state->type);
}
if ((event_state->prevtype || event_state->prevval) && /* Ignore cleared event state. */
!(ISMOUSE_BUTTON(event_state->prevtype) || ISKEYBOARD(event_state->prevtype))) {
CLOG_WARN(WM_LOG_HANDLERS,
"Non-keyboard/mouse button found in 'win->eventstate->prevtype = %d'",
event_state->prevtype);
}
#endif
switch (type) {
/* Mouse move, also to inactive window (X11 does this). */
case GHOST_kEventCursorMove: {
GHOST_TEventCursorData *cd = customdata;
copy_v2_v2_int(&event.x, &cd->x);
wm_stereo3d_mouse_offset_apply(win, &event.x);
wm_tablet_data_from_ghost(&cd->tablet, &event.tablet);
event.type = MOUSEMOVE;
{
wmEvent *event_new = wm_event_add_mousemove(win, &event);
copy_v2_v2_int(&event_state->x, &event_new->x);
event_state->tablet.is_motion_absolute = event_new->tablet.is_motion_absolute;
}
/* Also add to other window if event is there, this makes overdraws disappear nicely. */
/* It remaps mousecoord to other window in event. */
wmWindow *win_other = wm_event_cursor_other_windows(wm, win, &event);
if (win_other) {
wmEvent event_other = *win_other->eventstate;
/* See comment for this operation on `event` for details. */
event_other.prevtype = event_other.type;
event_other.prevval = event_other.val;
copy_v2_v2_int(&event_other.x, &event.x);
event_other.type = MOUSEMOVE;
{
wmEvent *event_new = wm_event_add_mousemove(win_other, &event_other);
copy_v2_v2_int(&win_other->eventstate->x, &event_new->x);
win_other->eventstate->tablet.is_motion_absolute = event_new->tablet.is_motion_absolute;
}
}
break;
}
case GHOST_kEventTrackpad: {
GHOST_TEventTrackpadData *pd = customdata;
switch (pd->subtype) {
case GHOST_kTrackpadEventMagnify:
event.type = MOUSEZOOM;
pd->deltaX = -pd->deltaX;
pd->deltaY = -pd->deltaY;
break;
case GHOST_kTrackpadEventSmartMagnify:
event.type = MOUSESMARTZOOM;
break;
case GHOST_kTrackpadEventRotate:
event.type = MOUSEROTATE;
break;
case GHOST_kTrackpadEventScroll:
default:
event.type = MOUSEPAN;
break;
}
event.x = event_state->x = pd->x;
event.y = event_state->y = pd->y;
event.val = KM_NOTHING;
/* The direction is inverted from the device due to system preferences. */
event.is_direction_inverted = pd->isDirectionInverted;
wm_event_add_trackpad(win, &event, pd->deltaX, -pd->deltaY);
break;
}
/* Mouse button. */
case GHOST_kEventButtonDown:
case GHOST_kEventButtonUp: {
GHOST_TEventButtonData *bd = customdata;
/* Get value and type from Ghost. */
event.val = (type == GHOST_kEventButtonDown) ? KM_PRESS : KM_RELEASE;
if (bd->button == GHOST_kButtonMaskLeft) {
event.type = LEFTMOUSE;
}
else if (bd->button == GHOST_kButtonMaskRight) {
event.type = RIGHTMOUSE;
}
else if (bd->button == GHOST_kButtonMaskButton4) {
event.type = BUTTON4MOUSE;
}
else if (bd->button == GHOST_kButtonMaskButton5) {
event.type = BUTTON5MOUSE;
}
else if (bd->button == GHOST_kButtonMaskButton6) {
event.type = BUTTON6MOUSE;
}
else if (bd->button == GHOST_kButtonMaskButton7) {
event.type = BUTTON7MOUSE;
}
else {
event.type = MIDDLEMOUSE;
}
/* Get tablet data. */
wm_tablet_data_from_ghost(&bd->tablet, &event.tablet);
wm_eventemulation(&event, false);
wm_event_prev_values_set(&event, event_state);
/* Copy to event state. */
event_state->val = event.val;
event_state->type = event.type;
/* Double click test. */
if (wm_event_is_double_click(&event)) {
CLOG_INFO(WM_LOG_HANDLERS, 1, "Send double click");
event.val = KM_DBL_CLICK;
}
if (event.val == KM_PRESS) {
wm_event_prev_click_set(&event, event_state);
}
/* Add to other window if event is there (not to both!). */
wmWindow *win_other = wm_event_cursor_other_windows(wm, win, &event);
if (win_other) {
wmEvent event_other = *win_other->eventstate;
/* See comment for this operation on `event` for details. */
event_other.prevtype = event_other.type;
event_other.prevval = event_other.val;
copy_v2_v2_int(&event_other.x, &event.x);
event_other.type = event.type;
event_other.val = event.val;
event_other.tablet = event.tablet;
wm_event_add(win_other, &event_other);
}
else {
wm_event_add(win, &event);
}
break;
}
/* Keyboard. */
case GHOST_kEventKeyDown:
case GHOST_kEventKeyUp: {
GHOST_TEventKeyData *kd = customdata;
short keymodifier = KM_NOTHING;
event.type = convert_key(kd->key);
event.ascii = kd->ascii;
/* Might be not NULL terminated. */
memcpy(event.utf8_buf, kd->utf8_buf, sizeof(event.utf8_buf));
event.is_repeat = kd->is_repeat;
event.val = (type == GHOST_kEventKeyDown) ? KM_PRESS : KM_RELEASE;
wm_eventemulation(&event, false);
wm_event_prev_values_set(&event, event_state);
/* Copy to event state. */
event_state->val = event.val;
event_state->type = event.type;
event_state->is_repeat = event.is_repeat;
/* Exclude arrow keys, esc, etc from text input. */
if (type == GHOST_kEventKeyUp) {
event.ascii = '\0';
/* Ghost should do this already for key up. */
if (event.utf8_buf[0]) {
CLOG_ERROR(WM_LOG_EVENTS,
"ghost on your platform is misbehaving, utf8 events on key up!");
}
event.utf8_buf[0] = '\0';
}
else {
if (event.ascii < 32 && event.ascii > 0) {
event.ascii = '\0';
}
if (event.utf8_buf[0] < 32 && event.utf8_buf[0] > 0) {
event.utf8_buf[0] = '\0';
}
}
if (event.utf8_buf[0]) {
if (BLI_str_utf8_size(event.utf8_buf) == -1) {
CLOG_ERROR(WM_LOG_EVENTS,
"ghost detected an invalid unicode character '%d'",
(int)(unsigned char)event.utf8_buf[0]);
event.utf8_buf[0] = '\0';
}
}
/* Assigning both first and second is strange. - campbell */
switch (event.type) {
case EVT_LEFTSHIFTKEY:
case EVT_RIGHTSHIFTKEY:
if (event.val == KM_PRESS) {
if (event_state->ctrl || event_state->alt || event_state->oskey) {
keymodifier = (KM_MOD_FIRST | KM_MOD_SECOND);
}
else {
keymodifier = KM_MOD_FIRST;
}
}
event.shift = event_state->shift = keymodifier;
break;
case EVT_LEFTCTRLKEY:
case EVT_RIGHTCTRLKEY:
if (event.val == KM_PRESS) {
if (event_state->shift || event_state->alt || event_state->oskey) {
keymodifier = (KM_MOD_FIRST | KM_MOD_SECOND);
}
else {
keymodifier = KM_MOD_FIRST;
}
}
event.ctrl = event_state->ctrl = keymodifier;
break;
case EVT_LEFTALTKEY:
case EVT_RIGHTALTKEY:
if (event.val == KM_PRESS) {
if (event_state->ctrl || event_state->shift || event_state->oskey) {
keymodifier = (KM_MOD_FIRST | KM_MOD_SECOND);
}
else {
keymodifier = KM_MOD_FIRST;
}
}
event.alt = event_state->alt = keymodifier;
break;
case EVT_OSKEY:
if (event.val == KM_PRESS) {
if (event_state->ctrl || event_state->alt || event_state->shift) {
keymodifier = (KM_MOD_FIRST | KM_MOD_SECOND);
}
else {
keymodifier = KM_MOD_FIRST;
}
}
event.oskey = event_state->oskey = keymodifier;
break;
default:
if (event.val == KM_PRESS && event.keymodifier == 0) {
/* Only set in eventstate, for next event. */
event_state->keymodifier = event.type;
}
else if (event.val == KM_RELEASE && event.keymodifier == event.type) {
event.keymodifier = event_state->keymodifier = 0;
}
break;
}
/* Double click test. */
/* If previous event was same type, and previous was release, and now it presses... */
if (wm_event_is_double_click(&event)) {
CLOG_INFO(WM_LOG_HANDLERS, 1, "Send double click");
event.val = KM_DBL_CLICK;
}
/* This case happens on holding a key pressed, it should not generate
* press events events with the same key as modifier. */
if (event.keymodifier == event.type) {
event.keymodifier = 0;
}
/* This case happens with an external numpad, and also when using 'dead keys'
* (to compose complex latin characters e.g.), it's not really clear why.
* Since it's impossible to map a key modifier to an unknown key,
* it shouldn't harm to clear it. */
if (event.keymodifier == EVT_UNKNOWNKEY) {
event_state->keymodifier = event.keymodifier = 0;
}
/* If test_break set, it catches this. Do not set with modifier presses.
* XXX Keep global for now? */
if ((event.type == EVT_ESCKEY && event.val == KM_PRESS) &&
/* Check other modifiers because ms-windows uses these to bring up the task manager. */
(event.shift == 0 && event.ctrl == 0 && event.alt == 0)) {
G.is_break = true;
}
/* Double click test - only for press. */
if (event.val == KM_PRESS) {
/* Don't reset timer & location when holding the key generates repeat events. */
if (event.is_repeat == false) {
wm_event_prev_click_set(&event, event_state);
}
}
wm_event_add(win, &event);
break;
}
case GHOST_kEventWheel: {
GHOST_TEventWheelData *wheelData = customdata;
if (wheelData->z > 0) {
event.type = WHEELUPMOUSE;
}
else {
event.type = WHEELDOWNMOUSE;
}
event.val = KM_PRESS;
wm_event_add(win, &event);
break;
}
case GHOST_kEventTimer: {
event.type = TIMER;
event.custom = EVT_DATA_TIMER;
event.customdata = customdata;
event.val = KM_NOTHING;
event.keymodifier = 0;
wm_event_add(win, &event);
break;
}
#ifdef WITH_INPUT_NDOF
case GHOST_kEventNDOFMotion: {
event.type = NDOF_MOTION;
event.val = KM_NOTHING;
attach_ndof_data(&event, customdata);
wm_event_add(win, &event);
CLOG_INFO(WM_LOG_HANDLERS, 1, "sending NDOF_MOTION, prev = %d %d", event.x, event.y);
break;
}
case GHOST_kEventNDOFButton: {
GHOST_TEventNDOFButtonData *e = customdata;
event.type = NDOF_BUTTON_NONE + e->button;
switch (e->action) {
case GHOST_kPress:
event.val = KM_PRESS;
break;
case GHOST_kRelease:
event.val = KM_RELEASE;
break;
}
event.custom = 0;
event.customdata = NULL;
wm_event_add(win, &event);
break;
}
#endif /* WITH_INPUT_NDOF */
case GHOST_kEventUnknown:
case GHOST_kNumEventTypes:
break;
case GHOST_kEventWindowDeactivate: {
event.type = WINDEACTIVATE;
wm_event_add(win, &event);
break;
}
#ifdef WITH_INPUT_IME
case GHOST_kEventImeCompositionStart: {
event.val = KM_PRESS;
win->ime_data = customdata;
win->ime_data->is_ime_composing = true;
event.type = WM_IME_COMPOSITE_START;
wm_event_add(win, &event);
break;
}
case GHOST_kEventImeComposition: {
event.val = KM_PRESS;
event.type = WM_IME_COMPOSITE_EVENT;
wm_event_add(win, &event);
break;
}
case GHOST_kEventImeCompositionEnd: {
event.val = KM_PRESS;
if (win->ime_data) {
win->ime_data->is_ime_composing = false;
}
event.type = WM_IME_COMPOSITE_END;
wm_event_add(win, &event);
break;
}
#endif /* WITH_INPUT_IME */
}
#if 0
WM_event_print(&event);
#endif
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name WM Interface Locking
* \{ */
/**
* Check whether operator is allowed to run in case interface is locked,
* If interface is unlocked, will always return truth.
*/
static bool wm_operator_check_locked_interface(bContext *C, wmOperatorType *ot)
{
wmWindowManager *wm = CTX_wm_manager(C);
if (wm->is_interface_locked) {
if ((ot->flag & OPTYPE_LOCK_BYPASS) == 0) {
return false;
}
}
return true;
}
void WM_set_locked_interface(wmWindowManager *wm, bool lock)
{
/* This will prevent events from being handled while interface is locked
*
* Use a "local" flag for now, because currently no other areas could
* benefit of locked interface anyway (aka using G.is_interface_locked
* wouldn't be useful anywhere outside of window manager, so let's not
* pollute global context with such an information for now).
*/
wm->is_interface_locked = lock ? 1 : 0;
/* This will prevent drawing regions which uses non-threadsafe data.
* Currently it'll be just a 3D viewport.
*
* TODO(sergey): Make it different locked states, so different jobs
* could lock different areas of blender and allow
* interaction with others?
*/
BKE_spacedata_draw_locks(lock);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Event / Keymap Matching API
* \{ */
wmKeyMap *WM_event_get_keymap_from_handler(wmWindowManager *wm, wmEventHandler_Keymap *handler)
{
wmKeyMap *keymap;
if (handler->dynamic.keymap_fn != NULL) {
keymap = handler->dynamic.keymap_fn(wm, handler);
BLI_assert(handler->keymap == NULL);
}
else {
keymap = WM_keymap_active(wm, handler->keymap);
BLI_assert(keymap != NULL);
}
return keymap;
}
wmKeyMapItem *WM_event_match_keymap_item(bContext *C, wmKeyMap *keymap, const wmEvent *event)
{
LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) {
if (wm_eventmatch(event, kmi)) {
wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
if (WM_operator_poll_context(C, ot, WM_OP_INVOKE_DEFAULT)) {
return kmi;
}
}
}
return NULL;
}
wmKeyMapItem *WM_event_match_keymap_item_from_handlers(bContext *C,
wmWindowManager *wm,
ListBase *handlers,
const wmEvent *event)
{
LISTBASE_FOREACH (wmEventHandler *, handler_base, handlers) {
/* During this loop, UI handlers for nested menus can tag multiple handlers free. */
if (handler_base->flag & WM_HANDLER_DO_FREE) {
/* Pass. */
}
else if (handler_base->poll == NULL || handler_base->poll(CTX_wm_region(C), event)) {
if (handler_base->type == WM_HANDLER_TYPE_KEYMAP) {
wmEventHandler_Keymap *handler = (wmEventHandler_Keymap *)handler_base;
wmKeyMap *keymap = WM_event_get_keymap_from_handler(wm, handler);
if (keymap && WM_keymap_poll(C, keymap)) {
wmKeyMapItem *kmi = WM_event_match_keymap_item(C, keymap, event);
if (kmi != NULL) {
return kmi;
}
}
}
}
}
return NULL;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Cursor Keymap Status
*
* Show cursor keys in the status bar.
* This is done by detecting changes to the state - full keymap lookups are expensive
* so only perform this on changing tools, space types, pressing different modifier keys... etc.
* \{ */
/** State storage to detect changes between calls to refresh the information. */
struct CursorKeymapInfo_State {
struct {
short shift, ctrl, alt, oskey;
} modifiers;
short space_type;
short region_type;
/* Never use, just compare memory for changes. */
bToolRef tref;
};
struct CursorKeymapInfo {
/* 0: mouse button index
* 1: event type (click/press, drag)
* 2: text.
*/
char text[3][2][128];
wmEvent state_event;
struct CursorKeymapInfo_State state;
};
static void wm_event_cursor_store(struct CursorKeymapInfo_State *state,
const wmEvent *event,
short space_type,
short region_type,
const bToolRef *tref)
{
state->modifiers.shift = event->shift;
state->modifiers.ctrl = event->ctrl;
state->modifiers.alt = event->alt;
state->modifiers.oskey = event->oskey;
state->space_type = space_type;
state->region_type = region_type;
state->tref = tref ? *tref : (bToolRef){0};
}
const char *WM_window_cursor_keymap_status_get(const wmWindow *win,
int button_index,
int type_index)
{
if (win->cursor_keymap_status != NULL) {
struct CursorKeymapInfo *cd = win->cursor_keymap_status;
const char *msg = cd->text[button_index][type_index];
if (*msg) {
return msg;
}
}
return NULL;
}
/**
* Similar to #BKE_screen_area_map_find_area_xy and related functions,
* use here since the area is stored in the window manager.
*/
ScrArea *WM_window_status_area_find(wmWindow *win, bScreen *screen)
{
if (screen->state == SCREENFULL) {
return NULL;
}
ScrArea *area_statusbar = NULL;
LISTBASE_FOREACH (ScrArea *, area, &win->global_areas.areabase) {
if (area->spacetype == SPACE_STATUSBAR) {
area_statusbar = area;
break;
}
}
return area_statusbar;
}
void WM_window_status_area_tag_redraw(wmWindow *win)
{
bScreen *screen = WM_window_get_active_screen(win);
ScrArea *area = WM_window_status_area_find(win, screen);
if (area != NULL) {
ED_area_tag_redraw(area);
}
}
void WM_window_cursor_keymap_status_refresh(bContext *C, wmWindow *win)
{
bScreen *screen = WM_window_get_active_screen(win);
ScrArea *area_statusbar = WM_window_status_area_find(win, screen);
if (area_statusbar == NULL) {
MEM_SAFE_FREE(win->cursor_keymap_status);
return;
}
struct CursorKeymapInfo *cd;
if (UNLIKELY(win->cursor_keymap_status == NULL)) {
win->cursor_keymap_status = MEM_callocN(sizeof(struct CursorKeymapInfo), __func__);
}
cd = win->cursor_keymap_status;
/* Detect unchanged state (early exit). */
if (memcmp(&cd->state_event, win->eventstate, sizeof(wmEvent)) == 0) {
return;
}
/* Now perform more comprehensive check,
* still keep this fast since it happens on mouse-move. */
struct CursorKeymapInfo cd_prev = *((struct CursorKeymapInfo *)win->cursor_keymap_status);
cd->state_event = *win->eventstate;
/* Find active region and associated area. */
ARegion *region = screen->active_region;
if (region == NULL) {
return;
}
ScrArea *area = NULL;
ED_screen_areas_iter (win, screen, area_iter) {
if (BLI_findindex(&area_iter->regionbase, region) != -1) {
area = area_iter;
break;
}
}
if (area == NULL) {
return;
}
/* Keep as-is. */
if (ELEM(area->spacetype, SPACE_STATUSBAR, SPACE_TOPBAR)) {
return;
}
if (ELEM(region->regiontype,
RGN_TYPE_HEADER,
RGN_TYPE_TOOL_HEADER,
RGN_TYPE_FOOTER,
RGN_TYPE_TEMPORARY,
RGN_TYPE_HUD)) {
return;
}
/* Fallback to window. */
if (ELEM(region->regiontype, RGN_TYPE_TOOLS, RGN_TYPE_TOOL_PROPS)) {
region = BKE_area_find_region_type(area, RGN_TYPE_WINDOW);
}
/* Detect changes to the state. */
{
bToolRef *tref = NULL;
if ((region->regiontype == RGN_TYPE_WINDOW) &&
((1 << area->spacetype) & WM_TOOLSYSTEM_SPACE_MASK)) {
ViewLayer *view_layer = WM_window_get_active_view_layer(win);
WorkSpace *workspace = WM_window_get_active_workspace(win);
const bToolKey tkey = {
.space_type = area->spacetype,
.mode = WM_toolsystem_mode_from_spacetype(view_layer, area, area->spacetype),
};
tref = WM_toolsystem_ref_find(workspace, &tkey);
}
wm_event_cursor_store(&cd->state, win->eventstate, area->spacetype, region->regiontype, tref);
if (memcmp(&cd->state, &cd_prev.state, sizeof(cd->state)) == 0) {
return;
}
}
/* Changed context found, detect changes to keymap and refresh the status bar. */
const struct {
int button_index;
int type_index; /* 0: press or click, 1: drag. */
int event_type;
int event_value;
} event_data[] = {
{0, 0, LEFTMOUSE, KM_PRESS},
{0, 0, LEFTMOUSE, KM_CLICK},
{0, 1, EVT_TWEAK_L, KM_ANY},
{1, 0, MIDDLEMOUSE, KM_PRESS},
{1, 0, MIDDLEMOUSE, KM_CLICK},
{1, 1, EVT_TWEAK_M, KM_ANY},
{2, 0, RIGHTMOUSE, KM_PRESS},
{2, 0, RIGHTMOUSE, KM_CLICK},
{2, 1, EVT_TWEAK_R, KM_ANY},
};
for (int button_index = 0; button_index < 3; button_index++) {
cd->text[button_index][0][0] = '\0';
cd->text[button_index][1][0] = '\0';
}
CTX_wm_window_set(C, win);
CTX_wm_area_set(C, area);
CTX_wm_region_set(C, region);
ListBase *handlers[] = {
®ion->handlers,
&area->handlers,
&win->handlers,
};
wmWindowManager *wm = CTX_wm_manager(C);
for (int data_index = 0; data_index < ARRAY_SIZE(event_data); data_index++) {
const int button_index = event_data[data_index].button_index;
const int type_index = event_data[data_index].type_index;
if (cd->text[button_index][type_index][0] != 0) {
continue;
}
wmEvent test_event = *win->eventstate;
test_event.type = event_data[data_index].event_type;
test_event.val = event_data[data_index].event_value;
wm_eventemulation(&test_event, true);
wmKeyMapItem *kmi = NULL;
for (int handler_index = 0; handler_index < ARRAY_SIZE(handlers); handler_index++) {
kmi = WM_event_match_keymap_item_from_handlers(C, wm, handlers[handler_index], &test_event);
if (kmi) {
break;
}
}
if (kmi) {
wmOperatorType *ot = WM_operatortype_find(kmi->idname, 0);
const char *name = (ot) ? WM_operatortype_name(ot, kmi->ptr) : kmi->idname;
STRNCPY(cd->text[button_index][type_index], name);
}
}
if (memcmp(&cd_prev.text, &cd->text, sizeof(cd_prev.text)) != 0) {
ED_area_tag_redraw(area_statusbar);
}
CTX_wm_window_set(C, NULL);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Modal Keymap Status
* \{ */
bool WM_window_modal_keymap_status_draw(bContext *C, wmWindow *win, uiLayout *layout)
{
wmWindowManager *wm = CTX_wm_manager(C);
wmKeyMap *keymap = NULL;
wmOperator *op = NULL;
LISTBASE_FOREACH (wmEventHandler *, handler_base, &win->modalhandlers) {
if (handler_base->type == WM_HANDLER_TYPE_OP) {
wmEventHandler_Op *handler = (wmEventHandler_Op *)handler_base;
if (handler->op != NULL) {
/* 'handler->keymap' could be checked too, seems not to be used. */
wmKeyMap *keymap_test = WM_keymap_active(wm, handler->op->type->modalkeymap);
if (keymap_test && keymap_test->modal_items) {
keymap = keymap_test;
op = handler->op;
break;
}
}
}
}
if (keymap == NULL || keymap->modal_items == NULL) {
return false;
}
const EnumPropertyItem *items = keymap->modal_items;
uiLayout *row = uiLayoutRow(layout, true);
for (int i = 0; items[i].identifier; i++) {
if (!items[i].identifier[0]) {
continue;
}
if ((keymap->poll_modal_item != NULL) &&
(keymap->poll_modal_item(op, items[i].value) == false)) {
continue;
}
bool show_text = true;
{
/* Warning: O(n^2). */
wmKeyMapItem *kmi = NULL;
for (kmi = keymap->items.first; kmi; kmi = kmi->next) {
if (kmi->propvalue == items[i].value) {
break;
}
}
if (kmi != NULL) {
if (kmi->val == KM_RELEASE) {
/* Assume release events just disable something which was toggled on. */
continue;
}
if (uiTemplateEventFromKeymapItem(row, items[i].name, kmi, false)) {
show_text = false;
}
}
}
if (show_text) {
char buf[UI_MAX_DRAW_STR];
int available_len = sizeof(buf);
char *p = buf;
WM_modalkeymap_operator_items_to_string_buf(
op->type, items[i].value, true, UI_MAX_SHORTCUT_STR, &available_len, &p);
p -= 1;
if (p > buf) {
BLI_snprintf(p, available_len, ": %s", items[i].name);
uiItemL(row, buf, 0);
}
}
}
return true;
}
/** \} */
|