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
|
/*
* rcrun.c -- virtual machine to run byte-compiled ~/.twinrc code
*
* Copyright (C) 2001 by Massimiliano Ghilardi
*
* 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.
*
*/
#include "twin.h"
#include "main.h"
#include "data.h"
#include "builtin.h"
#include "draw.h"
#include "resize.h"
#include "util.h"
#include "wm.h"
#include "extensions.h"
#include "methods.h"
#include "hw.h"
#include "common.h"
#include "hw_multi.h"
#include "rctypes.h"
#include "rcparse_tab.h"
#include "rcrun.h"
#ifdef CONF_WM_RC
# include "rcproto.h"
#endif
#ifdef CONF__MODULES
# include "dl.h"
#endif
#include "Tw/Twkeys.h"
#include "hotkey.h"
#define MAX_RUNCYCLE 1024 /* kill a queue after this number of steps */
#define MAX_RUNSTACK 28
typedef struct run run;
struct run {
run *next;
uldat W; /* the current widget (id) */
uldat depth; /* index of last used stack element;
* stack[depth] is the current instruction */
uldat cycle;
union {
timevalue WakeUp;
str Name;
} SW; /* what we are waiting for: sleep timeout or widget map */
wm_ctx C; /* event that generated the run queue */
node stack[MAX_RUNSTACK];
};
byte GlobalsAreStatic;
node Globals[GLOBAL_MAX];
node *MenuBinds; /* array of pointers to nodes inside MenuList */
udat MenuBindsMax;
static run *Run; /* list of running queues */
static run *Sleep; /* list of sleeping queues */
static run *Wait; /* list of waiting-for-window queues */
static run *Interactive; /* list of waiting-for-interactive-op queues */
/* shell-like wildcard pattern matching */
static byte wildcard_match(str p, str q) {
byte c;
if (!q)
q = "";
for (;;) {
switch (c = *p++) {
case '\0':
return !*q;
break;
case '\\':
if (*q++ != *p++)
return FALSE;
break;
case '?':
if (*q++ == '\0')
return FALSE;
break;
case '*':
c = *p;
if (c != '\\' && c != '?' && c != '*' && c != '[') {
while (*q != c) {
if (*q++ == '\0')
return FALSE;
}
}
do {
if (wildcard_match(p, q))
return TRUE;
} while (*q++ != '\0');
return FALSE;
case '[': {
str endp;
byte invert = 0, found = 0;
byte chr;
endp = p;
if (*endp == '!')
endp++;
for (;;) {
if (*endp == '\0')
goto dft; /* no matching ] */
if (*endp == '\\')
endp++;
if (*++endp == ']')
break;
}
if (*p == '!') {
invert++;
p++;
}
chr = *q++;
c = *p++;
do {
if (c == '\\')
c = *p++;
if (*p == '-' && p[1] != ']') {
p++;
if (*p == '\\')
p++;
if (chr >= c && chr <= *p)
found = 1;
p++;
} else {
if (chr == c)
found = 1;
}
} while ((c = *p++) != ']');
if (found == invert)
return FALSE;
break;
}
dft:
default:
if (*q++ != c)
return FALSE;
break;
}
}
}
node LookupNodeName(str name, node head) {
node l;
if (name) for (l = head; l; l = l->next) {
if (!CmpStr(name, l->name))
return l;
}
return NULL;
}
static run *ReverseRunList(run *l) {
run *base = NULL, *v;
while (l) {
v = l->next;
l->next = base;
base = l;
l = v;
}
return base;
}
static ldat Pos2Ctx(udat Pos) {
switch (Pos) {
case POS_TITLE: return CTX_TITLE;
case POS_CORNER:return CTX_CORNER;
case POS_SIDE: return CTX_SIDE;
case POS_BAR_BACK:
case POS_BAR_FWD:
case POS_TAB:
case POS_ARROW_BACK:
case POS_ARROW_FWD:
return CTX_BARS;
case POS_INSIDE:return CTX_INSIDE;
case POS_MENU: return CTX_MENU;
case POS_SCREENBUTTON:
case POS_ROOT:
return CTX_ROOT;
default:
if (Pos < BUTTON_MAX)
return CTX_BUTTON(Pos);
}
return 0;
}
static node RCFindKeyBind(ldat label, ldat shiftflags) {
node l = KeyList;
for (; l; l = l->next) {
if (label == l->id && shiftflags == l->x.ctx)
return l->body;
}
return NULL;
}
static node RCFindMouseBind(ldat code, ldat ctx) {
node l = MouseList;
ldat hc = code & (HOLD_CODE(3) | HOLD_CODE(4));
code &= HOLD_ANY;
for (; l; l = l->next) {
/* triple-inclusive match here:
* (l->id & code) : match buttons
* (l->id & hc) : match hold/click
* (l->x.ctx & ctx): match release context
*/
if ((l->id & code) && (l->id & hc) && (l->x.ctx & ctx))
return l;
}
return NULL;
}
hwfont *RCFindBorderPattern(window W, byte Border) {
node l;
if (!W)
return NULL;
for (l = BorderList; l; l=l->next) {
if ((l->x.f.flag == FL_INACTIVE) == Border && wildcard_match(l->name, W->Name))
break;
}
return W->BorderPattern[Border] = l ? (hwfont *)l->data : NULL;
}
INLINE void RCRemove(run **p) {
run *r = *p;
*p = r->next;
r->next = NULL;
}
#define RCAddFirst(r, head) do { (r)->next = (head); (head) = (r); } while (0)
static void RCKillAll(void) {
run *r, *s, **l, *list[4];
list[0] = Run; list[1] = Sleep;
list[2] = Wait; list[3] = Interactive;
for (s = *(l = list); l < list + 4; s = *++l) {
while ((r = s)) {
s = r->next;
FreeMem(r);
}
}
Run = Sleep = Wait = Interactive = (run *)0;
}
INLINE void RCKill(run **p) {
run *r;
if ((r = *p)) {
*p = r->next;
FreeMem(r);
}
}
static run *RCNew(node l) {
run *r;
if ((r = (run *)AllocMem(sizeof(run)))) {
r->cycle = 0;
r->stack[ r->depth = 0 ] = l;
RCAddFirst(r, Run);
}
return r;
}
static ldat applyflagx(node n) {
ldat x = 0;
switch (n->x.f.plus_minus) {
case 0:
case '+':
case FL_ON:
x++;
break;
case '-':
case FL_OFF:
x--;
break;
default:
break;
}
return x * n->x.f.a;
}
static ldat applyflagy(node n) {
ldat y = 0;
switch (n->x.f.flag) {
case 0:
case '+':
case FL_ON:
y++;
break;
case '-':
case FL_OFF:
y--;
break;
default:
break;
}
return y * n->x.f.b;
}
static window RCFindWindowName(str name) {
uldat len = strlen(name);
window W;
screen S = All->FirstScreen;
while (S) {
/* search among mapped windows */
W = (window)S->FirstW;
while (W) {
if (IS_WINDOW(W) && W->NameLen == len && !CmpMem(W->Name, name, len))
return W;
W = (window)W->Next;
}
S = S->Next;
}
return NULL;
}
static screen RCFindScreenName(str name) {
uldat len = strlen(name);
screen S = All->FirstScreen;
while (S) {
if (S->NameLen == len && !CmpMem(S->Name, name, len))
break;
}
return S;
}
INLINE widget RCCheck4WidgetId(run *r) {
widget W;
if (!(W = (widget)Id2Obj(widget_magic_id, r->W))
|| !W->Parent || !IS_SCREEN(W->Parent))
r->W = NOID;
return W;
}
#define Snext 0
#define Sfunc 1
#define Sbody 2
#define Ssleep 3
#define Swait 4
#define Sinter 5
#define Serr 6
INLINE widget ForwardWindow(widget W) {
while (W) {
if (IS_WINDOW(W))
return W;
W = W->Next;
}
return W;
}
INLINE widget BackwardWindow(widget W) {
while (W) {
if (IS_WINDOW(W))
return W;
W = W->Prev;
}
return W;
}
INLINE screen ScreenOf(widget W) {
widget P;
return W && (P=W->Parent) && IS_SCREEN(P) ? (screen)P : (screen)0;
}
/* run the specified queue */
static byte RCSteps(run *r) {
widget W, SkipW;
screen S;
wm_ctx *C;
node n, f;
ldat flag;
str *argv;
byte state, ret;
W = RCCheck4WidgetId(r);
S = ScreenOf(W);
C = &r->C;
n = r->stack[r->depth];
while (r->cycle++ < MAX_RUNCYCLE) {
state = Snext;
if (n) switch (n->id) {
case EXEC:
if (flag_secure)
printk(flag_secure_msg);
else switch (fork()) {
case -1: /* error */
break;
case 0: /* child */
execvp((char *)n->x.v.argv[0], (char **)n->x.v.argv);
exit(1);
break;
default: /* parent */
break;
}
break;
case EXECTTY:
/*
* luckily, this does not immediately Map() the window...
* it goes in a MSG_MAP queued in the WM_MsgPort,
* so we have the time to reach the corresponding WAIT here
*/
Ext(Term,Open)(n->x.v.argv[0], n->x.v.argv);
break;
case INTERACTIVE:
C->W = W;
if (!C->Screen && !(C->Screen=S))
C->Screen = All->FirstScreen;
ret = FALSE;
switch (n->x.f.flag) {
case MENU: ret = ActivateCtx(C, STATE_MENU); break;
case SCROLL: ret = ActivateCtx(C, STATE_SCROLL); break;
case MOVE: ret = ActivateCtx(C, STATE_DRAG); break;
case RESIZE: ret = ActivateCtx(C, STATE_RESIZE); break;
case SCREEN: ret = ActivateCtx(C, STATE_SCREEN); break;
default: state = Serr; break;
}
if (ret)
state = Sinter;
break;
case MENU:
/* this is just like INTERACTIVE MENU ... but does not wait! */
ActivateCtx(C, STATE_MENU);
break;
case MODULE:
#ifdef CONF__MODULES
if (n->x.f.a == -1)
n->x.f.a = DlName2Code(n->name);
if (n->x.f.flag == FL_ON)
DlLoad(n->x.f.a);
else
DlUnLoad(n->x.f.a);
#endif
break;
case MOVE:
if (W && IS_WINDOW(W))
DragWindow((window)W, applyflagx(n), applyflagy(n));
break;
case MOVESCREEN:
if (S && S != All->FirstScreen)
Act(Focus,S)(S);
DragFirstScreen(applyflagx(n), applyflagy(n));
break;
case NOP:
break;
case RESTART:
SendControlMsg(Ext(WM,MsgPort), MSG_CONTROL_RESTART, 1 + strlen(n->name), n->name);
return Serr;
break;
case RESIZE:
if (W && IS_WINDOW(W)) {
dat x = applyflagx(n), y = applyflagy(n);
if (n->x.f.plus_minus == 0)
x = n->x.f.a - W->XWidth + 2*!(W->Flags & WINDOWFL_BORDERLESS);
if (n->x.f.flag == 0)
y = n->x.f.b - W->YWidth + 2*!(W->Flags & WINDOWFL_BORDERLESS);
ResizeRelWindow((window)W, x, y);
Check4Resize((window)W); /* send MSG_WINDOW_CHANGE and realloc(W->Contents) */
}
break;
case RESIZESCREEN:
if (S && S != All->FirstScreen)
Act(Focus,S)(S);
ResizeFirstScreen(applyflagx(n));
break;
case SCROLL:
if (W && IS_WINDOW(W))
ScrollWindow((window)W, applyflagx(n), applyflagy(n));
break;
case SENDTOSCREEN:
if (W && IS_WINDOW(W) && S && n->name) {
screen Screen = RCFindScreenName(n->name);
if (S != Screen) {
Act(UnMap,W)(W);
Act(Map,W)(W, (widget)Screen);
}
}
break;
case SLEEP:
r->SW.WakeUp.Seconds = n->x.f.a;
r->SW.WakeUp.Fraction = (frac_t)0;
IncrTime(&r->SW.WakeUp, &All->Now);
state = Ssleep;
break;
case STDERR:
argv = n->x.v.argv;
while (*argv)
printk("%s ", *argv++);
printk("\n");
break;
case SYNTHETICKEY:
if (W)
SyntheticKey(W, n->x.f.a, n->x.f.flag, strlen(n->name), n->name);
break;
case WAIT:
/* remember the window name we are waiting for */
r->SW.Name = n->name;
state = Swait;
break;
case WINDOW:
if (n->name)
W = (widget)RCFindWindowName(n->name);
else {
ldat i = applyflagx(n);
flag = n->x.f.plus_minus;
/*
* WINDOW <n> :
* n = 0 : re-get current focused window
* n > 0 : 1 = First Window, 2 = Second, etc.
*
* WINDOW {+|-}<n>
* n = 0 : no-op
* n < 0 : -1 : Prev Window, -2 Prev Prev, etc.
* n > 0 : +1 : Next Window, +2 Next Next, etc.
*/
if (flag == 0) {
if (i == 0) {
W = All->FirstScreen->FocusW;
if (W && !IS_WINDOW(W))
W = (widget)0;
} else {
if (i > 0) i--;
W = ForwardWindow(All->FirstScreen->FirstW);
}
}
if (W) {
while (i > 0 && W) {
i--;
if ((SkipW = ForwardWindow(W->Next)))
W = SkipW;
else {
S = ScreenOf(W);
W = NULL;
if (S) while ((S = S->Next) && !(W = ForwardWindow(S->FirstW)))
;
}
}
while (i < 0 && W) {
i++;
if ((SkipW = BackwardWindow(W->Prev)))
W = SkipW;
else {
S = ScreenOf(W);
W = NULL;
if (S) while ((S = S->Prev) && !(W = BackwardWindow(S->LastW)))
;
}
}
}
}
r->W = W ? W->Id : NOID;
S = ScreenOf(W);
if (n->body) /* enter function */
state = Sbody;
break;
case BEEP:
BeepHW();
break;
case CENTER:
if (W && IS_WINDOW(W))
CenterWindow((window)W);
break;
case CLOSE:
if (W)
AskCloseWidget(W);
break;
case KILL:
/* BRUTAL! */
if (W) {
msgport M = W->Owner;
/*
* try not to exagerate with brutality:
* allow deleting remote clients msgports only
*/
if (M->RemoteData.FdSlot != NOSLOT) {
Ext(Remote,KillSlot)(M->RemoteData.FdSlot);
W = NULL;
S = NULL;
r->W = NOID;
} else
AskCloseWidget(W);
}
break;
case QUIT:
Quit(0);
break;
case REFRESH:
RefreshVideo();
break;
case WINDOWLIST:
ShowWinList(C);
break;
case FOCUS:
if (W && S) {
flag = n->x.f.flag;
if (flag == FL_TOGGLE)
flag = (S->FocusW == (widget)W) ? FL_OFF : FL_ON;
if (flag == FL_ON && S != All->FirstScreen)
Act(Focus,S)(S);
Act(Focus,W)(flag == FL_ON ? W : (widget)0);
}
break;
case MAXIMIZE:
case FULLSCREEN:
if (W && IS_WINDOW(W) && S)
MaximizeWindow((window)W, n->id == FULLSCREEN);
break;
case LOWER:
if (W && S)
MakeLastWidget(W, FALSE);
break;
case RAISE:
if (W && S)
MakeFirstWidget(W, FALSE);
break;
case RAISELOWER:
if (W && S) {
if ((widget)W == S->FirstW)
MakeLastWidget(W, TRUE);
else
MakeFirstWidget(W, TRUE);
}
break;
case ROLL:
if (W && IS_WINDOW(W)) {
flag = n->x.f.flag;
if (flag == FL_TOGGLE)
flag = W->Attrib & WINDOW_ROLLED_UP ? FL_OFF : FL_ON;
RollUpWindow((window)W, flag == FL_ON);
}
break;
case USERFUNC:
if (n->x.func || ((f = LookupNodeName(n->name, FuncList))
&& (n->x.func = f->body)))
/* stored into n->f.func to speedup future references */
state = Sfunc;
else /* user function not found. abort. */
state = Serr;
break;
default:
/* unknown command. abort. */
state = Serr;
break;
}
if (state == Snext) {
/* next instruction */
if (!n || !(n = n->next)) {
/* end of function. pop the stack. */
if (r->depth)
n = r->stack[--r->depth];
else /* stack empty. exit. */
break;
}
} else if (state == Sfunc || state == Sbody) {
/* enter f or n->body */
if (r->depth < MAX_RUNSTACK) {
r->stack[r->depth++] = n->next;
n = (state == Sfunc) ? n->x.func : n->body;
} else /* stack overflow */
break;
} else if (state == Ssleep || state == Swait || state == Sinter) {
/* prepare to resume from next instruction after sleeping */
r->stack[r->depth] = n->next;
break;
} else
break;
}
return state;
}
/* run all the runnable queues */
static void RCRun(void) {
run *r, **p;
/* ensure correct execution order */
Run = ReverseRunList(Run);
p = &Run;
while ((r = *p)) switch (RCSteps(r)) {
case Ssleep:
/* go to sleep */
RCRemove(p);
RCAddFirst(r, Sleep);
break;
case Swait:
/* wait 4 window */
RCRemove(p);
RCAddFirst(r, Wait);
break;
case Sinter:
/* wait 4 interactive op */
RCRemove(p);
RCAddFirst(r, Interactive);
break;
default:
/* exit, runaway or error */
RCKill(p);
break;
}
}
/*
* let's see how much we can sleep:
* return FALSE if no limit to sleep time;
* else _t will be filled with max time to sleep.
*/
static byte RCSleep(timevalue *_t) {
timevalue *t = _t;
run *r = Sleep;
t->Seconds = MAXTIME_T;
t->Fraction = (frac_t)0; /* not MAXFRAC_T as Normalize() would overflow */
while (r) {
if (CmpTime(&r->SW.WakeUp, t) < 0)
t = &r->SW.WakeUp;
r = r->next;
}
if (t != _t) {
if (CmpTime(t, &All->Now) > 0)
SubTime(_t, t, &All->Now);
else {
/* avoid busy looping */
_t->Seconds = 0;
_t->Fraction = 10 MilliSECs;
}
return TRUE;
}
return FALSE;
}
/*
* this is the real Initialization / Restart function.
*
* kill the queues, reload .twinrc and restart queues
*/
static void RCReload(void) {
byte success;
#if !defined(CONF_WM_RC) && defined(CONF__MODULES)
module M;
byte (*mod_rcload)(void) = (byte (*)(void))0;
if ((M = DlLoad(RCParseSo)))
mod_rcload = M->Private;
# if 0
/* this would garble -hw=tty display */
else
printk("twin: failed to load the RC parser:\n"
" %s\n", ErrStr);
# endif
#endif
success =
#if defined(CONF_WM_RC)
rcload()
#elif defined(CONF__MODULES)
(mod_rcload && mod_rcload())
#else
0
#endif
;
#if !defined(CONF_WM_RC) && defined(CONF__MODULES)
if (M)
DlUnLoad(RCParseSo);
#endif
if (success) {
QueuedDrawArea2FullScreen = TRUE;
ResetBorderPattern();
RCKillAll();
if (CallList)
RCNew(CallList);
FillButtonWin();
UpdateOptionWin();
HideMenu(!!(All->SetUp->Flags & SETUP_HIDEMENU));
}
}
/* wake up queues when slept enough or when finished interactive op */
static void RCWake(void) {
run **p = &Sleep, *r;
while ((r = *p)) {
if (CmpTime(&r->SW.WakeUp, &All->Now) <= 0) {
RCRemove(p); /* p does not change but *p is now the next run */
RCAddFirst(r, Run);
} else
p = &r->next;
}
if ((All->State & STATE_ANY) == STATE_DEFAULT && Interactive) {
p = &Interactive;
while ((r = *p)) {
RCRemove(p); /* p does not change but *p is now the next run */
RCAddFirst(r, Run);
}
}
}
/* wake up queues when the wanted window appears */
static void RCWake4Window(window W) {
str Name = W->Name;
run **p = &Wait, *r;
if (Name) while ((r = *p)) {
if (!CmpStr(r->SW.Name, Name)) {
r->W = W->Id;
RCRemove(p); /* p does not change but *p is now the next run */
RCAddFirst(r, Run);
} else
p = &r->next;
}
}
static byte MouseClickReleaseSameCtx(uldat W1, uldat W2, ldat clickCtx, ldat relCtx, ldat ctx) {
if ((ctx & clickCtx) && (ctx & relCtx)) {
if ((clickCtx & CTX_ANY_WIN) != (relCtx & CTX_ANY_WIN))
return FALSE;
if ((clickCtx & CTX_ANY_WIN) && (relCtx & CTX_ANY_WIN))
return W1 == W2;
return TRUE;
}
return FALSE;
}
/* handle incoming messages */
byte RC_VMQueue(CONST wm_ctx *C) {
uldat ClickWinId = All->FirstScreen->ClickWindow
? All->FirstScreen->ClickWindow->Id : NOID;
widget W;
ldat ctx;
node n;
run *r;
udat Code;
byte used = FALSE;
/* from wm.c : */
extern byte ClickWindowPos;
switch (C->Type) {
case MSG_KEY:
case MSG_MOUSE:
n = (node)0;
if (C->Type == MSG_MOUSE) {
ctx = Pos2Ctx(C->Pos);
if (isSINGLE_PRESS(C->Code)) {
Code = (C->Code & HOLD_ANY) | HOLD_CODE(3);
n = RCFindMouseBind((ldat)Code, ctx);
if (n) {
ClickWinId = C->W ? C->W->Id : NOID;
n = n->body;
}
} else if (isSINGLE_RELEASE(C->Code)) {
Code = HOLD_CODE(RELEASE_N(C->Code)) | HOLD_CODE(4);
n = RCFindMouseBind((ldat)Code, ctx);
if (n && MouseClickReleaseSameCtx(ClickWinId, C->W ? C->W->Id : NOID,
Pos2Ctx(ClickWindowPos), ctx, n->x.ctx))
n = n->body;
else
n = NULL;
}
} else
n = RCFindKeyBind((ldat)C->Code, (ldat)C->ShiftFlags);
if (n && (r = RCNew(n))) {
used = TRUE, CopyMem(C, &r->C, sizeof(wm_ctx));
r->W = ClickWinId;
/* to preserve execution orded, run it right now ! */
RCRun();
}
break;
case MSG_MAP:
used = TRUE, RCWake4Window((window)C->W);
break;
case MSG_CONTROL:
if (C->Code == MSG_CONTROL_OPEN) {
used = TRUE;
if (All->State != STATE_DEFAULT)
/*
* return to STATE_DEFAULT, and rely on RCReload()
* to set QueuedDrawArea2FullScreen = TRUE
*/
ForceRelease(C);
RCReload();
}
break;
case MSG_MENU_ROW:
if (C->Code >= COD_RESERVED && C->Code < MenuBindsMax + COD_RESERVED) {
n = MenuBinds[C->Code - COD_RESERVED];
if (n && (r = RCNew(n))) {
used = TRUE;
CopyMem(C, &r->C, sizeof(wm_ctx));
W = All->FirstScreen->FocusW;
r->W = W ? W->Id : NOID;
r->C.ByMouse = FALSE;
/* to preserve execution orded, run it right now ! */
RCRun();
}
}
break;
default:
break;
}
return used;
}
/* virtual machine main loop */
byte RC_VM(timevalue *t) {
/* do the real thing: run the queues */
RCWake();
RCRun();
return RCSleep(t);
}
void QuitRC(void) {
ResetBorderPattern();
RCKillAll();
}
#define COD_COMMON_FIRST COD_COMMON_DRAG
#define COD_COMMON_DRAG (COD_RESERVED)
#define COD_COMMON_RESIZE (COD_RESERVED + 1)
#define COD_COMMON_SCROLL (COD_RESERVED + 2)
#define COD_COMMON_CENTER (COD_RESERVED + 3)
#define COD_COMMON_MAXIMIZE (COD_RESERVED + 4)
#define COD_COMMON_FULLSCREEN (COD_RESERVED + 5)
#define COD_COMMON_ROLLTOGGLE (COD_RESERVED + 6)
#define COD_COMMON_RAISELOWER (COD_RESERVED + 7)
#define COD_COMMON_UNFOCUS (COD_RESERVED + 8)
#define COD_COMMON_NEXT (COD_RESERVED + 9)
#define COD_COMMON_WINDOWLIST (COD_RESERVED + 10)
#define COD_COMMON_REFRESH (COD_RESERVED + 11)
#define COD_COMMON_HOTKEY (COD_RESERVED + 12)
#define COD_COMMON_CLOSE (COD_RESERVED + 13)
#define COD_COMMON_LAST COD_COMMON_CLOSE
static byte USEDefaultCommonMenu(void) {
menu Menu;
menuitem Item;
window W;
row Row;
if (!(Menu = Do(Create,Menu)(FnMenu, Ext(WM,MsgPort), (hwcol)0, (hwcol)0, (hwcol)0,
(hwcol)0, (hwcol)0, (hwcol)0, TRUE)))
return FALSE;
if ((W=Win4Menu(Menu)) &&
(Item = Item4Menu(Menu, W, TRUE, 8, " Window ")) &&
/* we cannot create rows with codes >= COD_RESERVED... */
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Move ")) && (Row->Code = COD_COMMON_DRAG) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Resize ")) && (Row->Code = COD_COMMON_RESIZE) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Scroll ")) && (Row->Code = COD_COMMON_SCROLL) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Center ")) && (Row->Code = COD_COMMON_CENTER) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Maximize ")) && (Row->Code = COD_COMMON_MAXIMIZE) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Full Screen ")) && (Row->Code = COD_COMMON_FULLSCREEN) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Roll Up ")) && (Row->Code = COD_COMMON_ROLLTOGGLE) &&
Row4Menu(W, 0, ROW_IGNORE, 13,"") &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Raise/Lower ")) && (Row->Code = COD_COMMON_RAISELOWER) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," UnFocus ")) && (Row->Code = COD_COMMON_UNFOCUS) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Next ")) && (Row->Code = COD_COMMON_NEXT) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," List... ")) && (Row->Code = COD_COMMON_WINDOWLIST) &&
Row4Menu(W, 0, ROW_IGNORE, 13,"") &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Refresh ")) && (Row->Code = COD_COMMON_REFRESH) &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Send HotKey ")) && (Row->Code = COD_COMMON_HOTKEY) &&
Row4Menu(W, 0, ROW_IGNORE, 13,"") &&
(Row = Row4Menu(W, 0, ROW_ACTIVE, 13," Close ")) && (Row->Code = COD_COMMON_CLOSE)) {
/* success */
Item->Left = 0; /* remove padding */
if (All->CommonMenu)
Delete(All->CommonMenu);
All->CommonMenu = Menu;
return TRUE;
}
/* out of memory */
Delete(Menu);
return FALSE;
}
byte InitRC(void) {
byte InitRCOptions(void);
static struct node N[] = {
{ INTERACTIVE, NULL, NULL, NULL, NULL, { { 0, MOVE, }, } }, /* COD_COMMON_DRAG */
{ INTERACTIVE, NULL, NULL, NULL, NULL, { { 0, RESIZE, }, } }, /* COD_COMMON_RESIZE */
{ INTERACTIVE, NULL, NULL, NULL, NULL, { { 0, SCROLL, }, } }, /* COD_COMMON_SCROLL */
{ CENTER, }, /* COD_COMMON_CENTER */
{ MAXIMIZE, }, /* COD_COMMON_MAXIMIZE */
{ FULLSCREEN, }, /* COD_COMMON_FULLSCREEN */
{ ROLL, NULL, NULL, NULL, NULL, { { 0, FL_TOGGLE, }, } },/* COD_COMMON_ROLLTOGGLE */
{ RAISELOWER, }, /* COD_COMMON_RAISELOWER */
{ FOCUS, NULL, NULL, NULL, NULL, { { 0, FL_OFF, }, } }, /* COD_COMMON_UNFOCUS */
{ WINDOW, NULL, NULL, NULL, NULL, { { '+', 0, 1, }, } }, /* COD_COMMON_NEXT */
{ WINDOWLIST, }, /* COD_COMMON_WINDOWLIST */
{ REFRESH, }, /* COD_COMMON_REFRESH */
{ SYNTHETICKEY, }, /* COD_COMMON_HOTKEY */
{ CLOSE, } /* COD_COMMON_CLOSE */
};
static struct node *pN[] = {
N, N+1, N+2, N+3, N+4, N+5, N+6, N+7, N+8, N+9, N+10, N+11, N+12, N+13
};
/* now user functions list */
static struct node F[] = {
/* AddToFunc _UnFocus ( Window 0; Focus Off;) */
{ 0, "_UnFocus", NULL, F+1, },
{ WINDOW, NULL, N+8, NULL, NULL, { { 0, 0, } } }
};
/* now keyboard binds */
static struct node K[] = {
/* Key HOT_KEY Interactive Menu */
{ 0, NULL, NULL, K+1, },
{ INTERACTIVE, NULL, NULL, NULL, NULL, { { 0, MENU, }, } }
};
/* now mouse binds */
static struct node M[] = {
/* Mouse 1 0 Close */
{ HOLD_LEFT|HOLD_CODE(4), "0", M+1, N+13,NULL, { { CTX_0, } } },
/* Mouse 123 1 RaiseLower */
{ HOLD_ANY |HOLD_CODE(4), "1", M+2, N+7, NULL, { { CTX_1, } } },
/* Mouse 123 3 Roll Toggle */
{ HOLD_ANY |HOLD_CODE(4), "2", M+3, N+6, NULL, { { CTX_2, } } },
/* Mouse H1 TS Interactive Move */
{ HOLD_LEFT|HOLD_CODE(3), "TS", M+4, N+0, NULL, { { CTX_TITLE|CTX_SIDE, } } },
/* Mouse H1 C Interactive Resize */
{ HOLD_LEFT|HOLD_CODE(3), "C", M+5, N+1, NULL, { { CTX_CORNER, } } },
/* Mouse H1 C Interactive Scroll */
{ HOLD_LEFT|HOLD_CODE(3), "B", M+6, N+2, NULL, { { CTX_BARS, } } },
/* Mouse H1 R _UnFocus */
{ HOLD_LEFT|HOLD_CODE(3), "R", M+8, M+7, NULL, { { CTX_ROOT, } } },
{ USERFUNC, "_UnFocus", },
/* Mouse H2 R WindowList */
{ HOLD_MIDDLE|HOLD_CODE(3),"R", M+9, N+10,NULL, { { CTX_ROOT, } } },
/* Mouse H3 A Interactive Menu */
{ HOLD_RIGHT|HOLD_CODE(3), "A", M+10,K+1, NULL, { { CTX_ANY, } } },
/* Mouse H1 M Interactive Screen */
{ HOLD_LEFT|HOLD_CODE(3), "M", NULL,M+11,NULL, { { CTX_MENU, } } },
{ INTERACTIVE, NULL, NULL, NULL, NULL, { { 0, SCREEN, }, } }
};
static button_vec V[] = {
{ {'[', ']'}, 0, TRUE, FALSE },
{ {'\x12', '\x12'} , -2, TRUE, FALSE },
{ {'>', '<'} , -4, TRUE, FALSE }
};
str Seq = "";
/*
* this is really heavy on the compiler...
* but it should be able to optimize it
* to just the line with the correct TW_xxx key
*/
#define IS(key,len,seq) if (HOT_KEY == TW_##key) Seq = seq;
# include "hw_keys.h"
#undef IS
K[0].name = N[COD_COMMON_HOTKEY - COD_COMMON_FIRST].name = Seq;
K[0].id = N[COD_COMMON_HOTKEY - COD_COMMON_FIRST].x.f.a = HOT_KEY;
WriteMem(Globals, 0, sizeof(Globals));
FuncList = F;
KeyList = K;
MouseList = M;
MenuBinds = pN;
MenuBindsMax = COD_COMMON_LAST - COD_COMMON_FIRST + 1;
GlobalsAreStatic = TRUE;
WriteMem(All->ButtonVec, 0, sizeof(All->ButtonVec));
CopyMem(V, All->ButtonVec, sizeof(V));
All->SetUp->SelectionButton = HOLD_LEFT;
All->SetUp->PasteButton = HOLD_MIDDLE;
All->SetUp->DeltaXShade = 3;
All->SetUp->DeltaXShade = 2;
if (USEDefaultCommonMenu()) {
InitRCOptions();
UpdateOptionWin();
FillButtonWin();
HideMenu(!!(All->SetUp->Flags & SETUP_HIDEMENU));
Act(DrawMenu,All->FirstScreen)(All->FirstScreen, 0, MAXDAT);
return TRUE;
}
return FALSE;
}
|