summaryrefslogtreecommitdiff
path: root/anzeige.c
blob: 42453a3b106275173e45a8ccf03f1085ca685c69 (plain)
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
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>

#include "fonts.h"
#include "input_gadgets.h"
#include "multiplexer.h"

void drop_privileges();
void handle_signals(int signo);

t_display_data display_data;

int main(int argc, char **argv)
{
  pthread_t thread_id;
  int ret_val;
  #define TEXT_BUFFER_LENGTH 256
  char *text_buffer;
  char *text_buffer_end;
  t_scroll_buffer scroll_buffers[2];

  // Set up gpi pointer for direct register access
  ret_val = multiplexer_setup_root();
  if (ret_val)
    exit(ret_val);

  // Drop root privileges
  drop_privileges();

  // set up / register signal handler
  struct sigaction new_action, old_action;
  new_action.sa_handler = handle_signals;
  sigemptyset (&new_action.sa_mask);
  new_action.sa_flags = 0;
  sigaction (SIGINT, NULL, &old_action);
  if (old_action.sa_handler != SIG_IGN)
    sigaction (SIGINT, &new_action, NULL);
  sigaction (SIGHUP, NULL, &old_action);
  if (old_action.sa_handler != SIG_IGN)
    sigaction (SIGHUP, &new_action, NULL);
  sigaction (SIGTERM, NULL, &old_action);
  if (old_action.sa_handler != SIG_IGN)
    sigaction (SIGTERM, &new_action, NULL);struct sigaction action;

 /************************************************************************\
  * You are about to change the GPIO settings of your computer.          *
  * Mess this up and it will stop working!                               *
  * It might be a good idea to 'sync' before running this program        *
  * so at least you still have your code changes written to the SD-card! *
 \************************************************************************/

  // configure the gpio pins and start the multiplexer thread
  thread_id = multiplexer_setup_non_root(&display_data);

  int i,j;

  text_buffer = malloc(TEXT_BUFFER_LENGTH);
  if (text_buffer == NULL) {
    fprintf(stderr, "malloc failed to allocate 24 bytes\n");
    exit(EXIT_FAILURE);
  }
  memset(text_buffer, 0, TEXT_BUFFER_LENGTH);

  text_buffer_end = text_buffer;

  text_buffer_end = gadgets_retrieve_weather_warnings(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end);
  if (text_buffer_end == NULL) {
    fprintf(stderr, "gadgets_retrieve_weather_warnings failed\n");
    free(text_buffer);
    return EXIT_FAILURE;
  }

  text_buffer_end = gadgets_retrieve_current_temperature(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end);
  if (text_buffer_end == NULL) {
    fprintf(stderr, "gadgets_retrieve_current_temperature failed\n");
    free(text_buffer);
    return EXIT_FAILURE;
  }

  text_buffer_end = gadgets_retrieve_weather_forecast(text_buffer_end,text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end);
  if (text_buffer_end == NULL) {
    fprintf(stderr, "gadgets_retrieve_weather_forecast failed\n");
    free(text_buffer);
    return EXIT_FAILURE;
  }

  scroll_buffers[0].scroll_len = render(text_buffer, TEXT_BUFFER_LENGTH, scroll_buffers[0].buf, sizeof(scroll_buffers[0].buf), 1);

  i=-40;
  while (display_data.keep_running) {
    usleep(100000);
    for (j=0; j<40; j++)
      if (i+j<0)
        display_data.buf[(display_data.should_buf+1)%3][j] = 0;
      else
        display_data.buf[(display_data.should_buf+1)%3][j] = scroll_buffers[0].buf[(i+j) % scroll_buffers[0].scroll_len];
    display_data.should_buf = (display_data.should_buf+1)%3;
    i++;
    i = i % scroll_buffers[0].scroll_len;
  }

  free(text_buffer);
  display_data.keep_running = 0;
  pthread_join(thread_id, NULL);

  return 0;

} // main

//
// Drop (root) privileges
//
void drop_privileges()
{
  /*  Drop superuser privileges in correct order */

  int is_already_unprivileged = 0;

  if (setgid(99) == -1) {
    perror("can't drop group privileges");
    if (! (is_already_unprivileged = (errno == 1)))
      exit(-1);
  }
  if (setuid(99) == -1) {
    perror("can't drop user privileges");
    if (is_already_unprivileged != (errno == 1))
      exit(-1);
  }
} // drop_root

void handle_signals(int signo)
{
  if ((signo == SIGINT) || (signo == SIGHUP) || (signo == SIGTERM)) {
    fprintf(stderr, "stopping\n");
    display_data.keep_running = 0;
  }
}