You are not logged in.

#1 2009-07-22 20:34:11

Vintendo
Member
From: Netherlands
Registered: 2008-04-21
Posts: 375
Website

C question

I wanted to make a random desktop background setter in C but I have a couple of questions, first of all. Which library can I use to set the background. I thought xsetbg but I can't find it...

Second of all, I am trying to get all the names of the directory contents in a array but it's not working. Line 61 prints the file name, 62 doesn't...

  1 #include <dirent.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <malloc.h>
  5 #include <string.h>
  6 
  7 static char **files = NULL;
  8 size_t filecount = 0;
  9 
 10 char **getFileArray(const char *path);
 11 
 12 int main(int argc, char **argv) {
 13   if (argc != 2) {
 14     fprintf (stderr, "Usage: %s folder with images\n", argv[0]);
 15     return 1;
 16   }
 17   files = getFileArray(argv[1]);
 18   setRandomBackground(files);
 19 }
 20 
 21 int setRandomBackground(char **fileArray) {
 22   int random = 1; //rand() % filecount;
 23   if(random != 0) {
 24     //Set background, how?  
 25     char fehCommand[256];
 26     sprintf(fehCommand, "feh --bg-scale %s", fileArray[random]);
 27     printf("test %s", fehCommand);
 28 //    system(fehCommand);
 29   }
 30 }
 31 
 32 char **getFileArray(const char *path) {
 33   DIR *dir = opendir(path);
 34   struct dirent *dp;
 35   size_t i = 0;
 36   char **files;
 37 
 38   if(dir == NULL) {
 39     return NULL; //opendir failed
 40   }
 41 
 42   while((dp = readdir(dir)) != NULL) {
 43     filecount++;
 44   }
 45   files = (char **) malloc(filecount * sizeof(*files));
 46   if(files == NULL) {
 47     return NULL; //malloc failed;
 48   }
 49   
 50   rewinddir(dir);
 51   while((dp = readdir(dir)) != NULL) {
 52     files[i] = strdup(dp->d_name);
 53     if(files[i] = NULL) { //Something failed, free what's been allocated, so there's no leak.
 54       while(i > 0) {
 55         free(files[--i]);
 56       }
 57       free(files);
 58       return NULL;
 59     }
 60 
 61 //    printf("%d: %s\n", i, dp->d_name);
 62     printf("%d%s", i, files[i]);
 63     i++;
 64   }

Last edited by Vintendo (2009-07-22 20:35:34)

Offline

#2 2009-07-22 20:56:28

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: C question

What WM? Gnome can be set via:

gconftool-2 --type string --set /desktop/gnome/background/picture_filename "ABSOLUTE IMG PATH"

And for KDE you'll have to experiment with:

~/.kde/share/config/kdesktoprc

Unfortunately this requires restarting the kdestop process.

Offline

#3 2009-07-22 20:58:19

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C question

Don't know answer to your first question, but in answer to your second question, look at line 53.  You need an ==, you're doing assignment otherwise.

Offline

#4 2009-07-22 21:01:26

Vintendo
Member
From: Netherlands
Registered: 2008-04-21
Posts: 375
Website

Re: C question

thanks _0_, and to think I have stared at that for 2 days...

One other question, how can I find out the mimetype of an image, or another way to check if a file is an image...

Last edited by Vintendo (2009-07-22 22:30:14)

Offline

#5 2009-07-22 22:37:46

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C question

Doesn't feh allow you to place images ON the root window? I recall some crazy conkys using it.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#6 2009-07-22 22:39:45

Vintendo
Member
From: Netherlands
Registered: 2008-04-21
Posts: 375
Website

Re: C question

I don't know, you can use it to set the wallpaper, so it works now...

Offline

#7 2009-07-23 00:28:39

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: C question

On a side note...

Vintendo wrote:
   files = (char **) malloc(filecount * sizeof(*files));

I humbly advise you not to cast the return value of malloc (or any function that returns a pointer to void).

Offline

#8 2009-07-23 16:38:01

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Re: C question

Why?


(lambda ())

Offline

#9 2009-07-23 17:10:53

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C question

andre.ramaciotti wrote:

Why?

Found this link for a google search on 'casting malloc':

http://c-faq.com/malloc/mallocnocast.html

Offline

#10 2009-07-23 19:56:46

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Re: C question

Thanks. smile


(lambda ())

Offline

Board footer

Powered by FluxBB