Code

forgot to add this
[ncmpc.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <glib.h>
7 #include "config.h"
8 #include "libmpdclient.h"
9 #include "support.h"
10 #include "mpc.h"
11 #include "options.h"
12 #include "command.h"
13 #include "screen.h"
14 #include "conf.h"
17 static mpd_client_t *mpc = NULL;
20 void
21 exit_and_cleanup(void)
22 {
23   screen_exit();
24   charset_close();
25   if( mpc )
26     {
27       if( mpc_error(mpc) )
28         fprintf(stderr,"Error: %s\n", mpc_error_str(mpc));
29       mpc_close(mpc);
30     }
31 }
33 void
34 catch_sigint( int sig )
35 {
36   printf( "\nExiting...\n");
37   exit(EXIT_SUCCESS);
38 }
40 int 
41 main(int argc, const char *argv[])
42 {
43   options_t *options;
44   struct sigaction act;
45   int counter, connected;
47   /* initialize options */
48   options = options_init();
49   
50   /* read configuration */
51   read_rc_file(NULL, options);
53   /* parse command line options */
54   options_parse(argc, argv);
56   /* initialize local charset */
57   if( charset_init() )
58     exit(EXIT_FAILURE);
60   /* setup signal behavior - SIGINT */
61   sigemptyset( &act.sa_mask );
62   act.sa_flags    = 0;
63   act.sa_handler = catch_sigint;
64   if( sigaction( SIGINT, &act, NULL )<0 )
65     {
66       perror("signal");
67       exit(EXIT_FAILURE);
68     }
69   /* setup signal behavior - SIGWINCH  */
70   sigemptyset( &act.sa_mask );
71   act.sa_flags    = 0;
72   act.sa_handler = screen_resized;
73   if( sigaction( SIGWINCH, &act, NULL )<0 )
74     {
75       perror("sigaction()");
76       exit(EXIT_FAILURE);
77     }
78   /* setup signal behavior - SIGTERM */
79   sigemptyset( &act.sa_mask );
80   act.sa_flags    = 0;
81   act.sa_handler = catch_sigint;
82   if( sigaction( SIGTERM, &act, NULL )<0 )
83     {
84       perror("sigaction()");
85       exit(EXIT_FAILURE);
86     }
88   /* set xterm title */
89   if( getenv("DISPLAY") )
90     printf("%c]0;%s%c", '\033', PACKAGE " v" VERSION, '\007');
92   /* install exit function */
93   atexit(exit_and_cleanup);
95   /* connect to our music player daemon */
96   mpc = mpc_connect(options->host, options->port);
97   if( mpc_error(mpc) )
98     exit(EXIT_FAILURE);
100   /* initialize curses */
101   screen_init();
102   
103   counter=0;
104   connected=1;
105   while( connected || options->reconnect )
106     {
107       command_t cmd;
109       if( connected && counter==0  )
110         {
111           mpc_update(mpc);
112           if( mpc_error(mpc) )
113             {
114               connected=0;
115               screen_status_printf("Lost connection to %s", options->host);
116               mpd_closeConnection(mpc->connection);
117               mpc->connection = NULL;
118             }
119           else
120             mpd_finishCommand(mpc->connection);
121           counter=10;
122         }
124       if( connected )
125         {
126           screen_update(mpc);
127           if( (cmd=get_keyboard_command()) != CMD_NONE )
128             {
129               screen_cmd(mpc, cmd);
130               if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
131                 counter=10; /* make shure we dont update the volume yet */
132               else
133                 counter=0;
134             }
135         }
136       else if( options->reconnect )
137         {
138           sleep(3);
139           screen_status_printf("Connecting to %s...  [Press Ctrl-C to abort]", 
140                                options->host);
141           if( mpc_reconnect(mpc, options->host, options->port) == 0 )
142             {
143               screen_status_printf("Connected to %s!", options->host);
144               connected=1;
145               counter=0;
146             }
147         }
149       if( counter>0 )
150         counter--;
152     }
153   exit(EXIT_FAILURE);