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
|
/*
* IceSH - A command line window manager
* Copyright (C) 2001 Mathias Hasselmann
*
* Based on Marks testwinhints.cc.
* Inspired by MJ Ray's WindowC
*
* Release under terms of the GNU Library General Public License
*
* 2001/07/18: Mathias Hasselmann <mathias.hasselmann@gmx.net>
* - initial version
*/
#include "config.h"
#include "intl.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#ifdef CONFIG_I18N
#include <locale.h>
#endif
#include "base.h"
#include "WinMgr.h"
#if 1
#define THROW(Result) { rc = (Result); goto exceptionHandler; }
#define TRY(Command) { if ((rc = (Command))) THROW(rc); }
#define CATCH(Handler) { exceptionHandler: { Handler } return rc; }
#endif
/******************************************************************************/
/******************************************************************************/
char const * ApplicationName(NULL);
Display *display(NULL);
Window root;
/******************************************************************************/
/******************************************************************************/
struct Symbol {
char const * name;
long code;
};
struct SymbolTable {
long parseIdentifier(char const * identifier, size_t const len) const;
long parseIdentifier(char const * identifier) const {
return parseIdentifier(identifier, strlen(identifier));
}
long parseExpression(char const * expression) const;
void listSymbols(char const * label) const;
bool valid(long code) const { return code != fErrCode; }
bool invalid(long code) const { return code == fErrCode; }
Symbol const * fSymbols;
long fMin, fMax, fErrCode;
};
class YWindowProperty {
public:
YWindowProperty(Window window, Atom property, Atom type = AnyPropertyType,
long length = 0, long offset = 0, Bool deleteProp = False):
fType(None), fFormat(0), fCount(0), fAfter(0), fData(NULL),
fStatus(XGetWindowProperty(display, window, property,
offset, length, deleteProp, type,
&fType, &fFormat, &fCount, &fAfter,
&fData)) {
}
virtual ~YWindowProperty() {
if (NULL != fData) XFree(fData);
}
Atom type() const { return fType; }
int format() const { return fFormat; }
long count() const { return fCount; }
unsigned long after() const { return fAfter; }
template <class T>
T data(unsigned index) const { return ((T *) fData)[index]; }
operator int() const { return fStatus; }
private:
Atom fType;
int fFormat;
unsigned long fCount, fAfter;
unsigned char * fData;
int fStatus;
};
class YTextProperty {
public:
YTextProperty(Window window, Atom property):
fList(NULL), fCount(0),
fStatus(XGetTextProperty(display, window, &fProperty, property)
? Success : BadValue) {
}
virtual ~YTextProperty() {
if (NULL != fList)
XFreeStringList(fList);
}
char * item(unsigned index);
char ** list();
int count();
operator int() const { return fStatus; }
private:
void allocateList();
XTextProperty fProperty;
char ** fList;
int fCount, fStatus;
};
char * YTextProperty::item(unsigned index) {
return list()[index];
}
char ** YTextProperty::list() {
allocateList();
return fList;
}
int YTextProperty::count() {
allocateList();
return fCount;
}
void YTextProperty::allocateList() {
if (NULL == fList)
XTextPropertyToStringList(&fProperty, &fList, &fCount);
}
class YWindowTreeNode {
public:
YWindowTreeNode(Window window):
fRoot(None), fParent(None), fChildren(NULL), fCount(0),
fSuccess(XQueryTree(display, window, &fRoot, &fParent,
&fChildren, &fCount)) {
}
virtual ~YWindowTreeNode() {
if (NULL != fChildren) XFree(fChildren);
}
operator bool() { return fSuccess; }
private:
Window fRoot, fParent, * fChildren;
unsigned fCount;
bool fSuccess;
};
/******************************************************************************/
Atom ATOM_WM_STATE;
Atom ATOM_WIN_WORKSPACE;
Atom ATOM_WIN_WORKSPACE_NAMES;
Atom ATOM_WIN_WORKSPACE_COUNT;
Atom ATOM_WIN_STATE;
Atom ATOM_WIN_HINTS;
Atom ATOM_WIN_LAYER;
Atom ATOM_WIN_TRAY;
/******************************************************************************/
/******************************************************************************/
#define CHECK_ARGUMENT_COUNT(Count) { \
if ((argv + argc - argp) < (Count)) { \
msg(_("Action `%s' requires at least %d arguments."), action, Count); \
THROW(1); \
} \
}
#define CHECK_EXPRESSION(SymTab, Code, Str) { \
if ((SymTab).invalid(Code)) { \
msg(_("Invalid expression: `%s'"), Str); \
THROW(1); \
} \
}
#define FOREACH_WINDOW(Var) \
for (Window *Var = windowList; Var < windowList + windowCount; ++Var)
/******************************************************************************/
Symbol stateIdentifiers[] = {
{ "AllWorkspaces", WinStateAllWorkspaces },
{ "Sticky", WinStateAllWorkspaces },
{ "Minimized", WinStateMinimized },
{ "Maximized", WinStateMaximizedVert |
WinStateMaximizedHoriz },
{ "MaximizedVert", WinStateMaximizedVert },
{ "MaximizedVertical", WinStateMaximizedVert },
{ "MaximizedHoriz", WinStateMaximizedHoriz },
{ "MaximizedHorizontal", WinStateMaximizedHoriz },
{ "Hidden", WinStateHidden },
{ "All", WIN_STATE_ALL },
{ NULL, 0 }
};
Symbol hintIdentifiers[] = {
{ "SkipFocus", WinHintsSkipFocus },
{ "SkipWindowMenu", WinHintsSkipWindowMenu },
{ "SkipTaskBar", WinHintsSkipTaskBar },
{ "FocusOnClick", WinHintsFocusOnClick },
{ "DoNotCover", WinHintsDoNotCover },
{ "All", WIN_HINTS_ALL },
{ NULL, 0 }
};
Symbol layerIdentifiers[] = {
{ "Desktop", WinLayerDesktop },
{ "Below", WinLayerBelow },
{ "Normal", WinLayerNormal },
{ "OnTop", WinLayerOnTop },
{ "Dock", WinLayerDock },
{ "AboveDock", WinLayerAboveDock },
{ "Menu", WinLayerMenu },
{ NULL, 0 }
};
Symbol trayOptionIdentifiers[] = {
{ "Ignore", WinTrayIgnore },
{ "Minimized", WinTrayMinimized },
{ "Exclusive", WinTrayExclusive },
{ NULL, 0 }
};
SymbolTable layers = {
layerIdentifiers, 0, WinLayerCount - 1, WinLayerInvalid
};
SymbolTable states = {
stateIdentifiers, 0, WIN_STATE_ALL, -1
};
SymbolTable hints = {
hintIdentifiers, 0, WIN_HINTS_ALL, -1
};
SymbolTable trayOptions = {
trayOptionIdentifiers, 0, WinTrayOptionCount - 1, WinTrayInvalid
};
/******************************************************************************/
long SymbolTable::parseIdentifier(char const * id, size_t const len) const {
for (Symbol const * sym(fSymbols); NULL != sym && NULL != sym->name; ++sym)
if (!(sym->name[len] || strncasecmp(sym->name, id, len)))
return sym->code;
char *endptr;
long value(strtol(id, &endptr, 0));
return (NULL != endptr && '\0' == *endptr &&
value >= fMin && value <= fMax ? value : fErrCode);
}
#if 0
static char const * strnxt(const char * str, const char * delim) {
str+= strcspn(str, delim);
str+= strspn(str, delim);
return str;
}
#endif
/*
* Counts the tokens separated by delim
*/
unsigned strtoken(const char * str, const char * delim) {
unsigned count = 0;
if (str) {
while (*str) {
str = strnxt(str, delim);
++count;
}
}
return count;
}
long SymbolTable::parseExpression(char const * expression) const {
long value(0);
for (char const * token(expression);
*token != '\0' && value != fErrCode; token = strnxt(token, "+|"))
{
char const * id(token + strspn(token, " \t"));
value|= parseIdentifier(id = newstr(id, "+| \t"));
delete[] id;
}
return value;
}
void SymbolTable::listSymbols(char const * label) const {
printf(_("Named symbols of the domain `%s' (numeric range: %ld-%ld):\n"),
label, fMin, fMax);
for (Symbol const * sym(fSymbols); NULL != sym && NULL != sym->name; ++sym)
printf(" %-20s (%ld)\n", sym->name, sym->code);
puts("");
}
/******************************************************************************/
Status getState(Window window, long & mask, long & state) {
YWindowProperty winState(window, ATOM_WIN_STATE, XA_CARDINAL, 2);
if (Success == winState && XA_CARDINAL == winState.type() &&
32 == winState.format() && 1L <= winState.count()) {
state = winState.data<long>(0);
mask = winState.count() >= 2L
? winState.data<long>(1)
: WIN_STATE_ALL;
return winState;
}
return BadValue;
}
Status setState(Window window, long mask, long state) {
XClientMessageEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.window = window;
xev.message_type = ATOM_WIN_STATE;
xev.format = 32;
xev.data.l[0] = mask;
xev.data.l[1] = state;
xev.data.l[2] = CurrentTime;
return XSendEvent(display, root, False, SubstructureNotifyMask, (XEvent *) &xev);
}
Status toggleState(Window window, long newState) {
long mask, state;
if (Success != getState(window, mask, state))
state = mask = 0;
MSG(("old mask/state: %d/%d", mask, state));
XClientMessageEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.window = window;
xev.message_type = ATOM_WIN_STATE;
xev.format = 32;
xev.data.l[0] = newState;
xev.data.l[1] = (state & mask & newState) ^ newState;
xev.data.l[2] = CurrentTime;
MSG(("new mask/state: %d/%d", xev.data.l[0], xev.data.l[1]));
return XSendEvent(display, root, False, SubstructureNotifyMask, (XEvent *) &xev);
}
/******************************************************************************/
Status setHints(Window window, long hints) {
return XChangeProperty(display, window, ATOM_WIN_HINTS, XA_CARDINAL, 32,
PropModeReplace, (unsigned char *) &hints, 1);
}
/******************************************************************************/
struct WorkspaceInfo {
WorkspaceInfo(Window root):
fCount(root, ATOM_WIN_WORKSPACE_COUNT, XA_CARDINAL, 1),
fNames(root, ATOM_WIN_WORKSPACE_NAMES)
{
if (fCount == Success)
fStatus = fNames;
else
fStatus = fCount;
}
int parseWorkspaceName(char const * name);
long count();
operator int() const { return fStatus; }
YWindowProperty fCount;
YTextProperty fNames;
int fStatus;
};
long WorkspaceInfo::count() {
return (Success == fCount ? fCount.data<long>(0) : 0);
}
int WorkspaceInfo::parseWorkspaceName(char const * name) {
long workspace(WinWorkspaceInvalid);
if (Success == fStatus) {
for (int n(0); n < fNames.count() &&
WinWorkspaceInvalid == workspace; ++n)
if (!strcmp(name, fNames.item(n))) workspace = n;
if (WinWorkspaceInvalid == workspace) {
char *endptr;
workspace = strtol(name, &endptr, 0);
if (NULL == endptr || '\0' != *endptr) {
msg(_("Invalid workspace name: `%s'"), name);
return WinWorkspaceInvalid;
}
}
if (workspace > count()) {
msg(_("Workspace out of range: %d"), workspace);
return WinWorkspaceInvalid;
}
}
return workspace;
}
Status setWorkspace(Window window, long workspace) {
XClientMessageEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.window = window;
xev.message_type = ATOM_WIN_WORKSPACE;
xev.format = 32;
xev.data.l[0] = workspace;
xev.data.l[1] = CurrentTime;
return XSendEvent(display, root, False, SubstructureNotifyMask, (XEvent *) &xev);
}
/******************************************************************************/
Status setLayer(Window window, long layer) {
XClientMessageEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.window = window;
xev.message_type = ATOM_WIN_LAYER;
xev.format = 32;
xev.data.l[0] = layer;
xev.data.l[1] = CurrentTime;
return XSendEvent(display, root, False, SubstructureNotifyMask, (XEvent *) &xev);
}
Status setTrayHint(Window window, long trayopt) {
XClientMessageEvent xev;
memset(&xev, 0, sizeof(xev));
xev.type = ClientMessage;
xev.window = window;
xev.message_type = ATOM_WIN_TRAY;
xev.format = 32;
xev.data.l[0] = trayopt;
xev.data.l[1] = CurrentTime;
return XSendEvent(display, root, False, SubstructureNotifyMask, (XEvent *) &xev);
}
/******************************************************************************/
Window getClientWindow(Window window)
{
if (None != YWindowProperty(window, ATOM_WM_STATE).type())
return window;
Window root, parent;
unsigned nchildren;
Window *children;
unsigned int i;
if (!XQueryTree (display, window, &root, &parent, &children, &nchildren)) {
warn("XQueryTree failed for window 0x%x", window);
return None;
}
Window client(None);
for (i = 0; client == None && i < nchildren; ++i)
if (None != YWindowProperty(children[i], ATOM_WM_STATE).type())
client = children [i];
for (i = 0; client == None && i < nchildren; ++i)
client = getClientWindow(children [i]);
XFree(children);
return client;
}
Window pickWindow (void) {
Cursor cursor;
bool running(true);
Window target(None);
int count(0);
unsigned escape;
cursor = XCreateFontCursor(display, XC_crosshair);
escape = XKeysymToKeycode(display, XK_Escape);
// this is broken
XGrabKey(display, escape, 0, root, False, GrabModeAsync, GrabModeAsync);
XGrabPointer(display, root, False, ButtonPressMask|ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, root, cursor, CurrentTime);
do {
XEvent event;
XNextEvent (display, &event);
switch (event.type) {
case KeyPress:
case KeyRelease:
if (event.xkey.keycode == escape) running = false;
break;
case ButtonPress:
++count;
if (target == None)
target = event.xbutton.subwindow == None
? event.xbutton.window
: event.xbutton.subwindow;
break;
case ButtonRelease:
--count;
break;
}
} while (running && (None == target || 0 != count));
XUngrabPointer(display, CurrentTime);
// and this is broken
XUngrabKey(display, escape, 0, root);
return (None == target || root == target ? target
: getClientWindow(target));
}
/******************************************************************************/
/******************************************************************************/
static void printUsage() {
printf(_("\
Usage: %s [OPTIONS] ACTIONS\n\
\n\
Options:\n\
-display DISPLAY Connects to the X server specified by DISPLAY.\n\
Default: $DISPLAY or :0.0 when not set.\n\
-window WINDOW_ID Specifies the window to manipulate. Special\n\
identifiers are `root' for the root window and\n\
`focus' for the currently focused window.\n\
-class WM_CLASS Window management class of the window(s) to\n\
manipulate. If WM_CLASS contains a period, only\n\
windows with exactly the same WM_CLASS property\n\
are matched. If there is no period, windows of\n\
the same class and windows of the same instance\n\
(aka. `-name') are selected.\n\
\n\
Actions:\n\
setIconTitle TITLE Set the icon title.\n\
setWindowTitle TITLE Set the window title.\n\
setGeometry geometry Set the window geometry\n\
setState MASK STATE Set the GNOME window state to STATE.\n\
Only the bits selected by MASK are affected.\n\
STATE and MASK are expressions of the domain\n\
`GNOME window state'.\n\
toggleState STATE Toggle the GNOME window state bits specified by\n\
the STATE expression.\n\
setHints HINTS Set the GNOME window hints to HINTS.\n\
setLayer LAYER Moves the window to another GNOME window layer.\n\
setWorkspace WORKSPACE Moves the window to another workspace. Select\n\
the root window to change the current workspace.\n\
listWorkspaces Lists the names of all workspaces.\n\
setTrayOption TRAYOPTION Set the IceWM tray option hint.\n\
\n\
Expressions:\n\
Expressions are list of symbols of one domain concatenated by `+' or `|':\n\
\n\
EXPRESSION ::= SYMBOL | EXPRESSION ( `+' | `|' ) SYMBOL\n\n"),
ApplicationName);
states.listSymbols(_("GNOME window state"));
hints.listSymbols(_("GNOME window hint"));
layers.listSymbols(_("GNOME window layer"));
trayOptions.listSymbols(_("IceWM tray option"));
}
static void usageError(char const *msg, ...) {
fprintf(stderr, "%s: ", ApplicationName);
fputs(_("Usage error: "), stderr);
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fputs("\n\n", stderr);
printUsage();
}
/******************************************************************************/
/******************************************************************************/
int main(int argc, char **argv) {
#ifdef CONFIG_I18N
setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
bindtextdomain(PACKAGE, LOCDIR);
textdomain(PACKAGE);
#endif
#endif
ApplicationName = my_basename(*argv);
char const *dpyname(NULL);
char const *winname(NULL);
char *wmclass(NULL);
char *wmname(NULL);
int rc(0);
Window singleWindowList[] = { None };
Window *windowList(NULL);
unsigned windowCount(0);
char **argp(argv + 1);
for (char *arg; argp < argv + argc && '-' == *(arg = *argp); ++argp) {
if (arg[1] == '-') ++arg;
size_t sep(strcspn(arg, "=:"));
char *val(arg[sep] ? arg + sep + 1 : *++argp);
if (!(strpcmp(arg, "-display") || val == NULL)) {
dpyname = val;
} else if (!(strpcmp(arg, "-window") || val == NULL)) {
winname = val;
} else if (!(strpcmp(arg, "-class") || val == NULL)) {
wmname = val;
char *p = val;
char *d = val;
while (*p) {
if (*p == '\\') {
p++;
if (*p == '\0')
break;
} else if (*p == '.') {
*d++ = 0;
wmclass = d;
p++;
continue;
}
*d++ = *p++;
}
*d++ = 0;
MSG(("wmname: `%s'; wmclass: `%s'", wmname, wmclass));
#ifdef DEBUG
} else if (!(strpcmp(arg, "-debug"))) {
debug = 1;
--argp;
#endif
} else if (strcmp(arg, "-?") && strcmp(arg, "-help")) {
usageError (_("Invalid argument: `%s'."), arg);
THROW(1);
} else {
printUsage();
THROW(0);
}
}
if (argp >= argv + argc) {
usageError(_("No actions specified."));
THROW(1);
}
/******************************************************************************/
if (NULL == (display = XOpenDisplay(dpyname))) {
warn(_("Can't open display: %s. X must be running and $DISPLAY set."),
XDisplayName(dpyname));
THROW(3);
}
root = RootWindow(display, DefaultScreen(display));
ATOM_WM_STATE = XInternAtom(display, "WM_STATE", False);
ATOM_WIN_WORKSPACE = XInternAtom(display, XA_WIN_WORKSPACE, False);
ATOM_WIN_WORKSPACE_NAMES = XInternAtom(display, XA_WIN_WORKSPACE_NAMES, False);
ATOM_WIN_WORKSPACE_COUNT = XInternAtom(display, XA_WIN_WORKSPACE_COUNT, False);
ATOM_WIN_STATE = XInternAtom(display, XA_WIN_STATE, False);
ATOM_WIN_HINTS = XInternAtom(display, XA_WIN_HINTS, False);
ATOM_WIN_LAYER = XInternAtom(display, XA_WIN_LAYER, False);
ATOM_WIN_TRAY = XInternAtom(display, XA_WIN_TRAY, False);
/******************************************************************************/
if (winname) {
if (!strcmp(winname, "root")) {
*(windowList = singleWindowList) = root;
windowCount = 1;
MSG(("root window selected"));
} else if (!strcmp(winname, "focus")) {
int dummy;
windowList = singleWindowList;
windowCount = 1;
XGetInputFocus(display, windowList, &dummy);
MSG(("focused window selected"));
} else {
char *eptr;
*(windowList = singleWindowList) = strtol(winname, &eptr, 0);
windowCount = 1;
if (NULL == eptr || '\0' != *eptr) {
msg(_("Invalid window identifier: `%s'"), winname);
THROW(1);
}
MSG(("focused window selected"));
}
} else {
if (NULL == wmname) {
*(windowList = singleWindowList) = pickWindow();
windowCount = 1;
MSG(("window picked"));
} else {
Window dummy;
XQueryTree(display, root, &dummy, &dummy,
&windowList, &windowCount);
MSG(("window tree fetched, got %d window handles", windowCount));
}
}
if (wmname) {
unsigned matchingWindowCount = 0;
for (unsigned i = 0; i < windowCount; ++i) {
XClassHint classhint;
windowList[i] = getClientWindow(windowList[i]);
if (windowList[i] != None &&
XGetClassHint(display, windowList[i], &classhint)) {
if (wmclass) {
if (strcmp(classhint.res_name, wmname) ||
strcmp(classhint.res_class, wmclass))
windowList[i] = None;
} else {
if (strcmp(classhint.res_name, wmname) &&
strcmp(classhint.res_class, wmname))
windowList[i] = None;
}
if (windowList[i] != None) {
MSG(("selected window 0x%x: `%s.%s'", windowList[i],
classhint.res_name, classhint.res_class));
windowList[matchingWindowCount++] = windowList[i];
}
}
}
windowCount = matchingWindowCount;
}
MSG(("windowCount: %d", windowCount));
/******************************************************************************/
while (argp < argv + argc) {
char const * action(*argp++);
if (!strcmp(action, "setWindowTitle")) {
CHECK_ARGUMENT_COUNT (1)
char const * title(*argp++);
MSG(("setWindowTitle: `%s'", title));
FOREACH_WINDOW(window) XStoreName(display, *window, title);
} else if (!strcmp(action, "setIconTitle")) {
CHECK_ARGUMENT_COUNT (1)
char const * title(*argp++);
MSG(("setIconTitle: `%s'", title));
FOREACH_WINDOW(window) XSetIconName(display, *window, title);
} else if (!strcmp(action, "setGeometry")) {
CHECK_ARGUMENT_COUNT (1)
char const * geometry(*argp++);
int geom_x, geom_y; unsigned geom_width, geom_height;
int status(XParseGeometry(geometry, &geom_x, &geom_y,
&geom_width, &geom_height));
FOREACH_WINDOW(window) {
XSizeHints normal;
long supplied;
XGetWMNormalHints(display, *window, &normal, &supplied);
Window root; int x, y; unsigned width, height, dummy;
XGetGeometry(display, *window, &root,
&x, &y, &width, &height, &dummy, &dummy);
if (status & XValue) x = geom_x;
if (status & YValue) y = geom_y;
if (status & WidthValue) width = geom_width;
if (status & HeightValue) height = geom_height;
if (normal.flags & PResizeInc) {
width*= max(1, normal.width_inc);
height*= max(1, normal.height_inc);
}
if (normal.flags & PBaseSize) {
width+= normal.base_width;
height+= normal.base_height;
}
if (status & XNegative)
x+= DisplayWidth(display, DefaultScreen(display)) - width;
if (status & YNegative)
y+= DisplayHeight(display, DefaultScreen(display)) - height;
MSG(("setGeometry: %dx%d%+i%+i", width, height, x, y));
XMoveResizeWindow(display, *window, x, y, width, height);
}
} else if (!strcmp(action, "setState")) {
CHECK_ARGUMENT_COUNT (2)
unsigned mask(states.parseExpression(*argp++));
unsigned state(states.parseExpression(*argp++));
CHECK_EXPRESSION(states, mask, argp[-2])
CHECK_EXPRESSION(states, state, argp[-1])
MSG(("setState: %d %d", mask, state));
FOREACH_WINDOW(window) setState(*window, mask, state);
} else if (!strcmp(action, "toggleState")) {
CHECK_ARGUMENT_COUNT (1)
unsigned state(states.parseExpression(*argp++));
CHECK_EXPRESSION(states, state, argp[-1])
MSG(("toggleState: %d", state));
FOREACH_WINDOW(window) toggleState(*window, state);
} else if (!strcmp(action, "setHints")) {
CHECK_ARGUMENT_COUNT (1)
unsigned hint(hints.parseExpression(*argp++));
CHECK_EXPRESSION(hints, hint, argp[-1])
MSG(("setHints: %d", hint));
FOREACH_WINDOW(window) setHints(*window, hint);
} else if (!strcmp(action, "setWorkspace")) {
CHECK_ARGUMENT_COUNT (1)
const long workspace(WorkspaceInfo(root).
parseWorkspaceName(*argp++));
if (WinWorkspaceInvalid == workspace) THROW(1);
MSG(("setWorkspace: %d", workspace));
FOREACH_WINDOW(window) setWorkspace(*window, workspace);
} else if (!strcmp(action, "listWorkspaces")) {
YTextProperty workspaceNames(root, ATOM_WIN_WORKSPACE_NAMES);
for (int n(0); n < workspaceNames.count(); ++n)
printf(_("workspace #%d: `%s'\n"), n, workspaceNames.item(n));
} else if (!strcmp(action, "setLayer")) {
CHECK_ARGUMENT_COUNT (1)
unsigned layer(layers.parseExpression(*argp++));
CHECK_EXPRESSION(layers, layer, argp[-1])
MSG(("setLayer: %d", layer));
FOREACH_WINDOW(window) setLayer(*window, layer);
} else if (!strcmp(action, "setTrayOption")) {
CHECK_ARGUMENT_COUNT (1)
unsigned trayopt(trayOptions.parseExpression(*argp++));
CHECK_EXPRESSION(trayOptions, trayopt, argp[-1])
MSG(("setTrayOption: %d", trayopt));
FOREACH_WINDOW(window) setTrayHint(*window, trayopt);
} else {
msg(_("Unknown action: `%s'"), action);
THROW(1);
}
}
CATCH(
if (windowList != singleWindowList) {
XFree(windowList);
}
if (display) {
XSync(display, False);
XCloseDisplay(display);
}
)
return 0;
}
|