Thread: how to pass data types around -- I'm losing mine.

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    how to pass data types around -- I'm losing mine.

    just a little program using imlib2 and Pixmap and the other stuff ones needs to set an image on the back ground. I am trying to figure out why it is not working, I've narrowed it down to pixmap getting lost, but that does not mean it is just that, but it is deff one part of it. My code:
    main:
    Code:
    /*
    Sep 21, 2017
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "image.h"
    #include "rotate.h"
    
    //globals
    
    Display *dis;
    Pixmap pixmap;
    Imlib_Context context;
    Imlib_Color_Modifier modifier;
    Imlib_Image buff;
    Imlib_Image img;
    
    int sw = 0, sh = 0;
    
    int main(int argc, char **argv)
    {
    int w,h;
    
    
    dis = XOpenDisplay(NULL);
    
        if(!dis)
        return 1;
    
        if(argc < 1)
            return 1;
    printf("argv[argc] %s\n", argv[1]);
    
      if(pixmap)
            printf("main init_imgs YES YES YES \n");
    
        init_imgs(&w,&h, dis,  pixmap,  context,  modifier,  img,  buff);
        sw = w;
        sh = h;
    
      if(pixmap)
            printf("main set_standard_image YES YES YES \n");
        else
            printf("main NO PIXMAP\n");
    
            printf("MAIN\n");
         set_standard_image(img,buff,sw,sh,argv[1],modifier,context,dis,pixmap);
    
        return 0;
    }
    Image.c
    Code:
    /*
    Sep 21, 2017
    */
    
    
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/Xatom.h>
    #include </usr/include/Imlib2.h>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "image.h"
    
    void init_imgs(int *sgw, int *sgh, Display *dis, Pixmap pixmap, Imlib_Context context, Imlib_Color_Modifier modifier, Imlib_Image img, Imlib_Image buff)
    {
    
    
      //  dis = XOpenDisplay(NULL);
    
        if(!dis)
        return;
    
        Visual *vis;
        Colormap cm;
    
        int w,h;
    
    //    Imlib_Color_Range range;
    
    
        unsigned int depth;
      //  int alpha = 256;
    
        //char *filename;
    
        modifier = NULL;
        context = imlib_context_new();
    
        imlib_context_push(context);
        imlib_context_set_display(dis);
        vis = DefaultVisual(dis, 0);
        cm = DefaultColormap(dis, 0);
    
        w = DisplayWidth(dis, 0);
        h = DisplayHeight(dis, 0);
    
    
    
        *sgw = w;
        *sgh = h;
    // printf("init image:2: sw %d sh %d\n ",&sw,&sh);
        depth = DefaultDepth(dis, 0);
    
        pixmap = XCreatePixmap(dis, RootWindow(dis, 0), w, h, depth);
    
        if(pixmap)
            printf("YES YES YES \n");
    
        imlib_context_set_display(dis);
        imlib_context_set_visual(vis);
        imlib_context_set_colormap(cm);
        imlib_context_set_drawable(pixmap);
        imlib_context_set_color_range(imlib_create_color_range());
    
        buff = imlib_create_image(w, h);
        imlib_context_set_image(buff);
    
    
    
     //   imlib_context_set_color(0, 0, 0, 255);
       // imlib_image_fill_rectangle(0, 0,sw,sh);
        imlib_context_set_dither(1);
        imlib_context_set_blend(1);
    
        modifier = imlib_create_color_modifier();
        imlib_context_set_color_modifier(modifier);
         if(pixmap)
            printf("YES YES YES \n");
             if(pixmap)
            printf("YES YES YES \n");
             if(pixmap)
            printf("YES YES YES \n");
    
    
    }
    
    
    void load_image(Imlib_Image img, char *filename)
    {
        img = imlib_load_image(filename);
    
    }
    
    
    // Adapted from fluxbox' bsetroot
    int setRootAtoms (Display *dis, Pixmap pixmap)
    {
    
    
        Atom atom_root, atom_eroot, type;
        unsigned char *data_root, *data_eroot;
        int format;
        unsigned long length, after;
    
        atom_root = XInternAtom (dis, "_XROOTMAP_ID", True);
        atom_eroot = XInternAtom (dis, "ESETROOT_PMAP_ID", True);
    
        // doing this to clean up after old background
        if (atom_root != None && atom_eroot != None)
        {
            XGetWindowProperty (dis, RootWindow (dis, 0),
                                atom_root, 0L, 1L, False, AnyPropertyType,
                                &type, &format, &length, &after, &data_root);
    
        if (type == XA_PIXMAP)
        {
            XGetWindowProperty (dis, RootWindow (dis, 0),
                                atom_eroot, 0L, 1L, False, AnyPropertyType,
                                &type, &format, &length, &after, &data_eroot);
    
        if (data_root && data_eroot && type == XA_PIXMAP &&
            *((Pixmap *) data_root) == *((Pixmap *) data_eroot))
        {
            XKillClient (dis, *((Pixmap *) data_root));
        }
      } // second if
    } //first if
    
        atom_root = XInternAtom (dis, "_XROOTPMAP_ID", False);
        atom_eroot = XInternAtom (dis, "ESETROOT_PMAP_ID", False);
    
        if (atom_root == None || atom_eroot == None)
            return 0;
    
        // setting new background atoms
        XChangeProperty (dis, RootWindow (dis, 0),
                        atom_root, XA_PIXMAP, 32, PropModeReplace,
                        (unsigned char *) &pixmap, 1);
    
        XChangeProperty (dis, RootWindow (dis, 0), atom_eroot,
                        XA_PIXMAP, 32, PropModeReplace, (unsigned char *) &pixmap,
                        1);
    
    
        return 1;
    }
    
    
    
    void display_image(Imlib_Color_Modifier modifier,  Imlib_Context context, Display *dis, Pixmap pixmap)
    {
    
       if (modifier != NULL) {
          imlib_context_set_color_modifier(modifier);
          imlib_apply_color_modifier();
          imlib_free_color_modifier();
          modifier = NULL;
        }
    
    printf("display image\n");
    if(!dis)
        printf("display_image no dis\n");
    
        if(!pixmap)
        printf("display_image no pixmap\n");
    
        imlib_render_image_on_drawable(0, 0);
        imlib_free_image();
        imlib_free_color_range();
    
        if (setRootAtoms (dis, pixmap) == 0)
            fprintf (stderr, "Couldn't create atoms...\n");
    
        XKillClient (dis, AllTemporary);
        XSetCloseDownMode (dis, RetainTemporary);
    
        XSetWindowBackgroundPixmap (dis, RootWindow (dis, 0),pixmap);
           XClearWindow (dis, RootWindow (dis, 0));
    
        XFlush (dis);
        XSync (dis, False);
    
        imlib_context_pop();
      //  imlib_context_free(context);
    
    }
    rotate.c . I haven't gotten that far yet, just trying to set this up to show/display an image on the background first.
    Code:
    /*
    Sep 21, 2017
    */
    
    
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/Xatom.h>
    #include </usr/include/Imlib2.h>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "image.h"
    #include "rotate.h"
    
    /*
    
       imlib_context_set_image(img.image);
    
            imlib_image_flip_horizontal();
       imlib_context_set_blend(1);
    imlib_context_set_image(buffer);
    
    
        //        imlib_blend_image_onto_image_at_angle (Imlib_Image source_image, char merge_alpha,
         //       int source_x, int source_y, int source_width, int source_height,
         //       int destination_x, int destination_y, int angle_x, int angle_y)
    
                                                                        //right side  left side image
            imlib_blend_image_onto_image_at_angle(img.image, 0, 0,0,w,h,
            R.destination_y, R.destination_x, R.angle_x , R.angle_y);
    
    
              set_image();
        free_image();
        set_buffer();
        display_image();
    
    
            */
    
    
    void set_standard_image(Imlib_Image img, Imlib_Image buff,int  sw, int sh, char *filename,
    Imlib_Color_Modifier modifier, Imlib_Context context, Display *dis, Pixmap pixmap)
    {
        printf("In set _ image %s\n", filename);
    
        int w = 0, h = 0;
    
    if(!dis)
        printf("no dis\n");
    
        if(!pixmap)
        printf("no pixmap\n");
    
    
      if(!buff)
        printf("no pixmap\n");
    
    
        img = imlib_load_image(filename);
    
        imlib_context_set_image(img);
        w = imlib_image_get_width();
        h = imlib_image_get_height();
        imlib_context_set_image(buff);
    
    
        imlib_context_set_image(img);
    
    
        imlib_blend_image_onto_image(img,0,0,0,w,h,0,0,sw,sh);
    
    
        display_image(modifier,  context, dis, pixmap);
    
    }
    output:
    Code:
    ./rotate-me /home/userx/slackware0.jpg
    argv[argc] /home/userx/slackware0.jpg
    YES YES YES 
    YES YES YES 
    YES YES YES 
    YES YES YES 
    
    // (post comment ) after it leaves init_images pixmap gets lost
    
    main NO PIXMAP
    MAIN
    In set _ image /home/userx/slackware0.jpg
    no pixmap
    no pixmap
    display image
    display_image no pixmap
    userx@/media/data/C-Projects/experimental/rotate-me/bin/Debug <>
    if I try declaring it a pointer in globals I get errors ( of course)

    Code:
    /media/data/C-Projects/experimental/rotate-me/image.c|57|warning: assignment makes pointer from integer without a cast [-Wint-conversion]|
    /media/data/C-Projects/experimental/rotate-me/image.c|65|warning: passing argument 1 of 'imlib_context_set_drawable' makes integer from pointer without a cast [-Wint-conversion]|
    /usr/include/Imlib2.h|134|note: expected 'Drawable {aka long unsigned int}' but argument is of type 'Pixmap * {aka long unsigned int *}'|
    I know it is not getting out of that function only being passed into it to get
    Code:
      pixmap = XCreatePixmap(dis, RootWindow(dis, 0), w, h, depth);
    but maing it global in main, isn't keeping it that way? google is not helping me so I am posting in here now.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > init_imgs(&w,&h, dis, pixmap, context, modifier, img, buff);
    Perhaps you need to understand what call by value means.

    Because anything init_imgs() does to pixmap, is done on the local variable copy it receives, and not this one.

    > but maing it global in main, isn't keeping it that way?
    Making things global just makes it worse in the long run. Better to understand how to pass output parameters properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    > init_imgs(&w,&h, dis, pixmap, context, modifier, img, buff);
    Perhaps you need to understand what call by value means.

    Because anything init_imgs() does to pixmap, is done on the local variable copy it receives, and not this one.

    > but making it global in main, isn't keeping it that way?
    Making things global just makes it worse in the long run. Better to understand how to pass output parameters properly.
    this is the whole reason I am doing it like this, spiting it up and passing them about, I got that int pass back through the prams but it is the pixmap data type thingy that is getting me,
    that is why I posted, to learn how to better pass values back through the prams especially with data types, ie . pixmap ..because I know it got caught up in that function, I just am having a hard time figuring out how to get it out of there so I can use it elsewhere. whence I figure that out then I can cut back on global's.

    thanks for you clue on what to look it up as, Perhaps you need to understand what call by value means.

    but I need that stuff passed around, if it preforms an operation on the data (type) then I need it elsewhere , I need to pass by reference, yes?

    that is why I made it global because I need that pixmap to set the image and the imlb_Image img and buff to do that image stuff too. Not different ones but the same ones.

    Code:
    display image
    X Error of failed request:  BadPixmap (invalid Pixmap parameter)
      Major opcode of failed request:  2 (X_ChangeWindowAttributes)
      Resource id in failed request:  0x6d0964b0
      Serial number of failed request:  26
      Current serial number in output stream:  28
    Last edited by userxbw; 09-21-2017 at 12:48 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you seem to understand enough to make your output integers into pointers.
    > void init_imgs(int *sgw, int *sgh,

    So why not
    void init_imgs(int *sgw, int *sgh, Display *dis, Pixmap *pixmap,

    You use *pixmap to refer to it in the rest of this function.

    And don't forget to use an & when you call it in main.
    init_imgs(&w,&h, dis, &pixmap, context, modifier, img, buff);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Well you seem to understand enough to make your output integers into pointers.
    > void init_imgs(int *sgw, int *sgh,

    So why not
    void init_imgs(int *sgw, int *sgh, Display *dis, Pixmap *pixmap,

    You use *pixmap to refer to it in the rest of this function.

    And don't forget to use an & when you call it in main.
    init_imgs(&w,&h, dis, &pixmap, context, modifier, img, buff);
    as stated before I even put the errors I was getting when trying that method if I make pixmap a pointer I get an int error
    again,
    Code:
    assignment makes pointer from integer without a cast
    expected 'Drawable {aka long unsigned int}' but argument is of type 'Pixmap * {aka long unsigned int *}'|
    I will go back and try it again and check for constancy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass data using Sockets
    By chaituchemico in forum Linux Programming
    Replies: 17
    Last Post: 09-16-2011, 01:49 PM
  2. dynamic array and losing data of the first element
    By fx69 in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2010, 05:15 AM
  3. DLL's to pass data
    By MotorCityMadMan in forum Windows Programming
    Replies: 3
    Last Post: 05-10-2007, 11:36 PM
  4. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  5. How to pass data between 2 Threads
    By dv007 in forum C Programming
    Replies: 2
    Last Post: 02-22-2006, 01:18 PM

Tags for this Thread