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
|
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include "defaults.h"
#include "fvwmlib.h"
#include "Parse.h"
#include "PictureBase.h"
#include "FScreen.h"
#include "FEvent.h"
#define GLOBAL_SCREEN_NAME "_global"
/* In fact, only corners matter -- there will never be GRAV_NONE */
enum {GRAV_POS = 0, GRAV_NONE = 1, GRAV_NEG = 2};
static int grav_matrix[3][3] =
{
{ NorthWestGravity, NorthGravity, NorthEastGravity },
{ WestGravity, CenterGravity, EastGravity },
{ SouthWestGravity, SouthGravity, SouthEastGravity }
};
#define DEFAULT_GRAVITY NorthWestGravity
static Display *disp;
static bool is_randr_present;
static bool randr_initialised;
static void scan_screens(Display *);
static struct monitor *monitor_by_name(const char *);
enum monitor_tracking monitor_mode;
bool is_tracking_shared;
struct screen_infos screen_info_q;
struct monitors monitor_q;
int randr_event;
const char *prev_focused_monitor;
static struct monitor *monitor_global = NULL;
static void GetMouseXY(XEvent *eventp, int *x, int *y)
{
XEvent e;
if (eventp == NULL)
{
eventp = &e;
e.type = 0;
}
fev_get_evpos_or_query(
disp, DefaultRootWindow(disp), eventp, x, y);
}
struct monitor *
monitor_new(void)
{
struct monitor *m;
m = fxcalloc(1, sizeof *m);
return (m);
}
static void
monitor_scan_edges(struct monitor *m)
{
struct monitor *m_loop;
if (m == NULL)
return;
m->edge.top = m->edge.bottom = m->edge.left = m->edge.right =
MONITOR_OUTSIDE_EDGE;
TAILQ_FOREACH(m_loop, &monitor_q, entry) {
if (m_loop == m)
continue;
/* Top Edge */
if ( m->si->y == m_loop->si->y + m_loop->si->h )
m->edge.top = MONITOR_INSIDE_EDGE;
/* Bottom Edge */
if ( m->si->y + m->si->h == m_loop->si->y )
m->edge.bottom = MONITOR_INSIDE_EDGE;
/* Left Edge */
if ( m->si->x == m_loop->si->x + m_loop->si->w )
m->edge.left = MONITOR_INSIDE_EDGE;
/* Right Edge */
if ( m->si->x + m->si->w == m_loop->si->x )
m->edge.right = MONITOR_INSIDE_EDGE;
}
}
void
monitor_refresh_global(void)
{
/* Creating the global monitor outside of the monitor_q structure
* allows for only the relevant information to be presented to callers
* which are asking for a global screen.
*
* This structure therefore only uses some of the fields:
*
* si->name,
* si->{x, y, w, h}
*/
if (monitor_global == NULL) {
monitor_global = monitor_new();
monitor_global->si = screen_info_new();
monitor_global->si->name = fxstrdup(GLOBAL_SCREEN_NAME);
}
/* At this point, the global screen has been initialised. Refresh the
* coordinate list.
*/
monitor_global->si->x = 0;
monitor_global->si->y = 0;
monitor_global->si->w = monitor_get_all_widths();
monitor_global->si->h = monitor_get_all_heights();
}
struct screen_info *
screen_info_new(void)
{
struct screen_info *si;
si = fxcalloc(1, sizeof *si);
return (si);
}
struct screen_info *
screen_info_by_name(const char *name)
{
struct screen_info *si;
TAILQ_FOREACH(si, &screen_info_q, entry) {
if (strcmp(si->name, name) == 0)
return (si);
}
return (NULL);
}
struct monitor *
monitor_get_global(void)
{
monitor_refresh_global();
return monitor_global;
}
struct monitor *
monitor_get_prev(void)
{
struct monitor *m, *mret = NULL;
struct monitor *this = monitor_get_current();
TAILQ_FOREACH(m, &monitor_q, entry) {
if (m == this)
continue;
if (m->is_prev) {
mret = m;
break;
}
}
/* Can be NULL -- is checked in expand.c */
return (mret);
}
struct monitor *
monitor_get_current(void)
{
int JunkX = 0, JunkY = 0, x, y;
Window JunkRoot, JunkChild;
unsigned int JunkMask;
struct monitor *mon;
static const char *cur_mon;
prev_focused_monitor = cur_mon;
FQueryPointer(disp, DefaultRootWindow(disp), &JunkRoot, &JunkChild,
&JunkX, &JunkY, &x, &y, &JunkMask);
mon = FindScreenOfXY(x, y);
if (mon != NULL)
cur_mon = mon->si->name;
return (mon);
}
struct monitor *
monitor_resolve_name(const char *scr)
{
struct monitor *m = NULL;
if (scr == NULL)
{
return NULL;
}
/* "@g" is for the global screen. */
if (strcmp(scr, "g") == 0) {
monitor_refresh_global();
m = monitor_global;
}
/* "@c" is for the current screen. */
else if (strcmp(scr, "c") == 0)
m = monitor_get_current();
/* "@p" is for the primary screen. */
else if (strcmp(scr, "p") == 0)
m = monitor_by_primary();
else
/* Assume the monitor name is a literal RandR name (such as
* HDMI2). */
m = monitor_by_name(scr);
return (m);
}
static struct monitor *
monitor_by_name(const char *name)
{
struct monitor *m, *mret;
if (name == NULL) {
fvwm_debug(__func__, "%s: name is NULL; shouldn't happen. "
"Returning current monitor\n", __func__);
return (monitor_get_current());
}
mret = NULL;
TAILQ_FOREACH(m, &monitor_q, entry) {
if (strcmp(m->si->name, name) == 0) {
mret = m;
break;
}
}
return (mret);
}
struct monitor *
monitor_by_output(int output)
{
struct monitor *m, *mret = NULL;
TAILQ_FOREACH(m, &monitor_q, entry) {
if (m->si->rr_output == output) {
mret = m;
break;
}
}
if (mret == NULL) {
fvwm_debug(__func__,
"%s: couldn't find monitor with output '%d', "
"returning first output.\n", __func__, output);
mret = TAILQ_FIRST(&monitor_q);
}
return (mret);
}
struct monitor *
monitor_by_primary(void)
{
struct monitor *m = NULL, *m_loop;
TAILQ_FOREACH(m_loop, &monitor_q, entry) {
if (m_loop->flags & MONITOR_PRIMARY) {
m = m_loop;
break;
}
}
return (m);
}
static void
monitor_check_primary(void)
{
if (monitor_by_primary() == NULL) {
struct monitor *m;
m = TAILQ_FIRST(&monitor_q);
m->flags |= MONITOR_PRIMARY;
}
}
int
monitor_get_all_widths(void)
{
return (DisplayWidth(disp, DefaultScreen(disp)));
}
int
monitor_get_all_heights(void)
{
return (DisplayHeight(disp, DefaultScreen(disp)));
}
void
monitor_assign_virtual(struct monitor *ref)
{
struct monitor *m;
if (monitor_mode == MONITOR_TRACKING_M || is_tracking_shared)
return;
TAILQ_FOREACH(m, &monitor_q, entry) {
if (m == ref)
continue;
memcpy(
&m->virtual_scr, &ref->virtual_scr,
sizeof(m->virtual_scr));
}
}
void
FScreenSelect(Display *dpy)
{
XRRSelectInput(disp, DefaultRootWindow(disp),
RRScreenChangeNotifyMask | RROutputChangeNotifyMask);
}
void
monitor_output_change(Display *dpy, XRRScreenChangeNotifyEvent *e)
{
XRRScreenResources *res;
struct monitor *m = NULL;
fvwm_debug(__func__, "%s: outputs have changed\n", __func__);
if ((res = XRRGetScreenResources(dpy, e->root)) == NULL) {
fvwm_debug(__func__,
"%s: couldn't acquire screen resources\n",
__func__);
return;
}
{
struct screen_info *si;
XRROutputInfo *oinfo = NULL;
int i;
scan_screens(dpy);
for (i = 0; i < res->noutput; i++) {
oinfo = XRRGetOutputInfo(dpy, res, res->outputs[i]);
if (oinfo == NULL)
continue;
si = screen_info_by_name(oinfo->name);
if (si == NULL)
continue;
TAILQ_FOREACH(m, &monitor_q, entry) {
if (m->si == si) {
m->emit &= ~MONITOR_ALL;
break;
}
}
if (m == NULL)
continue;
if (oinfo->connection == RR_Connected &&
m->flags & MONITOR_ENABLED) {
if (m->flags & MONITOR_CHANGED)
m->emit |= MONITOR_CHANGED;
continue;
}
if (oinfo->connection == RR_Disconnected &&
m->flags & MONITOR_DISABLED) {
continue;
}
switch (oinfo->connection) {
case RR_Connected:
m->flags &= ~MONITOR_DISABLED;
m->flags |= MONITOR_ENABLED;
m->emit |= MONITOR_ENABLED;
break;
case RR_Disconnected:
m->flags &= ~MONITOR_ENABLED;
m->flags |= MONITOR_DISABLED;
m->emit |= MONITOR_DISABLED;
break;
default:
break;
}
}
XRRFreeOutputInfo(oinfo);
}
XRRFreeScreenResources(res);
TAILQ_FOREACH(m, &monitor_q, entry)
monitor_scan_edges(m);
monitor_check_primary();
}
static void
scan_screens(Display *dpy)
{
XRRMonitorInfo *rrm;
struct monitor *m = NULL;
int i, n = 0;
Window root = RootWindow(dpy, DefaultScreen(dpy));
rrm = XRRGetMonitors(dpy, root, false, &n);
if (n <= 0 && (!randr_initialised && monitor_get_count() == 0)) {
fvwm_debug(__func__, "get monitors failed\n");
exit(101);
}
for (i = 0; i < n; i++) {
char *name = XGetAtomName(dpy, rrm[i].name);
if (name == NULL) {
fprintf(
stderr,
"%s: couldn't detect monitor with empty name\n",
__func__);
exit (101);
}
if (((m = monitor_by_name(name)) == NULL) ||
(m != NULL && strcmp(m->si->name, name) != 0)) {
m = monitor_new();
m->flags |= MONITOR_NEW;
m->si = screen_info_new();
m->si->name = strdup(name);
memset(&m->virtual_scr, 0, sizeof(m->virtual_scr));
TAILQ_INSERT_TAIL(&screen_info_q, m->si, entry);
TAILQ_INSERT_TAIL(&monitor_q, m, entry);
goto set_coords;
}
if ((strcmp(m->si->name, name) == 0) &&
(m->si->x != rrm[i].x || m->si->y != rrm[i].y ||
m->si->w != rrm[i].width || m->si->h != rrm[i].height)) {
if (m->flags & MONITOR_ENABLED)
m->flags |= MONITOR_CHANGED;
}
set_coords:
m->si->x = rrm[i].x;
m->si->y = rrm[i].y;
m->si->w = rrm[i].width;
m->si->h = rrm[i].height;
m->si->rr_output = *rrm[i].outputs;
if (rrm[i].primary > 0)
m->flags |= MONITOR_PRIMARY;
else
m->flags &= ~MONITOR_PRIMARY;
XFree(name);
}
monitor_scan_edges(m);
monitor_check_primary();
XRRFreeMonitors(rrm);
}
void FScreenInit(Display *dpy)
{
XRRScreenResources *res = NULL;
struct monitor *m;
int err_base = 0, major, minor;
if (randr_initialised)
return;
disp = dpy;
randr_event = 0;
is_randr_present = false;
randr_initialised = false;
if (TAILQ_EMPTY(&monitor_q))
TAILQ_INIT(&monitor_q);
if (TAILQ_EMPTY(&screen_info_q))
TAILQ_INIT(&screen_info_q);
if (!XRRQueryExtension(dpy, &randr_event, &err_base) ||
!XRRQueryVersion (dpy, &major, &minor)) {
fvwm_debug(__func__, "RandR not present");
goto randr_fail;
}
if (major == 1 && minor >= 5)
is_randr_present = true;
if (!is_randr_present) {
/* Something went wrong. */
fvwm_debug(__func__, "Couldn't initialise XRandR: %s\n",
strerror(errno));
goto randr_fail;
}
fvwm_debug(__func__, "Using RandR %d.%d\n", major, minor);
/* XRandR is present, so query the screens we have. */
res = XRRGetScreenResources(dpy, DefaultRootWindow(dpy));
if (res == NULL || (res != NULL && res->noutput == 0)) {
XRRFreeScreenResources(res);
fvwm_debug(__func__, "RandR present, yet no outputs found.");
goto randr_fail;
}
XRRFreeScreenResources(res);
scan_screens(dpy);
randr_initialised = true;
is_tracking_shared = false;
TAILQ_FOREACH(m, &monitor_q, entry) {
m->Desktops = fxcalloc(1, sizeof *m->Desktops);
m->Desktops->name = NULL;
m->Desktops->next = NULL;
m->Desktops->desk = 0;
m->flags |= (MONITOR_NEW|MONITOR_ENABLED);
m->is_prev = false;
monitor_scan_edges(m);
}
monitor_check_primary();
return;
randr_fail:
fprintf(stderr, "Unable to initialise RandR\n");
exit(101);
}
void
monitor_dump_state(struct monitor *m)
{
struct monitor *mcur, *m2;
mcur = monitor_get_current();
fvwm_debug(__func__, "Monitor Debug\n");
fvwm_debug(__func__, "\tnumber of outputs: %d\n", monitor_get_count());
TAILQ_FOREACH(m2, &monitor_q, entry) {
if (m2 == NULL) {
fvwm_debug(__func__,
"monitor in list is NULL. Bug!\n");
continue;
}
if (m != NULL && m2 != m)
continue;
fvwm_debug(
__func__,
"\tName:\t%s\n"
"\tDisabled:\t%s\n"
"\tIs Primary:\t%s\n"
"\tIs Current:\t%s\n"
"\tIs Previous:\t%s\n"
"\tOutput:\t%d\n"
"\tCoords:\t{x: %d, y: %d, w: %d, h: %d}\n"
"\tVirtScr: {\n"
"\t\tVxMax: %d, VyMax: %d, Vx: %d, Vy: %d\n"
"\t\tEdgeScrollX: %d, EdgeScrollY: %d\n"
"\t\tCurrentDesk: %d\n"
"\t\tCurrentPage: {x: %d, y: %d}\n"
"\t\tMyDisplayWidth: %d, MyDisplayHeight: %d\n\t}\n"
"\tDesktops:\t%s\n"
"\tFlags:%s\n\n",
m2->si->name,
(m2->flags & MONITOR_DISABLED) ? "true" : "false",
(m2->flags & MONITOR_PRIMARY) ? "yes" : "no",
(mcur && m2 == mcur) ? "yes" : "no",
m2->is_prev ? "yes" : "no",
(int)m2->si->rr_output,
m2->si->x, m2->si->y, m2->si->w, m2->si->h,
m2->virtual_scr.VxMax, m2->virtual_scr.VyMax,
m2->virtual_scr.Vx, m2->virtual_scr.Vy,
m2->virtual_scr.EdgeScrollX,
m2->virtual_scr.EdgeScrollY,
m2->virtual_scr.CurrentDesk,
(int)(m2->virtual_scr.Vx / monitor_get_all_widths()),
(int)(m2->virtual_scr.Vy / monitor_get_all_heights()),
monitor_get_all_widths(),
monitor_get_all_heights(),
m2->Desktops ? "yes" : "no",
monitor_mode == MONITOR_TRACKING_G ? "global" :
monitor_mode == MONITOR_TRACKING_M ? "per-monitor" :
"Unknown"
);
}
}
int
monitor_get_count(void)
{
struct monitor *m = NULL;
int c = 0;
TAILQ_FOREACH(m, &monitor_q, entry) {
if (m->flags & MONITOR_DISABLED)
continue;
c++;
}
return (c);
}
struct monitor *
FindScreenOfXY(int x, int y)
{
struct monitor *m;
int xa, ya;
int all_widths, all_heights;
all_widths = monitor_get_all_widths();
all_heights = monitor_get_all_heights();
xa = abs(x);
ya = abs(y);
xa %= all_widths;
ya %= all_heights;
TAILQ_FOREACH(m, &monitor_q, entry) {
/* If we have more than one screen configured, then don't match
* on the global screen, as that's separate to XY positioning
* which is only concerned with the *specific* screen.
*/
if (monitor_get_count() > 0 &&
strcmp(m->si->name, GLOBAL_SCREEN_NAME) == 0)
continue;
if (xa >= m->si->x && xa < m->si->x + m->si->w &&
ya >= m->si->y && ya < m->si->y + m->si->h)
return (m);
}
/* FIXME: this is a convenience, but could confuse callers. */
if (m == NULL)
return TAILQ_FIRST(&monitor_q);
return (NULL);
}
/* Returns the primary screen if arg.name is NULL. */
static struct monitor *
FindScreen(fscreen_scr_arg *arg, fscreen_scr_t screen)
{
struct monitor *m = NULL;
fscreen_scr_arg tmp;
if (monitor_get_count() == 0) {
monitor_refresh_global();
return (monitor_global);
}
switch (screen)
{
case FSCREEN_GLOBAL:
monitor_refresh_global();
m = monitor_global;
break;
case FSCREEN_PRIMARY:
m = monitor_by_primary();
break;
case FSCREEN_CURRENT:
/* translate to xypos format */
if (!arg)
{
tmp.mouse_ev = NULL;
arg = &tmp;
}
GetMouseXY(arg->mouse_ev, &arg->xypos.x, &arg->xypos.y);
/* fall through */
case FSCREEN_XYPOS:
/* translate to screen number */
if (!arg)
{
tmp.xypos.x = 0;
tmp.xypos.y = 0;
arg = &tmp;
}
m = FindScreenOfXY(arg->xypos.x, arg->xypos.y);
break;
case FSCREEN_BY_NAME:
if (arg == NULL || arg->name == NULL)
{
m = monitor_by_primary();
}
else
{
m = monitor_resolve_name(arg->name);
}
break;
default:
/* XXX: Possible error condition here? */
break;
}
return (m);
}
/* Given pointer coordinates, return the screen the pointer is on.
*
* Perhaps most useful with $[pointer.screen]
*/
const char *
FScreenOfPointerXY(int x, int y)
{
struct monitor *m;
m = FindScreenOfXY(x, y);
return (m != NULL) ? m->si->name : "unknown";
}
/* Returns the specified screens geometry rectangle. screen can be a screen
* number or any of the values FSCREEN_GLOBAL, FSCREEN_CURRENT,
* FSCREEN_PRIMARY or FSCREEN_XYPOS. The arg union only has a meaning for
* FSCREEN_CURRENT and FSCREEN_XYARG. For FSCREEN_CURRENT its mouse_ev member
* may be given. It is tried to find out the pointer position from the event
* first before querying the pointer. For FSCREEN_XYPOS the xpos member is used
* to fetch the x/y position of the point on the screen. If arg is NULL, the
* position 0 0 is assumed instead.
*
* Any of the arguments arg, x, y, w and h may be NULL.
*
* FSCREEN_GLOBAL: return the global screen dimensions
* FSCREEN_CURRENT: return dimensions of the screen with the pointer
* FSCREEN_PRIMARY: return the primary screen dimensions
* FSCREEN_XYPOS: return dimensions of the screen with the given coordinates
* FSCREEN_BY_NAME: return dimensions of the screen with the given name
* of of the primary screen if arg.name is NULL.
*
* The function returns False if the global screen was returned and more than
* one screen is configured. Otherwise it returns True.
*/
Bool FScreenGetScrRect(fscreen_scr_arg *arg, fscreen_scr_t screen,
int *x, int *y, int *w, int *h)
{
struct monitor *m = FindScreen(arg, screen);
if (m == NULL) {
fvwm_debug(__func__, "m is NULL, using global screen\n");
monitor_refresh_global();
m = monitor_global;
}
if (x)
*x = m->si->x;
if (y)
*y = m->si->y;
if (w)
*w = m->si->w;
if (h)
*h = m->si->h;
return !((monitor_get_count() > 1) && m == monitor_global);
}
/* Translates the coodinates *x *y from the screen specified by arg_src and
* screen_src to coordinates on the screen specified by arg_dest and
* screen_dest. (see FScreenGetScrRect for more details). */
void FScreenTranslateCoordinates(
fscreen_scr_arg *arg_src, fscreen_scr_t screen_src,
fscreen_scr_arg *arg_dest, fscreen_scr_t screen_dest,
int *x, int *y)
{
int x_src;
int y_src;
int x_dest;
int y_dest;
FScreenGetScrRect(arg_src, screen_src, &x_src, &y_src, NULL, NULL);
FScreenGetScrRect(arg_dest, screen_dest, &x_dest, &y_dest, NULL, NULL);
if (x)
{
*x = *x + x_src - x_dest;
}
if (y)
{
*y = *y + y_src - y_dest;
}
return;
}
/* Arguments work exactly like for FScreenGetScrRect() */
int FScreenClipToScreen(fscreen_scr_arg *arg, fscreen_scr_t screen,
int *x, int *y, int w, int h)
{
int sx = 0;
int sy = 0;
int sw = 0;
int sh = 0;
int lx = (x) ? *x : 0;
int ly = (y) ? *y : 0;
int x_grav = GRAV_POS;
int y_grav = GRAV_POS;
FScreenGetScrRect(arg, screen, &sx, &sy, &sw, &sh);
if (lx + w > sx + sw)
{
lx = sx + sw - w;
x_grav = GRAV_NEG;
}
if (ly + h > sy + sh)
{
ly = sy + sh - h;
y_grav = GRAV_NEG;
}
if (lx < sx)
{
lx = sx;
x_grav = GRAV_POS;
}
if (ly < sy)
{
ly = sy;
y_grav = GRAV_POS;
}
if (x)
{
*x = lx;
}
if (y)
{
*y = ly;
}
return grav_matrix[y_grav][x_grav];
}
/* Arguments work exactly like for FScreenGetScrRect() */
void FScreenCenterOnScreen(fscreen_scr_arg *arg, fscreen_scr_t screen,
int *x, int *y, int w, int h)
{
int sx = 0;
int sy = 0;
int sw = 0;
int sh = 0;
int lx;
int ly;
FScreenGetScrRect(arg, screen, &sx, &sy, &sw, &sh);
lx = (sw - w) / 2;
ly = (sh - h) / 2;
if (lx < 0)
lx = 0;
if (ly < 0)
ly = 0;
lx += sx;
ly += sy;
if (x)
{
*x = lx;
}
if (y)
{
*y = ly;
}
}
void FScreenGetResistanceRect(
int wx, int wy, unsigned int ww, unsigned int wh, int *x0, int *y0,
int *x1, int *y1)
{
fscreen_scr_arg arg;
arg.xypos.x = wx + ww / 2;
arg.xypos.y = wy + wh / 2;
FScreenGetScrRect(&arg, FSCREEN_XYPOS, x0, y0, x1, y1);
*x1 += *x0;
*y1 += *y0;
}
/* Arguments work exactly like for FScreenGetScrRect() */
Bool FScreenIsRectangleOnScreen(fscreen_scr_arg *arg, fscreen_scr_t screen,
rectangle *rec)
{
int sx = 0;
int sy = 0;
int sw = 0;
int sh = 0;
FScreenGetScrRect(arg, screen, &sx, &sy, &sw, &sh);
return (rec->x + rec->width > sx && rec->x < sx + sw &&
rec->y + rec->height > sy && rec->y < sy + sh) ? True : False;
}
/*
* FScreenParseGeometry
* Does the same as XParseGeometry, but handles additional "@scr".
* Since it isn't safe to define "ScreenValue" constant (actual values
* of other "XXXValue" are specified in Xutil.h, not by us, so there can
* be a clash). *_screen_return is set to the screen's name or NULL if no
* present in parse_string.
*
*/
int FScreenParseGeometryWithScreen(
char *parsestring, int *x_return, int *y_return,
unsigned int *width_return, unsigned int *height_return,
char **screen_return)
{
char *geom_str;
int ret;
char *at;
if (screen_return != NULL)
{
*screen_return = NULL;
}
/* Safety net */
if (parsestring == NULL || *parsestring == '\0')
{
return 0;
}
/* No screen specified; parse geometry as standard. */
at = strchr(parsestring, '@');
if (at == NULL)
{
geom_str = parsestring;
}
else
{
/* If the geometry specification contains an '@' symbol, assume
* the screen is specified. This must be the name of the
* monitor in question.
*/
int atpos;
if (screen_return != NULL)
{
*screen_return = at + 1;
}
atpos = (int)(at - parsestring);
geom_str = fxstrdup(parsestring);
geom_str[atpos] = 0;
}
/* Do the parsing */
ret = XParseGeometry(
geom_str, x_return, y_return, width_return, height_return);
if (geom_str != parsestring)
{
free(geom_str);
}
return ret;
}
/* Same as above, but dump screen return value to keep compatible with the X
* function. */
int FScreenParseGeometry(
char *parsestring, int *x_return, int *y_return,
unsigned int *width_return, unsigned int *height_return)
{
struct monitor *m = monitor_get_current();
int rc, x, y, w, h;
x = 0;
y = 0;
w = monitor_get_all_widths();
h = monitor_get_all_heights();
{
char *scr;
rc = FScreenParseGeometryWithScreen(
parsestring, x_return, y_return, width_return,
height_return, &scr);
m = monitor_resolve_name(scr);
if (m == NULL)
{
/* fall back to current screen */
m = monitor_get_current();
}
x = m->si->x;
y = m->si->y;
w = m->si->w;
h = m->si->h;
}
/* adapt geometry to selected screen */
if (rc & XValue)
{
if (rc & XNegative)
*x_return -= (monitor_get_all_widths() - w - x);
else
*x_return += x;
}
if (rc & YValue)
{
if (rc & YNegative)
*y_return -= (monitor_get_all_heights() - h - y);
else
*y_return += y;
}
return rc;
}
/* FScreenGetGeometry
* Parses the geometry in a form: XGeometry[@screen], i.e.
* [=][<width>{xX}<height>][{+-}<xoffset>{+-}<yoffset>][@<screen>]
* where <screen> is either a number or "G" (global) "C" (current)
* or "P" (primary)
*
* Args:
* parsestring, x_r, y_r, w_r, h_r the same as in XParseGeometry
* hints window hints structure, may be NULL
* flags bitmask of allowed flags (XValue, WidthValue, XNegative...)
*
* Note1:
* hints->width and hints->height will be used to calc negative geometry
* if width/height isn't specified in the geometry itself.
*
* Note2:
* This function's behaviour is crafted to sutisfy/emulate the
* FvwmWinList::MakeMeWindow()'s behaviour.
*
* Note3:
* A special value of `flags' when [XY]Value are there but [XY]Negative
* aren't, means that in case of negative geometry specification
* x_r/y_r values will be promoted to the screen border, but w/h
* wouldn't be subtracted, so that the program can do x-=w later
* ([XY]Negative *will* be returned, albeit absent in `flags').
* This option is supposed for proggies like FvwmButtons, which
* receive geometry specification long before they are able to actually
* use it (and which calculate w/h themselves).
* (The same effect can't be obtained with omitting {Width,Height}Value
* in the flags, since the app may wish to get the dimensions but apply
* some constraints later (as FvwmButtons do, BTW...).)
* This option can be also useful in cases where dimensions are
* specified not in pixels but in some other units (e.g., charcells).
*/
int FScreenGetGeometry(
char *parsestring, int *x_return, int *y_return,
int *width_return, int *height_return, XSizeHints *hints, int flags)
{
fscreen_scr_arg arg;
int ret;
int saved;
int x, y;
unsigned int w = 0, h = 0;
int grav, x_grav, y_grav;
int scr_x, scr_y;
int scr_w, scr_h;
/* I. Do the parsing and strip off extra bits */
{
char *scr;
ret = FScreenParseGeometryWithScreen(
parsestring, &x, &y, &w, &h, &scr);
saved = ret & (XNegative | YNegative);
ret &= flags;
arg.mouse_ev = NULL;
arg.name = scr;
FScreenGetScrRect(
&arg, FSCREEN_BY_NAME, &scr_x, &scr_y, &scr_w, &scr_h);
}
/* II. Interpret and fill in the values */
/* Fill in dimensions for future negative calculations if
* omitted/forbidden */
/* Maybe should use *x_return,*y_return if hints==NULL?
* Unreliable... */
if (hints != NULL && hints->flags & PSize)
{
if ((ret & WidthValue) == 0)
{
w = hints->width;
}
if ((ret & HeightValue) == 0)
{
h = hints->height;
}
}
else
{
/* This branch is required for case when size *is* specified,
* but masked off */
if ((ret & WidthValue) == 0)
{
w = 0;
}
if ((ret & HeightValue) == 0)
{
h = 0;
}
}
/* Advance coords to the screen... */
x += scr_x;
y += scr_y;
/* ...and process negative geometries */
if (saved & XNegative)
{
x += scr_w;
}
if (saved & YNegative)
{
y += scr_h;
}
if (ret & XNegative)
{
x -= w;
}
if (ret & YNegative)
{
y -= h;
}
/* Restore negative bits */
ret |= saved;
/* Guess orientation */
x_grav = (ret & XNegative)? GRAV_NEG : GRAV_POS;
y_grav = (ret & YNegative)? GRAV_NEG : GRAV_POS;
grav = grav_matrix[y_grav][x_grav];
/* Return the values */
if (ret & XValue)
{
*x_return = x;
if (hints != NULL)
{
hints->x = x;
}
}
if (ret & YValue)
{
*y_return = y;
if (hints != NULL)
{
hints->y = y;
}
}
if (ret & WidthValue)
{
*width_return = w;
if (hints != NULL)
{
hints->width = w;
}
}
if (ret & HeightValue)
{
*height_return = h;
if (hints != NULL)
{
hints->height = h;
}
}
if (1 /*flags & GravityValue*/ && grav != DEFAULT_GRAVITY)
{
if (hints != NULL && hints->flags & PWinGravity)
{
hints->win_gravity = grav;
}
}
if (hints != NULL && ret & XValue && ret & YValue)
hints->flags |= USPosition;
return ret;
}
/* FScreenMangleScreenIntoUSPosHints
* A hack to mangle the screen number into the XSizeHints structure.
* If the USPosition flag is set, hints->x is set to the magic number and
* hints->y is set to the screen number. If the USPosition flag is clear,
* x and y are set to zero.
*
* Note: This is a *hack* to allow modules to specify the target screen for
* their windows and have the StartsOnScreen style set for them at the same
* time. Do *not* rely on the mechanism described above.
*/
void FScreenMangleScreenIntoUSPosHints(fscreen_scr_t screen, XSizeHints *hints)
{
if (hints->flags & USPosition)
{
hints->x = FSCREEN_MANGLE_USPOS_HINTS_MAGIC;
hints->y = (short)screen;
}
else
{
hints->x = 0;
hints->y = 0;
}
return;
}
/* FScreenMangleScreenIntoUSPosHints
* A hack to mangle the screen number into the XSizeHints structure.
* If the USPosition flag is set, hints->x is set to the magic number and
* hints->y is set to the screen spec. If the USPosition flag is clear,
* x and y are set to zero.
*
* Note: This is a *hack* to allow modules to specify the target screen for
* their windows and have the StartsOnScreen style set for them at the same
* time. Do *not* rely on the mechanism described above.
*/
int FScreenFetchMangledScreenFromUSPosHints(XSizeHints *hints)
{
int screen;
if ((hints->flags & USPosition) &&
hints->x == FSCREEN_MANGLE_USPOS_HINTS_MAGIC)
{
screen = hints->y;
} else
screen = 0;
return screen;
}
|