You are not logged in.
Pages: 1
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
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
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
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
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
I don't know, you can use it to set the wallpaper, so it works now...
Offline
On a side note...
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
Why?
(lambda ())
Offline
Why?
Found this link for a google search on 'casting malloc':
Offline
Thanks.
(lambda ())
Offline
Pages: 1