1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
|
/* battstat A MATE battery meter for laptops.
* Copyright (C) 2000 by Jörgen Pehrson <jp@spektr.eu.org>
* Copyright (C) 2002 Free Software Foundation
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02110-1301, USA.
*
$Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <mate-panel-applet.h>
#include <mate-panel-applet-gsettings.h>
#ifdef HAVE_LIBNOTIFY
#include <libnotify/notify.h>
#endif
#include "battstat.h"
#include "pixmaps.h"
#ifndef gettext_noop
#define gettext_noop(String) (String)
#endif
#define BATTSTAT_SCHEMA "org.mate.panel.applet.battstat"
static gboolean check_for_updates (gpointer data);
static void about_cb( GtkAction *, ProgressData * );
static void help_cb( GtkAction *, ProgressData * );
static const GtkActionEntry battstat_menu_actions [] = {
{ "BattstatProperties", GTK_STOCK_PROPERTIES, N_("_Preferences"),
NULL, NULL,
G_CALLBACK (prop_cb) },
{ "BattstatHelp", GTK_STOCK_HELP, N_("_Help"),
NULL, NULL,
G_CALLBACK (help_cb) },
{ "BattstatAbout", GTK_STOCK_ABOUT, N_("_About"),
NULL, NULL,
G_CALLBACK (about_cb) }
};
#define AC_POWER_STRING _("System is running on AC power")
#define DC_POWER_STRING _("System is running on battery power")
/* The icons for Battery, Critical, AC and Charging */
#if GTK_CHECK_VERSION (3, 0, 0)
static GdkPixbuf *statusimage[STATUS_PIXMAP_NUM];
#else
static GdkPixmap *statusimage[STATUS_PIXMAP_NUM];
static GdkBitmap *statusmask[STATUS_PIXMAP_NUM];
#endif
/* Assuming a horizontal battery, the colour is drawn into it one horizontal
line at a time as a vertical gradient. The following arrays decide where
each horizontal line starts (the length of the lines varies with the
percentage battery life remaining).
*/
static const int pixel_offset_top[]={ 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5 };
static const int pixel_top_length[]={ 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2 };
static const int pixel_offset_bottom[]={ 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 38, 38 };
/* The following array is the colour of each line. The (slightly) varying
colours are what makes for the gradient effect. The 'dark' colours are
used to draw the end of the bar, giving it more of a 3D look. The code
assumes that all of these arrays will have the same number of elements.
*/
static GdkColor green[] = {
{0,0x7A00,0xDB00,0x7000},
{0,0x9100,0xE900,0x8500},
{0,0xA000,0xF100,0x9500},
{0,0x9600,0xEE00,0x8A00},
{0,0x8E00,0xE900,0x8100},
{0,0x8500,0xE500,0x7700},
{0,0x7C00,0xDF00,0x6E00},
{0,0x7300,0xDA00,0x6500},
{0,0x6A00,0xD600,0x5B00},
{0,0x6000,0xD000,0x5100},
{0,0x5600,0xCA00,0x4600},
{0,0x5100,0xC100,0x4200},
};
static GdkColor red[] = {
{0,0xD900,0x7200,0x7400},
{0,0xE600,0x8800,0x8C00},
{0,0xF000,0x9600,0x9A00},
{0,0xEB00,0x8D00,0x9100},
{0,0xE700,0x8300,0x8800},
{0,0xE200,0x7A00,0x7F00},
{0,0xDD00,0x7100,0x7600},
{0,0xD800,0x6700,0x6D00},
{0,0xD300,0x5D00,0x6300},
{0,0xCD00,0x5400,0x5900},
{0,0xC800,0x4900,0x4F00},
{0,0xC100,0x4200,0x4700},
};
static GdkColor yellow[] = {
{0,0xD800,0xD900,0x7200},
{0,0xE600,0xE500,0x8800},
{0,0xF000,0xEF00,0x9600},
{0,0xEB00,0xEA00,0x8D00},
{0,0xE700,0xE600,0x8300},
{0,0xE200,0xE100,0x7A00},
{0,0xDD00,0xDC00,0x7100},
{0,0xD800,0xD700,0x6700},
{0,0xD300,0xD200,0x5D00},
{0,0xCD00,0xCC00,0x5400},
{0,0xC800,0xC600,0x4900},
{0,0xC100,0xBF00,0x4200},
};
static GdkColor orange[] = {
{0,0xD900,0xAD00,0x7200},
{0,0xE600,0xBB00,0x8800},
{0,0xF000,0xC700,0x9600},
{0,0xEB00,0xC000,0x8D00},
{0,0xE700,0xB900,0x8300},
{0,0xE200,0xB300,0x7A00},
{0,0xDD00,0xAB00,0x7100},
{0,0xD800,0xA400,0x6700},
{0,0xD300,0x9E00,0x5D00},
{0,0xCD00,0x9600,0x5400},
{0,0xC800,0x8D00,0x4900},
{0,0xC100,0x8600,0x4200},
};
static GdkColor darkgreen[] = {
{0,0x6500,0xC600,0x5B00},
{0,0x7B00,0xD300,0x6F00},
{0,0x8A00,0xDB00,0x7F00},
{0,0x8000,0xD800,0x7400},
{0,0x7800,0xD400,0x6B00},
{0,0x6F00,0xCF00,0x6200},
{0,0x6600,0xCA00,0x5900},
{0,0x5D00,0xC500,0x5000},
{0,0x5400,0xC100,0x4600},
{0,0x4B00,0xBB00,0x3C00},
{0,0x4100,0xB600,0x3100},
{0,0x3C00,0xAC00,0x2D00},
};
static GdkColor darkorange[] = {
{0,0xC400,0x9700,0x5C00},
{0,0xD000,0xA500,0x7200},
{0,0xDA00,0xB100,0x8000},
{0,0xD500,0xAA00,0x7700},
{0,0xD100,0xA300,0x6D00},
{0,0xCD00,0x9D00,0x6400},
{0,0xC700,0x9600,0x5B00},
{0,0xC300,0x8F00,0x5200},
{0,0xBE00,0x8800,0x4800},
{0,0xB800,0x8100,0x3F00},
{0,0xB300,0x7900,0x3400},
{0,0xAC00,0x7200,0x2D00},
};
static GdkColor darkyellow[] = {
{0,0xC200,0xC400,0x5C00},
{0,0xD000,0xCF00,0x7200},
{0,0xDA00,0xD900,0x8000},
{0,0xD500,0xD400,0x7700},
{0,0xD100,0xD000,0x6D00},
{0,0xCD00,0xCB00,0x6400},
{0,0xC700,0xC600,0x5B00},
{0,0xC300,0xC200,0x5200},
{0,0xBE00,0xBD00,0x4800},
{0,0xB800,0xB700,0x3F00},
{0,0xB300,0xB200,0x3400},
{0,0xAC00,0xAA00,0x2D00},
};
static GdkColor darkred[] = {
{0,0xC900,0x6200,0x6400},
{0,0xD600,0x7800,0x7C00},
{0,0xDA00,0x8000,0x8500},
{0,0xD500,0x7700,0x7B00},
{0,0xD100,0x6D00,0x7200},
{0,0xCD00,0x6400,0x6900},
{0,0xC700,0x5B00,0x6100},
{0,0xC300,0x5200,0x5700},
{0,0xBE00,0x4800,0x4E00},
{0,0xB800,0x3F00,0x4400},
{0,0xB100,0x3200,0x3700},
{0,0xA200,0x3200,0x3700},
};
/* Initialise the global static variables that store our status pixmaps from
their XPM format (as stored in pixmaps.h). This should only be done once
since they are global variables.
*/
static void
initialise_global_pixmaps( void )
{
#if !GTK_CHECK_VERSION (3, 0, 0)
GdkDrawable *defaults;
defaults = gdk_screen_get_root_window( gdk_screen_get_default() );
#endif
statusimage[STATUS_PIXMAP_BATTERY] =
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_xpm);
#else
gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_BATTERY],
NULL, battery_small_xpm );
#endif
statusimage[STATUS_PIXMAP_METER] =
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_pixbuf_new_from_xpm_data ((const char **) battery_small_meter_xpm);
#else
gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_METER],
NULL, battery_small_meter_xpm );
#endif
statusimage[STATUS_PIXMAP_AC] =
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_pixbuf_new_from_xpm_data ((const char **) ac_small_xpm);
#else
gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_AC],
NULL, ac_small_xpm );
#endif
statusimage[STATUS_PIXMAP_CHARGE] =
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_pixbuf_new_from_xpm_data ((const char **) charge_small_xpm);
#else
gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_CHARGE],
NULL, charge_small_xpm );
#endif
statusimage[STATUS_PIXMAP_WARNING] =
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_pixbuf_new_from_xpm_data ((const char **) warning_small_xpm);
#else
gdk_pixmap_create_from_xpm_d( defaults, &statusmask[STATUS_PIXMAP_WARNING],
NULL, warning_small_xpm );
#endif
}
#if !GTK_CHECK_VERSION (3, 0, 0)
/* For non-truecolour displays, each GdkColor has to have a palette entry
allocated for it. This should only be done once for the entire display.
*/
static void
allocate_battery_colours( void )
{
GdkColormap *colourmap;
int i;
colourmap = gdk_colormap_get_system();
/* assumed: all the colour arrays have the same number of elements */
for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
{
gdk_colormap_alloc_color( colourmap, &darkorange[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &darkyellow[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &darkred[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &darkgreen[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &orange[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &yellow[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &red[i], FALSE, TRUE );
gdk_colormap_alloc_color( colourmap, &green[i], FALSE, TRUE );
}
}
#endif
/* Our backends may be either event driven or poll-based.
* If they are event driven then we know this the first time we
* receive an event.
*/
static gboolean event_driven = FALSE;
static GSList *instances;
static void
status_change_callback (void)
{
GSList *instance;
for (instance = instances; instance; instance = instance->next)
{
ProgressData *battstat = instance->data;
if (battstat->timeout_id)
{
g_source_remove (battstat->timeout_id);
battstat->timeout_id = 0;
}
check_for_updates (battstat);
}
event_driven = TRUE;
}
/* The following two functions keep track of how many instances of the applet
are currently running. When the first instance is started, some global
initialisation is done. When the last instance exits, cleanup occurs.
The teardown code here isn't entirely complete (for example, it doesn't
deallocate the GdkColors or free the GdkPixmaps. This is OK so long
as the process quits immediately when the last applet is removed (which
it does.)
*/
static const char *
static_global_initialisation (int no_hal, ProgressData *battstat)
{
gboolean first_time;
const char *err;
first_time = !instances;
instances = g_slist_prepend (instances, battstat);
if (!first_time)
return NULL;
#if !GTK_CHECK_VERSION (3, 0, 0)
allocate_battery_colours();
#endif
initialise_global_pixmaps();
err = power_management_initialise (no_hal, status_change_callback);
return err;
}
static void
static_global_teardown (ProgressData *battstat)
{
instances = g_slist_remove (instances, battstat);
/* remaining instances... */
if (instances)
return;
/* instances == 0 */
power_management_cleanup();
}
/* Pop up an error dialog on the same screen as 'applet' saying 'msg'.
*/
static void
battstat_error_dialog( GtkWidget *applet, const char *msg )
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new( NULL, 0, GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK, "%s", msg);
gtk_window_set_screen( GTK_WINDOW (dialog),
gtk_widget_get_screen (GTK_WIDGET (applet)) );
#if GTK_CHECK_VERSION (3, 0, 0)
g_signal_connect_swapped( G_OBJECT (dialog), "response",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (dialog) );
#else
g_signal_connect_swapped( GTK_OBJECT (dialog), "response",
G_CALLBACK (gtk_widget_destroy),
GTK_OBJECT (dialog) );
#endif
gtk_widget_show_all( dialog );
}
/* Format a string describing how much time is left to fully (dis)charge
the battery. The return value must be g_free()d.
*/
static char *
get_remaining (BatteryStatus *info)
{
int hours;
int mins;
hours = info->minutes / 60;
mins = info->minutes % 60;
if (info->on_ac_power && !info->charging)
return g_strdup_printf (_("Battery charged (%d%%)"), info->percent);
else if (info->minutes < 0 && !info->on_ac_power)
return g_strdup_printf (_("Unknown time (%d%%) remaining"), info->percent);
else if (info->minutes < 0 && info->on_ac_power)
return g_strdup_printf (_("Unknown time (%d%%) until charged"), info->percent);
else
if (hours == 0)
if (!info->on_ac_power)
return g_strdup_printf (ngettext (
"%d minute (%d%%) remaining",
"%d minutes (%d%%) remaining",
mins), mins, info->percent);
else
return g_strdup_printf (ngettext (
"%d minute until charged (%d%%)",
"%d minutes until charged (%d%%)",
mins), mins, info->percent);
else if (mins == 0)
if (!info->on_ac_power)
return g_strdup_printf (ngettext (
"%d hour (%d%%) remaining",
"%d hours (%d%%) remaining",
hours), hours, info->percent);
else
return g_strdup_printf (ngettext (
"%d hour until charged (%d%%)",
"%d hours until charged (%d%%)",
hours), hours, info->percent);
else
if (!info->on_ac_power)
/* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes"
* Swap order with "%2$s %2$d %1$s %1$d if needed */
return g_strdup_printf (_("%d %s %d %s (%d%%) remaining"),
hours, ngettext ("hour", "hours", hours),
mins, ngettext ("minute", "minutes", mins),
info->percent);
else
/* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes"
* Swap order with "%2$s %2$d %1$s %1$d if needed */
return g_strdup_printf (_("%d %s %d %s until charged (%d%%)"),
hours, ngettext ("hour", "hours", hours),
mins, ngettext ("minute", "minutes", mins),
info->percent);
}
static gboolean
battery_full_notify (GtkWidget *applet)
{
#ifdef HAVE_LIBNOTIFY
GError *error = NULL;
GdkPixbuf *icon;
gboolean result;
if (!notify_is_initted () && !notify_init (_("Battery Monitor")))
return FALSE;
icon = gtk_icon_theme_load_icon (
gtk_icon_theme_get_default (),
"battery",
48,
GTK_ICON_LOOKUP_USE_BUILTIN,
NULL);
NotifyNotification *n = notify_notification_new (_("Your battery is now fully recharged"), "", /* "battery" */ NULL);
/* XXX: it would be nice to pass this as a named icon */
notify_notification_set_icon_from_pixbuf (n, icon);
g_object_unref (icon);
result = notify_notification_show (n, &error);
if (error)
{
g_warning ("%s", error->message);
g_error_free (error);
}
g_object_unref (G_OBJECT (n));
return result;
#else
return FALSE;
#endif
}
/* Show a dialog notifying the user that their battery is done charging.
*/
static void
battery_full_dialog (GtkWidget *applet)
{
/* first attempt to use libnotify */
if (battery_full_notify (applet))
return;
GtkWidget *dialog, *hbox, *image, *label;
GdkPixbuf *pixbuf;
gchar *new_label;
dialog = gtk_dialog_new_with_buttons (
_("Battery Notice"),
NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,
GTK_RESPONSE_ACCEPT,
NULL);
#if GTK_CHECK_VERSION (3, 0, 0)
g_signal_connect_swapped (G_OBJECT (dialog), "response",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (dialog));
#else
g_signal_connect_swapped (GTK_OBJECT (dialog), "response",
G_CALLBACK (gtk_widget_destroy),
GTK_OBJECT (dialog));
#endif
gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
hbox = gtk_hbox_new (FALSE, 6);
pixbuf = gtk_icon_theme_load_icon (
gtk_icon_theme_get_default (),
"battery",
48,
GTK_ICON_LOOKUP_USE_BUILTIN,
NULL);
image = gtk_image_new_from_pixbuf (pixbuf);
g_object_unref (pixbuf);
gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 6);
new_label = g_strdup_printf (
"<span weight=\"bold\" size=\"larger\">%s</span>",
_("Your battery is now fully recharged"));
label = gtk_label_new (new_label);
g_free (new_label);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 6);
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox);
gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE);
gtk_window_stick (GTK_WINDOW (dialog));
gtk_window_set_skip_pager_hint (GTK_WINDOW (dialog), TRUE);
gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE);
gtk_widget_show_all (dialog);
}
/* Destroy the low battery notification dialog and mark it as such.
*/
static void
battery_low_dialog_destroy( ProgressData *battstat )
{
gtk_widget_destroy( battstat->battery_low_dialog );
battstat->battery_low_dialog = NULL;
battstat->battery_low_label = NULL;
}
/* Determine if suspend is unsupported. For the time being this involves
* distribution-specific magic :(
*/
/* #define HAVE_PMI */
static gboolean
is_suspend_unavailable( void )
{
#ifdef HAVE_PMI
int status;
status = system( "pmi query suspend" );
/* -1 - fail (pmi unavailable?). return 'false' since we don't know.
* 0 - success (can suspend). return 'false' since not unavailable.
* 1 - success (cannot suspend). return 'true' since unavailable.
*/
if( WEXITSTATUS( status ) == 1 )
return TRUE;
else
return FALSE;
#else
return FALSE; /* return 'false' since we don't know. */
#endif
}
/* Update the text label in the battery low dialog.
*/
static void
battery_low_update_text( ProgressData *battstat, BatteryStatus *info )
{
const char *suggest;
gchar *remaining, *new_label;
GtkRequisition size;
/* If we're not displaying the dialog then don't update it. */
if( battstat->battery_low_label == NULL ||
battstat->battery_low_dialog == NULL )
return;
gtk_widget_size_request( GTK_WIDGET( battstat->battery_low_label ), &size );
/* If the label has never been set before, the width will be 0. If it
has been set before (width > 0) then we want to keep the size of
the old widget (to keep the dialog from changing sizes) so we set it
explicitly here.
*/
if( size.width > 0 )
gtk_widget_set_size_request( GTK_WIDGET( battstat->battery_low_label ),
size.width, size.height );
if (info->minutes < 0 && !info->on_ac_power)
{
/* we don't know the remaining time */
remaining = g_strdup_printf (_("You have %d%% of your total battery "
"capacity remaining."), info->percent);
}
else
{
remaining = g_strdup_printf( ngettext(
"You have %d minute of battery power "
"remaining (%d%% of the total capacity).",
"You have %d minutes of battery power "
"remaining (%d%% of the total capacity).",
info->minutes ),
info->minutes,info->percent );
}
if( is_suspend_unavailable() )
/* TRANSLATORS: this is a list, it is left as a single string
* to allow you to make it appear like a list would in your
* locale. This is if the laptop does not support suspend. */
suggest = _("To avoid losing your work:\n"
" \xE2\x80\xA2 plug your laptop into external power, or\n"
" \xE2\x80\xA2 save open documents and shut your laptop down."
);
else
/* TRANSLATORS: this is a list, it is left as a single string
* to allow you to make it appear like a list would in your
* locale. This is if the laptop supports suspend. */
suggest = _("To avoid losing your work:\n"
" \xE2\x80\xA2 suspend your laptop to save power,\n"
" \xE2\x80\xA2 plug your laptop into external power, or\n"
" \xE2\x80\xA2 save open documents and shut your laptop down."
);
new_label = g_strdup_printf(
"<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s",
_("Your battery is running low"), remaining, suggest );
gtk_label_set_markup( battstat->battery_low_label, new_label );
g_free( remaining );
g_free( new_label );
}
/* Show a dialog notifying the user that their battery is running low.
*/
static void
battery_low_dialog( ProgressData *battery, BatteryStatus *info )
{
GtkWidget *hbox, *image, *label;
GtkWidget *vbox;
GdkPixbuf *pixbuf;
/* If the dialog is already displayed then don't display it again. */
if( battery->battery_low_dialog != NULL )
return;
battery->battery_low_dialog = gtk_dialog_new_with_buttons (
_("Battery Notice"),
NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,
GTK_RESPONSE_ACCEPT,
NULL);
gtk_dialog_set_default_response( GTK_DIALOG (battery->battery_low_dialog),
GTK_RESPONSE_ACCEPT );
#if GTK_CHECK_VERSION (3, 0, 0)
g_signal_connect_swapped( G_OBJECT (battery->battery_low_dialog),
#else
g_signal_connect_swapped( GTK_OBJECT (battery->battery_low_dialog),
#endif
"response",
G_CALLBACK (battery_low_dialog_destroy),
battery );
gtk_container_set_border_width (GTK_CONTAINER (battery->battery_low_dialog),
6);
hbox = gtk_hbox_new (FALSE, 6);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
"battery",
48,
GTK_ICON_LOOKUP_USE_BUILTIN,
NULL);
image = gtk_image_new_from_pixbuf (pixbuf);
g_object_unref (pixbuf);
vbox = gtk_vbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 6);
gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0);
label = gtk_label_new ("");
battery->battery_low_label = GTK_LABEL( label );
gtk_label_set_line_wrap( battery->battery_low_label, TRUE );
gtk_label_set_selectable( battery->battery_low_label, TRUE );
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 6);
gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (battery->battery_low_dialog))), hbox);
gtk_window_set_keep_above (GTK_WINDOW (battery->battery_low_dialog), TRUE);
gtk_window_stick (GTK_WINDOW (battery->battery_low_dialog));
gtk_window_set_focus_on_map (GTK_WINDOW (battery->battery_low_dialog),
FALSE);
gtk_window_set_skip_pager_hint (GTK_WINDOW (battery->battery_low_dialog),
TRUE);
battery_low_update_text( battery, info );
gtk_window_set_position (GTK_WINDOW (battery->battery_low_dialog),
GTK_WIN_POS_CENTER);
gtk_widget_show_all (battery->battery_low_dialog);
}
/* Update the text of the tooltip from the provided info.
*/
static void
update_tooltip( ProgressData *battstat, BatteryStatus *info )
{
gchar *powerstring;
gchar *remaining;
gchar *tiptext;
if (info->present)
{
if (info->on_ac_power)
powerstring = AC_POWER_STRING;
else
powerstring = DC_POWER_STRING;
remaining = get_remaining (info);
tiptext = g_strdup_printf ("%s\n%s", powerstring, remaining);
g_free (remaining);
}
else
{
if (info->on_ac_power)
tiptext = g_strdup_printf ("%s\n%s", AC_POWER_STRING,
_("No battery present"));
else
tiptext = g_strdup_printf ("%s\n%s", DC_POWER_STRING,
_("Battery status unknown"));
}
gtk_widget_set_tooltip_text (battstat->applet, tiptext);
g_free (tiptext);
}
#if GTK_CHECK_VERSION (3, 0, 0)
static void
pixbuf_draw_line( GdkPixbuf *pixbuf, GdkColor *colour, int x1, int y1, int x2, int y2)
{
guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
int stride = gdk_pixbuf_get_rowstride (pixbuf);
int channels = gdk_pixbuf_get_n_channels (pixbuf);
guchar r, g, b, a;
int i, n;
r = colour->red >> 8;
g = colour->green >> 8;
b = colour->blue >> 8;
a = 0xFF;
pixels += stride * y1 + 4 * x1;
if (x1 == x2)
{
/* stride = gdk_pixbuf_get_rowstride (pixbuf); */
n = y2 - y1;
}
else if (y1 == y2)
{
stride = gdk_pixbuf_get_n_channels (pixbuf);
n = x2 - x1;
}
else
g_assert_not_reached ();
for (i = 0; i < n; i++)
{
pixels[0] = r;
pixels[1] = g;
pixels[2] = b;
if (channels == 4)
pixels[3] = a;
pixels += stride;
}
}
#endif
/* Redraw the battery meter image.
*/
static void
update_battery_image (ProgressData *battstat, int batt_percent, int batt_time)
{
GdkColor *color, *darkcolor;
#if GTK_CHECK_VERSION (3, 0, 0)
GdkPixbuf *pixbuf;
#else
GdkPixmap *pixmap;
GdkBitmap *pixmask;
#endif
guint progress_value;
gint i, x;
int batt_life;
if (!battstat->showbattery)
return;
batt_life = !battstat->red_value_is_time ? batt_percent : batt_time;
if (batt_life <= battstat->red_val)
{
color = red;
darkcolor = darkred;
}
else if (batt_life <= battstat->orange_val)
{
color = orange;
darkcolor = darkorange;
}
else if (batt_life <= battstat->yellow_val)
{
color = yellow;
darkcolor = darkyellow;
}
else
{
color = green;
darkcolor = darkgreen;
}
#if !GTK_CHECK_VERSION (3, 0, 0)
/* We keep this pixgc allocated so we don't have to alloc/free it every
time. A widget has to be realized before it has a valid ->window so
we do that here for battstat->applet just in case it's not already done.
*/
if( battstat->pixgc == NULL )
{
gtk_widget_realize( battstat->applet );
battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
}
#endif
/* Depending on if the meter is horizontally oriented start out with the
appropriate XPM image (from pixmaps.h)
*/
if (battstat->horizont)
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_gray_xpm);
#else
pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
NULL, battery_gray_xpm );
#endif
else
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) battery_y_gray_xpm);
#else
pixmap = gdk_pixmap_create_from_xpm_d( gtk_widget_get_window (battstat->applet), &pixmask,
NULL, battery_y_gray_xpm );
#endif
/* The core code responsible for painting the battery meter. For each
colour in our gradient array, draw a vertical or horizontal line
depending on the current orientation of the meter.
*/
if (battstat->draintop) {
progress_value = PROGLEN * batt_life / 100.0;
for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
{
#if !GTK_CHECK_VERSION (3, 0, 0)
gdk_gc_set_foreground (battstat->pixgc, &color[i]);
#endif
if (battstat->horizont)
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &color[i], pixel_offset_top[i], i + 2,
#else
gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_top[i], i + 2,
#endif
pixel_offset_top[i] + progress_value, i + 2);
else
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &color[i], i + 2, pixel_offset_top[i],
#else
gdk_draw_line (pixmap, battstat->pixgc, i + 2, pixel_offset_top[i],
#endif
i + 2, pixel_offset_top[i] + progress_value);
}
}
else
{
progress_value = PROGLEN * batt_life / 100.0;
for( i = 0; i < G_N_ELEMENTS( orange ); i++)
{
#if !GTK_CHECK_VERSION (3, 0, 0)
gdk_gc_set_foreground (battstat->pixgc, &color[i]);
#endif
if (battstat->horizont)
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &color[i], pixel_offset_bottom[i], i + 2,
#else
gdk_draw_line (pixmap, battstat->pixgc, pixel_offset_bottom[i], i + 2,
#endif
pixel_offset_bottom[i] - progress_value, i + 2);
else
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &color[i], i + 2,
#else
gdk_draw_line (pixmap, battstat->pixgc, i + 2,
#endif
pixel_offset_bottom[i] - 1, i + 2,
pixel_offset_bottom[i] - progress_value);
}
for( i = 0; i < G_N_ELEMENTS( orange ); i++ )
{
x = pixel_offset_bottom[i] - progress_value - pixel_top_length[i];
if (x < pixel_offset_top[i])
x = pixel_offset_top[i];
if (progress_value < 33)
{
#if !GTK_CHECK_VERSION (3, 0, 0)
gdk_gc_set_foreground (battstat->pixgc, &darkcolor[i]);
#endif
if (battstat->horizont)
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &darkcolor[i],
#else
gdk_draw_line (pixmap, battstat->pixgc,
#endif
pixel_offset_bottom[i] - progress_value - 1,
i + 2, x, i + 2);
else
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line (pixbuf, &darkcolor[i], i + 2,
#else
gdk_draw_line (pixmap, battstat->pixgc, i + 2,
#endif
pixel_offset_bottom[i] - progress_value - 1,
i + 2, x);
}
}
}
/* Store our newly created pixmap into the GtkImage. This results in
the last reference to the old pixmap/mask being dropped.
*/
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_image_set_from_pixbuf( GTK_IMAGE(battstat->battery),
pixbuf);
#else
gtk_image_set_from_pixmap( GTK_IMAGE(battstat->battery),
pixmap, pixmask );
#endif
/* The GtkImage does not assume a reference to the pixmap or mask;
you still need to unref them if you own references. GtkImage will
add its own reference rather than adopting yours.
*/
#if GTK_CHECK_VERSION (3, 0, 0)
g_object_unref( G_OBJECT(pixbuf) );
#else
g_object_unref( G_OBJECT(pixmap) );
g_object_unref( G_OBJECT(pixmask) );
#endif
}
/* Update the text label that either shows the percentage of time left.
*/
static void
update_percent_label( ProgressData *battstat, BatteryStatus *info )
{
gchar *new_label;
if (info->present && battstat->showtext == APPLET_SHOW_PERCENT)
new_label = g_strdup_printf ("%d%%", info->percent);
else if (info->present && battstat->showtext == APPLET_SHOW_TIME)
{
/* Fully charged or unknown (-1) time remaining display none */
if ((info->on_ac_power && info->percent == 100) || info->minutes < 0)
new_label = g_strdup ("");
else
{
int time;
time = info->minutes;
new_label = g_strdup_printf ("%d:%02d", time/60, time%60);
}
}
else
new_label = g_strdup (_("N/A"));
gtk_label_set_text (GTK_LABEL (battstat->percent), new_label);
g_free (new_label);
}
#if !GTK_CHECK_VERSION (3, 0, 0)
/* Utility function to create a copy of a GdkPixmap */
static GdkPixmap *
copy_gdk_pixmap( GdkPixmap *src, GdkGC *gc )
{
gint height, width;
GdkPixmap *dest;
#if GTK_CHECK_VERSION(3, 0, 0)
width = gdk_window_get_width(GDK_WINDOW(src));
height = gdk_window_get_height(GDK_WINDOW(src));
#else
gdk_drawable_get_size(GDK_DRAWABLE(src), &width, &height);
#endif
dest = gdk_pixmap_new( GDK_DRAWABLE( src ), width, height, -1 );
gdk_draw_drawable( GDK_DRAWABLE( dest ), gc, GDK_DRAWABLE( src ),
0, 0, 0, 0, width, height );
return dest;
}
#endif
/* Determine what status icon we ought to be displaying and change the
status icon to display it if it is different from what we are currently
showing.
*/
static void
possibly_update_status_icon( ProgressData *battstat, BatteryStatus *info )
{
StatusPixmapIndex pixmap_index;
int batt_life, last_batt_life;
batt_life = !battstat->red_value_is_time ? info->percent : info->minutes;
last_batt_life = !battstat->red_value_is_time ? battstat->last_batt_life :
battstat->last_minutes;
if( info->on_ac_power )
{
if( info->charging )
pixmap_index = STATUS_PIXMAP_CHARGE;
else
pixmap_index = STATUS_PIXMAP_AC;
}
else /* on battery */
{
if (batt_life > battstat->red_val)
pixmap_index = STATUS_PIXMAP_BATTERY;
else
pixmap_index = STATUS_PIXMAP_WARNING;
}
/* If we are showing the full length battery meter then the status icon
should display static icons. If we are not showing the full meter
then the status icon will show a smaller meter if we are on battery.
*/
if( !battstat->showbattery &&
(pixmap_index == STATUS_PIXMAP_BATTERY ||
pixmap_index == STATUS_PIXMAP_WARNING) )
pixmap_index = STATUS_PIXMAP_METER;
/* Take care of drawing the smaller meter. */
if( pixmap_index == STATUS_PIXMAP_METER &&
(batt_life != last_batt_life ||
battstat->last_pixmap_index != STATUS_PIXMAP_METER) )
{
GdkColor *colour;
#if GTK_CHECK_VERSION (3, 0, 0)
GdkPixbuf *meter;
#else
GdkPixmap *meter;
#endif
guint progress_value;
gint i, x;
#if !GTK_CHECK_VERSION (3, 0, 0)
/* We keep this pixgc allocated so we don't have to alloc/free it every
time. A widget has to be realized before it has a valid ->window so
we do that here for battstat->applet just in case it's not already done.
*/
if( battstat->pixgc == NULL )
{
gtk_widget_realize( battstat->applet );
battstat->pixgc = gdk_gc_new( gtk_widget_get_window (battstat->applet) );
}
#endif
/* Pull in a clean version of the icons so that we don't paint over
top of the same icon over and over. We neglect to free/update the
statusmask here since it will always stay the same.
*/
#if GTK_CHECK_VERSION (3, 0, 0)
meter = gdk_pixbuf_copy ( statusimage[STATUS_PIXMAP_METER]);
#else
meter = copy_gdk_pixmap( statusimage[STATUS_PIXMAP_METER],
battstat->pixgc );
#endif
if (batt_life <= battstat->red_val)
{
colour = red;
}
else if (batt_life <= battstat->orange_val)
{
colour = orange;
}
else if (batt_life <= battstat->yellow_val)
{
colour = yellow;
}
else
{
colour = green;
}
progress_value = 12 * info->percent / 100.0;
for( i = 0; i < 10; i++ )
{
#if !GTK_CHECK_VERSION (3, 0, 0)
gdk_gc_set_foreground( battstat->pixgc, &colour[(i * 13 / 10)] );
#endif
if( i >= 2 && i <= 7 )
x = 17;
else
x = 16;
#if GTK_CHECK_VERSION (3, 0, 0)
pixbuf_draw_line( meter, &colour[(i * 13 / 10)],
#else
gdk_draw_line( meter, battstat->pixgc,
#endif
i + 1, x - progress_value,
i + 1, x );
}
/* force a redraw immediately */
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_image_set_from_pixbuf( GTK_IMAGE (battstat->status), meter);
#else
gtk_image_set_from_pixmap( GTK_IMAGE (battstat->status),
meter, statusmask[STATUS_PIXMAP_METER] );
#endif
/* free our private pixmap copy */
g_object_unref( G_OBJECT( meter ) );
battstat->last_pixmap_index = STATUS_PIXMAP_METER;
}
else if( pixmap_index != battstat->last_pixmap_index )
{
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_image_set_from_pixbuf (GTK_IMAGE (battstat->status),
statusimage[pixmap_index]);
#else
gtk_image_set_from_pixmap (GTK_IMAGE (battstat->status),
statusimage[pixmap_index],
statusmask[pixmap_index]);
#endif
battstat->last_pixmap_index = pixmap_index;
}
}
/* Gets called as a gtk_timeout once per second. Checks for updates and
makes any changes as appropriate.
*/
static gboolean
check_for_updates( gpointer data )
{
ProgressData *battstat = data;
BatteryStatus info;
const char *err;
if (DEBUG) g_print("check_for_updates()\n");
if( (err = power_management_getinfo( &info )) )
battstat_error_dialog( battstat->applet, err );
if (!event_driven)
{
int timeout;
/* if on AC and not event driven scale back the polls to once every 10 */
if (info.on_ac_power)
timeout = 10;
else
timeout = 2;
if (timeout != battstat->timeout)
{
battstat->timeout = timeout;
if (battstat->timeout_id)
g_source_remove (battstat->timeout_id);
battstat->timeout_id = g_timeout_add_seconds (battstat->timeout,
check_for_updates,
battstat);
}
}
possibly_update_status_icon( battstat, &info );
if (!info.on_ac_power &&
battstat->last_batt_life != 1000 &&
(
/* if percentage drops below red_val */
(!battstat->red_value_is_time &&
battstat->last_batt_life > battstat->red_val &&
info.percent <= battstat->red_val) ||
/* if time drops below red_val */
(battstat->red_value_is_time &&
battstat->last_minutes > battstat->red_val &&
info.minutes <= battstat->red_val)
) &&
info.present)
{
/* Warn that battery dropped below red_val */
if(battstat->lowbattnotification)
{
battery_low_dialog(battstat, &info);
if(battstat->beep)
gdk_beep();
}
#if 0
/* FIXME: mate-applets doesn't depend on libmate anymore */
mate_triggers_do ("", NULL, "battstat_applet", "LowBattery", NULL);
#endif
}
if( battstat->last_charging &&
battstat->last_acline_status &&
battstat->last_acline_status!=1000 &&
!info.charging &&
info.on_ac_power &&
info.present &&
info.percent > 99)
{
/* Inform that battery now fully charged */
#if 0
/* FIXME: mate-applets doesn't depend on libmate anymore */
mate_triggers_do ("", NULL, "battstat_applet", "BatteryFull", NULL);
#endif
if(battstat->fullbattnot)
{
battery_full_dialog (battstat->applet);
if (battstat->beep)
gdk_beep();
}
}
/* If the warning dialog is displayed and we just got plugged in then
stop displaying it.
*/
if( battstat->battery_low_dialog && info.on_ac_power )
battery_low_dialog_destroy( battstat );
if( info.on_ac_power != battstat->last_acline_status ||
info.percent != battstat->last_batt_life ||
info.minutes != battstat->last_minutes ||
info.charging != battstat->last_charging )
{
/* Update the tooltip */
update_tooltip( battstat, &info );
/* If the warning dialog box is currently displayed, update that too. */
if( battstat->battery_low_dialog != NULL )
battery_low_update_text( battstat, &info );
}
if( info.percent != battstat->last_batt_life )
{
/* Update the battery meter image */
update_battery_image (battstat, info.percent, info.minutes);
}
if( (battstat->showtext == APPLET_SHOW_PERCENT &&
battstat->last_batt_life != info.percent) ||
(battstat->showtext == APPLET_SHOW_TIME &&
battstat->last_minutes != info.minutes) ||
battstat->last_acline_status != info.on_ac_power ||
battstat->last_present != info.present ||
battstat->refresh_label ) /* set by properties dialog */
{
/* Update the label */
update_percent_label( battstat, &info );
/* done */
battstat->refresh_label = FALSE;
}
battstat->last_charging = info.charging;
battstat->last_batt_life = info.percent;
battstat->last_minutes = info.minutes;
battstat->last_acline_status = info.on_ac_power;
battstat->last_present = info.present;
return TRUE;
}
/* Gets called when the user removes the applet from the panel. Clean up
all instance-specific data and call the global teardown function to
decrease our applet count (and possibly perform global cleanup)
*/
static void
destroy_applet( GtkWidget *widget, ProgressData *battstat )
{
if (DEBUG) g_print("destroy_applet()\n");
if (battstat->prop_win)
gtk_widget_destroy (GTK_WIDGET (battstat->prop_win));
if( battstat->battery_low_dialog )
battery_low_dialog_destroy( battstat );
if (battstat->timeout_id)
g_source_remove (battstat->timeout_id);
#if !GTK_CHECK_VERSION (3, 0, 0)
if( battstat->pixgc )
g_object_unref( G_OBJECT(battstat->pixgc) );
#endif
g_object_unref( G_OBJECT(battstat->status) );
g_object_unref( G_OBJECT(battstat->percent) );
g_object_unref( G_OBJECT(battstat->battery) );
g_free( battstat );
static_global_teardown (battstat);
}
/* Common function invoked by the 'Help' context menu item and the 'Help'
* button in the preferences dialog.
*/
void
battstat_show_help( ProgressData *battstat, const char *section )
{
GError *error = NULL;
char *uri;
if (section)
uri = g_strdup_printf ("help:mate-battstat/%s", section);
else
uri = g_strdup ("help:mate-battstat");
gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET (battstat->applet)),
uri,
gtk_get_current_event_time (),
&error);
g_free (uri);
if( error )
{
char *message;
message = g_strdup_printf( _("There was an error displaying help: %s"),
error->message );
battstat_error_dialog( battstat->applet, message );
g_error_free( error );
g_free( message );
}
}
/* Called when the user selects the 'help' menu item.
*/
static void
help_cb( GtkAction *action, ProgressData *battstat )
{
battstat_show_help( battstat, NULL );
}
/* Called when the user selects the 'about' menu item.
*/
static void
about_cb( GtkAction *action, ProgressData *battstat )
{
const gchar *authors[] = {
"J\xC3\xB6rgen Pehrson <jp@spektr.eu.org>",
"Lennart Poettering <lennart@poettering.de> (Linux ACPI support)",
"Seth Nickell <snickell@stanford.edu> (GNOME2 port)",
"Davyd Madeley <davyd@madeley.id.au>",
"Ryan Lortie <desrt@desrt.ca>",
"Joe Marcus Clarke <marcus@FreeBSD.org> (FreeBSD ACPI support)",
NULL
};
const gchar *documenters[] = {
"J\xC3\xB6rgen Pehrson <jp@spektr.eu.org>",
"Trevor Curtis <tcurtis@somaradio.ca>",
"Davyd Madeley <davyd@madeley.id.au>",
NULL
};
char *comments = g_strdup_printf ("%s\n\n%s",
_("This utility shows the status of your laptop battery."),
power_management_using_upower () ?
/* ture */ _("upower backend enabled.") :
(power_management_using_hal () ?
/* true */ _("HAL backend enabled.") :
/* false */ _("Legacy (non-HAL) backend enabled.")));
gtk_show_about_dialog( NULL,
"version", VERSION,
"copyright", "\xC2\xA9 2000 The Gnulix Society, "
"\xC2\xA9 2002-2005 Free Software Foundation and "
"others",
"comments", comments,
"authors", authors,
"documenters", documenters,
"translator-credits", _("translator-credits"),
"logo-icon-name", "battery",
NULL );
g_free (comments);
}
/* Rotate text on side panels. Called on initial startup and when the
* orientation changes (ie: the panel we were on moved or we moved to
* another panel).
*/
static void
setup_text_orientation( ProgressData *battstat )
{
if( battstat->orienttype == MATE_PANEL_APPLET_ORIENT_RIGHT )
gtk_label_set_angle( GTK_LABEL( battstat->percent ), 90 );
else if( battstat->orienttype == MATE_PANEL_APPLET_ORIENT_LEFT )
gtk_label_set_angle( GTK_LABEL( battstat->percent ), 270 );
else
gtk_label_set_angle( GTK_LABEL( battstat->percent ), 0 );
}
/* This signal is delivered by the panel when the orientation of the applet
has changed. This is either because the applet has just been created,
has just been moved to a new panel or the panel that the applet was on
has changed orientation.
*/
static void
change_orient (MatePanelApplet *applet,
MatePanelAppletOrient orient,
ProgressData *battstat)
{
if (DEBUG) g_print("change_orient()\n");
/* Ignore the update if we already know. */
if( orient != battstat->orienttype )
{
battstat->orienttype = orient;
/* The applet changing orientation very likely involves the layout
being changed to better fit the new shape of the panel.
*/
setup_text_orientation( battstat );
reconfigure_layout( battstat );
}
}
/* This is delivered when our size has changed. This happens when the applet
is just created or if the size of the panel has changed.
*/
static void
size_allocate( MatePanelApplet *applet, GtkAllocation *allocation,
ProgressData *battstat)
{
if (DEBUG) g_print("applet_change_pixel_size()\n");
/* Ignore the update if we already know. */
if( battstat->width == allocation->width &&
battstat->height == allocation->height )
return;
battstat->width = allocation->width;
battstat->height = allocation->height;
/* The applet changing size could result in the layout changing. */
reconfigure_layout( battstat );
}
/* Get our settings out of gsettings.
*/
static void
load_preferences(ProgressData *battstat)
{
MatePanelApplet *applet = MATE_PANEL_APPLET (battstat->applet);
GSettings *settings = battstat->settings;
if (DEBUG) g_print("load_preferences()\n");
battstat->red_val = g_settings_get_int (settings, "red-value");
battstat->red_val = CLAMP (battstat->red_val, 0, 100);
battstat->red_value_is_time = g_settings_get_boolean (settings, "red-value-is-time");
/* automatically calculate orangle and yellow values from the red value */
battstat->orange_val = battstat->red_val * ORANGE_MULTIPLIER;
battstat->orange_val = CLAMP (battstat->orange_val, 0, 100);
battstat->yellow_val = battstat->red_val * YELLOW_MULTIPLIER;
battstat->yellow_val = CLAMP (battstat->yellow_val, 0, 100);
battstat->lowbattnotification = g_settings_get_boolean (settings, "low-battery-notification");
battstat->fullbattnot = g_settings_get_boolean (settings, "full-battery-notification");
battstat->beep = g_settings_get_boolean (settings, "beep");
battstat->draintop = g_settings_get_boolean (settings, "drain-from-top");
battstat->showstatus = g_settings_get_boolean (settings, "show-status");
battstat->showbattery = g_settings_get_boolean (settings, "show-battery");
/* for miagration from older versions */
if (battstat->showstatus && battstat->showbattery)
battstat->showbattery = FALSE;
battstat->showtext = g_settings_get_int (settings, "show-text");
}
/* Convenience function to attach a child widget to a GtkTable in the
position indicated by 'loc'. This is very special-purpose for 3x3
tables and only supports positions that are used in this applet.
*/
static void
table_layout_attach( GtkTable *table, LayoutLocation loc, GtkWidget *child )
{
GtkAttachOptions flags;
flags = GTK_FILL | GTK_EXPAND;
switch( loc )
{
case LAYOUT_LONG:
gtk_table_attach( table, child, 1, 2, 0, 2, flags, flags, 2, 2 );
break;
case LAYOUT_TOPLEFT:
gtk_table_attach( table, child, 0, 1, 0, 1, flags, flags, 2, 2 );
break;
case LAYOUT_TOP:
gtk_table_attach( table, child, 1, 2, 0, 1, flags, flags, 2, 2 );
break;
case LAYOUT_LEFT:
gtk_table_attach( table, child, 0, 1, 1, 2, flags, flags, 2, 2 );
break;
case LAYOUT_CENTRE:
gtk_table_attach( table, child, 1, 2, 1, 2, flags, flags, 2, 2 );
break;
case LAYOUT_RIGHT:
gtk_table_attach( table, child, 2, 3, 1, 2, flags, flags, 2, 2 );
break;
case LAYOUT_BOTTOM:
gtk_table_attach( table, child, 1, 2, 2, 3, flags, flags, 2, 2 );
break;
default:
break;
}
}
/* The layout has (maybe) changed. Calculate what layout we ought to be
using and update some things if anything has changed. This is called
from size/orientation change callbacks and from the preferences dialog
when elements get added or removed.
*/
void
reconfigure_layout( ProgressData *battstat )
{
gboolean up_down_order = FALSE;
gboolean do_square = FALSE;
LayoutConfiguration c;
int battery_horiz = 0;
int needwidth;
/* Decide if we are doing to do 'square' mode. */
switch( battstat->orienttype )
{
case MATE_PANEL_APPLET_ORIENT_UP:
case MATE_PANEL_APPLET_ORIENT_DOWN:
up_down_order = TRUE;
/* Need to be at least 46px tall to do square mode on a horiz. panel */
if( battstat->height >= 46 )
do_square = TRUE;
break;
case MATE_PANEL_APPLET_ORIENT_LEFT:
case MATE_PANEL_APPLET_ORIENT_RIGHT:
/* We need 64px to fix the text beside anything. */
if( battstat->showtext )
needwidth = 64;
/* We need 48px to fix the icon and battery side-by-side. */
else
needwidth = 48;
if( battstat->width >= needwidth )
do_square = TRUE;
break;
}
/* Default to no elements being displayed. */
c.status = c.text = c.battery = LAYOUT_NONE;
if( do_square )
{
/* Square mode is only useful if we have the battery meter shown. */
if( battstat->showbattery )
{
c.battery = LAYOUT_LONG;
/* if( battstat->showstatus ) */ /* make this always true */
c.status = LAYOUT_TOPLEFT;
if( battstat->showtext )
c.text = LAYOUT_LEFT;
}
else
{
/* We have enough room to do 'square' mode but the battery meter is
not requested. We can, instead, take up the extra space by stacking
our items in the opposite order that we normally would (ie: stack
horizontally on a vertical panel and vertically on horizontal).
*/
up_down_order = !up_down_order;
do_square = FALSE;
}
}
if( !do_square )
{
if( up_down_order )
{
/* Stack horizontally for top and bottom panels. */
/* if( battstat->showstatus ) */ /* make this always true */
c.status = LAYOUT_LEFT;
if( battstat->showbattery )
c.battery = LAYOUT_CENTRE;
if( battstat->showtext )
c.text = LAYOUT_RIGHT;
battery_horiz = 1;
}
else
{
/* Stack vertically for left and right panels. */
/* if( battstat->showstatus ) */ /* make this always true */
c.status = LAYOUT_TOP;
if( battstat->showbattery )
c.battery = LAYOUT_CENTRE;
if( battstat->showtext )
c.text = LAYOUT_BOTTOM;
}
}
if( memcmp( &c, &battstat->layout, sizeof (LayoutConfiguration) ) )
{
/* Something in the layout has changed. Rebuild. */
/* Start by removing any elements in the table from the table. */
if( battstat->layout.text )
gtk_container_remove( GTK_CONTAINER( battstat->table ),
battstat->percent );
if( battstat->layout.status )
gtk_container_remove( GTK_CONTAINER( battstat->table ),
battstat->status );
if( battstat->layout.battery )
gtk_container_remove( GTK_CONTAINER( battstat->table ),
battstat->battery );
/* Attach the elements to their new locations. */
table_layout_attach( GTK_TABLE(battstat->table),
c.battery, battstat->battery );
table_layout_attach( GTK_TABLE(battstat->table),
c.status, battstat->status );
table_layout_attach( GTK_TABLE(battstat->table),
c.text, battstat->percent );
gtk_widget_show_all( battstat->applet );
}
/* If we are showing the battery meter and we weren't showing it before or
if the orientation has changed, we had better update it right now.
*/
if( (c.battery && !battstat->layout.battery) ||
battery_horiz != battstat->horizont )
{
battstat->horizont = battery_horiz;
update_battery_image (battstat,
battstat->last_batt_life, battstat->last_minutes);
}
battstat->layout = c;
/* Check for generic updates. This is required, for example, to make sure
the text label is immediately updated to show the time remaining or
percentage.
*/
check_for_updates( battstat );
}
/* Allocate the widgets for the applet and connect our signals.
*/
static gint
create_layout(ProgressData *battstat)
{
if (DEBUG) g_print("create_layout()\n");
/* Have our background automatically painted. */
mate_panel_applet_set_background_widget( MATE_PANEL_APPLET( battstat->applet ),
GTK_WIDGET( battstat->applet ) );
/* Allocate the four widgets that we need. */
battstat->table = gtk_table_new( 3, 3, FALSE );
battstat->percent = gtk_label_new( "" );
battstat->status = gtk_image_new();
battstat->battery = gtk_image_new();
/* When you first get a pointer to a newly created GtkWidget it has one
'floating' reference. When you first add this widget to a container
the container adds a real reference and removes the floating reference
if one exists. Since we insert/remove these widgets from the table
when our layout is reconfigured, we need to keep our own 'real'
reference to each widget. This adds a real reference to each widget
and "sinks" the floating reference.
*/
g_object_ref( battstat->status );
g_object_ref( battstat->percent );
g_object_ref( battstat->battery );
#if GTK_CHECK_VERSION (3, 0, 0)
g_object_ref_sink( G_OBJECT( battstat->status ) );
g_object_ref_sink( G_OBJECT( battstat->percent ) );
g_object_ref_sink( G_OBJECT( battstat->battery ) );
#else
g_object_ref_sink( GTK_OBJECT( battstat->status ) );
g_object_ref_sink( GTK_OBJECT( battstat->percent ) );
g_object_ref_sink( GTK_OBJECT( battstat->battery ) );
#endif
/* Let reconfigure_layout know that the table is currently empty. */
battstat->layout.status = LAYOUT_NONE;
battstat->layout.text = LAYOUT_NONE;
battstat->layout.battery = LAYOUT_NONE;
/* Put the table directly inside the applet and show everything. */
gtk_container_add (GTK_CONTAINER (battstat->applet), battstat->table);
gtk_widget_show_all (battstat->applet);
/* Attach all sorts of signals to the applet. */
g_signal_connect(G_OBJECT(battstat->applet),
"destroy",
G_CALLBACK(destroy_applet),
battstat);
g_signal_connect (battstat->applet,
"change_orient",
G_CALLBACK(change_orient),
battstat);
g_signal_connect (battstat->applet,
"size_allocate",
G_CALLBACK (size_allocate),
battstat);
return FALSE;
}
/* Called by the factory to fill in the fields for the applet.
*/
static gboolean
battstat_applet_fill (MatePanelApplet *applet)
{
ProgressData *battstat;
AtkObject *atk_widget;
GtkActionGroup *action_group;
gchar *ui_path;
const char *err;
int no_hal;
if (DEBUG) g_print("main()\n");
g_set_application_name (_("Battery Charge Monitor"));
gtk_window_set_default_icon_name ("battery");
mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR);
battstat = g_new0 (ProgressData, 1);
battstat->settings = mate_panel_applet_settings_new (applet, BATTSTAT_SCHEMA);
/* Some starting values... */
battstat->applet = GTK_WIDGET (applet);
battstat->refresh_label = TRUE;
battstat->last_batt_life = 1000;
battstat->last_acline_status = 1000;
battstat->last_pixmap_index = 1000;
battstat->last_charging = 1000;
battstat->orienttype = mate_panel_applet_get_orient (applet);
battstat->horizont = TRUE;
battstat->battery_low_dialog = NULL;
battstat->battery_low_label = NULL;
#if !GTK_CHECK_VERSION (3, 0, 0)
battstat->pixgc = NULL;
#endif
battstat->timeout = -1;
battstat->timeout_id = 0;
/* The first received size_allocate event will cause a reconfigure. */
battstat->height = -1;
battstat->width = -1;
load_preferences (battstat);
create_layout (battstat);
setup_text_orientation( battstat );
action_group = gtk_action_group_new ("Battstat Applet Actions");
gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
gtk_action_group_add_actions (action_group,
battstat_menu_actions,
G_N_ELEMENTS (battstat_menu_actions),
battstat);
ui_path = g_build_filename (BATTSTAT_MENU_UI_DIR, "battstat-applet-menu.xml", NULL);
mate_panel_applet_setup_menu_from_file (MATE_PANEL_APPLET (battstat->applet),
ui_path, action_group);
g_free (ui_path);
if (mate_panel_applet_get_locked_down (MATE_PANEL_APPLET (battstat->applet))) {
GtkAction *action;
action = gtk_action_group_get_action (action_group, "BattstatProperties");
gtk_action_set_visible (action, FALSE);
}
g_object_unref (action_group);
atk_widget = gtk_widget_get_accessible (battstat->applet);
if (GTK_IS_ACCESSIBLE (atk_widget)) {
atk_object_set_name (atk_widget, _("Battery Charge Monitor"));
atk_object_set_description(atk_widget, _("Monitor a laptop's remaining power"));
}
no_hal = g_settings_get_boolean (battstat->settings, "no-hal");
if ((err = static_global_initialisation (no_hal, battstat)))
battstat_error_dialog (GTK_WIDGET (applet), err);
return TRUE;
}
/* Boilerplate... */
static gboolean
battstat_applet_factory (MatePanelApplet *applet,
const gchar *iid,
gpointer data)
{
gboolean retval = FALSE;
if (!strcmp (iid, "BattstatApplet"))
retval = battstat_applet_fill (applet);
return retval;
}
MATE_PANEL_APPLET_OUT_PROCESS_FACTORY ("BattstatAppletFactory",
PANEL_TYPE_APPLET,
"battstat",
battstat_applet_factory,
NULL)
|