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
|
/* license {{{ */
/*
wmctrl
A command line tool to interact with an EWMH/NetWM compatible X Window Manager.
Author, current maintainer: Tomas Styblo <tripie@cpan.org>
Copyright (C) 2003
This program is free software which I release under the GNU General Public
License. You may redistribute and/or modify this program under the terms
of that 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.
To get a copy of the GNU General Puplic License, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* }}} */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/xpm.h>
#include <X11/Xmd.h>
#include <glib.h>
#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define _NET_WM_STATE_ADD 1 /* add/set property */
#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
/* help {{{ */
#define HELP "wmctrl " VERSION "\n" \
"Usage: wmctrl [OPTION]...\n" \
"Actions:\n" \
" -m Show information about the window manager and\n" \
" about the environment.\n" \
" -l List windows managed by the window manager.\n" \
" -d List desktops. The current desktop is marked\n" \
" with an asterisk.\n" \
" -j List current desktop.\n" \
" -s <DESK> Switch to the specified desktop.\n" \
" -a <WIN> Activate the window by switching to its desktop and\n" \
" raising it.\n" \
" -c <WIN> Close the window gracefully.\n" \
" -R <WIN> Move the window to the current desktop and\n" \
" activate it.\n" \
" -Y <WIN> Iconify (minimize) the window.\n" \
" -r <WIN> -t <DESK> Move the window to the specified desktop.\n" \
" -r <WIN> -e <MVARG> Resize and move the window around the desktop.\n" \
" The format of the <MVARG> argument is described below.\n" \
" -r <WIN> -y <MVARG> Resize and move like above, then reactivate.\n" \
" -r <WIN> -b <STARG> Change the state of the window. Using this option it's\n" \
" possible for example to make the window maximized,\n" \
" shaded or fullscreen. The format of the <STARG>\n" \
" argument and list of possible states is given below.\n" \
" -r <WIN> -N <STR> Set the name (long title) of the window.\n" \
" -r <WIN> -I <STR> Set the icon name (short title) of the window.\n" \
" -r <WIN> -M <PATH> Set the mini-icon of the window to the xpm bitmap in <PATH>.\n" \
" -r <WIN> -T <STR> Set both the name and the icon name of the window.\n" \
" -r <WIN> -L List information about the window.\n" \
" -k (on|off|toggle) Activate or deactivate window manager's\n" \
" \"showing the desktop\" mode. Many window managers\n" \
" do not implement this mode.\n" \
" -o <X>,<Y> Change the viewport for the current desktop.\n" \
" The X and Y values are separated with a comma.\n" \
" They define the top left corner of the viewport.\n" \
" The window manager may ignore the request.\n" \
" -n <NUM> Change number of desktops.\n" \
" The window manager may ignore the request.\n" \
" -g <W>,<H> Change geometry (common size) of all desktops.\n" \
" The window manager may ignore the request.\n" \
" -h Print help.\n" \
"\n" \
"Options:\n" \
" -S List windows in stacking order (bottom to top).\n" \
" -i Interpret <WIN> as a numerical window ID.\n" \
" -p Include PIDs in the window list. Very few\n" \
" X applications support this feature.\n" \
" -G Include geometry in the window list.\n" \
" -x Include WM_CLASS in the window list or\n" \
" interpret <WIN> as the WM_CLASS name.\n" \
" -u Override auto-detection and force UTF-8 mode.\n" \
" -F Modifies the behavior of the window title matching\n" \
" algorithm. It will match only the full window title\n" \
" instead of a substring, when this option is used.\n" \
" Furthermore it makes the matching case sensitive.\n" \
" -v Be verbose. Useful for debugging.\n" \
" -w <WA> Use a workaround. The option may appear multiple\n" \
" times. List of available workarounds is given below.\n" \
"\n" \
"Arguments:\n" \
" <WIN> This argument specifies the window. By default it's\n" \
" interpreted as a string. The string is matched\n" \
" against the window titles and the first matching\n" \
" window is used. The matching isn't case sensitive\n" \
" and the string may appear in any position\n" \
" of the title.\n" \
"\n" \
" The -i option may be used to interpret the argument\n" \
" as a numerical window ID represented as a decimal\n" \
" number. If it starts with \"0x\", then\n" \
" it will be interpreted as a hexadecimal number.\n" \
"\n" \
" The -x option may be used to interpret the argument\n" \
" as a string, which is matched against the window's\n" \
" class name (WM_CLASS property). The first matching\n" \
" window is used. The matching isn't case sensitive\n" \
" and the string may appear in any position\n" \
" of the class name. So it's recommended to always use\n" \
" the -F option in conjunction with the -x option.\n" \
"\n" \
" The special string \":SELECT:\" (without the quotes)\n" \
" may be used to instruct wmctrl to let you select the\n" \
" window by clicking on it.\n" \
"\n" \
" The special string \":ACTIVE:\" (without the quotes)\n" \
" may be used to instruct wmctrl to use the currently\n" \
" active window for the action.\n" \
"\n" \
" <DESK> A desktop number. Desktops are counted from zero.\n" \
"\n" \
" <MVARG> Specifies a change to the position and size\n" \
" of the window. The format of the argument is:\n" \
"\n" \
" <G>,<X>,<Y>,<W>,<H>\n" \
"\n" \
" <G>: Gravity specified as a number. The numbers are\n" \
" defined in the EWMH specification. The value of\n" \
" zero is particularly useful, it means \"use the\n" \
" default gravity of the window\".\n" \
" <X>,<Y>: Coordinates of new position of the window.\n" \
" <W>,<H>: New width and height of the window.\n" \
"\n" \
" The value of -1 may appear in place of\n" \
" any of the <X>, <Y>, <W> and <H> properties\n" \
" to left the property unchanged.\n" \
"\n" \
" <STARG> Specifies a change to the state of the window\n" \
" by the means of _NET_WM_STATE request.\n" \
" This option allows two properties to be changed\n" \
" simultaneously, specifically to allow both\n" \
" horizontal and vertical maximization to be\n" \
" altered together.\n" \
"\n" \
" The format of the argument is:\n" \
"\n" \
" (remove|add|toggle),<PROP1>[,<PROP2>]\n" \
"\n" \
" The EWMH specification defines the\n" \
" following properties:\n" \
"\n" \
" modal, sticky, maximized_vert, maximized_horz,\n" \
" shaded, skip_taskbar, skip_pager, hidden,\n" \
" fullscreen, above, below\n" \
"\n" \
"Workarounds:\n" \
"\n" \
" DESKTOP_TITLES_INVALID_UTF8 Print non-ASCII desktop titles correctly\n" \
" when using Window Maker.\n" \
"\n" \
"The format of the window list:\n" \
"\n" \
" <window ID> <desktop ID> <client machine> <window title>\n" \
"\n" \
"The format of the desktop list:\n" \
"\n" \
" <desktop ID> [-*] <geometry> <viewport> <workarea> <title>\n" \
"\n" \
"\n" \
"Author, current maintainer: Tomas Styblo <tripie@cpan.org>\n" \
"Released under the GNU General Public License.\n" \
"Copyright (C) 2003\n"
/* }}} */
#define MAX_PROPERTY_VALUE_LEN 4096
#define SELECT_WINDOW_MAGIC ":SELECT:"
#define ACTIVE_WINDOW_MAGIC ":ACTIVE:"
#define p_verbose(...) if (options.verbose) { \
fprintf(stderr, __VA_ARGS__); \
}
/* declarations of static functions *//*{{{*/
static gboolean wm_supports (Display *disp, const gchar *prop);
static Window *get_client_list (Display *disp, unsigned long *size);
static int client_msg(Display *disp, Window win, char *msg,
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4);
static int list_windows (Display *disp);
static int list_current_desktop (Display *disp);
static int list_desktops (Display *disp);
static int showing_desktop (Display *disp);
static int change_viewport (Display *disp);
static int change_geometry (Display *disp);
static int change_number_of_desktops (Display *disp);
static int switch_desktop (Display *disp);
static int wm_info (Display *disp);
static gchar *get_output_str (gchar *str, gboolean is_utf8);
static int action_window (Display *disp, Window win, char mode);
static int action_window_pid (Display *disp, char mode);
static int action_window_str (Display *disp, char mode);
static int activate_window (Display *disp, Window win,
gboolean switch_desktop);
static int close_window (Display *disp, Window win);
static int longest_str (gchar **strv);
static int window_to_desktop (Display *disp, Window win, int desktop);
static void window_set_title (Display *disp, Window win, char *str, char mode);
static gchar *get_window_title (Display *disp, Window win);
static gchar *get_window_class (Display *disp, Window win);
static gchar *get_property (Display *disp, Window win,
Atom xa_prop_type, gchar *prop_name, unsigned long *size);
static void init_charset(void);
static int window_move_resize (Display *disp, Window win, char *arg);
static void display_window (Display *disp, Window win, int max_client_machine_len);
static int window_state (Display *disp, Window win, char *arg);
static Window Select_Window(Display *dpy);
static Window get_active_window(Display *dpy);
/*}}}*/
static struct {
int verbose;
int force_utf8;
int show_class;
int show_pid;
int show_geometry;
int stacking_order;
int match_by_id;
int match_by_cls;
int full_window_title_match;
int wa_desktop_titles_invalid_utf8;
char *param_window;
char *param;
} options;
static gboolean envir_utf8;
int main (int argc, char **argv) { /* {{{ */
int opt;
int action = 0;
int ret = EXIT_SUCCESS;
int missing_option = 1;
Display *disp;
memset(&options, 0, sizeof(options)); /* just for sure */
/* necessary to make g_get_charset() and g_locale_*() work */
setlocale(LC_ALL, "");
/* make "--help" and "--version" work. I don't want to use
* getopt_long for portability reasons */
if (argc == 2 && argv[1]) {
if (strcmp(argv[1], "--help") == 0) {
fputs(HELP, stdout);
return EXIT_SUCCESS;
}
else if (strcmp(argv[1], "--version") == 0) {
puts(VERSION);
return EXIT_SUCCESS;
}
}
while ((opt = getopt(argc, argv, "FGVvhSlupidjmxa:r:s:c:t:w:k:o:n:g:e:y:b:z:E:N:I:T:LR:Y:M:")) != -1) {
missing_option = 0;
switch (opt) {
case 'F':
options.full_window_title_match = 1;
break;
case 'S':
options.stacking_order = 1;
break;
case 'G':
options.show_geometry = 1;
break;
case 'i':
options.match_by_id = 1;
break;
case 'v':
options.verbose = 1;
break;
case 'u':
options.force_utf8 = 1;
break;
case 'x':
options.match_by_cls = 1;
options.show_class = 1;
break;
case 'p':
options.show_pid = 1;
break;
case 'a': case 'c': case 'R': case 'z': case 'Y': case 'E':
options.param_window = optarg;
action = opt;
break;
case 'r':
options.param_window = optarg;
break;
case 't': case 'e': case 'b': case 'N': case 'I': case 'T': case 'y': case 'L': case 'M':
options.param = optarg;
action = opt;
break;
case 's':
options.param = optarg;
action = opt;
break;
case 'w':
if (strcmp(optarg, "DESKTOP_TITLES_INVALID_UTF8") == 0) {
options.wa_desktop_titles_invalid_utf8 = 1;
}
else {
fprintf(stderr, "Unknown workaround: %s\n", optarg);
return EXIT_FAILURE;
}
break;
case 'k': case 'o': case 'n': case 'g':
options.param = optarg;
action = opt;
break;
case '?':
return EXIT_FAILURE;
default:
action = opt;
}
}
if (missing_option) {
fputs(HELP, stderr);
return EXIT_FAILURE;
}
init_charset();
if (! (disp = XOpenDisplay(NULL))) {
fputs("Cannot open display.\n", stderr);
return EXIT_FAILURE;
}
switch (action) {
case 'V':
puts(VERSION);
break;
case 'h':
fputs(HELP, stdout);
break;
case 'l':
ret = list_windows(disp);
break;
case 'j':
ret = list_current_desktop(disp);
break;
case 'd':
ret = list_desktops(disp);
break;
case 's':
ret = switch_desktop(disp);
break;
case 'm':
ret = wm_info(disp);
break;
case 'a': case 'c': case 'R': case 'z': case 'E':
case 't': case 'e': case 'b': case 'N': case 'I': case 'T': case 'y': case 'L': case 'M':
case 'Y':
if (! options.param_window) {
fputs("No window was specified.\n", stderr);
return EXIT_FAILURE;
}
if (options.match_by_id) {
ret = action_window_pid(disp, action);
}
else {
ret = action_window_str(disp, action);
}
break;
case 'k':
ret = showing_desktop(disp);
break;
case 'o':
ret = change_viewport(disp);
break;
case 'n':
ret = change_number_of_desktops(disp);
break;
case 'g':
ret = change_geometry(disp);
break;
}
XCloseDisplay(disp);
return ret;
}
/* }}} */
static void init_charset (void) {/*{{{*/
const gchar *charset; /* unused */
gchar *lang = getenv("LANG") ? g_ascii_strup(getenv("LANG"), -1) : NULL;
gchar *lc_ctype = getenv("LC_CTYPE") ? g_ascii_strup(getenv("LC_CTYPE"), -1) : NULL;
/* this glib function doesn't work on my system ... */
envir_utf8 = g_get_charset(&charset);
/* ... therefore we will examine the environment variables */
if (lc_ctype && (strstr(lc_ctype, "UTF8") || strstr(lc_ctype, "UTF-8"))) {
envir_utf8 = TRUE;
}
else if (lang && (strstr(lang, "UTF8") || strstr(lang, "UTF-8"))) {
envir_utf8 = TRUE;
}
g_free(lang);
g_free(lc_ctype);
if (options.force_utf8) {
envir_utf8 = TRUE;
}
p_verbose("envir_utf8: %d\n", envir_utf8);
}/*}}}*/
static int client_msg(Display *disp, Window win, char *msg, /* {{{ */
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4) {
XEvent event;
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
event.xclient.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.message_type = XInternAtom(disp, msg, False);
event.xclient.window = win;
event.xclient.format = 32;
event.xclient.data.l[0] = data0;
event.xclient.data.l[1] = data1;
event.xclient.data.l[2] = data2;
event.xclient.data.l[3] = data3;
event.xclient.data.l[4] = data4;
if (XSendEvent(disp, DefaultRootWindow(disp), False, mask, &event)) {
return EXIT_SUCCESS;
}
else {
fprintf(stderr, "Cannot send %s event.\n", msg);
return EXIT_FAILURE;
}
}/*}}}*/
static gchar *get_output_str (gchar *str, gboolean is_utf8) {/*{{{*/
gchar *out;
if (str == NULL) {
return NULL;
}
if (envir_utf8) {
if (is_utf8) {
out = g_strdup(str);
}
else {
if (! (out = g_locale_to_utf8(str, -1, NULL, NULL, NULL))) {
p_verbose("Cannot convert string from locale charset to UTF-8.\n");
out = g_strdup(str);
}
}
}
else {
if (is_utf8) {
if (! (out = g_locale_from_utf8(str, -1, NULL, NULL, NULL))) {
p_verbose("Cannot convert string from UTF-8 to locale charset.\n");
out = g_strdup(str);
}
}
else {
out = g_strdup(str);
}
}
return out;
}/*}}}*/
static int wm_info (Display *disp) {/*{{{*/
Window *sup_window = NULL;
gchar *wm_name = NULL;
gchar *wm_class = NULL;
unsigned long *wm_pid = NULL;
unsigned long *showing_desktop = NULL;
gboolean name_is_utf8 = TRUE;
gchar *name_out;
gchar *class_out;
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_SUPPORTING_WM_CHECK", NULL))) {
if (! (sup_window = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_SUPPORTING_WM_CHECK", NULL))) {
fputs("Cannot get window manager info properties.\n"
"(_NET_SUPPORTING_WM_CHECK or _WIN_SUPPORTING_WM_CHECK)\n", stderr);
return EXIT_FAILURE;
}
}
/* WM_NAME */
if (! (wm_name = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_name = get_property(disp, *sup_window,
XA_STRING, "_NET_WM_NAME", NULL))) {
p_verbose("Cannot get name of the window manager (_NET_WM_NAME).\n");
}
}
name_out = get_output_str(wm_name, name_is_utf8);
/* WM_CLASS */
if (! (wm_class = get_property(disp, *sup_window,
XInternAtom(disp, "UTF8_STRING", False), "WM_CLASS", NULL))) {
name_is_utf8 = FALSE;
if (! (wm_class = get_property(disp, *sup_window,
XA_STRING, "WM_CLASS", NULL))) {
p_verbose("Cannot get class of the window manager (WM_CLASS).\n");
}
}
class_out = get_output_str(wm_class, name_is_utf8);
/* WM_PID */
if (! (wm_pid = (unsigned long *)get_property(disp, *sup_window,
XA_CARDINAL, "_NET_WM_PID", NULL))) {
p_verbose("Cannot get pid of the window manager (_NET_WM_PID).\n");
}
/* _NET_SHOWING_DESKTOP */
if (! (showing_desktop = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_SHOWING_DESKTOP", NULL))) {
p_verbose("Cannot get the _NET_SHOWING_DESKTOP property.\n");
}
/* print out the info */
printf("Name: %s\n", name_out ? name_out : "N/A");
printf("Class: %s\n", class_out ? class_out : "N/A");
if (wm_pid) {
printf("PID: %lu\n", *wm_pid);
}
else {
printf("PID: N/A\n");
}
if (showing_desktop) {
printf("Window manager's \"showing the desktop\" mode: %s\n",
*showing_desktop == 1 ? "ON" : "OFF");
}
else {
printf("Window manager's \"showing the desktop\" mode: N/A\n");
}
g_free(name_out);
g_free(sup_window);
g_free(wm_name);
g_free(wm_class);
g_free(wm_pid);
g_free(showing_desktop);
return EXIT_SUCCESS;
}/*}}}*/
static int showing_desktop (Display *disp) {/*{{{*/
unsigned long state;
unsigned long *showing_desktop = NULL;
/* _NET_SHOWING_DESKTOP */
if (! (showing_desktop = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_SHOWING_DESKTOP", NULL))) {
p_verbose("Cannot get the _NET_SHOWING_DESKTOP property.\n");
}
if (strcmp(options.param, "on") == 0) {
state = 1;
}
else if (strcmp(options.param, "off") == 0) {
state = 0;
}
else if (strcmp(options.param, "toggle") == 0) {
if(*showing_desktop == 0) { state = 1; }
if(*showing_desktop == 1) { state = 0; }
}
else {
fputs("The argument to the -k option must be either \"on\" or \"off\" or \"toggle\"\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_SHOWING_DESKTOP",
state, 0, 0, 0, 0);
}/*}}}*/
static int change_viewport (Display *disp) {/*{{{*/
unsigned long x, y;
const char *argerr = "The -o option expects two integers separated with a comma.\n";
if (sscanf(options.param, "%lu,%lu", &x, &y) == 2) {
return client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_VIEWPORT",
x, y, 0, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
static int change_geometry (Display *disp) {/*{{{*/
unsigned long x, y;
const char *argerr = "The -g option expects two integers separated with a comma.\n";
if (sscanf(options.param, "%lu,%lu", &x, &y) == 2) {
return client_msg(disp, DefaultRootWindow(disp), "_NET_DESKTOP_GEOMETRY",
x, y, 0, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
static int change_number_of_desktops (Display *disp) {/*{{{*/
unsigned long n;
if (sscanf(options.param, "%lu", &n) != 1) {
fputs("The -n option expects an integer.\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_NUMBER_OF_DESKTOPS",
n, 0, 0, 0, 0);
}/*}}}*/
static int switch_desktop (Display *disp) {/*{{{*/
int target = -1;
target = atoi(options.param);
if (target == -1) {
fputs("Invalid desktop ID.\n", stderr);
return EXIT_FAILURE;
}
return client_msg(disp, DefaultRootWindow(disp), "_NET_CURRENT_DESKTOP",
(unsigned long)target, 0, 0, 0, 0);
}/*}}}*/
static void window_set_title (Display *disp, Window win, /* {{{ */
char *title, char mode) {
gchar *title_utf8;
gchar *title_local;
if (envir_utf8) {
title_utf8 = g_strdup(title);
title_local = NULL;
}
else {
if (! (title_utf8 = g_locale_to_utf8(title, -1, NULL, NULL, NULL))) {
title_utf8 = g_strdup(title);
}
title_local = g_strdup(title);
}
if (mode == 'T' || mode == 'N') {
/* set name */
if (title_local) {
XChangeProperty(disp, win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
(guchar *) title_local, strlen(title_local));
}
else {
XDeleteProperty(disp, win, XA_WM_NAME);
}
XChangeProperty(disp, win, XInternAtom(disp, "_NET_WM_NAME", False),
XInternAtom(disp, "UTF8_STRING", False), 8, PropModeReplace,
(guchar *) title_utf8, strlen(title_utf8));
}
if (mode == 'T' || mode == 'I') {
/* set icon name */
if (title_local) {
XChangeProperty(disp, win, XA_WM_ICON_NAME, XA_STRING, 8, PropModeReplace,
(guchar *) title_local, strlen(title_local));
}
else {
XDeleteProperty(disp, win, XA_WM_ICON_NAME);
}
XChangeProperty(disp, win, XInternAtom(disp, "_NET_WM_ICON_NAME", False),
XInternAtom(disp, "UTF8_STRING", False), 8, PropModeReplace,
(guchar *) title_utf8, strlen(title_utf8));
}
g_free(title_utf8);
g_free(title_local);
}/*}}}*/
int window_set_mini_icon (Display *disp, Window win, /* {{{ */
char *path, char mode) {
XpmImage xpmImage;
XpmInfo xpmInfo;
Colormap colormap;
CARD32 *xpmColors;
XColor xc;
char *c;
unsigned long *iconBuffer;
int iconBufferSize;
int i;
if (XpmReadFileToXpmImage(path, &xpmImage, &xpmInfo) != 0) {
fputs("Invalid icon path.\n", stderr);
return EXIT_FAILURE;
}
colormap = DefaultColormap(disp, DefaultScreen(disp));
xpmColors = (CARD32 *)g_malloc0(xpmImage.ncolors * sizeof(CARD32));
for( i = 0; i < xpmImage.ncolors; i++ ) {
c = xpmImage.colorTable[i].c_color;
if (c == NULL) c = xpmImage.colorTable[i].g_color;
// else if (c == NULL) c = xpmImage.colorTable[i].g4_color;
// else if (c == NULL) c = xpmImage.colorTable[i].m_color;
// else if (c == NULL) c = xpmImage.colorTable[i].symbolic;
if (c == NULL) {
// Unknown color
xpmColors[i] = 0;
} else if (strcmp(c, "None") == 0) {
// No color - see thru
xpmColors[i] = 0;
} else if (XParseColor(disp, colormap, c, &xc)) {
// Color parsed successfully
((char *)(xpmColors + i))[0] = xc.blue >> 8;
((char *)(xpmColors + i))[1] = xc.green >> 8;
((char *)(xpmColors + i))[2] = xc.red >> 8;
((char *)(xpmColors + i))[3] = 0xff;
} else {
// Color parsing failed
xpmColors[i] = 0;
}
}
iconBufferSize = 2 + xpmImage.width * xpmImage.height;
iconBuffer = (unsigned long *)g_malloc0( iconBufferSize * sizeof( unsigned long ) );
iconBuffer[0] = (unsigned long)xpmImage.width;
iconBuffer[1] = (unsigned long)xpmImage.height;
for (i = 0; i < xpmImage.width * xpmImage.height; i++) {
iconBuffer[i+2] = (unsigned long)xpmColors[xpmImage.data[i]];
}
XChangeProperty(disp, win, XInternAtom(disp, "_NET_WM_ICON", False), XInternAtom(disp, "CARDINAL", False), 32, PropModeReplace, (const unsigned char*)iconBuffer, iconBufferSize);
g_free(xpmColors);
g_free(iconBuffer);
}/*}}}*/
static int window_to_desktop (Display *disp, Window win, int desktop) {/*{{{*/
unsigned long *cur_desktop = NULL;
Window root = DefaultRootWindow(disp);
if (desktop == -1) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
fputs("Cannot get current desktop properties. "
"(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
"\n", stderr);
return EXIT_FAILURE;
}
}
desktop = *cur_desktop;
}
g_free(cur_desktop);
return client_msg(disp, win, "_NET_WM_DESKTOP", (unsigned long)desktop,
0, 0, 0, 0);
}/*}}}*/
static int activate_window (Display *disp, Window win, /* {{{ */
gboolean switch_desktop) {
unsigned long *desktop;
/* desktop ID */
if ((desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
if ((desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_WIN_WORKSPACE", NULL)) == NULL) {
p_verbose("Cannot find desktop ID of the window.\n");
}
}
if (switch_desktop && desktop) {
if (client_msg(disp, DefaultRootWindow(disp),
"_NET_CURRENT_DESKTOP",
*desktop, 0, 0, 0, 0) != EXIT_SUCCESS) {
p_verbose("Cannot switch desktop.\n");
}
g_free(desktop);
}
client_msg(disp, win, "_NET_ACTIVE_WINDOW",
0, 0, 0, 0, 0);
XMapRaised(disp, win);
return EXIT_SUCCESS;
}/*}}}*/
static int iconify_window (Display *disp, Window win) {/* {{{ */
return !XIconifyWindow(disp, win, DefaultScreen(disp));
}/*}}}*/
static int close_window (Display *disp, Window win) {/*{{{*/
return client_msg(disp, win, "_NET_CLOSE_WINDOW",
0, 0, 0, 0, 0);
}/*}}}*/
static gchar * normalize_wm_state_name(const char * name)
{
char * short_names[] = {
"modal", "sticky", "maximized_vert", "maximized_horz",
"shaded", "skip_taskbar", "skip_pager", "hidden",
"fullscreen", "above", "below", 0};
int i;
for (i = 0; short_names[i]; i++)
{
if (strcmp(short_names[i], name) == 0)
{
gchar * upcase = g_ascii_strup(name, -1);
gchar * result = g_strdup_printf("_NET_WM_STATE_%s", upcase);
g_free(upcase);
return result;
}
}
if (strcmp("undecorated", name) == 0)
{
return g_strdup("_OB_WM_STATE_UNDECORATED");
}
return g_strdup(name);
}
static int window_state (Display *disp, Window win, char *arg) {/*{{{*/
unsigned long action;
Atom prop1 = 0;
Atom prop2 = 0;
char *p1, *p2;
const char *argerr = "The -b option expects a list of comma separated parameters: \"(remove|add|toggle),<PROP1>[,<PROP2>]\"\n";
if (!arg || strlen(arg) == 0) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if ((p1 = strchr(arg, ','))) {
gchar *tmp_prop1;
*p1 = '\0';
/* action */
if (strcmp(arg, "remove") == 0) {
action = _NET_WM_STATE_REMOVE;
}
else if (strcmp(arg, "add") == 0) {
action = _NET_WM_STATE_ADD;
}
else if (strcmp(arg, "toggle") == 0) {
action = _NET_WM_STATE_TOGGLE;
}
else {
fputs("Invalid action. Use either remove, add or toggle.\n", stderr);
return EXIT_FAILURE;
}
p1++;
/* the second property */
if ((p2 = strchr(p1, ','))) {
gchar *tmp_prop2;
*p2 = '\0';
p2++;
if (strlen(p2) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop2 = normalize_wm_state_name(p2);
p_verbose("State 2: %s\n", tmp_prop2);
prop2 = XInternAtom(disp, tmp_prop2, False);
g_free(tmp_prop2);
}
/* the first property */
if (strlen(p1) == 0) {
fputs("Invalid zero length property.\n", stderr);
return EXIT_FAILURE;
}
tmp_prop1 = normalize_wm_state_name(p1);
p_verbose("State 1: %s\n", tmp_prop1);
prop1 = XInternAtom(disp, tmp_prop1, False);
g_free(tmp_prop1);
return client_msg(disp, win, "_NET_WM_STATE",
action, (unsigned long)prop1, (unsigned long)prop2, 0, 0);
}
else {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
}/*}}}*/
static gboolean wm_supports (Display *disp, const gchar *prop) {/*{{{*/
Atom xa_prop = XInternAtom(disp, prop, False);
Atom *list;
unsigned long size;
int i;
if (! (list = (Atom *)get_property(disp, DefaultRootWindow(disp),
XA_ATOM, "_NET_SUPPORTED", &size))) {
p_verbose("Cannot get _NET_SUPPORTED property.\n");
return FALSE;
}
for (i = 0; i < size / sizeof(Atom); i++) {
if (list[i] == xa_prop) {
g_free(list);
return TRUE;
}
}
g_free(list);
return FALSE;
}/*}}}*/
static int window_move_resize (Display *disp, Window win, char *arg) {/*{{{*/
signed long grav, x, y, w, h;
unsigned long grflags;
const char *argerr = "The -e option expects a list of comma separated integers: \"gravity,X,Y,width,height\"\n";
if (!arg || strlen(arg) == 0) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if (sscanf(arg, "%ld,%ld,%ld,%ld,%ld", &grav, &x, &y, &w, &h) != 5) {
fputs(argerr, stderr);
return EXIT_FAILURE;
}
if (grav < 0) {
fputs("Value of gravity mustn't be negative. Use zero to use the default gravity of the window.\n", stderr);
return EXIT_FAILURE;
}
grflags = grav;
if (x != -1) grflags |= (1 << 8);
if (y != -1) grflags |= (1 << 9);
if (w != -1) grflags |= (1 << 10);
if (h != -1) grflags |= (1 << 11);
p_verbose("grflags: %lu\n", grflags);
if (wm_supports(disp, "_NET_MOVERESIZE_WINDOW")){
return client_msg(disp, win, "_NET_MOVERESIZE_WINDOW",
grflags, (unsigned long)x, (unsigned long)y, (unsigned long)w, (unsigned long)h);
}
else {
p_verbose("WM doesn't support _NET_MOVERESIZE_WINDOW. Gravity will be ignored.\n");
if ((w < 1 || h < 1) && (x >= 0 && y >= 0)) {
XMoveWindow(disp, win, x, y);
}
else if ((x < 0 || y < 0) && (w >= 1 && h >= -1)) {
XResizeWindow(disp, win, w, h);
}
else if (x >= 0 && y >= 0 && w >= 1 && h >= 1) {
XMoveResizeWindow(disp, win, x, y, w, h);
}
return EXIT_SUCCESS;
}
}/*}}}*/
static int window_say_title (Display *disp, Window win) {
gchar *title_utf8 = get_window_title(disp, win);
printf("%s\n", title_utf8);
g_free(title_utf8);
return EXIT_SUCCESS;
}
static void display_window (Display *disp, Window win, int max_client_machine_len) {/*{{{*/
gchar *title_utf8 = get_window_title(disp, win); /* UTF8 */
gchar *title_out = get_output_str(title_utf8, TRUE);
gchar *client_machine;
gchar *class_out = get_window_class(disp, win); /* UTF8 */
unsigned long *pid;
unsigned long *desktop;
int x, y, junkx, junky;
unsigned int wwidth, wheight, bw, depth;
Window junkroot;
/* desktop ID */
if ((desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
desktop = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_WIN_WORKSPACE", NULL);
}
/* client machine */
client_machine = get_property(disp, win,
XA_STRING, "WM_CLIENT_MACHINE", NULL);
/* pid */
pid = (unsigned long *)get_property(disp, win,
XA_CARDINAL, "_NET_WM_PID", NULL);
/* geometry */
XGetGeometry (disp, win, &junkroot, &junkx, &junky,
&wwidth, &wheight, &bw, &depth);
XTranslateCoordinates (disp, win, junkroot, junkx, junky,
&x, &y, &junkroot);
/* special desktop ID -1 means "all desktops", so we
have to convert the desktop value to signed long */
printf("0x%.8lx %2ld", win,
desktop ? (signed long)*desktop : 0);
if (options.show_pid) {
printf(" %-6lu", pid ? *pid : 0);
}
if (options.show_geometry) {
printf(" %-4d %-4d %-4d %-4d", x, y, wwidth, wheight);
}
if (options.show_class) {
printf(" %-20s ", class_out ? class_out : "N/A");
}
if (max_client_machine_len == 0) {
max_client_machine_len = strlen(client_machine);
}
printf(" %*s %s\n",
max_client_machine_len,
client_machine ? client_machine : "N/A",
title_out ? title_out : "N/A"
);
g_free(title_utf8);
g_free(title_out);
g_free(desktop);
g_free(client_machine);
g_free(class_out);
g_free(pid);
}/*}}}*/
static int action_window (Display *disp, Window win, char mode) {/*{{{*/
int rv;
p_verbose("Using window: 0x%.8lx\n", win);
switch (mode) {
case 'a':
return activate_window(disp, win, TRUE);
case 'Y':
return iconify_window(disp, win);
case 'c':
return close_window(disp, win);
case 'L':
display_window(disp, win, 0);
return EXIT_SUCCESS;
case 'e':
/* resize/move the window around the desktop => -r -e */
return window_move_resize(disp, win, options.param);
case 'y':
/* resize/move the window, then activate it */
rv = window_move_resize(disp, win, options.param);
activate_window(disp, win, TRUE);
return rv;
case 'b':
/* change state of a window => -r -b */
return window_state(disp, win, options.param);
case 't':
/* move the window to the specified desktop => -r -t */
return window_to_desktop(disp, win, atoi(options.param));
case 'R':
/* move the window to the current desktop and activate it => -r */
if (window_to_desktop(disp, win, -1) == EXIT_SUCCESS) {
usleep(100000); /* 100 ms - make sure the WM has enough
time to move the window, before we activate it */
return activate_window(disp, win, FALSE);
}
else {
return EXIT_FAILURE;
}
case 'N': case 'I': case 'T':
window_set_title(disp, win, options.param, mode);
return EXIT_SUCCESS;
case 'z':
// iconify
return XLowerWindow(disp, win);
case 'E':
return window_say_title(disp, win);
case 'M':
window_set_mini_icon(disp, win, options.param, mode);
return EXIT_SUCCESS;
default:
fprintf(stderr, "Unknown action: '%c'\n", mode);
return EXIT_FAILURE;
}
}/*}}}*/
static int action_window_pid (Display *disp, char mode) {/*{{{*/
unsigned long wid;
if (sscanf(options.param_window, "0x%lx", &wid) != 1 &&
sscanf(options.param_window, "0X%lx", &wid) != 1 &&
sscanf(options.param_window, "%lu", &wid) != 1) {
fputs("Cannot convert argument to number.\n", stderr);
return EXIT_FAILURE;
}
return action_window(disp, (Window)wid, mode);
}/*}}}*/
static int action_window_str (Display *disp, char mode) {/*{{{*/
Window activate = 0;
Window *client_list;
unsigned long client_list_size;
int i;
if (strcmp(SELECT_WINDOW_MAGIC, options.param_window) == 0) {
activate = Select_Window(disp);
if (activate) {
return action_window(disp, activate, mode);
}
else {
return EXIT_FAILURE;
}
}
if (strcmp(ACTIVE_WINDOW_MAGIC, options.param_window) == 0) {
activate = get_active_window(disp);
if (activate)
{
return action_window(disp, activate, mode);
}
else
{
return EXIT_FAILURE;
}
}
else {
if ((client_list = get_client_list(disp, &client_list_size)) == NULL) {
return EXIT_FAILURE;
}
for (i = 0; i < client_list_size / sizeof(Window); i++) {
gchar *match_utf8;
if (options.show_class) {
match_utf8 = get_window_class(disp, client_list[i]); /* UTF8 */
}
else {
match_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */
}
if (match_utf8) {
gchar *match;
gchar *match_cf;
gchar *match_utf8_cf = NULL;
if (envir_utf8) {
match = g_strdup(options.param_window);
match_cf = g_utf8_casefold(options.param_window, -1);
}
else {
if (! (match = g_locale_to_utf8(options.param_window, -1, NULL, NULL, NULL))) {
match = g_strdup(options.param_window);
}
match_cf = g_utf8_casefold(match, -1);
}
if (!match || !match_cf) {
continue;
}
match_utf8_cf = g_utf8_casefold(match_utf8, -1);
if ((options.full_window_title_match && strcmp(match_utf8, match) == 0) ||
(!options.full_window_title_match && strstr(match_utf8_cf, match_cf))) {
activate = client_list[i];
g_free(match);
g_free(match_cf);
g_free(match_utf8);
g_free(match_utf8_cf);
break;
}
g_free(match);
g_free(match_cf);
g_free(match_utf8);
g_free(match_utf8_cf);
}
}
g_free(client_list);
if (activate) {
return action_window(disp, activate, mode);
}
else {
return EXIT_FAILURE;
}
}
}/*}}}*/
static int list_current_desktop (Display *disp) {/*{{{*/
unsigned long *cur_desktop = NULL;
Window root = DefaultRootWindow(disp);
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
fputs("Cannot get current desktop properties. "
"(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
"\n", stderr);
g_free(cur_desktop);
return EXIT_FAILURE;
}
}
printf("%-2d\n", *((int *) cur_desktop));
return EXIT_SUCCESS;
}
static int list_desktops (Display *disp) {/*{{{*/
unsigned long *num_desktops = NULL;
unsigned long *cur_desktop = NULL;
unsigned long desktop_list_size = 0;
unsigned long *desktop_geometry = NULL;
unsigned long desktop_geometry_size = 0;
gchar **desktop_geometry_str = NULL;
unsigned long *desktop_viewport = NULL;
unsigned long desktop_viewport_size = 0;
gchar **desktop_viewport_str = NULL;
unsigned long *desktop_workarea = NULL;
unsigned long desktop_workarea_size = 0;
gchar **desktop_workarea_str = NULL;
gchar *list = NULL;
int i;
int id;
Window root = DefaultRootWindow(disp);
int ret = EXIT_FAILURE;
gchar **names = NULL;
gboolean names_are_utf8 = TRUE;
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_NUMBER_OF_DESKTOPS", NULL))) {
if (! (num_desktops = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE_COUNT", NULL))) {
fputs("Cannot get number of desktops properties. "
"(_NET_NUMBER_OF_DESKTOPS or _WIN_WORKSPACE_COUNT)"
"\n", stderr);
goto cleanup;
}
}
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_NET_CURRENT_DESKTOP", NULL))) {
if (! (cur_desktop = (unsigned long *)get_property(disp, root,
XA_CARDINAL, "_WIN_WORKSPACE", NULL))) {
fputs("Cannot get current desktop properties. "
"(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
"\n", stderr);
goto cleanup;
}
}
if (options.wa_desktop_titles_invalid_utf8 ||
(list = get_property(disp, root,
XInternAtom(disp, "UTF8_STRING", False),
"_NET_DESKTOP_NAMES", &desktop_list_size)) == NULL) {
names_are_utf8 = FALSE;
if ((list = get_property(disp, root,
XA_STRING,
"_WIN_WORKSPACE_NAMES", &desktop_list_size)) == NULL) {
p_verbose("Cannot get desktop names properties. "
"(_NET_DESKTOP_NAMES or _WIN_WORKSPACE_NAMES)"
"\n");
/* ignore the error - list the desktops without names */
}
}
/* common size of all desktops */
if (! (desktop_geometry = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_GEOMETRY", &desktop_geometry_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_GEOMETRY).\n");
}
/* desktop viewport */
if (! (desktop_viewport = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_DESKTOP_VIEWPORT", &desktop_viewport_size))) {
p_verbose("Cannot get common size of all desktops (_NET_DESKTOP_VIEWPORT).\n");
}
/* desktop workarea */
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_NET_WORKAREA", &desktop_workarea_size))) {
if (! (desktop_workarea = (unsigned long *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_WORKAREA", &desktop_workarea_size))) {
p_verbose("Cannot get _NET_WORKAREA property.\n");
}
}
/* prepare the array of desktop names */
names = g_malloc0(*num_desktops * sizeof(char *));
if (list) {
id = 0;
names[id++] = list;
for (i = 0; i < desktop_list_size; i++) {
if (list[i] == '\0') {
if (id >= *num_desktops) {
break;
}
names[id++] = list + i + 1;
}
}
}
/* prepare desktop geometry strings */
desktop_geometry_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_geometry && desktop_geometry_size > 0) {
if (desktop_geometry_size == 2 * sizeof(*desktop_geometry)) {
/* only one value - use it for all desktops */
p_verbose("WM provides _NET_DESKTOP_GEOMETRY value common for all desktops.\n");
for (i = 0; i < *num_desktops; i++) {
desktop_geometry_str[i] = g_strdup_printf("%lux%lu",
desktop_geometry[0], desktop_geometry[1]);
}
}
else {
/* seperate values for desktops of different size */
p_verbose("WM provides separate _NET_DESKTOP_GEOMETRY value for each desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_geometry_size / sizeof(*desktop_geometry) / 2) {
desktop_geometry_str[i] = g_strdup_printf("%lux%lu",
desktop_geometry[i*2], desktop_geometry[i*2+1]);
}
else {
desktop_geometry_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_geometry_str[i] = g_strdup("N/A");
}
}
/* prepare desktop viewport strings */
desktop_viewport_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_viewport && desktop_viewport_size > 0) {
if (desktop_viewport_size == 2 * sizeof(*desktop_viewport)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_DESKTOP_VIEWPORT value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
desktop_viewport_str[i] = g_strdup_printf("%lu,%lu",
desktop_viewport[0], desktop_viewport[1]);
}
else {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_viewport_size / sizeof(*desktop_viewport) / 2) {
desktop_viewport_str[i] = g_strdup_printf("%lu,%lu",
desktop_viewport[i*2], desktop_viewport[i*2+1]);
}
else {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_viewport_str[i] = g_strdup("N/A");
}
}
/* prepare desktop workarea strings */
desktop_workarea_str = g_malloc0((*num_desktops + 1) * sizeof(char *));
if (desktop_workarea && desktop_workarea_size > 0) {
if (desktop_workarea_size == 4 * sizeof(*desktop_workarea)) {
/* only one value - use it for current desktop */
p_verbose("WM provides _NET_WORKAREA value only for the current desktop.\n");
for (i = 0; i < *num_desktops; i++) {
if (i == *cur_desktop) {
desktop_workarea_str[i] = g_strdup_printf("%lu,%lu %lux%lu",
desktop_workarea[0], desktop_workarea[1],
desktop_workarea[2], desktop_workarea[3]);
}
else {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
}
else {
/* seperate values for each of desktops */
for (i = 0; i < *num_desktops; i++) {
if (i < desktop_workarea_size / sizeof(*desktop_workarea) / 4) {
desktop_workarea_str[i] = g_strdup_printf("%lu,%lu %lux%lu",
desktop_workarea[i*4], desktop_workarea[i*4+1],
desktop_workarea[i*4+2], desktop_workarea[i*4+3]);
}
else {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
}
}
else {
for (i = 0; i < *num_desktops; i++) {
desktop_workarea_str[i] = g_strdup("N/A");
}
}
/* print the list */
for (i = 0; i < *num_desktops; i++) {
gchar *out = get_output_str(names[i], names_are_utf8);
printf("%-2d %c DG: %-*s VP: %-*s WA: %-*s %s\n", i, i == *cur_desktop ? '*' : '-',
longest_str(desktop_geometry_str), desktop_geometry_str[i],
longest_str(desktop_viewport_str), desktop_viewport_str[i],
longest_str(desktop_workarea_str), desktop_workarea_str[i],
out ? out : "N/A");
g_free(out);
}
p_verbose("Total number of desktops: %lu\n", *num_desktops);
p_verbose("Current desktop ID (counted from zero): %lu\n", *cur_desktop);
ret = EXIT_SUCCESS;
goto cleanup;
cleanup:
g_free(names);
g_free(num_desktops);
g_free(cur_desktop);
g_free(desktop_geometry);
g_strfreev(desktop_geometry_str);
g_free(desktop_viewport);
g_strfreev(desktop_viewport_str);
g_free(desktop_workarea);
g_strfreev(desktop_workarea_str);
g_free(list);
return ret;
}/*}}}*/
static int longest_str (gchar **strv) {/*{{{*/
int max = 0;
int i = 0;
while (strv && strv[i]) {
if (strlen(strv[i]) > max) {
max = strlen(strv[i]);
}
i++;
}
return max;
}/*}}}*/
static Window *get_client_list (Display *disp, unsigned long *size) {/*{{{*/
Window *client_list = NULL;
char * msg = NULL;
if (options.stacking_order)
{
msg = "_NET_CLIENT_LIST_STACKING";
client_list = (Window *) get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_CLIENT_LIST_STACKING", size);
}
else
{
msg = "_NET_CLIENT_LIST or _WIN_CLIENT_LIST";
client_list = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_WINDOW, "_NET_CLIENT_LIST", size);
if (!client_list)
client_list = (Window *)get_property(disp, DefaultRootWindow(disp),
XA_CARDINAL, "_WIN_CLIENT_LIST", size);
}
if (!client_list)
fprintf(stderr, "Cannot get client list properties.\n(%s)\n", msg);
return client_list;
}/*}}}*/
static int list_windows (Display *disp) {/*{{{*/
Window *client_list;
unsigned long client_list_size;
int i;
int max_client_machine_len = 0;
if ((client_list = get_client_list(disp, &client_list_size)) == NULL) {
return EXIT_FAILURE;
}
/* find the longest client_machine name */
for (i = 0; i < client_list_size / sizeof(Window); i++) {
gchar *client_machine;
if ((client_machine = get_property(disp, client_list[i],
XA_STRING, "WM_CLIENT_MACHINE", NULL))) {
max_client_machine_len = strlen(client_machine);
}
g_free(client_machine);
}
/* print the list */
for (i = 0; i < client_list_size / sizeof(Window); i++) {
display_window(disp, client_list[i], max_client_machine_len);
}
g_free(client_list);
return EXIT_SUCCESS;
}/*}}}*/
static gchar *get_window_class (Display *disp, Window win) {/*{{{*/
gchar *class_utf8;
gchar *wm_class;
unsigned long size;
wm_class = get_property(disp, win, XA_STRING, "WM_CLASS", &size);
if (wm_class) {
gchar *p_0 = strchr(wm_class, '\0');
if (wm_class + size - 1 > p_0) {
*(p_0) = '.';
}
class_utf8 = g_locale_to_utf8(wm_class, -1, NULL, NULL, NULL);
}
else {
class_utf8 = NULL;
}
g_free(wm_class);
return class_utf8;
}/*}}}*/
static gchar *get_window_title (Display *disp, Window win) {/*{{{*/
gchar *title_utf8;
gchar *wm_name;
gchar *net_wm_name;
wm_name = get_property(disp, win, XA_STRING, "WM_NAME", NULL);
net_wm_name = get_property(disp, win,
XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL);
if (net_wm_name) {
title_utf8 = g_strdup(net_wm_name);
}
else {
if (wm_name) {
title_utf8 = g_locale_to_utf8(wm_name, -1, NULL, NULL, NULL);
}
else {
title_utf8 = NULL;
}
}
g_free(wm_name);
g_free(net_wm_name);
return title_utf8;
}/*}}}*/
static gchar *get_property (Display *disp, Window win, /*{{{*/
Atom xa_prop_type, gchar *prop_name, unsigned long *size) {
Atom xa_prop_name;
Atom xa_ret_type;
int ret_format;
unsigned long ret_nitems;
unsigned long ret_bytes_after;
unsigned long tmp_size;
unsigned char *ret_prop;
gchar *ret;
xa_prop_name = XInternAtom(disp, prop_name, False);
/* MAX_PROPERTY_VALUE_LEN / 4 explanation (XGetWindowProperty manpage):
*
* long_length = Specifies the length in 32-bit multiples of the
* data to be retrieved.
*
* NOTE: see
* http://mail.gnome.org/archives/wm-spec-list/2003-March/msg00067.html
* In particular:
*
* When the X window system was ported to 64-bit architectures, a
* rather peculiar design decision was made. 32-bit quantities such
* as Window IDs, atoms, etc, were kept as longs in the client side
* APIs, even when long was changed to 64 bits.
*
*/
if (XGetWindowProperty(disp, win, xa_prop_name, 0, MAX_PROPERTY_VALUE_LEN / 4, False,
xa_prop_type, &xa_ret_type, &ret_format,
&ret_nitems, &ret_bytes_after, &ret_prop) != Success) {
p_verbose("Cannot get %s property.\n", prop_name);
return NULL;
}
if (xa_ret_type != xa_prop_type) {
p_verbose("Invalid type of %s property.\n", prop_name);
XFree(ret_prop);
return NULL;
}
/* null terminate the result to make string handling easier */
tmp_size = (ret_format / 8) * ret_nitems;
/* Correct 64 Architecture implementation of 32 bit data */
if(ret_format==32) tmp_size *= sizeof(long)/4;
ret = g_malloc(tmp_size + 1);
memcpy(ret, ret_prop, tmp_size);
ret[tmp_size] = '\0';
if (size) {
*size = tmp_size;
}
XFree(ret_prop);
return ret;
}/*}}}*/
static Window Select_Window(Display *dpy) {/*{{{*/
/*
* Routine to let user select a window using the mouse
* Taken from xfree86.
*/
int status;
Cursor cursor;
XEvent event;
Window target_win = None, root = DefaultRootWindow(dpy);
int buttons = 0;
int dummyi;
unsigned int dummy;
/* Make the target cursor */
cursor = XCreateFontCursor(dpy, XC_crosshair);
/* Grab the pointer using target cursor, letting it room all over */
status = XGrabPointer(dpy, root, False,
ButtonPressMask|ButtonReleaseMask, GrabModeSync,
GrabModeAsync, root, cursor, CurrentTime);
if (status != GrabSuccess) {
fputs("ERROR: Cannot grab mouse.\n", stderr);
return 0;
}
/* Let the user select a window... */
while ((target_win == None) || (buttons != 0)) {
/* allow one more event */
XAllowEvents(dpy, SyncPointer, CurrentTime);
XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
switch (event.type) {
case ButtonPress:
if (target_win == None) {
target_win = event.xbutton.subwindow; /* window selected */
if (target_win == None) target_win = root;
}
buttons++;
break;
case ButtonRelease:
if (buttons > 0) /* there may have been some down before we started */
buttons--;
break;
}
}
XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi,
&dummy, &dummy, &dummy, &dummy) && target_win != root) {
target_win = XmuClientWindow (dpy, target_win);
}
return(target_win);
}/*}}}*/
static Window get_active_window(Display *disp) {/*{{{*/
char *prop;
unsigned long size;
Window ret = (Window)0;
prop = get_property(disp, DefaultRootWindow(disp), XA_WINDOW,
"_NET_ACTIVE_WINDOW", &size);
if (prop) {
ret = *((Window*)prop);
g_free(prop);
}
return(ret);
}/*}}}*/
|