Code

Imported ncmpc (mpc-ncures).
[ncmpc.git] / main.c
1 /* 
2  * $Id: main.c,v 1.5 2004/03/16 14:34:49 kalle Exp $ 
3  *
4  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <glib.h>
11 #include "config.h"
12 #include "libmpdclient.h"
13 #include "mpc.h"
14 #include "options.h"
15 #include "command.h"
16 #include "screen.h"
19 static mpd_client_t *mpc = NULL;
21 void
22 exit_and_cleanup(void)
23 {
24   screen_exit();
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, char *argv[])
42 {
43   options_t *options;
44   struct sigaction act;
45   int counter;
47   /* parse command line options */
48   options_init();
49   options = options_parse(argc, argv);
51   /* setup signal behavior - SIGINT */
52   sigemptyset( &act.sa_mask );
53   act.sa_flags    = 0;
54   act.sa_handler = catch_sigint;
55   if( sigaction( SIGINT, &act, NULL )<0 )
56     {
57       perror("signal");
58       exit(EXIT_FAILURE);
59     }
60   /* setup signal behavior - SIGWINCH  */
61   sigemptyset( &act.sa_mask );
62   act.sa_flags    = 0;
63   act.sa_handler = screen_resized;
64   if( sigaction( SIGWINCH, &act, NULL )<0 )
65     {
66       perror("sigaction()");
67       exit(EXIT_FAILURE);
68     }
69   /* setup signal behavior - SIGTERM */
70   sigemptyset( &act.sa_mask );
71   act.sa_flags    = 0;
72   act.sa_handler = catch_sigint;
73   if( sigaction( SIGTERM, &act, NULL )<0 )
74     {
75       perror("sigaction()");
76       exit(EXIT_FAILURE);
77     }
79   /* set xterm title */
80   printf("%c]0;%s%c", '\033', PACKAGE " v" VERSION, '\007');
82   /* install exit function */
83   atexit(exit_and_cleanup);
85   /* connect to our music player daemon */
86   mpc = mpc_connect(options->host, options->port);
87   if( mpc_error(mpc) )
88     exit(EXIT_FAILURE);
90   screen_init();
91 #if 0
92   mpc_update(mpc);
93    mpc_update_filelist(mpc);
94   screen_paint(mpc);
95   //  sleep(1);
96 #endif
97   
98   counter=0;
99   while( !mpc_error(mpc) )
100     {
101       command_t cmd;
103       if( !counter )
104         {
105           mpc_update(mpc);
106           mpd_finishCommand(mpc->connection);
107           counter=10;
108         }
109       else
110         counter--;
111       
112       screen_update(mpc);
113       
114       if( (cmd=get_keyboard_command()) != CMD_NONE )
115         {
116           screen_cmd(mpc, cmd);
117           counter=0;
118         }
119     }
121   exit(EXIT_FAILURE);