DomLin
DomLin

Reputation: 11

Countdown timer in C without freezing user input

I'm a newbie in C. I'm on the verge of finishing a program. Can anybody teach me how to make a countdown timer while being able to input something? I'm currently using sleep(), and as I've heard, it's bad for threading, and freezes input. Is that right?

Here's the function:

void nowPlaying(SONG * h, SONG * t, int * randPrev, int shuffleCon)
{
    SONG * ptr;
    ptr = h->next;
    int random, randCount, mincount = 0, choice = 4, tot = 0, tot2 = 0;

    if(h->next == t) {
        printf("No songs to be played. Add some!\n");
    }
    else if(h->next != t) {
        while(ptr->next != t) {
            tot2 = tot = ((ptr->cdown.minutes*60) + ptr->cdown.secs);
            do {
                printf("------------------------YouTunes------------------------\n");
                printf("========================================================\n");
                printf("TITLE : %s                                            \n", ptr->title);
                printf("ARTIST: %s                                            \n", ptr->artist);
                printf("ALBUM : %s                                            \n", ptr->album);
                switch(ptr->genre) {
                case 1:
                    printf("GENRE : POP                                           \n");
                    break;
                case 2:
                    printf("GENRE : OPM                                           \n");
                    break;
                case 3:
                    printf("GENRE : ROCK                                          \n");
                    break;
                case 4:
                    printf("GENRE : R&B                                           \n");
                    break;
                case 5:
                    printf("GENRE : ACOUSTIC                                      \n");
                    break;
                case 6:
                    printf("GENRE : CLASSICAL                                     \n");
                    break;
                }

                while(tot2 >= 60) {
                    tot2 = tot2 - 60;
                    mincount++;
                    //if(time_left % 60 == 0) mincount++;
                }

                printf("TIME: ");

                if(mincount < 10)
                    printf("0%d", mincount);
                else
                    printf("%d", mincount);

                printf(":");

                if(tot2 < 10)
                    printf("0%d", tot2);
                else if(tot2 == 60)
                    printf("00");
                else
                    printf("%d", tot2);

                printf("\n========================================================\n");
                printf("[1] Prev                 [0]Exit                  [2] Next\n");
                printf("Choice: ");
                //scanf("%d", &choice);
                //timeout(500);
                tot--;
                tot2 = tot;
                mincount = 0;
                sleep(1);
                system("clear");

                if(shuffleCon == 0) {
                    if(choice == 1) {
                        if(ptr->prev == h) {
                            //do nothing
                        }
                        else if(ptr->prev != h) {
                            ptr = ptr->prev;
                        }
                    }
                    else if(choice == 2) {
                        if(ptr->next == t) {
                            //do nothing
                        }
                        else if(ptr->next != t) {
                            ptr = ptr->next;
                        }
                    }
                }
                else if(shuffleCon == 1) {
                    if(choice == 1) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }
                    }
                    else if(choice == 2) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }

                        if(ptr == t) {
                            ptr = ptr->prev;
                            ptr = ptr->prev;
                        }
                    }
                }
            } while(tot != -1);

            if(shuffleCon == 0)
                ptr = ptr->next;
            else if(shuffleCon == 1) {
                random = shuffle(h, t, randPrev);
                randCount = 0;
                ptr = h->next;
                while(randCount != random) {
                    ptr = ptr->next;
                    randCount++;
                }
            }
        }
    }
}

Upvotes: 1

Views: 733

Answers (1)

abligh
abligh

Reputation: 25129

Broadly speaking, one of these:

  1. Use SIGALRM (see the alarm man page), and rely on the signal to interrupt the system call expecting user input.

  2. (Better, and certainly a more valuable programming exercise), write an event loop with select or poll and set the timeout to be the time remaining until the timeout.

For the sort of application you are talking about, (2) would be a better choice.

Upvotes: 4

Related Questions