patch-src_tpb_c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. $OpenBSD: patch-src_tpb_c,v 1.4 2007/06/01 16:44:11 jcs Exp $
  2. --- src/tpb.c.orig Mon Jul 18 08:15:59 2005
  3. +++ src/tpb.c Fri Jun 1 10:34:14 2007
  4. @@ -25,13 +25,21 @@
  5. #include <sys/wait.h>
  6. #include <locale.h>
  7. #include <signal.h>
  8. +#ifdef __OpenBSD__
  9. +#include <soundcard.h>
  10. +#else
  11. #include <sys/soundcard.h>
  12. +#endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. +#ifdef __OpenBSD__
  20. +#include <sys/ioctl.h>
  21. +#include <machine/apmvar.h>
  22. +#endif
  23. #include "config.h"
  24. #if ENABLE_NLS
  25. @@ -84,8 +92,13 @@ int apmiser_running(void);
  26. int *xerrorhandler(Display *display, XErrorEvent *event);
  27. #endif /* HAVE_LIBX11 */
  28. void sig_chld_handler(int signo);
  29. +#ifdef __OpenBSD__
  30. +const char *apm_battery_level(int state);
  31. +#endif
  32. /* }}} */
  33. +int nfd = -1;
  34. +
  35. int main(int argc, char **argv) /* {{{ */
  36. {
  37. t_thinkpad_state thinkpad_state, last_thinkpad_state;
  38. @@ -103,6 +116,17 @@ int main(int argc, char **argv) /* {{{ */
  39. Display *display = NULL;
  40. #endif /* HAVE_LIBX11 */
  41. + /* open nvram */
  42. + if ((nfd = open(PATH_NVRAM, O_RDONLY|O_NONBLOCK, 0)) < 0) {
  43. + fprintf(stderr, _("Unable to open device %s: "), PATH_NVRAM);
  44. + perror(NULL);
  45. + return -1;
  46. + }
  47. +
  48. + /* drop privileges */
  49. + setegid(getgid());
  50. + setgid(getgid());
  51. +
  52. /* zero thinkpad_state */
  53. memset(&thinkpad_state, 0, sizeof(thinkpad_state));
  54. @@ -143,7 +167,7 @@ int main(int argc, char **argv) /* {{{ */
  55. /* become a daemon if requested */
  56. if(cfg.daemon == STATE_ON) {
  57. - daemonize();
  58. + daemon(0, 0);
  59. }
  60. /* initialize osd */
  61. @@ -638,6 +662,28 @@ int main(int argc, char **argv) /* {{{ */
  62. #endif /* HAVE_LIBXOSD */
  63. } /* }}} */
  64. +#ifdef __OpenBSD__
  65. + /* determine the level of battery {{{ */
  66. + if(thinkpad_state.battery_level != last_thinkpad_state.battery_level) {
  67. + if(cfg.verbose == STATE_ON) {
  68. + printf(_("Battery level changed: %s\n"), _(apm_battery_level(thinkpad_state.battery_level)));
  69. + }
  70. + if(cfg.callback != NULL) {
  71. + snprintf(callback_cmd, sizeof(callback_cmd), "%s battery_level %s", cfg.callback, _(apm_battery_level(thinkpad_state.battery_level)));
  72. + if(fork_app(callback_cmd) != 0) {
  73. + _exit(0);
  74. + }
  75. + }
  76. +#ifdef HAVE_LIBXOSD
  77. + if(osd_ptr != NULL &&
  78. + ((cfg.osd == STATE_OFF && cfg.osdpowermgt == STATE_ON) || (cfg.osd == STATE_ON && cfg.osdpowermgt != STATE_OFF))) {
  79. + xosd_display(osd_ptr, 0, XOSD_printf, "Battery level changed to %s", apm_battery_level(thinkpad_state.battery_level));
  80. + xosd_display(osd_ptr, 1, XOSD_string, "");
  81. + }
  82. +#endif /* HAVE_LIBXOSD */
  83. + } /* }}} */
  84. +#endif
  85. +
  86. /* determine power management mode AC {{{ */
  87. if(thinkpad_state.powermgt_ac != last_thinkpad_state.powermgt_ac) {
  88. switch(thinkpad_state.powermgt_ac) {
  89. @@ -980,7 +1026,6 @@ Display *init_xgrabkey(void) /* {{{ */
  90. /* get the current state from the nvram */
  91. int get_nvram_state(t_thinkpad_state *thinkpad_state) /* {{{ */
  92. {
  93. - static int fdsc = -1; /* -1 -> file not opened */
  94. unsigned char buffer[114];
  95. struct {
  96. int pos;
  97. @@ -994,26 +1039,20 @@ int get_nvram_state(t_thinkpad_state *thinkpad_state)
  98. };
  99. int pos_len_idx = 0;
  100. - /* open nvram for reading */
  101. - if(fdsc == -1) { /* if not already opened, open nvram */
  102. - if((fdsc=open(cfg.nvram, O_RDONLY|O_NONBLOCK)) == -1) {
  103. - fprintf(stderr, _("Unable to open device %s: "), cfg.nvram);
  104. - perror(NULL);
  105. - return -1;
  106. - }
  107. - }
  108. + if (nfd < 0)
  109. + return -1;
  110. /* Read only the interesting bytes from nvram to reduce the CPU consupmtion of tpb */
  111. /* The kernel nvram driver reads byte-by-byte from nvram, so just reading interesting bytes reduces the amount of inb() calls */
  112. while (pos_len[pos_len_idx].pos != 0x0) {
  113. - if(lseek(fdsc, pos_len[pos_len_idx].pos, SEEK_SET) != pos_len[pos_len_idx].pos ) {
  114. - fprintf(stderr, _("Unable to seek in device %s: "), cfg.nvram);
  115. + if(lseek(nfd, pos_len[pos_len_idx].pos, SEEK_SET) != pos_len[pos_len_idx].pos ) {
  116. + fprintf(stderr, _("Unable to seek in device %s: "), PATH_NVRAM);
  117. perror(NULL);
  118. return -1;
  119. }
  120. - if(read(fdsc, buffer+pos_len[pos_len_idx].pos, pos_len[pos_len_idx].len) != pos_len[pos_len_idx].len) {
  121. + if(read(nfd, buffer+pos_len[pos_len_idx].pos, pos_len[pos_len_idx].len) != pos_len[pos_len_idx].len) {
  122. - fprintf(stderr, _("Unable to read from device %s: "), cfg.nvram);
  123. + fprintf(stderr, _("Unable to read from device %s: "), PATH_NVRAM);
  124. perror(NULL);
  125. return -1;
  126. }
  127. @@ -1045,11 +1084,28 @@ int get_nvram_state(t_thinkpad_state *thinkpad_state)
  128. /* get the current state from the apm subsystem */
  129. int get_apm_state(t_thinkpad_state *thinkpad_state) /* {{{ */
  130. {
  131. - unsigned int i;
  132. static int fdsc = -1; /* -1 -> file not opened */
  133. +#ifdef __OpenBSD__
  134. + struct apm_power_info info;
  135. +#else
  136. + unsigned int i;
  137. char buffer[38];
  138. char *tokens[9];
  139. +#endif
  140. +#ifdef __OpenBSD__
  141. + memset(&info, 0, sizeof(info));
  142. +
  143. + if (fdsc == -1)
  144. + fdsc = open("/dev/apm", O_RDONLY);
  145. +
  146. + if (fdsc) {
  147. + if (ioctl(fdsc, APM_IOC_GETPOWER, &info) == 0) {
  148. + thinkpad_state->ac_state = (info.ac_state ? STATE_ON : STATE_OFF);
  149. + thinkpad_state->battery_level = info.battery_state;
  150. + }
  151. + }
  152. +#else
  153. /* Read the state of the ac line from proc filesystem.
  154. * Documentation of /proc/apm from linux kernel (/usr/src/linux/arch/i386/kernel/apm.c)
  155. *
  156. @@ -1122,6 +1178,7 @@ int get_apm_state(t_thinkpad_state *thinkpad_state) /*
  157. thinkpad_state->ac_state = STATE_ON;
  158. break;
  159. }
  160. +#endif
  161. return 0;
  162. } /* }}} */
  163. @@ -1283,6 +1340,11 @@ void set_nvram_volume_level(t_thinkpad_state *thinkpad
  164. int fdsc;
  165. char buffer;
  166. +#ifdef __OpenBSD__
  167. + /* nvram writing not supported in OpenBSD */
  168. + return;
  169. +#endif
  170. +
  171. /* only use writeback to nvram when cfg.mixersteps is different from DEFAULT_MIXERSTEPS */
  172. if(cfg.mixersteps != DEFAULT_MIXERSTEPS) {
  173. /* open nvram */
  174. @@ -1466,5 +1528,28 @@ void sig_chld_handler(int signo) /* {{{ */
  175. waitpid(-1, &status, WNOHANG);
  176. return;
  177. } /* }}} */
  178. +
  179. +#ifdef __OpenBSD__
  180. +const char *
  181. +apm_battery_level(int state)
  182. +{
  183. + switch (state) {
  184. + case APM_BATT_HIGH:
  185. + return "high";
  186. + case APM_BATT_LOW:
  187. + return "low";
  188. + case APM_BATT_CRITICAL:
  189. + return "CRITICAL";
  190. + case APM_BATT_CHARGING:
  191. + return "charging";
  192. + case APM_BATTERY_ABSENT:
  193. + return "absent";
  194. + case APM_BATT_UNKNOWN:
  195. + return "unknown";
  196. + default:
  197. + return "invalid battery state";
  198. + }
  199. +}
  200. +#endif
  201. /* vim600:set fen:set fdm=marker:set fdl=0: */