Page 10 of 15 FirstFirst 123456789101112131415 LastLast
Results 91 to 100 of 150

Thread: Llama's and Intuition's Techie Thread

  1. #91
    Variable Bitrate intuitionsys's Avatar
    Join Date
    Jul 2005
    Location
    Northern Virginia
    Posts
    293
    How about timesharing the individual functions, e.g.:

    Code:
    complicated_function();
    sleep_function( delay / 2 );
    simpler_function();
    sleep_function( delay / 12 );
    or the inverse, whichever might work better.
    Silverwolf 2 is dead.
    Silverwolf 3...?

  2. #92
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    Quote Originally Posted by intuitionsys View Post
    I'm thinking...
    Heh. I know there must be SOME solution. Programs like Firefox and GTK support events and timers. I don't feel like reading the source code of each of those programs though.

    Quote Originally Posted by intuitionsys View Post
    How about timesharing the individual functions, e.g.:

    Code:
    complicated_function();
    sleep_function( delay / 2 );
    simpler_function();
    sleep_function( delay / 12 );
    or the inverse, whichever might work better.
    I'm not sure I understand what you are saying. There aren't any complicated or intensive functions. The problem is we are either going to be stuck blocking on user input or blocking on a timer. I need to block on both until either one happens.

  3. #93
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    TheLlama begins reading the Xt source code to figure out how they implement AppAddTimout()

    EDIT: TheLlama curses at 20 year old source code with ambiguous names and poor commenting.

  4. #94
    Variable Bitrate intuitionsys's Avatar
    Join Date
    Jul 2005
    Location
    Northern Virginia
    Posts
    293
    I thought of something else but it's sort of backwards; ignore as necessary

    For a project I'm working on at work the application performs huge DMA transfers at very high data rates (up to 400 Mbit/s). I'm performing data analysis between buffer fills that requires a timeout, i.e.:

    1. get a timestamp
    2. do some fancy math
    3. get timestamp within the high CPU usage iteration in step 2
    4. if calculation is taking too long get out of step 2

    Would you be able to implement a certain amount of granularity within each function, each of which times itself, returns an additive delay which you could use in your master event loop?

    I haven't looked at how Firefox does it either You might look at the Qt source too. Qt is my friend
    Silverwolf 2 is dead.
    Silverwolf 3...?

  5. #95
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    Quote Originally Posted by intuitionsys View Post
    I thought of something else but it's sort of backwards; ignore as necessary

    For a project I'm working on at work the application performs huge DMA transfers at very high data rates (up to 400 Mbit/s). I'm performing data analysis between buffer fills that requires a timeout, i.e.:

    1. get a timestamp
    2. do some fancy math
    3. get timestamp within the high CPU usage iteration in step 2
    4. if calculation is taking too long get out of step 2

    Would you be able to implement a certain amount of granularity within each function, each of which times itself, returns an additive delay which you could use in your master event loop?

    I haven't looked at how Firefox does it either You might look at the Qt source too. Qt is my friend
    That is an ideal solution to another problem. My functions do not time much time to run at all. Waiting until the end of the loop (or beginning of the loop) to dispatch events is fine in terms of design and performance. The problem is that I need to be able to accept user input while still being able to unblock for a timer. Even if I followed your example I would still need the golden function.
    Code:
    while(1) {
      block_until_time_elapsed_or_user_event(); // This is what I need for single threaded operation.
      handle_events();
      dispatch_events();
      redraw();
    }
    Basically, I need the same functionality of the POSIX console select() function. Select will block on a set of file descriptors until it has data, or timeout. So, for my REAL mp3 player I have select block on /dev/input/mouse and /dev/psaux and provide a timeout equal to the time until the next timer fires. Does that make sense?

    Of course, this function doesn't exist. I would need to either create a custom X server or use Polling in order to implement it. I think.

    I could rework the Application class to use multiple threads but I still haven't figured out a way to arrange the threads so it won't deadlock my computer because Xlib is not thread safe.

  6. #96
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    OK. I've finally figured it out. It is strange how I haven't been able to come up with a solution for over a month. Once I post the question online I figure it out myself. I mentioned the select() statement in my previous post. I did some more research and figured out that X11 actually has a file descriptor. Therefore, I can actually use select() with X11! This also eliminates any need for threads!

    Here is the generic demonstation of the method:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    
    Display *dis;
    Window win;
    int x11_fd;
    fd_set in_fds;
    
    struct timeval tv;
    XEvent ev;
    
    int main() {
        dis = XOpenDisplay(NULL);
        win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 256, 256, \
            0, BlackPixel (dis, 0), BlackPixel(dis, 0));
        
        // You don't need all of these. Make the mask as you normally would.
        XSelectInput(dis, win, 
            ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | \
            ButtonPressMask | ButtonReleaseMask  | StructureNotifyMask);
    
        XMapWindow(dis, win);
        XFlush(dis);
        
        // This returns the FD of the X11 Display (or something like that)
        x11_fd = ConnectionNumber(dis);
    
        // Main loop
        while (1) {
            // Create a File Description Set containing x11_fd
            FD_ZERO(&in_fds);
            FD_SET(x11_fd, &in_fds);
    
            // Set our timer.  One second sounds good.
            tv.tv_usec = 0;
            tv.tv_sec = 1;
    
            // Wait for X Event or a Timer
            if (select(x11_fd+1, &in_fds, 0, 0, &tv))
                printf("Event Received!\n");
            else
                // Handle timer here
                printf("Timer Fired!\n");
    
            // Handle XEvents and flush the input 
            while (XPending(dis))
                XNextEvent(dis, &ev);
        }
        return(0);
    }
    This code will handle user input as soon as it happens. It will timeout if input hasn't happened within a second.

    My solution is basically the same. Except, I need to recalculate the timeval each time an event occurs. I can do this by using gettimeofday() each time select completes and removing that much from the time remaining on the timer.

    EDIT: Apparently this method is fairly portable. So far I know it runs on Linux and OSX. Let me know if you find any other OSes that it either works or doesn't work with.

  7. #97
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    Here is more code that I'm sure someone will find handy at some point in time. I know I will be using it.

    This example shows an interval timer that will still accept X input.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <sys/time.h>
    
    Display *dis;
    Window win;
    int x11_fd;
    fd_set in_fds;
    
    #ifndef timeradd
    # define timeradd(a, b, result)                           \
       do {                                        \
         (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                 \
         (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;                  \
         if ((result)->tv_usec >= 1000000)                         \
           {                                       \
         ++(result)->tv_sec;                           \
         (result)->tv_usec -= 1000000;                         \
           }                                       \
       } while (0)
    #endif
    #ifndef timersub
     # define timersub(a, b, result)                           \
       do {                                        \
         (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                 \
         (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                  \
         if ((result)->tv_usec < 0) {                          \
           --(result)->tv_sec;                             \
           (result)->tv_usec += 1000000;                       \
         }                                         \
       } while (0)
    #endif
    
    
    
    int main() {
        struct timeval tv;
        struct timeval stv;
        struct timeval tv_period;
    
        XEvent ev;
    
        dis = XOpenDisplay(NULL);
        win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 256, 256, \
            0, BlackPixel (dis, 0), BlackPixel(dis, 0));
        
        // You don't need all of these. Make the mask as you normally would.
        XSelectInput(dis, win, 
            ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
            ButtonPressMask | ButtonReleaseMask  | StructureNotifyMask 
            );
    
        XMapWindow(dis, win);
        XFlush(dis);
        
        // This returns the FD of the X11 connection (or something like that)
        x11_fd = ConnectionNumber(dis);
    
        // Set Timer Period (this is how you configure the frequency):
        tv_period.tv_sec = 0;
        tv_period.tv_usec = 500000; // 500000us = 500ms = 0.5s 
    
    
        tv.tv_sec = tv_period.tv_sec;       // Set tv=1 sec so select() will timeout.
        tv.tv_usec = tv_period.tv_usec;
        gettimeofday(&stv, 0);              // Get the time of day and
        timeradd(&stv, &tv_period, &stv);   // Trust my math for now.. :)
    
        // Main loop
        while(1) {
            // Create a File Description Set containing x11_fd
            FD_ZERO(&in_fds);
            FD_SET(x11_fd, &in_fds);
    
    
            // Wait for X Event or a Timer
            if (select(x11_fd+1, &in_fds, 0, 0, &tv)) {
                printf("Event Received!\n");
    
                gettimeofday(&tv, 0);
                timersub(&stv, &tv, &tv);  // set tv = remaining time.
            }
            else {
                printf("Timer Fired!\n");
                // Initialize timer variables again.
                tv.tv_sec = tv_period.tv_sec;       // Set tv=1 sec so select() will timeout.
                tv.tv_usec = tv_period.tv_usec;
                gettimeofday(&stv, 0);
                timeradd(&stv, &tv_period, &stv);  // Trust my math for now.. :)
            }
    
            // Handle XEvents and flush the input 
            while(XPending(dis))
                XNextEvent(dis, &ev);
        }
        return(0);
    }
    EDIT: Just a note about the last two examples. They should work fine on all POSIX variants except for OpenVMS. OpenVMS's X implementation is such that File Descriptors are not possible. For this, one can use OpenVMS's XMultiplexInput() function. You can just use a #ifdef/#else/#endif block to handle both cases.

    EDIT AGAIN: Apparently the second example contains a bug. But the concept is the same.. I'll try to search it out tomorrow.

  8. #98
    Low Bitrate
    Join Date
    Mar 2000
    Location
    Northville, MI, USA
    Posts
    65
    Okay... going back to an OLD part of the thread. I finished my QT FMod Wrapper for the music section of my App.

    Just wondering if you guys have any suggestions on playlist managment? Right now, I just have a load function, which takes a file name. My plan is to just stick MP3's in a specific directory, and have the program search and organize based on artists, album, etc... anybody got a better suggestion?

    -Chris

  9. #99
    FLAC TheLlama's Avatar
    Join Date
    Jul 2004
    Location
    All over the world
    Posts
    970
    Quote Originally Posted by strandtc View Post
    Okay... going back to an OLD part of the thread. I finished my QT FMod Wrapper for the music section of my App.

    Just wondering if you guys have any suggestions on playlist managment? Right now, I just have a load function, which takes a file name. My plan is to just stick MP3's in a specific directory, and have the program search and organize based on artists, album, etc... anybody got a better suggestion?

    -Chris
    What do you mean by organize? Do you mean to store it all in a database or do you mean just organizing the filesystem?

    I agree that the mp3s should be organized on you disk, but only for maintenace really. I will tell you why relying on the Filesystem is a bad idea in the long run. You are stuck with one single sorting scheme. So, if you decide to organize by artist/album then you cannot view "All the Albums" or "Songs from Genre" or "Songs from 1970-1980".

    Storing everything in a relational database is a better idea because you can have a table for the artists, a table for albums, songs, etc.. and you can select them however you want.

  10. #100
    Low Bitrate
    Join Date
    Mar 2000
    Location
    Northville, MI, USA
    Posts
    65
    Well.. I don't know yet exactly.. thats why I'm looking for suggestions.

    Relational database is best.. but I don't want to run a full database like MySQL...

    I was initially thinking... on startup... crawl the directory strucutre, getting ID3 tags, and building the "by artist, by album, etc" relation that way on the fly.

    Better ideas?

    -Chris

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •