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
|
/*
* Copyright (C) 2002 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "sn-monitor.h"
#include "sn-internals.h"
#include "sn-xmessages.h"
#include <sys/time.h>
struct SnMonitorContext
{
int refcount;
SnDisplay *display;
int screen;
SnMonitorEventFunc event_func;
void *event_func_data;
SnFreeFunc free_data_func;
/* a context doesn't get events for sequences
* started prior to context creation
*/
int creation_serial;
};
struct SnMonitorEvent
{
int refcount;
SnMonitorEventType type;
SnMonitorContext *context;
SnStartupSequence *sequence;
};
struct SnStartupSequence
{
int refcount;
SnDisplay *display;
int screen;
char *id;
char *name;
char *description;
char *wmclass;
int workspace;
Time timestamp;
char *binary_name;
char *icon_name;
unsigned int completed : 1;
unsigned int canceled : 1;
unsigned int timestamp_set : 1;
int creation_serial;
struct timeval initiation_time;
};
static SnList *context_list = NULL;
static SnList *sequence_list = NULL;
static int next_sequence_serial = 0;
static void xmessage_func (SnDisplay *display,
const char *message_type,
const char *message,
void *user_data);
/**
* sn_monitor_context_new:
* @display: an #SnDisplay
* @screen: an X screen number
* @event_func: function to call when an event is received
* @event_func_data: extra data to pass to @event_func
* @free_data_func: function to free @event_func_data when the context is freed
*
* Creates a new context for monitoring startup sequences. Normally
* only launch feedback indicator applications such as the window
* manager or task manager would use #SnMonitorContext.
* #SnLauncherContext and #SnLauncheeContext are more often used by
* applications.
*
* To detect startup sequence initiations, PropertyChangeMask must be
* selected on all root windows for a display. libsn does not do this
* for you because it's pretty likely to mess something up. So you
* have to do it yourself in programs that use #SnMonitorContext.
*
* Return value: a new #SnMonitorContext
**/
SnMonitorContext*
sn_monitor_context_new (SnDisplay *display,
int screen,
SnMonitorEventFunc event_func,
void *event_func_data,
SnFreeFunc free_data_func)
{
SnMonitorContext *context;
context = sn_new0 (SnMonitorContext, 1);
context->refcount = 1;
context->event_func = event_func;
context->event_func_data = event_func_data;
context->free_data_func = free_data_func;
context->display = display;
sn_display_ref (context->display);
context->screen = screen;
if (context_list == NULL)
context_list = sn_list_new ();
if (sn_list_empty (context_list))
sn_internal_add_xmessage_func (display,
screen,
"_NET_STARTUP_INFO",
"_NET_STARTUP_INFO_BEGIN",
xmessage_func,
NULL, NULL);
sn_list_prepend (context_list, context);
/* We get events for serials >= creation_serial */
context->creation_serial = next_sequence_serial;
return context;
}
/**
* sn_monitor_context_ref:
* @context: an #SnMonitorContext
*
* Increments the reference count on @context.
*
**/
void
sn_monitor_context_ref (SnMonitorContext *context)
{
context->refcount += 1;
}
/**
* sn_monitor_context_unref:
* @context: an #SnMonitorContext
*
* Decrements the reference count on @context and frees the
* context if the count reaches 0.
**/
void
sn_monitor_context_unref (SnMonitorContext *context)
{
context->refcount -= 1;
if (context->refcount == 0)
{
sn_list_remove (context_list, context);
if (sn_list_empty (context_list))
sn_internal_remove_xmessage_func (context->display,
context->screen,
"_NET_STARTUP_INFO",
xmessage_func,
NULL);
if (context->free_data_func)
(* context->free_data_func) (context->event_func_data);
sn_display_unref (context->display);
sn_free (context);
}
}
void
sn_monitor_event_ref (SnMonitorEvent *event)
{
event->refcount += 1;
}
void
sn_monitor_event_unref (SnMonitorEvent *event)
{
event->refcount -= 1;
if (event->refcount == 0)
{
if (event->context)
sn_monitor_context_unref (event->context);
if (event->sequence)
sn_startup_sequence_unref (event->sequence);
sn_free (event);
}
}
SnMonitorEvent*
sn_monitor_event_copy (SnMonitorEvent *event)
{
SnMonitorEvent *copy;
copy = sn_new0 (SnMonitorEvent, 1);
copy->refcount = 1;
copy->type = event->type;
copy->context = event->context;
if (copy->context)
sn_monitor_context_ref (copy->context);
copy->sequence = event->sequence;
if (copy->sequence)
sn_startup_sequence_ref (copy->sequence);
return copy;
}
SnMonitorEventType
sn_monitor_event_get_type (SnMonitorEvent *event)
{
return event->type;
}
SnStartupSequence*
sn_monitor_event_get_startup_sequence (SnMonitorEvent *event)
{
return event->sequence;
}
SnMonitorContext*
sn_monitor_event_get_context (SnMonitorEvent *event)
{
return event->context;
}
void
sn_startup_sequence_ref (SnStartupSequence *sequence)
{
sequence->refcount += 1;
}
void
sn_startup_sequence_unref (SnStartupSequence *sequence)
{
sequence->refcount -= 1;
if (sequence->refcount == 0)
{
sn_free (sequence->id);
sn_free (sequence->name);
sn_free (sequence->description);
sn_free (sequence->wmclass);
sn_free (sequence->binary_name);
sn_free (sequence->icon_name);
sn_display_unref (sequence->display);
sn_free (sequence);
}
}
const char*
sn_startup_sequence_get_id (SnStartupSequence *sequence)
{
return sequence->id;
}
sn_bool_t
sn_startup_sequence_get_completed (SnStartupSequence *sequence)
{
return sequence->completed;
}
const char*
sn_startup_sequence_get_name (SnStartupSequence *sequence)
{
return sequence->name;
}
const char*
sn_startup_sequence_get_description (SnStartupSequence *sequence)
{
return sequence->description;
}
int
sn_startup_sequence_get_workspace (SnStartupSequence *sequence)
{
return sequence->workspace;
}
Time
sn_startup_sequence_get_timestamp (SnStartupSequence *sequence)
{
if (!sequence->timestamp_set)
{
fprintf (stderr,
"libsn: Buggy startup-notification launcher! No timestamp!\n");
/* Unfortunately, all values are valid; let's just return -1 */
return -1;
}
else
return sequence->timestamp;
}
const char*
sn_startup_sequence_get_wmclass (SnStartupSequence *sequence)
{
return sequence->wmclass;
}
const char*
sn_startup_sequence_get_binary_name (SnStartupSequence *sequence)
{
return sequence->binary_name;
}
const char*
sn_startup_sequence_get_icon_name (SnStartupSequence *sequence)
{
return sequence->icon_name;
}
int
sn_startup_sequence_get_screen (SnStartupSequence *sequence)
{
return sequence->screen;
}
/**
* sn_startup_sequence_get_initiated_time:
* @sequence: an #SnStartupSequence
* @tv_sec: seconds as in struct timeval
* @tv_usec: microseconds as struct timeval
*
* When a startup sequence is first monitored, libstartup-notification
* calls gettimeofday() and records the time, this function
* returns that recorded time.
*
**/
void
sn_startup_sequence_get_initiated_time (SnStartupSequence *sequence,
long *tv_sec,
long *tv_usec)
{
if (tv_sec)
*tv_sec = sequence->initiation_time.tv_sec;
if (tv_usec)
*tv_usec = sequence->initiation_time.tv_usec;
}
/**
* sn_startup_sequence_get_last_active_time:
* @sequence: an #SnStartupSequence
* @tv_sec: seconds as in struct timeval
* @tv_usec: microseconds as in struct timeval
*
* Returns the last time we had evidence the startup was active.
* This function should be used to decide whether a sequence
* has timed out.
*
**/
void
sn_startup_sequence_get_last_active_time (SnStartupSequence *sequence,
long *tv_sec,
long *tv_usec)
{
/* for now the same as get_initiated_time */
if (tv_sec)
*tv_sec = sequence->initiation_time.tv_sec;
if (tv_usec)
*tv_usec = sequence->initiation_time.tv_usec;
}
void
sn_startup_sequence_complete (SnStartupSequence *sequence)
{
char *keys[2];
char *vals[2];
char *message;
if (sequence->id == NULL)
return;
if (sequence->screen < 0)
return;
keys[0] = "ID";
keys[1] = NULL;
vals[0] = sequence->id;
vals[1] = NULL;
message = sn_internal_serialize_message ("remove",
(const char**) keys,
(const char **) vals);
sn_internal_broadcast_xmessage (sequence->display,
sequence->screen,
"_NET_STARTUP_INFO",
"_NET_STARTUP_INFO_BEGIN",
message);
sn_free (message);
}
static SnStartupSequence*
sn_startup_sequence_new (SnDisplay *display)
{
SnStartupSequence *sequence;
sequence = sn_new0 (SnStartupSequence, 1);
sequence->refcount = 1;
sequence->creation_serial = next_sequence_serial;
++next_sequence_serial;
sequence->id = NULL;
sequence->display = display;
sn_display_ref (display);
sequence->screen = -1; /* not set */
sequence->workspace = -1; /* not set */
sequence->timestamp = 0;
sequence->timestamp_set = FALSE;
sequence->initiation_time.tv_sec = 0;
sequence->initiation_time.tv_usec = 0;
gettimeofday (&sequence->initiation_time, NULL);
return sequence;
}
typedef struct
{
SnMonitorEvent *base_event;
SnList *events;
} CreateContextEventsData;
static sn_bool_t
create_context_events_foreach (void *value,
void *data)
{
/* Make a list of events holding a ref to the context they'll go to,
* for reentrancy robustness
*/
SnMonitorContext *context = value;
CreateContextEventsData *ced = data;
/* Don't send events for startup sequences initiated before the
* context was created
*/
if (ced->base_event->sequence->creation_serial >=
context->creation_serial)
{
SnMonitorEvent *copy;
copy = sn_monitor_event_copy (ced->base_event);
copy->context = context;
sn_monitor_context_ref (copy->context);
sn_list_prepend (ced->events, copy);
}
return TRUE;
}
static sn_bool_t
dispatch_event_foreach (void *value,
void *data)
{
SnMonitorEvent *event = value;
/* Dispatch and free events */
if (event->context->event_func)
(* event->context->event_func) (event,
event->context->event_func_data);
sn_monitor_event_unref (event);
return TRUE;
}
static sn_bool_t
filter_event (SnMonitorEvent *event)
{
sn_bool_t retval;
retval = FALSE;
/* Filter out duplicate events and update flags */
switch (event->type)
{
case SN_MONITOR_EVENT_CANCELED:
if (event->sequence->canceled)
{
retval = TRUE;
}
else
{
event->sequence->canceled = TRUE;
}
break;
case SN_MONITOR_EVENT_COMPLETED:
if (event->sequence->completed)
{
retval = TRUE;
}
else
{
event->sequence->completed = TRUE;
}
break;
default:
break;
}
return retval;
}
static SnStartupSequence*
add_sequence (SnDisplay *display)
{
SnStartupSequence *sequence;
sequence =
sn_startup_sequence_new (display);
if (sequence)
{
sn_startup_sequence_ref (sequence); /* ref held by sequence list */
if (sequence_list == NULL)
sequence_list = sn_list_new ();
sn_list_prepend (sequence_list, sequence);
}
return sequence;
}
static void
remove_sequence (SnStartupSequence *sequence)
{
sn_list_remove (sequence_list, sequence);
sn_startup_sequence_unref (sequence);
}
static void
dispatch_monitor_event (SnDisplay *display,
SnMonitorEvent *event)
{
if (event->type == SN_MONITOR_EVENT_INITIATED)
{
if (event->sequence == NULL)
event->sequence = add_sequence (display);
}
if (event->sequence != NULL &&
!filter_event (event))
{
CreateContextEventsData cced;
cced.base_event = event;
cced.events = sn_list_new ();
sn_list_foreach (context_list, create_context_events_foreach,
&cced);
sn_list_foreach (cced.events, dispatch_event_foreach, NULL);
/* values in the events list freed on dispatch */
sn_list_free (cced.events);
/* remove from sequence list */
if (event->type == SN_MONITOR_EVENT_COMPLETED)
remove_sequence (event->sequence);
}
}
sn_bool_t
sn_internal_monitor_process_event (SnDisplay *display)
{
sn_bool_t retval;
if (context_list == NULL ||
sn_list_empty (context_list))
return FALSE; /* no one cares */
retval = FALSE;
return retval;
}
typedef struct
{
SnDisplay *display;
const char *id;
SnStartupSequence *found;
} FindSequenceByIdData;
static sn_bool_t
find_sequence_by_id_foreach (void *value,
void *data)
{
SnStartupSequence *sequence = value;
FindSequenceByIdData *fsd = data;
if (strcmp (sequence->id, fsd->id) == 0 &&
sn_internal_display_get_id (sequence->display) ==
sn_internal_display_get_id (fsd->display))
{
fsd->found = sequence;
return FALSE;
}
return TRUE;
}
static SnStartupSequence*
find_sequence_for_id (SnDisplay *display,
const char *id)
{
FindSequenceByIdData fsd;
if (sequence_list == NULL)
return NULL;
fsd.display = display;
fsd.id = id;
fsd.found = NULL;
sn_list_foreach (sequence_list, find_sequence_by_id_foreach, &fsd);
return fsd.found;
}
static sn_bool_t
do_xmessage_event_foreach (void *value,
void *data)
{
SnMonitorEvent *event = value;
SnDisplay *display = data;
dispatch_monitor_event (display, event);
return TRUE;
}
static sn_bool_t
unref_event_foreach (void *value,
void *data)
{
sn_monitor_event_unref (value);
return TRUE;
}
static void
xmessage_func (SnDisplay *display,
const char *message_type,
const char *message,
void *user_data)
{
/* assert (strcmp (message_type, KDE_STARTUP_INFO_ATOM) == 0); */
char *prefix;
char **names;
char **values;
int i;
const char *launch_id;
SnStartupSequence *sequence;
SnList *events;
prefix = NULL;
names = NULL;
values = NULL;
if (!sn_internal_unserialize_message (message, &prefix, &names, &values))
return;
launch_id = NULL;
i = 0;
while (names[i])
{
if (strcmp (names[i], "ID") == 0)
{
launch_id = values[i];
break;
}
++i;
}
events = sn_list_new ();
if (launch_id == NULL)
goto out;
sequence = find_sequence_for_id (display, launch_id);
if (strcmp (prefix, "new") == 0)
{
if (sequence == NULL)
{
SnMonitorEvent *event;
char *time_str;
sequence = add_sequence (display);
if (sequence == NULL)
goto out;
sequence->id = sn_internal_strdup (launch_id);
/* Current spec says timestamp is part of the startup id; so we need
* to get the timestamp here if the launcher is using the current spec
*/
time_str = sn_internal_find_last_occurrence (sequence->id, "_TIME");
if (time_str != NULL)
{
/* Skip past the "_TIME" part */
time_str += 5;
sequence->timestamp = sn_internal_string_to_ulong (time_str);
sequence->timestamp_set = TRUE;
}
event = sn_new (SnMonitorEvent, 1);
event->refcount = 1;
event->type = SN_MONITOR_EVENT_INITIATED;
event->context = NULL;
event->sequence = sequence; /* ref from add_sequence goes here */
sn_list_append (events, event);
}
}
if (sequence == NULL)
goto out;
if (strcmp (prefix, "change") == 0 ||
strcmp (prefix, "new") == 0)
{
sn_bool_t changed = FALSE;
i = 0;
while (names[i])
{
if (strcmp (names[i], "BIN") == 0)
{
if (sequence->binary_name == NULL)
{
sequence->binary_name = sn_internal_strdup (values[i]);
changed = TRUE;
}
}
else if (strcmp (names[i], "NAME") == 0)
{
if (sequence->name == NULL)
{
sequence->name = sn_internal_strdup (values[i]);
changed = TRUE;
}
}
else if (strcmp (names[i], "SCREEN") == 0)
{
if (sequence->screen < 0)
{
int n;
n = atoi (values[i]);
if (n >= 0 && n < sn_internal_display_get_screen_number (sequence->display))
{
sequence->screen = n;
changed = TRUE;
}
}
}
else if (strcmp (names[i], "DESCRIPTION") == 0)
{
if (sequence->description == NULL)
{
sequence->description = sn_internal_strdup (values[i]);
changed = TRUE;
}
}
else if (strcmp (names[i], "ICON") == 0)
{
if (sequence->icon_name == NULL)
{
sequence->icon_name = sn_internal_strdup (values[i]);
changed = TRUE;
}
}
else if (strcmp (names[i], "DESKTOP") == 0)
{
int workspace;
workspace = sn_internal_string_to_ulong (values[i]);
sequence->workspace = workspace;
changed = TRUE;
}
else if (strcmp (names[i], "TIMESTAMP") == 0 &&
!sequence->timestamp_set)
{
/* Old version of the spec says that the timestamp was
* sent as part of a TIMESTAMP message. We try to
* handle that to enable backwards compatibility with
* older launchers.
*/
Time timestamp;
timestamp = sn_internal_string_to_ulong (values[i]);
sequence->timestamp = timestamp;
sequence->timestamp_set = TRUE;
changed = TRUE;
}
else if (strcmp (names[i], "WMCLASS") == 0)
{
if (sequence->wmclass == NULL)
{
sequence->wmclass = sn_internal_strdup (values[i]);
changed = TRUE;
}
}
++i;
}
if (strcmp (prefix, "new") == 0)
{
if (sequence->screen < 0)
{
SnMonitorEvent *event;
event = sn_new (SnMonitorEvent, 1);
event->refcount = 1;
event->type = SN_MONITOR_EVENT_COMPLETED;
event->context = NULL;
event->sequence = sequence;
sn_startup_sequence_ref (sequence);
sn_list_append (events, event);
fprintf (stderr,
"Ending startup notification for %s (%s) because SCREEN "
"field was not provided; this is a bug in the launcher "
"application\n",
sequence->name ? sequence->name : "???",
sequence->binary_name ? sequence->binary_name : "???");
}
}
else if (changed)
{
SnMonitorEvent *event;
event = sn_new (SnMonitorEvent, 1);
event->refcount = 1;
event->type = SN_MONITOR_EVENT_CHANGED;
event->context = NULL;
event->sequence = sequence;
sn_startup_sequence_ref (sequence);
sn_list_append (events, event);
}
}
else if (strcmp (prefix, "remove") == 0)
{
SnMonitorEvent *event;
event = sn_new (SnMonitorEvent, 1);
event->refcount = 1;
event->type = SN_MONITOR_EVENT_COMPLETED;
event->context = NULL;
event->sequence = sequence;
sn_startup_sequence_ref (sequence);
sn_list_append (events, event);
}
sn_list_foreach (events,
do_xmessage_event_foreach,
display);
out:
if (events != NULL)
{
sn_list_foreach (events, unref_event_foreach, NULL);
sn_list_free (events);
}
sn_free (prefix);
sn_internal_strfreev (names);
sn_internal_strfreev (values);
}
|